PostgreSQL Source Code  git master
lmgr.c File Reference
#include "postgres.h"
#include "access/subtrans.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "commands/progress.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/sinvaladt.h"
#include "utils/inval.h"
Include dependency graph for lmgr.c:

Go to the source code of this file.

Data Structures

struct  XactLockTableWaitInfo
 

Typedefs

typedef struct XactLockTableWaitInfo XactLockTableWaitInfo
 

Functions

static void XactLockTableWaitErrorCb (void *arg)
 
void RelationInitLockInfo (Relation relation)
 
static void SetLocktagRelationOid (LOCKTAG *tag, Oid relid)
 
void LockRelationOid (Oid relid, LOCKMODE lockmode)
 
bool ConditionalLockRelationOid (Oid relid, LOCKMODE lockmode)
 
void LockRelationId (LockRelId *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 CheckRelationLockedByMe (Relation relation, LOCKMODE lockmode, bool orstronger)
 
bool CheckRelationOidLockedByMe (Oid relid, LOCKMODE lockmode, bool orstronger)
 
bool LockHasWaitersRelation (Relation relation, LOCKMODE lockmode)
 
void LockRelationIdForSession (LockRelId *relid, LOCKMODE lockmode)
 
void UnlockRelationIdForSession (LockRelId *relid, LOCKMODE lockmode)
 
void LockRelationForExtension (Relation relation, LOCKMODE lockmode)
 
bool ConditionalLockRelationForExtension (Relation relation, LOCKMODE lockmode)
 
int RelationExtensionLockWaiterCount (Relation relation)
 
void UnlockRelationForExtension (Relation relation, LOCKMODE lockmode)
 
void LockDatabaseFrozenIds (LOCKMODE lockmode)
 
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)
 
uint32 SpeculativeInsertionLockAcquire (TransactionId xid)
 
void SpeculativeInsertionLockRelease (TransactionId xid)
 
void SpeculativeInsertionWait (TransactionId xid, uint32 token)
 
void WaitForLockersMultiple (List *locktags, LOCKMODE lockmode, bool progress)
 
void WaitForLockers (LOCKTAG heaplocktag, LOCKMODE lockmode, bool progress)
 
void LockDatabaseObject (Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
 
bool ConditionalLockDatabaseObject (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)
 
bool ConditionalLockSharedObject (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 LockApplyTransactionForSession (Oid suboid, TransactionId xid, uint16 objid, LOCKMODE lockmode)
 
void UnlockApplyTransactionForSession (Oid suboid, TransactionId xid, uint16 objid, LOCKMODE lockmode)
 
void DescribeLockTag (StringInfo buf, const LOCKTAG *tag)
 
const char * GetLockNameFromTagType (uint16 locktag_type)
 

Variables

static uint32 speculativeInsertionToken = 0
 

Typedef Documentation

◆ XactLockTableWaitInfo

Function Documentation

◆ CheckRelationLockedByMe()

bool CheckRelationLockedByMe ( Relation  relation,
LOCKMODE  lockmode,
bool  orstronger 
)

Definition at line 330 of file lmgr.c.

331 {
332  LOCKTAG tag;
333 
335  relation->rd_lockInfo.lockRelId.dbId,
336  relation->rd_lockInfo.lockRelId.relId);
337 
338  return LockHeldByMe(&tag, lockmode, orstronger);
339 }
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode, bool orstronger)
Definition: lock.c:590
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
Definition: lock.h:165
LockRelId lockRelId
Definition: rel.h:46
Oid relId
Definition: rel.h:40
Oid dbId
Definition: rel.h:41
LockInfoData rd_lockInfo
Definition: rel.h:114

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

Referenced by addRangeTableEntryForRelation(), ExecGetRangeTableRelation(), relation_open(), and try_relation_open().

◆ CheckRelationOidLockedByMe()

bool CheckRelationOidLockedByMe ( Oid  relid,
LOCKMODE  lockmode,
bool  orstronger 
)

Definition at line 347 of file lmgr.c.

348 {
349  LOCKTAG tag;
350 
351  SetLocktagRelationOid(&tag, relid);
352 
353  return LockHeldByMe(&tag, lockmode, orstronger);
354 }
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:89

References LockHeldByMe(), and SetLocktagRelationOid().

Referenced by SetRelationHasSubclass().

◆ ConditionalLockDatabaseObject()

bool ConditionalLockDatabaseObject ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 1018 of file lmgr.c.

1020 {
1021  LOCKTAG tag;
1022  LOCALLOCK *locallock;
1024 
1025  SET_LOCKTAG_OBJECT(tag,
1026  MyDatabaseId,
1027  classid,
1028  objid,
1029  objsubid);
1030 
1031  res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
1032 
1033  if (res == LOCKACQUIRE_NOT_AVAIL)
1034  return false;
1035 
1036  /*
1037  * Now that we have the lock, check for invalidation messages; see notes
1038  * in LockRelationOid.
1039  */
1041  {
1043  MarkLockClear(locallock);
1044  }
1045 
1046  return true;
1047 }
Oid MyDatabaseId
Definition: globals.c:92
void AcceptInvalidationMessages(void)
Definition: inval.c:806
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:780
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1802
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262
LockAcquireResult
Definition: lock.h:500
@ LOCKACQUIRE_ALREADY_CLEAR
Definition: lock.h:504
@ LOCKACQUIRE_NOT_AVAIL
Definition: lock.h:501

