PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
lock.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * lock.c
4 * POSTGRES primary lock mechanism
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/storage/lmgr/lock.c
12 *
13 * NOTES
14 * A lock table is a shared memory hash table. When
15 * a process tries to acquire a lock of a type that conflicts
16 * with existing locks, it is put to sleep using the routines
17 * in storage/lmgr/proc.c.
18 *
19 * For the most part, this code should be invoked via lmgr.c
20 * or another lock-management module, not directly.
21 *
22 * Interface:
23 *
24 * LockManagerShmemInit(), GetLocksMethodTable(), GetLockTagsMethodTable(),
25 * LockAcquire(), LockRelease(), LockReleaseAll(),
26 * LockCheckConflicts(), GrantLock()
27 *
28 *-------------------------------------------------------------------------
29 */
30#include "postgres.h"
31
32#include <signal.h>
33#include <unistd.h>
34
35#include "access/transam.h"
36#include "access/twophase.h"
38#include "access/xlog.h"
39#include "access/xlogutils.h"
40#include "miscadmin.h"
41#include "pg_trace.h"
42#include "storage/lmgr.h"
43#include "storage/proc.h"
44#include "storage/procarray.h"
45#include "storage/spin.h"
46#include "storage/standby.h"
47#include "utils/memutils.h"
48#include "utils/ps_status.h"
49#include "utils/resowner.h"
50
51
52/* GUC variables */
53int max_locks_per_xact; /* used to set the lock table size */
54bool log_lock_failure = false;
55
56#define NLOCKENTS() \
57 mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
58
59
60/*
61 * Data structures defining the semantics of the standard lock methods.
62 *
63 * The conflict table defines the semantics of the various lock modes.
64 */
65static const LOCKMASK LockConflicts[] = {
66 0,
67
68 /* AccessShareLock */
70
71 /* RowShareLock */
73
74 /* RowExclusiveLock */
77
78 /* ShareUpdateExclusiveLock */
82
83 /* ShareLock */
87
88 /* ShareRowExclusiveLock */
92
93 /* ExclusiveLock */
98
99 /* AccessExclusiveLock */
104
105};
106
107/* Names of lock modes, for debug printouts */
108static const char *const lock_mode_names[] =
109{
110 "INVALID",
111 "AccessShareLock",
112 "RowShareLock",
113 "RowExclusiveLock",
114 "ShareUpdateExclusiveLock",
115 "ShareLock",
116 "ShareRowExclusiveLock",
117 "ExclusiveLock",
118 "AccessExclusiveLock"
119};
120
121#ifndef LOCK_DEBUG
122static bool Dummy_trace = false;
123#endif
124
129#ifdef LOCK_DEBUG
130 &Trace_locks
131#else
133#endif
134};
135
140#ifdef LOCK_DEBUG
141 &Trace_userlocks
142#else
144#endif
145};
146
147/*
148 * map from lock method id to the lock table data structures
149 */
150static const LockMethod LockMethods[] = {
151 NULL,
154};
155
156
157/* Record that's written to 2PC state file when a lock is persisted */
158typedef struct TwoPhaseLockRecord
159{
163
164
165/*
166 * Count of the number of fast path lock slots we believe to be used. This
167 * might be higher than the real number if another backend has transferred
168 * our locks to the primary lock table, but it can never be lower than the
169 * real value, since only we can acquire locks on our own behalf.
170 *
171 * XXX Allocate a static array of the maximum size. We could use a pointer
172 * and then allocate just the right size to save a couple kB, but then we
173 * would have to initialize that, while for the static array that happens
174 * automatically. Doesn't seem worth the extra complexity.
175 */
177
178/*
179 * Flag to indicate if the relation extension lock is held by this backend.
180 * This flag is used to ensure that while holding the relation extension lock
181 * we don't try to acquire a heavyweight lock on any other object. This
182 * restriction implies that the relation extension lock won't ever participate
183 * in the deadlock cycle because we can never wait for any other heavyweight
184 * lock after acquiring this lock.
185 *
186 * Such a restriction is okay for relation extension locks as unlike other
187 * heavyweight locks these are not held till the transaction end. These are
188 * taken for a short duration to extend a particular relation and then
189 * released.
190 */
191static bool IsRelationExtensionLockHeld PG_USED_FOR_ASSERTS_ONLY = false;
192
193/*
194 * Number of fast-path locks per backend - size of the arrays in PGPROC.
195 * This is set only once during start, before initializing shared memory,
196 * and remains constant after that.
197 *
198 * We set the limit based on max_locks_per_transaction GUC, because that's
199 * the best information about expected number of locks per backend we have.
200 * See InitializeFastPathLocks() for details.
201 */
203
204/*
205 * Macros to calculate the fast-path group and index for a relation.
206 *
207 * The formula is a simple hash function, designed to spread the OIDs a bit,
208 * so that even contiguous values end up in different groups. In most cases
209 * there will be gaps anyway, but the multiplication should help a bit.
210 *
211 * The selected constant (49157) is a prime not too close to 2^k, and it's
212 * small enough to not cause overflows (in 64-bit).
213 */
214#define FAST_PATH_REL_GROUP(rel) \
215 (((uint64) (rel) * 49157) % FastPathLockGroupsPerBackend)
216
217/*
218 * Given the group/slot indexes, calculate the slot index in the whole array
219 * of fast-path lock slots.
220 */
221#define FAST_PATH_SLOT(group, index) \
222 (AssertMacro((uint32) (group) < FastPathLockGroupsPerBackend), \
223 AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_GROUP), \
224 ((group) * FP_LOCK_SLOTS_PER_GROUP + (index)))
225
226/*
227 * Given a slot index (into the whole per-backend array), calculated using
228 * the FAST_PATH_SLOT macro, split it into group and index (in the group).
229 */
230#define FAST_PATH_GROUP(index) \
231 (AssertMacro((uint32) (index) < FastPathLockSlotsPerBackend()), \
232 ((index) / FP_LOCK_SLOTS_PER_GROUP))
233#define FAST_PATH_INDEX(index) \
234 (AssertMacro((uint32) (index) < FastPathLockSlotsPerBackend()), \
235 ((index) % FP_LOCK_SLOTS_PER_GROUP))
236
237/* Macros for manipulating proc->fpLockBits */
238#define FAST_PATH_BITS_PER_SLOT 3
239#define FAST_PATH_LOCKNUMBER_OFFSET 1
240#define FAST_PATH_MASK ((1 << FAST_PATH_BITS_PER_SLOT) - 1)
241#define FAST_PATH_BITS(proc, n) (proc)->fpLockBits[FAST_PATH_GROUP(n)]
242#define FAST_PATH_GET_BITS(proc, n) \
243 ((FAST_PATH_BITS(proc, n) >> (FAST_PATH_BITS_PER_SLOT * FAST_PATH_INDEX(n))) & FAST_PATH_MASK)
244#define FAST_PATH_BIT_POSITION(n, l) \
245 (AssertMacro((l) >= FAST_PATH_LOCKNUMBER_OFFSET), \
246 AssertMacro((l) < FAST_PATH_BITS_PER_SLOT+FAST_PATH_LOCKNUMBER_OFFSET), \
247 AssertMacro((n) < FastPathLockSlotsPerBackend()), \
248 ((l) - FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_BITS_PER_SLOT * (FAST_PATH_INDEX(n))))
249#define FAST_PATH_SET_LOCKMODE(proc, n, l) \
250 FAST_PATH_BITS(proc, n) |= UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l)
251#define FAST_PATH_CLEAR_LOCKMODE(proc, n, l) \
252 FAST_PATH_BITS(proc, n) &= ~(UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l))
253#define FAST_PATH_CHECK_LOCKMODE(proc, n, l) \
254 (FAST_PATH_BITS(proc, n) & (UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l)))
255
256/*
257 * The fast-path lock mechanism is concerned only with relation locks on
258 * unshared relations by backends bound to a database. The fast-path
259 * mechanism exists mostly to accelerate acquisition and release of locks
260 * that rarely conflict. Because ShareUpdateExclusiveLock is
261 * self-conflicting, it can't use the fast-path mechanism; but it also does
262 * not conflict with any of the locks that do, so we can ignore it completely.
263 */
264#define EligibleForRelationFastPath(locktag, mode) \
265 ((locktag)->locktag_lockmethodid == DEFAULT_LOCKMETHOD && \
266 (locktag)->locktag_type == LOCKTAG_RELATION && \
267 (locktag)->locktag_field1 == MyDatabaseId && \
268 MyDatabaseId != InvalidOid && \
269 (mode) < ShareUpdateExclusiveLock)
270#define ConflictsWithRelationFastPath(locktag, mode) \
271 ((locktag)->locktag_lockmethodid == DEFAULT_LOCKMETHOD && \
272 (locktag)->locktag_type == LOCKTAG_RELATION && \
273 (locktag)->locktag_field1 != InvalidOid && \
274 (mode) > ShareUpdateExclusiveLock)
275
276static bool FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode);
277static bool FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode);
278static bool FastPathTransferRelationLocks(LockMethod lockMethodTable,
279 const LOCKTAG *locktag, uint32 hashcode);
281
282/*
283 * To make the fast-path lock mechanism work, we must have some way of
284 * preventing the use of the fast-path when a conflicting lock might be present.
285 * We partition* the locktag space into FAST_PATH_STRONG_LOCK_HASH_PARTITIONS,
286 * and maintain an integer count of the number of "strong" lockers
287 * in each partition. When any "strong" lockers are present (which is
288 * hopefully not very often), the fast-path mechanism can't be used, and we
289 * must fall back to the slower method of pushing matching locks directly
290 * into the main lock tables.
291 *
292 * The deadlock detector does not know anything about the fast path mechanism,
293 * so any locks that might be involved in a deadlock must be transferred from
294 * the fast-path queues to the main lock table.
295 */
296
297#define FAST_PATH_STRONG_LOCK_HASH_BITS 10
298#define FAST_PATH_STRONG_LOCK_HASH_PARTITIONS \
299 (1 << FAST_PATH_STRONG_LOCK_HASH_BITS)
300#define FastPathStrongLockHashPartition(hashcode) \
301 ((hashcode) % FAST_PATH_STRONG_LOCK_HASH_PARTITIONS)
302
303typedef struct
304{
305 slock_t mutex;
308
310
311
312/*
313 * Pointers to hash tables containing lock state
314 *
315 * The LockMethodLockHash and LockMethodProcLockHash hash tables are in
316 * shared memory; LockMethodLocalHash is local to each backend.
317 */
321
322
323/* private state for error cleanup */
327
328
329#ifdef LOCK_DEBUG
330
331/*------
332 * The following configuration options are available for lock debugging:
333 *
334 * TRACE_LOCKS -- give a bunch of output what's going on in this file
335 * TRACE_USERLOCKS -- same but for user locks
336 * TRACE_LOCK_OIDMIN-- do not trace locks for tables below this oid
337 * (use to avoid output on system tables)
338 * TRACE_LOCK_TABLE -- trace locks on this table (oid) unconditionally
339 * DEBUG_DEADLOCKS -- currently dumps locks at untimely occasions ;)
340 *
341 * Furthermore, but in storage/lmgr/lwlock.c:
342 * TRACE_LWLOCKS -- trace lightweight locks (pretty useless)
343 *
344 * Define LOCK_DEBUG at compile time to get all these enabled.
345 * --------
346 */
347
348int Trace_lock_oidmin = FirstNormalObjectId;
349bool Trace_locks = false;
350bool Trace_userlocks = false;
351int Trace_lock_table = 0;
352bool Debug_deadlocks = false;
353
354
355inline static bool
356LOCK_DEBUG_ENABLED(const LOCKTAG *tag)
357{
358 return
360 ((Oid) tag->locktag_field2 >= (Oid) Trace_lock_oidmin))
361 || (Trace_lock_table &&
362 (tag->locktag_field2 == Trace_lock_table));
363}
364
365
366inline static void
367LOCK_PRINT(const char *where, const LOCK *lock, LOCKMODE type)
368{
369 if (LOCK_DEBUG_ENABLED(&lock->tag))
370 elog(LOG,
371 "%s: lock(%p) id(%u,%u,%u,%u,%u,%u) grantMask(%x) "
372 "req(%d,%d,%d,%d,%d,%d,%d)=%d "
373 "grant(%d,%d,%d,%d,%d,%d,%d)=%d wait(%d) type(%s)",
374 where, lock,
378 lock->grantMask,
379 lock->requested[1], lock->requested[2], lock->requested[3],
380 lock->requested[4], lock->requested[5], lock->requested[6],
381 lock->requested[7], lock->nRequested,
382 lock->granted[1], lock->granted[2], lock->granted[3],
383 lock->granted[4], lock->granted[5], lock->granted[6],
384 lock->granted[7], lock->nGranted,
385 dclist_count(&lock->waitProcs),
386 LockMethods[LOCK_LOCKMETHOD(*lock)]->lockModeNames[type]);
387}
388
389
390inline static void
391PROCLOCK_PRINT(const char *where, const PROCLOCK *proclockP)
392{
393 if (LOCK_DEBUG_ENABLED(&proclockP->tag.myLock->tag))
394 elog(LOG,
395 "%s: proclock(%p) lock(%p) method(%u) proc(%p) hold(%x)",
396 where, proclockP, proclockP->tag.myLock,
397 PROCLOCK_LOCKMETHOD(*(proclockP)),
398 proclockP->tag.myProc, (int) proclockP->holdMask);
399}
400#else /* not LOCK_DEBUG */
401
402#define LOCK_PRINT(where, lock, type) ((void) 0)
403#define PROCLOCK_PRINT(where, proclockP) ((void) 0)
404#endif /* not LOCK_DEBUG */
405
406
407static uint32 proclock_hash(const void *key, Size keysize);
408static void RemoveLocalLock(LOCALLOCK *locallock);
409static PROCLOCK *SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
410 const LOCKTAG *locktag, uint32 hashcode, LOCKMODE lockmode);
411static void GrantLockLocal(LOCALLOCK *locallock, ResourceOwner owner);
412static void BeginStrongLockAcquire(LOCALLOCK *locallock, uint32 fasthashcode);
413static void FinishStrongLockAcquire(void);
414static ProcWaitStatus WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner);
415static void ReleaseLockIfHeld(LOCALLOCK *locallock, bool sessionLock);
416static void LockReassignOwner(LOCALLOCK *locallock, ResourceOwner parent);
417static bool UnGrantLock(LOCK *lock, LOCKMODE lockmode,
418 PROCLOCK *proclock, LockMethod lockMethodTable);
419static void CleanUpLock(LOCK *lock, PROCLOCK *proclock,
420 LockMethod lockMethodTable, uint32 hashcode,
421 bool wakeupNeeded);
422static void LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc,
423 LOCKTAG *locktag, LOCKMODE lockmode,
424 bool decrement_strong_lock_count);
425static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc,
427
428
429/*
430 * Initialize the lock manager's shmem data structures.
431 *
432 * This is called from CreateSharedMemoryAndSemaphores(), which see for more
433 * comments. In the normal postmaster case, the shared hash tables are
434 * created here, and backends inherit pointers to them via fork(). In the
435 * EXEC_BACKEND case, each backend re-executes this code to obtain pointers to
436 * the already existing shared hash tables. In either case, each backend must
437 * also call InitLockManagerAccess() to create the locallock hash table.
438 */
439void
441{
442 HASHCTL info;
443 long init_table_size,
444 max_table_size;
445 bool found;
446
447 /*
448 * Compute init/max size to request for lock hashtables. Note these
449 * calculations must agree with LockManagerShmemSize!
450 */
451 max_table_size = NLOCKENTS();
452 init_table_size = max_table_size / 2;
453
454 /*
455 * Allocate hash table for LOCK structs. This stores per-locked-object
456 * information.
457 */
458 info.keysize = sizeof(LOCKTAG);
459 info.entrysize = sizeof(LOCK);
461
462 LockMethodLockHash = ShmemInitHash("LOCK hash",
463 init_table_size,
464 max_table_size,
465 &info,
467
468 /* Assume an average of 2 holders per lock */
469 max_table_size *= 2;
470 init_table_size *= 2;
471
472 /*
473 * Allocate hash table for PROCLOCK structs. This stores
474 * per-lock-per-holder information.
475 */
476 info.keysize = sizeof(PROCLOCKTAG);
477 info.entrysize = sizeof(PROCLOCK);
478 info.hash = proclock_hash;
480
481 LockMethodProcLockHash = ShmemInitHash("PROCLOCK hash",
482 init_table_size,
483 max_table_size,
484 &info,
486
487 /*
488 * Allocate fast-path structures.
489 */
491 ShmemInitStruct("Fast Path Strong Relation Lock Data",
492 sizeof(FastPathStrongRelationLockData), &found);
493 if (!found)
495}
496
497/*
498 * Initialize the lock manager's backend-private data structures.
499 */
500void
502{
503 /*
504 * Allocate non-shared hash table for LOCALLOCK structs. This stores lock
505 * counts and resource owner information.
506 */
507 HASHCTL info;
508
509 info.keysize = sizeof(LOCALLOCKTAG);
510 info.entrysize = sizeof(LOCALLOCK);
511
512 LockMethodLocalHash = hash_create("LOCALLOCK hash",
513 16,
514 &info,
516}
517
518
519/*
520 * Fetch the lock method table associated with a given lock
521 */
524{
525 LOCKMETHODID lockmethodid = LOCK_LOCKMETHOD(*lock);
526
527 Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
528 return LockMethods[lockmethodid];
529}
530
531/*
532 * Fetch the lock method table associated with a given locktag
533 */
536{
537 LOCKMETHODID lockmethodid = (LOCKMETHODID) locktag->locktag_lockmethodid;
538
539 Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
540 return LockMethods[lockmethodid];
541}
542
543
544/*
545 * Compute the hash code associated with a LOCKTAG.
546 *
547 * To avoid unnecessary recomputations of the hash code, we try to do this
548 * just once per function, and then pass it around as needed. Aside from
549 * passing the hashcode to hash_search_with_hash_value(), we can extract
550 * the lock partition number from the hashcode.
551 */
552uint32
554{
555 return get_hash_value(LockMethodLockHash, locktag);
556}
557
558/*
559 * Compute the hash code associated with a PROCLOCKTAG.
560 *
561 * Because we want to use just one set of partition locks for both the
562 * LOCK and PROCLOCK hash tables, we have to make sure that PROCLOCKs
563 * fall into the same partition number as their associated LOCKs.
564 * dynahash.c expects the partition number to be the low-order bits of
565 * the hash code, and therefore a PROCLOCKTAG's hash code must have the
566 * same low-order bits as the associated LOCKTAG's hash code. We achieve
567 * this with this specialized hash function.
568 */
569static uint32
570proclock_hash(const void *key, Size keysize)
571{
572 const PROCLOCKTAG *proclocktag = (const PROCLOCKTAG *) key;
573 uint32 lockhash;
574 Datum procptr;
575
576 Assert(keysize == sizeof(PROCLOCKTAG));
577
578 /* Look into the associated LOCK object, and compute its hash code */
579 lockhash = LockTagHashCode(&proclocktag->myLock->tag);
580
581 /*
582 * To make the hash code also depend on the PGPROC, we xor the proc
583 * struct's address into the hash code, left-shifted so that the
584 * partition-number bits don't change. Since this is only a hash, we
585 * don't care if we lose high-order bits of the address; use an
586 * intermediate variable to suppress cast-pointer-to-int warnings.
587 */
588 procptr = PointerGetDatum(proclocktag->myProc);
589 lockhash ^= ((uint32) procptr) << LOG2_NUM_LOCK_PARTITIONS;
590
591 return lockhash;
592}
593
594/*
595 * Compute the hash code associated with a PROCLOCKTAG, given the hashcode
596 * for its underlying LOCK.
597 *
598 * We use this just to avoid redundant calls of LockTagHashCode().
599 */
600static inline uint32
601ProcLockHashCode(const PROCLOCKTAG *proclocktag, uint32 hashcode)
602{
603 uint32 lockhash = hashcode;
604 Datum procptr;
605
606 /*
607 * This must match proclock_hash()!
608 */
609 procptr = PointerGetDatum(proclocktag->myProc);
610 lockhash ^= ((uint32) procptr) << LOG2_NUM_LOCK_PARTITIONS;
611
612 return lockhash;
613}
614
615/*
616 * Given two lock modes, return whether they would conflict.
617 */
618bool
620{
621 LockMethod lockMethodTable = LockMethods[DEFAULT_LOCKMETHOD];
622
623 if (lockMethodTable->conflictTab[mode1] & LOCKBIT_ON(mode2))
624 return true;
625
626 return false;
627}
628
629/*
630 * LockHeldByMe -- test whether lock 'locktag' is held by the current
631 * transaction
632 *
633 * Returns true if current transaction holds a lock on 'tag' of mode
634 * 'lockmode'. If 'orstronger' is true, a stronger lockmode is also OK.
635 * ("Stronger" is defined as "numerically higher", which is a bit
636 * semantically dubious but is OK for the purposes we use this for.)
637 */
638bool
639LockHeldByMe(const LOCKTAG *locktag,
640 LOCKMODE lockmode, bool orstronger)
641{
642 LOCALLOCKTAG localtag;
643 LOCALLOCK *locallock;
644
645 /*
646 * See if there is a LOCALLOCK entry for this lock and lockmode
647 */
648 MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
649 localtag.lock = *locktag;
650 localtag.mode = lockmode;
651
653 &localtag,
654 HASH_FIND, NULL);
655
656 if (locallock && locallock->nLocks > 0)
657 return true;
658
659 if (orstronger)
660 {
661 LOCKMODE slockmode;
662
663 for (slockmode = lockmode + 1;
664 slockmode <= MaxLockMode;
665 slockmode++)
666 {
667 if (LockHeldByMe(locktag, slockmode, false))
668 return true;
669 }
670 }
671
672 return false;
673}
674
675#ifdef USE_ASSERT_CHECKING
676/*
677 * GetLockMethodLocalHash -- return the hash of local locks, for modules that
678 * evaluate assertions based on all locks held.
679 */
680HTAB *
681GetLockMethodLocalHash(void)
682{
683 return LockMethodLocalHash;
684}
685#endif
686
687/*
688 * LockHasWaiters -- look up 'locktag' and check if releasing this
689 * lock would wake up other processes waiting for it.
690 */
691bool
692LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
693{
694 LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
695 LockMethod lockMethodTable;
696 LOCALLOCKTAG localtag;
697 LOCALLOCK *locallock;
698 LOCK *lock;
699 PROCLOCK *proclock;
700 LWLock *partitionLock;
701 bool hasWaiters = false;
702
703 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
704 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
705 lockMethodTable = LockMethods[lockmethodid];
706 if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
707 elog(ERROR, "unrecognized lock mode: %d", lockmode);
708
709#ifdef LOCK_DEBUG
710 if (LOCK_DEBUG_ENABLED(locktag))
711 elog(LOG, "LockHasWaiters: lock [%u,%u] %s",
712 locktag->locktag_field1, locktag->locktag_field2,
713 lockMethodTable->lockModeNames[lockmode]);
714#endif
715
716 /*
717 * Find the LOCALLOCK entry for this lock and lockmode
718 */
719 MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
720 localtag.lock = *locktag;
721 localtag.mode = lockmode;
722
724 &localtag,
725 HASH_FIND, NULL);
726
727 /*
728 * let the caller print its own error message, too. Do not ereport(ERROR).
729 */
730 if (!locallock || locallock->nLocks <= 0)
731 {
732 elog(WARNING, "you don't own a lock of type %s",
733 lockMethodTable->lockModeNames[lockmode]);
734 return false;
735 }
736
737 /*
738 * Check the shared lock table.
739 */
740 partitionLock = LockHashPartitionLock(locallock->hashcode);
741
742 LWLockAcquire(partitionLock, LW_SHARED);
743
744 /*
745 * We don't need to re-find the lock or proclock, since we kept their
746 * addresses in the locallock table, and they couldn't have been removed
747 * while we were holding a lock on them.
748 */
749 lock = locallock->lock;
750 LOCK_PRINT("LockHasWaiters: found", lock, lockmode);
751 proclock = locallock->proclock;
752 PROCLOCK_PRINT("LockHasWaiters: found", proclock);
753
754 /*
755 * Double-check that we are actually holding a lock of the type we want to
756 * release.
757 */
758 if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
759 {
760 PROCLOCK_PRINT("LockHasWaiters: WRONGTYPE", proclock);
761 LWLockRelease(partitionLock);
762 elog(WARNING, "you don't own a lock of type %s",
763 lockMethodTable->lockModeNames[lockmode]);
764 RemoveLocalLock(locallock);
765 return false;
766 }
767
768 /*
769 * Do the checking.
770 */
771 if ((lockMethodTable->conflictTab[lockmode] & lock->waitMask) != 0)
772 hasWaiters = true;
773
774 LWLockRelease(partitionLock);
775
776 return hasWaiters;
777}
778
779/*
780 * LockAcquire -- Check for lock conflicts, sleep if conflict found,
781 * set lock if/when no conflicts.
782 *
783 * Inputs:
784 * locktag: unique identifier for the lockable object
785 * lockmode: lock mode to acquire
786 * sessionLock: if true, acquire lock for session not current transaction
787 * dontWait: if true, don't wait to acquire lock
788 *
789 * Returns one of:
790 * LOCKACQUIRE_NOT_AVAIL lock not available, and dontWait=true
791 * LOCKACQUIRE_OK lock successfully acquired
792 * LOCKACQUIRE_ALREADY_HELD incremented count for lock already held
793 * LOCKACQUIRE_ALREADY_CLEAR incremented count for lock already clear
794 *
795 * In the normal case where dontWait=false and the caller doesn't need to
796 * distinguish a freshly acquired lock from one already taken earlier in
797 * this same transaction, there is no need to examine the return value.
798 *
799 * Side Effects: The lock is acquired and recorded in lock tables.
800 *
801 * NOTE: if we wait for the lock, there is no way to abort the wait
802 * short of aborting the transaction.
803 */
805LockAcquire(const LOCKTAG *locktag,
806 LOCKMODE lockmode,
807 bool sessionLock,
808 bool dontWait)
809{
810 return LockAcquireExtended(locktag, lockmode, sessionLock, dontWait,
811 true, NULL, false);
812}
813
814/*
815 * LockAcquireExtended - allows us to specify additional options
816 *
817 * reportMemoryError specifies whether a lock request that fills the lock
818 * table should generate an ERROR or not. Passing "false" allows the caller
819 * to attempt to recover from lock-table-full situations, perhaps by forcibly
820 * canceling other lock holders and then retrying. Note, however, that the
821 * return code for that is LOCKACQUIRE_NOT_AVAIL, so that it's unsafe to use
822 * in combination with dontWait = true, as the cause of failure couldn't be
823 * distinguished.
824 *
825 * If locallockp isn't NULL, *locallockp receives a pointer to the LOCALLOCK
826 * table entry if a lock is successfully acquired, or NULL if not.
827 *
828 * logLockFailure indicates whether to log details when a lock acquisition
829 * fails with dontWait = true.
830 */
833 LOCKMODE lockmode,
834 bool sessionLock,
835 bool dontWait,
836 bool reportMemoryError,
837 LOCALLOCK **locallockp,
838 bool logLockFailure)
839{
840 LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
841 LockMethod lockMethodTable;
842 LOCALLOCKTAG localtag;
843 LOCALLOCK *locallock;
844 LOCK *lock;
845 PROCLOCK *proclock;
846 bool found;
847 ResourceOwner owner;
848 uint32 hashcode;
849 LWLock *partitionLock;
850 bool found_conflict;
851 ProcWaitStatus waitResult;
852 bool log_lock = false;
853
854 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
855 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
856 lockMethodTable = LockMethods[lockmethodid];
857 if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
858 elog(ERROR, "unrecognized lock mode: %d", lockmode);
859
860 if (RecoveryInProgress() && !InRecovery &&
861 (locktag->locktag_type == LOCKTAG_OBJECT ||
862 locktag->locktag_type == LOCKTAG_RELATION) &&
863 lockmode > RowExclusiveLock)
865 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
866 errmsg("cannot acquire lock mode %s on database objects while recovery is in progress",
867 lockMethodTable->lockModeNames[lockmode]),
868 errhint("Only RowExclusiveLock or less can be acquired on database objects during recovery.")));
869
870#ifdef LOCK_DEBUG
871 if (LOCK_DEBUG_ENABLED(locktag))
872 elog(LOG, "LockAcquire: lock [%u,%u] %s",
873 locktag->locktag_field1, locktag->locktag_field2,
874 lockMethodTable->lockModeNames[lockmode]);
875#endif
876
877 /* Identify owner for lock */
878 if (sessionLock)
879 owner = NULL;
880 else
881 owner = CurrentResourceOwner;
882
883 /*
884 * Find or create a LOCALLOCK entry for this lock and lockmode
885 */
886 MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
887 localtag.lock = *locktag;
888 localtag.mode = lockmode;
889
891 &localtag,
892 HASH_ENTER, &found);
893
894 /*
895 * if it's a new locallock object, initialize it
896 */
897 if (!found)
898 {
899 locallock->lock = NULL;
900 locallock->proclock = NULL;
901 locallock->hashcode = LockTagHashCode(&(localtag.lock));
902 locallock->nLocks = 0;
903 locallock->holdsStrongLockCount = false;
904 locallock->lockCleared = false;
905 locallock->numLockOwners = 0;
906 locallock->maxLockOwners = 8;
907 locallock->lockOwners = NULL; /* in case next line fails */
908 locallock->lockOwners = (LOCALLOCKOWNER *)
910 locallock->maxLockOwners * sizeof(LOCALLOCKOWNER));
911 }
912 else
913 {
914 /* Make sure there will be room to remember the lock */
915 if (locallock->numLockOwners >= locallock->maxLockOwners)
916 {
917 int newsize = locallock->maxLockOwners * 2;
918
919 locallock->lockOwners = (LOCALLOCKOWNER *)
920 repalloc(locallock->lockOwners,
921 newsize * sizeof(LOCALLOCKOWNER));
922 locallock->maxLockOwners = newsize;
923 }
924 }
925 hashcode = locallock->hashcode;
926
927 if (locallockp)
928 *locallockp = locallock;
929
930 /*
931 * If we already hold the lock, we can just increase the count locally.
932 *
933 * If lockCleared is already set, caller need not worry about absorbing
934 * sinval messages related to the lock's object.
935 */
936 if (locallock->nLocks > 0)
937 {
938 GrantLockLocal(locallock, owner);
939 if (locallock->lockCleared)
941 else
943 }
944
945 /*
946 * We don't acquire any other heavyweight lock while holding the relation
947 * extension lock. We do allow to acquire the same relation extension
948 * lock more than once but that case won't reach here.
949 */
950 Assert(!IsRelationExtensionLockHeld);
951
952 /*
953 * Prepare to emit a WAL record if acquisition of this lock needs to be
954 * replayed in a standby server.
955 *
956 * Here we prepare to log; after lock is acquired we'll issue log record.
957 * This arrangement simplifies error recovery in case the preparation step
958 * fails.
959 *
960 * Only AccessExclusiveLocks can conflict with lock types that read-only
961 * transactions can acquire in a standby server. Make sure this definition
962 * matches the one in GetRunningTransactionLocks().
963 */
964 if (lockmode >= AccessExclusiveLock &&
965 locktag->locktag_type == LOCKTAG_RELATION &&
968 {
970 log_lock = true;
971 }
972
973 /*
974 * Attempt to take lock via fast path, if eligible. But if we remember
975 * having filled up the fast path array, we don't attempt to make any
976 * further use of it until we release some locks. It's possible that some
977 * other backend has transferred some of those locks to the shared hash
978 * table, leaving space free, but it's not worth acquiring the LWLock just
979 * to check. It's also possible that we're acquiring a second or third
980 * lock type on a relation we have already locked using the fast-path, but
981 * for now we don't worry about that case either.
982 */
983 if (EligibleForRelationFastPath(locktag, lockmode) &&
985 {
986 uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode);
987 bool acquired;
988
989 /*
990 * LWLockAcquire acts as a memory sequencing point, so it's safe to
991 * assume that any strong locker whose increment to
992 * FastPathStrongRelationLocks->counts becomes visible after we test
993 * it has yet to begin to transfer fast-path locks.
994 */
996 if (FastPathStrongRelationLocks->count[fasthashcode] != 0)
997 acquired = false;
998 else
999 acquired = FastPathGrantRelationLock(locktag->locktag_field2,
1000 lockmode);
1002 if (acquired)
1003 {
1004 /*
1005 * The locallock might contain stale pointers to some old shared
1006 * objects; we MUST reset these to null before considering the
1007 * lock to be acquired via fast-path.
1008 */
1009 locallock->lock = NULL;
1010 locallock->proclock = NULL;
1011 GrantLockLocal(locallock, owner);
1012 return LOCKACQUIRE_OK;
1013 }
1014 }
1015
1016 /*
1017 * If this lock could potentially have been taken via the fast-path by
1018 * some other backend, we must (temporarily) disable further use of the
1019 * fast-path for this lock tag, and migrate any locks already taken via
1020 * this method to the main lock table.
1021 */
1022 if (ConflictsWithRelationFastPath(locktag, lockmode))
1023 {
1024 uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode);
1025
1026 BeginStrongLockAcquire(locallock, fasthashcode);
1027 if (!FastPathTransferRelationLocks(lockMethodTable, locktag,
1028 hashcode))
1029 {
1031 if (locallock->nLocks == 0)
1032 RemoveLocalLock(locallock);
1033 if (locallockp)
1034 *locallockp = NULL;
1035 if (reportMemoryError)
1036 ereport(ERROR,
1037 (errcode(ERRCODE_OUT_OF_MEMORY),
1038 errmsg("out of shared memory"),
1039 errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
1040 else
1041 return LOCKACQUIRE_NOT_AVAIL;
1042 }
1043 }
1044
1045 /*
1046 * We didn't find the lock in our LOCALLOCK table, and we didn't manage to
1047 * take it via the fast-path, either, so we've got to mess with the shared
1048 * lock table.
1049 */
1050 partitionLock = LockHashPartitionLock(hashcode);
1051
1052 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
1053
1054 /*
1055 * Find or create lock and proclock entries with this tag
1056 *
1057 * Note: if the locallock object already existed, it might have a pointer
1058 * to the lock already ... but we should not assume that that pointer is
1059 * valid, since a lock object with zero hold and request counts can go
1060 * away anytime. So we have to use SetupLockInTable() to recompute the
1061 * lock and proclock pointers, even if they're already set.
1062 */
1063 proclock = SetupLockInTable(lockMethodTable, MyProc, locktag,
1064 hashcode, lockmode);
1065 if (!proclock)
1066 {
1068 LWLockRelease(partitionLock);
1069 if (locallock->nLocks == 0)
1070 RemoveLocalLock(locallock);
1071 if (locallockp)
1072 *locallockp = NULL;
1073 if (reportMemoryError)
1074 ereport(ERROR,
1075 (errcode(ERRCODE_OUT_OF_MEMORY),
1076 errmsg("out of shared memory"),
1077 errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
1078 else
1079 return LOCKACQUIRE_NOT_AVAIL;
1080 }
1081 locallock->proclock = proclock;
1082 lock = proclock->tag.myLock;
1083 locallock->lock = lock;
1084
1085 /*
1086 * If lock requested conflicts with locks requested by waiters, must join
1087 * wait queue. Otherwise, check for conflict with already-held locks.
1088 * (That's last because most complex check.)
1089 */
1090 if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
1091 found_conflict = true;
1092 else
1093 found_conflict = LockCheckConflicts(lockMethodTable, lockmode,
1094 lock, proclock);
1095
1096 if (!found_conflict)
1097 {
1098 /* No conflict with held or previously requested locks */
1099 GrantLock(lock, proclock, lockmode);
1100 waitResult = PROC_WAIT_STATUS_OK;
1101 }
1102 else
1103 {
1104 /*
1105 * Join the lock's wait queue. We call this even in the dontWait
1106 * case, because JoinWaitQueue() may discover that we can acquire the
1107 * lock immediately after all.
1108 */
1109 waitResult = JoinWaitQueue(locallock, lockMethodTable, dontWait);
1110 }
1111
1112 if (waitResult == PROC_WAIT_STATUS_ERROR)
1113 {
1114 /*
1115 * We're not getting the lock because a deadlock was detected already
1116 * while trying to join the wait queue, or because we would have to
1117 * wait but the caller requested no blocking.
1118 *
1119 * Undo the changes to shared entries before releasing the partition
1120 * lock.
1121 */
1123
1124 if (proclock->holdMask == 0)
1125 {
1126 uint32 proclock_hashcode;
1127
1128 proclock_hashcode = ProcLockHashCode(&proclock->tag,
1129 hashcode);
1130 dlist_delete(&proclock->lockLink);
1131 dlist_delete(&proclock->procLink);
1133 &(proclock->tag),
1134 proclock_hashcode,
1136 NULL))
1137 elog(PANIC, "proclock table corrupted");
1138 }
1139 else
1140 PROCLOCK_PRINT("LockAcquire: did not join wait queue", proclock);
1141 lock->nRequested--;
1142 lock->requested[lockmode]--;
1143 LOCK_PRINT("LockAcquire: did not join wait queue",
1144 lock, lockmode);
1145 Assert((lock->nRequested > 0) &&
1146 (lock->requested[lockmode] >= 0));
1147 Assert(lock->nGranted <= lock->nRequested);
1148 LWLockRelease(partitionLock);
1149 if (locallock->nLocks == 0)
1150 RemoveLocalLock(locallock);
1151
1152 if (dontWait)
1153 {
1154 /*
1155 * Log lock holders and waiters as a detail log message if
1156 * logLockFailure = true and lock acquisition fails with dontWait
1157 * = true
1158 */
1159 if (logLockFailure)
1160 {
1162 lock_waiters_sbuf,
1163 lock_holders_sbuf;
1164 const char *modename;
1165 int lockHoldersNum = 0;
1166
1168 initStringInfo(&lock_waiters_sbuf);
1169 initStringInfo(&lock_holders_sbuf);
1170
1171 DescribeLockTag(&buf, &locallock->tag.lock);
1172 modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
1173 lockmode);
1174
1175 /* Gather a list of all lock holders and waiters */
1176 LWLockAcquire(partitionLock, LW_SHARED);
1177 GetLockHoldersAndWaiters(locallock, &lock_holders_sbuf,
1178 &lock_waiters_sbuf, &lockHoldersNum);
1179 LWLockRelease(partitionLock);
1180
1181 ereport(LOG,
1182 (errmsg("process %d could not obtain %s on %s",
1183 MyProcPid, modename, buf.data),
1185 "Process holding the lock: %s, Wait queue: %s.",
1186 "Processes holding the lock: %s, Wait queue: %s.",
1187 lockHoldersNum,
1188 lock_holders_sbuf.data,
1189 lock_waiters_sbuf.data)));
1190
1191 pfree(buf.data);
1192 pfree(lock_holders_sbuf.data);
1193 pfree(lock_waiters_sbuf.data);
1194 }
1195 if (locallockp)
1196 *locallockp = NULL;
1197 return LOCKACQUIRE_NOT_AVAIL;
1198 }
1199 else
1200 {
1202 /* DeadLockReport() will not return */
1203 }
1204 }
1205
1206 /*
1207 * We are now in the lock queue, or the lock was already granted. If
1208 * queued, go to sleep.
1209 */
1210 if (waitResult == PROC_WAIT_STATUS_WAITING)
1211 {
1212 Assert(!dontWait);
1213 PROCLOCK_PRINT("LockAcquire: sleeping on lock", proclock);
1214 LOCK_PRINT("LockAcquire: sleeping on lock", lock, lockmode);
1215 LWLockRelease(partitionLock);
1216
1217 waitResult = WaitOnLock(locallock, owner);
1218
1219 /*
1220 * NOTE: do not do any material change of state between here and
1221 * return. All required changes in locktable state must have been
1222 * done when the lock was granted to us --- see notes in WaitOnLock.
1223 */
1224
1225 if (waitResult == PROC_WAIT_STATUS_ERROR)
1226 {
1227 /*
1228 * We failed as a result of a deadlock, see CheckDeadLock(). Quit
1229 * now.
1230 */
1231 Assert(!dontWait);
1233 /* DeadLockReport() will not return */
1234 }
1235 }
1236 else
1237 LWLockRelease(partitionLock);
1238 Assert(waitResult == PROC_WAIT_STATUS_OK);
1239
1240 /* The lock was granted to us. Update the local lock entry accordingly */
1241 Assert((proclock->holdMask & LOCKBIT_ON(lockmode)) != 0);
1242 GrantLockLocal(locallock, owner);
1243
1244 /*
1245 * Lock state is fully up-to-date now; if we error out after this, no
1246 * special error cleanup is required.
1247 */
1249
1250 /*
1251 * Emit a WAL record if acquisition of this lock needs to be replayed in a
1252 * standby server.
1253 */
1254 if (log_lock)
1255 {
1256 /*
1257 * Decode the locktag back to the original values, to avoid sending
1258 * lots of empty bytes with every message. See lock.h to check how a
1259 * locktag is defined for LOCKTAG_RELATION
1260 */
1262 locktag->locktag_field2);
1263 }
1264
1265 return LOCKACQUIRE_OK;
1266}
1267
1268/*
1269 * Find or create LOCK and PROCLOCK objects as needed for a new lock
1270 * request.
1271 *
1272 * Returns the PROCLOCK object, or NULL if we failed to create the objects
1273 * for lack of shared memory.
1274 *
1275 * The appropriate partition lock must be held at entry, and will be
1276 * held at exit.
1277 */
1278static PROCLOCK *
1279SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
1280 const LOCKTAG *locktag, uint32 hashcode, LOCKMODE lockmode)
1281{
1282 LOCK *lock;
1283 PROCLOCK *proclock;
1284 PROCLOCKTAG proclocktag;
1285 uint32 proclock_hashcode;
1286 bool found;
1287
1288 /*
1289 * Find or create a lock with this tag.
1290 */
1292 locktag,
1293 hashcode,
1295 &found);
1296 if (!lock)
1297 return NULL;
1298
1299 /*
1300 * if it's a new lock object, initialize it
1301 */
1302 if (!found)
1303 {
1304 lock->grantMask = 0;
1305 lock->waitMask = 0;
1306 dlist_init(&lock->procLocks);
1307 dclist_init(&lock->waitProcs);
1308 lock->nRequested = 0;
1309 lock->nGranted = 0;
1310 MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
1311 MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
1312 LOCK_PRINT("LockAcquire: new", lock, lockmode);
1313 }
1314 else
1315 {
1316 LOCK_PRINT("LockAcquire: found", lock, lockmode);
1317 Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
1318 Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
1319 Assert(lock->nGranted <= lock->nRequested);
1320 }
1321
1322 /*
1323 * Create the hash key for the proclock table.
1324 */
1325 proclocktag.myLock = lock;
1326 proclocktag.myProc = proc;
1327
1328 proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode);
1329
1330 /*
1331 * Find or create a proclock entry with this tag
1332 */
1334 &proclocktag,
1335 proclock_hashcode,
1337 &found);
1338 if (!proclock)
1339 {
1340 /* Oops, not enough shmem for the proclock */
1341 if (lock->nRequested == 0)
1342 {
1343 /*
1344 * There are no other requestors of this lock, so garbage-collect
1345 * the lock object. We *must* do this to avoid a permanent leak
1346 * of shared memory, because there won't be anything to cause
1347 * anyone to release the lock object later.
1348 */
1349 Assert(dlist_is_empty(&(lock->procLocks)));
1351 &(lock->tag),
1352 hashcode,
1354 NULL))
1355 elog(PANIC, "lock table corrupted");
1356 }
1357 return NULL;
1358 }
1359
1360 /*
1361 * If new, initialize the new entry
1362 */
1363 if (!found)
1364 {
1365 uint32 partition = LockHashPartition(hashcode);
1366
1367 /*
1368 * It might seem unsafe to access proclock->groupLeader without a
1369 * lock, but it's not really. Either we are initializing a proclock
1370 * on our own behalf, in which case our group leader isn't changing
1371 * because the group leader for a process can only ever be changed by
1372 * the process itself; or else we are transferring a fast-path lock to
1373 * the main lock table, in which case that process can't change its
1374 * lock group leader without first releasing all of its locks (and in
1375 * particular the one we are currently transferring).
1376 */
1377 proclock->groupLeader = proc->lockGroupLeader != NULL ?
1378 proc->lockGroupLeader : proc;
1379 proclock->holdMask = 0;
1380 proclock->releaseMask = 0;
1381 /* Add proclock to appropriate lists */
1382 dlist_push_tail(&lock->procLocks, &proclock->lockLink);
1383 dlist_push_tail(&proc->myProcLocks[partition], &proclock->procLink);
1384 PROCLOCK_PRINT("LockAcquire: new", proclock);
1385 }
1386 else
1387 {
1388 PROCLOCK_PRINT("LockAcquire: found", proclock);
1389 Assert((proclock->holdMask & ~lock->grantMask) == 0);
1390
1391#ifdef CHECK_DEADLOCK_RISK
1392
1393 /*
1394 * Issue warning if we already hold a lower-level lock on this object
1395 * and do not hold a lock of the requested level or higher. This
1396 * indicates a deadlock-prone coding practice (eg, we'd have a
1397 * deadlock if another backend were following the same code path at
1398 * about the same time).
1399 *
1400 * This is not enabled by default, because it may generate log entries
1401 * about user-level coding practices that are in fact safe in context.
1402 * It can be enabled to help find system-level problems.
1403 *
1404 * XXX Doing numeric comparison on the lockmodes is a hack; it'd be
1405 * better to use a table. For now, though, this works.
1406 */
1407 {
1408 int i;
1409
1410 for (i = lockMethodTable->numLockModes; i > 0; i--)
1411 {
1412 if (proclock->holdMask & LOCKBIT_ON(i))
1413 {
1414 if (i >= (int) lockmode)
1415 break; /* safe: we have a lock >= req level */
1416 elog(LOG, "deadlock risk: raising lock level"
1417 " from %s to %s on object %u/%u/%u",
1418 lockMethodTable->lockModeNames[i],
1419 lockMethodTable->lockModeNames[lockmode],
1420 lock->tag.locktag_field1, lock->tag.locktag_field2,
1421 lock->tag.locktag_field3);
1422 break;
1423 }
1424 }
1425 }
1426#endif /* CHECK_DEADLOCK_RISK */
1427 }
1428
1429 /*
1430 * lock->nRequested and lock->requested[] count the total number of
1431 * requests, whether granted or waiting, so increment those immediately.
1432 * The other counts don't increment till we get the lock.
1433 */
1434 lock->nRequested++;
1435 lock->requested[lockmode]++;
1436 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
1437
1438 /*
1439 * We shouldn't already hold the desired lock; else locallock table is
1440 * broken.
1441 */
1442 if (proclock->holdMask & LOCKBIT_ON(lockmode))
1443 elog(ERROR, "lock %s on object %u/%u/%u is already held",
1444 lockMethodTable->lockModeNames[lockmode],
1445 lock->tag.locktag_field1, lock->tag.locktag_field2,
1446 lock->tag.locktag_field3);
1447
1448 return proclock;
1449}
1450
1451/*
1452 * Check and set/reset the flag that we hold the relation extension lock.
1453 *
1454 * It is callers responsibility that this function is called after
1455 * acquiring/releasing the relation extension lock.
1456 *
1457 * Pass acquired as true if lock is acquired, false otherwise.
1458 */
1459static inline void
1460CheckAndSetLockHeld(LOCALLOCK *locallock, bool acquired)
1461{
1462#ifdef USE_ASSERT_CHECKING
1463 if (LOCALLOCK_LOCKTAG(*locallock) == LOCKTAG_RELATION_EXTEND)
1464 IsRelationExtensionLockHeld = acquired;
1465#endif
1466}
1467
1468/*
1469 * Subroutine to free a locallock entry
1470 */
1471static void
1473{
1474 int i;
1475
1476 for (i = locallock->numLockOwners - 1; i >= 0; i--)
1477 {
1478 if (locallock->lockOwners[i].owner != NULL)
1479 ResourceOwnerForgetLock(locallock->lockOwners[i].owner, locallock);
1480 }
1481 locallock->numLockOwners = 0;
1482 if (locallock->lockOwners != NULL)
1483 pfree(locallock->lockOwners);
1484 locallock->lockOwners = NULL;
1485
1486 if (locallock->holdsStrongLockCount)
1487 {
1488 uint32 fasthashcode;
1489
1490 fasthashcode = FastPathStrongLockHashPartition(locallock->hashcode);
1491
1493 Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
1494 FastPathStrongRelationLocks->count[fasthashcode]--;
1495 locallock->holdsStrongLockCount = false;
1497 }
1498
1500 &(locallock->tag),
1501 HASH_REMOVE, NULL))
1502 elog(WARNING, "locallock table corrupted");
1503
1504 /*
1505 * Indicate that the lock is released for certain types of locks
1506 */
1507 CheckAndSetLockHeld(locallock, false);
1508}
1509
1510/*
1511 * LockCheckConflicts -- test whether requested lock conflicts
1512 * with those already granted
1513 *
1514 * Returns true if conflict, false if no conflict.
1515 *
1516 * NOTES:
1517 * Here's what makes this complicated: one process's locks don't
1518 * conflict with one another, no matter what purpose they are held for
1519 * (eg, session and transaction locks do not conflict). Nor do the locks
1520 * of one process in a lock group conflict with those of another process in
1521 * the same group. So, we must subtract off these locks when determining
1522 * whether the requested new lock conflicts with those already held.
1523 */
1524bool
1526 LOCKMODE lockmode,
1527 LOCK *lock,
1528 PROCLOCK *proclock)
1529{
1530 int numLockModes = lockMethodTable->numLockModes;
1531 LOCKMASK myLocks;
1532 int conflictMask = lockMethodTable->conflictTab[lockmode];
1533 int conflictsRemaining[MAX_LOCKMODES];
1534 int totalConflictsRemaining = 0;
1535 dlist_iter proclock_iter;
1536 int i;
1537
1538 /*
1539 * first check for global conflicts: If no locks conflict with my request,
1540 * then I get the lock.
1541 *
1542 * Checking for conflict: lock->grantMask represents the types of
1543 * currently held locks. conflictTable[lockmode] has a bit set for each
1544 * type of lock that conflicts with request. Bitwise compare tells if
1545 * there is a conflict.
1546 */
1547 if (!(conflictMask & lock->grantMask))
1548 {
1549 PROCLOCK_PRINT("LockCheckConflicts: no conflict", proclock);
1550 return false;
1551 }
1552
1553 /*
1554 * Rats. Something conflicts. But it could still be my own lock, or a
1555 * lock held by another member of my locking group. First, figure out how
1556 * many conflicts remain after subtracting out any locks I hold myself.
1557 */
1558 myLocks = proclock->holdMask;
1559 for (i = 1; i <= numLockModes; i++)
1560 {
1561 if ((conflictMask & LOCKBIT_ON(i)) == 0)
1562 {
1563 conflictsRemaining[i] = 0;
1564 continue;
1565 }
1566 conflictsRemaining[i] = lock->granted[i];
1567 if (myLocks & LOCKBIT_ON(i))
1568 --conflictsRemaining[i];
1569 totalConflictsRemaining += conflictsRemaining[i];
1570 }
1571
1572 /* If no conflicts remain, we get the lock. */
1573 if (totalConflictsRemaining == 0)
1574 {
1575 PROCLOCK_PRINT("LockCheckConflicts: resolved (simple)", proclock);
1576 return false;
1577 }
1578
1579 /* If no group locking, it's definitely a conflict. */
1580 if (proclock->groupLeader == MyProc && MyProc->lockGroupLeader == NULL)
1581 {
1582 Assert(proclock->tag.myProc == MyProc);
1583 PROCLOCK_PRINT("LockCheckConflicts: conflicting (simple)",
1584 proclock);
1585 return true;
1586 }
1587
1588 /*
1589 * The relation extension lock conflict even between the group members.
1590 */
1592 {
1593 PROCLOCK_PRINT("LockCheckConflicts: conflicting (group)",
1594 proclock);
1595 return true;
1596 }
1597
1598 /*
1599 * Locks held in conflicting modes by members of our own lock group are
1600 * not real conflicts; we can subtract those out and see if we still have
1601 * a conflict. This is O(N) in the number of processes holding or
1602 * awaiting locks on this object. We could improve that by making the
1603 * shared memory state more complex (and larger) but it doesn't seem worth
1604 * it.
1605 */
1606 dlist_foreach(proclock_iter, &lock->procLocks)
1607 {
1608 PROCLOCK *otherproclock =
1609 dlist_container(PROCLOCK, lockLink, proclock_iter.cur);
1610
1611 if (proclock != otherproclock &&
1612 proclock->groupLeader == otherproclock->groupLeader &&
1613 (otherproclock->holdMask & conflictMask) != 0)
1614 {
1615 int intersectMask = otherproclock->holdMask & conflictMask;
1616
1617 for (i = 1; i <= numLockModes; i++)
1618 {
1619 if ((intersectMask & LOCKBIT_ON(i)) != 0)
1620 {
1621 if (conflictsRemaining[i] <= 0)
1622 elog(PANIC, "proclocks held do not match lock");
1623 conflictsRemaining[i]--;
1624 totalConflictsRemaining--;
1625 }
1626 }
1627
1628 if (totalConflictsRemaining == 0)
1629 {
1630 PROCLOCK_PRINT("LockCheckConflicts: resolved (group)",
1631 proclock);
1632 return false;
1633 }
1634 }
1635 }
1636
1637 /* Nope, it's a real conflict. */
1638 PROCLOCK_PRINT("LockCheckConflicts: conflicting (group)", proclock);
1639 return true;
1640}
1641
1642/*
1643 * GrantLock -- update the lock and proclock data structures to show
1644 * the lock request has been granted.
1645 *
1646 * NOTE: if proc was blocked, it also needs to be removed from the wait list
1647 * and have its waitLock/waitProcLock fields cleared. That's not done here.
1648 *
1649 * NOTE: the lock grant also has to be recorded in the associated LOCALLOCK
1650 * table entry; but since we may be awaking some other process, we can't do
1651 * that here; it's done by GrantLockLocal, instead.
1652 */
1653void
1654GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
1655{
1656 lock->nGranted++;
1657 lock->granted[lockmode]++;
1658 lock->grantMask |= LOCKBIT_ON(lockmode);
1659 if (lock->granted[lockmode] == lock->requested[lockmode])
1660 lock->waitMask &= LOCKBIT_OFF(lockmode);
1661 proclock->holdMask |= LOCKBIT_ON(lockmode);
1662 LOCK_PRINT("GrantLock", lock, lockmode);
1663 Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
1664 Assert(lock->nGranted <= lock->nRequested);
1665}
1666
1667/*
1668 * UnGrantLock -- opposite of GrantLock.
1669 *
1670 * Updates the lock and proclock data structures to show that the lock
1671 * is no longer held nor requested by the current holder.
1672 *
1673 * Returns true if there were any waiters waiting on the lock that
1674 * should now be woken up with ProcLockWakeup.
1675 */
1676static bool
1677UnGrantLock(LOCK *lock, LOCKMODE lockmode,
1678 PROCLOCK *proclock, LockMethod lockMethodTable)
1679{
1680 bool wakeupNeeded = false;
1681
1682 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
1683 Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
1684 Assert(lock->nGranted <= lock->nRequested);
1685
1686 /*
1687 * fix the general lock stats
1688 */
1689 lock->nRequested--;
1690 lock->requested[lockmode]--;
1691 lock->nGranted--;
1692 lock->granted[lockmode]--;
1693
1694 if (lock->granted[lockmode] == 0)
1695 {
1696 /* change the conflict mask. No more of this lock type. */
1697 lock->grantMask &= LOCKBIT_OFF(lockmode);
1698 }
1699
1700 LOCK_PRINT("UnGrantLock: updated", lock, lockmode);
1701
1702 /*
1703 * We need only run ProcLockWakeup if the released lock conflicts with at
1704 * least one of the lock types requested by waiter(s). Otherwise whatever
1705 * conflict made them wait must still exist. NOTE: before MVCC, we could
1706 * skip wakeup if lock->granted[lockmode] was still positive. But that's
1707 * not true anymore, because the remaining granted locks might belong to
1708 * some waiter, who could now be awakened because he doesn't conflict with
1709 * his own locks.
1710 */
1711 if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
1712 wakeupNeeded = true;
1713
1714 /*
1715 * Now fix the per-proclock state.
1716 */
1717 proclock->holdMask &= LOCKBIT_OFF(lockmode);
1718 PROCLOCK_PRINT("UnGrantLock: updated", proclock);
1719
1720 return wakeupNeeded;
1721}
1722
1723/*
1724 * CleanUpLock -- clean up after releasing a lock. We garbage-collect the
1725 * proclock and lock objects if possible, and call ProcLockWakeup if there
1726 * are remaining requests and the caller says it's OK. (Normally, this
1727 * should be called after UnGrantLock, and wakeupNeeded is the result from
1728 * UnGrantLock.)
1729 *
1730 * The appropriate partition lock must be held at entry, and will be
1731 * held at exit.
1732 */
1733static void
1734CleanUpLock(LOCK *lock, PROCLOCK *proclock,
1735 LockMethod lockMethodTable, uint32 hashcode,
1736 bool wakeupNeeded)
1737{
1738 /*
1739 * If this was my last hold on this lock, delete my entry in the proclock
1740 * table.
1741 */
1742 if (proclock->holdMask == 0)
1743 {
1744 uint32 proclock_hashcode;
1745
1746 PROCLOCK_PRINT("CleanUpLock: deleting", proclock);
1747 dlist_delete(&proclock->lockLink);
1748 dlist_delete(&proclock->procLink);
1749 proclock_hashcode = ProcLockHashCode(&proclock->tag, hashcode);
1751 &(proclock->tag),
1752 proclock_hashcode,
1754 NULL))
1755 elog(PANIC, "proclock table corrupted");
1756 }
1757
1758 if (lock->nRequested == 0)
1759 {
1760 /*
1761 * The caller just released the last lock, so garbage-collect the lock
1762 * object.
1763 */
1764 LOCK_PRINT("CleanUpLock: deleting", lock, 0);
1767 &(lock->tag),
1768 hashcode,
1770 NULL))
1771 elog(PANIC, "lock table corrupted");
1772 }
1773 else if (wakeupNeeded)
1774 {
1775 /* There are waiters on this lock, so wake them up. */
1776 ProcLockWakeup(lockMethodTable, lock);
1777 }
1778}
1779
1780/*
1781 * GrantLockLocal -- update the locallock data structures to show
1782 * the lock request has been granted.
1783 *
1784 * We expect that LockAcquire made sure there is room to add a new
1785 * ResourceOwner entry.
1786 */
1787static void
1789{
1790 LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
1791 int i;
1792
1793 Assert(locallock->numLockOwners < locallock->maxLockOwners);
1794 /* Count the total */
1795 locallock->nLocks++;
1796 /* Count the per-owner lock */
1797 for (i = 0; i < locallock->numLockOwners; i++)
1798 {
1799 if (lockOwners[i].owner == owner)
1800 {
1801 lockOwners[i].nLocks++;
1802 return;
1803 }
1804 }
1805 lockOwners[i].owner = owner;
1806 lockOwners[i].nLocks = 1;
1807 locallock->numLockOwners++;
1808 if (owner != NULL)
1809 ResourceOwnerRememberLock(owner, locallock);
1810
1811 /* Indicate that the lock is acquired for certain types of locks. */
1812 CheckAndSetLockHeld(locallock, true);
1813}
1814
1815/*
1816 * BeginStrongLockAcquire - inhibit use of fastpath for a given LOCALLOCK,
1817 * and arrange for error cleanup if it fails
1818 */
1819static void
1820BeginStrongLockAcquire(LOCALLOCK *locallock, uint32 fasthashcode)
1821{
1823 Assert(locallock->holdsStrongLockCount == false);
1824
1825 /*
1826 * Adding to a memory location is not atomic, so we take a spinlock to
1827 * ensure we don't collide with someone else trying to bump the count at
1828 * the same time.
1829 *
1830 * XXX: It might be worth considering using an atomic fetch-and-add
1831 * instruction here, on architectures where that is supported.
1832 */
1833
1835 FastPathStrongRelationLocks->count[fasthashcode]++;
1836 locallock->holdsStrongLockCount = true;
1837 StrongLockInProgress = locallock;
1839}
1840
1841/*
1842 * FinishStrongLockAcquire - cancel pending cleanup for a strong lock
1843 * acquisition once it's no longer needed
1844 */
1845static void
1847{
1848 StrongLockInProgress = NULL;
1849}
1850
1851/*
1852 * AbortStrongLockAcquire - undo strong lock state changes performed by
1853 * BeginStrongLockAcquire.
1854 */
1855void
1857{
1858 uint32 fasthashcode;
1859 LOCALLOCK *locallock = StrongLockInProgress;
1860
1861 if (locallock == NULL)
1862 return;
1863
1864 fasthashcode = FastPathStrongLockHashPartition(locallock->hashcode);
1865 Assert(locallock->holdsStrongLockCount == true);
1867 Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
1868 FastPathStrongRelationLocks->count[fasthashcode]--;
1869 locallock->holdsStrongLockCount = false;
1870 StrongLockInProgress = NULL;
1872}
1873
1874/*
1875 * GrantAwaitedLock -- call GrantLockLocal for the lock we are doing
1876 * WaitOnLock on.
1877 *
1878 * proc.c needs this for the case where we are booted off the lock by
1879 * timeout, but discover that someone granted us the lock anyway.
1880 *
1881 * We could just export GrantLockLocal, but that would require including
1882 * resowner.h in lock.h, which creates circularity.
1883 */
1884void
1886{
1888}
1889
1890/*
1891 * GetAwaitedLock -- Return the lock we're currently doing WaitOnLock on.
1892 */
1893LOCALLOCK *
1895{
1896 return awaitedLock;
1897}
1898
1899/*
1900 * ResetAwaitedLock -- Forget that we are waiting on a lock.
1901 */
1902void
1904{
1905 awaitedLock = NULL;
1906}
1907
1908/*
1909 * MarkLockClear -- mark an acquired lock as "clear"
1910 *
1911 * This means that we know we have absorbed all sinval messages that other
1912 * sessions generated before we acquired this lock, and so we can confidently
1913 * assume we know about any catalog changes protected by this lock.
1914 */
1915void
1917{
1918 Assert(locallock->nLocks > 0);
1919 locallock->lockCleared = true;
1920}
1921
1922/*
1923 * WaitOnLock -- wait to acquire a lock
1924 *
1925 * This is a wrapper around ProcSleep, with extra tracing and bookkeeping.
1926 */
1927static ProcWaitStatus
1929{
1930 ProcWaitStatus result;
1931
1932 TRACE_POSTGRESQL_LOCK_WAIT_START(locallock->tag.lock.locktag_field1,
1933 locallock->tag.lock.locktag_field2,
1934 locallock->tag.lock.locktag_field3,
1935 locallock->tag.lock.locktag_field4,
1936 locallock->tag.lock.locktag_type,
1937 locallock->tag.mode);
1938
1939 /* adjust the process title to indicate that it's waiting */
1940 set_ps_display_suffix("waiting");
1941
1942 /*
1943 * Record the fact that we are waiting for a lock, so that
1944 * LockErrorCleanup will clean up if cancel/die happens.
1945 */
1946 awaitedLock = locallock;
1947 awaitedOwner = owner;
1948
1949 /*
1950 * NOTE: Think not to put any shared-state cleanup after the call to
1951 * ProcSleep, in either the normal or failure path. The lock state must
1952 * be fully set by the lock grantor, or by CheckDeadLock if we give up
1953 * waiting for the lock. This is necessary because of the possibility
1954 * that a cancel/die interrupt will interrupt ProcSleep after someone else
1955 * grants us the lock, but before we've noticed it. Hence, after granting,
1956 * the locktable state must fully reflect the fact that we own the lock;
1957 * we can't do additional work on return.
1958 *
1959 * We can and do use a PG_TRY block to try to clean up after failure, but
1960 * this still has a major limitation: elog(FATAL) can occur while waiting
1961 * (eg, a "die" interrupt), and then control won't come back here. So all
1962 * cleanup of essential state should happen in LockErrorCleanup, not here.
1963 * We can use PG_TRY to clear the "waiting" status flags, since doing that
1964 * is unimportant if the process exits.
1965 */
1966 PG_TRY();
1967 {
1968 result = ProcSleep(locallock);
1969 }
1970 PG_CATCH();
1971 {
1972 /* In this path, awaitedLock remains set until LockErrorCleanup */
1973
1974 /* reset ps display to remove the suffix */
1976
1977 /* and propagate the error */
1978 PG_RE_THROW();
1979 }
1980 PG_END_TRY();
1981
1982 /*
1983 * We no longer want LockErrorCleanup to do anything.
1984 */
1985 awaitedLock = NULL;
1986
1987 /* reset ps display to remove the suffix */
1989
1990 TRACE_POSTGRESQL_LOCK_WAIT_DONE(locallock->tag.lock.locktag_field1,
1991 locallock->tag.lock.locktag_field2,
1992 locallock->tag.lock.locktag_field3,
1993 locallock->tag.lock.locktag_field4,
1994 locallock->tag.lock.locktag_type,
1995 locallock->tag.mode);
1996
1997 return result;
1998}
1999
2000/*
2001 * Remove a proc from the wait-queue it is on (caller must know it is on one).
2002 * This is only used when the proc has failed to get the lock, so we set its
2003 * waitStatus to PROC_WAIT_STATUS_ERROR.
2004 *
2005 * Appropriate partition lock must be held by caller. Also, caller is
2006 * responsible for signaling the proc if needed.
2007 *
2008 * NB: this does not clean up any locallock object that may exist for the lock.
2009 */
2010void
2012{
2013 LOCK *waitLock = proc->waitLock;
2014 PROCLOCK *proclock = proc->waitProcLock;
2015 LOCKMODE lockmode = proc->waitLockMode;
2016 LOCKMETHODID lockmethodid = LOCK_LOCKMETHOD(*waitLock);
2017
2018 /* Make sure proc is waiting */
2020 Assert(proc->links.next != NULL);
2021 Assert(waitLock);
2022 Assert(!dclist_is_empty(&waitLock->waitProcs));
2023 Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
2024
2025 /* Remove proc from lock's wait queue */
2026 dclist_delete_from_thoroughly(&waitLock->waitProcs, &proc->links);
2027
2028 /* Undo increments of request counts by waiting process */
2029 Assert(waitLock->nRequested > 0);
2030 Assert(waitLock->nRequested > proc->waitLock->nGranted);
2031 waitLock->nRequested--;
2032 Assert(waitLock->requested[lockmode] > 0);
2033 waitLock->requested[lockmode]--;
2034 /* don't forget to clear waitMask bit if appropriate */
2035 if (waitLock->granted[lockmode] == waitLock->requested[lockmode])
2036 waitLock->waitMask &= LOCKBIT_OFF(lockmode);
2037
2038 /* Clean up the proc's own state, and pass it the ok/fail signal */
2039 proc->waitLock = NULL;
2040 proc->waitProcLock = NULL;
2042
2043 /*
2044 * Delete the proclock immediately if it represents no already-held locks.
2045 * (This must happen now because if the owner of the lock decides to
2046 * release it, and the requested/granted counts then go to zero,
2047 * LockRelease expects there to be no remaining proclocks.) Then see if
2048 * any other waiters for the lock can be woken up now.
2049 */
2050 CleanUpLock(waitLock, proclock,
2051 LockMethods[lockmethodid], hashcode,
2052 true);
2053}
2054
2055/*
2056 * LockRelease -- look up 'locktag' and release one 'lockmode' lock on it.
2057 * Release a session lock if 'sessionLock' is true, else release a
2058 * regular transaction lock.
2059 *
2060 * Side Effects: find any waiting processes that are now wakable,
2061 * grant them their requested locks and awaken them.
2062 * (We have to grant the lock here to avoid a race between
2063 * the waking process and any new process to
2064 * come along and request the lock.)
2065 */
2066bool
2067LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
2068{
2069 LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
2070 LockMethod lockMethodTable;
2071 LOCALLOCKTAG localtag;
2072 LOCALLOCK *locallock;
2073 LOCK *lock;
2074 PROCLOCK *proclock;
2075 LWLock *partitionLock;
2076 bool wakeupNeeded;
2077
2078 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
2079 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
2080 lockMethodTable = LockMethods[lockmethodid];
2081 if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
2082 elog(ERROR, "unrecognized lock mode: %d", lockmode);
2083
2084#ifdef LOCK_DEBUG
2085 if (LOCK_DEBUG_ENABLED(locktag))
2086 elog(LOG, "LockRelease: lock [%u,%u] %s",
2087 locktag->locktag_field1, locktag->locktag_field2,
2088 lockMethodTable->lockModeNames[lockmode]);
2089#endif
2090
2091 /*
2092 * Find the LOCALLOCK entry for this lock and lockmode
2093 */
2094 MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
2095 localtag.lock = *locktag;
2096 localtag.mode = lockmode;
2097
2099 &localtag,
2100 HASH_FIND, NULL);
2101
2102 /*
2103 * let the caller print its own error message, too. Do not ereport(ERROR).
2104 */
2105 if (!locallock || locallock->nLocks <= 0)
2106 {
2107 elog(WARNING, "you don't own a lock of type %s",
2108 lockMethodTable->lockModeNames[lockmode]);
2109 return false;
2110 }
2111
2112 /*
2113 * Decrease the count for the resource owner.
2114 */
2115 {
2116 LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
2117 ResourceOwner owner;
2118 int i;
2119
2120 /* Identify owner for lock */
2121 if (sessionLock)
2122 owner = NULL;
2123 else
2124 owner = CurrentResourceOwner;
2125
2126 for (i = locallock->numLockOwners - 1; i >= 0; i--)
2127 {
2128 if (lockOwners[i].owner == owner)
2129 {
2130 Assert(lockOwners[i].nLocks > 0);
2131 if (--lockOwners[i].nLocks == 0)
2132 {
2133 if (owner != NULL)
2134 ResourceOwnerForgetLock(owner, locallock);
2135 /* compact out unused slot */
2136 locallock->numLockOwners--;
2137 if (i < locallock->numLockOwners)
2138 lockOwners[i] = lockOwners[locallock->numLockOwners];
2139 }
2140 break;
2141 }
2142 }
2143 if (i < 0)
2144 {
2145 /* don't release a lock belonging to another owner */
2146 elog(WARNING, "you don't own a lock of type %s",
2147 lockMethodTable->lockModeNames[lockmode]);
2148 return false;
2149 }
2150 }
2151
2152 /*
2153 * Decrease the total local count. If we're still holding the lock, we're
2154 * done.
2155 */
2156 locallock->nLocks--;
2157
2158 if (locallock->nLocks > 0)
2159 return true;
2160
2161 /*
2162 * At this point we can no longer suppose we are clear of invalidation
2163 * messages related to this lock. Although we'll delete the LOCALLOCK
2164 * object before any intentional return from this routine, it seems worth
2165 * the trouble to explicitly reset lockCleared right now, just in case
2166 * some error prevents us from deleting the LOCALLOCK.
2167 */
2168 locallock->lockCleared = false;
2169
2170 /* Attempt fast release of any lock eligible for the fast path. */
2171 if (EligibleForRelationFastPath(locktag, lockmode) &&
2173 {
2174 bool released;
2175
2176 /*
2177 * We might not find the lock here, even if we originally entered it
2178 * here. Another backend may have moved it to the main table.
2179 */
2181 released = FastPathUnGrantRelationLock(locktag->locktag_field2,
2182 lockmode);
2184 if (released)
2185 {
2186 RemoveLocalLock(locallock);
2187 return true;
2188 }
2189 }
2190
2191 /*
2192 * Otherwise we've got to mess with the shared lock table.
2193 */
2194 partitionLock = LockHashPartitionLock(locallock->hashcode);
2195
2196 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
2197
2198 /*
2199 * Normally, we don't need to re-find the lock or proclock, since we kept
2200 * their addresses in the locallock table, and they couldn't have been
2201 * removed while we were holding a lock on them. But it's possible that
2202 * the lock was taken fast-path and has since been moved to the main hash
2203 * table by another backend, in which case we will need to look up the
2204 * objects here. We assume the lock field is NULL if so.
2205 */
2206 lock = locallock->lock;
2207 if (!lock)
2208 {
2209 PROCLOCKTAG proclocktag;
2210
2211 Assert(EligibleForRelationFastPath(locktag, lockmode));
2213 locktag,
2214 locallock->hashcode,
2215 HASH_FIND,
2216 NULL);
2217 if (!lock)
2218 elog(ERROR, "failed to re-find shared lock object");
2219 locallock->lock = lock;
2220
2221 proclocktag.myLock = lock;
2222 proclocktag.myProc = MyProc;
2224 &proclocktag,
2225 HASH_FIND,
2226 NULL);
2227 if (!locallock->proclock)
2228 elog(ERROR, "failed to re-find shared proclock object");
2229 }
2230 LOCK_PRINT("LockRelease: found", lock, lockmode);
2231 proclock = locallock->proclock;
2232 PROCLOCK_PRINT("LockRelease: found", proclock);
2233
2234 /*
2235 * Double-check that we are actually holding a lock of the type we want to
2236 * release.
2237 */
2238 if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
2239 {
2240 PROCLOCK_PRINT("LockRelease: WRONGTYPE", proclock);
2241 LWLockRelease(partitionLock);
2242 elog(WARNING, "you don't own a lock of type %s",
2243 lockMethodTable->lockModeNames[lockmode]);
2244 RemoveLocalLock(locallock);
2245 return false;
2246 }
2247
2248 /*
2249 * Do the releasing. CleanUpLock will waken any now-wakable waiters.
2250 */
2251 wakeupNeeded = UnGrantLock(lock, lockmode, proclock, lockMethodTable);
2252
2253 CleanUpLock(lock, proclock,
2254 lockMethodTable, locallock->hashcode,
2255 wakeupNeeded);
2256
2257 LWLockRelease(partitionLock);
2258
2259 RemoveLocalLock(locallock);
2260 return true;
2261}
2262
2263/*
2264 * LockReleaseAll -- Release all locks of the specified lock method that
2265 * are held by the current process.
2266 *
2267 * Well, not necessarily *all* locks. The available behaviors are:
2268 * allLocks == true: release all locks including session locks.
2269 * allLocks == false: release all non-session locks.
2270 */
2271void
2272LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
2273{
2274 HASH_SEQ_STATUS status;
2275 LockMethod lockMethodTable;
2276 int i,
2277 numLockModes;
2278 LOCALLOCK *locallock;
2279 LOCK *lock;
2280 int partition;
2281 bool have_fast_path_lwlock = false;
2282
2283 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
2284 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
2285 lockMethodTable = LockMethods[lockmethodid];
2286
2287#ifdef LOCK_DEBUG
2288 if (*(lockMethodTable->trace_flag))
2289 elog(LOG, "LockReleaseAll: lockmethod=%d", lockmethodid);
2290#endif
2291
2292 /*
2293 * Get rid of our fast-path VXID lock, if appropriate. Note that this is
2294 * the only way that the lock we hold on our own VXID can ever get
2295 * released: it is always and only released when a toplevel transaction
2296 * ends.
2297 */
2298 if (lockmethodid == DEFAULT_LOCKMETHOD)
2300
2301 numLockModes = lockMethodTable->numLockModes;
2302
2303 /*
2304 * First we run through the locallock table and get rid of unwanted
2305 * entries, then we scan the process's proclocks and get rid of those. We
2306 * do this separately because we may have multiple locallock entries
2307 * pointing to the same proclock, and we daren't end up with any dangling
2308 * pointers. Fast-path locks are cleaned up during the locallock table
2309 * scan, though.
2310 */
2312
2313 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
2314 {
2315 /*
2316 * If the LOCALLOCK entry is unused, something must've gone wrong
2317 * while trying to acquire this lock. Just forget the local entry.
2318 */
2319 if (locallock->nLocks == 0)
2320 {
2321 RemoveLocalLock(locallock);
2322 continue;
2323 }
2324
2325 /* Ignore items that are not of the lockmethod to be removed */
2326 if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
2327 continue;
2328
2329 /*
2330 * If we are asked to release all locks, we can just zap the entry.
2331 * Otherwise, must scan to see if there are session locks. We assume
2332 * there is at most one lockOwners entry for session locks.
2333 */
2334 if (!allLocks)
2335 {
2336 LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
2337
2338 /* If session lock is above array position 0, move it down to 0 */
2339 for (i = 0; i < locallock->numLockOwners; i++)
2340 {
2341 if (lockOwners[i].owner == NULL)
2342 lockOwners[0] = lockOwners[i];
2343 else
2344 ResourceOwnerForgetLock(lockOwners[i].owner, locallock);
2345 }
2346
2347 if (locallock->numLockOwners > 0 &&
2348 lockOwners[0].owner == NULL &&
2349 lockOwners[0].nLocks > 0)
2350 {
2351 /* Fix the locallock to show just the session locks */
2352 locallock->nLocks = lockOwners[0].nLocks;
2353 locallock->numLockOwners = 1;
2354 /* We aren't deleting this locallock, so done */
2355 continue;
2356 }
2357 else
2358 locallock->numLockOwners = 0;
2359 }
2360
2361#ifdef USE_ASSERT_CHECKING
2362
2363 /*
2364 * Tuple locks are currently held only for short durations within a
2365 * transaction. Check that we didn't forget to release one.
2366 */
2367 if (LOCALLOCK_LOCKTAG(*locallock) == LOCKTAG_TUPLE && !allLocks)
2368 elog(WARNING, "tuple lock held at commit");
2369#endif
2370
2371 /*
2372 * If the lock or proclock pointers are NULL, this lock was taken via
2373 * the relation fast-path (and is not known to have been transferred).
2374 */
2375 if (locallock->proclock == NULL || locallock->lock == NULL)
2376 {
2377 LOCKMODE lockmode = locallock->tag.mode;
2378 Oid relid;
2379
2380 /* Verify that a fast-path lock is what we've got. */
2381 if (!EligibleForRelationFastPath(&locallock->tag.lock, lockmode))
2382 elog(PANIC, "locallock table corrupted");
2383
2384 /*
2385 * If we don't currently hold the LWLock that protects our
2386 * fast-path data structures, we must acquire it before attempting
2387 * to release the lock via the fast-path. We will continue to
2388 * hold the LWLock until we're done scanning the locallock table,
2389 * unless we hit a transferred fast-path lock. (XXX is this
2390 * really such a good idea? There could be a lot of entries ...)
2391 */
2392 if (!have_fast_path_lwlock)
2393 {
2395 have_fast_path_lwlock = true;
2396 }
2397
2398 /* Attempt fast-path release. */
2399 relid = locallock->tag.lock.locktag_field2;
2400 if (FastPathUnGrantRelationLock(relid, lockmode))
2401 {
2402 RemoveLocalLock(locallock);
2403 continue;
2404 }
2405
2406 /*
2407 * Our lock, originally taken via the fast path, has been
2408 * transferred to the main lock table. That's going to require
2409 * some extra work, so release our fast-path lock before starting.
2410 */
2412 have_fast_path_lwlock = false;
2413
2414 /*
2415 * Now dump the lock. We haven't got a pointer to the LOCK or
2416 * PROCLOCK in this case, so we have to handle this a bit
2417 * differently than a normal lock release. Unfortunately, this
2418 * requires an extra LWLock acquire-and-release cycle on the
2419 * partitionLock, but hopefully it shouldn't happen often.
2420 */
2421 LockRefindAndRelease(lockMethodTable, MyProc,
2422 &locallock->tag.lock, lockmode, false);
2423 RemoveLocalLock(locallock);
2424 continue;
2425 }
2426
2427 /* Mark the proclock to show we need to release this lockmode */
2428 if (locallock->nLocks > 0)
2429 locallock->proclock->releaseMask |= LOCKBIT_ON(locallock->tag.mode);
2430
2431 /* And remove the locallock hashtable entry */
2432 RemoveLocalLock(locallock);
2433 }
2434
2435 /* Done with the fast-path data structures */
2436 if (have_fast_path_lwlock)
2438
2439 /*
2440 * Now, scan each lock partition separately.
2441 */
2442 for (partition = 0; partition < NUM_LOCK_PARTITIONS; partition++)
2443 {
2444 LWLock *partitionLock;
2445 dlist_head *procLocks = &MyProc->myProcLocks[partition];
2446 dlist_mutable_iter proclock_iter;
2447
2448 partitionLock = LockHashPartitionLockByIndex(partition);
2449
2450 /*
2451 * If the proclock list for this partition is empty, we can skip
2452 * acquiring the partition lock. This optimization is trickier than
2453 * it looks, because another backend could be in process of adding
2454 * something to our proclock list due to promoting one of our
2455 * fast-path locks. However, any such lock must be one that we
2456 * decided not to delete above, so it's okay to skip it again now;
2457 * we'd just decide not to delete it again. We must, however, be
2458 * careful to re-fetch the list header once we've acquired the
2459 * partition lock, to be sure we have a valid, up-to-date pointer.
2460 * (There is probably no significant risk if pointer fetch/store is
2461 * atomic, but we don't wish to assume that.)
2462 *
2463 * XXX This argument assumes that the locallock table correctly
2464 * represents all of our fast-path locks. While allLocks mode
2465 * guarantees to clean up all of our normal locks regardless of the
2466 * locallock situation, we lose that guarantee for fast-path locks.
2467 * This is not ideal.
2468 */
2469 if (dlist_is_empty(procLocks))
2470 continue; /* needn't examine this partition */
2471
2472 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
2473
2474 dlist_foreach_modify(proclock_iter, procLocks)
2475 {
2476 PROCLOCK *proclock = dlist_container(PROCLOCK, procLink, proclock_iter.cur);
2477 bool wakeupNeeded = false;
2478
2479 Assert(proclock->tag.myProc == MyProc);
2480
2481 lock = proclock->tag.myLock;
2482
2483 /* Ignore items that are not of the lockmethod to be removed */
2484 if (LOCK_LOCKMETHOD(*lock) != lockmethodid)
2485 continue;
2486
2487 /*
2488 * In allLocks mode, force release of all locks even if locallock
2489 * table had problems
2490 */
2491 if (allLocks)
2492 proclock->releaseMask = proclock->holdMask;
2493 else
2494 Assert((proclock->releaseMask & ~proclock->holdMask) == 0);
2495
2496 /*
2497 * Ignore items that have nothing to be released, unless they have
2498 * holdMask == 0 and are therefore recyclable
2499 */
2500 if (proclock->releaseMask == 0 && proclock->holdMask != 0)
2501 continue;
2502
2503 PROCLOCK_PRINT("LockReleaseAll", proclock);
2504 LOCK_PRINT("LockReleaseAll", lock, 0);
2505 Assert(lock->nRequested >= 0);
2506 Assert(lock->nGranted >= 0);
2507 Assert(lock->nGranted <= lock->nRequested);
2508 Assert((proclock->holdMask & ~lock->grantMask) == 0);
2509
2510 /*
2511 * Release the previously-marked lock modes
2512 */
2513 for (i = 1; i <= numLockModes; i++)
2514 {
2515 if (proclock->releaseMask & LOCKBIT_ON(i))
2516 wakeupNeeded |= UnGrantLock(lock, i, proclock,
2517 lockMethodTable);
2518 }
2519 Assert((lock->nRequested >= 0) && (lock->nGranted >= 0));
2520 Assert(lock->nGranted <= lock->nRequested);
2521 LOCK_PRINT("LockReleaseAll: updated", lock, 0);
2522
2523 proclock->releaseMask = 0;
2524
2525 /* CleanUpLock will wake up waiters if needed. */
2526 CleanUpLock(lock, proclock,
2527 lockMethodTable,
2528 LockTagHashCode(&lock->tag),
2529 wakeupNeeded);
2530 } /* loop over PROCLOCKs within this partition */
2531
2532 LWLockRelease(partitionLock);
2533 } /* loop over partitions */
2534
2535#ifdef LOCK_DEBUG
2536 if (*(lockMethodTable->trace_flag))
2537 elog(LOG, "LockReleaseAll done");
2538#endif
2539}
2540
2541/*
2542 * LockReleaseSession -- Release all session locks of the specified lock method
2543 * that are held by the current process.
2544 */
2545void
2547{
2548 HASH_SEQ_STATUS status;
2549 LOCALLOCK *locallock;
2550
2551 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
2552 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
2553
2555
2556 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
2557 {
2558 /* Ignore items that are not of the specified lock method */
2559 if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
2560 continue;
2561
2562 ReleaseLockIfHeld(locallock, true);
2563 }
2564}
2565
2566/*
2567 * LockReleaseCurrentOwner
2568 * Release all locks belonging to CurrentResourceOwner
2569 *
2570 * If the caller knows what those locks are, it can pass them as an array.
2571 * That speeds up the call significantly, when a lot of locks are held.
2572 * Otherwise, pass NULL for locallocks, and we'll traverse through our hash
2573 * table to find them.
2574 */
2575void
2576LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks)
2577{
2578 if (locallocks == NULL)
2579 {
2580 HASH_SEQ_STATUS status;
2581 LOCALLOCK *locallock;
2582
2584
2585 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
2586 ReleaseLockIfHeld(locallock, false);
2587 }
2588 else
2589 {
2590 int i;
2591
2592 for (i = nlocks - 1; i >= 0; i--)
2593 ReleaseLockIfHeld(locallocks[i], false);
2594 }
2595}
2596
2597/*
2598 * ReleaseLockIfHeld
2599 * Release any session-level locks on this lockable object if sessionLock
2600 * is true; else, release any locks held by CurrentResourceOwner.
2601 *
2602 * It is tempting to pass this a ResourceOwner pointer (or NULL for session
2603 * locks), but without refactoring LockRelease() we cannot support releasing
2604 * locks belonging to resource owners other than CurrentResourceOwner.
2605 * If we were to refactor, it'd be a good idea to fix it so we don't have to
2606 * do a hashtable lookup of the locallock, too. However, currently this
2607 * function isn't used heavily enough to justify refactoring for its
2608 * convenience.
2609 */
2610static void
2611ReleaseLockIfHeld(LOCALLOCK *locallock, bool sessionLock)
2612{
2613 ResourceOwner owner;
2614 LOCALLOCKOWNER *lockOwners;
2615 int i;
2616
2617 /* Identify owner for lock (must match LockRelease!) */
2618 if (sessionLock)
2619 owner = NULL;
2620 else
2621 owner = CurrentResourceOwner;
2622
2623 /* Scan to see if there are any locks belonging to the target owner */
2624 lockOwners = locallock->lockOwners;
2625 for (i = locallock->numLockOwners - 1; i >= 0; i--)
2626 {
2627 if (lockOwners[i].owner == owner)
2628 {
2629 Assert(lockOwners[i].nLocks > 0);
2630 if (lockOwners[i].nLocks < locallock->nLocks)
2631 {
2632 /*
2633 * We will still hold this lock after forgetting this
2634 * ResourceOwner.
2635 */
2636 locallock->nLocks -= lockOwners[i].nLocks;
2637 /* compact out unused slot */
2638 locallock->numLockOwners--;
2639 if (owner != NULL)
2640 ResourceOwnerForgetLock(owner, locallock);
2641 if (i < locallock->numLockOwners)
2642 lockOwners[i] = lockOwners[locallock->numLockOwners];
2643 }
2644 else
2645 {
2646 Assert(lockOwners[i].nLocks == locallock->nLocks);
2647 /* We want to call LockRelease just once */
2648 lockOwners[i].nLocks = 1;
2649 locallock->nLocks = 1;
2650 if (!LockRelease(&locallock->tag.lock,
2651 locallock->tag.mode,
2652 sessionLock))
2653 elog(WARNING, "ReleaseLockIfHeld: failed??");
2654 }
2655 break;
2656 }
2657 }
2658}
2659
2660/*
2661 * LockReassignCurrentOwner
2662 * Reassign all locks belonging to CurrentResourceOwner to belong
2663 * to its parent resource owner.
2664 *
2665 * If the caller knows what those locks are, it can pass them as an array.
2666 * That speeds up the call significantly, when a lot of locks are held
2667 * (e.g pg_dump with a large schema). Otherwise, pass NULL for locallocks,
2668 * and we'll traverse through our hash table to find them.
2669 */
2670void
2671LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
2672{
2674
2675 Assert(parent != NULL);
2676
2677 if (locallocks == NULL)
2678 {
2679 HASH_SEQ_STATUS status;
2680 LOCALLOCK *locallock;
2681
2683
2684 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
2685 LockReassignOwner(locallock, parent);
2686 }
2687 else
2688 {
2689 int i;
2690
2691 for (i = nlocks - 1; i >= 0; i--)
2692 LockReassignOwner(locallocks[i], parent);
2693 }
2694}
2695
2696/*
2697 * Subroutine of LockReassignCurrentOwner. Reassigns a given lock belonging to
2698 * CurrentResourceOwner to its parent.
2699 */
2700static void
2702{
2703 LOCALLOCKOWNER *lockOwners;
2704 int i;
2705 int ic = -1;
2706 int ip = -1;
2707
2708 /*
2709 * Scan to see if there are any locks belonging to current owner or its
2710 * parent
2711 */
2712 lockOwners = locallock->lockOwners;
2713 for (i = locallock->numLockOwners - 1; i >= 0; i--)
2714 {
2715 if (lockOwners[i].owner == CurrentResourceOwner)
2716 ic = i;
2717 else if (lockOwners[i].owner == parent)
2718 ip = i;
2719 }
2720
2721 if (ic < 0)
2722 return; /* no current locks */
2723
2724 if (ip < 0)
2725 {
2726 /* Parent has no slot, so just give it the child's slot */
2727 lockOwners[ic].owner = parent;
2728 ResourceOwnerRememberLock(parent, locallock);
2729 }
2730 else
2731 {
2732 /* Merge child's count with parent's */
2733 lockOwners[ip].nLocks += lockOwners[ic].nLocks;
2734 /* compact out unused slot */
2735 locallock->numLockOwners--;
2736 if (ic < locallock->numLockOwners)
2737 lockOwners[ic] = lockOwners[locallock->numLockOwners];
2738 }
2740}
2741
2742/*
2743 * FastPathGrantRelationLock
2744 * Grant lock using per-backend fast-path array, if there is space.
2745 */
2746static bool
2748{
2749 uint32 i;
2750 uint32 unused_slot = FastPathLockSlotsPerBackend();
2751
2752 /* fast-path group the lock belongs to */
2753 uint32 group = FAST_PATH_REL_GROUP(relid);
2754
2755 /* Scan for existing entry for this relid, remembering empty slot. */
2756 for (i = 0; i < FP_LOCK_SLOTS_PER_GROUP; i++)
2757 {
2758 /* index into the whole per-backend array */
2759 uint32 f = FAST_PATH_SLOT(group, i);
2760
2761 if (FAST_PATH_GET_BITS(MyProc, f) == 0)
2762 unused_slot = f;
2763 else if (MyProc->fpRelId[f] == relid)
2764 {
2765 Assert(!FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode));
2766 FAST_PATH_SET_LOCKMODE(MyProc, f, lockmode);
2767 return true;
2768 }
2769 }
2770
2771 /* If no existing entry, use any empty slot. */
2772 if (unused_slot < FastPathLockSlotsPerBackend())
2773 {
2774 MyProc->fpRelId[unused_slot] = relid;
2775 FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode);
2776 ++FastPathLocalUseCounts[group];
2777 return true;
2778 }
2779
2780 /* No existing entry, and no empty slot. */
2781 return false;
2782}
2783
2784/*
2785 * FastPathUnGrantRelationLock
2786 * Release fast-path lock, if present. Update backend-private local
2787 * use count, while we're at it.
2788 */
2789static bool
2791{
2792 uint32 i;
2793 bool result = false;
2794
2795 /* fast-path group the lock belongs to */
2796 uint32 group = FAST_PATH_REL_GROUP(relid);
2797
2798 FastPathLocalUseCounts[group] = 0;
2799 for (i = 0; i < FP_LOCK_SLOTS_PER_GROUP; i++)
2800 {
2801 /* index into the whole per-backend array */
2802 uint32 f = FAST_PATH_SLOT(group, i);
2803
2804 if (MyProc->fpRelId[f] == relid
2805 && FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode))
2806 {
2807 Assert(!result);
2808 FAST_PATH_CLEAR_LOCKMODE(MyProc, f, lockmode);
2809 result = true;
2810 /* we continue iterating so as to update FastPathLocalUseCount */
2811 }
2812 if (FAST_PATH_GET_BITS(MyProc, f) != 0)
2813 ++FastPathLocalUseCounts[group];
2814 }
2815 return result;
2816}
2817
2818/*
2819 * FastPathTransferRelationLocks
2820 * Transfer locks matching the given lock tag from per-backend fast-path
2821 * arrays to the shared hash table.
2822 *
2823 * Returns true if successful, false if ran out of shared memory.
2824 */
2825static bool
2826FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag,
2827 uint32 hashcode)
2828{
2829 LWLock *partitionLock = LockHashPartitionLock(hashcode);
2830 Oid relid = locktag->locktag_field2;
2831 uint32 i;
2832
2833 /* fast-path group the lock belongs to */
2834 uint32 group = FAST_PATH_REL_GROUP(relid);
2835
2836 /*
2837 * Every PGPROC that can potentially hold a fast-path lock is present in
2838 * ProcGlobal->allProcs. Prepared transactions are not, but any
2839 * outstanding fast-path locks held by prepared transactions are
2840 * transferred to the main lock table.
2841 */
2842 for (i = 0; i < ProcGlobal->allProcCount; i++)
2843 {
2844 PGPROC *proc = &ProcGlobal->allProcs[i];
2845 uint32 j;
2846
2848
2849 /*
2850 * If the target backend isn't referencing the same database as the
2851 * lock, then we needn't examine the individual relation IDs at all;
2852 * none of them can be relevant.
2853 *
2854 * proc->databaseId is set at backend startup time and never changes
2855 * thereafter, so it might be safe to perform this test before
2856 * acquiring &proc->fpInfoLock. In particular, it's certainly safe to
2857 * assume that if the target backend holds any fast-path locks, it
2858 * must have performed a memory-fencing operation (in particular, an
2859 * LWLock acquisition) since setting proc->databaseId. However, it's
2860 * less clear that our backend is certain to have performed a memory
2861 * fencing operation since the other backend set proc->databaseId. So
2862 * for now, we test it after acquiring the LWLock just to be safe.
2863 *
2864 * Also skip groups without any registered fast-path locks.
2865 */
2866 if (proc->databaseId != locktag->locktag_field1 ||
2867 proc->fpLockBits[group] == 0)
2868 {
2869 LWLockRelease(&proc->fpInfoLock);
2870 continue;
2871 }
2872
2873 for (j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
2874 {
2875 uint32 lockmode;
2876
2877 /* index into the whole per-backend array */
2878 uint32 f = FAST_PATH_SLOT(group, j);
2879
2880 /* Look for an allocated slot matching the given relid. */
2881 if (relid != proc->fpRelId[f] || FAST_PATH_GET_BITS(proc, f) == 0)
2882 continue;
2883
2884 /* Find or create lock object. */
2885 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
2886 for (lockmode = FAST_PATH_LOCKNUMBER_OFFSET;
2888 ++lockmode)
2889 {
2890 PROCLOCK *proclock;
2891
2892 if (!FAST_PATH_CHECK_LOCKMODE(proc, f, lockmode))
2893 continue;
2894 proclock = SetupLockInTable(lockMethodTable, proc, locktag,
2895 hashcode, lockmode);
2896 if (!proclock)
2897 {
2898 LWLockRelease(partitionLock);
2899 LWLockRelease(&proc->fpInfoLock);
2900 return false;
2901 }
2902 GrantLock(proclock->tag.myLock, proclock, lockmode);
2903 FAST_PATH_CLEAR_LOCKMODE(proc, f, lockmode);
2904 }
2905 LWLockRelease(partitionLock);
2906
2907 /* No need to examine remaining slots. */
2908 break;
2909 }
2910 LWLockRelease(&proc->fpInfoLock);
2911 }
2912 return true;
2913}
2914
2915/*
2916 * FastPathGetRelationLockEntry
2917 * Return the PROCLOCK for a lock originally taken via the fast-path,
2918 * transferring it to the primary lock table if necessary.
2919 *
2920 * Note: caller takes care of updating the locallock object.
2921 */
2922static PROCLOCK *
2924{
2925 LockMethod lockMethodTable = LockMethods[DEFAULT_LOCKMETHOD];
2926 LOCKTAG *locktag = &locallock->tag.lock;
2927 PROCLOCK *proclock = NULL;
2928 LWLock *partitionLock = LockHashPartitionLock(locallock->hashcode);
2929 Oid relid = locktag->locktag_field2;
2930 uint32 i,
2931 group;
2932
2933 /* fast-path group the lock belongs to */
2934 group = FAST_PATH_REL_GROUP(relid);
2935
2937
2938 for (i = 0; i < FP_LOCK_SLOTS_PER_GROUP; i++)
2939 {
2940 uint32 lockmode;
2941
2942 /* index into the whole per-backend array */
2943 uint32 f = FAST_PATH_SLOT(group, i);
2944
2945 /* Look for an allocated slot matching the given relid. */
2946 if (relid != MyProc->fpRelId[f] || FAST_PATH_GET_BITS(MyProc, f) == 0)
2947 continue;
2948
2949 /* If we don't have a lock of the given mode, forget it! */
2950 lockmode = locallock->tag.mode;
2951 if (!FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode))
2952 break;
2953
2954 /* Find or create lock object. */
2955 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
2956
2957 proclock = SetupLockInTable(lockMethodTable, MyProc, locktag,
2958 locallock->hashcode, lockmode);
2959 if (!proclock)
2960 {
2961 LWLockRelease(partitionLock);
2963 ereport(ERROR,
2964 (errcode(ERRCODE_OUT_OF_MEMORY),
2965 errmsg("out of shared memory"),
2966 errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
2967 }
2968 GrantLock(proclock->tag.myLock, proclock, lockmode);
2969 FAST_PATH_CLEAR_LOCKMODE(MyProc, f, lockmode);
2970
2971 LWLockRelease(partitionLock);
2972
2973 /* No need to examine remaining slots. */
2974 break;
2975 }
2976
2978
2979 /* Lock may have already been transferred by some other backend. */
2980 if (proclock == NULL)
2981 {
2982 LOCK *lock;
2983 PROCLOCKTAG proclocktag;
2984 uint32 proclock_hashcode;
2985
2986 LWLockAcquire(partitionLock, LW_SHARED);
2987
2989 locktag,
2990 locallock->hashcode,
2991 HASH_FIND,
2992 NULL);
2993 if (!lock)
2994 elog(ERROR, "failed to re-find shared lock object");
2995
2996 proclocktag.myLock = lock;
2997 proclocktag.myProc = MyProc;
2998
2999 proclock_hashcode = ProcLockHashCode(&proclocktag, locallock->hashcode);
3000 proclock = (PROCLOCK *)
3002 &proclocktag,
3003 proclock_hashcode,
3004 HASH_FIND,
3005 NULL);
3006 if (!proclock)
3007 elog(ERROR, "failed to re-find shared proclock object");
3008 LWLockRelease(partitionLock);
3009 }
3010
3011 return proclock;
3012}
3013
3014/*
3015 * GetLockConflicts
3016 * Get an array of VirtualTransactionIds of xacts currently holding locks
3017 * that would conflict with the specified lock/lockmode.
3018 * xacts merely awaiting such a lock are NOT reported.
3019 *
3020 * The result array is palloc'd and is terminated with an invalid VXID.
3021 * *countp, if not null, is updated to the number of items set.
3022 *
3023 * Of course, the result could be out of date by the time it's returned, so
3024 * use of this function has to be thought about carefully. Similarly, a
3025 * PGPROC with no "lxid" will be considered non-conflicting regardless of any
3026 * lock it holds. Existing callers don't care about a locker after that
3027 * locker's pg_xact updates complete. CommitTransaction() clears "lxid" after
3028 * pg_xact updates and before releasing locks.
3029 *
3030 * Note we never include the current xact's vxid in the result array,
3031 * since an xact never blocks itself.
3032 */
3034GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
3035{
3036 static VirtualTransactionId *vxids;
3037 LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
3038 LockMethod lockMethodTable;
3039 LOCK *lock;
3040 LOCKMASK conflictMask;
3041 dlist_iter proclock_iter;
3042 PROCLOCK *proclock;
3043 uint32 hashcode;
3044 LWLock *partitionLock;
3045 int count = 0;
3046 int fast_count = 0;
3047
3048 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
3049 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
3050 lockMethodTable = LockMethods[lockmethodid];
3051 if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
3052 elog(ERROR, "unrecognized lock mode: %d", lockmode);
3053
3054 /*
3055 * Allocate memory to store results, and fill with InvalidVXID. We only
3056 * need enough space for MaxBackends + max_prepared_xacts + a terminator.
3057 * InHotStandby allocate once in TopMemoryContext.
3058 */
3059 if (InHotStandby)
3060 {
3061 if (vxids == NULL)
3062 vxids = (VirtualTransactionId *)
3064 sizeof(VirtualTransactionId) *
3066 }
3067 else
3068 vxids = (VirtualTransactionId *)
3071
3072 /* Compute hash code and partition lock, and look up conflicting modes. */
3073 hashcode = LockTagHashCode(locktag);
3074 partitionLock = LockHashPartitionLock(hashcode);
3075 conflictMask = lockMethodTable->conflictTab[lockmode];
3076
3077 /*
3078 * Fast path locks might not have been entered in the primary lock table.
3079 * If the lock we're dealing with could conflict with such a lock, we must
3080 * examine each backend's fast-path array for conflicts.
3081 */
3082 if (ConflictsWithRelationFastPath(locktag, lockmode))
3083 {
3084 int i;
3085 Oid relid = locktag->locktag_field2;
3087
3088 /* fast-path group the lock belongs to */
3089 uint32 group = FAST_PATH_REL_GROUP(relid);
3090
3091 /*
3092 * Iterate over relevant PGPROCs. Anything held by a prepared
3093 * transaction will have been transferred to the primary lock table,
3094 * so we need not worry about those. This is all a bit fuzzy, because
3095 * new locks could be taken after we've visited a particular
3096 * partition, but the callers had better be prepared to deal with that
3097 * anyway, since the locks could equally well be taken between the
3098 * time we return the value and the time the caller does something
3099 * with it.
3100 */
3101 for (i = 0; i < ProcGlobal->allProcCount; i++)
3102 {
3103 PGPROC *proc = &ProcGlobal->allProcs[i];
3104 uint32 j;
3105
3106 /* A backend never blocks itself */
3107 if (proc == MyProc)
3108 continue;
3109
3111
3112 /*
3113 * If the target backend isn't referencing the same database as
3114 * the lock, then we needn't examine the individual relation IDs
3115 * at all; none of them can be relevant.
3116 *
3117 * See FastPathTransferRelationLocks() for discussion of why we do
3118 * this test after acquiring the lock.
3119 *
3120 * Also skip groups without any registered fast-path locks.
3121 */
3122 if (proc->databaseId != locktag->locktag_field1 ||
3123 proc->fpLockBits[group] == 0)
3124 {
3125 LWLockRelease(&proc->fpInfoLock);
3126 continue;
3127 }
3128
3129 for (j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
3130 {
3131 uint32 lockmask;
3132
3133 /* index into the whole per-backend array */
3134 uint32 f = FAST_PATH_SLOT(group, j);
3135
3136 /* Look for an allocated slot matching the given relid. */
3137 if (relid != proc->fpRelId[f])
3138 continue;
3139 lockmask = FAST_PATH_GET_BITS(proc, f);
3140 if (!lockmask)
3141 continue;
3142 lockmask <<= FAST_PATH_LOCKNUMBER_OFFSET;
3143
3144 /*
3145 * There can only be one entry per relation, so if we found it
3146 * and it doesn't conflict, we can skip the rest of the slots.
3147 */
3148 if ((lockmask & conflictMask) == 0)
3149 break;
3150
3151 /* Conflict! */
3152 GET_VXID_FROM_PGPROC(vxid, *proc);
3153
3155 vxids[count++] = vxid;
3156 /* else, xact already committed or aborted */
3157
3158 /* No need to examine remaining slots. */
3159 break;
3160 }
3161
3162 LWLockRelease(&proc->fpInfoLock);
3163 }
3164 }
3165
3166 /* Remember how many fast-path conflicts we found. */
3167 fast_count = count;
3168
3169 /*
3170 * Look up the lock object matching the tag.
3171 */
3172 LWLockAcquire(partitionLock, LW_SHARED);
3173
3175 locktag,
3176 hashcode,
3177 HASH_FIND,
3178 NULL);
3179 if (!lock)
3180 {
3181 /*
3182 * If the lock object doesn't exist, there is nothing holding a lock
3183 * on this lockable object.
3184 */
3185 LWLockRelease(partitionLock);
3186 vxids[count].procNumber = INVALID_PROC_NUMBER;
3188 if (countp)
3189 *countp = count;
3190 return vxids;
3191 }
3192
3193 /*
3194 * Examine each existing holder (or awaiter) of the lock.
3195 */
3196 dlist_foreach(proclock_iter, &lock->procLocks)
3197 {
3198 proclock = dlist_container(PROCLOCK, lockLink, proclock_iter.cur);
3199
3200 if (conflictMask & proclock->holdMask)
3201 {
3202 PGPROC *proc = proclock->tag.myProc;
3203
3204 /* A backend never blocks itself */
3205 if (proc != MyProc)
3206 {
3208
3209 GET_VXID_FROM_PGPROC(vxid, *proc);
3210
3212 {
3213 int i;
3214
3215 /* Avoid duplicate entries. */
3216 for (i = 0; i < fast_count; ++i)
3217 if (VirtualTransactionIdEquals(vxids[i], vxid))
3218 break;
3219 if (i >= fast_count)
3220 vxids[count++] = vxid;
3221 }
3222 /* else, xact already committed or aborted */
3223 }
3224 }
3225 }
3226
3227 LWLockRelease(partitionLock);
3228
3229 if (count > MaxBackends + max_prepared_xacts) /* should never happen */
3230 elog(PANIC, "too many conflicting locks found");
3231
3232 vxids[count].procNumber = INVALID_PROC_NUMBER;
3234 if (countp)
3235 *countp = count;
3236 return vxids;
3237}
3238
3239/*
3240 * Find a lock in the shared lock table and release it. It is the caller's
3241 * responsibility to verify that this is a sane thing to do. (For example, it
3242 * would be bad to release a lock here if there might still be a LOCALLOCK
3243 * object with pointers to it.)
3244 *
3245 * We currently use this in two situations: first, to release locks held by
3246 * prepared transactions on commit (see lock_twophase_postcommit); and second,
3247 * to release locks taken via the fast-path, transferred to the main hash
3248 * table, and then released (see LockReleaseAll).
3249 */
3250static void
3252 LOCKTAG *locktag, LOCKMODE lockmode,
3253 bool decrement_strong_lock_count)
3254{
3255 LOCK *lock;
3256 PROCLOCK *proclock;
3257 PROCLOCKTAG proclocktag;
3258 uint32 hashcode;
3259 uint32 proclock_hashcode;
3260 LWLock *partitionLock;
3261 bool wakeupNeeded;
3262
3263 hashcode = LockTagHashCode(locktag);
3264 partitionLock = LockHashPartitionLock(hashcode);
3265
3266 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
3267
3268 /*
3269 * Re-find the lock object (it had better be there).
3270 */
3272 locktag,
3273 hashcode,
3274 HASH_FIND,
3275 NULL);
3276 if (!lock)
3277 elog(PANIC, "failed to re-find shared lock object");
3278
3279 /*
3280 * Re-find the proclock object (ditto).
3281 */
3282 proclocktag.myLock = lock;
3283 proclocktag.myProc = proc;
3284
3285 proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode);
3286
3288 &proclocktag,
3289 proclock_hashcode,
3290 HASH_FIND,
3291 NULL);
3292 if (!proclock)
3293 elog(PANIC, "failed to re-find shared proclock object");
3294
3295 /*
3296 * Double-check that we are actually holding a lock of the type we want to
3297 * release.
3298 */
3299 if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
3300 {
3301 PROCLOCK_PRINT("lock_twophase_postcommit: WRONGTYPE", proclock);
3302 LWLockRelease(partitionLock);
3303 elog(WARNING, "you don't own a lock of type %s",
3304 lockMethodTable->lockModeNames[lockmode]);
3305 return;
3306 }
3307
3308 /*
3309 * Do the releasing. CleanUpLock will waken any now-wakable waiters.
3310 */
3311 wakeupNeeded = UnGrantLock(lock, lockmode, proclock, lockMethodTable);
3312
3313 CleanUpLock(lock, proclock,
3314 lockMethodTable, hashcode,
3315 wakeupNeeded);
3316
3317 LWLockRelease(partitionLock);
3318
3319 /*
3320 * Decrement strong lock count. This logic is needed only for 2PC.
3321 */
3322 if (decrement_strong_lock_count
3323 && ConflictsWithRelationFastPath(locktag, lockmode))
3324 {
3325 uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode);
3326
3328 Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
3329 FastPathStrongRelationLocks->count[fasthashcode]--;
3331 }
3332}
3333
3334/*
3335 * CheckForSessionAndXactLocks
3336 * Check to see if transaction holds both session-level and xact-level
3337 * locks on the same object; if so, throw an error.
3338 *
3339 * If we have both session- and transaction-level locks on the same object,
3340 * PREPARE TRANSACTION must fail. This should never happen with regular
3341 * locks, since we only take those at session level in some special operations
3342 * like VACUUM. It's possible to hit this with advisory locks, though.
3343 *
3344 * It would be nice if we could keep the session hold and give away the
3345 * transactional hold to the prepared xact. However, that would require two
3346 * PROCLOCK objects, and we cannot be sure that another PROCLOCK will be
3347 * available when it comes time for PostPrepare_Locks to do the deed.
3348 * So for now, we error out while we can still do so safely.
3349 *
3350 * Since the LOCALLOCK table stores a separate entry for each lockmode,
3351 * we can't implement this check by examining LOCALLOCK entries in isolation.
3352 * We must build a transient hashtable that is indexed by locktag only.
3353 */
3354static void
3356{
3357 typedef struct
3358 {
3359 LOCKTAG lock; /* identifies the lockable object */
3360 bool sessLock; /* is any lockmode held at session level? */
3361 bool xactLock; /* is any lockmode held at xact level? */
3362 } PerLockTagEntry;
3363
3364 HASHCTL hash_ctl;
3365 HTAB *lockhtab;
3366 HASH_SEQ_STATUS status;
3367 LOCALLOCK *locallock;
3368
3369 /* Create a local hash table keyed by LOCKTAG only */
3370 hash_ctl.keysize = sizeof(LOCKTAG);
3371 hash_ctl.entrysize = sizeof(PerLockTagEntry);
3372 hash_ctl.hcxt = CurrentMemoryContext;
3373
3374 lockhtab = hash_create("CheckForSessionAndXactLocks table",
3375 256, /* arbitrary initial size */
3376 &hash_ctl,
3378
3379 /* Scan local lock table to find entries for each LOCKTAG */
3381
3382 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
3383 {
3384 LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
3385 PerLockTagEntry *hentry;
3386 bool found;
3387 int i;
3388
3389 /*
3390 * Ignore VXID locks. We don't want those to be held by prepared
3391 * transactions, since they aren't meaningful after a restart.
3392 */
3394 continue;
3395
3396 /* Ignore it if we don't actually hold the lock */
3397 if (locallock->nLocks <= 0)
3398 continue;
3399
3400 /* Otherwise, find or make an entry in lockhtab */
3401 hentry = (PerLockTagEntry *) hash_search(lockhtab,
3402 &locallock->tag.lock,
3403 HASH_ENTER, &found);
3404 if (!found) /* initialize, if newly created */
3405 hentry->sessLock = hentry->xactLock = false;
3406
3407 /* Scan to see if we hold lock at session or xact level or both */
3408 for (i = locallock->numLockOwners - 1; i >= 0; i--)
3409 {
3410 if (lockOwners[i].owner == NULL)
3411 hentry->sessLock = true;
3412 else
3413 hentry->xactLock = true;
3414 }
3415
3416 /*
3417 * We can throw error immediately when we see both types of locks; no
3418 * need to wait around to see if there are more violations.
3419 */
3420 if (hentry->sessLock && hentry->xactLock)
3421 ereport(ERROR,
3422 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3423 errmsg("cannot PREPARE while holding both session-level and transaction-level locks on the same object")));
3424 }
3425
3426 /* Success, so clean up */
3427 hash_destroy(lockhtab);
3428}
3429
3430/*
3431 * AtPrepare_Locks
3432 * Do the preparatory work for a PREPARE: make 2PC state file records
3433 * for all locks currently held.
3434 *
3435 * Session-level locks are ignored, as are VXID locks.
3436 *
3437 * For the most part, we don't need to touch shared memory for this ---
3438 * all the necessary state information is in the locallock table.
3439 * Fast-path locks are an exception, however: we move any such locks to
3440 * the main table before allowing PREPARE TRANSACTION to succeed.
3441 */
3442void
3444{
3445 HASH_SEQ_STATUS status;
3446 LOCALLOCK *locallock;
3447
3448 /* First, verify there aren't locks of both xact and session level */
3450
3451 /* Now do the per-locallock cleanup work */
3453
3454 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
3455 {
3456 TwoPhaseLockRecord record;
3457 LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
3458 bool haveSessionLock;
3459 bool haveXactLock;
3460 int i;
3461
3462 /*
3463 * Ignore VXID locks. We don't want those to be held by prepared
3464 * transactions, since they aren't meaningful after a restart.
3465 */
3467 continue;
3468
3469 /* Ignore it if we don't actually hold the lock */
3470 if (locallock->nLocks <= 0)
3471 continue;
3472
3473 /* Scan to see whether we hold it at session or transaction level */
3474 haveSessionLock = haveXactLock = false;
3475 for (i = locallock->numLockOwners - 1; i >= 0; i--)
3476 {
3477 if (lockOwners[i].owner == NULL)
3478 haveSessionLock = true;
3479 else
3480 haveXactLock = true;
3481 }
3482
3483 /* Ignore it if we have only session lock */
3484 if (!haveXactLock)
3485 continue;
3486
3487 /* This can't happen, because we already checked it */
3488 if (haveSessionLock)
3489 ereport(ERROR,
3490 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3491 errmsg("cannot PREPARE while holding both session-level and transaction-level locks on the same object")));
3492
3493 /*
3494 * If the local lock was taken via the fast-path, we need to move it
3495 * to the primary lock table, or just get a pointer to the existing
3496 * primary lock table entry if by chance it's already been
3497 * transferred.
3498 */
3499 if (locallock->proclock == NULL)
3500 {
3501 locallock->proclock = FastPathGetRelationLockEntry(locallock);
3502 locallock->lock = locallock->proclock->tag.myLock;
3503 }
3504
3505 /*
3506 * Arrange to not release any strong lock count held by this lock
3507 * entry. We must retain the count until the prepared transaction is
3508 * committed or rolled back.
3509 */
3510 locallock->holdsStrongLockCount = false;
3511
3512 /*
3513 * Create a 2PC record.
3514 */
3515 memcpy(&(record.locktag), &(locallock->tag.lock), sizeof(LOCKTAG));
3516 record.lockmode = locallock->tag.mode;
3517
3519 &record, sizeof(TwoPhaseLockRecord));
3520 }
3521}
3522
3523/*
3524 * PostPrepare_Locks
3525 * Clean up after successful PREPARE
3526 *
3527 * Here, we want to transfer ownership of our locks to a dummy PGPROC
3528 * that's now associated with the prepared transaction, and we want to
3529 * clean out the corresponding entries in the LOCALLOCK table.
3530 *
3531 * Note: by removing the LOCALLOCK entries, we are leaving dangling
3532 * pointers in the transaction's resource owner. This is OK at the
3533 * moment since resowner.c doesn't try to free locks retail at a toplevel
3534 * transaction commit or abort. We could alternatively zero out nLocks
3535 * and leave the LOCALLOCK entries to be garbage-collected by LockReleaseAll,
3536 * but that probably costs more cycles.
3537 */
3538void
3540{
3541 PGPROC *newproc = TwoPhaseGetDummyProc(xid, false);
3542 HASH_SEQ_STATUS status;
3543 LOCALLOCK *locallock;
3544 LOCK *lock;
3545 PROCLOCK *proclock;
3546 PROCLOCKTAG proclocktag;
3547 int partition;
3548
3549 /* Can't prepare a lock group follower. */
3550 Assert(MyProc->lockGroupLeader == NULL ||
3552
3553 /* This is a critical section: any error means big trouble */
3555
3556 /*
3557 * First we run through the locallock table and get rid of unwanted
3558 * entries, then we scan the process's proclocks and transfer them to the
3559 * target proc.
3560 *
3561 * We do this separately because we may have multiple locallock entries
3562 * pointing to the same proclock, and we daren't end up with any dangling
3563 * pointers.
3564 */
3566
3567 while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
3568 {
3569 LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
3570 bool haveSessionLock;
3571 bool haveXactLock;
3572 int i;
3573
3574 if (locallock->proclock == NULL || locallock->lock == NULL)
3575 {
3576 /*
3577 * We must've run out of shared memory while trying to set up this
3578 * lock. Just forget the local entry.
3579 */
3580 Assert(locallock->nLocks == 0);
3581 RemoveLocalLock(locallock);
3582 continue;
3583 }
3584
3585 /* Ignore VXID locks */
3587 continue;
3588
3589 /* Scan to see whether we hold it at session or transaction level */
3590 haveSessionLock = haveXactLock = false;
3591 for (i = locallock->numLockOwners - 1; i >= 0; i--)
3592 {
3593 if (lockOwners[i].owner == NULL)
3594 haveSessionLock = true;
3595 else
3596 haveXactLock = true;
3597 }
3598
3599 /* Ignore it if we have only session lock */
3600 if (!haveXactLock)
3601 continue;
3602
3603 /* This can't happen, because we already checked it */
3604 if (haveSessionLock)
3605 ereport(PANIC,
3606 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3607 errmsg("cannot PREPARE while holding both session-level and transaction-level locks on the same object")));
3608
3609 /* Mark the proclock to show we need to release this lockmode */
3610 if (locallock->nLocks > 0)
3611 locallock->proclock->releaseMask |= LOCKBIT_ON(locallock->tag.mode);
3612
3613 /* And remove the locallock hashtable entry */
3614 RemoveLocalLock(locallock);
3615 }
3616
3617 /*
3618 * Now, scan each lock partition separately.
3619 */
3620 for (partition = 0; partition < NUM_LOCK_PARTITIONS; partition++)
3621 {
3622 LWLock *partitionLock;
3623 dlist_head *procLocks = &(MyProc->myProcLocks[partition]);
3624 dlist_mutable_iter proclock_iter;
3625
3626 partitionLock = LockHashPartitionLockByIndex(partition);
3627
3628 /*
3629 * If the proclock list for this partition is empty, we can skip
3630 * acquiring the partition lock. This optimization is safer than the
3631 * situation in LockReleaseAll, because we got rid of any fast-path
3632 * locks during AtPrepare_Locks, so there cannot be any case where
3633 * another backend is adding something to our lists now. For safety,
3634 * though, we code this the same way as in LockReleaseAll.
3635 */
3636 if (dlist_is_empty(procLocks))
3637 continue; /* needn't examine this partition */
3638
3639 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
3640
3641 dlist_foreach_modify(proclock_iter, procLocks)
3642 {
3643 proclock = dlist_container(PROCLOCK, procLink, proclock_iter.cur);
3644
3645 Assert(proclock->tag.myProc == MyProc);
3646
3647 lock = proclock->tag.myLock;
3648
3649 /* Ignore VXID locks */
3651 continue;
3652
3653 PROCLOCK_PRINT("PostPrepare_Locks", proclock);
3654 LOCK_PRINT("PostPrepare_Locks", lock, 0);
3655 Assert(lock->nRequested >= 0);
3656 Assert(lock->nGranted >= 0);
3657 Assert(lock->nGranted <= lock->nRequested);
3658 Assert((proclock->holdMask & ~lock->grantMask) == 0);
3659
3660 /* Ignore it if nothing to release (must be a session lock) */
3661 if (proclock->releaseMask == 0)
3662 continue;
3663
3664 /* Else we should be releasing all locks */
3665 if (proclock->releaseMask != proclock->holdMask)
3666 elog(PANIC, "we seem to have dropped a bit somewhere");
3667
3668 /*
3669 * We cannot simply modify proclock->tag.myProc to reassign
3670 * ownership of the lock, because that's part of the hash key and
3671 * the proclock would then be in the wrong hash chain. Instead
3672 * use hash_update_hash_key. (We used to create a new hash entry,
3673 * but that risks out-of-memory failure if other processes are
3674 * busy making proclocks too.) We must unlink the proclock from
3675 * our procLink chain and put it into the new proc's chain, too.
3676 *
3677 * Note: the updated proclock hash key will still belong to the
3678 * same hash partition, cf proclock_hash(). So the partition lock
3679 * we already hold is sufficient for this.
3680 */
3681 dlist_delete(&proclock->procLink);
3682
3683 /*
3684 * Create the new hash key for the proclock.
3685 */
3686 proclocktag.myLock = lock;
3687 proclocktag.myProc = newproc;
3688
3689 /*
3690 * Update groupLeader pointer to point to the new proc. (We'd
3691 * better not be a member of somebody else's lock group!)
3692 */
3693 Assert(proclock->groupLeader == proclock->tag.myProc);
3694 proclock->groupLeader = newproc;
3695
3696 /*
3697 * Update the proclock. We should not find any existing entry for
3698 * the same hash key, since there can be only one entry for any
3699 * given lock with my own proc.
3700 */
3702 proclock,
3703 &proclocktag))
3704 elog(PANIC, "duplicate entry found while reassigning a prepared transaction's locks");
3705
3706 /* Re-link into the new proc's proclock list */
3707 dlist_push_tail(&newproc->myProcLocks[partition], &proclock->procLink);
3708
3709 PROCLOCK_PRINT("PostPrepare_Locks: updated", proclock);
3710 } /* loop over PROCLOCKs within this partition */
3711
3712 LWLockRelease(partitionLock);
3713 } /* loop over partitions */
3714
3716}
3717
3718
3719/*
3720 * Estimate shared-memory space used for lock tables
3721 */
3722Size
3724{
3725 Size size = 0;
3726 long max_table_size;
3727
3728 /* lock hash table */
3729 max_table_size = NLOCKENTS();
3730 size = add_size(size, hash_estimate_size(max_table_size, sizeof(LOCK)));
3731
3732 /* proclock hash table */
3733 max_table_size *= 2;
3734 size = add_size(size, hash_estimate_size(max_table_size, sizeof(PROCLOCK)));
3735
3736 /*
3737 * Since NLOCKENTS is only an estimate, add 10% safety margin.
3738 */
3739 size = add_size(size, size / 10);
3740
3741 return size;
3742}
3743
3744/*
3745 * GetLockStatusData - Return a summary of the lock manager's internal
3746 * status, for use in a user-level reporting function.
3747 *
3748 * The return data consists of an array of LockInstanceData objects,
3749 * which are a lightly abstracted version of the PROCLOCK data structures,
3750 * i.e. there is one entry for each unique lock and interested PGPROC.
3751 * It is the caller's responsibility to match up related items (such as
3752 * references to the same lockable object or PGPROC) if wanted.
3753 *
3754 * The design goal is to hold the LWLocks for as short a time as possible;
3755 * thus, this function simply makes a copy of the necessary data and releases
3756 * the locks, allowing the caller to contemplate and format the data for as
3757 * long as it pleases.
3758 */
3759LockData *
3761{
3762 LockData *data;
3763 PROCLOCK *proclock;
3764 HASH_SEQ_STATUS seqstat;
3765 int els;
3766 int el;
3767 int i;
3768
3769 data = (LockData *) palloc(sizeof(LockData));
3770
3771 /* Guess how much space we'll need. */
3772 els = MaxBackends;
3773 el = 0;
3774 data->locks = (LockInstanceData *) palloc(sizeof(LockInstanceData) * els);
3775
3776 /*
3777 * First, we iterate through the per-backend fast-path arrays, locking
3778 * them one at a time. This might produce an inconsistent picture of the
3779 * system state, but taking all of those LWLocks at the same time seems
3780 * impractical (in particular, note MAX_SIMUL_LWLOCKS). It shouldn't
3781 * matter too much, because none of these locks can be involved in lock
3782 * conflicts anyway - anything that might must be present in the main lock
3783 * table. (For the same reason, we don't sweat about making leaderPid
3784 * completely valid. We cannot safely dereference another backend's
3785 * lockGroupLeader field without holding all lock partition locks, and
3786 * it's not worth that.)
3787 */
3788 for (i = 0; i < ProcGlobal->allProcCount; ++i)
3789 {
3790 PGPROC *proc = &ProcGlobal->allProcs[i];
3791
3792 /* Skip backends with pid=0, as they don't hold fast-path locks */
3793 if (proc->pid == 0)
3794 continue;
3795
3797
3798 for (uint32 g = 0; g < FastPathLockGroupsPerBackend; g++)
3799 {
3800 /* Skip groups without registered fast-path locks */
3801 if (proc->fpLockBits[g] == 0)
3802 continue;
3803
3804 for (int j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
3805 {
3806 LockInstanceData *instance;
3807 uint32 f = FAST_PATH_SLOT(g, j);
3808 uint32 lockbits = FAST_PATH_GET_BITS(proc, f);
3809
3810 /* Skip unallocated slots */
3811 if (!lockbits)
3812 continue;
3813
3814 if (el >= els)
3815 {
3816 els += MaxBackends;
3817 data->locks = (LockInstanceData *)
3818 repalloc(data->locks, sizeof(LockInstanceData) * els);
3819 }
3820
3821 instance = &data->locks[el];
3822 SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId,
3823 proc->fpRelId[f]);
3824 instance->holdMask = lockbits << FAST_PATH_LOCKNUMBER_OFFSET;
3825 instance->waitLockMode = NoLock;
3826 instance->vxid.procNumber = proc->vxid.procNumber;
3827 instance->vxid.localTransactionId = proc->vxid.lxid;
3828 instance->pid = proc->pid;
3829 instance->leaderPid = proc->pid;
3830 instance->fastpath = true;
3831
3832 /*
3833 * Successfully taking fast path lock means there were no
3834 * conflicting locks.
3835 */
3836 instance->waitStart = 0;
3837
3838 el++;
3839 }
3840 }
3841
3842 if (proc->fpVXIDLock)
3843 {
3845 LockInstanceData *instance;
3846
3847 if (el >= els)
3848 {
3849 els += MaxBackends;
3850 data->locks = (LockInstanceData *)
3851 repalloc(data->locks, sizeof(LockInstanceData) * els);
3852 }
3853
3854 vxid.procNumber = proc->vxid.procNumber;
3856
3857 instance = &data->locks[el];
3858 SET_LOCKTAG_VIRTUALTRANSACTION(instance->locktag, vxid);
3859 instance->holdMask = LOCKBIT_ON(ExclusiveLock);
3860 instance->waitLockMode = NoLock;
3861 instance->vxid.procNumber = proc->vxid.procNumber;
3862 instance->vxid.localTransactionId = proc->vxid.lxid;
3863 instance->pid = proc->pid;
3864 instance->leaderPid = proc->pid;
3865 instance->fastpath = true;
3866 instance->waitStart = 0;
3867
3868 el++;
3869 }
3870
3871 LWLockRelease(&proc->fpInfoLock);
3872 }
3873
3874 /*
3875 * Next, acquire lock on the entire shared lock data structure. We do
3876 * this so that, at least for locks in the primary lock table, the state
3877 * will be self-consistent.
3878 *
3879 * Since this is a read-only operation, we take shared instead of
3880 * exclusive lock. There's not a whole lot of point to this, because all
3881 * the normal operations require exclusive lock, but it doesn't hurt
3882 * anything either. It will at least allow two backends to do
3883 * GetLockStatusData in parallel.
3884 *
3885 * Must grab LWLocks in partition-number order to avoid LWLock deadlock.
3886 */
3887 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
3889
3890 /* Now we can safely count the number of proclocks */
3892 if (data->nelements > els)
3893 {
3894 els = data->nelements;
3895 data->locks = (LockInstanceData *)
3896 repalloc(data->locks, sizeof(LockInstanceData) * els);
3897 }
3898
3899 /* Now scan the tables to copy the data */
3901
3902 while ((proclock = (PROCLOCK *) hash_seq_search(&seqstat)))
3903 {
3904 PGPROC *proc = proclock->tag.myProc;
3905 LOCK *lock = proclock->tag.myLock;
3906 LockInstanceData *instance = &data->locks[el];
3907
3908 memcpy(&instance->locktag, &lock->tag, sizeof(LOCKTAG));
3909 instance->holdMask = proclock->holdMask;
3910 if (proc->waitLock == proclock->tag.myLock)
3911 instance->waitLockMode = proc->waitLockMode;
3912 else
3913 instance->waitLockMode = NoLock;
3914 instance->vxid.procNumber = proc->vxid.procNumber;
3915 instance->vxid.localTransactionId = proc->vxid.lxid;
3916 instance->pid = proc->pid;
3917 instance->leaderPid = proclock->groupLeader->pid;
3918 instance->fastpath = false;
3919 instance->waitStart = (TimestampTz) pg_atomic_read_u64(&proc->waitStart);
3920
3921 el++;
3922 }
3923
3924 /*
3925 * And release locks. We do this in reverse order for two reasons: (1)
3926 * Anyone else who needs more than one of the locks will be trying to lock
3927 * them in increasing order; we don't want to release the other process
3928 * until it can get all the locks it needs. (2) This avoids O(N^2)
3929 * behavior inside LWLockRelease.
3930 */
3931 for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
3933
3934 Assert(el == data->nelements);
3935
3936 return data;
3937}
3938
3939/*
3940 * GetBlockerStatusData - Return a summary of the lock manager's state
3941 * concerning locks that are blocking the specified PID or any member of
3942 * the PID's lock group, for use in a user-level reporting function.
3943 *
3944 * For each PID within the lock group that is awaiting some heavyweight lock,
3945 * the return data includes an array of LockInstanceData objects, which are
3946 * the same data structure used by GetLockStatusData; but unlike that function,
3947 * this one reports only the PROCLOCKs associated with the lock that that PID
3948 * is blocked on. (Hence, all the locktags should be the same for any one
3949 * blocked PID.) In addition, we return an array of the PIDs of those backends
3950 * that are ahead of the blocked PID in the lock's wait queue. These can be
3951 * compared with the PIDs in the LockInstanceData objects to determine which
3952 * waiters are ahead of or behind the blocked PID in the queue.
3953 *
3954 * If blocked_pid isn't a valid backend PID or nothing in its lock group is
3955 * waiting on any heavyweight lock, return empty arrays.
3956 *
3957 * The design goal is to hold the LWLocks for as short a time as possible;
3958 * thus, this function simply makes a copy of the necessary data and releases
3959 * the locks, allowing the caller to contemplate and format the data for as
3960 * long as it pleases.
3961 */
3963GetBlockerStatusData(int blocked_pid)
3964{
3966 PGPROC *proc;
3967 int i;
3968
3970
3971 /*
3972 * Guess how much space we'll need, and preallocate. Most of the time
3973 * this will avoid needing to do repalloc while holding the LWLocks. (We
3974 * assume, but check with an Assert, that MaxBackends is enough entries
3975 * for the procs[] array; the other two could need enlargement, though.)
3976 */
3977 data->nprocs = data->nlocks = data->npids = 0;
3978 data->maxprocs = data->maxlocks = data->maxpids = MaxBackends;
3979 data->procs = (BlockedProcData *) palloc(sizeof(BlockedProcData) * data->maxprocs);
3980 data->locks = (LockInstanceData *) palloc(sizeof(LockInstanceData) * data->maxlocks);
3981 data->waiter_pids = (int *) palloc(sizeof(int) * data->maxpids);
3982
3983 /*
3984 * In order to search the ProcArray for blocked_pid and assume that that
3985 * entry won't immediately disappear under us, we must hold ProcArrayLock.
3986 * In addition, to examine the lock grouping fields of any other backend,
3987 * we must hold all the hash partition locks. (Only one of those locks is
3988 * actually relevant for any one lock group, but we can't know which one
3989 * ahead of time.) It's fairly annoying to hold all those locks
3990 * throughout this, but it's no worse than GetLockStatusData(), and it
3991 * does have the advantage that we're guaranteed to return a
3992 * self-consistent instantaneous state.
3993 */
3994 LWLockAcquire(ProcArrayLock, LW_SHARED);
3995
3996 proc = BackendPidGetProcWithLock(blocked_pid);
3997
3998 /* Nothing to do if it's gone */
3999 if (proc != NULL)
4000 {
4001 /*
4002 * Acquire lock on the entire shared lock data structure. See notes
4003 * in GetLockStatusData().
4004 */
4005 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
4007
4008 if (proc->lockGroupLeader == NULL)
4009 {
4010 /* Easy case, proc is not a lock group member */
4012 }
4013 else
4014 {
4015 /* Examine all procs in proc's lock group */
4016 dlist_iter iter;
4017
4019 {
4020 PGPROC *memberProc;
4021
4022 memberProc = dlist_container(PGPROC, lockGroupLink, iter.cur);
4024 }
4025 }
4026
4027 /*
4028 * And release locks. See notes in GetLockStatusData().
4029 */
4030 for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
4032
4033 Assert(data->nprocs <= data->maxprocs);
4034 }
4035
4036 LWLockRelease(ProcArrayLock);
4037
4038 return data;
4039}
4040
4041/* Accumulate data about one possibly-blocked proc for GetBlockerStatusData */
4042static void
4044{
4045 LOCK *theLock = blocked_proc->waitLock;
4046 BlockedProcData *bproc;
4047 dlist_iter proclock_iter;
4048 dlist_iter proc_iter;
4049 dclist_head *waitQueue;
4050 int queue_size;
4051
4052 /* Nothing to do if this proc is not blocked */
4053 if (theLock == NULL)
4054 return;
4055
4056 /* Set up a procs[] element */
4057 bproc = &data->procs[data->nprocs++];
4058 bproc->pid = blocked_proc->pid;
4059 bproc->first_lock = data->nlocks;
4060 bproc->first_waiter = data->npids;
4061
4062 /*
4063 * We may ignore the proc's fast-path arrays, since nothing in those could
4064 * be related to a contended lock.
4065 */
4066
4067 /* Collect all PROCLOCKs associated with theLock */
4068 dlist_foreach(proclock_iter, &theLock->procLocks)
4069 {
4070 PROCLOCK *proclock =
4071 dlist_container(PROCLOCK, lockLink, proclock_iter.cur);
4072 PGPROC *proc = proclock->tag.myProc;
4073 LOCK *lock = proclock->tag.myLock;
4074 LockInstanceData *instance;
4075
4076 if (data->nlocks >= data->maxlocks)
4077 {
4078 data->maxlocks += MaxBackends;
4079 data->locks = (LockInstanceData *)
4080 repalloc(data->locks, sizeof(LockInstanceData) * data->maxlocks);
4081 }
4082
4083 instance = &data->locks[data->nlocks];
4084 memcpy(&instance->locktag, &lock->tag, sizeof(LOCKTAG));
4085 instance->holdMask = proclock->holdMask;
4086 if (proc->waitLock == lock)
4087 instance->waitLockMode = proc->waitLockMode;
4088 else
4089 instance->waitLockMode = NoLock;
4090 instance->vxid.procNumber = proc->vxid.procNumber;
4091 instance->vxid.localTransactionId = proc->vxid.lxid;
4092 instance->pid = proc->pid;
4093 instance->leaderPid = proclock->groupLeader->pid;
4094 instance->fastpath = false;
4095 data->nlocks++;
4096 }
4097
4098 /* Enlarge waiter_pids[] if it's too small to hold all wait queue PIDs */
4099 waitQueue = &(theLock->waitProcs);
4100 queue_size = dclist_count(waitQueue);
4101
4102 if (queue_size > data->maxpids - data->npids)
4103 {
4104 data->maxpids = Max(data->maxpids + MaxBackends,
4105 data->npids + queue_size);
4106 data->waiter_pids = (int *) repalloc(data->waiter_pids,
4107 sizeof(int) * data->maxpids);
4108 }
4109
4110 /* Collect PIDs from the lock's wait queue, stopping at blocked_proc */
4111 dclist_foreach(proc_iter, waitQueue)
4112 {
4113 PGPROC *queued_proc = dlist_container(PGPROC, links, proc_iter.cur);
4114
4115 if (queued_proc == blocked_proc)
4116 break;
4117 data->waiter_pids[data->npids++] = queued_proc->pid;
4118 queued_proc = (PGPROC *) queued_proc->links.next;
4119 }
4120
4121 bproc->num_locks = data->nlocks - bproc->first_lock;
4122 bproc->num_waiters = data->npids - bproc->first_waiter;
4123}
4124
4125/*
4126 * Returns a list of currently held AccessExclusiveLocks, for use by
4127 * LogStandbySnapshot(). The result is a palloc'd array,
4128 * with the number of elements returned into *nlocks.
4129 *
4130 * XXX This currently takes a lock on all partitions of the lock table,
4131 * but it's possible to do better. By reference counting locks and storing
4132 * the value in the ProcArray entry for each backend we could tell if any
4133 * locks need recording without having to acquire the partition locks and
4134 * scan the lock table. Whether that's worth the additional overhead
4135 * is pretty dubious though.
4136 */
4139{
4140 xl_standby_lock *accessExclusiveLocks;
4141 PROCLOCK *proclock;
4142 HASH_SEQ_STATUS seqstat;
4143 int i;
4144 int index;
4145 int els;
4146
4147 /*
4148 * Acquire lock on the entire shared lock data structure.
4149 *
4150 * Must grab LWLocks in partition-number order to avoid LWLock deadlock.
4151 */
4152 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
4154
4155 /* Now we can safely count the number of proclocks */
4157
4158 /*
4159 * Allocating enough space for all locks in the lock table is overkill,
4160 * but it's more convenient and faster than having to enlarge the array.
4161 */
4162 accessExclusiveLocks = palloc(els * sizeof(xl_standby_lock));
4163
4164 /* Now scan the tables to copy the data */
4166
4167 /*
4168 * If lock is a currently granted AccessExclusiveLock then it will have
4169 * just one proclock holder, so locks are never accessed twice in this
4170 * particular case. Don't copy this code for use elsewhere because in the
4171 * general case this will give you duplicate locks when looking at
4172 * non-exclusive lock types.
4173 */
4174 index = 0;
4175 while ((proclock = (PROCLOCK *) hash_seq_search(&seqstat)))
4176 {
4177 /* make sure this definition matches the one used in LockAcquire */
4178 if ((proclock->holdMask & LOCKBIT_ON(AccessExclusiveLock)) &&
4180 {
4181 PGPROC *proc = proclock->tag.myProc;
4182 LOCK *lock = proclock->tag.myLock;
4183 TransactionId xid = proc->xid;
4184
4185 /*
4186 * Don't record locks for transactions if we know they have
4187 * already issued their WAL record for commit but not yet released
4188 * lock. It is still possible that we see locks held by already
4189 * complete transactions, if they haven't yet zeroed their xids.
4190 */
4191 if (!TransactionIdIsValid(xid))
4192 continue;
4193
4194 accessExclusiveLocks[index].xid = xid;
4195 accessExclusiveLocks[index].dbOid = lock->tag.locktag_field1;
4196 accessExclusiveLocks[index].relOid = lock->tag.locktag_field2;
4197
4198 index++;
4199 }
4200 }
4201
4202 Assert(index <= els);
4203
4204 /*
4205 * And release locks. We do this in reverse order for two reasons: (1)
4206 * Anyone else who needs more than one of the locks will be trying to lock
4207 * them in increasing order; we don't want to release the other process
4208 * until it can get all the locks it needs. (2) This avoids O(N^2)
4209 * behavior inside LWLockRelease.
4210 */
4211 for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
4213
4214 *nlocks = index;
4215 return accessExclusiveLocks;
4216}
4217
4218/* Provide the textual name of any lock mode */
4219const char *
4221{
4222 Assert(lockmethodid > 0 && lockmethodid < lengthof(LockMethods));
4223 Assert(mode > 0 && mode <= LockMethods[lockmethodid]->numLockModes);
4224 return LockMethods[lockmethodid]->lockModeNames[mode];
4225}
4226
4227#ifdef LOCK_DEBUG
4228/*
4229 * Dump all locks in the given proc's myProcLocks lists.
4230 *
4231 * Caller is responsible for having acquired appropriate LWLocks.
4232 */
4233void
4234DumpLocks(PGPROC *proc)
4235{
4236 int i;
4237
4238 if (proc == NULL)
4239 return;
4240
4241 if (proc->waitLock)
4242 LOCK_PRINT("DumpLocks: waiting on", proc->waitLock, 0);
4243
4244 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
4245 {
4246 dlist_head *procLocks = &proc->myProcLocks[i];
4247 dlist_iter iter;
4248
4249 dlist_foreach(iter, procLocks)
4250 {
4251 PROCLOCK *proclock = dlist_container(PROCLOCK, procLink, iter.cur);
4252 LOCK *lock = proclock->tag.myLock;
4253
4254 Assert(proclock->tag.myProc == proc);
4255 PROCLOCK_PRINT("DumpLocks", proclock);
4256 LOCK_PRINT("DumpLocks", lock, 0);
4257 }
4258 }
4259}
4260
4261/*
4262 * Dump all lmgr locks.
4263 *
4264 * Caller is responsible for having acquired appropriate LWLocks.
4265 */
4266void
4267DumpAllLocks(void)
4268{
4269 PGPROC *proc;
4270 PROCLOCK *proclock;
4271 LOCK *lock;
4272 HASH_SEQ_STATUS status;
4273
4274 proc = MyProc;
4275
4276 if (proc && proc->waitLock)
4277 LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0);
4278
4280
4281 while ((proclock = (PROCLOCK *) hash_seq_search(&status)) != NULL)
4282 {
4283 PROCLOCK_PRINT("DumpAllLocks", proclock);
4284
4285 lock = proclock->tag.myLock;
4286 if (lock)
4287 LOCK_PRINT("DumpAllLocks", lock, 0);
4288 else
4289 elog(LOG, "DumpAllLocks: proclock->tag.myLock = NULL");
4290 }
4291}
4292#endif /* LOCK_DEBUG */
4293
4294/*
4295 * LOCK 2PC resource manager's routines
4296 */
4297
4298/*
4299 * Re-acquire a lock belonging to a transaction that was prepared.
4300 *
4301 * Because this function is run at db startup, re-acquiring the locks should
4302 * never conflict with running transactions because there are none. We
4303 * assume that the lock state represented by the stored 2PC files is legal.
4304 *
4305 * When switching from Hot Standby mode to normal operation, the locks will
4306 * be already held by the startup process. The locks are acquired for the new
4307 * procs without checking for conflicts, so we don't get a conflict between the
4308 * startup process and the dummy procs, even though we will momentarily have
4309 * a situation where two procs are holding the same AccessExclusiveLock,
4310 * which isn't normally possible because the conflict. If we're in standby
4311 * mode, but a recovery snapshot hasn't been established yet, it's possible
4312 * that some but not all of the locks are already held by the startup process.
4313 *
4314 * This approach is simple, but also a bit dangerous, because if there isn't
4315 * enough shared memory to acquire the locks, an error will be thrown, which
4316 * is promoted to FATAL and recovery will abort, bringing down postmaster.
4317 * A safer approach would be to transfer the locks like we do in
4318 * AtPrepare_Locks, but then again, in hot standby mode it's possible for
4319 * read-only backends to use up all the shared lock memory anyway, so that
4320 * replaying the WAL record that needs to acquire a lock will throw an error
4321 * and PANIC anyway.
4322 */
4323void
4325 void *recdata, uint32 len)
4326{
4327 TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
4328 PGPROC *proc = TwoPhaseGetDummyProc(xid, false);
4329 LOCKTAG *locktag;
4330 LOCKMODE lockmode;
4331 LOCKMETHODID lockmethodid;
4332 LOCK *lock;
4333 PROCLOCK *proclock;
4334 PROCLOCKTAG proclocktag;
4335 bool found;
4336 uint32 hashcode;
4337 uint32 proclock_hashcode;
4338 int partition;
4339 LWLock *partitionLock;
4340 LockMethod lockMethodTable;
4341
4342 Assert(len == sizeof(TwoPhaseLockRecord));
4343 locktag = &rec->locktag;
4344 lockmode = rec->lockmode;
4345 lockmethodid = locktag->locktag_lockmethodid;
4346
4347 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
4348 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
4349 lockMethodTable = LockMethods[lockmethodid];
4350
4351 hashcode = LockTagHashCode(locktag);
4352 partition = LockHashPartition(hashcode);
4353 partitionLock = LockHashPartitionLock(hashcode);
4354
4355 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
4356
4357 /*
4358 * Find or create a lock with this tag.
4359 */
4361 locktag,
4362 hashcode,
4364 &found);
4365 if (!lock)
4366 {
4367 LWLockRelease(partitionLock);
4368 ereport(ERROR,
4369 (errcode(ERRCODE_OUT_OF_MEMORY),
4370 errmsg("out of shared memory"),
4371 errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
4372 }
4373
4374 /*
4375 * if it's a new lock object, initialize it
4376 */
4377 if (!found)
4378 {
4379 lock->grantMask = 0;
4380 lock->waitMask = 0;
4381 dlist_init(&lock->procLocks);
4382 dclist_init(&lock->waitProcs);
4383 lock->nRequested = 0;
4384 lock->nGranted = 0;
4385 MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
4386 MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
4387 LOCK_PRINT("lock_twophase_recover: new", lock, lockmode);
4388 }
4389 else
4390 {
4391 LOCK_PRINT("lock_twophase_recover: found", lock, lockmode);
4392 Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
4393 Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
4394 Assert(lock->nGranted <= lock->nRequested);
4395 }
4396
4397 /*
4398 * Create the hash key for the proclock table.
4399 */
4400 proclocktag.myLock = lock;
4401 proclocktag.myProc = proc;
4402
4403 proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode);
4404
4405 /*
4406 * Find or create a proclock entry with this tag
4407 */
4409 &proclocktag,
4410 proclock_hashcode,
4412 &found);
4413 if (!proclock)
4414 {
4415 /* Oops, not enough shmem for the proclock */
4416 if (lock->nRequested == 0)
4417 {
4418 /*
4419 * There are no other requestors of this lock, so garbage-collect
4420 * the lock object. We *must* do this to avoid a permanent leak
4421 * of shared memory, because there won't be anything to cause
4422 * anyone to release the lock object later.
4423 */
4426 &(lock->tag),
4427 hashcode,
4429 NULL))
4430 elog(PANIC, "lock table corrupted");
4431 }
4432 LWLockRelease(partitionLock);
4433 ereport(ERROR,
4434 (errcode(ERRCODE_OUT_OF_MEMORY),
4435 errmsg("out of shared memory"),
4436 errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
4437 }
4438
4439 /*
4440 * If new, initialize the new entry
4441 */
4442 if (!found)
4443 {
4444 Assert(proc->lockGroupLeader == NULL);
4445 proclock->groupLeader = proc;
4446 proclock->holdMask = 0;
4447 proclock->releaseMask = 0;
4448 /* Add proclock to appropriate lists */
4449 dlist_push_tail(&lock->procLocks, &proclock->lockLink);
4450 dlist_push_tail(&proc->myProcLocks[partition],
4451 &proclock->procLink);
4452 PROCLOCK_PRINT("lock_twophase_recover: new", proclock);
4453 }
4454 else
4455 {
4456 PROCLOCK_PRINT("lock_twophase_recover: found", proclock);
4457 Assert((proclock->holdMask & ~lock->grantMask) == 0);
4458 }
4459
4460 /*
4461 * lock->nRequested and lock->requested[] count the total number of
4462 * requests, whether granted or waiting, so increment those immediately.
4463 */
4464 lock->nRequested++;
4465 lock->requested[lockmode]++;
4466 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
4467
4468 /*
4469 * We shouldn't already hold the desired lock.
4470 */
4471 if (proclock->holdMask & LOCKBIT_ON(lockmode))
4472 elog(ERROR, "lock %s on object %u/%u/%u is already held",
4473 lockMethodTable->lockModeNames[lockmode],
4474 lock->tag.locktag_field1, lock->tag.locktag_field2,
4475 lock->tag.locktag_field3);
4476
4477 /*
4478 * We ignore any possible conflicts and just grant ourselves the lock. Not
4479 * only because we don't bother, but also to avoid deadlocks when
4480 * switching from standby to normal mode. See function comment.
4481 */
4482 GrantLock(lock, proclock, lockmode);
4483
4484 /*
4485 * Bump strong lock count, to make sure any fast-path lock requests won't
4486 * be granted without consulting the primary lock table.
4487 */
4488 if (ConflictsWithRelationFastPath(&lock->tag, lockmode))
4489 {
4490 uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode);
4491
4493 FastPathStrongRelationLocks->count[fasthashcode]++;
4495 }
4496
4497 LWLockRelease(partitionLock);
4498}
4499
4500/*
4501 * Re-acquire a lock belonging to a transaction that was prepared, when
4502 * starting up into hot standby mode.
4503 */
4504void
4506 void *recdata, uint32 len)
4507{
4508 TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
4509 LOCKTAG *locktag;
4510 LOCKMODE lockmode;
4511 LOCKMETHODID lockmethodid;
4512
4513 Assert(len == sizeof(TwoPhaseLockRecord));
4514 locktag = &rec->locktag;
4515 lockmode = rec->lockmode;
4516 lockmethodid = locktag->locktag_lockmethodid;
4517
4518 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
4519 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
4520
4521 if (lockmode == AccessExclusiveLock &&
4522 locktag->locktag_type == LOCKTAG_RELATION)
4523 {
4525 locktag->locktag_field1 /* dboid */ ,
4526 locktag->locktag_field2 /* reloid */ );
4527 }
4528}
4529
4530
4531/*
4532 * 2PC processing routine for COMMIT PREPARED case.
4533 *
4534 * Find and release the lock indicated by the 2PC record.
4535 */
4536void
4538 void *recdata, uint32 len)
4539{
4540 TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
4541 PGPROC *proc = TwoPhaseGetDummyProc(xid, true);
4542 LOCKTAG *locktag;
4543 LOCKMETHODID lockmethodid;
4544 LockMethod lockMethodTable;
4545
4546 Assert(len == sizeof(TwoPhaseLockRecord));
4547 locktag = &rec->locktag;
4548 lockmethodid = locktag->locktag_lockmethodid;
4549
4550 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
4551 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
4552 lockMethodTable = LockMethods[lockmethodid];
4553
4554 LockRefindAndRelease(lockMethodTable, proc, locktag, rec->lockmode, true);
4555}
4556
4557/*
4558 * 2PC processing routine for ROLLBACK PREPARED case.
4559 *
4560 * This is actually just the same as the COMMIT case.
4561 */
4562void
4564 void *recdata, uint32 len)
4565{
4566 lock_twophase_postcommit(xid, info, recdata, len);
4567}
4568
4569/*
4570 * VirtualXactLockTableInsert
4571 *
4572 * Take vxid lock via the fast-path. There can't be any pre-existing
4573 * lockers, as we haven't advertised this vxid via the ProcArray yet.
4574 *
4575 * Since MyProc->fpLocalTransactionId will normally contain the same data
4576 * as MyProc->vxid.lxid, you might wonder if we really need both. The
4577 * difference is that MyProc->vxid.lxid is set and cleared unlocked, and
4578 * examined by procarray.c, while fpLocalTransactionId is protected by
4579 * fpInfoLock and is used only by the locking subsystem. Doing it this
4580 * way makes it easier to verify that there are no funny race conditions.
4581 *
4582 * We don't bother recording this lock in the local lock table, since it's
4583 * only ever released at the end of a transaction. Instead,
4584 * LockReleaseAll() calls VirtualXactLockTableCleanup().
4585 */
4586void
4588{
4590
4592
4595 Assert(MyProc->fpVXIDLock == false);
4596
4597 MyProc->fpVXIDLock = true;
4599
4601}
4602
4603/*
4604 * VirtualXactLockTableCleanup
4605 *
4606 * Check whether a VXID lock has been materialized; if so, release it,
4607 * unblocking waiters.
4608 */
4609void
4611{
4612 bool fastpath;
4613 LocalTransactionId lxid;
4614
4616
4617 /*
4618 * Clean up shared memory state.
4619 */
4621
4622 fastpath = MyProc->fpVXIDLock;
4624 MyProc->fpVXIDLock = false;
4626
4628
4629 /*
4630 * If fpVXIDLock has been cleared without touching fpLocalTransactionId,
4631 * that means someone transferred the lock to the main lock table.
4632 */
4633 if (!fastpath && LocalTransactionIdIsValid(lxid))
4634 {
4636 LOCKTAG locktag;
4637
4638 vxid.procNumber = MyProcNumber;
4639 vxid.localTransactionId = lxid;
4640 SET_LOCKTAG_VIRTUALTRANSACTION(locktag, vxid);
4641
4643 &locktag, ExclusiveLock, false);
4644 }
4645}
4646
4647/*
4648 * XactLockForVirtualXact
4649 *
4650 * If TransactionIdIsValid(xid), this is essentially XactLockTableWait(xid,
4651 * NULL, NULL, XLTW_None) or ConditionalXactLockTableWait(xid). Unlike those
4652 * functions, it assumes "xid" is never a subtransaction and that "xid" is
4653 * prepared, committed, or aborted.
4654 *
4655 * If !TransactionIdIsValid(xid), this locks every prepared XID having been
4656 * known as "vxid" before its PREPARE TRANSACTION.
4657 */
4658static bool
4660 TransactionId xid, bool wait)
4661{
4662 bool more = false;
4663
4664 /* There is no point to wait for 2PCs if you have no 2PCs. */
4665 if (max_prepared_xacts == 0)
4666 return true;
4667
4668 do
4669 {
4671 LOCKTAG tag;
4672
4673 /* Clear state from previous iterations. */
4674 if (more)
4675 {
4677 more = false;
4678 }
4679
4680 /* If we have no xid, try to find one. */
4681 if (!TransactionIdIsValid(xid))
4682 xid = TwoPhaseGetXidByVirtualXID(vxid, &more);
4683 if (!TransactionIdIsValid(xid))
4684 {
4685 Assert(!more);
4686 return true;
4687 }
4688
4689 /* Check or wait for XID completion. */
4690 SET_LOCKTAG_TRANSACTION(tag, xid);
4691 lar = LockAcquire(&tag, ShareLock, false, !wait);
4692 if (lar == LOCKACQUIRE_NOT_AVAIL)
4693 return false;
4694 LockRelease(&tag, ShareLock, false);
4695 } while (more);
4696
4697 return true;
4698}
4699
4700/*
4701 * VirtualXactLock
4702 *
4703 * If wait = true, wait as long as the given VXID or any XID acquired by the
4704 * same transaction is still running. Then, return true.
4705 *
4706 * If wait = false, just check whether that VXID or one of those XIDs is still
4707 * running, and return true or false.
4708 */
4709bool
4711{
4712 LOCKTAG tag;
4713 PGPROC *proc;
4715
4717
4719 /* no vxid lock; localTransactionId is a normal, locked XID */
4720 return XactLockForVirtualXact(vxid, vxid.localTransactionId, wait);
4721
4723
4724 /*
4725 * If a lock table entry must be made, this is the PGPROC on whose behalf
4726 * it must be done. Note that the transaction might end or the PGPROC
4727 * might be reassigned to a new backend before we get around to examining
4728 * it, but it doesn't matter. If we find upon examination that the
4729 * relevant lxid is no longer running here, that's enough to prove that
4730 * it's no longer running anywhere.
4731 */
4732 proc = ProcNumberGetProc(vxid.procNumber);
4733 if (proc == NULL)
4734 return XactLockForVirtualXact(vxid, InvalidTransactionId, wait);
4735
4736 /*
4737 * We must acquire this lock before checking the procNumber and lxid
4738 * against the ones we're waiting for. The target backend will only set
4739 * or clear lxid while holding this lock.
4740 */
4742
4743 if (proc->vxid.procNumber != vxid.procNumber
4745 {
4746 /* VXID ended */
4747 LWLockRelease(&proc->fpInfoLock);
4748 return XactLockForVirtualXact(vxid, InvalidTransactionId, wait);
4749 }
4750
4751 /*
4752 * If we aren't asked to wait, there's no need to set up a lock table
4753 * entry. The transaction is still in progress, so just return false.
4754 */
4755 if (!wait)
4756 {
4757 LWLockRelease(&proc->fpInfoLock);
4758 return false;
4759 }
4760
4761 /*
4762 * OK, we're going to need to sleep on the VXID. But first, we must set
4763 * up the primary lock table entry, if needed (ie, convert the proc's
4764 * fast-path lock on its VXID to a regular lock).
4765 */
4766 if (proc->fpVXIDLock)
4767 {
4768 PROCLOCK *proclock;
4769 uint32 hashcode;
4770 LWLock *partitionLock;
4771
4772 hashcode = LockTagHashCode(&tag);
4773
4774 partitionLock = LockHashPartitionLock(hashcode);
4775 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
4776
4778 &tag, hashcode, ExclusiveLock);
4779 if (!proclock)
4780 {
4781 LWLockRelease(partitionLock);
4782 LWLockRelease(&proc->fpInfoLock);
4783 ereport(ERROR,
4784 (errcode(ERRCODE_OUT_OF_MEMORY),
4785 errmsg("out of shared memory"),
4786 errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
4787 }
4788 GrantLock(proclock->tag.myLock, proclock, ExclusiveLock);
4789
4790 LWLockRelease(partitionLock);
4791
4792 proc->fpVXIDLock = false;
4793 }
4794
4795 /*
4796 * If the proc has an XID now, we'll avoid a TwoPhaseGetXidByVirtualXID()
4797 * search. The proc might have assigned this XID but not yet locked it,
4798 * in which case the proc will lock this XID before releasing the VXID.
4799 * The fpInfoLock critical section excludes VirtualXactLockTableCleanup(),
4800 * so we won't save an XID of a different VXID. It doesn't matter whether
4801 * we save this before or after setting up the primary lock table entry.
4802 */
4803 xid = proc->xid;
4804
4805 /* Done with proc->fpLockBits */
4806 LWLockRelease(&proc->fpInfoLock);
4807
4808 /* Time to wait. */
4809 (void) LockAcquire(&tag, ShareLock, false, false);
4810
4811 LockRelease(&tag, ShareLock, false);
4812 return XactLockForVirtualXact(vxid, xid, wait);
4813}
4814
4815/*
4816 * LockWaiterCount
4817 *
4818 * Find the number of lock requester on this locktag
4819 */
4820int
4822{
4823 LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
4824 LOCK *lock;
4825 bool found;
4826 uint32 hashcode;
4827 LWLock *partitionLock;
4828 int waiters = 0;
4829
4830 if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
4831 elog(ERROR, "unrecognized lock method: %d", lockmethodid);
4832
4833 hashcode = LockTagHashCode(locktag);
4834 partitionLock = LockHashPartitionLock(hashcode);
4835 LWLockAcquire(partitionLock, LW_EXCLUSIVE);
4836
4838 locktag,
4839 hashcode,
4840 HASH_FIND,
4841 &found);
4842 if (found)
4843 {
4844 Assert(lock != NULL);
4845 waiters = lock->nRequested;
4846 }
4847 LWLockRelease(partitionLock);
4848
4849 return waiters;
4850}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:467
#define Max(x, y)
Definition: c.h:969
uint16_t uint16
Definition: c.h:501
uint32_t uint32
Definition: c.h:502
#define lengthof(array)
Definition: c.h:759
uint32 LocalTransactionId
Definition: c.h:625
#define MemSet(start, val, len)
Definition: c.h:991
uint32 TransactionId
Definition: c.h:623
size_t Size
Definition: c.h:576
int64 TimestampTz
Definition: timestamp.h:39
void DeadLockReport(void)
Definition: deadlock.c:1075
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:865
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:968
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1420
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1341
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:783
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
bool hash_update_hash_key(HTAB *hashp, void *existingEntry, const void *newKeyPtr)
Definition: dynahash.c:1145
uint32 get_hash_value(HTAB *hashp, const void *keyPtr)
Definition: dynahash.c:911
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1273
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define WARNING
Definition: elog.h:36
#define PG_END_TRY(...)
Definition: elog.h:397
#define PANIC
Definition: elog.h:42
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:382
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:149
int MyProcPid
Definition: globals.c:48
ProcNumber MyProcNumber
Definition: globals.c:91
int MaxBackends
Definition: globals.c:147
Assert(PointerIsAligned(start, uint64))
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_REMOVE
Definition: hsearch.h:115
@ HASH_ENTER
Definition: hsearch.h:114
@ HASH_ENTER_NULL
Definition: hsearch.h:116
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_FUNCTION
Definition: hsearch.h:98
#define HASH_BLOBS
Definition: hsearch.h:97
#define HASH_PARTITION
Definition: hsearch.h:92
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
static bool dclist_is_empty(const dclist_head *head)
Definition: ilist.h:682
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:640
static bool dlist_is_empty(const dlist_head *head)
Definition: ilist.h:336
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
static void dclist_delete_from_thoroughly(dclist_head *head, dlist_node *node)
Definition: ilist.h:776
static void dclist_init(dclist_head *head)
Definition: ilist.h:671
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
#define dclist_foreach(iter, lhead)
Definition: ilist.h:970
int j
Definition: isn.c:78
int i
Definition: isn.c:77
void DescribeLockTag(StringInfo buf, const LOCKTAG *tag)
Definition: lmgr.c:1243
static bool XactLockForVirtualXact(VirtualTransactionId vxid, TransactionId xid, bool wait)
Definition: lock.c:4659
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:805
static LOCALLOCK * awaitedLock
Definition: lock.c:325
static void RemoveLocalLock(LOCALLOCK *locallock)
Definition: lock.c:1472
static void LockReassignOwner(LOCALLOCK *locallock, ResourceOwner parent)
Definition: lock.c:2701
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode, bool orstronger)
Definition: lock.c:639
static bool Dummy_trace
Definition: lock.c:122
static const char *const lock_mode_names[]
Definition: lock.c:108
#define LOCK_PRINT(where, lock, type)
Definition: lock.c:402
bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
Definition: lock.c:619
static PROCLOCK * SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc, const LOCKTAG *locktag, uint32 hashcode, LOCKMODE lockmode)
Definition: lock.c:1279
static PROCLOCK * FastPathGetRelationLockEntry(LOCALLOCK *locallock)
Definition: lock.c:2923
void VirtualXactLockTableInsert(VirtualTransactionId vxid)
Definition: lock.c:4587
#define NLOCKENTS()
Definition: lock.c:56
#define FastPathStrongLockHashPartition(hashcode)
Definition: lock.c:300
static uint32 ProcLockHashCode(const PROCLOCKTAG *proclocktag, uint32 hashcode)
Definition: lock.c:601
#define FAST_PATH_CHECK_LOCKMODE(proc, n, l)
Definition: lock.c:253
void GrantAwaitedLock(void)
Definition: lock.c:1885
int LockWaiterCount(const LOCKTAG *locktag)
Definition: lock.c:4821
void AtPrepare_Locks(void)
Definition: lock.c:3443
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:2067
void lock_twophase_postcommit(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4537
#define FAST_PATH_LOCKNUMBER_OFFSET
Definition: lock.c:239
Size LockManagerShmemSize(void)
Definition: lock.c:3723
#define FAST_PATH_REL_GROUP(rel)
Definition: lock.c:214
void InitLockManagerAccess(void)
Definition: lock.c:501
void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
Definition: lock.c:1654
void VirtualXactLockTableCleanup(void)
Definition: lock.c:4610
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4710
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition: lock.c:3034
static volatile FastPathStrongRelationLockData * FastPathStrongRelationLocks
Definition: lock.c:309
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition: lock.c:2011
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp, bool logLockFailure)
Definition: lock.c:832
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition: lock.c:2272
#define FAST_PATH_SLOT(group, index)
Definition: lock.c:221
static void CheckAndSetLockHeld(LOCALLOCK *locallock, bool acquired)
Definition: lock.c:1460
#define ConflictsWithRelationFastPath(locktag, mode)
Definition: lock.c:270
void ResetAwaitedLock(void)
Definition: lock.c:1903
static bool FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag, uint32 hashcode)
Definition: lock.c:2826
static HTAB * LockMethodLocalHash
Definition: lock.c:320
void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2671
static bool UnGrantLock(LOCK *lock, LOCKMODE lockmode, PROCLOCK *proclock, LockMethod lockMethodTable)
Definition: lock.c:1677
#define FAST_PATH_SET_LOCKMODE(proc, n, l)
Definition: lock.c:249
#define PROCLOCK_PRINT(where, proclockP)
Definition: lock.c:403
static void CleanUpLock(LOCK *lock, PROCLOCK *proclock, LockMethod lockMethodTable, uint32 hashcode, bool wakeupNeeded)
Definition: lock.c:1734
static uint32 proclock_hash(const void *key, Size keysize)
Definition: lock.c:570
static bool FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode)
Definition: lock.c:2790
void AbortStrongLockAcquire(void)
Definition: lock.c:1856
static bool FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
Definition: lock.c:2747
static int FastPathLocalUseCounts[FP_LOCK_GROUPS_PER_BACKEND_MAX]
Definition: lock.c:176
static HTAB * LockMethodLockHash
Definition: lock.c:318
static ResourceOwner awaitedOwner
Definition: lock.c:326
BlockedProcsData * GetBlockerStatusData(int blocked_pid)
Definition: lock.c:3963
void LockManagerShmemInit(void)
Definition: lock.c:440
void lock_twophase_postabort(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4563
static ProcWaitStatus WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
Definition: lock.c:1928
bool LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:692
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition: lock.c:4220
static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc, BlockedProcsData *data)
Definition: lock.c:4043
#define FAST_PATH_CLEAR_LOCKMODE(proc, n, l)
Definition: lock.c:251
int max_locks_per_xact
Definition: lock.c:53
static const LockMethod LockMethods[]
Definition: lock.c:150
void lock_twophase_standby_recover(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4505
void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2576
LOCALLOCK * GetAwaitedLock(void)
Definition: lock.c:1894
void lock_twophase_recover(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4324
void LockReleaseSession(LOCKMETHODID lockmethodid)
Definition: lock.c:2546
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1916
LockData * GetLockStatusData(void)
Definition: lock.c:3760
static bool IsRelationExtensionLockHeld PG_USED_FOR_ASSERTS_ONLY
Definition: lock.c:191
static const LockMethodData default_lockmethod
Definition: lock.c:125
#define FAST_PATH_GET_BITS(proc, n)
Definition: lock.c:242
static LOCALLOCK * StrongLockInProgress
Definition: lock.c:324
#define FAST_PATH_BITS_PER_SLOT
Definition: lock.c:238
static const LockMethodData user_lockmethod
Definition: lock.c:136
int FastPathLockGroupsPerBackend
Definition: lock.c:202
#define EligibleForRelationFastPath(locktag, mode)
Definition: lock.c:264
uint32 LockTagHashCode(const LOCKTAG *locktag)
Definition: lock.c:553
static void BeginStrongLockAcquire(LOCALLOCK *locallock, uint32 fasthashcode)
Definition: lock.c:1820
bool LockCheckConflicts(LockMethod lockMethodTable, LOCKMODE lockmode, LOCK *lock, PROCLOCK *proclock)
Definition: lock.c:1525
static void GrantLockLocal(LOCALLOCK *locallock, ResourceOwner owner)
Definition: lock.c:1788
static const LOCKMASK LockConflicts[]
Definition: lock.c:65
static void ReleaseLockIfHeld(LOCALLOCK *locallock, bool sessionLock)
Definition: lock.c:2611
LockMethod GetLocksMethodTable(const LOCK *lock)
Definition: lock.c:523
static void FinishStrongLockAcquire(void)
Definition: lock.c:1846
#define FAST_PATH_STRONG_LOCK_HASH_PARTITIONS
Definition: lock.c:298
void PostPrepare_Locks(TransactionId xid)
Definition: lock.c:3539
xl_standby_lock * GetRunningTransactionLocks(int *nlocks)
Definition: lock.c:4138
static void LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc, LOCKTAG *locktag, LOCKMODE lockmode, bool decrement_strong_lock_count)
Definition: lock.c:3251
static void CheckForSessionAndXactLocks(void)
Definition: lock.c:3355
static HTAB * LockMethodProcLockHash
Definition: lock.c:319
struct TwoPhaseLockRecord TwoPhaseLockRecord
LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag)
Definition: lock.c:535
bool log_lock_failure
Definition: lock.c:54
uint16 LOCKMETHODID
Definition: lock.h:123
#define DEFAULT_LOCKMETHOD
Definition: lock.h:126
struct LOCALLOCK LOCALLOCK
#define LOCK_LOCKTAG(lock)
Definition: lock.h:326
struct LOCK LOCK
#define SET_LOCKTAG_VIRTUALTRANSACTION(locktag, vxid)
Definition: lock.h:236
struct PROCLOCK PROCLOCK
@ LOCKTAG_OBJECT
Definition: lock.h:146
@ LOCKTAG_RELATION_EXTEND
Definition: lock.h:139
@ LOCKTAG_RELATION
Definition: lock.h:138
@ LOCKTAG_TUPLE
Definition: lock.h:142
@ LOCKTAG_VIRTUALTRANSACTION
Definition: lock.h:144
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:68
#define LockHashPartitionLock(hashcode)
Definition: lock.h:527
#define GET_VXID_FROM_PGPROC(vxid_dst, proc)
Definition: lock.h:78
#define LOCK_LOCKMETHOD(lock)
Definition: lock.h:325
#define LOCKBIT_OFF(lockmode)
Definition: lock.h:86
#define LOCALLOCK_LOCKMETHOD(llock)
Definition: lock.h:444
#define InvalidLocalTransactionId
Definition: lock.h:66
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:227
struct LOCKTAG LOCKTAG
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:182
#define MAX_LOCKMODES
Definition: lock.h:83
struct PROCLOCKTAG PROCLOCKTAG
#define LOCKBIT_ON(lockmode)
Definition: lock.h:85
#define LocalTransactionIdIsValid(lxid)
Definition: lock.h:67
#define LOCALLOCK_LOCKTAG(llock)
Definition: lock.h:445
#define LockHashPartition(hashcode)
Definition: lock.h:525
#define VirtualTransactionIdEquals(vxid1, vxid2)
Definition: lock.h:72
struct LOCALLOCKTAG LOCALLOCKTAG
#define PROCLOCK_LOCKMETHOD(proclock)
Definition: lock.h:383
#define LockHashPartitionLockByIndex(i)
Definition: lock.h:530
LockAcquireResult
Definition: lock.h:501
@ LOCKACQUIRE_ALREADY_CLEAR
Definition: lock.h:505
@ LOCKACQUIRE_OK
Definition: lock.h:503
@ LOCKACQUIRE_ALREADY_HELD
Definition: lock.h:504
@ LOCKACQUIRE_NOT_AVAIL
Definition: lock.h:502
#define VirtualTransactionIdIsRecoveredPreparedXact(vxid)
Definition: lock.h:70
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define ShareRowExclusiveLock
Definition: lockdefs.h:41
#define AccessShareLock
Definition: lockdefs.h:36
int LOCKMASK
Definition: lockdefs.h:25
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define ExclusiveLock
Definition: lockdefs.h:42
#define RowShareLock
Definition: lockdefs.h:37
#define ShareLock
Definition: lockdefs.h:40
#define MaxLockMode
Definition: lockdefs.h:45
#define RowExclusiveLock
Definition: lockdefs.h:38
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:97
#define LOG2_NUM_LOCK_PARTITIONS
Definition: lwlock.h:96
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1256
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:2167
void pfree(void *pointer)
Definition: mcxt.c:2147
void * palloc0(Size size)
Definition: mcxt.c:1970
MemoryContext TopMemoryContext
Definition: mcxt.c:165
void * palloc(Size size)
Definition: mcxt.c:1940
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
static PgChecksumMode mode
Definition: pg_checksums.c:55
const void size_t len
const void * data
static char * buf
Definition: pg_test_fsync.c:72
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
unsigned int Oid
Definition: postgres_ext.h:30
#define FP_LOCK_GROUPS_PER_BACKEND_MAX
Definition: proc.h:83
#define FastPathLockSlotsPerBackend()
Definition: proc.h:85
#define FP_LOCK_SLOTS_PER_GROUP
Definition: proc.h:84
ProcWaitStatus
Definition: proc.h:124
@ PROC_WAIT_STATUS_OK
Definition: proc.h:125
@ PROC_WAIT_STATUS_WAITING
Definition: proc.h:126
@ PROC_WAIT_STATUS_ERROR
Definition: proc.h:127
PGPROC * BackendPidGetProcWithLock(int pid)
Definition: procarray.c:3219
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
Definition: procarray.c:3138
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
void set_ps_display_remove_suffix(void)
Definition: ps_status.c:423
void set_ps_display_suffix(const char *suffix)
Definition: ps_status.c:371
void ResourceOwnerRememberLock(ResourceOwner owner, LOCALLOCK *locallock)
Definition: resowner.c:1062
ResourceOwner ResourceOwnerGetParent(ResourceOwner owner)
Definition: resowner.c:905
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void ResourceOwnerForgetLock(ResourceOwner owner, LOCALLOCK *locallock)
Definition: resowner.c:1082
HTAB * ShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags)
Definition: shmem.c:332
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:57
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
ProcWaitStatus JoinWaitQueue(LOCALLOCK *locallock, LockMethod lockMethodTable, bool dontWait)
Definition: proc.c:1141
PGPROC * MyProc
Definition: proc.c:67
void GetLockHoldersAndWaiters(LOCALLOCK *locallock, StringInfo lock_holders_sbuf, StringInfo lock_waiters_sbuf, int *lockHoldersNum)
Definition: proc.c:1901
ProcWaitStatus ProcSleep(LOCALLOCK *locallock)
Definition: proc.c:1310
void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
Definition: proc.c:1740
PROC_HDR * ProcGlobal
Definition: proc.c:79
void LogAccessExclusiveLockPrepare(void)
Definition: standby.c:1448
void StandbyAcquireAccessExclusiveLock(TransactionId xid, Oid dbOid, Oid relOid)
Definition: standby.c:986
void LogAccessExclusiveLock(Oid dbOid, Oid relOid)
Definition: standby.c:1431
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
int first_lock
Definition: lock.h:477
int first_waiter
Definition: lock.h:481
int num_waiters
Definition: lock.h:482
int num_locks
Definition: lock.h:478
uint32 count[FAST_PATH_STRONG_LOCK_HASH_PARTITIONS]
Definition: lock.c:306
Size keysize
Definition: hsearch.h:75
HashValueFunc hash
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
long num_partitions
Definition: hsearch.h:68
Definition: dynahash.c:220
int64 nLocks
Definition: lock.h:424
struct ResourceOwnerData * owner
Definition: lock.h:423
LOCKTAG lock
Definition: lock.h:411
LOCKMODE mode
Definition: lock.h:412
LOCALLOCKOWNER * lockOwners
Definition: lock.h:439
uint32 hashcode
Definition: lock.h:433
int maxLockOwners
Definition: lock.h:438
LOCK * lock
Definition: lock.h:434
int64 nLocks
Definition: lock.h:436
int numLockOwners
Definition: lock.h:437
bool holdsStrongLockCount
Definition: lock.h:440
PROCLOCK * proclock
Definition: lock.h:435
LOCALLOCKTAG tag
Definition: lock.h:430
bool lockCleared
Definition: lock.h:441
Definition: lock.h:166
uint8 locktag_type
Definition: lock.h:171
uint32 locktag_field3
Definition: lock.h:169
uint32 locktag_field1
Definition: lock.h:167
uint8 locktag_lockmethodid
Definition: lock.h:172
uint16 locktag_field4
Definition: lock.h:170
uint32 locktag_field2
Definition: lock.h:168
Definition: lock.h:310
int nRequested
Definition: lock.h:320
LOCKTAG tag
Definition: lock.h:312
int requested[MAX_LOCKMODES]
Definition: lock.h:319
dclist_head waitProcs
Definition: lock.h:318
int granted[MAX_LOCKMODES]
Definition: lock.h:321
LOCKMASK grantMask
Definition: lock.h:315
LOCKMASK waitMask
Definition: lock.h:316
int nGranted
Definition: lock.h:322
dlist_head procLocks
Definition: lock.h:317
Definition: lwlock.h:42
Definition: lock.h:467
LOCKMASK holdMask
Definition: lock.h:456
LOCKMODE waitLockMode
Definition: lock.h:457
bool fastpath
Definition: lock.h:463
LOCKTAG locktag
Definition: lock.h:455
TimestampTz waitStart
Definition: lock.h:459
int leaderPid
Definition: lock.h:462
VirtualTransactionId vxid
Definition: lock.h:458
const bool * trace_flag
Definition: lock.h:114
const LOCKMASK * conflictTab
Definition: lock.h:112
const char *const * lockModeNames
Definition: lock.h:113
int numLockModes
Definition: lock.h:111
Definition: proc.h:163
LWLock fpInfoLock
Definition: proc.h:294
struct PGPROC::@127 vxid
LocalTransactionId lxid
Definition: proc.h:201
PROCLOCK * waitProcLock
Definition: proc.h:234
dlist_head lockGroupMembers
Definition: proc.h:306
Oid * fpRelId
Definition: proc.h:296
Oid databaseId
Definition: proc.h:208
uint64 * fpLockBits
Definition: proc.h:295
pg_atomic_uint64 waitStart
Definition: proc.h:238
bool fpVXIDLock
Definition: proc.h:297
ProcNumber procNumber
Definition: proc.h:196
int pid
Definition: proc.h:183
LOCK * waitLock
Definition: proc.h:233
TransactionId xid
Definition: proc.h:173
LOCKMODE waitLockMode
Definition: proc.h:235
PGPROC * lockGroupLeader
Definition: proc.h:305
LocalTransactionId fpLocalTransactionId
Definition: proc.h:298
dlist_head myProcLocks[NUM_LOCK_PARTITIONS]
Definition: proc.h:262
ProcWaitStatus waitStatus
Definition: proc.h:168
dlist_node links
Definition: proc.h:164
LOCK * myLock
Definition: lock.h:366
PGPROC * myProc
Definition: lock.h:367
Definition: lock.h:371
LOCKMASK holdMask
Definition: lock.h:377
dlist_node lockLink
Definition: lock.h:379
PGPROC * groupLeader
Definition: lock.h:376
LOCKMASK releaseMask
Definition: lock.h:378
PROCLOCKTAG tag
Definition: lock.h:373
dlist_node procLink
Definition: lock.h:380
PGPROC * allProcs
Definition: proc.h:372
uint32 allProcCount
Definition: proc.h:390
LOCKTAG locktag
Definition: lock.c:160
LOCKMODE lockmode
Definition: lock.c:161
LocalTransactionId localTransactionId
Definition: lock.h:63
ProcNumber procNumber
Definition: lock.h:62
dlist_node * cur
Definition: ilist.h:179
dlist_node * cur
Definition: ilist.h:200
dlist_node * next
Definition: ilist.h:140
Definition: type.h:96
TransactionId xid
Definition: lockdefs.h:53
#define InvalidTransactionId
Definition: transam.h:31
#define FirstNormalObjectId
Definition: transam.h:197
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1264
int max_prepared_xacts
Definition: twophase.c:115
TransactionId TwoPhaseGetXidByVirtualXID(VirtualTransactionId vxid, bool *have_more)
Definition: twophase.c:852
PGPROC * TwoPhaseGetDummyProc(TransactionId xid, bool lock_held)
Definition: twophase.c:918
#define TWOPHASE_RM_LOCK_ID
Definition: twophase_rmgr.h:25
const char * type
bool RecoveryInProgress(void)
Definition: xlog.c:6522
#define XLogStandbyInfoActive()
Definition: xlog.h:123
bool InRecovery
Definition: xlogutils.c:50
#define InHotStandby
Definition: xlogutils.h:60
static struct link * links
Definition: zic.c:299