advisory.patch

text/plain

Filename: advisory.patch
Type: text/plain
Part: 0
Message: Re: Transaction-scope advisory locks

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: context
File+
src/backend/storage/lmgr/lock.c 1 0
src/backend/storage/lmgr/proc.c 1 0
src/backend/utils/adt/lockfuncs.c 152 0
src/include/catalog/pg_proc.h 16 0
src/include/utils/builtins.h 8 0
*** a/src/backend/storage/lmgr/lock.c
--- b/src/backend/storage/lmgr/lock.c
***************
*** 130,136 **** static const LockMethodData default_lockmethod = {
  
  static const LockMethodData user_lockmethod = {
  	AccessExclusiveLock,		/* highest valid lock mode number */
! 	false,
  	LockConflicts,
  	lock_mode_names,
  #ifdef LOCK_DEBUG
--- 130,136 ----
  
  static const LockMethodData user_lockmethod = {
  	AccessExclusiveLock,		/* highest valid lock mode number */
! 	true,
  	LockConflicts,
  	lock_mode_names,
  #ifdef LOCK_DEBUG
*** a/src/backend/storage/lmgr/proc.c
--- b/src/backend/storage/lmgr/proc.c
***************
*** 629,636 **** LockWaitCancel(void)
   * At subtransaction abort, we release all locks held by the subtransaction;
   * this is implemented by retail releasing of the locks under control of
   * the ResourceOwner mechanism.
-  *
-  * Note that user locks are not released in any case.
   */
  void
  ProcReleaseLocks(bool isCommit)
--- 629,634 ----
***************
*** 641,646 **** ProcReleaseLocks(bool isCommit)
--- 639,645 ----
  	LockWaitCancel();
  	/* Release locks */
  	LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
+ 	LockReleaseAll(USER_LOCKMETHOD, false);
  }
  
  
*** a/src/backend/utils/adt/lockfuncs.c
--- b/src/backend/utils/adt/lockfuncs.c
***************
*** 343,348 **** pg_advisory_lock_int8(PG_FUNCTION_ARGS)
--- 343,365 ----
  }
  
  /*
+  * pg_advisory_xact_lock(int8) - acquire xact scoped
+  * exclusive lock on an int8 key
+  */
+ Datum
+ pg_advisory_xact_lock_int8(PG_FUNCTION_ARGS)
+ {
+ 	int64		key = PG_GETARG_INT64(0);
+ 	LOCKTAG		tag;
+ 
+ 	SET_LOCKTAG_INT64(tag, key);
+ 
+ 	(void) LockAcquire(&tag, ExclusiveLock, false, false);
+ 
+ 	PG_RETURN_VOID();
+ }
+ 
+ /*
   * pg_advisory_lock_shared(int8) - acquire share lock on an int8 key
   */
  Datum
***************
*** 359,364 **** pg_advisory_lock_shared_int8(PG_FUNCTION_ARGS)
--- 376,398 ----
  }
  
  /*
+  * pg_advisory_xact_lock_shared(int8) - acquire xact scoped
+  * share lock on an int8 key
+  */
+ Datum
+ pg_advisory_xact_lock_shared_int8(PG_FUNCTION_ARGS)
+ {
+ 	int64		key = PG_GETARG_INT64(0);
+ 	LOCKTAG		tag;
+ 
+ 	SET_LOCKTAG_INT64(tag, key);
+ 
+ 	(void) LockAcquire(&tag, ShareLock, false, false);
+ 
+ 	PG_RETURN_VOID();
+ }
+ 
+ /*
   * pg_try_advisory_lock(int8) - acquire exclusive lock on an int8 key, no wait
   *
   * Returns true if successful, false if lock not available
***************
*** 378,383 **** pg_try_advisory_lock_int8(PG_FUNCTION_ARGS)
--- 412,437 ----
  }
  
  /*
+  * pg_try_advisory_xact_lock(int8) - acquire xact scoped
+  * exclusive lock on an int8 key, no wait
+  *
+  * Returns true if successful, false if lock not available
+  */
+ Datum
+ pg_try_advisory_xact_lock_int8(PG_FUNCTION_ARGS)
+ {
+ 	int64		key = PG_GETARG_INT64(0);
+ 	LOCKTAG		tag;
+ 	LockAcquireResult res;
+ 
+ 	SET_LOCKTAG_INT64(tag, key);
+ 
+ 	res = LockAcquire(&tag, ExclusiveLock, false, true);
+ 
+ 	PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL);
+ }
+ 
+ /*
   * pg_try_advisory_lock_shared(int8) - acquire share lock on an int8 key, no wait
   *
   * Returns true if successful, false if lock not available
***************
*** 397,402 **** pg_try_advisory_lock_shared_int8(PG_FUNCTION_ARGS)
--- 451,476 ----
  }
  
  /*
+  * pg_try_advisory_xact_lock_shared(int8) - acquire xact scoped
+  * share lock on an int8 key, no wait
+  *
+  * Returns true if successful, false if lock not available
+  */
+ Datum
+ pg_try_advisory_xact_lock_shared_int8(PG_FUNCTION_ARGS)
+ {
+ 	int64		key = PG_GETARG_INT64(0);
+ 	LOCKTAG		tag;
+ 	LockAcquireResult res;
+ 
+ 	SET_LOCKTAG_INT64(tag, key);
+ 
+ 	res = LockAcquire(&tag, ShareLock, false, true);
+ 
+ 	PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL);
+ }
+ 
+ /*
   * pg_advisory_unlock(int8) - release exclusive lock on an int8 key
   *
   * Returns true if successful, false if lock was not held
***************
*** 452,457 **** pg_advisory_lock_int4(PG_FUNCTION_ARGS)
--- 526,549 ----
  }
  
  /*
+  * pg_advisory_xact_lock(int4, int4) - acquire xact scoped
+  * exclusive lock on 2 int4 keys
+  */
+ Datum
+ pg_advisory_xact_lock_int4(PG_FUNCTION_ARGS)
+ {
+ 	int32		key1 = PG_GETARG_INT32(0);
+ 	int32		key2 = PG_GETARG_INT32(1);
+ 	LOCKTAG		tag;
+ 
+ 	SET_LOCKTAG_INT32(tag, key1, key2);
+ 
+ 	(void) LockAcquire(&tag, ExclusiveLock, false, false);
+ 
+ 	PG_RETURN_VOID();
+ }
+ 
+ /*
   * pg_advisory_lock_shared(int4, int4) - acquire share lock on 2 int4 keys
   */
  Datum
