1 /*-------------------------------------------------------------------------
4 * POSTGRES low-level lock mechanism
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.93 2001/08/29 19:14:39 petere Exp $
14 * Outside modules can create a lock table and acquire/release
15 * locks. A lock table is a shared memory hash table. When
16 * a process tries to acquire a lock of a type that conflicts
17 * with existing locks, it is put to sleep using the routines
18 * in storage/lmgr/proc.c.
20 * For the most part, this code should be invoked via lmgr.c
21 * or another lock-management module, not directly.
25 * LockAcquire(), LockRelease(), LockMethodTableInit(),
26 * LockMethodTableRename(), LockReleaseAll,
27 * LockCheckConflicts(), GrantLock()
29 *-------------------------------------------------------------------------
33 #include <sys/types.h>
37 #include "access/xact.h"
38 #include "miscadmin.h"
39 #include "storage/proc.h"
40 #include "utils/memutils.h"
41 #include "utils/ps_status.h"
44 /* This configuration variable is used to set the lock table size */
45 int max_locks_per_xact; /* set by guc.c */
47 #define NLOCKENTS(maxBackends) (max_locks_per_xact * (maxBackends))
50 static int WaitOnLock(LOCKMETHOD lockmethod, LOCKMODE lockmode,
51 LOCK *lock, HOLDER *holder);
52 static void LockCountMyLocks(SHMEM_OFFSET lockOffset, PROC *proc,
55 static char *lock_mode_names[] =
61 "ShareUpdateExclusiveLock",
63 "ShareRowExclusiveLock",
72 * The following configuration options are available for lock debugging:
74 * TRACE_LOCKS -- give a bunch of output what's going on in this file
75 * TRACE_USERLOCKS -- same but for user locks
76 * TRACE_LOCK_OIDMIN-- do not trace locks for tables below this oid
77 * (use to avoid output on system tables)
78 * TRACE_LOCK_TABLE -- trace locks on this table (oid) unconditionally
79 * DEBUG_DEADLOCKS -- currently dumps locks at untimely occasions ;)
81 * Furthermore, but in storage/ipc/spin.c:
82 * TRACE_SPINLOCKS -- trace spinlocks (pretty useless)
84 * Define LOCK_DEBUG at compile time to get all these enabled.
88 int Trace_lock_oidmin = BootstrapObjectIdData;
89 bool Trace_locks = false;
90 bool Trace_userlocks = false;
91 int Trace_lock_table = 0;
92 bool Debug_deadlocks = false;
96 LOCK_DEBUG_ENABLED(const LOCK *lock)
99 (((LOCK_LOCKMETHOD(*lock) == DEFAULT_LOCKMETHOD && Trace_locks)
100 || (LOCK_LOCKMETHOD(*lock) == USER_LOCKMETHOD && Trace_userlocks))
101 && (lock->tag.relId >= (Oid) Trace_lock_oidmin))
102 || (Trace_lock_table && (lock->tag.relId == Trace_lock_table));
107 LOCK_PRINT(const char *where, const LOCK *lock, LOCKMODE type)
109 if (LOCK_DEBUG_ENABLED(lock))
111 "%s: lock(%lx) tbl(%d) rel(%u) db(%u) obj(%u) grantMask(%x) "
112 "req(%d,%d,%d,%d,%d,%d,%d)=%d "
113 "grant(%d,%d,%d,%d,%d,%d,%d)=%d wait(%d) type(%s)",
114 where, MAKE_OFFSET(lock),
115 lock->tag.lockmethod, lock->tag.relId, lock->tag.dbId,
116 lock->tag.objId.blkno, lock->grantMask,
117 lock->requested[1], lock->requested[2], lock->requested[3],
118 lock->requested[4], lock->requested[5], lock->requested[6],
119 lock->requested[7], lock->nRequested,
120 lock->granted[1], lock->granted[2], lock->granted[3],
121 lock->granted[4], lock->granted[5], lock->granted[6],
122 lock->granted[7], lock->nGranted,
123 lock->waitProcs.size, lock_mode_names[type]);
128 HOLDER_PRINT(const char *where, const HOLDER *holderP)
131 (((HOLDER_LOCKMETHOD(*holderP) == DEFAULT_LOCKMETHOD && Trace_locks)
132 || (HOLDER_LOCKMETHOD(*holderP) == USER_LOCKMETHOD && Trace_userlocks))
133 && (((LOCK *) MAKE_PTR(holderP->tag.lock))->tag.relId >= (Oid) Trace_lock_oidmin))
134 || (Trace_lock_table && (((LOCK *) MAKE_PTR(holderP->tag.lock))->tag.relId == Trace_lock_table))
137 "%s: holder(%lx) lock(%lx) tbl(%d) proc(%lx) xid(%u) hold(%d,%d,%d,%d,%d,%d,%d)=%d",
138 where, MAKE_OFFSET(holderP), holderP->tag.lock,
139 HOLDER_LOCKMETHOD(*(holderP)),
140 holderP->tag.proc, holderP->tag.xid,
141 holderP->holding[1], holderP->holding[2], holderP->holding[3],
142 holderP->holding[4], holderP->holding[5], holderP->holding[6],
143 holderP->holding[7], holderP->nHolding);
146 #else /* not LOCK_DEBUG */
148 #define LOCK_PRINT(where, lock, type)
149 #define HOLDER_PRINT(where, holderP)
151 #endif /* not LOCK_DEBUG */
155 SPINLOCK LockMgrLock; /* in Shmem or created in
156 * CreateSpinlocks() */
159 * These are to simplify/speed up some bit arithmetic.
161 * XXX is a fetch from a static array really faster than a shift?
162 * Wouldn't bet on it...
165 static LOCKMASK BITS_OFF[MAX_LOCKMODES];
166 static LOCKMASK BITS_ON[MAX_LOCKMODES];
171 static bool LockingIsDisabled;
174 * map from lockmethod to the lock table structure
176 static LOCKMETHODTABLE *LockMethodTable[MAX_LOCK_METHODS];
178 static int NumLockMethods;
181 * InitLocks -- Init the lock module. Create a private data
182 * structure for constructing conflict masks.
191 for (i = 0; i < MAX_LOCKMODES; i++, bit <<= 1)
199 * LockDisable -- sets LockingIsDisabled flag to TRUE or FALSE.
202 LockDisable(bool status)
204 LockingIsDisabled = status;
208 * Boolean function to determine current locking status
211 LockingDisabled(void)
213 return LockingIsDisabled;
217 * Fetch the lock method table associated with a given lock
220 GetLocksMethodTable(LOCK *lock)
222 LOCKMETHOD lockmethod = LOCK_LOCKMETHOD(*lock);
224 Assert(lockmethod > 0 && lockmethod < NumLockMethods);
225 return LockMethodTable[lockmethod];
230 * LockMethodInit -- initialize the lock table's lock type
233 * Notes: just copying. Should only be called once.
236 LockMethodInit(LOCKMETHODTABLE *lockMethodTable,
237 LOCKMASK *conflictsP,
243 lockMethodTable->ctl->numLockModes = numModes;
245 for (i = 0; i < numModes; i++, prioP++, conflictsP++)
247 lockMethodTable->ctl->conflictTab[i] = *conflictsP;
248 lockMethodTable->ctl->prio[i] = *prioP;
253 * LockMethodTableInit -- initialize a lock table structure
256 * (a) a lock table has four separate entries in the shmem index
257 * table. This is because every shared hash table and spinlock
258 * has its name stored in the shmem index at its creation. It
259 * is wasteful, in this case, but not much space is involved.
261 * NOTE: data structures allocated here are allocated permanently, using
262 * TopMemoryContext and shared memory. We don't ever release them anyway,
263 * and in normal multi-backend operation the lock table structures set up
264 * by the postmaster are inherited by each backend, so they must be in
268 LockMethodTableInit(char *tabName,
269 LOCKMASK *conflictsP,
274 LOCKMETHODTABLE *lockMethodTable;
279 long init_table_size,
282 if (numModes >= MAX_LOCKMODES)
284 elog(NOTICE, "LockMethodTableInit: too many lock types %d greater than %d",
285 numModes, MAX_LOCKMODES);
286 return INVALID_LOCKMETHOD;
289 /* Compute init/max size to request for lock hashtables */
290 max_table_size = NLOCKENTS(maxBackends);
291 init_table_size = max_table_size / 10;
293 /* Allocate a string for the shmem index table lookups. */
294 /* This is just temp space in this routine, so palloc is OK. */
295 shmemName = (char *) palloc(strlen(tabName) + 32);
297 /* each lock table has a non-shared, permanent header */
298 lockMethodTable = (LOCKMETHODTABLE *)
299 MemoryContextAlloc(TopMemoryContext, sizeof(LOCKMETHODTABLE));
302 * find/acquire the spinlock for the table
304 SpinAcquire(LockMgrLock);
307 * allocate a control structure from shared memory or attach to it if
310 sprintf(shmemName, "%s (ctl)", tabName);
311 lockMethodTable->ctl = (LOCKMETHODCTL *)
312 ShmemInitStruct(shmemName, sizeof(LOCKMETHODCTL), &found);
314 if (!lockMethodTable->ctl)
315 elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
323 * we're first - initialize
327 MemSet(lockMethodTable->ctl, 0, sizeof(LOCKMETHODCTL));
328 lockMethodTable->ctl->masterLock = LockMgrLock;
329 lockMethodTable->ctl->lockmethod = NumLockMethods;
333 * other modules refer to the lock table by a lockmethod ID
335 LockMethodTable[NumLockMethods] = lockMethodTable;
337 Assert(NumLockMethods <= MAX_LOCK_METHODS);
340 * allocate a hash table for LOCK structs. This is used to store
341 * per-locked-object information.
343 info.keysize = SHMEM_LOCKTAB_KEYSIZE;
344 info.datasize = SHMEM_LOCKTAB_DATASIZE;
345 info.hash = tag_hash;
346 hash_flags = (HASH_ELEM | HASH_FUNCTION);
348 sprintf(shmemName, "%s (lock hash)", tabName);
349 lockMethodTable->lockHash = ShmemInitHash(shmemName,
355 if (!lockMethodTable->lockHash)
356 elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
357 Assert(lockMethodTable->lockHash->hash == tag_hash);
360 * allocate a hash table for HOLDER structs. This is used to store
361 * per-lock-holder information.
363 info.keysize = SHMEM_HOLDERTAB_KEYSIZE;
364 info.datasize = SHMEM_HOLDERTAB_DATASIZE;
365 info.hash = tag_hash;
366 hash_flags = (HASH_ELEM | HASH_FUNCTION);
368 sprintf(shmemName, "%s (holder hash)", tabName);
369 lockMethodTable->holderHash = ShmemInitHash(shmemName,
375 if (!lockMethodTable->holderHash)
376 elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
378 /* init ctl data structures */
379 LockMethodInit(lockMethodTable, conflictsP, prioP, numModes);
381 SpinRelease(LockMgrLock);
385 return lockMethodTable->ctl->lockmethod;
389 * LockMethodTableRename -- allocate another lockmethod ID to the same
392 * NOTES: Both the lock module and the lock chain (lchain.c)
393 * module use table id's to distinguish between different
394 * kinds of locks. Short term and long term locks look
395 * the same to the lock table, but are handled differently
396 * by the lock chain manager. This function allows the
397 * client to use different lockmethods when acquiring/releasing
398 * short term and long term locks, yet store them all in one hashtable.
402 LockMethodTableRename(LOCKMETHOD lockmethod)
404 LOCKMETHOD newLockMethod;
406 if (NumLockMethods >= MAX_LOCK_METHODS)
407 return INVALID_LOCKMETHOD;
408 if (LockMethodTable[lockmethod] == INVALID_LOCKMETHOD)
409 return INVALID_LOCKMETHOD;
411 /* other modules refer to the lock table by a lockmethod ID */
412 newLockMethod = NumLockMethods;
415 LockMethodTable[newLockMethod] = LockMethodTable[lockmethod];
416 return newLockMethod;
420 * LockAcquire -- Check for lock conflicts, sleep if conflict found,
421 * set lock if/when no conflicts.
423 * Returns: TRUE if lock was acquired, FALSE otherwise. Note that
424 * a FALSE return is to be expected if dontWait is TRUE;
425 * but if dontWait is FALSE, only a parameter error can cause
426 * a FALSE return. (XXX probably we should just elog on parameter
427 * errors, instead of conflating this with failure to acquire lock?)
429 * Side Effects: The lock is acquired and recorded in lock tables.
431 * NOTE: if we wait for the lock, there is no way to abort the wait
432 * short of aborting the transaction.
435 * Note on User Locks:
437 * User locks are handled totally on the application side as
438 * long term cooperative locks which extend beyond the normal
439 * transaction boundaries. Their purpose is to indicate to an
440 * application that someone is `working' on an item. So it is
441 * possible to put an user lock on a tuple's oid, retrieve the
442 * tuple, work on it for an hour and then update it and remove
443 * the lock. While the lock is active other clients can still
444 * read and write the tuple but they can be aware that it has
445 * been locked at the application level by someone.
446 * User locks use lock tags made of an uint16 and an uint32, for
447 * example 0 and a tuple oid, or any other arbitrary pair of
448 * numbers following a convention established by the application.
449 * In this sense tags don't refer to tuples or database entities.
450 * User locks and normal locks are completely orthogonal and
451 * they don't interfere with each other, so it is possible
452 * to acquire a normal lock on an user-locked tuple or user-lock
453 * a tuple for which a normal write lock already exists.
454 * User locks are always non blocking, therefore they are never
455 * acquired if already held by another process. They must be
456 * released explicitly by the application but they are released
457 * automatically when a backend terminates.
458 * They are indicated by a lockmethod 2 which is an alias for the
459 * normal lock table, and are distinguished from normal locks
460 * by the following differences:
462 * normal lock user lock
465 * tag.dbId database oid database oid
466 * tag.relId rel oid or 0 0
467 * tag.objId block id lock id2
469 * tag.offnum 0 lock id1
470 * holder.xid xid or 0 0
471 * persistence transaction user or backend
474 * The lockmode parameter can have the same values for normal locks
475 * although probably only WRITE_LOCK can have some practical use.
481 LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
482 TransactionId xid, LOCKMODE lockmode, bool dontWait)
490 LOCKMETHODTABLE *lockMethodTable;
492 int myHolding[MAX_LOCKMODES];
496 if (lockmethod == USER_LOCKMETHOD && Trace_userlocks)
497 elog(DEBUG, "LockAcquire: user lock [%u] %s",
498 locktag->objId.blkno, lock_mode_names[lockmode]);
501 /* ???????? This must be changed when short term locks will be used */
502 locktag->lockmethod = lockmethod;
504 Assert(lockmethod < NumLockMethods);
505 lockMethodTable = LockMethodTable[lockmethod];
506 if (!lockMethodTable)
508 elog(NOTICE, "LockAcquire: bad lock table %d", lockmethod);
512 if (LockingIsDisabled)
515 masterLock = lockMethodTable->ctl->masterLock;
517 SpinAcquire(masterLock);
520 * Find or create a lock with this tag
522 Assert(lockMethodTable->lockHash->hash == tag_hash);
523 lock = (LOCK *) hash_search(lockMethodTable->lockHash, (Pointer) locktag,
527 SpinRelease(masterLock);
528 elog(FATAL, "LockAcquire: lock table %d is corrupted", lockmethod);
533 * if it's a new lock object, initialize it
539 SHMQueueInit(&(lock->lockHolders));
540 ProcQueueInit(&(lock->waitProcs));
541 lock->nRequested = 0;
543 MemSet((char *) lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
544 MemSet((char *) lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
545 LOCK_PRINT("LockAcquire: new", lock, lockmode);
549 LOCK_PRINT("LockAcquire: found", lock, lockmode);
550 Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
551 Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
552 Assert(lock->nGranted <= lock->nRequested);
556 * Create the hash key for the holder table.
558 MemSet(&holdertag, 0, sizeof(HOLDERTAG)); /* must clear padding,
560 holdertag.lock = MAKE_OFFSET(lock);
561 holdertag.proc = MAKE_OFFSET(MyProc);
562 TransactionIdStore(xid, &holdertag.xid);
565 * Find or create a holder entry with this tag
567 holderTable = lockMethodTable->holderHash;
568 holder = (HOLDER *) hash_search(holderTable, (Pointer) &holdertag,
572 SpinRelease(masterLock);
573 elog(FATAL, "LockAcquire: holder table corrupted");
578 * If new, initialize the new entry
582 holder->nHolding = 0;
583 MemSet((char *) holder->holding, 0, sizeof(int) * MAX_LOCKMODES);
584 /* Add holder to appropriate lists */
585 SHMQueueInsertBefore(&lock->lockHolders, &holder->lockLink);
586 SHMQueueInsertBefore(&MyProc->procHolders, &holder->procLink);
587 HOLDER_PRINT("LockAcquire: new", holder);
591 HOLDER_PRINT("LockAcquire: found", holder);
592 Assert((holder->nHolding >= 0) && (holder->holding[lockmode] >= 0));
593 Assert(holder->nHolding <= lock->nGranted);
595 #ifdef CHECK_DEADLOCK_RISK
598 * Issue warning if we already hold a lower-level lock on this
599 * object and do not hold a lock of the requested level or higher.
600 * This indicates a deadlock-prone coding practice (eg, we'd have
601 * a deadlock if another backend were following the same code path
602 * at about the same time).
604 * This is not enabled by default, because it may generate log
605 * entries about user-level coding practices that are in fact safe
606 * in context. It can be enabled to help find system-level
609 * XXX Doing numeric comparison on the lockmodes is a hack; it'd be
610 * better to use a table. For now, though, this works.
612 for (i = lockMethodTable->ctl->numLockModes; i > 0; i--)
614 if (holder->holding[i] > 0)
616 if (i >= (int) lockmode)
617 break; /* safe: we have a lock >= req level */
618 elog(DEBUG, "Deadlock risk: raising lock level"
619 " from %s to %s on object %u/%u/%u",
620 lock_mode_names[i], lock_mode_names[lockmode],
621 lock->tag.relId, lock->tag.dbId, lock->tag.objId.blkno);
625 #endif /* CHECK_DEADLOCK_RISK */
629 * lock->nRequested and lock->requested[] count the total number of
630 * requests, whether granted or waiting, so increment those
631 * immediately. The other counts don't increment till we get the lock.
634 lock->requested[lockmode]++;
635 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
638 * If I already hold one or more locks of the requested type, just
639 * grant myself another one without blocking.
641 if (holder->holding[lockmode] > 0)
643 GrantLock(lock, holder, lockmode);
644 HOLDER_PRINT("LockAcquire: owning", holder);
645 SpinRelease(masterLock);
650 * If this process (under any XID) is a holder of the lock, also grant
651 * myself another one without blocking.
653 LockCountMyLocks(holder->tag.lock, MyProc, myHolding);
654 if (myHolding[lockmode] > 0)
656 GrantLock(lock, holder, lockmode);
657 HOLDER_PRINT("LockAcquire: my other XID owning", holder);
658 SpinRelease(masterLock);
663 * If lock requested conflicts with locks requested by waiters, must
664 * join wait queue. Otherwise, check for conflict with already-held
665 * locks. (That's last because most complex check.)
667 if (lockMethodTable->ctl->conflictTab[lockmode] & lock->waitMask)
668 status = STATUS_FOUND;
670 status = LockCheckConflicts(lockMethodTable, lockmode,
674 if (status == STATUS_OK)
676 /* No conflict with held or previously requested locks */
677 GrantLock(lock, holder, lockmode);
681 Assert(status == STATUS_FOUND);
683 * We can't acquire the lock immediately. If caller specified no
684 * blocking, remove the holder entry and return FALSE without waiting.
688 if (holder->nHolding == 0)
690 SHMQueueDelete(&holder->lockLink);
691 SHMQueueDelete(&holder->procLink);
692 holder = (HOLDER *) hash_search(holderTable,
694 HASH_REMOVE, &found);
695 if (!holder || !found)
696 elog(NOTICE, "LockAcquire: remove holder, table corrupted");
699 HOLDER_PRINT("LockAcquire: NHOLDING", holder);
701 lock->requested[lockmode]--;
702 LOCK_PRINT("LockAcquire: conditional lock failed", lock, lockmode);
703 Assert((lock->nRequested > 0) && (lock->requested[lockmode] >= 0));
704 Assert(lock->nGranted <= lock->nRequested);
705 SpinRelease(masterLock);
710 * Construct bitmask of locks this process holds on this object.
716 for (i = 1, tmpMask = 2;
717 i <= lockMethodTable->ctl->numLockModes;
720 if (myHolding[i] > 0)
721 heldLocks |= tmpMask;
723 MyProc->heldLocks = heldLocks;
727 * Sleep till someone wakes me up.
729 status = WaitOnLock(lockmethod, lockmode, lock, holder);
732 * NOTE: do not do any material change of state between here and
733 * return. All required changes in locktable state must have been
734 * done when the lock was granted to us --- see notes in
739 * Check the holder entry status, in case something in the ipc
740 * communication doesn't work correctly.
742 if (!((holder->nHolding > 0) && (holder->holding[lockmode] > 0)))
744 HOLDER_PRINT("LockAcquire: INCONSISTENT", holder);
745 LOCK_PRINT("LockAcquire: INCONSISTENT", lock, lockmode);
746 /* Should we retry ? */
747 SpinRelease(masterLock);
750 HOLDER_PRINT("LockAcquire: granted", holder);
751 LOCK_PRINT("LockAcquire: granted", lock, lockmode);
754 SpinRelease(masterLock);
756 return status == STATUS_OK;
760 * LockCheckConflicts -- test whether requested lock conflicts
761 * with those already granted
763 * Returns STATUS_FOUND if conflict, STATUS_OK if no conflict.
766 * Here's what makes this complicated: one process's locks don't
767 * conflict with one another, even if they are held under different
768 * transaction IDs (eg, session and xact locks do not conflict).
769 * So, we must subtract off our own locks when determining whether the
770 * requested new lock conflicts with those already held.
772 * The caller can optionally pass the process's total holding counts, if
773 * known. If NULL is passed then these values will be computed internally.
776 LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
781 int *myHolding) /* myHolding[] array or NULL */
783 LOCKMETHODCTL *lockctl = lockMethodTable->ctl;
784 int numLockModes = lockctl->numLockModes;
788 int localHolding[MAX_LOCKMODES];
791 * first check for global conflicts: If no locks conflict with my
792 * request, then I get the lock.
794 * Checking for conflict: lock->grantMask represents the types of
795 * currently held locks. conflictTable[lockmode] has a bit set for
796 * each type of lock that conflicts with request. Bitwise compare
797 * tells if there is a conflict.
799 if (!(lockctl->conflictTab[lockmode] & lock->grantMask))
801 HOLDER_PRINT("LockCheckConflicts: no conflict", holder);
806 * Rats. Something conflicts. But it could still be my own lock. We
807 * have to construct a conflict mask that does not reflect our own
808 * locks. Locks held by the current process under another XID also
809 * count as "our own locks".
811 if (myHolding == NULL)
813 /* Caller didn't do calculation of total holding for me */
814 LockCountMyLocks(holder->tag.lock, proc, localHolding);
815 myHolding = localHolding;
818 /* Compute mask of lock types held by other processes */
821 for (i = 1; i <= numLockModes; i++, tmpMask <<= 1)
823 if (lock->granted[i] != myHolding[i])
828 * now check again for conflicts. 'bitmask' describes the types of
829 * locks held by other processes. If one of these conflicts with the
830 * kind of lock that I want, there is a conflict and I have to sleep.
832 if (!(lockctl->conflictTab[lockmode] & bitmask))
834 /* no conflict. OK to get the lock */
835 HOLDER_PRINT("LockCheckConflicts: resolved", holder);
839 HOLDER_PRINT("LockCheckConflicts: conflicting", holder);
844 * LockCountMyLocks --- Count total number of locks held on a given lockable
845 * object by a given process (under any transaction ID).
847 * XXX This could be rather slow if the process holds a large number of locks.
848 * Perhaps it could be sped up if we kept yet a third hashtable of per-
849 * process lock information. However, for the normal case where a transaction
850 * doesn't hold a large number of locks, keeping such a table would probably
854 LockCountMyLocks(SHMEM_OFFSET lockOffset, PROC *proc, int *myHolding)
856 SHM_QUEUE *procHolders = &(proc->procHolders);
860 MemSet(myHolding, 0, MAX_LOCKMODES * sizeof(int));
862 holder = (HOLDER *) SHMQueueNext(procHolders, procHolders,
863 offsetof(HOLDER, procLink));
867 if (lockOffset == holder->tag.lock)
869 for (i = 1; i < MAX_LOCKMODES; i++)
870 myHolding[i] += holder->holding[i];
873 holder = (HOLDER *) SHMQueueNext(procHolders, &holder->procLink,
874 offsetof(HOLDER, procLink));
879 * GrantLock -- update the lock and holder data structures to show
880 * the lock request has been granted.
882 * NOTE: if proc was blocked, it also needs to be removed from the wait list
883 * and have its waitLock/waitHolder fields cleared. That's not done here.
886 GrantLock(LOCK *lock, HOLDER *holder, LOCKMODE lockmode)
889 lock->granted[lockmode]++;
890 lock->grantMask |= BITS_ON[lockmode];
891 if (lock->granted[lockmode] == lock->requested[lockmode])
892 lock->waitMask &= BITS_OFF[lockmode];
893 LOCK_PRINT("GrantLock", lock, lockmode);
894 Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
895 Assert(lock->nGranted <= lock->nRequested);
896 holder->holding[lockmode]++;
898 Assert((holder->nHolding > 0) && (holder->holding[lockmode] > 0));
902 * WaitOnLock -- wait to acquire a lock
904 * Caller must have set MyProc->heldLocks to reflect locks already held
905 * on the lockable object by this process (under all XIDs).
907 * The locktable spinlock must be held at entry.
910 WaitOnLock(LOCKMETHOD lockmethod, LOCKMODE lockmode,
911 LOCK *lock, HOLDER *holder)
913 LOCKMETHODTABLE *lockMethodTable = LockMethodTable[lockmethod];
917 Assert(lockmethod < NumLockMethods);
919 LOCK_PRINT("WaitOnLock: sleeping on lock", lock, lockmode);
921 old_status = pstrdup(get_ps_display());
922 new_status = (char *) palloc(strlen(old_status) + 10);
923 strcpy(new_status, old_status);
924 strcat(new_status, " waiting");
925 set_ps_display(new_status);
928 * NOTE: Think not to put any shared-state cleanup after the call to
929 * ProcSleep, in either the normal or failure path. The lock state
930 * must be fully set by the lock grantor, or by HandleDeadLock if we
931 * give up waiting for the lock. This is necessary because of the
932 * possibility that a cancel/die interrupt will interrupt ProcSleep
933 * after someone else grants us the lock, but before we've noticed it.
934 * Hence, after granting, the locktable state must fully reflect the
935 * fact that we own the lock; we can't do additional work on return.
936 * Contrariwise, if we fail, any cleanup must happen in xact abort
937 * processing, not here, to ensure it will also happen in the
941 if (ProcSleep(lockMethodTable,
944 holder) != STATUS_OK)
948 * We failed as a result of a deadlock, see HandleDeadLock(). Quit
949 * now. Removal of the holder and lock objects, if no longer
950 * needed, will happen in xact cleanup (see above for motivation).
952 LOCK_PRINT("WaitOnLock: aborting on lock", lock, lockmode);
953 SpinRelease(lockMethodTable->ctl->masterLock);
954 elog(ERROR, "deadlock detected");
958 set_ps_display(old_status);
962 LOCK_PRINT("WaitOnLock: wakeup on lock", lock, lockmode);
967 * Remove a proc from the wait-queue it is on
968 * (caller must know it is on one).
970 * Locktable lock must be held by caller.
972 * NB: this does not remove the process' holder object, nor the lock object,
973 * even though their counts might now have gone to zero. That will happen
974 * during a subsequent LockReleaseAll call, which we expect will happen
975 * during transaction cleanup. (Removal of a proc from its wait queue by
976 * this routine can only happen if we are aborting the transaction.)
979 RemoveFromWaitQueue(PROC *proc)
981 LOCK *waitLock = proc->waitLock;
982 LOCKMODE lockmode = proc->waitLockMode;
984 /* Make sure proc is waiting */
985 Assert(proc->links.next != INVALID_OFFSET);
987 Assert(waitLock->waitProcs.size > 0);
989 /* Remove proc from lock's wait queue */
990 SHMQueueDelete(&(proc->links));
991 waitLock->waitProcs.size--;
993 /* Undo increments of request counts by waiting process */
994 Assert(waitLock->nRequested > 0);
995 Assert(waitLock->nRequested > proc->waitLock->nGranted);
996 waitLock->nRequested--;
997 Assert(waitLock->requested[lockmode] > 0);
998 waitLock->requested[lockmode]--;
999 /* don't forget to clear waitMask bit if appropriate */
1000 if (waitLock->granted[lockmode] == waitLock->requested[lockmode])
1001 waitLock->waitMask &= BITS_OFF[lockmode];
1003 /* Clean up the proc's own state */
1004 proc->waitLock = NULL;
1005 proc->waitHolder = NULL;
1007 /* See if any other waiters for the lock can be woken up now */
1008 ProcLockWakeup(GetLocksMethodTable(waitLock), waitLock);
1012 * LockRelease -- look up 'locktag' in lock table 'lockmethod' and
1013 * release one 'lockmode' lock on it.
1015 * Side Effects: find any waiting processes that are now wakable,
1016 * grant them their requested locks and awaken them.
1017 * (We have to grant the lock here to avoid a race between
1018 * the waking process and any new process to
1019 * come along and request the lock.)
1022 LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
1023 TransactionId xid, LOCKMODE lockmode)
1026 SPINLOCK masterLock;
1028 LOCKMETHODTABLE *lockMethodTable;
1030 HOLDERTAG holdertag;
1032 bool wakeupNeeded = false;
1035 if (lockmethod == USER_LOCKMETHOD && Trace_userlocks)
1036 elog(DEBUG, "LockRelease: user lock tag [%u] %d", locktag->objId.blkno, lockmode);
1039 /* ???????? This must be changed when short term locks will be used */
1040 locktag->lockmethod = lockmethod;
1042 Assert(lockmethod < NumLockMethods);
1043 lockMethodTable = LockMethodTable[lockmethod];
1044 if (!lockMethodTable)
1046 elog(NOTICE, "lockMethodTable is null in LockRelease");
1050 if (LockingIsDisabled)
1053 masterLock = lockMethodTable->ctl->masterLock;
1054 SpinAcquire(masterLock);
1057 * Find a lock with this tag
1059 Assert(lockMethodTable->lockHash->hash == tag_hash);
1060 lock = (LOCK *) hash_search(lockMethodTable->lockHash, (Pointer) locktag,
1064 * let the caller print its own error message, too. Do not
1069 SpinRelease(masterLock);
1070 elog(NOTICE, "LockRelease: locktable corrupted");
1076 SpinRelease(masterLock);
1077 elog(NOTICE, "LockRelease: no such lock");
1080 LOCK_PRINT("LockRelease: found", lock, lockmode);
1083 * Find the holder entry for this holder.
1085 MemSet(&holdertag, 0, sizeof(HOLDERTAG)); /* must clear padding,
1087 holdertag.lock = MAKE_OFFSET(lock);
1088 holdertag.proc = MAKE_OFFSET(MyProc);
1089 TransactionIdStore(xid, &holdertag.xid);
1091 holderTable = lockMethodTable->holderHash;
1092 holder = (HOLDER *) hash_search(holderTable, (Pointer) &holdertag,
1093 HASH_FIND_SAVE, &found);
1094 if (!holder || !found)
1096 SpinRelease(masterLock);
1098 if (!found && lockmethod == USER_LOCKMETHOD)
1099 elog(NOTICE, "LockRelease: no lock with this tag");
1102 elog(NOTICE, "LockRelease: holder table corrupted");
1105 HOLDER_PRINT("LockRelease: found", holder);
1108 * Check that we are actually holding a lock of the type we want to
1111 if (!(holder->holding[lockmode] > 0))
1113 HOLDER_PRINT("LockRelease: WRONGTYPE", holder);
1114 Assert(holder->holding[lockmode] >= 0);
1115 SpinRelease(masterLock);
1116 elog(NOTICE, "LockRelease: you don't own a lock of type %s",
1117 lock_mode_names[lockmode]);
1120 Assert(holder->nHolding > 0);
1121 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
1122 Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
1123 Assert(lock->nGranted <= lock->nRequested);
1126 * fix the general lock stats
1129 lock->requested[lockmode]--;
1131 lock->granted[lockmode]--;
1133 if (lock->granted[lockmode] == 0)
1135 /* change the conflict mask. No more of this lock type. */
1136 lock->grantMask &= BITS_OFF[lockmode];
1139 LOCK_PRINT("LockRelease: updated", lock, lockmode);
1140 Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
1141 Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
1142 Assert(lock->nGranted <= lock->nRequested);
1145 * We need only run ProcLockWakeup if the released lock conflicts with
1146 * at least one of the lock types requested by waiter(s). Otherwise
1147 * whatever conflict made them wait must still exist. NOTE: before
1148 * MVCC, we could skip wakeup if lock->granted[lockmode] was still
1149 * positive. But that's not true anymore, because the remaining
1150 * granted locks might belong to some waiter, who could now be
1151 * awakened because he doesn't conflict with his own locks.
1153 if (lockMethodTable->ctl->conflictTab[lockmode] & lock->waitMask)
1154 wakeupNeeded = true;
1156 if (lock->nRequested == 0)
1160 * if there's no one waiting in the queue, we just released the
1161 * last lock on this object. Delete it from the lock table.
1163 Assert(lockMethodTable->lockHash->hash == tag_hash);
1164 lock = (LOCK *) hash_search(lockMethodTable->lockHash,
1165 (Pointer) &(lock->tag),
1168 if (!lock || !found)
1170 SpinRelease(masterLock);
1171 elog(NOTICE, "LockRelease: remove lock, table corrupted");
1174 wakeupNeeded = false; /* should be false, but make sure */
1178 * Now fix the per-holder lock stats.
1180 holder->holding[lockmode]--;
1182 HOLDER_PRINT("LockRelease: updated", holder);
1183 Assert((holder->nHolding >= 0) && (holder->holding[lockmode] >= 0));
1186 * If this was my last hold on this lock, delete my entry in the
1189 if (holder->nHolding == 0)
1191 HOLDER_PRINT("LockRelease: deleting", holder);
1192 SHMQueueDelete(&holder->lockLink);
1193 SHMQueueDelete(&holder->procLink);
1194 holder = (HOLDER *) hash_search(holderTable, (Pointer) &holder,
1195 HASH_REMOVE_SAVED, &found);
1196 if (!holder || !found)
1198 SpinRelease(masterLock);
1199 elog(NOTICE, "LockRelease: remove holder, table corrupted");
1205 * Wake up waiters if needed.
1208 ProcLockWakeup(lockMethodTable, lock);
1210 SpinRelease(masterLock);
1215 * LockReleaseAll -- Release all locks in a process's lock list.
1217 * Well, not really *all* locks.
1219 * If 'allxids' is TRUE, all locks of the specified lock method are
1220 * released, regardless of transaction affiliation.
1222 * If 'allxids' is FALSE, all locks of the specified lock method and
1223 * specified XID are released.
1226 LockReleaseAll(LOCKMETHOD lockmethod, PROC *proc,
1227 bool allxids, TransactionId xid)
1229 SHM_QUEUE *procHolders = &(proc->procHolders);
1232 SPINLOCK masterLock;
1233 LOCKMETHODTABLE *lockMethodTable;
1240 if (lockmethod == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
1241 elog(DEBUG, "LockReleaseAll: lockmethod=%d, pid=%d",
1242 lockmethod, proc->pid);
1245 Assert(lockmethod < NumLockMethods);
1246 lockMethodTable = LockMethodTable[lockmethod];
1247 if (!lockMethodTable)
1249 elog(NOTICE, "LockReleaseAll: bad lockmethod %d", lockmethod);
1253 numLockModes = lockMethodTable->ctl->numLockModes;
1254 masterLock = lockMethodTable->ctl->masterLock;
1256 SpinAcquire(masterLock);
1258 holder = (HOLDER *) SHMQueueNext(procHolders, procHolders,
1259 offsetof(HOLDER, procLink));
1263 bool wakeupNeeded = false;
1265 /* Get link first, since we may unlink/delete this holder */
1266 nextHolder = (HOLDER *) SHMQueueNext(procHolders, &holder->procLink,
1267 offsetof(HOLDER, procLink));
1269 Assert(holder->tag.proc == MAKE_OFFSET(proc));
1271 lock = (LOCK *) MAKE_PTR(holder->tag.lock);
1273 /* Ignore items that are not of the lockmethod to be removed */
1274 if (LOCK_LOCKMETHOD(*lock) != lockmethod)
1277 /* If not allxids, ignore items that are of the wrong xid */
1278 if (!allxids && !TransactionIdEquals(xid, holder->tag.xid))
1281 HOLDER_PRINT("LockReleaseAll", holder);
1282 LOCK_PRINT("LockReleaseAll", lock, 0);
1283 Assert(lock->nRequested >= 0);
1284 Assert(lock->nGranted >= 0);
1285 Assert(lock->nGranted <= lock->nRequested);
1286 Assert(holder->nHolding >= 0);
1287 Assert(holder->nHolding <= lock->nRequested);
1290 * fix the general lock stats
1292 if (lock->nRequested != holder->nHolding)
1294 for (i = 1; i <= numLockModes; i++)
1296 Assert(holder->holding[i] >= 0);
1297 if (holder->holding[i] > 0)
1299 lock->requested[i] -= holder->holding[i];
1300 lock->granted[i] -= holder->holding[i];
1301 Assert(lock->requested[i] >= 0 && lock->granted[i] >= 0);
1302 if (lock->granted[i] == 0)
1303 lock->grantMask &= BITS_OFF[i];
1306 * Read comments in LockRelease
1308 if (!wakeupNeeded &&
1309 lockMethodTable->ctl->conflictTab[i] & lock->waitMask)
1310 wakeupNeeded = true;
1313 lock->nRequested -= holder->nHolding;
1314 lock->nGranted -= holder->nHolding;
1315 Assert((lock->nRequested >= 0) && (lock->nGranted >= 0));
1316 Assert(lock->nGranted <= lock->nRequested);
1321 * This holder accounts for all the requested locks on the
1322 * object, so we can be lazy and just zero things out.
1324 lock->nRequested = 0;
1326 /* Fix the lock status, just for next LOCK_PRINT message. */
1327 for (i = 1; i <= numLockModes; i++)
1329 Assert(lock->requested[i] == lock->granted[i]);
1330 lock->requested[i] = lock->granted[i] = 0;
1333 LOCK_PRINT("LockReleaseAll: updated", lock, 0);
1335 HOLDER_PRINT("LockReleaseAll: deleting", holder);
1338 * Remove the holder entry from the linked lists
1340 SHMQueueDelete(&holder->lockLink);
1341 SHMQueueDelete(&holder->procLink);
1344 * remove the holder entry from the hashtable
1346 holder = (HOLDER *) hash_search(lockMethodTable->holderHash,
1350 if (!holder || !found)
1352 SpinRelease(masterLock);
1353 elog(NOTICE, "LockReleaseAll: holder table corrupted");
1357 if (lock->nRequested == 0)
1361 * We've just released the last lock, so garbage-collect the
1364 LOCK_PRINT("LockReleaseAll: deleting", lock, 0);
1365 Assert(lockMethodTable->lockHash->hash == tag_hash);
1366 lock = (LOCK *) hash_search(lockMethodTable->lockHash,
1367 (Pointer) &(lock->tag),
1368 HASH_REMOVE, &found);
1369 if (!lock || !found)
1371 SpinRelease(masterLock);
1372 elog(NOTICE, "LockReleaseAll: cannot remove lock from HTAB");
1376 else if (wakeupNeeded)
1377 ProcLockWakeup(lockMethodTable, lock);
1380 holder = nextHolder;
1383 SpinRelease(masterLock);
1386 if (lockmethod == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
1387 elog(DEBUG, "LockReleaseAll: done");
1394 LockShmemSize(int maxBackends)
1397 long max_table_size = NLOCKENTS(maxBackends);
1399 size += MAXALIGN(sizeof(PROC_HDR)); /* ProcGlobal */
1400 size += maxBackends * MAXALIGN(sizeof(PROC)); /* each MyProc */
1401 size += MAX_LOCK_METHODS * MAXALIGN(sizeof(LOCKMETHODCTL)); /* each
1402 * lockMethodTable->ctl */
1404 /* lockHash table */
1405 size += hash_estimate_size(max_table_size,
1406 SHMEM_LOCKTAB_KEYSIZE,
1407 SHMEM_LOCKTAB_DATASIZE);
1409 /* holderHash table */
1410 size += hash_estimate_size(max_table_size,
1411 SHMEM_HOLDERTAB_KEYSIZE,
1412 SHMEM_HOLDERTAB_DATASIZE);
1415 * Since the lockHash entry count above is only an estimate, add 10%
1426 * Dump all locks in the proc->procHolders list.
1428 * Must have already acquired the masterLock.
1433 SHMEM_OFFSET location;
1435 SHM_QUEUE *procHolders;
1438 int lockmethod = DEFAULT_LOCKMETHOD;
1439 LOCKMETHODTABLE *lockMethodTable;
1441 ShmemPIDLookup(MyProcPid, &location);
1442 if (location == INVALID_OFFSET)
1444 proc = (PROC *) MAKE_PTR(location);
1447 procHolders = &proc->procHolders;
1449 Assert(lockmethod < NumLockMethods);
1450 lockMethodTable = LockMethodTable[lockmethod];
1451 if (!lockMethodTable)
1455 LOCK_PRINT("DumpLocks: waiting on", proc->waitLock, 0);
1457 holder = (HOLDER *) SHMQueueNext(procHolders, procHolders,
1458 offsetof(HOLDER, procLink));
1462 Assert(holder->tag.proc == MAKE_OFFSET(proc));
1464 lock = (LOCK *) MAKE_PTR(holder->tag.lock);
1466 HOLDER_PRINT("DumpLocks", holder);
1467 LOCK_PRINT("DumpLocks", lock, 0);
1469 holder = (HOLDER *) SHMQueueNext(procHolders, &holder->procLink,
1470 offsetof(HOLDER, procLink));
1475 * Dump all postgres locks. Must have already acquired the masterLock.
1480 SHMEM_OFFSET location;
1482 HOLDER *holder = NULL;
1485 int lockmethod = DEFAULT_LOCKMETHOD;
1486 LOCKMETHODTABLE *lockMethodTable;
1488 HASH_SEQ_STATUS status;
1491 ShmemPIDLookup(pid, &location);
1492 if (location == INVALID_OFFSET)
1494 proc = (PROC *) MAKE_PTR(location);
1498 Assert(lockmethod < NumLockMethods);
1499 lockMethodTable = LockMethodTable[lockmethod];
1500 if (!lockMethodTable)
1503 holderTable = lockMethodTable->holderHash;
1506 LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0);
1508 hash_seq_init(&status, holderTable);
1509 while ((holder = (HOLDER *) hash_seq_search(&status)) &&
1510 (holder != (HOLDER *) TRUE))
1512 HOLDER_PRINT("DumpAllLocks", holder);
1514 if (holder->tag.lock)
1516 lock = (LOCK *) MAKE_PTR(holder->tag.lock);
1517 LOCK_PRINT("DumpAllLocks", lock, 0);
1520 elog(DEBUG, "DumpAllLocks: holder->tag.lock = NULL");
1524 #endif /* LOCK_DEBUG */