References AcceptInvalidationMessages(), LOCKACQUIRE_ALREADY_CLEAR, LOCKACQUIRE_NOT_AVAIL, LockAcquireExtended(), MarkLockClear(), MyDatabaseId, res, and SET_LOCKTAG_OBJECT.

Referenced by do_autovacuum().

◆ ConditionalLockPage()

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

Definition at line 522 of file lmgr.c.

523 {
524  LOCKTAG tag;
525 
526  SET_LOCKTAG_PAGE(tag,
527  relation->rd_lockInfo.lockRelId.dbId,
528  relation->rd_lockInfo.lockRelId.relId,
529  blkno);
530 
531  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
532 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:756
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:208

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

Referenced by ginInsertCleanup().

◆ ConditionalLockRelation()

bool ConditionalLockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 275 of file lmgr.c.

276 {
277  LOCKTAG tag;
278  LOCALLOCK *locallock;
280 
282  relation->rd_lockInfo.lockRelId.dbId,
283  relation->rd_lockInfo.lockRelId.relId);
284 
285  res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
286 
287  if (res == LOCKACQUIRE_NOT_AVAIL)
288  return false;
289 
290  /*
291  * Now that we have the lock, check for invalidation messages; see notes
292  * in LockRelationOid.
293  */
295  {
297  MarkLockClear(locallock);
298  }
299 
300  return true;
301 }

References AcceptInvalidationMessages(), LockRelId::dbId, LOCKACQUIRE_ALREADY_CLEAR, LOCKACQUIRE_NOT_AVAIL, LockAcquireExtended(), LockInfoData::lockRelId, MarkLockClear(), RelationData::rd_lockInfo, LockRelId::relId, res, and SET_LOCKTAG_RELATION.

Referenced by lazy_truncate_heap().

◆ ConditionalLockRelationForExtension()

bool ConditionalLockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 438 of file lmgr.c.

439 {
440  LOCKTAG tag;
441 
443  relation->rd_lockInfo.lockRelId.dbId,
444  relation->rd_lockInfo.lockRelId.relId);
445 
446  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
447 }
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190

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

◆ ConditionalLockRelationOid()

bool ConditionalLockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 151 of file lmgr.c.

152 {
153  LOCKTAG tag;
154  LOCALLOCK *locallock;
156 
157  SetLocktagRelationOid(&tag, relid);
158 
159  res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
160 
161  if (res == LOCKACQUIRE_NOT_AVAIL)
162  return false;
163 
164  /*
165  * Now that we have the lock, check for invalidation messages; see notes
166  * in LockRelationOid.
167  */
169  {
171  MarkLockClear(locallock);
172  }
173 
174  return true;
175 }

References AcceptInvalidationMessages(), LOCKACQUIRE_ALREADY_CLEAR, LOCKACQUIRE_NOT_AVAIL, LockAcquireExtended(), MarkLockClear(), res, and SetLocktagRelationOid().

Referenced by AlterTableMoveAll(), do_autovacuum(), LockTableRecurse(), LockViewRecurse_walker(), RangeVarGetRelidExtended(), and vacuum_open_relation().

◆ ConditionalLockSharedObject()

bool ConditionalLockSharedObject ( Oid  classid,
Oid  objid,
uint16  objsubid,
LOCKMODE  lockmode 
)

Definition at line 1097 of file lmgr.c.

1099 {
1100  LOCKTAG tag;
1101  LOCALLOCK *locallock;
1103 
1104  SET_LOCKTAG_OBJECT(tag,
1105  InvalidOid,
1106  classid,
1107  objid,
1108  objsubid);
1109 
1110  res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
1111 
1112  if (res == LOCKACQUIRE_NOT_AVAIL)
1113  return false;
1114 
1115  /*
1116  * Now that we have the lock, check for invalidation messages; see notes
1117  * in LockRelationOid.
1118  */
1120  {
1122  MarkLockClear(locallock);
1123  }
1124 
1125  return true;
1126 }
#define InvalidOid
Definition: postgres_ext.h:36

References AcceptInvalidationMessages(), InvalidOid, LOCKACQUIRE_ALREADY_CLEAR, LOCKACQUIRE_NOT_AVAIL, LockAcquireExtended(), MarkLockClear(), res, and SET_LOCKTAG_OBJECT.

Referenced by EventTriggerOnLogin().

◆ ConditionalLockTuple()

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

Definition at line 578 of file lmgr.c.