***************
*** 469,474 **** pg_advisory_lock_shared_int4(PG_FUNCTION_ARGS)
--- 561,584 ----
  }
  
  /*
+  * pg_advisory_xact_lock_shared(int4, int4) - acquire xact scoped
+  * share lock on 2 int4 keys
+  */
+ Datum
+ pg_advisory_xact_lock_shared_int4(PG_FUNCTION_ARGS)
+ {
+ 	int32		key1 = PG_GETARG_INT32(0);
+ 	int32		key2 = PG_GETARG_INT32(1);
+ 	LOCKTAG		tag;
+ 
+ 	SET_LOCKTAG_INT32(tag, key1, key2);
+ 
+ 	(void) LockAcquire(&tag, ShareLock, false, false);
+ 
+ 	PG_RETURN_VOID();
+ }
+ 
+ /*
   * pg_try_advisory_lock(int4, int4) - acquire exclusive lock on 2 int4 keys, no wait
   *
   * Returns true if successful, false if lock not available
***************
*** 489,494 **** pg_try_advisory_lock_int4(PG_FUNCTION_ARGS)
--- 599,625 ----
  }
  
  /*
+  * pg_try_advisory_xact_lock(int4, int4) - acquire xact scoped
+  * exclusive lock on 2 int4 keys, no wait
+  *
+  * Returns true if successful, false if lock not available
+  */
+ Datum
+ pg_try_advisory_xact_lock_int4(PG_FUNCTION_ARGS)
+ {
+ 	int32		key1 = PG_GETARG_INT32(0);
+ 	int32		key2 = PG_GETARG_INT32(1);
+ 	LOCKTAG		tag;
+ 	LockAcquireResult res;
+ 
+ 	SET_LOCKTAG_INT32(tag, key1, key2);
+ 
+ 	res = LockAcquire(&tag, ExclusiveLock, false, true);
+ 
+ 	PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL);
+ }
+ 
+ /*
   * pg_try_advisory_lock_shared(int4, int4) - acquire share lock on 2 int4 keys, no wait
   *
   * Returns true if successful, false if lock not available
***************
*** 509,514 **** pg_try_advisory_lock_shared_int4(PG_FUNCTION_ARGS)
--- 640,666 ----
  }
  
  /*
+  * pg_try_advisory_xact_lock_shared(int4, int4) - acquire xact scoped
+  * share lock on 2 int4 keys, no wait
+  *
+  * Returns true if successful, false if lock not available
+  */
+ Datum
+ pg_try_advisory_xact_lock_shared_int4(PG_FUNCTION_ARGS)
+ {
+ 	int32		key1 = PG_GETARG_INT32(0);
+ 	int32		key2 = PG_GETARG_INT32(1);
+ 	LOCKTAG		tag;
+ 	LockAcquireResult res;
+ 
+ 	SET_LOCKTAG_INT32(tag, key1, key2);
+ 
+ 	res = LockAcquire(&tag, ShareLock, false, true);
+ 
+ 	PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL);
+ }
+ 
+ /*
   * pg_advisory_unlock(int4, int4) - release exclusive lock on 2 int4 keys
   *
   * Returns true if successful, false if lock was not held
*** a/src/include/catalog/pg_proc.h
--- b/src/include/catalog/pg_proc.h
***************
*** 4404,4428 **** DESCR("is contained by");
--- 4404,4444 ----
  
  /* userlock replacements */
  DATA(insert OID = 2880 (  pg_advisory_lock				PGNSP PGUID 12 1 0 0 f f f t f v 1 0 2278 "20" _null_ _null_ _null_ _null_ pg_advisory_lock_int8 _null_ _null_ _null_ ));
