1 /*-------------------------------------------------------------------------
4 * POSTGRES lock manager code
6 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/storage/lmgr/lmgr.c
13 *-------------------------------------------------------------------------
18 #include "access/subtrans.h"
19 #include "access/transam.h"
20 #include "access/xact.h"
21 #include "catalog/catalog.h"
22 #include "miscadmin.h"
23 #include "storage/lmgr.h"
24 #include "storage/procarray.h"
25 #include "utils/inval.h"
29 * Per-backend counter for generating speculative insertion tokens.
31 * This may wrap around, but that's OK as it's only used for the short
32 * duration between inserting a tuple and checking that there are no (unique)
33 * constraint violations. It's theoretically possible that a backend sees a
34 * tuple that was speculatively inserted by another backend, but before it has
35 * started waiting on the token, the other backend completes its insertion,
36 * and then then performs 2^32 unrelated insertions. And after all that, the
37 * first backend finally calls SpeculativeInsertionLockAcquire(), with the
38 * intention of waiting for the first insertion to complete, but ends up
39 * waiting for the latest unrelated insertion instead. Even then, nothing
40 * particularly bad happens: in the worst case they deadlock, causing one of
41 * the transactions to abort.
43 static uint32 speculativeInsertionToken = 0;
47 * Struct to hold context info for transaction lock waits.
49 * 'oper' is the operation that needs to wait for the other transaction; 'rel'
50 * and 'ctid' specify the address of the tuple being waited for.
52 typedef struct XactLockTableWaitInfo
57 } XactLockTableWaitInfo;
59 static void XactLockTableWaitErrorCb(void *arg);
62 * RelationInitLockInfo
63 * Initializes the lock information in a relation descriptor.
65 * relcache.c must call this during creation of any reldesc.
68 RelationInitLockInfo(Relation relation)
70 Assert(RelationIsValid(relation));
71 Assert(OidIsValid(RelationGetRelid(relation)));
73 relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
75 if (relation->rd_rel->relisshared)
76 relation->rd_lockInfo.lockRelId.dbId = InvalidOid;
78 relation->rd_lockInfo.lockRelId.dbId = MyDatabaseId;
82 * SetLocktagRelationOid
83 * Set up a locktag for a relation, given only relation OID
86 SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
90 if (IsSharedRelation(relid))
95 SET_LOCKTAG_RELATION(*tag, dbid, relid);
101 * Lock a relation given only its OID. This should generally be used
102 * before attempting to open the relation's relcache entry.
105 LockRelationOid(Oid relid, LOCKMODE lockmode)
108 LockAcquireResult res;
110 SetLocktagRelationOid(&tag, relid);
112 res = LockAcquire(&tag, lockmode, false, false);
115 * Now that we have the lock, check for invalidation messages, so that we
116 * will update or flush any stale relcache entry before we try to use it.
117 * RangeVarGetRelid() specifically relies on us for this. We can skip
118 * this in the not-uncommon case that we already had the same type of lock
119 * being requested, since then no one else could have modified the
120 * relcache entry in an undesirable way. (In the case where our own xact
121 * modifies the rel, the relcache update happens via
122 * CommandCounterIncrement, not here.)
124 if (res != LOCKACQUIRE_ALREADY_HELD)
125 AcceptInvalidationMessages();
129 * ConditionalLockRelationOid
131 * As above, but only lock if we can get the lock without blocking.
132 * Returns TRUE iff the lock was acquired.
134 * NOTE: we do not currently need conditional versions of all the
135 * LockXXX routines in this file, but they could easily be added if needed.
138 ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
141 LockAcquireResult res;
143 SetLocktagRelationOid(&tag, relid);
145 res = LockAcquire(&tag, lockmode, false, true);
147 if (res == LOCKACQUIRE_NOT_AVAIL)
151 * Now that we have the lock, check for invalidation messages; see notes
152 * in LockRelationOid.
154 if (res != LOCKACQUIRE_ALREADY_HELD)
155 AcceptInvalidationMessages();
163 * Unlock, given a LockRelId. This is preferred over UnlockRelationOid
167 UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
171 SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
173 LockRelease(&tag, lockmode, false);
179 * Unlock, given only a relation Oid. Use UnlockRelationId if you can.
182 UnlockRelationOid(Oid relid, LOCKMODE lockmode)
186 SetLocktagRelationOid(&tag, relid);
188 LockRelease(&tag, lockmode, false);
194 * This is a convenience routine for acquiring an additional lock on an
195 * already-open relation. Never try to do "relation_open(foo, NoLock)"
196 * and then lock with this.
199 LockRelation(Relation relation, LOCKMODE lockmode)
202 LockAcquireResult res;
204 SET_LOCKTAG_RELATION(tag,
205 relation->rd_lockInfo.lockRelId.dbId,
206 relation->rd_lockInfo.lockRelId.relId);
208 res = LockAcquire(&tag, lockmode, false, false);
211 * Now that we have the lock, check for invalidation messages; see notes
212 * in LockRelationOid.
214 if (res != LOCKACQUIRE_ALREADY_HELD)
215 AcceptInvalidationMessages();
219 * ConditionalLockRelation
221 * This is a convenience routine for acquiring an additional lock on an
222 * already-open relation. Never try to do "relation_open(foo, NoLock)"
223 * and then lock with this.
226 ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
229 LockAcquireResult res;
231 SET_LOCKTAG_RELATION(tag,
232 relation->rd_lockInfo.lockRelId.dbId,
233 relation->rd_lockInfo.lockRelId.relId);
235 res = LockAcquire(&tag, lockmode, false, true);
237 if (res == LOCKACQUIRE_NOT_AVAIL)
241 * Now that we have the lock, check for invalidation messages; see notes
242 * in LockRelationOid.
244 if (res != LOCKACQUIRE_ALREADY_HELD)
245 AcceptInvalidationMessages();
253 * This is a convenience routine for unlocking a relation without also
257 UnlockRelation(Relation relation, LOCKMODE lockmode)
261 SET_LOCKTAG_RELATION(tag,
262 relation->rd_lockInfo.lockRelId.dbId,
263 relation->rd_lockInfo.lockRelId.relId);
265 LockRelease(&tag, lockmode, false);
269 * LockHasWaitersRelation
271 * This is a functiion to check if someone else is waiting on a
272 * lock, we are currently holding.
275 LockHasWaitersRelation(Relation relation, LOCKMODE lockmode)
279 SET_LOCKTAG_RELATION(tag,
280 relation->rd_lockInfo.lockRelId.dbId,
281 relation->rd_lockInfo.lockRelId.relId);
283 return LockHasWaiters(&tag, lockmode, false);
287 * LockRelationIdForSession
289 * This routine grabs a session-level lock on the target relation. The
290 * session lock persists across transaction boundaries. It will be removed
291 * when UnlockRelationIdForSession() is called, or if an ereport(ERROR) occurs,
292 * or if the backend exits.
294 * Note that one should also grab a transaction-level lock on the rel
295 * in any transaction that actually uses the rel, to ensure that the
296 * relcache entry is up to date.
299 LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
303 SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
305 (void) LockAcquire(&tag, lockmode, true, false);
309 * UnlockRelationIdForSession
312 UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
316 SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
318 LockRelease(&tag, lockmode, true);
322 * LockRelationForExtension
324 * This lock tag is used to interlock addition of pages to relations.
325 * We need such locking because bufmgr/smgr definition of P_NEW is not
326 * race-condition-proof.
328 * We assume the caller is already holding some type of regular lock on
329 * the relation, so no AcceptInvalidationMessages call is needed here.
332 LockRelationForExtension(Relation relation, LOCKMODE lockmode)
336 SET_LOCKTAG_RELATION_EXTEND(tag,
337 relation->rd_lockInfo.lockRelId.dbId,
338 relation->rd_lockInfo.lockRelId.relId);
340 (void) LockAcquire(&tag, lockmode, false, false);
344 * UnlockRelationForExtension
347 UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
351 SET_LOCKTAG_RELATION_EXTEND(tag,
352 relation->rd_lockInfo.lockRelId.dbId,
353 relation->rd_lockInfo.lockRelId.relId);
355 LockRelease(&tag, lockmode, false);
361 * Obtain a page-level lock. This is currently used by some index access
362 * methods to lock individual index pages.
365 LockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
369 SET_LOCKTAG_PAGE(tag,
370 relation->rd_lockInfo.lockRelId.dbId,
371 relation->rd_lockInfo.lockRelId.relId,
374 (void) LockAcquire(&tag, lockmode, false, false);
378 * ConditionalLockPage
380 * As above, but only lock if we can get the lock without blocking.
381 * Returns TRUE iff the lock was acquired.
384 ConditionalLockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
388 SET_LOCKTAG_PAGE(tag,
389 relation->rd_lockInfo.lockRelId.dbId,
390 relation->rd_lockInfo.lockRelId.relId,
393 return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
400 UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode)
404 SET_LOCKTAG_PAGE(tag,
405 relation->rd_lockInfo.lockRelId.dbId,
406 relation->rd_lockInfo.lockRelId.relId,
409 LockRelease(&tag, lockmode, false);
415 * Obtain a tuple-level lock. This is used in a less-than-intuitive fashion
416 * because we can't afford to keep a separate lock in shared memory for every
417 * tuple. See heap_lock_tuple before using this!
420 LockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
424 SET_LOCKTAG_TUPLE(tag,
425 relation->rd_lockInfo.lockRelId.dbId,
426 relation->rd_lockInfo.lockRelId.relId,
427 ItemPointerGetBlockNumber(tid),
428 ItemPointerGetOffsetNumber(tid));
430 (void) LockAcquire(&tag, lockmode, false, false);
434 * ConditionalLockTuple
436 * As above, but only lock if we can get the lock without blocking.
437 * Returns TRUE iff the lock was acquired.
440 ConditionalLockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
444 SET_LOCKTAG_TUPLE(tag,
445 relation->rd_lockInfo.lockRelId.dbId,
446 relation->rd_lockInfo.lockRelId.relId,
447 ItemPointerGetBlockNumber(tid),
448 ItemPointerGetOffsetNumber(tid));
450 return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
457 UnlockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
461 SET_LOCKTAG_TUPLE(tag,
462 relation->rd_lockInfo.lockRelId.dbId,
463 relation->rd_lockInfo.lockRelId.relId,
464 ItemPointerGetBlockNumber(tid),
465 ItemPointerGetOffsetNumber(tid));
467 LockRelease(&tag, lockmode, false);
471 * XactLockTableInsert
473 * Insert a lock showing that the given transaction ID is running ---
474 * this is done when an XID is acquired by a transaction or subtransaction.
475 * The lock can then be used to wait for the transaction to finish.
478 XactLockTableInsert(TransactionId xid)
482 SET_LOCKTAG_TRANSACTION(tag, xid);
484 (void) LockAcquire(&tag, ExclusiveLock, false, false);
488 * XactLockTableDelete
490 * Delete the lock showing that the given transaction ID is running.
491 * (This is never used for main transaction IDs; those locks are only
492 * released implicitly at transaction end. But we do use it for subtrans IDs.)
495 XactLockTableDelete(TransactionId xid)
499 SET_LOCKTAG_TRANSACTION(tag, xid);
501 LockRelease(&tag, ExclusiveLock, false);
507 * Wait for the specified transaction to commit or abort. If an operation
508 * is specified, an error context callback is set up. If 'oper' is passed as
509 * None, no error context callback is set up.
511 * Note that this does the right thing for subtransactions: if we wait on a
512 * subtransaction, we will exit as soon as it aborts or its top parent commits.
513 * It takes some extra work to ensure this, because to save on shared memory
514 * the XID lock of a subtransaction is released when it ends, whether
515 * successfully or unsuccessfully. So we have to check if it's "still running"
516 * and if so wait for its parent.
519 XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid,
523 XactLockTableWaitInfo info;
524 ErrorContextCallback callback;
527 * If an operation is specified, set up our verbose error context
530 if (oper != XLTW_None)
532 Assert(RelationIsValid(rel));
533 Assert(ItemPointerIsValid(ctid));
539 callback.callback = XactLockTableWaitErrorCb;
540 callback.arg = &info;
541 callback.previous = error_context_stack;
542 error_context_stack = &callback;
547 Assert(TransactionIdIsValid(xid));
548 Assert(!TransactionIdEquals(xid, GetTopTransactionIdIfAny()));
550 SET_LOCKTAG_TRANSACTION(tag, xid);
552 (void) LockAcquire(&tag, ShareLock, false, false);
554 LockRelease(&tag, ShareLock, false);
556 if (!TransactionIdIsInProgress(xid))
558 xid = SubTransGetParent(xid);
561 if (oper != XLTW_None)
562 error_context_stack = callback.previous;
566 * ConditionalXactLockTableWait
568 * As above, but only lock if we can get the lock without blocking.
569 * Returns TRUE if the lock was acquired.
572 ConditionalXactLockTableWait(TransactionId xid)
578 Assert(TransactionIdIsValid(xid));
579 Assert(!TransactionIdEquals(xid, GetTopTransactionIdIfAny()));
581 SET_LOCKTAG_TRANSACTION(tag, xid);
583 if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
586 LockRelease(&tag, ShareLock, false);
588 if (!TransactionIdIsInProgress(xid))
590 xid = SubTransGetParent(xid);
597 * SpeculativeInsertionLockAcquire
599 * Insert a lock showing that the given transaction ID is inserting a tuple,
600 * but hasn't yet decided whether it's going to keep it. The lock can then be
601 * used to wait for the decision to go ahead with the insertion, or aborting
604 * The token is used to distinguish multiple insertions by the same
605 * transaction. It is returned to caller.
608 SpeculativeInsertionLockAcquire(TransactionId xid)
612 speculativeInsertionToken++;
615 * Check for wrap-around. Zero means no token is held, so don't use that.
617 if (speculativeInsertionToken == 0)
618 speculativeInsertionToken = 1;
620 SET_LOCKTAG_SPECULATIVE_INSERTION(tag, xid, speculativeInsertionToken);
622 (void) LockAcquire(&tag, ExclusiveLock, false, false);
624 return speculativeInsertionToken;
628 * SpeculativeInsertionLockRelease
630 * Delete the lock showing that the given transaction is speculatively
634 SpeculativeInsertionLockRelease(TransactionId xid)
638 SET_LOCKTAG_SPECULATIVE_INSERTION(tag, xid, speculativeInsertionToken);
640 LockRelease(&tag, ExclusiveLock, false);
644 * SpeculativeInsertionWait
646 * Wait for the specified transaction to finish or abort the insertion of a
650 SpeculativeInsertionWait(TransactionId xid, uint32 token)
654 SET_LOCKTAG_SPECULATIVE_INSERTION(tag, xid, token);
656 Assert(TransactionIdIsValid(xid));
659 (void) LockAcquire(&tag, ShareLock, false, false);
660 LockRelease(&tag, ShareLock, false);
664 * XactLockTableWaitErrorContextCb
665 * Error context callback for transaction lock waits.
668 XactLockTableWaitErrorCb(void *arg)
670 XactLockTableWaitInfo *info = (XactLockTableWaitInfo *) arg;
673 * We would like to print schema name too, but that would require a
676 if (info->oper != XLTW_None &&
677 ItemPointerIsValid(info->ctid) && RelationIsValid(info->rel))
684 cxt = gettext_noop("while updating tuple (%u,%u) in relation \"%s\"");
687 cxt = gettext_noop("while deleting tuple (%u,%u) in relation \"%s\"");
690 cxt = gettext_noop("while locking tuple (%u,%u) in relation \"%s\"");
692 case XLTW_LockUpdated:
693 cxt = gettext_noop("while locking updated version (%u,%u) of tuple in relation \"%s\"");
695 case XLTW_InsertIndex:
696 cxt = gettext_noop("while inserting index tuple (%u,%u) in relation \"%s\"");
698 case XLTW_InsertIndexUnique:
699 cxt = gettext_noop("while checking uniqueness of tuple (%u,%u) in relation \"%s\"");
701 case XLTW_FetchUpdated:
702 cxt = gettext_noop("while rechecking updated tuple (%u,%u) in relation \"%s\"");
704 case XLTW_RecheckExclusionConstr:
705 cxt = gettext_noop("while checking exclusion constraint on tuple (%u,%u) in relation \"%s\"");
713 ItemPointerGetBlockNumber(info->ctid),
714 ItemPointerGetOffsetNumber(info->ctid),
715 RelationGetRelationName(info->rel));
720 * WaitForLockersMultiple
721 * Wait until no transaction holds locks that conflict with the given
722 * locktags at the given lockmode.
724 * To do this, obtain the current list of lockers, and wait on their VXIDs
725 * until they are finished.
727 * Note we don't try to acquire the locks on the given locktags, only the VXIDs
728 * of its lock holders; if somebody grabs a conflicting lock on the objects
729 * after we obtained our initial list of lockers, we will not wait for them.
732 WaitForLockersMultiple(List *locktags, LOCKMODE lockmode)
737 /* Done if no locks to wait for */
738 if (list_length(locktags) == 0)
741 /* Collect the transactions we need to wait on */
742 foreach(lc, locktags)
744 LOCKTAG *locktag = lfirst(lc);
746 holders = lappend(holders, GetLockConflicts(locktag, lockmode));
750 * Note: GetLockConflicts() never reports our own xid, hence we need not
751 * check for that. Also, prepared xacts are not reported, which is fine
752 * since they certainly aren't going to do anything anymore.
755 /* Finally wait for each such transaction to complete */
758 VirtualTransactionId *lockholders = lfirst(lc);
760 while (VirtualTransactionIdIsValid(*lockholders))
762 VirtualXactLock(*lockholders, true);
767 list_free_deep(holders);
773 * Same as WaitForLockersMultiple, for a single lock tag.
776 WaitForLockers(LOCKTAG heaplocktag, LOCKMODE lockmode)
780 l = list_make1(&heaplocktag);
781 WaitForLockersMultiple(l, lockmode);
789 * Obtain a lock on a general object of the current database. Don't use
790 * this for shared objects (such as tablespaces). It's unwise to apply it
791 * to relations, also, since a lock taken this way will NOT conflict with
792 * locks taken via LockRelation and friends.
795 LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
800 SET_LOCKTAG_OBJECT(tag,
806 (void) LockAcquire(&tag, lockmode, false, false);
808 /* Make sure syscaches are up-to-date with any changes we waited for */
809 AcceptInvalidationMessages();
813 * UnlockDatabaseObject
816 UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
821 SET_LOCKTAG_OBJECT(tag,
827 LockRelease(&tag, lockmode, false);
833 * Obtain a lock on a shared-across-databases object.
836 LockSharedObject(Oid classid, Oid objid, uint16 objsubid,
841 SET_LOCKTAG_OBJECT(tag,
847 (void) LockAcquire(&tag, lockmode, false, false);
849 /* Make sure syscaches are up-to-date with any changes we waited for */
850 AcceptInvalidationMessages();
857 UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid,
862 SET_LOCKTAG_OBJECT(tag,
868 LockRelease(&tag, lockmode, false);
872 * LockSharedObjectForSession
874 * Obtain a session-level lock on a shared-across-databases object.
875 * See LockRelationIdForSession for notes about session-level locks.
878 LockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid,
883 SET_LOCKTAG_OBJECT(tag,
889 (void) LockAcquire(&tag, lockmode, true, false);
893 * UnlockSharedObjectForSession
896 UnlockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid,
901 SET_LOCKTAG_OBJECT(tag,
907 LockRelease(&tag, lockmode, true);
912 * Append a description of a lockable object to buf.
914 * Ideally we would print names for the numeric values, but that requires
915 * getting locks on system tables, which might cause problems since this is
916 * typically used to report deadlock situations.
919 DescribeLockTag(StringInfo buf, const LOCKTAG *tag)
921 switch ((LockTagType) tag->locktag_type)
923 case LOCKTAG_RELATION:
924 appendStringInfo(buf,
925 _("relation %u of database %u"),
927 tag->locktag_field1);
929 case LOCKTAG_RELATION_EXTEND:
930 appendStringInfo(buf,
931 _("extension of relation %u of database %u"),
933 tag->locktag_field1);
936 appendStringInfo(buf,
937 _("page %u of relation %u of database %u"),
940 tag->locktag_field1);
943 appendStringInfo(buf,
944 _("tuple (%u,%u) of relation %u of database %u"),
948 tag->locktag_field1);
950 case LOCKTAG_TRANSACTION:
951 appendStringInfo(buf,
953 tag->locktag_field1);
955 case LOCKTAG_VIRTUALTRANSACTION:
956 appendStringInfo(buf,
957 _("virtual transaction %d/%u"),
959 tag->locktag_field2);
961 case LOCKTAG_SPECULATIVE_TOKEN:
962 appendStringInfo(buf,
963 _("speculative token %u of transaction %u"),
965 tag->locktag_field1);
968 appendStringInfo(buf,
969 _("object %u of class %u of database %u"),
972 tag->locktag_field1);
974 case LOCKTAG_USERLOCK:
975 /* reserved for old contrib code, now on pgfoundry */
976 appendStringInfo(buf,
977 _("user lock [%u,%u,%u]"),
980 tag->locktag_field3);
982 case LOCKTAG_ADVISORY:
983 appendStringInfo(buf,
984 _("advisory lock [%u,%u,%u,%u]"),
988 tag->locktag_field4);
991 appendStringInfo(buf,
992 _("unrecognized locktag type %d"),
993 (int) tag->locktag_type);