579 {
580  LOCKTAG tag;
581 
582  SET_LOCKTAG_TUPLE(tag,
583  relation->rd_lockInfo.lockRelId.dbId,
584  relation->rd_lockInfo.lockRelId.relId,
587 
588  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
589 }
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217

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

◆ ConditionalXactLockTableWait()

bool ConditionalXactLockTableWait ( TransactionId  xid)

Definition at line 730 of file lmgr.c.

731 {
732  LOCKTAG tag;
733  bool first = true;
734 
735  for (;;)
736  {
739 
740  SET_LOCKTAG_TRANSACTION(tag, xid);
741 
742  if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
743  return false;
744 
745  LockRelease(&tag, ShareLock, false);
746 
747  if (!TransactionIdIsInProgress(xid))
748  break;
749 
750  /* See XactLockTableWait about this case */
751  if (!first)
752  pg_usleep(1000L);
753  first = false;
755  }
756 
757  return true;
758 }
#define Assert(condition)
Definition: c.h:858
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1964
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:226
#define ShareLock
Definition: lockdefs.h:40
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1402
void pg_usleep(long microsec)
Definition: signal.c:53
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:163
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define TransactionIdIsValid(xid)
Definition: transam.h:41
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:439

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

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

◆ DescribeLockTag()

void DescribeLockTag ( StringInfo  buf,
const LOCKTAG tag 
)

Definition at line 1233 of file lmgr.c.

1234 {
1235  switch ((LockTagType) tag->locktag_type)
1236  {
1237  case LOCKTAG_RELATION:
1239  _("relation %u of database %u"),
1240  tag->locktag_field2,
1241  tag->locktag_field1);
1242  break;
1245  _("extension of relation %u of database %u"),
1246  tag->locktag_field2,
1247  tag->locktag_field1);
1248  break;
1251  _("pg_database.datfrozenxid of database %u"),
1252  tag->locktag_field1);
1253  break;
1254  case LOCKTAG_PAGE:
1256  _("page %u of relation %u of database %u"),
1257  tag->locktag_field3,
1258  tag->locktag_field2,
1259  tag->locktag_field1);
1260  break;
1261  case LOCKTAG_TUPLE:
1263  _("tuple (%u,%u) of relation %u of database %u"),
1264  tag->locktag_field3,
1265  tag->locktag_field4,
1266  tag->locktag_field2,
1267  tag->locktag_field1);
1268  break;
1269  case LOCKTAG_TRANSACTION:
1271  _("transaction %u"),
1272  tag->locktag_field1);
1273  break;
1276  _("virtual transaction %d/%u"),
1277  tag->locktag_field1,
1278  tag->locktag_field2);
1279  break;
1282  _("speculative token %u of transaction %u"),
1283  tag->locktag_field2,
1284  tag->locktag_field1);
1285  break;
1286  case LOCKTAG_OBJECT:
1288  _("object %u of class %u of database %u"),
1289  tag->locktag_field3,
1290  tag->locktag_field2,
1291  tag->locktag_field1);
1292  break;
1293  case LOCKTAG_USERLOCK:
1294  /* reserved for old contrib code, now on pgfoundry */
1296  _("user lock [%u,%u,%u]"),
1297  tag->locktag_field1,
1298  tag->locktag_field2,
1299  tag->locktag_field3);
1300  break;
1301  case LOCKTAG_ADVISORY:
1303  _("advisory lock [%u,%u,%u,%u]"),
1304  tag->locktag_field1,
1305  tag->locktag_field2,
1306  tag->locktag_field3,
1307  tag->locktag_field4);
1308  break;
1311  _("remote transaction %u of subscription %u of database %u"),
1312  tag->locktag_field3,
1313  tag->locktag_field2,
1314  tag->locktag_field1);
1315  break;
1316  default:
1318  _("unrecognized locktag type %d"),
1319  (int) tag->locktag_type);
1320  break;
1321  }
1322 }
#define _(x)
Definition: elog.c:90
LockTagType
Definition: lock.h:136
@ LOCKTAG_OBJECT
Definition: lock.h:145
@ LOCKTAG_RELATION_EXTEND
Definition: lock.h:138
@ LOCKTAG_RELATION
Definition: lock.h:137
@ LOCKTAG_TUPLE
Definition: lock.h:141
@ LOCKTAG_SPECULATIVE_TOKEN
Definition: lock.h:144
@ LOCKTAG_APPLY_TRANSACTION
Definition: lock.h:148
@ LOCKTAG_USERLOCK
Definition: lock.h:146
@ LOCKTAG_DATABASE_FROZEN_IDS
Definition: lock.h:139
@ LOCKTAG_VIRTUALTRANSACTION
Definition: lock.h:143
@ LOCKTAG_TRANSACTION
Definition: lock.h:142
@ LOCKTAG_PAGE
Definition: lock.h:140
@ LOCKTAG_ADVISORY
Definition: lock.h:147
static char * buf
Definition: pg_test_fsync.c:73
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
uint8 locktag_type
Definition: lock.h:170
uint32 locktag_field3
Definition: lock.h:168
uint32 locktag_field1
Definition: lock.h:166
uint16 locktag_field4
Definition: lock.h:169
uint32 locktag_field2
Definition: lock.h:167

References _, appendStringInfo(), buf, LOCKTAG_ADVISORY, LOCKTAG_APPLY_TRANSACTION, LOCKTAG_DATABASE_FROZEN_IDS, 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().

◆ GetLockNameFromTagType()

const char* GetLockNameFromTagType ( uint16  locktag_type)

Definition at line 1330 of file lmgr.c.

