1 /*-------------------------------------------------------------------------
4 * POSTGRES low-level lock mechanism
6 * Portions Copyright (c) 1996-2002, 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.120 2003/02/18 02:13:24 momjian 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 *-------------------------------------------------------------------------
36 #include "access/xact.h"
37 #include "miscadmin.h"
38 #include "storage/proc.h"
39 #include "utils/memutils.h"
40 #include "utils/ps_status.h"
43 /* This configuration variable is used to set the lock table size */
44 int max_locks_per_xact; /* set by guc.c */
46 #define NLOCKENTS(maxBackends) (max_locks_per_xact * (maxBackends))
49 static int WaitOnLock(LOCKMETHOD lockmethod, LOCKMODE lockmode,
50 LOCK *lock, PROCLOCK *proclock);
51 static void LockCountMyLocks(SHMEM_OFFSET lockOffset, PGPROC *proc,
54 static char *lock_mode_names[] =
60 "ShareUpdateExclusiveLock",
62 "ShareRowExclusiveLock",
71 * The following configuration options are available for lock debugging:
73 * TRACE_LOCKS -- give a bunch of output what's going on in this file
74 * TRACE_USERLOCKS -- same but for user locks
75 * TRACE_LOCK_OIDMIN-- do not trace locks for tables below this oid
76 * (use to avoid output on system tables)
77 * TRACE_LOCK_TABLE -- trace locks on this table (oid) unconditionally
78 * DEBUG_DEADLOCKS -- currently dumps locks at untimely occasions ;)
80 * Furthermore, but in storage/lmgr/lwlock.c:
81 * TRACE_LWLOCKS -- trace lightweight locks (pretty useless)
83 * Define LOCK_DEBUG at compile time to get all these enabled.
87 int Trace_lock_oidmin = BootstrapObjectIdData;
88 bool Trace_locks = false;
89 bool Trace_userlocks = false;
90 int Trace_lock_table = 0;
91 bool Debug_deadlocks = false;
95 LOCK_DEBUG_ENABLED(const LOCK *lock)
98 (((LOCK_LOCKMETHOD(*lock) == DEFAULT_LOCKMETHOD && Trace_locks)
99 || (LOCK_LOCKMETHOD(*lock) == USER_LOCKMETHOD && Trace_userlocks))
100 && (lock->tag.relId >= (Oid) Trace_lock_oidmin))
101 || (Trace_lock_table && (lock->tag.relId == Trace_lock_table));
106 LOCK_PRINT(const char *where, const LOCK *lock, LOCKMODE type)
108 if (LOCK_DEBUG_ENABLED(lock))
110 "%s: lock(%lx) tbl(%d) rel(%u) db(%u) obj(%u) grantMask(%x) "
111 "req(%d,%d,%d,%d,%d,%d,%d)=%d "
112 "grant(%d,%d,%d,%d,%d,%d,%d)=%d wait(%d) type(%s)",
113 where, MAKE_OFFSET(lock),
114 lock->tag.lockmethod, lock->tag.relId, lock->tag.dbId,
115 lock->tag.objId.blkno, lock->grantMask,
116 lock->requested[1], lock->requested[2], lock->requested[3],
117 lock->requested[4], lock->requested[5], lock->requested[6],
118 lock->requested[7], lock->nRequested,
119 lock->granted[1], lock->granted[2], lock->granted[3],
120 lock->granted[4], lock->granted[5], lock->granted[6],
121 lock->granted[7], lock->nGranted,
122 lock->waitProcs.size, lock_mode_names[type]);
127 PROCLOCK_PRINT(const char *where, const PROCLOCK *proclockP)
130 (((PROCLOCK_LOCKMETHOD(*proclockP) == DEFAULT_LOCKMETHOD && Trace_locks)
131 || (PROCLOCK_LOCKMETHOD(*proclockP) == USER_LOCKMETHOD && Trace_userlocks))
132 && (((LOCK *) MAKE_PTR(proclockP->tag.lock))->tag.relId >= (Oid) Trace_lock_oidmin))
133 || (Trace_lock_table && (((LOCK *) MAKE_PTR(proclockP->tag.lock))->tag.relId == Trace_lock_table))
136 "%s: proclock(%lx) lock(%lx) tbl(%d) proc(%lx) xid(%u) hold(%d,%d,%d,%d,%d,%d,%d)=%d",
137 where, MAKE_OFFSET(proclockP), proclockP->tag.lock,
138 PROCLOCK_LOCKMETHOD(*(proclockP)),
139 proclockP->tag.proc, proclockP->tag.xid,
140 proclockP->holding[1], proclockP->holding[2], proclockP->holding[3],
141 proclockP->holding[4], proclockP->holding[5], proclockP->holding[6],
142 proclockP->holding[7], proclockP->nHolding);
145 #else /* not LOCK_DEBUG */
147 #define LOCK_PRINT(where, lock, type)
148 #define PROCLOCK_PRINT(where, proclockP)
149 #endif /* not LOCK_DEBUG */
153 * These are to simplify/speed up some bit arithmetic.
155 * XXX is a fetch from a static array really faster than a shift?
156 * Wouldn't bet on it...
159 static LOCKMASK BITS_OFF[MAX_LOCKMODES];
160 static LOCKMASK BITS_ON[MAX_LOCKMODES];
163 * map from lockmethod to the lock table structure
165 static LOCKMETHODTABLE *LockMethodTable[MAX_LOCK_METHODS];
167 static int NumLockMethods;
170 * InitLocks -- Init the lock module. Create a private data
171 * structure for constructing conflict masks.
180 for (i = 0; i < MAX_LOCKMODES; i++, bit <<= 1)
189 * Fetch the lock method table associated with a given lock
192 GetLocksMethodTable(LOCK *lock)
194 LOCKMETHOD lockmethod = LOCK_LOCKMETHOD(*lock);
196 Assert(lockmethod > 0 && lockmethod < NumLockMethods);
197 return LockMethodTable[lockmethod];
202 * LockMethodInit -- initialize the lock table's lock type
205 * Notes: just copying. Should only be called once.
208 LockMethodInit(LOCKMETHODTABLE *lockMethodTable,
209 LOCKMASK *conflictsP,
214 lockMethodTable->numLockModes = numModes;
216 for (i = 0; i < numModes; i++, conflictsP++)
217 lockMethodTable->conflictTab[i] = *conflictsP;
221 * LockMethodTableInit -- initialize a lock table structure
223 * NOTE: data structures allocated here are allocated permanently, using
224 * TopMemoryContext and shared memory. We don't ever release them anyway,
225 * and in normal multi-backend operation the lock table structures set up
226 * by the postmaster are inherited by each backend, so they must be in
230 LockMethodTableInit(char *tabName,
231 LOCKMASK *conflictsP,
235 LOCKMETHODTABLE *lockMethodTable;
240 long init_table_size,
243 if (numModes >= MAX_LOCKMODES)
245 elog(WARNING, "LockMethodTableInit: too many lock types %d greater than %d",
246 numModes, MAX_LOCKMODES);
247 return INVALID_LOCKMETHOD;
250 /* Compute init/max size to request for lock hashtables */
251 max_table_size = NLOCKENTS(maxBackends);
252 init_table_size = max_table_size / 10;
254 /* Allocate a string for the shmem index table lookups. */
255 /* This is just temp space in this routine, so palloc is OK. */
256 shmemName = (char *) palloc(strlen(tabName) + 32);
258 /* each lock table has a header in shared memory */
259 sprintf(shmemName, "%s (lock method table)", tabName);
260 lockMethodTable = (LOCKMETHODTABLE *)
261 ShmemInitStruct(shmemName, sizeof(LOCKMETHODTABLE), &found);
263 if (!lockMethodTable)
264 elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
267 * Lock the LWLock for the table (probably not necessary here)
269 LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);
277 * we're first - initialize
281 MemSet(lockMethodTable, 0, sizeof(LOCKMETHODTABLE));
282 lockMethodTable->masterLock = LockMgrLock;
283 lockMethodTable->lockmethod = NumLockMethods;
287 * other modules refer to the lock table by a lockmethod ID
289 LockMethodTable[NumLockMethods] = lockMethodTable;
291 Assert(NumLockMethods <= MAX_LOCK_METHODS);
294 * allocate a hash table for LOCK structs. This is used to store
295 * per-locked-object information.
297 info.keysize = sizeof(LOCKTAG);
298 info.entrysize = sizeof(LOCK);
299 info.hash = tag_hash;
300 hash_flags = (HASH_ELEM | HASH_FUNCTION);
302 sprintf(shmemName, "%s (lock hash)", tabName);
303 lockMethodTable->lockHash = ShmemInitHash(shmemName,
309 if (!lockMethodTable->lockHash)
310 elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
311 Assert(lockMethodTable->lockHash->hash == tag_hash);
314 * allocate a hash table for PROCLOCK structs. This is used to store
315 * per-lock-proclock information.
317 info.keysize = sizeof(PROCLOCKTAG);
318 info.entrysize = sizeof(PROCLOCK);
319 info.hash = tag_hash;
320 hash_flags = (HASH_ELEM | HASH_FUNCTION);
322 sprintf(shmemName, "%s (proclock hash)", tabName);
323 lockMethodTable->proclockHash = ShmemInitHash(shmemName,
329 if (!lockMethodTable->proclockHash)
330 elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
332 /* init data structures */
333 LockMethodInit(lockMethodTable, conflictsP, numModes);
335 LWLockRelease(LockMgrLock);
339 return lockMethodTable->lockmethod;
343 * LockMethodTableRename -- allocate another lockmethod ID to the same
346 * NOTES: Both the lock module and the lock chain (lchain.c)
347 * module use table id's to distinguish between different
348 * kinds of locks. Short term and long term locks look
349 * the same to the lock table, but are handled differently
350 * by the lock chain manager. This function allows the
351 * client to use different lockmethods when acquiring/releasing
352 * short term and long term locks, yet store them all in one hashtable.
356 LockMethodTableRename(LOCKMETHOD lockmethod)
358 LOCKMETHOD newLockMethod;
360 if (NumLockMethods >= MAX_LOCK_METHODS)
361 return INVALID_LOCKMETHOD;
362 if (LockMethodTable[lockmethod] == INVALID_LOCKMETHOD)
363 return INVALID_LOCKMETHOD;
365 /* other modules refer to the lock table by a lockmethod ID */
366 newLockMethod = NumLockMethods;
369 LockMethodTable[newLockMethod] = LockMethodTable[lockmethod];
370 return newLockMethod;
374 * LockAcquire -- Check for lock conflicts, sleep if conflict found,
375 * set lock if/when no conflicts.
377 * Returns: TRUE if lock was acquired, FALSE otherwise. Note that
378 * a FALSE return is to be expected if dontWait is TRUE;
379 * but if dontWait is FALSE, only a parameter error can cause
380 * a FALSE return. (XXX probably we should just elog on parameter
381 * errors, instead of conflating this with failure to acquire lock?)
383 * Side Effects: The lock is acquired and recorded in lock tables.
385 * NOTE: if we wait for the lock, there is no way to abort the wait
386 * short of aborting the transaction.
389 * Note on User Locks:
391 * User locks are handled totally on the application side as
392 * long term cooperative locks which extend beyond the normal
393 * transaction boundaries. Their purpose is to indicate to an
394 * application that someone is `working' on an item. So it is
395 * possible to put an user lock on a tuple's oid, retrieve the
396 * tuple, work on it for an hour and then update it and remove
397 * the lock. While the lock is active other clients can still
398 * read and write the tuple but they can be aware that it has
399 * been locked at the application level by someone.
400 * User locks use lock tags made of an uint16 and an uint32, for
401 * example 0 and a tuple oid, or any other arbitrary pair of
402 * numbers following a convention established by the application.
403 * In this sense tags don't refer to tuples or database entities.
404 * User locks and normal locks are completely orthogonal and
405 * they don't interfere with each other, so it is possible
406 * to acquire a normal lock on an user-locked tuple or user-lock
407 * a tuple for which a normal write lock already exists.
408 * User locks are always non blocking, therefore they are never
409 * acquired if already held by another process. They must be
410 * released explicitly by the application but they are released
411 * automatically when a backend terminates.
412 * They are indicated by a lockmethod 2 which is an alias for the
413 * normal lock table, and are distinguished from normal locks
414 * by the following differences:
416 * normal lock user lock
419 * tag.dbId database oid database oid
420 * tag.relId rel oid or 0 0
421 * tag.objId block id lock id2
423 * tag.offnum 0 lock id1
424 * proclock.xid xid or 0 0
425 * persistence transaction user or backend
428 * The lockmode parameter can have the same values for normal locks
429 * although probably only WRITE_LOCK can have some practical use.
435 LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
436 TransactionId xid, LOCKMODE lockmode, bool dontWait)
439 PROCLOCKTAG proclocktag;
444 LOCKMETHODTABLE *lockMethodTable;
446 int myHolding[MAX_LOCKMODES];
450 if (lockmethod == USER_LOCKMETHOD && Trace_userlocks)
451 elog(LOG, "LockAcquire: user lock [%u] %s",
452 locktag->objId.blkno, lock_mode_names[lockmode]);
455 /* ???????? This must be changed when short term locks will be used */
456 locktag->lockmethod = lockmethod;
458 Assert(lockmethod < NumLockMethods);
459 lockMethodTable = LockMethodTable[lockmethod];
460 if (!lockMethodTable)
462 elog(WARNING, "LockAcquire: bad lock table %d", lockmethod);
466 masterLock = lockMethodTable->masterLock;
468 LWLockAcquire(masterLock, LW_EXCLUSIVE);
471 * Find or create a lock with this tag
473 Assert(lockMethodTable->lockHash->hash == tag_hash);
474 lock = (LOCK *) hash_search(lockMethodTable->lockHash,
479 LWLockRelease(masterLock);
480 elog(ERROR, "LockAcquire: lock table %d is out of memory",
486 * if it's a new lock object, initialize it
492 SHMQueueInit(&(lock->lockHolders));
493 ProcQueueInit(&(lock->waitProcs));
494 lock->nRequested = 0;
496 MemSet((char *) lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
497 MemSet((char *) lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
498 LOCK_PRINT("LockAcquire: new", lock, lockmode);
502 LOCK_PRINT("LockAcquire: found", lock, lockmode);
503 Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
504 Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
505 Assert(lock->nGranted <= lock->nRequested);
509 * Create the hash key for the proclock table.
511 MemSet(&proclocktag, 0, sizeof(PROCLOCKTAG)); /* must clear padding,
513 proclocktag.lock = MAKE_OFFSET(lock);
514 proclocktag.proc = MAKE_OFFSET(MyProc);
515 TransactionIdStore(xid, &proclocktag.xid);
518 * Find or create a proclock entry with this tag
520 proclockTable = lockMethodTable->proclockHash;
521 proclock = (PROCLOCK *) hash_search(proclockTable,
522 (void *) &proclocktag,
526 LWLockRelease(masterLock);
527 elog(ERROR, "LockAcquire: proclock table out of memory");
532 * If new, initialize the new entry
536 proclock->nHolding = 0;
537 MemSet((char *) proclock->holding, 0, sizeof(int) * MAX_LOCKMODES);
538 /* Add proclock to appropriate lists */
539 SHMQueueInsertBefore(&lock->lockHolders, &proclock->lockLink);
540 SHMQueueInsertBefore(&MyProc->procHolders, &proclock->procLink);
541 PROCLOCK_PRINT("LockAcquire: new", proclock);
545 PROCLOCK_PRINT("LockAcquire: found", proclock);
546 Assert((proclock->nHolding >= 0) && (proclock->holding[lockmode] >= 0));
547 Assert(proclock->nHolding <= lock->nGranted);
549 #ifdef CHECK_DEADLOCK_RISK
552 * Issue warning if we already hold a lower-level lock on this
553 * object and do not hold a lock of the requested level or higher.
554 * This indicates a deadlock-prone coding practice (eg, we'd have
555 * a deadlock if another backend were following the same code path
556 * at about the same time).
558 * This is not enabled by default, because it may generate log
559 * entries about user-level coding practices that are in fact safe
560 * in context. It can be enabled to help find system-level
563 * XXX Doing numeric comparison on the lockmodes is a hack; it'd be
564 * better to use a table. For now, though, this works.
566 for (i = lockMethodTable->numLockModes; i > 0; i--)
568 if (proclock->holding[i] > 0)
570 if (i >= (int) lockmode)
571 break; /* safe: we have a lock >= req level */
572 elog(LOG, "Deadlock risk: raising lock level"
573 " from %s to %s on object %u/%u/%u",
574 lock_mode_names[i], lock_mode_names[lockmode],
575 lock->tag.relId, lock->tag.dbId, lock->tag.objId.blkno);
579 #endif /* CHECK_DEADLOCK_RISK */
583 * lock->nRequested and lock->requested[] count the total number of
584 * requests, whether granted or waiting, so increment those
585 * immediately. The other counts don't increment till we get the lock.
588 lock->requested[lockmode]++;
589 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
592 * If I already hold one or more locks of the requested type, just
593 * grant myself another one without blocking.
595 if (proclock->holding[lockmode] > 0)
597 GrantLock(lock, proclock, lockmode);
598 PROCLOCK_PRINT("LockAcquire: owning", proclock);
599 LWLockRelease(masterLock);
604 * If this process (under any XID) is a proclock of the lock, also grant
605 * myself another one without blocking.
607 LockCountMyLocks(proclock->tag.lock, MyProc, myHolding);
608 if (myHolding[lockmode] > 0)
610 GrantLock(lock, proclock, lockmode);
611 PROCLOCK_PRINT("LockAcquire: my other XID owning", proclock);
612 LWLockRelease(masterLock);
617 * If lock requested conflicts with locks requested by waiters, must
618 * join wait queue. Otherwise, check for conflict with already-held
619 * locks. (That's last because most complex check.)
621 if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
622 status = STATUS_FOUND;
624 status = LockCheckConflicts(lockMethodTable, lockmode,
628 if (status == STATUS_OK)
630 /* No conflict with held or previously requested locks */
631 GrantLock(lock, proclock, lockmode);
635 Assert(status == STATUS_FOUND);
638 * We can't acquire the lock immediately. If caller specified no
639 * blocking, remove the proclock entry and return FALSE without
644 if (proclock->nHolding == 0)
646 SHMQueueDelete(&proclock->lockLink);
647 SHMQueueDelete(&proclock->procLink);
648 proclock = (PROCLOCK *) hash_search(proclockTable,
652 elog(WARNING, "LockAcquire: remove proclock, table corrupted");
655 PROCLOCK_PRINT("LockAcquire: NHOLDING", proclock);
657 lock->requested[lockmode]--;
658 LOCK_PRINT("LockAcquire: conditional lock failed", lock, lockmode);
659 Assert((lock->nRequested > 0) && (lock->requested[lockmode] >= 0));
660 Assert(lock->nGranted <= lock->nRequested);
661 LWLockRelease(masterLock);
666 * Construct bitmask of locks this process holds on this object.
672 for (i = 1, tmpMask = 2;
673 i <= lockMethodTable->numLockModes;
676 if (myHolding[i] > 0)
677 heldLocks |= tmpMask;
679 MyProc->heldLocks = heldLocks;
683 * Sleep till someone wakes me up.
685 status = WaitOnLock(lockmethod, lockmode, lock, proclock);
688 * NOTE: do not do any material change of state between here and
689 * return. All required changes in locktable state must have been
690 * done when the lock was granted to us --- see notes in
695 * Check the proclock entry status, in case something in the ipc
696 * communication doesn't work correctly.
698 if (!((proclock->nHolding > 0) && (proclock->holding[lockmode] > 0)))
700 PROCLOCK_PRINT("LockAcquire: INCONSISTENT", proclock);
701 LOCK_PRINT("LockAcquire: INCONSISTENT", lock, lockmode);
702 /* Should we retry ? */
703 LWLockRelease(masterLock);
706 PROCLOCK_PRINT("LockAcquire: granted", proclock);
707 LOCK_PRINT("LockAcquire: granted", lock, lockmode);
710 LWLockRelease(masterLock);
712 return status == STATUS_OK;
716 * LockCheckConflicts -- test whether requested lock conflicts
717 * with those already granted
719 * Returns STATUS_FOUND if conflict, STATUS_OK if no conflict.
722 * Here's what makes this complicated: one process's locks don't
723 * conflict with one another, even if they are held under different
724 * transaction IDs (eg, session and xact locks do not conflict).
725 * So, we must subtract off our own locks when determining whether the
726 * requested new lock conflicts with those already held.
728 * The caller can optionally pass the process's total holding counts, if
729 * known. If NULL is passed then these values will be computed internally.
732 LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
737 int *myHolding) /* myHolding[] array or NULL */
739 int numLockModes = lockMethodTable->numLockModes;
743 int localHolding[MAX_LOCKMODES];
746 * first check for global conflicts: If no locks conflict with my
747 * request, then I get the lock.
749 * Checking for conflict: lock->grantMask represents the types of
750 * currently held locks. conflictTable[lockmode] has a bit set for
751 * each type of lock that conflicts with request. Bitwise compare
752 * tells if there is a conflict.
754 if (!(lockMethodTable->conflictTab[lockmode] & lock->grantMask))
756 PROCLOCK_PRINT("LockCheckConflicts: no conflict", proclock);
761 * Rats. Something conflicts. But it could still be my own lock. We
762 * have to construct a conflict mask that does not reflect our own
763 * locks. Locks held by the current process under another XID also
764 * count as "our own locks".
766 if (myHolding == NULL)
768 /* Caller didn't do calculation of total holding for me */
769 LockCountMyLocks(proclock->tag.lock, proc, localHolding);
770 myHolding = localHolding;
773 /* Compute mask of lock types held by other processes */
776 for (i = 1; i <= numLockModes; i++, tmpMask <<= 1)
778 if (lock->granted[i] != myHolding[i])
783 * now check again for conflicts. 'bitmask' describes the types of
784 * locks held by other processes. If one of these conflicts with the
785 * kind of lock that I want, there is a conflict and I have to sleep.
787 if (!(lockMethodTable->conflictTab[lockmode] & bitmask))
789 /* no conflict. OK to get the lock */
790 PROCLOCK_PRINT("LockCheckConflicts: resolved", proclock);
794 PROCLOCK_PRINT("LockCheckConflicts: conflicting", proclock);
799 * LockCountMyLocks --- Count total number of locks held on a given lockable
800 * object by a given process (under any transaction ID).
802 * XXX This could be rather slow if the process holds a large number of locks.
803 * Perhaps it could be sped up if we kept yet a third hashtable of per-
804 * process lock information. However, for the normal case where a transaction
805 * doesn't hold a large number of locks, keeping such a table would probably
809 LockCountMyLocks(SHMEM_OFFSET lockOffset, PGPROC *proc, int *myHolding)
811 SHM_QUEUE *procHolders = &(proc->procHolders);
815 MemSet(myHolding, 0, MAX_LOCKMODES * sizeof(int));
817 proclock = (PROCLOCK *) SHMQueueNext(procHolders, procHolders,
818 offsetof(PROCLOCK, procLink));
822 if (lockOffset == proclock->tag.lock)
824 for (i = 1; i < MAX_LOCKMODES; i++)
825 myHolding[i] += proclock->holding[i];
828 proclock = (PROCLOCK *) SHMQueueNext(procHolders, &proclock->procLink,
829 offsetof(PROCLOCK, procLink));
834 * GrantLock -- update the lock and proclock data structures to show
835 * the lock request has been granted.
837 * NOTE: if proc was blocked, it also needs to be removed from the wait list
838 * and have its waitLock/waitHolder fields cleared. That's not done here.
841 GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
844 lock->granted[lockmode]++;
845 lock->grantMask |= BITS_ON[lockmode];
846 if (lock->granted[lockmode] == lock->requested[lockmode])
847 lock->waitMask &= BITS_OFF[lockmode];
848 LOCK_PRINT("GrantLock", lock, lockmode);
849 Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
850 Assert(lock->nGranted <= lock->nRequested);
851 proclock->holding[lockmode]++;
852 proclock->nHolding++;
853 Assert((proclock->nHolding > 0) && (proclock->holding[lockmode] > 0));
857 * WaitOnLock -- wait to acquire a lock
859 * Caller must have set MyProc->heldLocks to reflect locks already held
860 * on the lockable object by this process (under all XIDs).
862 * The locktable's masterLock must be held at entry.
865 WaitOnLock(LOCKMETHOD lockmethod, LOCKMODE lockmode,
866 LOCK *lock, PROCLOCK *proclock)
868 LOCKMETHODTABLE *lockMethodTable = LockMethodTable[lockmethod];
872 Assert(lockmethod < NumLockMethods);
874 LOCK_PRINT("WaitOnLock: sleeping on lock", lock, lockmode);
876 old_status = pstrdup(get_ps_display());
877 new_status = (char *) palloc(strlen(old_status) + 10);
878 strcpy(new_status, old_status);
879 strcat(new_status, " waiting");
880 set_ps_display(new_status);
883 * NOTE: Think not to put any shared-state cleanup after the call to
884 * ProcSleep, in either the normal or failure path. The lock state
885 * must be fully set by the lock grantor, or by CheckDeadLock if we
886 * give up waiting for the lock. This is necessary because of the
887 * possibility that a cancel/die interrupt will interrupt ProcSleep
888 * after someone else grants us the lock, but before we've noticed it.
889 * Hence, after granting, the locktable state must fully reflect the
890 * fact that we own the lock; we can't do additional work on return.
891 * Contrariwise, if we fail, any cleanup must happen in xact abort
892 * processing, not here, to ensure it will also happen in the
896 if (ProcSleep(lockMethodTable,
899 proclock) != STATUS_OK)
902 * We failed as a result of a deadlock, see CheckDeadLock(). Quit
903 * now. Removal of the proclock and lock objects, if no longer
904 * needed, will happen in xact cleanup (see above for motivation).
906 LOCK_PRINT("WaitOnLock: aborting on lock", lock, lockmode);
907 LWLockRelease(lockMethodTable->masterLock);
909 * Now that we aren't holding the LockMgrLock, print details about
910 * the detected deadlock. We didn't want to do this before because
911 * sending elog messages to the client while holding the shared lock
912 * is bad for concurrency.
915 elog(ERROR, "deadlock detected");
919 set_ps_display(old_status);
923 LOCK_PRINT("WaitOnLock: wakeup on lock", lock, lockmode);
928 * Remove a proc from the wait-queue it is on
929 * (caller must know it is on one).
931 * Locktable lock must be held by caller.
933 * NB: this does not remove the process' proclock object, nor the lock object,
934 * even though their counts might now have gone to zero. That will happen
935 * during a subsequent LockReleaseAll call, which we expect will happen
936 * during transaction cleanup. (Removal of a proc from its wait queue by
937 * this routine can only happen if we are aborting the transaction.)
940 RemoveFromWaitQueue(PGPROC *proc)
942 LOCK *waitLock = proc->waitLock;
943 LOCKMODE lockmode = proc->waitLockMode;
945 /* Make sure proc is waiting */
946 Assert(proc->links.next != INVALID_OFFSET);
948 Assert(waitLock->waitProcs.size > 0);
950 /* Remove proc from lock's wait queue */
951 SHMQueueDelete(&(proc->links));
952 waitLock->waitProcs.size--;
954 /* Undo increments of request counts by waiting process */
955 Assert(waitLock->nRequested > 0);
956 Assert(waitLock->nRequested > proc->waitLock->nGranted);
957 waitLock->nRequested--;
958 Assert(waitLock->requested[lockmode] > 0);
959 waitLock->requested[lockmode]--;
960 /* don't forget to clear waitMask bit if appropriate */
961 if (waitLock->granted[lockmode] == waitLock->requested[lockmode])
962 waitLock->waitMask &= BITS_OFF[lockmode];
964 /* Clean up the proc's own state */
965 proc->waitLock = NULL;
966 proc->waitHolder = NULL;
968 /* See if any other waiters for the lock can be woken up now */
969 ProcLockWakeup(GetLocksMethodTable(waitLock), waitLock);
973 * LockRelease -- look up 'locktag' in lock table 'lockmethod' and
974 * release one 'lockmode' lock on it.
976 * Side Effects: find any waiting processes that are now wakable,
977 * grant them their requested locks and awaken them.
978 * (We have to grant the lock here to avoid a race between
979 * the waking process and any new process to
980 * come along and request the lock.)
983 LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
984 TransactionId xid, LOCKMODE lockmode)
988 LOCKMETHODTABLE *lockMethodTable;
990 PROCLOCKTAG proclocktag;
992 bool wakeupNeeded = false;
995 if (lockmethod == USER_LOCKMETHOD && Trace_userlocks)
996 elog(LOG, "LockRelease: user lock tag [%u] %d", locktag->objId.blkno, lockmode);
999 /* ???????? This must be changed when short term locks will be used */
1000 locktag->lockmethod = lockmethod;
1002 Assert(lockmethod < NumLockMethods);
1003 lockMethodTable = LockMethodTable[lockmethod];
1004 if (!lockMethodTable)
1006 elog(WARNING, "lockMethodTable is null in LockRelease");
1010 masterLock = lockMethodTable->masterLock;
1011 LWLockAcquire(masterLock, LW_EXCLUSIVE);
1014 * Find a lock with this tag
1016 Assert(lockMethodTable->lockHash->hash == tag_hash);
1017 lock = (LOCK *) hash_search(lockMethodTable->lockHash,
1022 * let the caller print its own error message, too. Do not
1027 LWLockRelease(masterLock);
1028 elog(WARNING, "LockRelease: no such lock");
1031 LOCK_PRINT("LockRelease: found", lock, lockmode);
1034 * Find the proclock entry for this proclock.
1036 MemSet(&proclocktag, 0, sizeof(PROCLOCKTAG)); /* must clear padding,
1038 proclocktag.lock = MAKE_OFFSET(lock);
1039 proclocktag.proc = MAKE_OFFSET(MyProc);
1040 TransactionIdStore(xid, &proclocktag.xid);
1042 proclockTable = lockMethodTable->proclockHash;
1043 proclock = (PROCLOCK *) hash_search(proclockTable,
1044 (void *) &proclocktag,
1045 HASH_FIND_SAVE, NULL);
1048 LWLockRelease(masterLock);
1050 if (lockmethod == USER_LOCKMETHOD)
1051 elog(WARNING, "LockRelease: no lock with this tag");
1054 elog(WARNING, "LockRelease: proclock table corrupted");
1057 PROCLOCK_PRINT("LockRelease: found", proclock);
1060 * Check that we are actually holding a lock of the type we want to
1063 if (!(proclock->holding[lockmode] > 0))
1065 PROCLOCK_PRINT("LockRelease: WRONGTYPE", proclock);
1066 Assert(proclock->holding[lockmode] >= 0);
1067 LWLockRelease(masterLock);
1068 elog(WARNING, "LockRelease: you don't own a lock of type %s",
1069 lock_mode_names[lockmode]);
1072 Assert(proclock->nHolding > 0);
1073 Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
1074 Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
1075 Assert(lock->nGranted <= lock->nRequested);
1078 * fix the general lock stats
1081 lock->requested[lockmode]--;
1083 lock->granted[lockmode]--;
1085 if (lock->granted[lockmode] == 0)
1087 /* change the conflict mask. No more of this lock type. */
1088 lock->grantMask &= BITS_OFF[lockmode];
1091 LOCK_PRINT("LockRelease: updated", lock, lockmode);
1092 Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
1093 Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
1094 Assert(lock->nGranted <= lock->nRequested);
1097 * We need only run ProcLockWakeup if the released lock conflicts with
1098 * at least one of the lock types requested by waiter(s). Otherwise
1099 * whatever conflict made them wait must still exist. NOTE: before
1100 * MVCC, we could skip wakeup if lock->granted[lockmode] was still
1101 * positive. But that's not true anymore, because the remaining
1102 * granted locks might belong to some waiter, who could now be
1103 * awakened because he doesn't conflict with his own locks.
1105 if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
1106 wakeupNeeded = true;
1108 if (lock->nRequested == 0)
1111 * if there's no one waiting in the queue, we just released the
1112 * last lock on this object. Delete it from the lock table.
1114 Assert(lockMethodTable->lockHash->hash == tag_hash);
1115 lock = (LOCK *) hash_search(lockMethodTable->lockHash,
1116 (void *) &(lock->tag),
1121 LWLockRelease(masterLock);
1122 elog(WARNING, "LockRelease: remove lock, table corrupted");
1125 wakeupNeeded = false; /* should be false, but make sure */
1129 * Now fix the per-proclock lock stats.
1131 proclock->holding[lockmode]--;
1132 proclock->nHolding--;
1133 PROCLOCK_PRINT("LockRelease: updated", proclock);
1134 Assert((proclock->nHolding >= 0) && (proclock->holding[lockmode] >= 0));
1137 * If this was my last hold on this lock, delete my entry in the
1140 if (proclock->nHolding == 0)
1142 PROCLOCK_PRINT("LockRelease: deleting", proclock);
1143 SHMQueueDelete(&proclock->lockLink);
1144 SHMQueueDelete(&proclock->procLink);
1145 proclock = (PROCLOCK *) hash_search(proclockTable,
1147 HASH_REMOVE_SAVED, NULL);
1150 LWLockRelease(masterLock);
1151 elog(WARNING, "LockRelease: remove proclock, table corrupted");
1157 * Wake up waiters if needed.
1160 ProcLockWakeup(lockMethodTable, lock);
1162 LWLockRelease(masterLock);
1167 * LockReleaseAll -- Release all locks in a process's lock list.
1169 * Well, not really *all* locks.
1171 * If 'allxids' is TRUE, all locks of the specified lock method are
1172 * released, regardless of transaction affiliation.
1174 * If 'allxids' is FALSE, all locks of the specified lock method and
1175 * specified XID are released.
1178 LockReleaseAll(LOCKMETHOD lockmethod, PGPROC *proc,
1179 bool allxids, TransactionId xid)
1181 SHM_QUEUE *procHolders = &(proc->procHolders);
1183 PROCLOCK *nextHolder;
1184 LWLockId masterLock;
1185 LOCKMETHODTABLE *lockMethodTable;
1191 if (lockmethod == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
1192 elog(LOG, "LockReleaseAll: lockmethod=%d, pid=%d",
1193 lockmethod, proc->pid);
1196 Assert(lockmethod < NumLockMethods);
1197 lockMethodTable = LockMethodTable[lockmethod];
1198 if (!lockMethodTable)
1200 elog(WARNING, "LockReleaseAll: bad lockmethod %d", lockmethod);
1204 numLockModes = lockMethodTable->numLockModes;
1205 masterLock = lockMethodTable->masterLock;
1207 LWLockAcquire(masterLock, LW_EXCLUSIVE);
1209 proclock = (PROCLOCK *) SHMQueueNext(procHolders, procHolders,
1210 offsetof(PROCLOCK, procLink));
1214 bool wakeupNeeded = false;
1216 /* Get link first, since we may unlink/delete this proclock */
1217 nextHolder = (PROCLOCK *) SHMQueueNext(procHolders, &proclock->procLink,
1218 offsetof(PROCLOCK, procLink));
1220 Assert(proclock->tag.proc == MAKE_OFFSET(proc));
1222 lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
1224 /* Ignore items that are not of the lockmethod to be removed */
1225 if (LOCK_LOCKMETHOD(*lock) != lockmethod)
1228 /* If not allxids, ignore items that are of the wrong xid */
1229 if (!allxids && !TransactionIdEquals(xid, proclock->tag.xid))
1232 PROCLOCK_PRINT("LockReleaseAll", proclock);
1233 LOCK_PRINT("LockReleaseAll", lock, 0);
1234 Assert(lock->nRequested >= 0);
1235 Assert(lock->nGranted >= 0);
1236 Assert(lock->nGranted <= lock->nRequested);
1237 Assert(proclock->nHolding >= 0);
1238 Assert(proclock->nHolding <= lock->nRequested);
1241 * fix the general lock stats
1243 if (lock->nRequested != proclock->nHolding)
1245 for (i = 1; i <= numLockModes; i++)
1247 Assert(proclock->holding[i] >= 0);
1248 if (proclock->holding[i] > 0)
1250 lock->requested[i] -= proclock->holding[i];
1251 lock->granted[i] -= proclock->holding[i];
1252 Assert(lock->requested[i] >= 0 && lock->granted[i] >= 0);
1253 if (lock->granted[i] == 0)
1254 lock->grantMask &= BITS_OFF[i];
1257 * Read comments in LockRelease
1259 if (!wakeupNeeded &&
1260 lockMethodTable->conflictTab[i] & lock->waitMask)
1261 wakeupNeeded = true;
1264 lock->nRequested -= proclock->nHolding;
1265 lock->nGranted -= proclock->nHolding;
1266 Assert((lock->nRequested >= 0) && (lock->nGranted >= 0));
1267 Assert(lock->nGranted <= lock->nRequested);
1272 * This proclock accounts for all the requested locks on the
1273 * object, so we can be lazy and just zero things out.
1275 lock->nRequested = 0;
1277 /* Fix the lock status, just for next LOCK_PRINT message. */
1278 for (i = 1; i <= numLockModes; i++)
1280 Assert(lock->requested[i] == lock->granted[i]);
1281 lock->requested[i] = lock->granted[i] = 0;
1284 LOCK_PRINT("LockReleaseAll: updated", lock, 0);
1286 PROCLOCK_PRINT("LockReleaseAll: deleting", proclock);
1289 * Remove the proclock entry from the linked lists
1291 SHMQueueDelete(&proclock->lockLink);
1292 SHMQueueDelete(&proclock->procLink);
1295 * remove the proclock entry from the hashtable
1297 proclock = (PROCLOCK *) hash_search(lockMethodTable->proclockHash,
1303 LWLockRelease(masterLock);
1304 elog(WARNING, "LockReleaseAll: proclock table corrupted");
1308 if (lock->nRequested == 0)
1311 * We've just released the last lock, so garbage-collect the
1314 LOCK_PRINT("LockReleaseAll: deleting", lock, 0);
1315 Assert(lockMethodTable->lockHash->hash == tag_hash);
1316 lock = (LOCK *) hash_search(lockMethodTable->lockHash,
1317 (void *) &(lock->tag),
1321 LWLockRelease(masterLock);
1322 elog(WARNING, "LockReleaseAll: cannot remove lock from HTAB");
1326 else if (wakeupNeeded)
1327 ProcLockWakeup(lockMethodTable, lock);
1330 proclock = nextHolder;
1333 LWLockRelease(masterLock);
1336 if (lockmethod == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
1337 elog(LOG, "LockReleaseAll: done");
1344 LockShmemSize(int maxBackends)
1347 long max_table_size = NLOCKENTS(maxBackends);
1349 size += MAXALIGN(sizeof(PROC_HDR)); /* ProcGlobal */
1350 size += maxBackends * MAXALIGN(sizeof(PGPROC)); /* each MyProc */
1351 size += MAX_LOCK_METHODS * MAXALIGN(sizeof(LOCKMETHODTABLE)); /* each lockMethodTable */
1353 /* lockHash table */
1354 size += hash_estimate_size(max_table_size, sizeof(LOCK));
1356 /* proclockHash table */
1357 size += hash_estimate_size(max_table_size, sizeof(PROCLOCK));
1360 * Since the lockHash entry count above is only an estimate, add 10%
1369 * GetLockStatusData - Return a summary of the lock manager's internal
1370 * status, for use in a user-level reporting function.
1372 * The return data consists of an array of PROCLOCK objects, with the
1373 * associated PGPROC and LOCK objects for each. Note that multiple
1374 * copies of the same PGPROC and/or LOCK objects are likely to appear.
1375 * It is the caller's responsibility to match up duplicates if wanted.
1377 * The design goal is to hold the LockMgrLock for as short a time as possible;
1378 * thus, this function simply makes a copy of the necessary data and releases
1379 * the lock, allowing the caller to contemplate and format the data for as
1380 * long as it pleases.
1383 GetLockStatusData(void)
1386 HTAB *proclockTable;
1388 HASH_SEQ_STATUS seqstat;
1391 data = (LockData *) palloc(sizeof(LockData));
1393 LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);
1395 proclockTable = LockMethodTable[DEFAULT_LOCKMETHOD]->proclockHash;
1397 data->nelements = i = proclockTable->hctl->nentries;
1400 i = 1; /* avoid palloc(0) if empty table */
1402 data->proclockaddrs = (SHMEM_OFFSET *) palloc(sizeof(SHMEM_OFFSET) * i);
1403 data->proclocks = (PROCLOCK *) palloc(sizeof(PROCLOCK) * i);
1404 data->procs = (PGPROC *) palloc(sizeof(PGPROC) * i);
1405 data->locks = (LOCK *) palloc(sizeof(LOCK) * i);
1407 hash_seq_init(&seqstat, proclockTable);
1410 while ((proclock = hash_seq_search(&seqstat)))
1412 PGPROC *proc = (PGPROC *) MAKE_PTR(proclock->tag.proc);
1413 LOCK *lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
1415 data->proclockaddrs[i] = MAKE_OFFSET(proclock);
1416 memcpy(&(data->proclocks[i]), proclock, sizeof(PROCLOCK));
1417 memcpy(&(data->procs[i]), proc, sizeof(PGPROC));
1418 memcpy(&(data->locks[i]), lock, sizeof(LOCK));
1423 LWLockRelease(LockMgrLock);
1425 Assert(i == data->nelements);
1430 /* Provide the textual name of any lock mode */
1432 GetLockmodeName(LOCKMODE mode)
1434 Assert(mode <= MAX_LOCKMODES);
1435 return lock_mode_names[mode];
1440 * Dump all locks in the proc->procHolders list.
1442 * Must have already acquired the masterLock.
1448 SHM_QUEUE *procHolders;
1451 int lockmethod = DEFAULT_LOCKMETHOD;
1452 LOCKMETHODTABLE *lockMethodTable;
1458 procHolders = &proc->procHolders;
1460 Assert(lockmethod < NumLockMethods);
1461 lockMethodTable = LockMethodTable[lockmethod];
1462 if (!lockMethodTable)
1466 LOCK_PRINT("DumpLocks: waiting on", proc->waitLock, 0);
1468 proclock = (PROCLOCK *) SHMQueueNext(procHolders, procHolders,
1469 offsetof(PROCLOCK, procLink));
1473 Assert(proclock->tag.proc == MAKE_OFFSET(proc));
1475 lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
1477 PROCLOCK_PRINT("DumpLocks", proclock);
1478 LOCK_PRINT("DumpLocks", lock, 0);
1480 proclock = (PROCLOCK *) SHMQueueNext(procHolders, &proclock->procLink,
1481 offsetof(PROCLOCK, procLink));
1486 * Dump all postgres locks. Must have already acquired the masterLock.
1494 int lockmethod = DEFAULT_LOCKMETHOD;
1495 LOCKMETHODTABLE *lockMethodTable;
1496 HTAB *proclockTable;
1497 HASH_SEQ_STATUS status;
1503 Assert(lockmethod < NumLockMethods);
1504 lockMethodTable = LockMethodTable[lockmethod];
1505 if (!lockMethodTable)
1508 proclockTable = lockMethodTable->proclockHash;
1511 LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0);
1513 hash_seq_init(&status, proclockTable);
1514 while ((proclock = (PROCLOCK *) hash_seq_search(&status)) != NULL)
1516 PROCLOCK_PRINT("DumpAllLocks", proclock);
1518 if (proclock->tag.lock)
1520 lock = (LOCK *) MAKE_PTR(proclock->tag.lock);
1521 LOCK_PRINT("DumpAllLocks", lock, 0);
1524 elog(LOG, "DumpAllLocks: proclock->tag.lock = NULL");
1528 #endif /* LOCK_DEBUG */