PostgreSQL Source Code  git master
lmgr.h File Reference
#include "lib/stringinfo.h"
#include "storage/itemptr.h"
#include "storage/lock.h"
#include "utils/rel.h"
Include dependency graph for lmgr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef enum XLTW_Oper XLTW_Oper
 

Enumerations

enum  XLTW_Oper {
  XLTW_None, XLTW_Update, XLTW_Delete, XLTW_Lock,
  XLTW_LockUpdated, XLTW_InsertIndex, XLTW_InsertIndexUnique, XLTW_FetchUpdated,
  XLTW_RecheckExclusionConstr
}
 

Functions

void RelationInitLockInfo (Relation relation)
 
void LockRelationOid (Oid relid, LOCKMODE lockmode)
 
bool ConditionalLockRelationOid (Oid relid, LOCKMODE lockmode)
 
void UnlockRelationId (LockRelId *relid, LOCKMODE lockmode)
 
void UnlockRelationOid (Oid relid, LOCKMODE lockmode)
 
void LockRelation (Relation relation, LOCKMODE lockmode)
 
bool ConditionalLockRelation (Relation relation, LOCKMODE lockmode)
 
void UnlockRelation (Relation relation, LOCKMODE lockmode)
 
bool LockHasWaitersRelation (Relation relation, LOCKMODE lockmode)
 
void LockRelationIdForSession (LockRelId *relid, LOCKMODE lockmode)
 
void UnlockRelationIdForSession (LockRelId *relid, LOCKMODE lockmode)
 
void LockRelationForExtension (Relation relation, LOCKMODE lockmode)
 
void UnlockRelationForExtension (Relation relation, LOCKMODE lockmode)
 
bool ConditionalLockRelationForExtension (Relation relation, LOCKMODE lockmode)
 
int RelationExtensionLockWaiterCount (Relation relation)
 
void LockPage (Relation relation, BlockNumber blkno, LOCKMODE lockmode)
 
bool ConditionalLockPage (Relation relation, BlockNumber blkno, LOCKMODE lockmode)
 
void UnlockPage (Relation relation, BlockNumber blkno, LOCKMODE lockmode)
 
void LockTuple (Relation relation, ItemPointer tid, LOCKMODE lockmode)
 
bool ConditionalLockTuple (Relation relation, ItemPointer tid, LOCKMODE lockmode)
 
void UnlockTuple (Relation relation, ItemPointer tid, LOCKMODE lockmode)
 
void XactLockTableInsert (TransactionId xid)
 
void XactLockTableDelete (TransactionId xid)
 