1331 {
1332  if (locktag_type > LOCKTAG_LAST_TYPE)
1333  return "???";
1334  return LockTagTypeNames[locktag_type];
1335 }
#define LOCKTAG_LAST_TYPE
Definition: lock.h:152
const char *const LockTagTypeNames[]
Definition: lockfuncs.c:28

References LOCKTAG_LAST_TYPE, and LockTagTypeNames.

Referenced by pgstat_get_wait_event().

◆ LockApplyTransactionForSession()

void LockApplyTransactionForSession ( Oid  suboid,
TransactionId  xid,
uint16  objid,
LOCKMODE  lockmode 
)

Definition at line 1193 of file lmgr.c.

1195 {
1196  LOCKTAG tag;
1197 
1199  MyDatabaseId,
1200  suboid,
1201  xid,
1202  objid);
1203 
1204  (void) LockAcquire(&tag, lockmode, true, false);
1205 }
#define SET_LOCKTAG_APPLY_TRANSACTION(locktag, dboid, suboid, xid, objid)
Definition: lock.h:282

References LockAcquire(), MyDatabaseId, and SET_LOCKTAG_APPLY_TRANSACTION.

Referenced by pa_lock_stream(), and pa_lock_transaction().

◆ LockDatabaseFrozenIds()

void LockDatabaseFrozenIds ( LOCKMODE  lockmode)

Definition at line 487 of file lmgr.c.

488 {
489  LOCKTAG tag;
490 
492 
493  (void) LockAcquire(&tag, lockmode, false, false);
494 }
#define SET_LOCKTAG_DATABASE_FROZEN_IDS(locktag, dboid)
Definition: lock.h:199

References LockAcquire(), MyDatabaseId, and SET_LOCKTAG_DATABASE_FROZEN_IDS.

Referenced by vac_update_datfrozenxid().

◆ LockDatabaseObject()

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

Definition at line 994 of file lmgr.c.

996 {
997  LOCKTAG tag;
998 
999  SET_LOCKTAG_OBJECT(tag,
1000  MyDatabaseId,
1001  classid,
1002  objid,
1003  objsubid);
1004 
1005  (void) LockAcquire(&tag, lockmode, false, false);
1006 
1007  /* Make sure syscaches are up-to-date with any changes we waited for */
1009 }

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

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

◆ LockHasWaitersRelation()

bool LockHasWaitersRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 363 of file lmgr.c.

364 {
365  LOCKTAG tag;
366 
368  relation->rd_lockInfo.lockRelId.dbId,
369  relation->rd_lockInfo.lockRelId.relId);
370 
371  return LockHasWaiters(&tag, lockmode, false);
372 }
bool LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:643

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

Referenced by count_nondeletable_pages().

◆ LockPage()

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

Definition at line 503 of file lmgr.c.

504 {
505  LOCKTAG tag;
506 
507  SET_LOCKTAG_PAGE(tag,
508  relation->rd_lockInfo.lockRelId.dbId,
509  relation->rd_lockInfo.lockRelId.relId,
510  blkno);
511 
512  (void) LockAcquire(&tag, lockmode, false, false);
513 }

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

Referenced by ginInsertCleanup().

◆ LockRelation()

void LockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 244 of file lmgr.c.

245 {
246  LOCKTAG tag;
247  LOCALLOCK *locallock;
249 
251  relation->rd_lockInfo.lockRelId.dbId,
252  relation->rd_lockInfo.lockRelId.relId);
253 
254  res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
255 
256  /*
257  * Now that we have the lock, check for invalidation messages; see notes
258  * in LockRelationOid.
259  */
261  {
263  MarkLockClear(locallock);
264  }
265 }

References AcceptInvalidationMessages(), LockRelId::dbId, LOCKACQUIRE_ALREADY_CLEAR, LockAcquireExtended(), LockInfoData::lockRelId, MarkLockClear(), RelationData::rd_lockInfo, LockRelId::relId, res, and SET_LOCKTAG_RELATION.

Referenced by index_create().

◆ LockRelationForExtension()

void LockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

◆ LockRelationId()

void LockRelationId ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 184 of file lmgr.c.

185 {
186  LOCKTAG tag;
187  LOCALLOCK *locallock;
189 
190  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
191 
192  res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
193 
194  /*
195  * Now that we have the lock, check for invalidation messages; see notes
196  * in LockRelationOid.
197  */
199  {
201  MarkLockClear(locallock);
202  }
203 }

References AcceptInvalidationMessages(), LockRelId::dbId, LOCKACQUIRE_ALREADY_CLEAR, LockAcquireExtended(), MarkLockClear(), LockRelId::relId, res, and SET_LOCKTAG_RELATION.

Referenced by CreateDatabaseUsingWalLog(), and ScanSourceDatabasePgClass().

◆ LockRelationIdForSession()

void LockRelationIdForSession ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 387 of file lmgr.c.

388 {
389  LOCKTAG tag;
390 
391  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
392 
393  (void) LockAcquire(&tag, lockmode, true, false);
394 }

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

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

◆ LockRelationOid()

void LockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 108 of file lmgr.c.

