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