PostgreSQL Source Code  git master
lmgr.c File Reference
#include "postgres.h"
#include "access/subtrans.h"
#include "access/transam.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/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 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 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 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)
 
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)
 

Variables

static uint32 speculativeInsertionToken = 0
 

Typedef Documentation

◆ XactLockTableWaitInfo

Function Documentation

◆ CheckRelationLockedByMe()

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

Definition at line 302 of file lmgr.c.

References LockRelId::dbId, elog, GetLockmodeName(), LockHeldByMe(), LockInfoData::lockRelId, LOCKTAG::locktag_lockmethodid, MaxLockMode, RelationData::rd_lockInfo, RelationGetRelationName, LockRelId::relId, SET_LOCKTAG_RELATION, and WARNING.

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

303 {
304  LOCKTAG tag;
305 
307  relation->rd_lockInfo.lockRelId.dbId,
308  relation->rd_lockInfo.lockRelId.relId);
309 
310  if (LockHeldByMe(&tag, lockmode))
311  return true;
312 
313  if (orstronger)
314  {
315  LOCKMODE slockmode;
316 
317  for (slockmode = lockmode + 1;
318  slockmode <= MaxLockMode;
319  slockmode++)
320  {
321  if (LockHeldByMe(&tag, slockmode))
322  {
323 #ifdef NOT_USED
324  /* Sometimes this might be useful for debugging purposes */
325  elog(WARNING, "lock mode %s substituted for %s on relation %s",
326  GetLockmodeName(tag.locktag_lockmethodid, slockmode),
327  GetLockmodeName(tag.locktag_lockmethodid, lockmode),
328  RelationGetRelationName(relation));
329 #endif
330  return true;
331  }
332  }
333  }
334 
335  return false;
336 }
#define MaxLockMode
Definition: lockdefs.h:48
LockRelId lockRelId
Definition: rel.h:43
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition: lock.c:3925
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
Definition: lock.c:571
int LOCKMODE
Definition: lockdefs.h:26
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:86
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define WARNING
Definition: elog.h:40
uint8 locktag_lockmethodid
Definition: lock.h:171
#define elog(elevel,...)
Definition: elog.h:226
Oid relId
Definition: rel.h:37

◆ ConditionalLockPage()

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

Definition at line 489 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().

490 {
491  LOCKTAG tag;
492 
493  SET_LOCKTAG_PAGE(tag,
494  relation->rd_lockInfo.lockRelId.dbId,
495  relation->rd_lockInfo.lockRelId.relId,
496  blkno);
497 
498  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
499 }
LockRelId lockRelId
Definition: rel.h:43
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:199
Oid relId
Definition: rel.h:37

◆ ConditionalLockRelation()

bool ConditionalLockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 247 of file lmgr.c.

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

Referenced by lazy_truncate_heap().

248 {
249  LOCKTAG tag;
250  LOCALLOCK *locallock;
251  LockAcquireResult res;
252 
254  relation->rd_lockInfo.lockRelId.dbId,
255  relation->rd_lockInfo.lockRelId.relId);
256 
257  res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
258 
259  if (res == LOCKACQUIRE_NOT_AVAIL)
260  return false;
261 
262  /*
263  * Now that we have the lock, check for invalidation messages; see notes
264  * in LockRelationOid.
265  */
266  if (res != LOCKACQUIRE_ALREADY_CLEAR)
267  {
269  MarkLockClear(locallock);
270  }
271 
272  return true;
273 }
LockRelId lockRelId
Definition: rel.h:43
void AcceptInvalidationMessages(void)
Definition: inval.c:681
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:86
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1708
LockAcquireResult
Definition: lock.h:476
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:732
Oid relId
Definition: rel.h:37

◆ ConditionalLockRelationForExtension()

bool ConditionalLockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 420 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().

421 {
422  LOCKTAG tag;
423 
425  relation->rd_lockInfo.lockRelId.dbId,
426  relation->rd_lockInfo.lockRelId.relId);
427 
428  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
429 }
LockRelId lockRelId
Definition: rel.h:43
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:37