109 {
110  LOCKTAG tag;
111  LOCALLOCK *locallock;
113 
114  SetLocktagRelationOid(&tag, relid);
115 
116  res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
117 
118  /*
119  * Now that we have the lock, check for invalidation messages, so that we
120  * will update or flush any stale relcache entry before we try to use it.
121  * RangeVarGetRelid() specifically relies on us for this. We can skip
122  * this in the not-uncommon case that we already had the same type of lock
123  * being requested, since then no one else could have modified the
124  * relcache entry in an undesirable way. (In the case where our own xact
125  * modifies the rel, the relcache update happens via
126  * CommandCounterIncrement, not here.)
127  *
128  * However, in corner cases where code acts on tables (usually catalogs)
129  * recursively, we might get here while still processing invalidation
130  * messages in some outer execution of this function or a sibling. The
131  * "cleared" status of the lock tells us whether we really are done
132  * absorbing relevant inval messages.
133  */
135  {
137  MarkLockClear(locallock);
138  }
139 }

References AcceptInvalidationMessages(), LOCKACQUIRE_ALREADY_CLEAR, LockAcquireExtended(), MarkLockClear(), res, and SetLocktagRelationOid().

Referenced by AcquireDeletionLock(), AcquireExecutorLocks(), AlterTableMoveAll(), ATExecAttachPartition(), ATExecDetachPartition(), ATPostAlterTypeCleanup(), binary_upgrade_replorigin_advance(), copy_table_data(), CreateTriggerFiringOn(), find_inheritance_children_extended(), heap_create_with_catalog(), heap_drop_with_catalog(), index_create(), IndexSetParentIndex(), InitCatCachePhase2(), load_critical_index(), lock_and_open_sequence(), LockTableRecurse(), LockViewRecurse_walker(), LogicalRepSyncTableStart(), pg_replication_origin_advance(), RangeVarCallbackForAttachIndex(), RangeVarCallbackForDropRelation(), RangeVarCallbackForReindexIndex(), RangeVarGetAndCheckCreationNamespace(), RangeVarGetRelidExtended(), RefreshMatViewByOid(), relation_open(), ScanQueryForLocks(), SequenceChangePersistence(), and try_relation_open().

◆ LockSharedObject()

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

◆ LockSharedObjectForSession()

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

Definition at line 1153 of file lmgr.c.

1155 {
1156  LOCKTAG tag;
1157 
1158  SET_LOCKTAG_OBJECT(tag,
1159  InvalidOid,
1160  classid,
1161  objid,
1162  objsubid);
1163 
1164  (void) LockAcquire(&tag, lockmode, true, false);
1165 }

References InvalidOid, LockAcquire(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

◆ LockTuple()

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

Definition at line 558 of file lmgr.c.

559 {
560  LOCKTAG tag;
561 
562  SET_LOCKTAG_TUPLE(tag,
563  relation->rd_lockInfo.lockRelId.dbId,
564  relation->rd_lockInfo.lockRelId.relId,
567 
568  (void) LockAcquire(&tag, lockmode, false, false);
569 }

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

◆ RelationExtensionLockWaiterCount()

int RelationExtensionLockWaiterCount ( Relation  relation)

Definition at line 455 of file lmgr.c.

456 {
457  LOCKTAG tag;
458 
460  relation->rd_lockInfo.lockRelId.dbId,
461  relation->rd_lockInfo.lockRelId.relId);
462 
463  return LockWaiterCount(&tag);
464 }
int LockWaiterCount(const LOCKTAG *locktag)
Definition: lock.c:4661

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

Referenced by RelationAddBlocks().

◆ RelationInitLockInfo()

void RelationInitLockInfo ( Relation  relation)

Definition at line 71 of file lmgr.c.

72 {
73  Assert(RelationIsValid(relation));
75 
76  relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
77 
78  if (relation->rd_rel->relisshared)
80  else
82 }
#define OidIsValid(objectId)
Definition: c.h:775
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationIsValid(relation)
Definition: rel.h:478
Form_pg_class rd_rel
Definition: rel.h:111

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().

◆ SetLocktagRelationOid()

static void SetLocktagRelationOid ( LOCKTAG tag,
Oid  relid 
)
inlinestatic

Definition at line 89 of file lmgr.c.

90 {
91  Oid dbid;
92 
93  if (IsSharedRelation(relid))
94  dbid = InvalidOid;
95  else
96  dbid = MyDatabaseId;
97 
98  SET_LOCKTAG_RELATION(*tag, dbid, relid);
99 }
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:264
unsigned int Oid
Definition: postgres_ext.h:31

References InvalidOid, IsSharedRelation(), MyDatabaseId, and SET_LOCKTAG_RELATION.

Referenced by CheckRelationOidLockedByMe(), ConditionalLockRelationOid(), LockRelationOid(), and UnlockRelationOid().

◆ SpeculativeInsertionLockAcquire()

uint32 SpeculativeInsertionLockAcquire ( TransactionId  xid)

Definition at line 772 of file lmgr.c.

773 {
774  LOCKTAG tag;
775 
777 
778  /*
779  * Check for wrap-around. Zero means no token is held, so don't use that.
780  */
781  if (speculativeInsertionToken == 0)
783 
785 
786  (void) LockAcquire(&tag, ExclusiveLock, false, false);
787 
789 }
static uint32 speculativeInsertionToken
Definition: lmgr.c:46
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:247
#define ExclusiveLock
Definition: lockdefs.h:42

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

Referenced by ExecInsert().

◆ SpeculativeInsertionLockRelease()

void SpeculativeInsertionLockRelease ( TransactionId  xid)

Definition at line 798 of file lmgr.c.

799 {
800  LOCKTAG tag;
801 
803 
804  LockRelease(&tag, ExclusiveLock, false);
805 }

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

Referenced by ExecInsert().

◆ SpeculativeInsertionWait()

void SpeculativeInsertionWait ( TransactionId  xid,
uint32  token 
)

Definition at line 814 of file lmgr.c.

815 {
816  LOCKTAG tag;
817 
819 
821  Assert(token != 0);
822 
823  (void) LockAcquire(&tag, ShareLock, false, false);
824  LockRelease(&tag, ShareLock, false);
825 }
#define token
Definition: indent_globs.h:126

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

Referenced by _bt_doinsert(), and check_exclusion_or_unique_constraint().

◆ UnlockApplyTransactionForSession()

void UnlockApplyTransactionForSession ( Oid  suboid,
TransactionId  xid,
uint16  objid,
LOCKMODE  lockmode 
)

Definition at line 1211 of file lmgr.c.

1213 {
1214  LOCKTAG tag;
1215 
1217  MyDatabaseId,
1218  suboid,
1219  xid,
1220  objid);
1221 
1222  LockRelease(&tag, lockmode, true);
1223 }