+ DESCR("obtain exclusive a4dvisory lock");
+ DATA(insert OID = 3071 (  pg_advisory_xact_lock				PGNSP PGUID 12 1 0 0 f f f t f v 1 0 2278 "20" _null_ _null_ _null_ _null_ pg_advisory_xact_lock_int8 _null_ _null_ _null_ ));
  DESCR("obtain exclusive advisory lock");
  DATA(insert OID = 2881 (  pg_advisory_lock_shared		PGNSP PGUID 12 1 0 0 f f f t f v 1 0 2278 "20" _null_ _null_ _null_ _null_ pg_advisory_lock_shared_int8 _null_ _null_ _null_ ));
  DESCR("obtain shared advisory lock");
+ DATA(insert OID = 3072 (  pg_advisory_xact_lock_shared		PGNSP PGUID 12 1 0 0 f f f t f v 1 0 2278 "20" _null_ _null_ _null_ _null_ pg_advisory_xact_lock_shared_int8 _null_ _null_ _null_ ));
+ DESCR("obtain shared advisory lock");
  DATA(insert OID = 2882 (  pg_try_advisory_lock			PGNSP PGUID 12 1 0 0 f f f t f v 1 0 16 "20" _null_ _null_ _null_ _null_ pg_try_advisory_lock_int8 _null_ _null_ _null_ ));
  DESCR("obtain exclusive advisory lock if available");
+ DATA(insert OID = 3073 (  pg_try_advisory_xact_lock			PGNSP PGUID 12 1 0 0 f f f t f v 1 0 16 "20" _null_ _null_ _null_ _null_ pg_try_advisory_xact_lock_int8 _null_ _null_ _null_ ));
+ DESCR("obtain exclusive advisory lock if available");
  DATA(insert OID = 2883 (  pg_try_advisory_lock_shared	PGNSP PGUID 12 1 0 0 f f f t f v 1 0 16 "20" _null_ _null_ _null_ _null_ pg_try_advisory_lock_shared_int8 _null_ _null_ _null_ ));
  DESCR("obtain shared advisory lock if available");
+ DATA(insert OID = 3074 (  pg_try_advisory_xact_lock_shared	PGNSP PGUID 12 1 0 0 f f f t f v 1 0 16 "20" _null_ _null_ _null_ _null_ pg_try_advisory_xact_lock_shared_int8 _null_ _null_ _null_ ));
+ DESCR("obtain shared advisory lock if available");
  DATA(insert OID = 2884 (  pg_advisory_unlock			PGNSP PGUID 12 1 0 0 f f f t f v 1 0 16 "20" _null_ _null_ _null_ _null_ pg_advisory_unlock_int8 _null_ _null_ _null_ ));
  DESCR("release exclusive advisory lock");
  DATA(insert OID = 2885 (  pg_advisory_unlock_shared		PGNSP PGUID 12 1 0 0 f f f t f v 1 0 16 "20" _null_ _null_ _null_ _null_ pg_advisory_unlock_shared_int8 _null_ _null_ _null_ ));
  DESCR("release shared advisory lock");
  DATA(insert OID = 2886 (  pg_advisory_lock				PGNSP PGUID 12 1 0 0 f f f t f v 2 0 2278 "23 23" _null_ _null_ _null_ _null_ pg_advisory_lock_int4 _null_ _null_ _null_ ));
  DESCR("obtain exclusive advisory lock");
