From 62c7bd31c8878dd45c9b9b2429ab7a12103f3590 Mon Sep 17 00:00:00 2001 From: Itagaki Takahiro Date: Fri, 18 Feb 2011 14:04:34 +0900 Subject: Add transaction-level advisory locks. They share the same locking namespace with the existing session-level advisory locks, but they are automatically released at the end of the current transaction and cannot be released explicitly via unlock functions. Marko Tiikkaja, reviewed by me. --- src/backend/utils/adt/lockfuncs.c | 154 +++++++++++++++++++++++++++++++++++++- 1 file changed, 153 insertions(+), 1 deletion(-) (limited to 'src/backend/utils/adt/lockfuncs.c') diff --git a/src/backend/utils/adt/lockfuncs.c b/src/backend/utils/adt/lockfuncs.c index 8e369826cec..c6c948ce5e7 100644 --- a/src/backend/utils/adt/lockfuncs.c +++ b/src/backend/utils/adt/lockfuncs.c @@ -421,6 +421,23 @@ pg_advisory_lock_int8(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } +/* + * 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 */ @@ -437,6 +454,23 @@ pg_advisory_lock_shared_int8(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } +/* + * 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 * @@ -456,6 +490,26 @@ pg_try_advisory_lock_int8(PG_FUNCTION_ARGS) PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL); } +/* + * 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 * @@ -475,6 +529,26 @@ pg_try_advisory_lock_shared_int8(PG_FUNCTION_ARGS) PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL); } +/* + * 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 * @@ -530,6 +604,24 @@ pg_advisory_lock_int4(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } +/* + * 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 */ @@ -547,6 +639,24 @@ pg_advisory_lock_shared_int4(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } +/* + * 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 * @@ -567,6 +677,27 @@ pg_try_advisory_lock_int4(PG_FUNCTION_ARGS) PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL); } +/* + * 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 * @@ -587,6 +718,27 @@ pg_try_advisory_lock_shared_int4(PG_FUNCTION_ARGS) PG_RETURN_BOOL(res != LOCKACQUIRE_NOT_AVAIL); } +/* + * 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 * @@ -633,7 +785,7 @@ pg_advisory_unlock_shared_int4(PG_FUNCTION_ARGS) Datum pg_advisory_unlock_all(PG_FUNCTION_ARGS) { - LockReleaseAll(USER_LOCKMETHOD, true); + LockReleaseSession(USER_LOCKMETHOD); PG_RETURN_VOID(); } -- cgit v1.2.3