References LockRelease(), MyDatabaseId, and SET_LOCKTAG_APPLY_TRANSACTION.

Referenced by pa_unlock_stream(), and pa_unlock_transaction().

◆ UnlockDatabaseObject()

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

Definition at line 1053 of file lmgr.c.

1055 {
1056  LOCKTAG tag;
1057 
1058  SET_LOCKTAG_OBJECT(tag,
1059  MyDatabaseId,
1060  classid,
1061  objid,
1062  objsubid);
1063 
1064  LockRelease(&tag, lockmode, false);
1065 }

References LockRelease(), MyDatabaseId, and SET_LOCKTAG_OBJECT.

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

◆ UnlockPage()

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

Definition at line 538 of file lmgr.c.

539 {
540  LOCKTAG tag;
541 
542  SET_LOCKTAG_PAGE(tag,
543  relation->rd_lockInfo.lockRelId.dbId,
544  relation->rd_lockInfo.lockRelId.relId,
545  blkno);
546 
547  LockRelease(&tag, lockmode, false);
548 }

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

Referenced by ginInsertCleanup().

◆ UnlockRelation()

void UnlockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 310 of file lmgr.c.

311 {
312  LOCKTAG tag;
313 
315  relation->rd_lockInfo.lockRelId.dbId,
316  relation->rd_lockInfo.lockRelId.relId);
317 
318  LockRelease(&tag, lockmode, false);
319 }

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

Referenced by lazy_truncate_heap().

◆ UnlockRelationForExtension()

void UnlockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

◆ UnlockRelationId()

void UnlockRelationId ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 212 of file lmgr.c.

213 {
214  LOCKTAG tag;
215 
216  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
217 
218  LockRelease(&tag, lockmode, false);
219 }

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

Referenced by CreateDatabaseUsingWalLog(), index_close(), relation_close(), and ScanSourceDatabasePgClass().

◆ UnlockRelationIdForSession()

void UnlockRelationIdForSession ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 400 of file lmgr.c.

401 {
402  LOCKTAG tag;
403 
404  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
405 
406  LockRelease(&tag, lockmode, true);
407 }

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

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

◆ UnlockRelationOid()

◆ UnlockSharedObject()

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

Definition at line 1132 of file lmgr.c.

1134 {
1135  LOCKTAG tag;
1136 
1137  SET_LOCKTAG_OBJECT(tag,
1138  InvalidOid,
1139  classid,
1140  objid,
1141  objsubid);
1142 
1143  LockRelease(&tag, lockmode, false);
1144 }

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

Referenced by AddSubscriptionRelState(), AlterDatabaseSet(), createdb_failure_callback(), drop_local_obsolete_slots(), get_db_info(), get_object_address(), replorigin_drop_by_name(), and synchronize_slots().

◆ UnlockSharedObjectForSession()

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

Definition at line 1171 of file lmgr.c.

1173 {
1174  LOCKTAG tag;
1175 
1176  SET_LOCKTAG_OBJECT(tag,
1177  InvalidOid,
1178  classid,
1179  objid,
1180  objsubid);
1181 
1182  LockRelease(&tag, lockmode, true);
1183 }

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

◆ UnlockTuple()

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

Definition at line 595 of file lmgr.c.