+ DATA(insert OID = 3075 (  pg_advisory_xact_lock				PGNSP PGUID 12 1 0 0 f f f t f v 2 0 2278 "23 23" _null_ _null_ _null_ _null_ pg_advisory_xact_lock_int4 _null_ _null_ _null_ ));
+ DESCR("obtain exclusive advisory lock");
  DATA(insert OID = 2887 (  pg_advisory_lock_shared		PGNSP PGUID 12 1 0 0 f f f t f v 2 0 2278 "23 23" _null_ _null_ _null_ _null_ pg_advisory_lock_shared_int4 _null_ _null_ _null_ ));
  DESCR("obtain shared advisory lock");
+ DATA(insert OID = 3076 (  pg_advisory_xact_lock_shared		PGNSP PGUID 12 1 0 0 f f f t f v 2 0 2278 "23 23" _null_ _null_ _null_ _null_ pg_advisory_xact_lock_shared_int4 _null_ _null_ _null_ ));
+ DESCR("obtain shared advisory lock");
  DATA(insert OID = 2888 (  pg_try_advisory_lock			PGNSP PGUID 12 1 0 0 f f f t f v 2 0 16 "23 23" _null_ _null_ _null_ _null_ pg_try_advisory_lock_int4 _null_ _null_ _null_ ));
  DESCR("obtain exclusive advisory lock if available");
+ DATA(insert OID = 3077 (  pg_try_advisory_xact_lock			PGNSP PGUID 12 1 0 0 f f f t f v 2 0 16 "23 23" _null_ _null_ _null_ _null_ pg_try_advisory_xact_lock_int4 _null_ _null_ _null_ ));
+ DESCR("obtain exclusive advisory lock if available");
  DATA(insert OID = 2889 (  pg_try_advisory_lock_shared	PGNSP PGUID 12 1 0 0 f f f t f v 2 0 16 "23 23" _null_ _null_ _null_ _null_ pg_try_advisory_lock_shared_int4 _null_ _null_ _null_ ));
  DESCR("obtain shared advisory lock if available");
+ DATA(insert OID = 3079 (  pg_try_advisory_xact_lock_shared	PGNSP PGUID 12 1 0 0 f f f t f v 2 0 16 "23 23" _null_ _null_ _null_ _null_ pg_try_advisory_xact_lock_shared_int4 _null_ _null_ _null_ ));
+ DESCR("obtain shared advisory lock if available");
  DATA(insert OID = 2890 (  pg_advisory_unlock			PGNSP PGUID 12 1 0 0 f f f t f v 2 0 16 "23 23" _null_ _null_ _null_ _null_ pg_advisory_unlock_int4 _null_ _null_ _null_ ));
  DESCR("release exclusive advisory lock");
  DATA(insert OID = 2891 (  pg_advisory_unlock_shared		PGNSP PGUID 12 1 0 0 f f f t f v 2 0 16 "23 23" _null_ _null_ _null_ _null_ pg_advisory_unlock_shared_int4 _null_ _null_ _null_ ));
*** a/src/include/utils/builtins.h
--- b/src/include/utils/builtins.h
***************
*** 991,1005 **** extern Datum show_all_settings(PG_FUNCTION_ARGS);
--- 991,1013 ----
  /* lockfuncs.c */
  extern Datum pg_lock_status(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_lock_int8(PG_FUNCTION_ARGS);
+ extern Datum pg_advisory_xact_lock_int8(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_lock_shared_int8(PG_FUNCTION_ARGS);
+ extern Datum pg_advisory_xact_lock_shared_int8(PG_FUNCTION_ARGS);
  extern Datum pg_try_advisory_lock_int8(PG_FUNCTION_ARGS);
+ extern Datum pg_try_advisory_xact_lock_int8(PG_FUNCTION_ARGS);
  extern Datum pg_try_advisory_lock_shared_int8(PG_FUNCTION_ARGS);
+ extern Datum pg_try_advisory_xact_lock_shared_int8(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_unlock_int8(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_unlock_shared_int8(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_lock_int4(PG_FUNCTION_ARGS);
+ extern Datum pg_advisory_xact_lock_int4(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_lock_shared_int4(PG_FUNCTION_ARGS);
+ extern Datum pg_advisory_xact_lock_shared_int4(PG_FUNCTION_ARGS);
  extern Datum pg_try_advisory_lock_int4(PG_FUNCTION_ARGS);
+ extern Datum pg_try_advisory_xact_lock_int4(PG_FUNCTION_ARGS);
  extern Datum pg_try_advisory_lock_shared_int4(PG_FUNCTION_ARGS);
+ extern Datum pg_try_advisory_xact_lock_shared_int4(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_unlock_int4(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_unlock_shared_int4(PG_FUNCTION_ARGS);
  extern Datum pg_advisory_unlock_all(PG_FUNCTION_ARGS);