◆ ConditionalLockRelationOid()

bool ConditionalLockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 151 of file lmgr.c.

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

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

152 {
153  LOCKTAG tag;
154  LOCALLOCK *locallock;
155  LockAcquireResult res;
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  */
168  if (res != LOCKACQUIRE_ALREADY_CLEAR)
169  {
171  MarkLockClear(locallock);
172  }
173 
174  return true;
175 }
void AcceptInvalidationMessages(void)
Definition: inval.c:681
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:89
Definition: lock.h:164
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1708
LockAcquireResult
Definition: lock.h:476
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:732

◆ ConditionalLockTuple()

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

Definition at line 545 of file lmgr.c.

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

546 {
547  LOCKTAG tag;
548 
549  SET_LOCKTAG_TUPLE(tag,
550  relation->rd_lockInfo.lockRelId.dbId,
551  relation->rd_lockInfo.lockRelId.relId,
554 
555  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
556 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:208
LockRelId lockRelId
Definition: rel.h:43
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Oid relId
Definition: rel.h:37

◆ ConditionalXactLockTableWait()

bool ConditionalXactLockTableWait ( TransactionId  xid)

Definition at line 697 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(), heap_lock_tuple(), and heapam_tuple_lock().

698 {
699  LOCKTAG tag;
700  bool first = true;
701 
702  for (;;)
703  {
706 
707  SET_LOCKTAG_TRANSACTION(tag, xid);
708 
709  if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
710  return false;
711 
712  LockRelease(&tag, ShareLock, false);
713 
714  if (!TransactionIdIsInProgress(xid))
715  break;
716 
717  /* See XactLockTableWait about this case */
718  if (!first)
719  pg_usleep(1000L);
720  first = false;
722  }
723 
724  return true;
725 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:150
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:985
Definition: lock.h:164
void pg_usleep(long microsec)
Definition: signal.c:53
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
#define Assert(condition)
Definition: c.h:732
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:217
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ DescribeLockTag()

void DescribeLockTag ( StringInfo  buf,
const LOCKTAG tag 
)

Definition at line 1085 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().

1086 {
1087  switch ((LockTagType) tag->locktag_type)
1088  {
1089  case LOCKTAG_RELATION:
1090  appendStringInfo(buf,
1091  _("relation %u of database %u"),
1092  tag->locktag_field2,
1093  tag->locktag_field1);
1094  break;
1096  appendStringInfo(buf,
1097  _("extension of relation %u of database %u"),
1098  tag->locktag_field2,
1099  tag->locktag_field1);
1100  break;
1101  case LOCKTAG_PAGE:
1102  appendStringInfo(buf,
1103  _("page %u of relation %u of database %u"),
1104  tag->locktag_field3,
1105  tag->locktag_field2,
1106  tag->locktag_field1);
1107  break;
1108  case LOCKTAG_TUPLE:
1109  appendStringInfo(buf,
1110  _("tuple (%u,%u) of relation %u of database %u"),
1111  tag->locktag_field3,
1112  tag->locktag_field4,
1113  tag->locktag_field2,
1114  tag->locktag_field1);
1115  break;
1116  case LOCKTAG_TRANSACTION:
1117  appendStringInfo(buf,
1118  _("transaction %u"),
1119  tag->locktag_field1);
1120  break;
1122  appendStringInfo(buf,
1123  _("virtual transaction %d/%u"),
1124  tag->locktag_field1,
1125  tag->locktag_field2);
1126  break;
1128  appendStringInfo(buf,
1129  _("speculative token %u of transaction %u"),
1130  tag->locktag_field2,
1131  tag->locktag_field1);
1132  break;
1133  case LOCKTAG_OBJECT:
1134  appendStringInfo(buf,
1135  _("object %u of class %u of database %u"),
1136  tag->locktag_field3,
1137  tag->locktag_field2,
1138  tag->locktag_field1);
1139  break;
1140  case LOCKTAG_USERLOCK:
1141  /* reserved for old contrib code, now on pgfoundry */
1142  appendStringInfo(buf,
1143  _("user lock [%u,%u,%u]"),
1144  tag->locktag_field1,
1145  tag->locktag_field2,
1146  tag->locktag_field3);
1147  break;
1148  case LOCKTAG_ADVISORY:
1149  appendStringInfo(buf,
1150  _("advisory lock [%u,%u,%u,%u]"),
1151  tag->locktag_field1,
1152  tag->locktag_field2,
1153  tag->locktag_field3,
1154  tag->locktag_field4);
1155  break;
1156  default:
1157  appendStringInfo(buf,
1158  _("unrecognized locktag type %d"),
1159  (int) tag->locktag_type);
1160  break;
1161  }
1162 }
uint16 locktag_field4
Definition: lock.h:169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
uint32 locktag_field2
Definition: lock.h:167
uint8 locktag_type
Definition: lock.h:170
LockTagType
Definition: lock.h:138
uint32 locktag_field1
Definition: lock.h:166
#define _(x)
Definition: elog.c:84
uint32 locktag_field3
Definition: lock.h:168

◆ GetLockNameFromTagType()

const char* GetLockNameFromTagType ( uint16  locktag_type)

Definition at line 1170 of file lmgr.c.

References LOCKTAG_LAST_TYPE, and LockTagTypeNames.

Referenced by pgstat_get_wait_event().

1171 {
1172  if (locktag_type > LOCKTAG_LAST_TYPE)
1173  return "???";
1174  return LockTagTypeNames[locktag_type];
1175 }
#define LOCKTAG_LAST_TYPE
Definition: lock.h:152
const char *const LockTagTypeNames[]
Definition: lockfuncs.c:26

◆ LockDatabaseObject()

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

Definition at line 961 of file lmgr.c.

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

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

963 {
964  LOCKTAG tag;
965 
966  SET_LOCKTAG_OBJECT(tag,
967  MyDatabaseId,
968  classid,
969  objid,
970  objsubid);
971 
972  (void) LockAcquire(&tag, lockmode, false, false);
973 
974  /* Make sure syscaches are up-to-date with any changes we waited for */
976 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
void AcceptInvalidationMessages(void)
Definition: inval.c:681
Definition: lock.h:164
Oid MyDatabaseId
Definition: globals.c:85
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:253

◆ LockHasWaitersRelation()

bool LockHasWaitersRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 345 of file lmgr.c.

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

Referenced by count_nondeletable_pages().

346 {
347  LOCKTAG tag;
348 
350  relation->rd_lockInfo.lockRelId.dbId,
351  relation->rd_lockInfo.lockRelId.relId);
352 
353  return LockHasWaiters(&tag, lockmode, false);
354 }
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:86
bool LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:595
Oid relId
Definition: rel.h:37

◆ LockPage()

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

Definition at line 470 of file lmgr.c.

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

Referenced by ginInsertCleanup().

471 {
472  LOCKTAG tag;
473 
474  SET_LOCKTAG_PAGE(tag,
475  relation->rd_lockInfo.lockRelId.dbId,
476  relation->rd_lockInfo.lockRelId.relId,
477  blkno);
478 
479  (void) LockAcquire(&tag, lockmode, false, false);
480 }
LockRelId lockRelId
Definition: rel.h:43
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:199
Oid relId
Definition: rel.h:37

◆ LockRelation()

void LockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 216 of file lmgr.c.

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

Referenced by index_create().

217 {
218  LOCKTAG tag;
219  LOCALLOCK *locallock;
220  LockAcquireResult res;
221 
223  relation->rd_lockInfo.lockRelId.dbId,
224  relation->rd_lockInfo.lockRelId.relId);
225 
226  res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
227 
228  /*
229  * Now that we have the lock, check for invalidation messages; see notes
230  * in LockRelationOid.
231  */
232  if (res != LOCKACQUIRE_ALREADY_CLEAR)
233  {
235  MarkLockClear(locallock);
236  }
237 }
LockRelId lockRelId
Definition: rel.h:43
void AcceptInvalidationMessages(void)
Definition: inval.c:681
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:86
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1708
LockAcquireResult
Definition: lock.h:476
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:732
Oid relId
Definition: rel.h:37

◆ LockRelationForExtension()

void LockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 402 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(), gistvacuumscan(), pgstat_index(), RelationGetBufferForTuple(), revmap_physical_extend(), SpGistNewBuffer(), spgvacuumscan(), and vm_extend().

403 {
404  LOCKTAG tag;
405 
407  relation->rd_lockInfo.lockRelId.dbId,
408  relation->rd_lockInfo.lockRelId.relId);
409 
410  (void) LockAcquire(&tag, lockmode, false, false);
411 }
LockRelId lockRelId
Definition: rel.h:43
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:37

◆ LockRelationIdForSession()

void LockRelationIdForSession ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 369 of file lmgr.c.

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

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

370 {
371  LOCKTAG tag;
372 
373  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
374 
375  (void) LockAcquire(&tag, lockmode, true, false);
376 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
Oid relId
Definition: rel.h:37

◆ LockRelationOid()

void LockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

Definition at line 108 of file lmgr.c.

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

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

109 {
110  LOCKTAG tag;
111  LOCALLOCK *locallock;
112  LockAcquireResult res;
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  */
134  if (res != LOCKACQUIRE_ALREADY_CLEAR)
135  {
137  MarkLockClear(locallock);
138  }
139 }
void AcceptInvalidationMessages(void)
Definition: inval.c:681
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:89
Definition: lock.h:164
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1708
LockAcquireResult
Definition: lock.h:476
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:732

◆ LockSharedObject()

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

Definition at line 1002 of file lmgr.c.

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

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

1004 {
1005  LOCKTAG tag;
1006 
1007  SET_LOCKTAG_OBJECT(tag,
1008  InvalidOid,
1009  classid,
1010  objid,
1011  objsubid);
1012 
1013  (void) LockAcquire(&tag, lockmode, false, false);
1014 
1015  /* Make sure syscaches are up-to-date with any changes we waited for */
1017 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
void AcceptInvalidationMessages(void)
Definition: inval.c:681
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:253

◆ LockSharedObjectForSession()

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

Definition at line 1044 of file lmgr.c.

References InvalidOid, LockAcquire(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

1046 {
1047  LOCKTAG tag;
1048 
1049  SET_LOCKTAG_OBJECT(tag,
1050  InvalidOid,
1051  classid,
1052  objid,
1053  objsubid);
1054 
1055  (void) LockAcquire(&tag, lockmode, true, false);
1056 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:253

◆ LockTuple()

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

Definition at line 525 of file lmgr.c.

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

526 {
527  LOCKTAG tag;
528 
529  SET_LOCKTAG_TUPLE(tag,
530  relation->rd_lockInfo.lockRelId.dbId,
531  relation->rd_lockInfo.lockRelId.relId,
534 
535  (void) LockAcquire(&tag, lockmode, false, false);
536 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:208
LockRelId lockRelId
Definition: rel.h:43
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Oid relId
Definition: rel.h:37

◆ RelationExtensionLockWaiterCount()

int RelationExtensionLockWaiterCount ( Relation  relation)

Definition at line 437 of file lmgr.c.

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

Referenced by RelationAddExtraBlocks().

438 {
439  LOCKTAG tag;
440 
442  relation->rd_lockInfo.lockRelId.dbId,
443  relation->rd_lockInfo.lockRelId.relId);
444 
445  return LockWaiterCount(&tag);
446 }
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
int LockWaiterCount(const LOCKTAG *locktag)
Definition: lock.c:4467
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:37

◆ RelationInitLockInfo()

void RelationInitLockInfo ( Relation  relation)

Definition at line 71 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().

72 {
73  Assert(RelationIsValid(relation));
75 
76  relation->rd_lockInfo.lockRelId.relId = RelationGetRelid(relation);
77 
78  if (relation->rd_rel->relisshared)
80  else
82 }
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Form_pg_class rd_rel
Definition: rel.h:83
#define OidIsValid(objectId)
Definition: c.h:638
#define RelationIsValid(relation)
Definition: rel.h:392
LockInfoData rd_lockInfo
Definition: rel.h:86
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:732
#define RelationGetRelid(relation)
Definition: rel.h:419
Oid relId
Definition: rel.h:37

◆ SetLocktagRelationOid()

static void SetLocktagRelationOid ( LOCKTAG tag,
Oid  relid 
)
inlinestatic

Definition at line 89 of file lmgr.c.

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

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

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 }
unsigned int Oid
Definition: postgres_ext.h:31
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
Oid MyDatabaseId
Definition: globals.c:85
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:240
#define InvalidOid
Definition: postgres_ext.h:36

◆ SpeculativeInsertionLockAcquire()

uint32 SpeculativeInsertionLockAcquire ( TransactionId  xid)

Definition at line 739 of file lmgr.c.

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

Referenced by ExecInsert().

740 {
741  LOCKTAG tag;
742 
744 
745  /*
746  * Check for wrap-around. Zero means no token is held, so don't use that.
747  */
748  if (speculativeInsertionToken == 0)
750 
752 
753  (void) LockAcquire(&tag, ExclusiveLock, false, false);
754 
756 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:238
static uint32 speculativeInsertionToken
Definition: lmgr.c:46

◆ SpeculativeInsertionLockRelease()

void SpeculativeInsertionLockRelease ( TransactionId  xid)

Definition at line 765 of file lmgr.c.

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

Referenced by ExecInsert().

766 {
767  LOCKTAG tag;
768 
770 
771  LockRelease(&tag, ExclusiveLock, false);
772 }
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:238
static uint32 speculativeInsertionToken
Definition: lmgr.c:46
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884

◆ SpeculativeInsertionWait()

void SpeculativeInsertionWait ( TransactionId  xid,
uint32  token 
)

Definition at line 781 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().

782 {
783  LOCKTAG tag;
784 
785  SET_LOCKTAG_SPECULATIVE_INSERTION(tag, xid, token);
786 
788  Assert(token != 0);
789 
790  (void) LockAcquire(&tag, ShareLock, false, false);
791  LockRelease(&tag, ShareLock, false);
792 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
Definition: lock.h:164
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:238
#define Assert(condition)
Definition: c.h:732
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ UnlockDatabaseObject()

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

Definition at line 982 of file lmgr.c.

References LockRelease(), MyDatabaseId, and SET_LOCKTAG_OBJECT.

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

984 {
985  LOCKTAG tag;
986 
987  SET_LOCKTAG_OBJECT(tag,
988  MyDatabaseId,
989  classid,
990  objid,
991  objsubid);
992 
993  LockRelease(&tag, lockmode, false);
994 }
Definition: lock.h:164
Oid MyDatabaseId
Definition: globals.c:85
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:253
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884

◆ UnlockPage()

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

Definition at line 505 of file lmgr.c.

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

Referenced by ginInsertCleanup().

506 {
507  LOCKTAG tag;
508 
509  SET_LOCKTAG_PAGE(tag,
510  relation->rd_lockInfo.lockRelId.dbId,
511  relation->rd_lockInfo.lockRelId.relId,
512  blkno);
513 
514  LockRelease(&tag, lockmode, false);
515 }
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:199
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
Oid relId
Definition: rel.h:37

◆ UnlockRelation()

void UnlockRelation ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 282 of file lmgr.c.

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

Referenced by lazy_truncate_heap().

283 {
284  LOCKTAG tag;
285 
287  relation->rd_lockInfo.lockRelId.dbId,
288  relation->rd_lockInfo.lockRelId.relId);
289 
290  LockRelease(&tag, lockmode, false);
291 }
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:86
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
Oid relId
Definition: rel.h:37

◆ UnlockRelationForExtension()

void UnlockRelationForExtension ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 452 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(), gistvacuumscan(), pgstat_index(), RelationGetBufferForTuple(), revmap_physical_extend(), SpGistNewBuffer(), spgvacuumscan(), and vm_extend().

453 {
454  LOCKTAG tag;
455 
457  relation->rd_lockInfo.lockRelId.dbId,
458  relation->rd_lockInfo.lockRelId.relId);
459 
460  LockRelease(&tag, lockmode, false);
461 }
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:37

◆ UnlockRelationId()

void UnlockRelationId ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 184 of file lmgr.c.

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

Referenced by index_close(), and relation_close().

185 {
186  LOCKTAG tag;
187 
188  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
189 
190  LockRelease(&tag, lockmode, false);
191 }
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
Oid relId
Definition: rel.h:37

◆ UnlockRelationIdForSession()

void UnlockRelationIdForSession ( LockRelId relid,
LOCKMODE  lockmode 
)

Definition at line 382 of file lmgr.c.

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

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

383 {
384  LOCKTAG tag;
385 
386  SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
387 
388  LockRelease(&tag, lockmode, true);
389 }
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
Oid relId
Definition: rel.h:37

◆ UnlockRelationOid()

void UnlockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

◆ UnlockSharedObject()

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

Definition at line 1023 of file lmgr.c.

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

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

1025 {
1026  LOCKTAG tag;
1027 
1028  SET_LOCKTAG_OBJECT(tag,
1029  InvalidOid,
1030  classid,
1031  objid,
1032  objsubid);
1033 
1034  LockRelease(&tag, lockmode, false);
1035 }
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:253
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884

◆ UnlockSharedObjectForSession()

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

Definition at line 1062 of file lmgr.c.

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

1064 {
1065  LOCKTAG tag;
1066 
1067  SET_LOCKTAG_OBJECT(tag,
1068  InvalidOid,
1069  classid,
1070  objid,
1071  objsubid);
1072 
1073  LockRelease(&tag, lockmode, true);
1074 }
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:253
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884

◆ UnlockTuple()

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

Definition at line 562 of file lmgr.c.

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

563 {
564  LOCKTAG tag;
565 
566  SET_LOCKTAG_TUPLE(tag,
567  relation->rd_lockInfo.lockRelId.dbId,
568  relation->rd_lockInfo.lockRelId.relId,
571 
572  LockRelease(&tag, lockmode, false);
573 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:208
LockRelId lockRelId
Definition: rel.h:43
Oid dbId
Definition: rel.h:38
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:86
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Oid relId
Definition: rel.h:37

◆ WaitForLockers()

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

Definition at line 942 of file lmgr.c.

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

Referenced by DefineIndex(), and index_drop().

943 {
944  List *l;
945 
946  l = list_make1(&heaplocktag);
947  WaitForLockersMultiple(l, lockmode, progress);
948  list_free(l);
949 }
void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
Definition: lmgr.c:863
#define list_make1(x1)
Definition: pg_list.h:227
int progress
Definition: pgbench.c:232
void list_free(List *list)
Definition: list.c:1377
Definition: pg_list.h:50

◆ WaitForLockersMultiple()

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

Definition at line 863 of file lmgr.c.

References VirtualTransactionId::backendId, BackendIdGetProc(), GetLockConflicts(), lappend(), lfirst, list_free_deep(), list_length(), NIL, pgstat_progress_update_multi_param(), pgstat_progress_update_param(), PGPROC::pid, PROGRESS_WAITFOR_CURRENT_PID, PROGRESS_WAITFOR_DONE, PROGRESS_WAITFOR_TOTAL, values, VirtualTransactionIdIsValid, and VirtualXactLock().

Referenced by ReindexRelationConcurrently(), and WaitForLockers().

864 {
865  List *holders = NIL;
866  ListCell *lc;
867  int total = 0;
868  int done = 0;
869 
870  /* Done if no locks to wait for */
871  if (list_length(locktags) == 0)
872  return;
873 
874  /* Collect the transactions we need to wait on */
875  foreach(lc, locktags)
876  {
877  LOCKTAG *locktag = lfirst(lc);
878  int count;
879 
880  holders = lappend(holders,
881  GetLockConflicts(locktag, lockmode,
882  progress ? &count : NULL));
883  if (progress)
884  total += count;
885  }
886 
887  if (progress)
889 
890  /*
891  * Note: GetLockConflicts() never reports our own xid, hence we need not
892  * check for that. Also, prepared xacts are not reported, which is fine
893  * since they certainly aren't going to do anything anymore.
894  */
895 
896  /* Finally wait for each such transaction to complete */
897  foreach(lc, holders)
898  {
899  VirtualTransactionId *lockholders = lfirst(lc);
900 
901  while (VirtualTransactionIdIsValid(*lockholders))
902  {
903  /* If requested, publish who we're going to wait for. */
904  if (progress)
905  {
906  PGPROC *holder = BackendIdGetProc(lockholders->backendId);
907 
908  if (holder)
910  holder->pid);
911  }
912  VirtualXactLock(*lockholders, true);
913  lockholders++;
914 
915  if (progress)
917  }
918  }
919  if (progress)
920  {
921  const int index[] = {
925  };
926  const int64 values[] = {
927  0, 0, 0
928  };
929 
930  pgstat_progress_update_multi_param(3, index, values);
931  }
932 
933  list_free_deep(holders);
934 }
#define NIL
Definition: pg_list.h:65
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3220
Definition: lock.h:164
void list_free_deep(List *list)
Definition: list.c:1391
Definition: type.h:89
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4371
#define PROGRESS_WAITFOR_CURRENT_PID
Definition: progress.h:99
List * lappend(List *list, void *datum)
Definition: list.c:322
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:71
int progress
Definition: pgbench.c:232
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition: lock.c:2821
#define lfirst(lc)
Definition: pg_list.h:190
BackendId backendId
Definition: lock.h:65
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
Definition: pgstat.c:3242
#define PROGRESS_WAITFOR_DONE
Definition: progress.h:98
static int list_length(const List *l)
Definition: pg_list.h:169
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define PROGRESS_WAITFOR_TOTAL
Definition: progress.h:97
Definition: proc.h:95
Definition: pg_list.h:50
int pid
Definition: proc.h:109
PGPROC * BackendIdGetProc(int backendID)
Definition: sinvaladt.c:377

◆ XactLockTableDelete()

void XactLockTableDelete ( TransactionId  xid)

Definition at line 600 of file lmgr.c.

References ExclusiveLock, LockRelease(), and SET_LOCKTAG_TRANSACTION.

Referenced by CommitSubTransaction().

601 {
602  LOCKTAG tag;
603 
604  SET_LOCKTAG_TRANSACTION(tag, xid);
605 
606  LockRelease(&tag, ExclusiveLock, false);
607 }
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:217
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884

◆ XactLockTableInsert()

void XactLockTableInsert ( TransactionId  xid)

Definition at line 583 of file lmgr.c.

References ExclusiveLock, LockAcquire(), and SET_LOCKTAG_TRANSACTION.

Referenced by AssignTransactionId().

584 {
585  LOCKTAG tag;
586 
587  SET_LOCKTAG_TRANSACTION(tag, xid);
588 
589  (void) LockAcquire(&tag, ExclusiveLock, false, false);
590 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:217

◆ XactLockTableWait()

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

Definition at line 624 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(), heap_delete(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_update(), heapam_index_build_range_scan(), heapam_tuple_lock(), RelationFindReplTupleByIndex(), RelationFindReplTupleSeq(), and SnapBuildWaitSnapshot().

626 {
627  LOCKTAG tag;
630  bool first = true;
631 
632  /*
633  * If an operation is specified, set up our verbose error context
634  * callback.
635  */
636  if (oper != XLTW_None)
637  {
638  Assert(RelationIsValid(rel));
639  Assert(ItemPointerIsValid(ctid));
640 
641  info.rel = rel;
642  info.ctid = ctid;
643  info.oper = oper;
644 
646  callback.arg = &info;
647  callback.previous = error_context_stack;
649  }
650 
651  for (;;)
652  {
655 
656  SET_LOCKTAG_TRANSACTION(tag, xid);
657 
658  (void) LockAcquire(&tag, ShareLock, false, false);
659 
660  LockRelease(&tag, ShareLock, false);
661 
662  if (!TransactionIdIsInProgress(xid))
663  break;
664 
665  /*
666  * If the Xid belonged to a subtransaction, then the lock would have
667  * gone away as soon as it was finished; for correct tuple visibility,
668  * the right action is to wait on its parent transaction to go away.
669  * But instead of going levels up one by one, we can just wait for the
670  * topmost transaction to finish with the same end result, which also
671  * incurs less locktable traffic.
672  *
673  * Some uses of this function don't involve tuple visibility -- such
674  * as when building snapshots for logical decoding. It is possible to
675  * see a transaction in ProcArray before it registers itself in the
676  * locktable. The topmost transaction in that case is the same xid,
677  * so we try again after a short sleep. (Don't sleep the first time
678  * through, to avoid slowing down the normal case.)
679  */
680  if (!first)
681  pg_usleep(1000L);
682  first = false;
684  }
685 
686  if (oper != XLTW_None)
687  error_context_stack = callback.previous;
688 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
Relation rel
Definition: lmgr.c:58
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:708
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:150
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:985
Definition: lock.h:164
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
XLTW_Oper oper
Definition: lmgr.c:57
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:392
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
ItemPointer ctid
Definition: lmgr.c:59
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
static void XactLockTableWaitErrorCb(void *arg)
Definition: lmgr.c:799
#define Assert(condition)
Definition: c.h:732
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:217
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1884
#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

◆ XactLockTableWaitErrorCb()

static void XactLockTableWaitErrorCb ( void *  arg)
static

Definition at line 799 of file lmgr.c.

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

800 {
802 
803  /*
804  * We would like to print schema name too, but that would require a
805  * syscache lookup.
806  */
807  if (info->oper != XLTW_None &&
808  ItemPointerIsValid(info->ctid) && RelationIsValid(info->rel))
809  {
810  const char *cxt;
811 
812  switch (info->oper)
813  {
814  case XLTW_Update:
815  cxt = gettext_noop("while updating tuple (%u,%u) in relation \"%s\"");
816  break;
817  case XLTW_Delete:
818  cxt = gettext_noop("while deleting tuple (%u,%u) in relation \"%s\"");
819  break;
820  case XLTW_Lock:
821  cxt = gettext_noop("while locking tuple (%u,%u) in relation \"%s\"");
822  break;
823  case XLTW_LockUpdated:
824  cxt = gettext_noop("while locking updated version (%u,%u) of tuple in relation \"%s\"");
825  break;
826  case XLTW_InsertIndex:
827  cxt = gettext_noop("while inserting index tuple (%u,%u) in relation \"%s\"");
828  break;
830  cxt = gettext_noop("while checking uniqueness of tuple (%u,%u) in relation \"%s\"");
831  break;
832  case XLTW_FetchUpdated:
833  cxt = gettext_noop("while rechecking updated tuple (%u,%u) in relation \"%s\"");
834  break;
836  cxt = gettext_noop("while checking exclusion constraint on tuple (%u,%u) in relation \"%s\"");
837  break;
838 
839  default:
840  return;
841  }
842 
843  errcontext(cxt,
847  }
848 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
Relation rel
Definition: lmgr.c:58
#define gettext_noop(x)
Definition: c.h:1117
XLTW_Oper oper
Definition: lmgr.c:57
Definition: lmgr.h:26
#define RelationIsValid(relation)
Definition: rel.h:392
#define RelationGetRelationName(relation)
Definition: rel.h:453
ItemPointer ctid
Definition: lmgr.c:59
Definition: lmgr.h:29
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define errcontext
Definition: elog.h:183
void * arg
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98

Variable Documentation

◆ speculativeInsertionToken

uint32 speculativeInsertionToken = 0
static

Definition at line 46 of file lmgr.c.

Referenced by SpeculativeInsertionLockAcquire(), and SpeculativeInsertionLockRelease().