596 {
597  LOCKTAG tag;
598 
599  SET_LOCKTAG_TUPLE(tag,
600  relation->rd_lockInfo.lockRelId.dbId,
601  relation->rd_lockInfo.lockRelId.relId,
604 
605  LockRelease(&tag, lockmode, false);
606 }

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

◆ WaitForLockers()

void WaitForLockers ( LOCKTAG  heaplocktag,
LOCKMODE  lockmode,
bool  progress 
)

Definition at line 975 of file lmgr.c.

976 {
977  List *l;
978 
979  l = list_make1(&heaplocktag);
980  WaitForLockersMultiple(l, lockmode, progress);
981  list_free(l);
982 }
void list_free(List *list)
Definition: list.c:1546
void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
Definition: lmgr.c:897
#define list_make1(x1)
Definition: pg_list.h:212
static int progress
Definition: pgbench.c:261
Definition: pg_list.h:54

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

Referenced by DefineIndex(), and index_drop().

◆ WaitForLockersMultiple()

void WaitForLockersMultiple ( List locktags,
LOCKMODE  lockmode,
bool  progress 
)

Definition at line 897 of file lmgr.c.

898 {
899  List *holders = NIL;
900  ListCell *lc;
901  int total = 0;
902  int done = 0;
903 
904  /* Done if no locks to wait for */
905  if (locktags == NIL)
906  return;
907 
908  /* Collect the transactions we need to wait on */
909  foreach(lc, locktags)
910  {
911  LOCKTAG *locktag = lfirst(lc);
912  int count;
913 
914  holders = lappend(holders,
915  GetLockConflicts(locktag, lockmode,
916  progress ? &count : NULL));
917  if (progress)
918  total += count;
919  }
920 
921  if (progress)
923 
924  /*
925  * Note: GetLockConflicts() never reports our own xid, hence we need not
926  * check for that. Also, prepared xacts are reported and awaited.
927  */
928 
929  /* Finally wait for each such transaction to complete */
930  foreach(lc, holders)
931  {
932  VirtualTransactionId *lockholders = lfirst(lc);
933 
934  while (VirtualTransactionIdIsValid(*lockholders))
935  {
936  /* If requested, publish who we're going to wait for. */
937  if (progress)
938  {
939  PGPROC *holder = ProcNumberGetProc(lockholders->procNumber);
940 
941  if (holder)
943  holder->pid);
944  }
945  VirtualXactLock(*lockholders, true);
946  lockholders++;
947 
948  if (progress)
950  }
951  }
952  if (progress)
953  {
954  const int index[] = {
958  };
959  const int64 values[] = {
960  0, 0, 0
961  };
962 
964  }
965 
966  list_free_deep(holders);
967 }
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
static Datum values[MAXATTR]
Definition: bootstrap.c:150
List * lappend(List *list, void *datum)
Definition: list.c:339
void list_free_deep(List *list)
Definition: list.c:1560
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition: lock.c:2894
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4550
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:67
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
Definition: procarray.c:3142
#define PROGRESS_WAITFOR_DONE
Definition: progress.h:118
#define PROGRESS_WAITFOR_TOTAL
Definition: progress.h:117
#define PROGRESS_WAITFOR_CURRENT_PID
Definition: progress.h:119
Definition: proc.h:157
int pid
Definition: proc.h:177
ProcNumber procNumber
Definition: lock.h:61
Definition: type.h:95

References GetLockConflicts(), lappend(), lfirst, list_free_deep(), NIL, pgstat_progress_update_multi_param(), pgstat_progress_update_param(), PGPROC::pid, VirtualTransactionId::procNumber, ProcNumberGetProc(), progress, PROGRESS_WAITFOR_CURRENT_PID, PROGRESS_WAITFOR_DONE, PROGRESS_WAITFOR_TOTAL, values, VirtualTransactionIdIsValid, and VirtualXactLock().

Referenced by ATExecDetachPartition(), ReindexRelationConcurrently(), and WaitForLockers().

◆ XactLockTableDelete()

void XactLockTableDelete ( TransactionId  xid)

Definition at line 633 of file lmgr.c.

634 {
635  LOCKTAG tag;
636 
637  SET_LOCKTAG_TRANSACTION(tag, xid);
638 
639  LockRelease(&tag, ExclusiveLock, false);
640 }

References ExclusiveLock, LockRelease(), and SET_LOCKTAG_TRANSACTION.

Referenced by CommitSubTransaction().

◆ XactLockTableInsert()

void XactLockTableInsert ( TransactionId  xid)

Definition at line 616 of file lmgr.c.

617 {
618  LOCKTAG tag;
619 
620  SET_LOCKTAG_TRANSACTION(tag, xid);
621 
622  (void) LockAcquire(&tag, ExclusiveLock, false, false);
623 }

References ExclusiveLock, LockAcquire(), and SET_LOCKTAG_TRANSACTION.

Referenced by AssignTransactionId().

◆ XactLockTableWait()

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

Definition at line 657 of file lmgr.c.

