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