void XactLockTableWait (TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
 
bool ConditionalXactLockTableWait (TransactionId xid)
 
void WaitForLockers (LOCKTAG heaplocktag, LOCKMODE lockmode)
 
void WaitForLockersMultiple (List *locktags, LOCKMODE lockmode)
 
uint32 SpeculativeInsertionLockAcquire (TransactionId xid)
 
void SpeculativeInsertionLockRelease (TransactionId xid)
 
void SpeculativeInsertionWait (TransactionId xid, uint32 token)
 
void LockDatabaseObject (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
void UnlockDatabaseObject (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
void LockSharedObject (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
void UnlockSharedObject (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
void LockSharedObjectForSession (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
void UnlockSharedObjectForSession (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
void DescribeLockTag (StringInfo buf, const LOCKTAG *tag)
 
const char * GetLockNameFromTagType (uint16 locktag_type)
 

Typedef Documentation

◆ XLTW_Oper

Enumeration Type Documentation

◆ XLTW_Oper

enum XLTW_Oper
Enumerator
XLTW_None 
XLTW_Update 
XLTW_Delete 
XLTW_Lock 
XLTW_LockUpdated 
XLTW_InsertIndex 
XLTW_InsertIndexUnique 
XLTW_FetchUpdated 
XLTW_RecheckExclusionConstr 

Definition at line 24 of file lmgr.h.

Function Documentation

◆ ConditionalLockPage()

bool ConditionalLockPage ( Relation  relation,
BlockNumber  blkno,
LOCKMODE  lockmode 
)

Definition at line 419 of file lmgr.c.

References LockRelId::dbId, LockAcquire(), LOCKACQUIRE_NOT_AVAIL, LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_PAGE.

Referenced by ginInsertCleanup().

420 {
421  LOCKTAG tag;
422 
423  SET_LOCKTAG_PAGE(tag,
424  relation->rd_lockInfo.lockRelId.dbId,
425  relation->rd_lockInfo.lockRelId.relId,
426  blkno);
427 
428  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
429 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:209
Oid relId
Definition: rel.h:38

◆ ConditionalLockRelation()

bool ConditionalLockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 226 of file lmgr.c.

References AcceptInvalidationMessages(), LockRelId::dbId, LockAcquire(), LOCKACQUIRE_ALREADY_HELD, LOCKACQUIRE_NOT_AVAIL, LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by lazy_truncate_heap().

227 {
228  LOCKTAG tag;
229  LockAcquireResult res;
230 
232  relation->rd_lockInfo.lockRelId.dbId,
233  relation->rd_lockInfo.lockRelId.relId);
234 
235  res = LockAcquire(&tag, lockmode, false, true);
236 
237  if (res == LOCKACQUIRE_NOT_AVAIL)
238  return false;
239 
240  /*
241  * Now that we have the lock, check for invalidation messages; see notes
242  * in LockRelationOid.
243  */
244  if (res != LOCKACQUIRE_ALREADY_HELD)
246 
247  return true;
248 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
void AcceptInvalidationMessages(void)
Definition: inval.c:679
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
LockInfoData rd_lockInfo
Definition: rel.h:117
LockAcquireResult
Definition: lock.h:471
Oid relId
Definition: rel.h:38

◆ ConditionalLockRelationForExtension()

bool ConditionalLockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 350 of file lmgr.c.

References LockRelId::dbId, LockAcquire(), LOCKACQUIRE_NOT_AVAIL, LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION_EXTEND.

Referenced by RelationGetBufferForTuple().

351 {
352  LOCKTAG tag;
353 
355  relation->rd_lockInfo.lockRelId.dbId,
356  relation->rd_lockInfo.lockRelId.relId);
357 
358  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
359 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:201
Oid relId
Definition: rel.h:38

◆ ConditionalLockRelationOid()

bool ConditionalLockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 138 of file lmgr.c.

References AcceptInvalidationMessages(), LockAcquire(), LOCKACQUIRE_ALREADY_HELD, LOCKACQUIRE_NOT_AVAIL, and SetLocktagRelationOid().

Referenced by AlterTableMoveAll(), analyze_rel(), do_autovacuum(), LockTableRecurse(), RangeVarGetRelidExtended(), and vacuum_rel().

139 {
140  LOCKTAG tag;
141  LockAcquireResult res;
142 
143  SetLocktagRelationOid(&tag, relid);
144 
145  res = LockAcquire(&tag, lockmode, false, true);
146 
147  if (res == LOCKACQUIRE_NOT_AVAIL)
148  return false;
149 
150  /*
151  * Now that we have the lock, check for invalidation messages; see notes
152  * in LockRelationOid.
153  */
154  if (res != LOCKACQUIRE_ALREADY_HELD)
156 
157  return true;
158 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
void AcceptInvalidationMessages(void)
Definition: inval.c:679
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:86
Definition: lock.h:178
LockAcquireResult
Definition: lock.h:471

◆ ConditionalLockTuple()

bool ConditionalLockTuple ( Relation  relation,
ItemPointer  tid,
LOCKMODE  lockmode 
)

Definition at line 475 of file lmgr.c.

References LockRelId::dbId, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LockAcquire(), LOCKACQUIRE_NOT_AVAIL, LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_TUPLE.

476 {
477  LOCKTAG tag;
478 
479  SET_LOCKTAG_TUPLE(tag,
480  relation->rd_lockInfo.lockRelId.dbId,
481  relation->rd_lockInfo.lockRelId.relId,
484 
485  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
486 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:95
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
Oid relId
Definition: rel.h:38

◆ ConditionalXactLockTableWait()

bool ConditionalXactLockTableWait ( TransactionId  xid)

Definition at line 627 of file lmgr.c.

References Assert, GetTopTransactionIdIfAny(), LockAcquire(), LOCKACQUIRE_NOT_AVAIL, LockRelease(), pg_usleep(), SET_LOCKTAG_TRANSACTION, ShareLock, SubTransGetTopmostTransaction(), TransactionIdEquals, TransactionIdIsInProgress(), and TransactionIdIsValid.

Referenced by Do_MultiXactIdWait(), EvalPlanQualFetch(), and heap_lock_tuple().

628 {
629  LOCKTAG tag;
630  bool first = true;
631 
632  for (;;)
633  {
636 
637  SET_LOCKTAG_TRANSACTION(tag, xid);
638 
639  if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
640  return false;
641 
642  LockRelease(&tag, ShareLock, false);
643 
644  if (!TransactionIdIsInProgress(xid))
645  break;
646 
647  /* See XactLockTableWait about this case */
648  if (!first)
649  pg_usleep(1000L);
650  first = false;
652  }
653 
654  return true;
655 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:150
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:999
Definition: lock.h:178
void pg_usleep(long microsec)
Definition: signal.c:53
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:405
#define Assert(condition)
Definition: c.h:680
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:225
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ DescribeLockTag()

void DescribeLockTag ( StringInfo  buf,
const LOCKTAG tag 
)

Definition at line 980 of file lmgr.c.

References _, appendStringInfo(), LOCKTAG_ADVISORY, LOCKTAG::locktag_field1, LOCKTAG::locktag_field2, LOCKTAG::locktag_field3, LOCKTAG::locktag_field4, LOCKTAG_OBJECT, LOCKTAG_PAGE, LOCKTAG_RELATION, LOCKTAG_RELATION_EXTEND, LOCKTAG_SPECULATIVE_TOKEN, LOCKTAG_TRANSACTION, LOCKTAG_TUPLE, LOCKTAG::locktag_type, LOCKTAG_USERLOCK, and LOCKTAG_VIRTUALTRANSACTION.

Referenced by DeadLockReport(), and ProcSleep().

981 {
982  switch ((LockTagType) tag->locktag_type)
983  {
984  case LOCKTAG_RELATION:
985  appendStringInfo(buf,
986  _("relation %u of database %u"),
987  tag->locktag_field2,
988  tag->locktag_field1);
989  break;
991  appendStringInfo(buf,
992  _("extension of relation %u of database %u"),
993  tag->locktag_field2,
994  tag->locktag_field1);
995  break;
996  case LOCKTAG_PAGE:
997  appendStringInfo(buf,
998  _("page %u of relation %u of database %u"),
999  tag->locktag_field3,
1000  tag->locktag_field2,
1001  tag->locktag_field1);
1002  break;
1003  case LOCKTAG_TUPLE:
1004  appendStringInfo(buf,
1005  _("tuple (%u,%u) of relation %u of database %u"),
1006  tag->locktag_field3,
1007  tag->locktag_field4,
1008  tag->locktag_field2,
1009  tag->locktag_field1);
1010  break;
1011  case LOCKTAG_TRANSACTION:
1012  appendStringInfo(buf,
1013  _("transaction %u"),
1014  tag->locktag_field1);
1015  break;
1017  appendStringInfo(buf,
1018  _("virtual transaction %d/%u"),
1019  tag->locktag_field1,
1020  tag->locktag_field2);
1021  break;
1023  appendStringInfo(buf,
1024  _("speculative token %u of transaction %u"),
1025  tag->locktag_field2,
1026  tag->locktag_field1);
1027  break;
1028  case LOCKTAG_OBJECT:
1029  appendStringInfo(buf,
1030  _("object %u of class %u of database %u"),
1031  tag->locktag_field3,
1032  tag->locktag_field2,
1033  tag->locktag_field1);
1034  break;
1035  case LOCKTAG_USERLOCK:
1036  /* reserved for old contrib code, now on pgfoundry */
1037  appendStringInfo(buf,
1038  _("user lock [%u,%u,%u]"),
1039  tag->locktag_field1,
1040  tag->locktag_field2,
1041  tag->locktag_field3);
1042  break;
1043  case LOCKTAG_ADVISORY:
1044  appendStringInfo(buf,
1045  _("advisory lock [%u,%u,%u,%u]"),
1046  tag->locktag_field1,
1047  tag->locktag_field2,
1048  tag->locktag_field3,
1049  tag->locktag_field4);
1050  break;
1051  default:
1052  appendStringInfo(buf,
1053  _("unrecognized locktag type %d"),
1054  (int) tag->locktag_type);
1055  break;
1056  }
1057 }
uint16 locktag_field4
Definition: lock.h:183
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
uint32 locktag_field2
Definition: lock.h:181
uint8 locktag_type
Definition: lock.h:184
LockTagType
Definition: lock.h:138
uint32 locktag_field1
Definition: lock.h:180
#define _(x)
Definition: elog.c:84
uint32 locktag_field3
Definition: lock.h:182

◆ GetLockNameFromTagType()

const char* GetLockNameFromTagType ( uint16  locktag_type)

Definition at line 1065 of file lmgr.c.

References LOCKTAG_LAST_TYPE, and LockTagTypeNames.

Referenced by pgstat_get_wait_event().

1066 {
1067  if (locktag_type > LOCKTAG_LAST_TYPE)
1068  return "???";
1069  return LockTagTypeNames[locktag_type];
1070 }
#define LOCKTAG_LAST_TYPE
Definition: lock.h:166
const char *const LockTagTypeNames[]
Definition: lockfuncs.c:26

◆ LockDatabaseObject()

void LockDatabaseObject ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 856 of file lmgr.c.

References AcceptInvalidationMessages(), LockAcquire(), MyDatabaseId, and SET_LOCKTAG_OBJECT.

Referenced by AcquireDeletionLock(), AddEnumLabel(), get_object_address(), RangeVarGetAndCheckCreationNamespace(), and RenameEnumLabel().

858 {
859  LOCKTAG tag;
860 
861  SET_LOCKTAG_OBJECT(tag,
862  MyDatabaseId,
863  classid,
864  objid,
865  objsubid);
866 
867  (void) LockAcquire(&tag, lockmode, false, false);
868 
869  /* Make sure syscaches are up-to-date with any changes we waited for */
871 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
void AcceptInvalidationMessages(void)
Definition: inval.c:679
Definition: lock.h:178
Oid MyDatabaseId
Definition: globals.c:77
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:249

◆ LockHasWaitersRelation()

bool LockHasWaitersRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 275 of file lmgr.c.

References LockRelId::dbId, LockHasWaiters(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by count_nondeletable_pages().

276 {
277  LOCKTAG tag;
278 
280  relation->rd_lockInfo.lockRelId.dbId,
281  relation->rd_lockInfo.lockRelId.relId);
282 
283  return LockHasWaiters(&tag, lockmode, false);
284 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
LockInfoData rd_lockInfo
Definition: rel.h:117
bool LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:571
Oid relId
Definition: rel.h:38

◆ LockPage()

void LockPage ( Relation  relation,
BlockNumber  blkno,
LOCKMODE  lockmode 
)

Definition at line 400 of file lmgr.c.

References LockRelId::dbId, LockAcquire(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_PAGE.

Referenced by ginInsertCleanup().

401 {
402  LOCKTAG tag;
403 
404  SET_LOCKTAG_PAGE(tag,
405  relation->rd_lockInfo.lockRelId.dbId,
406  relation->rd_lockInfo.lockRelId.relId,
407  blkno);
408 
409  (void) LockAcquire(&tag, lockmode, false, false);
410 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:209
Oid relId
Definition: rel.h:38

◆ LockRelation()

void LockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 199 of file lmgr.c.

References AcceptInvalidationMessages(), LockRelId::dbId, LockAcquire(), LOCKACQUIRE_ALREADY_HELD, LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by index_create().

200 {
201  LOCKTAG tag;
202  LockAcquireResult res;
203 
205  relation->rd_lockInfo.lockRelId.dbId,
206  relation->rd_lockInfo.lockRelId.relId);
207 
208  res = LockAcquire(&tag, lockmode, false, false);
209 
210  /*
211  * Now that we have the lock, check for invalidation messages; see notes
212  * in LockRelationOid.
213  */
214  if (res != LOCKACQUIRE_ALREADY_HELD)
216 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
void AcceptInvalidationMessages(void)
Definition: inval.c:679
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
LockInfoData rd_lockInfo
Definition: rel.h:117
LockAcquireResult
Definition: lock.h:471
Oid relId
Definition: rel.h:38

◆ LockRelationForExtension()

void LockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 332 of file lmgr.c.

References LockRelId::dbId, LockAcquire(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION_EXTEND.

Referenced by _bt_getbuf(), BloomNewBuffer(), brin_getinsertbuffer(), brin_page_cleanup(), btvacuumscan(), fsm_extend(), GinNewBuffer(), ginvacuumcleanup(), gistNewBuffer(), gistvacuumcleanup(), lazy_scan_heap(), pgstat_index(), RelationGetBufferForTuple(), revmap_physical_extend(), SpGistNewBuffer(), spgvacuumscan(), and vm_extend().

333 {
334  LOCKTAG tag;
335 
337  relation->rd_lockInfo.lockRelId.dbId,
338  relation->rd_lockInfo.lockRelId.relId);
339 
340  (void) LockAcquire(&tag, lockmode, false, false);
341 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:201
Oid relId
Definition: rel.h:38

◆ LockRelationIdForSession()

void LockRelationIdForSession ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 299 of file lmgr.c.

References LockRelId::dbId, LockAcquire(), LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by DefineIndex(), index_drop(), and vacuum_rel().

300 {
301  LOCKTAG tag;
302 
303  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
304 
305  (void) LockAcquire(&tag, lockmode, true, false);
306 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
Oid relId
Definition: rel.h:38

◆ LockRelationOid()

void LockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 105 of file lmgr.c.

References AcceptInvalidationMessages(), LockAcquire(), LOCKACQUIRE_ALREADY_HELD, and SetLocktagRelationOid().

Referenced by AcquireDeletionLock(), AcquireExecutorLocks(), AlterTableMoveAll(), ATExecAttachPartition(), ATExecDetachPartition(), copy_heap_data(), CreateTrigger(), ExecLockNonLeafAppendTables(), ExecRefreshMatView(), find_inheritance_children(), heap_drop_with_catalog(), InitCatCachePhase2(), InitPlan(), load_critical_index(), lock_and_open_sequence(), LockTableRecurse(), pg_replication_origin_advance(), RangeVarCallbackForDropRelation(), RangeVarCallbackForReindexIndex(), RangeVarGetAndCheckCreationNamespace(), RangeVarGetRelidExtended(), relation_open(), ScanQueryForLocks(), and try_relation_open().

106 {
107  LOCKTAG tag;
108  LockAcquireResult res;
109 
110  SetLocktagRelationOid(&tag, relid);
111 
112  res = LockAcquire(&tag, lockmode, false, false);
113 
114  /*
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.)
123  */
124  if (res != LOCKACQUIRE_ALREADY_HELD)
126 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
void AcceptInvalidationMessages(void)
Definition: inval.c:679
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:86
Definition: lock.h:178
LockAcquireResult
Definition: lock.h:471

◆ LockSharedObject()

void LockSharedObject ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 897 of file lmgr.c.

References AcceptInvalidationMessages(), InvalidOid, LockAcquire(), and SET_LOCKTAG_OBJECT.

Referenced by AlterSubscription(), DropRole(), DropSubscription(), get_db_info(), get_object_address(), InitPostgres(), PreCommit_Notify(), SetSubscriptionRelState(), and shdepLockAndCheckObject().

899 {
900  LOCKTAG tag;
901 
902  SET_LOCKTAG_OBJECT(tag,
903  InvalidOid,
904  classid,
905  objid,
906  objsubid);
907 
908  (void) LockAcquire(&tag, lockmode, false, false);
909 
910  /* Make sure syscaches are up-to-date with any changes we waited for */
912 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
void AcceptInvalidationMessages(void)
Definition: inval.c:679
Definition: lock.h:178
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:249

◆ LockSharedObjectForSession()

void LockSharedObjectForSession ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 939 of file lmgr.c.

References InvalidOid, LockAcquire(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

941 {
942  LOCKTAG tag;
943 
944  SET_LOCKTAG_OBJECT(tag,
945  InvalidOid,
946  classid,
947  objid,
948  objsubid);
949 
950  (void) LockAcquire(&tag, lockmode, true, false);
951 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Definition: lock.h:178
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:249

◆ LockTuple()

void LockTuple ( Relation  relation,
ItemPointer  tid,
LOCKMODE  lockmode 
)

Definition at line 455 of file lmgr.c.

References LockRelId::dbId, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LockAcquire(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_TUPLE.

456 {
457  LOCKTAG tag;
458 
459  SET_LOCKTAG_TUPLE(tag,
460  relation->rd_lockInfo.lockRelId.dbId,
461  relation->rd_lockInfo.lockRelId.relId,
464 
465  (void) LockAcquire(&tag, lockmode, false, false);
466 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:95
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
Oid relId
Definition: rel.h:38

◆ RelationExtensionLockWaiterCount()

int RelationExtensionLockWaiterCount ( Relation  relation)

Definition at line 367 of file lmgr.c.

References LockRelId::dbId, LockInfoData::lockRelId, LockWaiterCount(), RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION_EXTEND.

Referenced by RelationAddExtraBlocks().

368 {
369  LOCKTAG tag;
370 
372  relation->rd_lockInfo.lockRelId.dbId,
373  relation->rd_lockInfo.lockRelId.relId);
374 
375  return LockWaiterCount(&tag);
376 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
int LockWaiterCount(const LOCKTAG *locktag)
Definition: lock.c:4387
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:201
Oid relId
Definition: rel.h:38

◆ RelationInitLockInfo()

void RelationInitLockInfo ( Relation  relation)

Definition at line 68 of file lmgr.c.

References Assert, LockRelId::dbId, InvalidOid, LockInfoData::lockRelId, MyDatabaseId, OidIsValid, RelationData::rd_lockInfo, RelationData::rd_rel, RelationGetRelid, RelationIsValid, and LockRelId::relId.

Referenced by formrdesc(), load_relcache_init_file(), RelationBuildDesc(), and RelationBuildLocalRelation().

69 {
70  Assert(RelationIsValid(relation));
72 
73  relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
74 
75  if (relation->rd_rel->relisshared)
77  else
79 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Form_pg_class rd_rel
Definition: rel.h:114
#define OidIsValid(objectId)
Definition: c.h:586
#define RelationIsValid(relation)
Definition: rel.h:398
LockInfoData rd_lockInfo
Definition: rel.h:117
Oid MyDatabaseId
Definition: globals.c:77
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:680
#define RelationGetRelid(relation)
Definition: rel.h:425
Oid relId
Definition: rel.h:38

◆ SpeculativeInsertionLockAcquire()

uint32 SpeculativeInsertionLockAcquire ( TransactionId  xid)

Definition at line 669 of file lmgr.c.

References ExclusiveLock, LockAcquire(), SET_LOCKTAG_SPECULATIVE_INSERTION, and speculativeInsertionToken.

Referenced by ExecInsert().

670 {
671  LOCKTAG tag;
672 
674 
675  /*
676  * Check for wrap-around. Zero means no token is held, so don't use that.
677  */
678  if (speculativeInsertionToken == 0)
680 
682 
683  (void) LockAcquire(&tag, ExclusiveLock, false, false);
684 
686 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:178
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:241
static uint32 speculativeInsertionToken
Definition: lmgr.c:43

◆ SpeculativeInsertionLockRelease()

void SpeculativeInsertionLockRelease ( TransactionId  xid)

Definition at line 695 of file lmgr.c.

References ExclusiveLock, LockRelease(), SET_LOCKTAG_SPECULATIVE_INSERTION, and speculativeInsertionToken.

Referenced by ExecInsert().

696 {
697  LOCKTAG tag;
698 
700 
701  LockRelease(&tag, ExclusiveLock, false);
702 }
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:178
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:241
static uint32 speculativeInsertionToken
Definition: lmgr.c:43
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818

◆ SpeculativeInsertionWait()

void SpeculativeInsertionWait ( TransactionId  xid,
uint32  token 
)

Definition at line 711 of file lmgr.c.

References Assert, LockAcquire(), LockRelease(), SET_LOCKTAG_SPECULATIVE_INSERTION, ShareLock, and TransactionIdIsValid.

Referenced by _bt_doinsert(), and check_exclusion_or_unique_constraint().

712 {
713  LOCKTAG tag;
714 
715  SET_LOCKTAG_SPECULATIVE_INSERTION(tag, xid, token);
716 
718  Assert(token != 0);
719 
720  (void) LockAcquire(&tag, ShareLock, false, false);
721  LockRelease(&tag, ShareLock, false);
722 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
Definition: lock.h:178
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:241
#define Assert(condition)
Definition: c.h:680
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ UnlockDatabaseObject()

void UnlockDatabaseObject ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 877 of file lmgr.c.

References LockRelease(), MyDatabaseId, and SET_LOCKTAG_OBJECT.

Referenced by get_object_address(), RangeVarGetAndCheckCreationNamespace(), and ReleaseDeletionLock().

879 {
880  LOCKTAG tag;
881 
882  SET_LOCKTAG_OBJECT(tag,
883  MyDatabaseId,
884  classid,
885  objid,
886  objsubid);
887 
888  LockRelease(&tag, lockmode, false);
889 }
Definition: lock.h:178
Oid MyDatabaseId
Definition: globals.c:77
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:249
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818

◆ UnlockPage()

void UnlockPage ( Relation  relation,
BlockNumber  blkno,
LOCKMODE  lockmode 
)

Definition at line 435 of file lmgr.c.

References LockRelId::dbId, LockRelease(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_PAGE.

Referenced by ginInsertCleanup().

436 {
437  LOCKTAG tag;
438 
439  SET_LOCKTAG_PAGE(tag,
440  relation->rd_lockInfo.lockRelId.dbId,
441  relation->rd_lockInfo.lockRelId.relId,
442  blkno);
443 
444  LockRelease(&tag, lockmode, false);
445 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:209
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
Oid relId
Definition: rel.h:38

◆ UnlockRelation()

void UnlockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 257 of file lmgr.c.

References LockRelId::dbId, LockRelease(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by lazy_truncate_heap().

258 {
259  LOCKTAG tag;
260 
262  relation->rd_lockInfo.lockRelId.dbId,
263  relation->rd_lockInfo.lockRelId.relId);
264 
265  LockRelease(&tag, lockmode, false);
266 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
LockInfoData rd_lockInfo
Definition: rel.h:117
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
Oid relId
Definition: rel.h:38

◆ UnlockRelationForExtension()

void UnlockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 382 of file lmgr.c.

References LockRelId::dbId, LockRelease(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_RELATION_EXTEND.

Referenced by _bt_getbuf(), BloomNewBuffer(), brin_getinsertbuffer(), brin_page_cleanup(), btvacuumscan(), fsm_extend(), GinNewBuffer(), ginvacuumcleanup(), gistNewBuffer(), gistvacuumcleanup(), lazy_scan_heap(), pgstat_index(), RelationGetBufferForTuple(), revmap_physical_extend(), SpGistNewBuffer(), spgvacuumscan(), and vm_extend().

383 {
384  LOCKTAG tag;
385 
387  relation->rd_lockInfo.lockRelId.dbId,
388  relation->rd_lockInfo.lockRelId.relId);
389 
390  LockRelease(&tag, lockmode, false);
391 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:201
Oid relId
Definition: rel.h:38

◆ UnlockRelationId()

void UnlockRelationId ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 167 of file lmgr.c.

References LockRelId::dbId, LockRelease(), LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by index_close(), and relation_close().

168 {
169  LOCKTAG tag;
170 
171  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
172 
173  LockRelease(&tag, lockmode, false);
174 }
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
Oid relId
Definition: rel.h:38

◆ UnlockRelationIdForSession()

void UnlockRelationIdForSession ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 312 of file lmgr.c.

References LockRelId::dbId, LockRelease(), LockRelId::relId, and SET_LOCKTAG_RELATION.

Referenced by DefineIndex(), index_drop(), and vacuum_rel().

313 {
314  LOCKTAG tag;
315 
316  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
317 
318  LockRelease(&tag, lockmode, true);
319 }
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:193
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
Oid relId
Definition: rel.h:38

◆ UnlockRelationOid()

void UnlockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 182 of file lmgr.c.

References LockRelease(), and SetLocktagRelationOid().

Referenced by AcquireExecutorLocks(), do_autovacuum(), expand_vacuum_rel(), find_inheritance_children(), InitCatCachePhase2(), load_critical_index(), LockTableRecurse(), pg_replication_origin_advance(), RangeVarCallbackForDropRelation(), RangeVarCallbackForReindexIndex(), RangeVarGetAndCheckCreationNamespace(), RangeVarGetRelidExtended(), ReleaseDeletionLock(), ScanQueryForLocks(), and try_relation_open().

183 {
184  LOCKTAG tag;
185 
186  SetLocktagRelationOid(&tag, relid);
187 
188  LockRelease(&tag, lockmode, false);
189 }
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:86
Definition: lock.h:178
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818

◆ UnlockSharedObject()

void UnlockSharedObject ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 918 of file lmgr.c.

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

Referenced by AlterDatabaseSet(), createdb_failure_callback(), get_db_info(), and get_object_address().

920 {
921  LOCKTAG tag;
922 
923  SET_LOCKTAG_OBJECT(tag,
924  InvalidOid,
925  classid,
926  objid,
927  objsubid);
928 
929  LockRelease(&tag, lockmode, false);
930 }
Definition: lock.h:178
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:249
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818

◆ UnlockSharedObjectForSession()

void UnlockSharedObjectForSession ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 957 of file lmgr.c.

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

959 {
960  LOCKTAG tag;
961 
962  SET_LOCKTAG_OBJECT(tag,
963  InvalidOid,
964  classid,
965  objid,
966  objsubid);
967 
968  LockRelease(&tag, lockmode, true);
969 }
Definition: lock.h:178
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:249
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818

◆ UnlockTuple()

void UnlockTuple ( Relation  relation,
ItemPointer  tid,
LOCKMODE  lockmode 
)

Definition at line 492 of file lmgr.c.

References LockRelId::dbId, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LockRelease(), LockInfoData::lockRelId, RelationData::rd_lockInfo, LockRelId::relId, and SET_LOCKTAG_TUPLE.

493 {
494  LOCKTAG tag;
495 
496  SET_LOCKTAG_TUPLE(tag,
497  relation->rd_lockInfo.lockRelId.dbId,
498  relation->rd_lockInfo.lockRelId.relId,
501 
502  LockRelease(&tag, lockmode, false);
503 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:178
LockInfoData rd_lockInfo
Definition: rel.h:117
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:95
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
Oid relId
Definition: rel.h:38

◆ WaitForLockers()

void WaitForLockers ( LOCKTAG  heaplocktag,
LOCKMODE  lockmode 
)

Definition at line 837 of file lmgr.c.

References list_free(), list_make1, and WaitForLockersMultiple().

Referenced by DefineIndex(), and index_drop().

838 {
839  List *l;
840 
841  l = list_make1(&heaplocktag);
842  WaitForLockersMultiple(l, lockmode);
843  list_free(l);
844 }
#define list_make1(x1)
Definition: pg_list.h:139
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45
void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode)
Definition: lmgr.c:793

◆ WaitForLockersMultiple()

void WaitForLockersMultiple ( List locktags,
LOCKMODE  lockmode 
)

Definition at line 793 of file lmgr.c.

References GetLockConflicts(), lappend(), lfirst, list_free_deep(), list_length(), NIL, VirtualTransactionIdIsValid, and VirtualXactLock().

Referenced by WaitForLockers().

794 {
795  List *holders = NIL;
796  ListCell *lc;
797 
798  /* Done if no locks to wait for */
799  if (list_length(locktags) == 0)
800  return;
801 
802  /* Collect the transactions we need to wait on */
803  foreach(lc, locktags)
804  {
805  LOCKTAG *locktag = lfirst(lc);
806 
807  holders = lappend(holders, GetLockConflicts(locktag, lockmode));
808  }
809 
810  /*
811  * Note: GetLockConflicts() never reports our own xid, hence we need not
812  * check for that. Also, prepared xacts are not reported, which is fine
813  * since they certainly aren't going to do anything anymore.
814  */
815 
816  /* Finally wait for each such transaction to complete */
817  foreach(lc, holders)
818  {
819  VirtualTransactionId *lockholders = lfirst(lc);
820 
821  while (VirtualTransactionIdIsValid(*lockholders))
822  {
823  VirtualXactLock(*lockholders, true);
824  lockholders++;
825  }
826  }
827 
828  list_free_deep(holders);
829 }
#define NIL
Definition: pg_list.h:69
Definition: lock.h:178
void list_free_deep(List *list)
Definition: list.c:1147
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4291
List * lappend(List *list, void *datum)
Definition: list.c:128
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:71
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode)
Definition: lock.c:2745
Definition: pg_list.h:45

◆ XactLockTableDelete()

void XactLockTableDelete ( TransactionId  xid)

Definition at line 530 of file lmgr.c.

References ExclusiveLock, LockRelease(), and SET_LOCKTAG_TRANSACTION.

Referenced by CommitSubTransaction().

531 {
532  LOCKTAG tag;
533 
534  SET_LOCKTAG_TRANSACTION(tag, xid);
535 
536  LockRelease(&tag, ExclusiveLock, false);
537 }
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:178
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:225
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818

◆ XactLockTableInsert()

void XactLockTableInsert ( TransactionId  xid)

Definition at line 513 of file lmgr.c.

References ExclusiveLock, LockAcquire(), and SET_LOCKTAG_TRANSACTION.

Referenced by AssignTransactionId().

514 {
515  LOCKTAG tag;
516 
517  SET_LOCKTAG_TRANSACTION(tag, xid);
518 
519  (void) LockAcquire(&tag, ExclusiveLock, false, false);
520 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:178
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:225

◆ XactLockTableWait()

void XactLockTableWait ( TransactionId  xid,
Relation  rel,
ItemPointer  ctid,
XLTW_Oper  oper 
)

Definition at line 554 of file lmgr.c.

References ErrorContextCallback::arg, Assert, callback(), ErrorContextCallback::callback, XactLockTableWaitInfo::ctid, error_context_stack, GetTopTransactionIdIfAny(), ItemPointerIsValid, LockAcquire(), LockRelease(), XactLockTableWaitInfo::oper, pg_usleep(), ErrorContextCallback::previous, XactLockTableWaitInfo::rel, RelationIsValid, SET_LOCKTAG_TRANSACTION, ShareLock, SubTransGetTopmostTransaction(), TransactionIdEquals, TransactionIdIsInProgress(), TransactionIdIsValid, XactLockTableWaitErrorCb(), and XLTW_None.

Referenced by _bt_doinsert(), check_exclusion_or_unique_constraint(), Do_MultiXactIdWait(), EvalPlanQualFetch(), heap_delete(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_update(), IndexBuildHeapRangeScan(), RelationFindReplTupleByIndex(), RelationFindReplTupleSeq(), and SnapBuildWaitSnapshot().

556 {
557  LOCKTAG tag;
560  bool first = true;
561 
562  /*
563  * If an operation is specified, set up our verbose error context
564  * callback.
565  */
566  if (oper != XLTW_None)
567  {
568  Assert(RelationIsValid(rel));
569  Assert(ItemPointerIsValid(ctid));
570 
571  info.rel = rel;
572  info.ctid = ctid;
573  info.oper = oper;
574 
576  callback.arg = &info;
577  callback.previous = error_context_stack;
579  }
580 
581  for (;;)
582  {
585 
586  SET_LOCKTAG_TRANSACTION(tag, xid);
587 
588  (void) LockAcquire(&tag, ShareLock, false, false);
589 
590  LockRelease(&tag, ShareLock, false);
591 
592  if (!TransactionIdIsInProgress(xid))
593  break;
594 
595  /*
596  * If the Xid belonged to a subtransaction, then the lock would have
597  * gone away as soon as it was finished; for correct tuple visibility,
598  * the right action is to wait on its parent transaction to go away.
599  * But instead of going levels up one by one, we can just wait for the
600  * topmost transaction to finish with the same end result, which also
601  * incurs less locktable traffic.
602  *
603  * Some uses of this function don't involve tuple visibility -- such
604  * as when building snapshots for logical decoding. It is possible to
605  * see a transaction in ProcArray before it registers itself in the
606  * locktable. The topmost transaction in that case is the same xid,
607  * so we try again after a short sleep. (Don't sleep the first time
608  * through, to avoid slowing down the normal case.)
609  */
610  if (!first)
611  pg_usleep(1000L);
612  first = false;
614  }
615 
616  if (oper != XLTW_None)
617  error_context_stack = callback.previous;
618 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:60
Relation rel
Definition: lmgr.c:55
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:683
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:150
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:999
Definition: lock.h:178
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
XLTW_Oper oper
Definition: lmgr.c:54
ErrorContextCallback * error_context_stack
Definition: elog.c:88
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: lmgr.h:26
#define RelationIsValid(relation)
Definition: rel.h:398
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
ItemPointer ctid
Definition: lmgr.c:56
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:405
static void XactLockTableWaitErrorCb(void *arg)
Definition: lmgr.c:729
#define Assert(condition)
Definition: c.h:680
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:225
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1818
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:377