659 {
660  LOCKTAG tag;
663  bool first = true;
664 
665  /*
666  * If an operation is specified, set up our verbose error context
667  * callback.
668  */
669  if (oper != XLTW_None)
670  {
671  Assert(RelationIsValid(rel));
672  Assert(ItemPointerIsValid(ctid));
673 
674  info.rel = rel;
675  info.ctid = ctid;
676  info.oper = oper;
677 
679  callback.arg = &info;
680  callback.previous = error_context_stack;
682  }
683 
684  for (;;)
685  {
688 
689  SET_LOCKTAG_TRANSACTION(tag, xid);
690 
691  (void) LockAcquire(&tag, ShareLock, false, false);
692 
693  LockRelease(&tag, ShareLock, false);
694 
695  if (!TransactionIdIsInProgress(xid))
696  break;
697 
698  /*
699  * If the Xid belonged to a subtransaction, then the lock would have
700  * gone away as soon as it was finished; for correct tuple visibility,
701  * the right action is to wait on its parent transaction to go away.
702  * But instead of going levels up one by one, we can just wait for the
703  * topmost transaction to finish with the same end result, which also
704  * incurs less locktable traffic.
705  *
706  * Some uses of this function don't involve tuple visibility -- such
707  * as when building snapshots for logical decoding. It is possible to
708  * see a transaction in ProcArray before it registers itself in the
709  * locktable. The topmost transaction in that case is the same xid,
710  * so we try again after a short sleep. (Don't sleep the first time
711  * through, to avoid slowing down the normal case.)
712  */
713  if (!first)
714  pg_usleep(1000L);
715  first = false;
717  }
718 
719  if (oper != XLTW_None)
720  error_context_stack = callback.previous;
721 }
ErrorContextCallback * error_context_stack
Definition: elog.c:94
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition: itemptr.h:83
static void XactLockTableWaitErrorCb(void *arg)
Definition: lmgr.c:832
@ XLTW_None
Definition: lmgr.h:26
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:370
ItemPointer ctid
Definition: lmgr.c:59
XLTW_Oper oper
Definition: lmgr.c:57
Relation rel
Definition: lmgr.c:58
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References Assert, callback(), XactLockTableWaitInfo::ctid, error_context_stack, GetTopTransactionIdIfAny(), ItemPointerIsValid(), LockAcquire(), LockRelease(), oper(), XactLockTableWaitInfo::oper, pg_usleep(), 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(), heap_delete(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_update(), heapam_index_build_range_scan(), heapam_tuple_lock(), RelationFindReplTupleByIndex(), RelationFindReplTupleSeq(), and SnapBuildWaitSnapshot().

◆ XactLockTableWaitErrorCb()

static void XactLockTableWaitErrorCb ( void *  arg)
static

Definition at line 832 of file lmgr.c.

833 {
835 
836  /*
837  * We would like to print schema name too, but that would require a
838  * syscache lookup.
839  */
840  if (info->oper != XLTW_None &&
841  ItemPointerIsValid(info->ctid) && RelationIsValid(info->rel))
842  {
843  const char *cxt;
844 
845  switch (info->oper)
846  {
847  case XLTW_Update:
848  cxt = gettext_noop("while updating tuple (%u,%u) in relation \"%s\"");
849  break;
850  case XLTW_Delete:
851  cxt = gettext_noop("while deleting tuple (%u,%u) in relation \"%s\"");
852  break;
853  case XLTW_Lock:
854  cxt = gettext_noop("while locking tuple (%u,%u) in relation \"%s\"");
855  break;
856  case XLTW_LockUpdated:
857  cxt = gettext_noop("while locking updated version (%u,%u) of tuple in relation \"%s\"");
858  break;
859  case XLTW_InsertIndex:
860  cxt = gettext_noop("while inserting index tuple (%u,%u) in relation \"%s\"");
861  break;
863  cxt = gettext_noop("while checking uniqueness of tuple (%u,%u) in relation \"%s\"");
864  break;
865  case XLTW_FetchUpdated:
866  cxt = gettext_noop("while rechecking updated tuple (%u,%u) in relation \"%s\"");
867  break;
869  cxt = gettext_noop("while checking exclusion constraint on tuple (%u,%u) in relation \"%s\"");
870  break;
871 
872  default:
873  return;
874  }
875 
876  errcontext(cxt,
880  }
881 }
#define gettext_noop(x)
Definition: c.h:1196
#define errcontext
Definition: elog.h:196
@ XLTW_FetchUpdated
Definition: lmgr.h:33
@ XLTW_Lock
Definition: lmgr.h:29
@ XLTW_Delete
Definition: lmgr.h:28
@ XLTW_InsertIndex
Definition: lmgr.h:31
@ XLTW_LockUpdated
Definition: lmgr.h:30
@ XLTW_RecheckExclusionConstr
Definition: lmgr.h:34
@ XLTW_Update
Definition: lmgr.h:27
@ XLTW_InsertIndexUnique
Definition: lmgr.h:32
void * arg
#define RelationGetRelationName(relation)
Definition: rel.h:539

References arg, XactLockTableWaitInfo::ctid, errcontext, gettext_noop, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), XactLockTableWaitInfo::oper, XactLockTableWaitInfo::rel, RelationGetRelationName, RelationIsValid, XLTW_Delete, XLTW_FetchUpdated, XLTW_InsertIndex, XLTW_InsertIndexUnique, XLTW_Lock, XLTW_LockUpdated, XLTW_None, XLTW_RecheckExclusionConstr, and XLTW_Update.

Referenced by XactLockTableWait().

Variable Documentation

◆ speculativeInsertionToken

uint32 speculativeInsertionToken = 0
static

Definition at line 46 of file lmgr.c.

Referenced by SpeculativeInsertionLockAcquire(), and SpeculativeInsertionLockRelease().