From 9fcc32051f5b09d1a7db62d452304dd4a3249328 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent Date: Wed, 2 Dec 2026 22:04:23 +0100 Subject: [PATCH v2 1/2] Allow non-relation fast-path locking This enhances the speed at which non-relation locks can be acquired, by avoiding heavyweight locks on relations. We add one bit to the fast-path lock bits indicating that a fast-path lock has overflowed, and store the additional data out-of-line in a FastPathExtra struct. This allows current Relation-based locks to keep their current optimized paths, whilst allowing non-relation locks to have similar performance benefits at only little cost (+8B/slot, or +170% vs the previous version). This will be used both by current locking workloads, and by future work on new foreign keys locking systems. --- src/backend/storage/lmgr/lmgr.c | 2 +- src/backend/storage/lmgr/lock.c | 492 ++++++++++++++++++++++++++---- src/backend/storage/lmgr/proc.c | 21 +- src/backend/utils/adt/lockfuncs.c | 10 + src/include/storage/lock.h | 9 + src/include/storage/locktag.h | 20 +- src/include/storage/proc.h | 3 +- src/tools/pgindent/typedefs.list | 1 + 8 files changed, 483 insertions(+), 75 deletions(-) diff --git a/src/backend/storage/lmgr/lmgr.c b/src/backend/storage/lmgr/lmgr.c index 2ccf7237fee..547496ef362 100644 --- a/src/backend/storage/lmgr/lmgr.c +++ b/src/backend/storage/lmgr/lmgr.c @@ -1302,8 +1302,8 @@ DescribeLockTag(StringInfo buf, const LOCKTAG *tag) case LOCKTAG_OBJECT: appendStringInfo(buf, _("object %u of class %u of database %u"), - tag->locktag_field3, tag->locktag_field2, + tag->locktag_field3, tag->locktag_field1); break; case LOCKTAG_USERLOCK: diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c index 52fdb656ae8..32ede1b0a3e 100644 --- a/src/backend/storage/lmgr/lock.c +++ b/src/backend/storage/lmgr/lock.c @@ -241,17 +241,23 @@ int FastPathLockGroupsPerBackend = 0; ((index) % FP_LOCK_SLOTS_PER_GROUP)) /* Macros for manipulating proc->fpLockBits */ -#define FAST_PATH_BITS_PER_SLOT 3 +#define FAST_PATH_BITS_PER_SLOT 4 +#define FAST_PATH_LOCKBITS_PER_SLOT 3 #define FAST_PATH_LOCKNUMBER_OFFSET 1 #define FAST_PATH_MASK ((1 << FAST_PATH_BITS_PER_SLOT) - 1) +#define FAST_PATH_LOCKMASK ((1 << FAST_PATH_LOCKBITS_PER_SLOT) - 1) #define FAST_PATH_BITS(proc, n) (proc)->fpLockBits[FAST_PATH_GROUP(n)] #define FAST_PATH_GET_BITS(proc, n) \ ((FAST_PATH_BITS(proc, n) >> (FAST_PATH_BITS_PER_SLOT * FAST_PATH_INDEX(n))) & FAST_PATH_MASK) +#define FAST_PATH_GET_LOCKBITS(proc, n) \ + (FAST_PATH_GET_BITS(proc, n) & FAST_PATH_LOCKMASK) + #define FAST_PATH_BIT_POSITION(n, l) \ (AssertMacro((l) >= FAST_PATH_LOCKNUMBER_OFFSET), \ AssertMacro((l) < FAST_PATH_BITS_PER_SLOT+FAST_PATH_LOCKNUMBER_OFFSET), \ AssertMacro((n) < FastPathLockSlotsPerBackend()), \ ((l) - FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_BITS_PER_SLOT * (FAST_PATH_INDEX(n)))) + #define FAST_PATH_SET_LOCKMODE(proc, n, l) \ FAST_PATH_BITS(proc, n) |= UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l) #define FAST_PATH_CLEAR_LOCKMODE(proc, n, l) \ @@ -259,31 +265,105 @@ int FastPathLockGroupsPerBackend = 0; #define FAST_PATH_CHECK_LOCKMODE(proc, n, l) \ (FAST_PATH_BITS(proc, n) & (UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l))) +#define FAST_PATH_SET_EXTENDED(proc, n) \ + FAST_PATH_BITS(proc, n) |= (UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, 4)) +#define FAST_PATH_CLEAR_EXTENDED(proc, n) \ + FAST_PATH_BITS(proc, n) &= ~(UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, 4)) +#define FAST_PATH_CHECK_EXTENDED(proc, n) \ + ((FAST_PATH_BITS(proc, n) & (UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, 4))) != 0) + +#define FAST_PATH_EXTRA(proc, n) (&(proc)->fpExtra[n]) + +StaticAssertDecl(sizeof(*((PGPROC *) NULL)->fpLockBits) * BITS_PER_BYTE >= FAST_PATH_BITS_PER_SLOT * FP_LOCK_SLOTS_PER_GROUP, + "Lock bits of a group must fit inside each fpLockBits slot"); + /* - * The fast-path lock mechanism is concerned only with relation locks on - * unshared relations by backends bound to a database. The fast-path + * The fast-path lock mechanism is concerned primarily with relation locks + * on unshared relations by backends bound to a database. The fast-path * mechanism exists mostly to accelerate acquisition and release of locks * that rarely conflict. Because ShareUpdateExclusiveLock is * self-conflicting, it can't use the fast-path mechanism; but it also does * not conflict with any of the locks that do, so we can ignore it completely. + * + * Relation locks are stored in abbreviated form, using only the relation ID. + * All other eligible fast-path locks store an ID in the ID array, store an + * indication that they're using extended storage, and store the extended + * information in their FastPathExtra entry. */ -#define EligibleForRelationFastPath(locktag, mode) \ - ((locktag)->locktag_lockmethodid == DEFAULT_LOCKMETHOD && \ - (locktag)->locktag_type == LOCKTAG_RELATION && \ - (locktag)->locktag_field1 == MyDatabaseId && \ - MyDatabaseId != InvalidOid && \ - (mode) < ShareUpdateExclusiveLock) -#define ConflictsWithRelationFastPath(locktag, mode) \ - ((locktag)->locktag_lockmethodid == DEFAULT_LOCKMETHOD && \ - (locktag)->locktag_type == LOCKTAG_RELATION && \ - (locktag)->locktag_field1 != InvalidOid && \ - (mode) > ShareUpdateExclusiveLock) + +static inline bool +EligibleForFastPath(const LOCKTAG *locktag, LOCKMODE mode) +{ + return locktag->locktag_lockmethodid == DEFAULT_LOCKMETHOD && + locktag->locktag_field1 == MyDatabaseId && + locktag->locktag_field2 != InvalidOid && + MyDatabaseId != InvalidOid && + mode < ShareUpdateExclusiveLock; +} + +static inline bool +ConflictsWithFastPath(const LOCKTAG *locktag, LOCKMODE mode) +{ + return locktag->locktag_lockmethodid == DEFAULT_LOCKMETHOD && + locktag->locktag_field1 != InvalidOid && + mode > ShareUpdateExclusiveLock; +} + +static inline bool +ConflictsWithRelationFastPath(const LOCKTAG *locktag, LOCKMODE mode) +{ + return locktag->locktag_type == LOCKTAG_RELATION && + ConflictsWithFastPath(locktag, mode); +} + +static inline bool +ConflictsWithExtendedFastPath(const LOCKTAG *locktag, LOCKMODE mode) +{ + return locktag->locktag_type != LOCKTAG_RELATION && + ConflictsWithFastPath(locktag, mode); +} + +static inline void +FPCopyTagToExtra(const LOCKTAG *locktag, FastPathExtra *extra) +{ + extra->locktag_field3 = locktag->locktag_field3; + extra->locktag_field4 = locktag->locktag_field4; + extra->locktag_type = locktag->locktag_type; + extra->locktag_lockmethodid = locktag->locktag_lockmethodid; +} +static inline void +CopyExtendedFastPathFromExtra(LOCKTAG *locktag, Oid dbOid, uint32 id, + const FastPathExtra *extra) +{ + Assert(MyDatabaseId != InvalidOid); + locktag->locktag_field1 = dbOid; + locktag->locktag_field2 = id; + locktag->locktag_field3 = extra->locktag_field3; + locktag->locktag_field4 = extra->locktag_field4; + locktag->locktag_type = extra->locktag_type; + locktag->locktag_lockmethodid = extra->locktag_lockmethodid; +} +static inline bool +FPExtraMatchesTag(const LOCKTAG *locktag, const FastPathExtra *extra) +{ + Assert(MyDatabaseId != InvalidOid); + return locktag->locktag_field3 == extra->locktag_field3 && + locktag->locktag_field4 == extra->locktag_field4 && + locktag->locktag_type == extra->locktag_type && + locktag->locktag_lockmethodid == extra->locktag_lockmethodid; +} + static bool FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode); +static bool FastPathGrantExtendedLock(const LOCKTAG *tag, LOCKMODE lockmode); static bool FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode); +static bool FastPathUnGrantExtendedLock(const LOCKTAG *tag, LOCKMODE lockmode); static bool FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag, uint32 hashcode); +static bool FastPathTransferExtendedLocks(LockMethod lockMethodTable, + const LOCKTAG *locktag, uint32 hashcode); static PROCLOCK *FastPathGetRelationLockEntry(LOCALLOCK *locallock); +static PROCLOCK *FastPathGetExtendedLockEntry(LOCALLOCK *locallock); /* * To make the fast-path lock mechanism work, we must have some way of @@ -477,7 +557,7 @@ LockManagerShmemRequest(void *arg) .hash_flags = HASH_ELEM | HASH_FUNCTION | HASH_PARTITION, ); - ShmemRequestStruct(.name = "Fast Path Strong Relation Lock Data", + ShmemRequestStruct(.name = "Fast Path Strong Object Lock Data", .size = mul_size(sizeof(pg_atomic_uint32), FAST_PATH_STRONG_LOCK_HASH_PARTITIONS), .ptr = (void **) (void *) &FastPathStrongRelationLocks, @@ -1019,7 +1099,7 @@ LockAcquireExtended(const LOCKTAG *locktag, * lock type on a relation we have already locked using the fast-path, but * for now we don't worry about that case either. */ - if (EligibleForRelationFastPath(locktag, lockmode)) + if (EligibleForFastPath(locktag, lockmode)) { if (FastPathLocalUseCounts[FAST_PATH_REL_GROUP(locktag->locktag_field2)] < FP_LOCK_SLOTS_PER_GROUP) @@ -1037,8 +1117,14 @@ LockAcquireExtended(const LOCKTAG *locktag, if (pg_atomic_read_u32(&FastPathStrongRelationLocks[fasthashcode]) != 0) acquired = false; else - acquired = FastPathGrantRelationLock(locktag->locktag_field2, - lockmode); + { + if (locktag->locktag_type == LOCKTAG_RELATION) + acquired = FastPathGrantRelationLock(locktag->locktag_field2, + lockmode); + else + acquired = FastPathGrantExtendedLock(locktag, lockmode); + } + LWLockRelease(&MyProc->fpInfoLock); if (acquired) { @@ -1069,13 +1155,20 @@ LockAcquireExtended(const LOCKTAG *locktag, * fast-path for this lock tag, and migrate any locks already taken via * this method to the main lock table. */ - if (ConflictsWithRelationFastPath(locktag, lockmode)) + if (ConflictsWithFastPath(locktag, lockmode)) { + bool transfered; uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode); BeginStrongLockAcquire(locallock, fasthashcode); - if (!FastPathTransferRelationLocks(lockMethodTable, locktag, - hashcode)) + if (locktag->locktag_type == LOCKTAG_RELATION) + transfered = FastPathTransferRelationLocks(lockMethodTable, + locktag, hashcode); + else + transfered = FastPathTransferExtendedLocks(lockMethodTable, + locktag, hashcode); + + if (!transfered) { AbortStrongLockAcquire(); if (locallock->nLocks == 0) @@ -2235,7 +2328,7 @@ LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock) locallock->lockCleared = false; /* Attempt fast release of any lock eligible for the fast path. */ - if (EligibleForRelationFastPath(locktag, lockmode) && + if (EligibleForFastPath(locktag, lockmode) && FastPathLocalUseCounts[FAST_PATH_REL_GROUP(locktag->locktag_field2)] > 0) { bool released; @@ -2245,9 +2338,15 @@ LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock) * here. Another backend may have moved it to the main table. */ LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE); - released = FastPathUnGrantRelationLock(locktag->locktag_field2, - lockmode); + + if (locktag->locktag_type == LOCKTAG_RELATION) + released = FastPathUnGrantRelationLock(locktag->locktag_field2, + lockmode); + else + released = FastPathUnGrantExtendedLock(locktag, lockmode); + LWLockRelease(&MyProc->fpInfoLock); + if (released) { RemoveLocalLock(locallock); @@ -2275,7 +2374,7 @@ LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock) { PROCLOCKTAG proclocktag; - Assert(EligibleForRelationFastPath(locktag, lockmode)); + Assert(EligibleForFastPath(locktag, lockmode)); lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash, locktag, locallock->hashcode, @@ -2442,10 +2541,9 @@ LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks) if (locallock->proclock == NULL || locallock->lock == NULL) { LOCKMODE lockmode = locallock->tag.mode; - Oid relid; /* Verify that a fast-path lock is what we've got. */ - if (!EligibleForRelationFastPath(&locallock->tag.lock, lockmode)) + if (!EligibleForFastPath(&locallock->tag.lock, lockmode)) elog(PANIC, "locallock table corrupted"); /* @@ -2463,11 +2561,25 @@ LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks) } /* Attempt fast-path release. */ - relid = locallock->tag.lock.locktag_field2; - if (FastPathUnGrantRelationLock(relid, lockmode)) + if (LOCALLOCK_LOCKTAG(*locallock) == LOCKTAG_RELATION) { - RemoveLocalLock(locallock); - continue; + Oid relid = locallock->tag.lock.locktag_field2; + + if (FastPathUnGrantRelationLock(relid, lockmode)) + { + RemoveLocalLock(locallock); + continue; + } + } + else + { + const LOCKTAG *tag = &locallock->tag.lock; + + if (FastPathUnGrantExtendedLock(tag, lockmode)) + { + RemoveLocalLock(locallock); + continue; + } } /* @@ -2807,11 +2919,12 @@ LockReassignOwner(LOCALLOCK *locallock, ResourceOwner parent) } /* - * FastPathGrantRelationLock + * FPGrantObjectLock * Grant lock using per-backend fast-path array, if there is space. */ -static bool -FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode) +static pg_always_inline bool +FPGrantObjectLock(Oid relid, const LOCKTAG *tag, LOCKMODE lockmode, + bool extended) { uint32 i; uint32 unused_slot = FastPathLockSlotsPerBackend(); @@ -2825,9 +2938,14 @@ FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode) /* index into the whole per-backend array */ uint32 f = FAST_PATH_SLOT(group, i); - if (FAST_PATH_GET_BITS(MyProc, f) == 0) + if (FAST_PATH_GET_LOCKBITS(MyProc, f) == 0) + { + Assert(FAST_PATH_CHECK_EXTENDED(MyProc, f) == 0); unused_slot = f; - else if (MyProc->fpRelId[f] == relid) + } + else if (extended == FAST_PATH_CHECK_EXTENDED(MyProc, f) && + MyProc->fpIds[f] == relid && + (!extended || FPExtraMatchesTag(tag, FAST_PATH_EXTRA(MyProc, f)))) { Assert(!FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode)); FAST_PATH_SET_LOCKMODE(MyProc, f, lockmode); @@ -2838,8 +2956,18 @@ FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode) /* If no existing entry, use any empty slot. */ if (unused_slot < FastPathLockSlotsPerBackend()) { - MyProc->fpRelId[unused_slot] = relid; - FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode); + if (extended) + { + FPCopyTagToExtra(tag, FAST_PATH_EXTRA(MyProc, unused_slot)); + MyProc->fpIds[unused_slot] = relid; + FAST_PATH_SET_EXTENDED(MyProc, unused_slot); + FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode); + } + else + { + MyProc->fpIds[unused_slot] = relid; + FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode); + } ++FastPathLocalUseCounts[group]; return true; } @@ -2849,12 +2977,33 @@ FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode) } /* - * FastPathUnGrantRelationLock - * Release fast-path lock, if present. Update backend-private local - * use count, while we're at it. + * FastPathGrantRelationLock + * Grant lock using per-backend fast-path array, if there is space. */ static bool -FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode) +FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode) +{ + return FPGrantObjectLock(relid, NULL, lockmode, false); +} + +/* + * FastPathGrantExtendedLock + * Grant lock using per-backend fast-path array, if there is space. + */ +static bool +FastPathGrantExtendedLock(const LOCKTAG *tag, LOCKMODE lockmode) +{ + return FPGrantObjectLock(tag->locktag_field2, tag, lockmode, true); +} + +/* + * FPUnGrantObjectLock + * Release a fast-path lock, if present. Specializes for + * relation or extended, depending on extended + */ +static pg_always_inline bool +FPUnGrantObjectLock(Oid relid, const LOCKTAG *tag, LOCKMODE lockmode, + bool extended) { uint32 i; bool result = false; @@ -2868,11 +3017,19 @@ FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode) /* index into the whole per-backend array */ uint32 f = FAST_PATH_SLOT(group, i); - if (MyProc->fpRelId[f] == relid + if (MyProc->fpIds[f] == relid + && extended == FAST_PATH_CHECK_EXTENDED(MyProc, f) + && (!extended || FPExtraMatchesTag(tag, FAST_PATH_EXTRA(MyProc, f))) && FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode)) { Assert(!result); + FAST_PATH_CLEAR_LOCKMODE(MyProc, f, lockmode); + + /* also clear the "extended" bit, if no locker remains */ + if (extended && !FAST_PATH_GET_LOCKBITS(MyProc, f)) + FAST_PATH_CLEAR_EXTENDED(MyProc, f); + result = true; /* we continue iterating so as to update FastPathLocalUseCount */ } @@ -2883,15 +3040,37 @@ FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode) } /* - * FastPathTransferRelationLocks + * FastPathUnGrantRelationLock + * Release relation's fast-path lock, if present. Update + * backend-private local use count, while we're at it. + */ +static bool +FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode) +{ + return FPUnGrantObjectLock(relid, NULL, lockmode, false); +} + +/* + * FastPathUnGrantExtendedLock + * FastPathUnGrantRelationLock, but for non-Relation objects, which + * require the FastPathExtra. + */ +static bool +FastPathUnGrantExtendedLock(const LOCKTAG *tag, LOCKMODE lockmode) +{ + return FPUnGrantObjectLock(tag->locktag_field2, tag, lockmode, true); +} + +/* + * FPTransferObjectLocks * Transfer locks matching the given lock tag from per-backend fast-path * arrays to the shared hash table. * * Returns true if successful, false if ran out of shared memory. */ -static bool -FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag, - uint32 hashcode) +static pg_always_inline bool +FPTransferObjectLocks(LockMethod lockMethodTable, const LOCKTAG *locktag, + uint32 hashcode, bool extended) { LWLock *partitionLock = LockHashPartitionLock(hashcode); Oid relid = locktag->locktag_field2; @@ -2944,14 +3123,21 @@ FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag /* index into the whole per-backend array */ uint32 f = FAST_PATH_SLOT(group, j); - /* Look for an allocated slot matching the given relid. */ - if (relid != proc->fpRelId[f] || FAST_PATH_GET_BITS(proc, f) == 0) + /* Look for an allocated slot matching the given id. */ + if (relid != proc->fpIds[f] || + FAST_PATH_GET_LOCKBITS(proc, f) == 0 || + extended != FAST_PATH_CHECK_EXTENDED(proc, f)) + continue; + + /* check the extra part if we're transferring an extended lock */ + if (extended && + !FPExtraMatchesTag(locktag, FAST_PATH_EXTRA(proc, f))) continue; /* Find or create lock object. */ LWLockAcquire(partitionLock, LW_EXCLUSIVE); for (lockmode = FAST_PATH_LOCKNUMBER_OFFSET; - lockmode < FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_BITS_PER_SLOT; + lockmode < FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_LOCKBITS_PER_SLOT; ++lockmode) { PROCLOCK *proclock; @@ -2969,6 +3155,11 @@ FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag GrantLock(proclock->tag.myLock, proclock, lockmode); FAST_PATH_CLEAR_LOCKMODE(proc, f, lockmode); } + + /* no locks left; clear the extended bit, too */ + if (extended) + FAST_PATH_CLEAR_EXTENDED(proc, f); + LWLockRelease(partitionLock); /* No need to examine remaining slots. */ @@ -2980,14 +3171,43 @@ FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag } /* - * FastPathGetRelationLockEntry + * FastPathTransferRelationLocks + * Transfer locks matching the given lock tag from per-backend fast-path + * arrays to the shared hash table. + * + * Returns true if successful, false if ran out of shared memory. + */ +static bool +FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag, + uint32 hashcode) +{ + return FPTransferObjectLocks(lockMethodTable, locktag, hashcode, false); +} + +/* + * FastPathTransferExtendedLocks + * Transfer locks matching the given lock tag from per-backend fast-path + * arrays to the shared hash table. + * + * Returns true if successful, false if ran out of shared memory. + */ +static bool +FastPathTransferExtendedLocks(LockMethod lockMethodTable, const LOCKTAG *locktag, + uint32 hashcode) +{ + return FPTransferObjectLocks(lockMethodTable, locktag, hashcode, true); +} + + +/* + * FPGetObjectLockEntry * Return the PROCLOCK for a lock originally taken via the fast-path, * transferring it to the primary lock table if necessary. * * Note: caller takes care of updating the locallock object. */ -static PROCLOCK * -FastPathGetRelationLockEntry(LOCALLOCK *locallock) +static pg_always_inline PROCLOCK * +FPGetObjectLockEntry(LOCALLOCK *locallock, bool extended) { LockMethod lockMethodTable = LockMethods[DEFAULT_LOCKMETHOD]; LOCKTAG *locktag = &locallock->tag.lock; @@ -3010,7 +3230,14 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock) uint32 f = FAST_PATH_SLOT(group, i); /* Look for an allocated slot matching the given relid. */ - if (relid != MyProc->fpRelId[f] || FAST_PATH_GET_BITS(MyProc, f) == 0) + if (relid != MyProc->fpIds[f] || + FAST_PATH_GET_BITS(MyProc, f) == 0 || + extended != FAST_PATH_CHECK_EXTENDED(MyProc, f)) + continue; + + /* and, if it's an extended lookup, that the extra data matches */ + if (extended && !FPExtraMatchesTag(&locallock->tag.lock, + FAST_PATH_EXTRA(MyProc, f))) continue; /* If we don't have a lock of the given mode, forget it! */ @@ -3035,6 +3262,9 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock) GrantLock(proclock->tag.myLock, proclock, lockmode); FAST_PATH_CLEAR_LOCKMODE(MyProc, f, lockmode); + if (extended && FAST_PATH_GET_LOCKBITS(MyProc, f) == 0) + FAST_PATH_CLEAR_EXTENDED(MyProc, f); + LWLockRelease(partitionLock); /* No need to examine remaining slots. */ @@ -3078,6 +3308,32 @@ FastPathGetRelationLockEntry(LOCALLOCK *locallock) return proclock; } +/* + * FastPathGetRelationLockEntry + * Return the PROCLOCK for a lock originally taken via the fast-path, + * transferring it to the primary lock table if necessary. + * + * Note: caller takes care of updating the locallock object. + */ +static PROCLOCK * +FastPathGetRelationLockEntry(LOCALLOCK *locallock) +{ + return FPGetObjectLockEntry(locallock, false); +} + +/* + * FastPathGetExtendedLockEntry + * Return the PROCLOCK for a lock originally taken via the fast-path, + * transferring it to the primary lock table if necessary. + * + * Note: caller takes care of updating the locallock object. + */ +static PROCLOCK * +FastPathGetExtendedLockEntry(LOCALLOCK *locallock) +{ + return FPGetObjectLockEntry(locallock, true); +} + /* * GetLockConflicts * Get an array of VirtualTransactionIds of xacts currently holding locks @@ -3198,11 +3454,17 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp) uint32 f = FAST_PATH_SLOT(group, j); /* Look for an allocated slot matching the given relid. */ - if (relid != proc->fpRelId[f]) + if (relid != proc->fpIds[f]) continue; - lockmask = FAST_PATH_GET_BITS(proc, f); + + lockmask = FAST_PATH_GET_LOCKBITS(proc, f); if (!lockmask) continue; + + /* we're only checking relation locks here */ + if (FAST_PATH_CHECK_EXTENDED(proc, f)) + continue; + lockmask <<= FAST_PATH_LOCKNUMBER_OFFSET; /* @@ -3226,6 +3488,98 @@ GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp) LWLockRelease(&proc->fpInfoLock); } } + else if (ConflictsWithExtendedFastPath(locktag, lockmode)) + { + Oid objid = locktag->locktag_field2; + VirtualTransactionId vxid; + + /* fast-path group the lock belongs to */ + uint32 group = FAST_PATH_REL_GROUP(objid); + + /* + * Iterate over relevant PGPROCs. Anything held by a prepared + * transaction will have been transferred to the primary lock table, + * so we need not worry about those. This is all a bit fuzzy, because + * new locks could be taken after we've visited a particular + * partition, but the callers had better be prepared to deal with that + * anyway, since the locks could equally well be taken between the + * time we return the value and the time the caller does something + * with it. + */ + for (uint32 i = 0; i < ProcGlobal->allProcCount; i++) + { + PGPROC *proc = GetPGProcByNumber(i); + uint32 j; + + /* A backend never blocks itself */ + if (proc == MyProc) + continue; + + LWLockAcquire(&proc->fpInfoLock, LW_SHARED); + + /* + * If the target backend isn't referencing the same database as + * the lock, then we needn't examine the individual relation IDs + * at all; none of them can be relevant. + * + * See FPTransferObjectLocks() for discussion of why we do + * this test after acquiring the lock. + * + * Also skip groups without any registered fast-path locks. + */ + if (proc->databaseId != locktag->locktag_field1 || + proc->fpLockBits[group] == 0) + { + LWLockRelease(&proc->fpInfoLock); + continue; + } + + for (j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++) + { + uint32 lockmask; + + /* index into the whole per-backend array */ + uint32 f = FAST_PATH_SLOT(group, j); + + /* Look for an allocated slot matching the given relid. */ + if (objid != proc->fpIds[f]) + continue; + + lockmask = FAST_PATH_GET_LOCKBITS(proc, f); + if (!lockmask) + continue; + + /* we're only checking extended locks here */ + if (!FAST_PATH_CHECK_EXTENDED(proc, f)) + continue; + + /* if the extended data doesn't match, skip */ + if (!FPExtraMatchesTag(locktag, FAST_PATH_EXTRA(proc, f))) + continue; + + lockmask <<= FAST_PATH_LOCKNUMBER_OFFSET; + + /* + * There can only be one entry per object, so if we found it + * and it doesn't conflict, we can skip the rest of the slots. + */ + if ((lockmask & conflictMask) == 0) + break; + + /* Conflict! */ + GET_VXID_FROM_PGPROC(vxid, *proc); + + if (VirtualTransactionIdIsValid(vxid)) + vxids[count++] = vxid; + /* else, xact already committed or aborted */ + + /* No need to examine remaining slots. */ + break; + } + + LWLockRelease(&proc->fpInfoLock); + } + } /* Remember how many fast-path conflicts we found. */ fast_count = count; @@ -3384,7 +3738,7 @@ LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc, * Decrement strong lock count. This logic is needed only for 2PC. */ if (decrement_strong_lock_count - && ConflictsWithRelationFastPath(locktag, lockmode)) + && ConflictsWithFastPath(locktag, lockmode)) { uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode); @@ -3560,7 +3914,10 @@ AtPrepare_Locks(void) */ if (locallock->proclock == NULL) { - locallock->proclock = FastPathGetRelationLockEntry(locallock); + if (LOCALLOCK_LOCKTAG(*locallock) == LOCKTAG_RELATION) + locallock->proclock = FastPathGetRelationLockEntry(locallock); + else + locallock->proclock = FastPathGetExtendedLockEntry(locallock); locallock->lock = locallock->proclock->tag.myLock; } @@ -3841,7 +4198,8 @@ GetLockStatusData(void) { LockInstanceData *instance; uint32 f = FAST_PATH_SLOT(g, j); - uint32 lockbits = FAST_PATH_GET_BITS(proc, f); + uint32 extended = FAST_PATH_CHECK_EXTENDED(proc, f); + uint32 lockbits = FAST_PATH_GET_LOCKBITS(proc, f); /* Skip unallocated slots */ if (!lockbits) @@ -3854,8 +4212,18 @@ GetLockStatusData(void) } instance = &data->locks[el]; - SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId, - proc->fpRelId[f]); + if (extended) + { + CopyExtendedFastPathFromExtra(&instance->locktag, + proc->databaseId, + proc->fpIds[f], + FAST_PATH_EXTRA(proc, f)); + } + else + { + SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId, + proc->fpIds[f]); + } instance->holdMask = lockbits << FAST_PATH_LOCKNUMBER_OFFSET; instance->waitLockMode = NoLock; instance->vxid.procNumber = proc->vxid.procNumber; @@ -4515,7 +4883,7 @@ lock_twophase_recover(FullTransactionId fxid, uint16 info, * Bump strong lock count, to make sure any fast-path lock requests won't * be granted without consulting the primary lock table. */ - if (ConflictsWithRelationFastPath(&lock->tag, lockmode)) + if (ConflictsWithFastPath(&lock->tag, lockmode)) { uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode); diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index a168bac3bc9..4f22b30f33a 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -105,16 +105,18 @@ CalculateFastPathLockShmemSize(void) { Size size = 0; Size fpLockBitsSize, - fpRelIdSize; + fpIdsSize, + fpExtraSize; /* * Memory needed for PGPROC fast-path lock arrays. Make sure the sizes are * nicely aligned in each backend. */ fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64)); - fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid)); + fpIdsSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid)); + fpExtraSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(FastPathExtra)); - size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpRelIdSize))); + size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpIdsSize + fpExtraSize))); Assert(TotalProcs > 0); Assert(size > 0); @@ -229,7 +231,8 @@ ProcGlobalShmemInit(void *arg) char *fpPtr, *fpEndPtr PG_USED_FOR_ASSERTS_ONLY; Size fpLockBitsSize, - fpRelIdSize; + fpIdsSize, + fpExtraSize; Assert(ProcGlobal); ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY; @@ -282,7 +285,8 @@ ProcGlobalShmemInit(void *arg) * shared memory and then divide that between backends. */ fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64)); - fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid)); + fpIdsSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid)); + fpExtraSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(FastPathExtra)); fpPtr = FastPathLockArrayShmemPtr; requestSize = FastPathLockArrayShmemSize; @@ -307,8 +311,11 @@ ProcGlobalShmemInit(void *arg) proc->fpLockBits = (uint64 *) fpPtr; fpPtr += fpLockBitsSize; - proc->fpRelId = (Oid *) fpPtr; - fpPtr += fpRelIdSize; + proc->fpIds = (Oid *) fpPtr; + fpPtr += fpIdsSize; + + proc->fpExtra = (FastPathExtra *) fpPtr; + fpPtr += fpExtraSize; Assert(fpPtr <= fpEndPtr); diff --git a/src/backend/utils/adt/lockfuncs.c b/src/backend/utils/adt/lockfuncs.c index 285a7c4b532..92fd4e5b364 100644 --- a/src/backend/utils/adt/lockfuncs.c +++ b/src/backend/utils/adt/lockfuncs.c @@ -338,6 +338,16 @@ pg_lock_status(PG_FUNCTION_ARGS) nulls[7] = true; break; case LOCKTAG_OBJECT: + values[1] = ObjectIdGetDatum(instance->locktag.locktag_field1); + values[7] = ObjectIdGetDatum(instance->locktag.locktag_field3); + values[8] = ObjectIdGetDatum(instance->locktag.locktag_field2); + values[9] = Int16GetDatum(instance->locktag.locktag_field4); + nulls[2] = true; + nulls[3] = true; + nulls[4] = true; + nulls[5] = true; + nulls[6] = true; + break; case LOCKTAG_USERLOCK: case LOCKTAG_ADVISORY: default: /* treat unknown locktags like OBJECT */ diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h index c63c9075466..0a3f59984dd 100644 --- a/src/include/storage/lock.h +++ b/src/include/storage/lock.h @@ -274,6 +274,15 @@ typedef struct LOCALLOCK #define LOCALLOCK_LOCKMETHOD(llock) ((llock).tag.lock.locktag_lockmethodid) #define LOCALLOCK_LOCKTAG(llock) ((LockTagType) (llock).tag.lock.locktag_type) +typedef struct FastPathExtra +{ + /* field1 is implied by backend's database */ + /* field2 is stored in the main fast-path ID slots */ + uint32 locktag_field3; /* a 32-bit ID field */ + uint16 locktag_field4; /* a 16-bit ID field */ + uint8 locktag_type; /* see enum LockTagType */ + uint8 locktag_lockmethodid; /* lockmethod indicator */ +} FastPathExtra; /* * These structures hold information passed from lmgr internals to the lock diff --git a/src/include/storage/locktag.h b/src/include/storage/locktag.h index 6b122497ec6..690b8296a11 100644 --- a/src/include/storage/locktag.h +++ b/src/include/storage/locktag.h @@ -60,6 +60,16 @@ extern PGDLLIMPORT const char *const LockTagTypeNames[]; * * We include lockmethodid in the locktag so that a single hash table in * shared memory can store locks of different lockmethods. + * + * To be eligible for fast-path locking: + * - the default lock method must be used; + * - locks should not be taken across database boundaries; + * - locktag_field1 must contain the database OID; and + * - locktag_field2 should contain a high-entropy identifier for the + * locked object. + * + * The fast-path locks can then lock the object without having to lock the + * shared lock table exclusively for low-level locks. */ typedef struct LOCKTAG { @@ -155,14 +165,16 @@ typedef struct LOCKTAG /* * ID info for an object is DB OID + CLASS OID + OBJECT OID + SUBID * - * Note: object ID has same representation as in pg_depend and + * Note: object ID has comparable representation as in pg_depend and * pg_description, but notice that we are constraining SUBID to 16 bits. - * Also, we use DB OID = 0 for shared objects such as tablespaces. + * Also, we use DB OID = 0 for shared objects such as tablespaces, and + * store objoid before classoid, so that fast-path locks use the objoid + * as primary identity, rather than classoid. */ #define SET_LOCKTAG_OBJECT(locktag,dboid,classoid,objoid,objsubid) \ ((locktag).locktag_field1 = (dboid), \ - (locktag).locktag_field2 = (classoid), \ - (locktag).locktag_field3 = (objoid), \ + (locktag).locktag_field2 = (objoid), \ + (locktag).locktag_field3 = (classoid), \ (locktag).locktag_field4 = (objsubid), \ (locktag).locktag_type = LOCKTAG_OBJECT, \ (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index abe40001d9a..96a326ff711 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -330,7 +330,8 @@ typedef struct PGPROC /*-- recording fast-path locks taken by this backend. --*/ LWLock fpInfoLock; /* protects per-backend fast-path state */ uint64 *fpLockBits; /* lock modes held for each fast-path slot */ - Oid *fpRelId; /* slots for rel oids */ + Oid *fpIds; /* slots for rel oids, or other object's ids */ + FastPathExtra *fpExtra; /* slots for extended lock data */ bool fpVXIDLock; /* are we holding a fast-path VXID lock? */ LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID * lock */ diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index f10eb469266..da27de3494c 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -842,6 +842,7 @@ FSMPage FSMPageData FakeRelCacheEntry FakeRelCacheEntryData +FastPathExtra FastPathMeta FdwInfo FdwRoutine -- 2.54.0 (Apple Git-157)