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 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)
 
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:44
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition: lock.c:4015
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
Definition: lock.c:598
int LOCKMODE
Definition: lockdefs.h:26
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:113
#define RelationGetRelationName(relation)
Definition: rel.h:491
#define WARNING
Definition: elog.h:40
uint8 locktag_lockmethodid
Definition: lock.h:171
#define elog(elevel,...)
Definition: elog.h:228
Oid relId
Definition: rel.h:38

◆ ConditionalLockPage()

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

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

505 {
506  LOCKTAG tag;
507 
508  SET_LOCKTAG_PAGE(tag,
509  relation->rd_lockInfo.lockRelId.dbId,
510  relation->rd_lockInfo.lockRelId.relId,
511  blkno);
512 
513  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
514 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:208
Oid relId
Definition: rel.h:38

◆ 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:44
void AcceptInvalidationMessages(void)
Definition: inval.c:684
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:113
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1799
LockAcquireResult
Definition: lock.h:487
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:771
Oid relId
Definition: rel.h:38

◆ 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:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:38

◆ 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:684
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:89
Definition: lock.h:164
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1799
LockAcquireResult
Definition: lock.h:487
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:771

◆ ConditionalLockTuple()

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

Definition at line 560 of file lmgr.c.

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

561 {
562  LOCKTAG tag;
563 
564  SET_LOCKTAG_TUPLE(tag,
565  relation->rd_lockInfo.lockRelId.dbId,
566  relation->rd_lockInfo.lockRelId.relId,
569 
570  return (LockAcquire(&tag, lockmode, false, true) != LOCKACQUIRE_NOT_AVAIL);
571 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Oid relId
Definition: rel.h:38

◆ ConditionalXactLockTableWait()

bool ConditionalXactLockTableWait ( TransactionId  xid)

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

713 {
714  LOCKTAG tag;
715  bool first = true;
716 
717  for (;;)
718  {
721 
722  SET_LOCKTAG_TRANSACTION(tag, xid);
723 
724  if (LockAcquire(&tag, ShareLock, false, true) == LOCKACQUIRE_NOT_AVAIL)
725  return false;
726 
727  LockRelease(&tag, ShareLock, false);
728 
729  if (!TransactionIdIsInProgress(xid))
730  break;
731 
732  /* See XactLockTableWait about this case */
733  if (!first)
734  pg_usleep(1000L);
735  first = false;
737  }
738 
739  return true;
740 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:150
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1320
Definition: lock.h:164
void pg_usleep(long microsec)
Definition: signal.c:53
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
#define Assert(condition)
Definition: c.h:800
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:226
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ DescribeLockTag()

void DescribeLockTag ( StringInfo  buf,
const LOCKTAG tag 
)

Definition at line 1100 of file lmgr.c.

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

1101 {
1102  switch ((LockTagType) tag->locktag_type)
1103  {
1104  case LOCKTAG_RELATION:
1105  appendStringInfo(buf,
1106  _("relation %u of database %u"),
1107  tag->locktag_field2,
1108  tag->locktag_field1);
1109  break;
1111  appendStringInfo(buf,
1112  _("extension of relation %u of database %u"),
1113  tag->locktag_field2,
1114  tag->locktag_field1);
1115  break;
1117  appendStringInfo(buf,
1118  _("pg_database.datfrozenxid of database %u"),
1119  tag->locktag_field1);
1120  break;
1121  case LOCKTAG_PAGE:
1122  appendStringInfo(buf,
1123  _("page %u of relation %u of database %u"),
1124  tag->locktag_field3,
1125  tag->locktag_field2,
1126  tag->locktag_field1);
1127  break;
1128  case LOCKTAG_TUPLE:
1129  appendStringInfo(buf,
1130  _("tuple (%u,%u) of relation %u of database %u"),
1131  tag->locktag_field3,
1132  tag->locktag_field4,
1133  tag->locktag_field2,
1134  tag->locktag_field1);
1135  break;
1136  case LOCKTAG_TRANSACTION:
1137  appendStringInfo(buf,
1138  _("transaction %u"),
1139  tag->locktag_field1);
1140  break;
1142  appendStringInfo(buf,
1143  _("virtual transaction %d/%u"),
1144  tag->locktag_field1,
1145  tag->locktag_field2);
1146  break;
1148  appendStringInfo(buf,
1149  _("speculative token %u of transaction %u"),
1150  tag->locktag_field2,
1151  tag->locktag_field1);
1152  break;
1153  case LOCKTAG_OBJECT:
1154  appendStringInfo(buf,
1155  _("object %u of class %u of database %u"),
1156  tag->locktag_field3,
1157  tag->locktag_field2,
1158  tag->locktag_field1);
1159  break;
1160  case LOCKTAG_USERLOCK:
1161  /* reserved for old contrib code, now on pgfoundry */
1162  appendStringInfo(buf,
1163  _("user lock [%u,%u,%u]"),
1164  tag->locktag_field1,
1165  tag->locktag_field2,
1166  tag->locktag_field3);
1167  break;
1168  case LOCKTAG_ADVISORY:
1169  appendStringInfo(buf,
1170  _("advisory lock [%u,%u,%u,%u]"),
1171  tag->locktag_field1,
1172  tag->locktag_field2,
1173  tag->locktag_field3,
1174  tag->locktag_field4);
1175  break;
1176  default:
1177  appendStringInfo(buf,
1178  _("unrecognized locktag type %d"),
1179  (int) tag->locktag_type);
1180  break;
1181  }
1182 }
uint16 locktag_field4
Definition: lock.h:169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
uint32 locktag_field2
Definition: lock.h:167
uint8 locktag_type
Definition: lock.h:170
LockTagType
Definition: lock.h:137
uint32 locktag_field1
Definition: lock.h:166
#define _(x)
Definition: elog.c:88
uint32 locktag_field3
Definition: lock.h:168

◆ GetLockNameFromTagType()

const char* GetLockNameFromTagType ( uint16  locktag_type)

Definition at line 1190 of file lmgr.c.

References LOCKTAG_LAST_TYPE, and LockTagTypeNames.

Referenced by pgstat_get_wait_event().

1191 {
1192  if (locktag_type > LOCKTAG_LAST_TYPE)
1193  return "???";
1194  return LockTagTypeNames[locktag_type];
1195 }
#define LOCKTAG_LAST_TYPE
Definition: lock.h:152
const char *const LockTagTypeNames[]
Definition: lockfuncs.c:29

◆ LockDatabaseFrozenIds()

void LockDatabaseFrozenIds ( LOCKMODE  lockmode)

Definition at line 469 of file lmgr.c.

References LockAcquire(), MyDatabaseId, and SET_LOCKTAG_DATABASE_FROZEN_IDS.

Referenced by vac_update_datfrozenxid().

470 {
471  LOCKTAG tag;
472 
474 
475  (void) LockAcquire(&tag, lockmode, false, false);
476 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Definition: lock.h:164
Oid MyDatabaseId
Definition: globals.c:85
#define SET_LOCKTAG_DATABASE_FROZEN_IDS(locktag, dboid)
Definition: lock.h:199

◆ LockDatabaseObject()

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

Definition at line 976 of file lmgr.c.

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

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

978 {
979  LOCKTAG tag;
980 
981  SET_LOCKTAG_OBJECT(tag,
982  MyDatabaseId,
983  classid,
984  objid,
985  objsubid);
986 
987  (void) LockAcquire(&tag, lockmode, false, false);
988 
989  /* Make sure syscaches are up-to-date with any changes we waited for */
991 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
void AcceptInvalidationMessages(void)
Definition: inval.c:684
Definition: lock.h:164
Oid MyDatabaseId
Definition: globals.c:85
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262

◆ 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:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:113
bool LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:634
Oid relId
Definition: rel.h:38

◆ LockPage()

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

Definition at line 485 of file lmgr.c.

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

Referenced by ginInsertCleanup().

486 {
487  LOCKTAG tag;
488 
489  SET_LOCKTAG_PAGE(tag,
490  relation->rd_lockInfo.lockRelId.dbId,
491  relation->rd_lockInfo.lockRelId.relId,
492  blkno);
493 
494  (void) LockAcquire(&tag, lockmode, false, false);
495 }
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:208
Oid relId
Definition: rel.h:38

◆ 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:44
void AcceptInvalidationMessages(void)
Definition: inval.c:684
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:113
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1799
LockAcquireResult
Definition: lock.h:487
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:771
Oid relId
Definition: rel.h:38

◆ 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:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:38

◆ 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:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
Oid relId
Definition: rel.h:38

◆ 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:684
static void SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
Definition: lmgr.c:89
Definition: lock.h:164
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1799
LockAcquireResult
Definition: lock.h:487
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:771

◆ LockSharedObject()

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

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

1019 {
1020  LOCKTAG tag;
1021 
1022  SET_LOCKTAG_OBJECT(tag,
1023  InvalidOid,
1024  classid,
1025  objid,
1026  objsubid);
1027 
1028  (void) LockAcquire(&tag, lockmode, false, false);
1029 
1030  /* Make sure syscaches are up-to-date with any changes we waited for */
1032 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
void AcceptInvalidationMessages(void)
Definition: inval.c:684
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262

◆ LockSharedObjectForSession()

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

Definition at line 1059 of file lmgr.c.

References InvalidOid, LockAcquire(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

1061 {
1062  LOCKTAG tag;
1063 
1064  SET_LOCKTAG_OBJECT(tag,
1065  InvalidOid,
1066  classid,
1067  objid,
1068  objsubid);
1069 
1070  (void) LockAcquire(&tag, lockmode, true, false);
1071 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262

◆ LockTuple()

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

Definition at line 540 of file lmgr.c.

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

541 {
542  LOCKTAG tag;
543 
544  SET_LOCKTAG_TUPLE(tag,
545  relation->rd_lockInfo.lockRelId.dbId,
546  relation->rd_lockInfo.lockRelId.relId,
549 
550  (void) LockAcquire(&tag, lockmode, false, false);
551 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217
LockRelId lockRelId
Definition: rel.h:44
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Oid relId
Definition: rel.h:38

◆ 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:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
int LockWaiterCount(const LOCKTAG *locktag)
Definition: lock.c:4557
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:38

◆ 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:44
Oid dbId
Definition: rel.h:39
Form_pg_class rd_rel
Definition: rel.h:110
#define OidIsValid(objectId)
Definition: c.h:706
#define RelationIsValid(relation)
Definition: rel.h:430
LockInfoData rd_lockInfo
Definition: rel.h:113
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:800
#define RelationGetRelid(relation)
Definition: rel.h:457
Oid relId
Definition: rel.h:38

◆ 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:236
#define InvalidOid
Definition: postgres_ext.h:36

◆ SpeculativeInsertionLockAcquire()

uint32 SpeculativeInsertionLockAcquire ( TransactionId  xid)

Definition at line 754 of file lmgr.c.

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

Referenced by ExecInsert().

755 {
756  LOCKTAG tag;
757 
759 
760  /*
761  * Check for wrap-around. Zero means no token is held, so don't use that.
762  */
763  if (speculativeInsertionToken == 0)
765 
767 
768  (void) LockAcquire(&tag, ExclusiveLock, false, false);
769 
771 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:247
static uint32 speculativeInsertionToken
Definition: lmgr.c:46

◆ SpeculativeInsertionLockRelease()

void SpeculativeInsertionLockRelease ( TransactionId  xid)

Definition at line 780 of file lmgr.c.

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

Referenced by ExecInsert().

781 {
782  LOCKTAG tag;
783 
785 
786  LockRelease(&tag, ExclusiveLock, false);
787 }
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:247
static uint32 speculativeInsertionToken
Definition: lmgr.c:46
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975

◆ SpeculativeInsertionWait()

void SpeculativeInsertionWait ( TransactionId  xid,
uint32  token 
)

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

797 {
798  LOCKTAG tag;
799 
800  SET_LOCKTAG_SPECULATIVE_INSERTION(tag, xid, token);
801 
803  Assert(token != 0);
804 
805  (void) LockAcquire(&tag, ShareLock, false, false);
806  LockRelease(&tag, ShareLock, false);
807 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
Definition: lock.h:164
#define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag, xid, token)
Definition: lock.h:247
#define Assert(condition)
Definition: c.h:800
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ UnlockDatabaseObject()

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

Definition at line 997 of file lmgr.c.

References LockRelease(), MyDatabaseId, and SET_LOCKTAG_OBJECT.

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

999 {
1000  LOCKTAG tag;
1001 
1002  SET_LOCKTAG_OBJECT(tag,
1003  MyDatabaseId,
1004  classid,
1005  objid,
1006  objsubid);
1007 
1008  LockRelease(&tag, lockmode, false);
1009 }
Definition: lock.h:164
Oid MyDatabaseId
Definition: globals.c:85
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975

◆ UnlockPage()

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

Definition at line 520 of file lmgr.c.

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

Referenced by ginInsertCleanup().

521 {
522  LOCKTAG tag;
523 
524  SET_LOCKTAG_PAGE(tag,
525  relation->rd_lockInfo.lockRelId.dbId,
526  relation->rd_lockInfo.lockRelId.relId,
527  blkno);
528 
529  LockRelease(&tag, lockmode, false);
530 }
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define SET_LOCKTAG_PAGE(locktag, dboid, reloid, blocknum)
Definition: lock.h:208
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
Oid relId
Definition: rel.h:38

◆ 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:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:181
LockInfoData rd_lockInfo
Definition: rel.h:113
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
Oid relId
Definition: rel.h:38

◆ 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:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
#define SET_LOCKTAG_RELATION_EXTEND(locktag, dboid, reloid)
Definition: lock.h:190
Oid relId
Definition: rel.h:38

◆ 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:39
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:1975
Oid relId
Definition: rel.h:38

◆ 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:39
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:1975
Oid relId
Definition: rel.h:38

◆ UnlockRelationOid()

void UnlockRelationOid ( Oid  relid,
LOCKMODE  lockmode 
)

◆ UnlockSharedObject()

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

Definition at line 1038 of file lmgr.c.

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

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

1040 {
1041  LOCKTAG tag;
1042 
1043  SET_LOCKTAG_OBJECT(tag,
1044  InvalidOid,
1045  classid,
1046  objid,
1047  objsubid);
1048 
1049  LockRelease(&tag, lockmode, false);
1050 }
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975

◆ UnlockSharedObjectForSession()

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

Definition at line 1077 of file lmgr.c.

References InvalidOid, LockRelease(), and SET_LOCKTAG_OBJECT.

Referenced by dbase_redo(), and movedb().

1079 {
1080  LOCKTAG tag;
1081 
1082  SET_LOCKTAG_OBJECT(tag,
1083  InvalidOid,
1084  classid,
1085  objid,
1086  objsubid);
1087 
1088  LockRelease(&tag, lockmode, true);
1089 }
Definition: lock.h:164
#define InvalidOid
Definition: postgres_ext.h:36
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition: lock.h:262
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975

◆ UnlockTuple()

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

Definition at line 577 of file lmgr.c.

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

578 {
579  LOCKTAG tag;
580 
581  SET_LOCKTAG_TUPLE(tag,
582  relation->rd_lockInfo.lockRelId.dbId,
583  relation->rd_lockInfo.lockRelId.relId,
586 
587  LockRelease(&tag, lockmode, false);
588 }
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition: lock.h:217
LockRelId lockRelId
Definition: rel.h:44
Oid dbId
Definition: rel.h:39
Definition: lock.h:164
LockInfoData rd_lockInfo
Definition: rel.h:113
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Oid relId
Definition: rel.h:38

◆ WaitForLockers()

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

Definition at line 957 of file lmgr.c.

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

Referenced by DefineIndex(), and index_drop().

958 {
959  List *l;
960 
961  l = list_make1(&heaplocktag);
962  WaitForLockersMultiple(l, lockmode, progress);
963  list_free(l);
964 }
void WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
Definition: lmgr.c:878
#define list_make1(x1)
Definition: pg_list.h:206
int progress
Definition: pgbench.c:235
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50

◆ WaitForLockersMultiple()

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

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

879 {
880  List *holders = NIL;
881  ListCell *lc;
882  int total = 0;
883  int done = 0;
884 
885  /* Done if no locks to wait for */
886  if (list_length(locktags) == 0)
887  return;
888 
889  /* Collect the transactions we need to wait on */
890  foreach(lc, locktags)
891  {
892  LOCKTAG *locktag = lfirst(lc);
893  int count;
894 
895  holders = lappend(holders,
896  GetLockConflicts(locktag, lockmode,
897  progress ? &count : NULL));
898  if (progress)
899  total += count;
900  }
901 
902  if (progress)
904 
905  /*
906  * Note: GetLockConflicts() never reports our own xid, hence we need not
907  * check for that. Also, prepared xacts are not reported, which is fine
908  * since they certainly aren't going to do anything anymore.
909  */
910 
911  /* Finally wait for each such transaction to complete */
912  foreach(lc, holders)
913  {
914  VirtualTransactionId *lockholders = lfirst(lc);
915 
916  while (VirtualTransactionIdIsValid(*lockholders))
917  {
918  /* If requested, publish who we're going to wait for. */
919  if (progress)
920  {
921  PGPROC *holder = BackendIdGetProc(lockholders->backendId);
922 
923  if (holder)
925  holder->pid);
926  }
927  VirtualXactLock(*lockholders, true);
928  lockholders++;
929 
930  if (progress)
932  }
933  }
934  if (progress)
935  {
936  const int index[] = {
940  };
941  const int64 values[] = {
942  0, 0, 0
943  };
944 
945  pgstat_progress_update_multi_param(3, index, values);
946  }
947 
948  list_free_deep(holders);
949 }
#define NIL
Definition: pg_list.h:65
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3377
Definition: lock.h:164
void list_free_deep(List *list)
Definition: list.c:1390
Definition: type.h:89
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4461
#define PROGRESS_WAITFOR_CURRENT_PID
Definition: progress.h:116
List * lappend(List *list, void *datum)
Definition: list.c:321
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:70
int progress
Definition: pgbench.c:235
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition: lock.c:2912
#define lfirst(lc)
Definition: pg_list.h:169
BackendId backendId
Definition: lock.h:64
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
Definition: pgstat.c:3399
#define PROGRESS_WAITFOR_DONE
Definition: progress.h:115
static int list_length(const List *l)
Definition: pg_list.h:149
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define PROGRESS_WAITFOR_TOTAL
Definition: progress.h:114
Definition: proc.h:120
Definition: pg_list.h:50
int pid
Definition: proc.h:145
PGPROC * BackendIdGetProc(int backendID)
Definition: sinvaladt.c:376

◆ XactLockTableDelete()

void XactLockTableDelete ( TransactionId  xid)

Definition at line 615 of file lmgr.c.

References ExclusiveLock, LockRelease(), and SET_LOCKTAG_TRANSACTION.

Referenced by CommitSubTransaction().

616 {
617  LOCKTAG tag;
618 
619  SET_LOCKTAG_TRANSACTION(tag, xid);
620 
621  LockRelease(&tag, ExclusiveLock, false);
622 }
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:226
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975

◆ XactLockTableInsert()

void XactLockTableInsert ( TransactionId  xid)

Definition at line 598 of file lmgr.c.

References ExclusiveLock, LockAcquire(), and SET_LOCKTAG_TRANSACTION.

Referenced by AssignTransactionId().

599 {
600  LOCKTAG tag;
601 
602  SET_LOCKTAG_TRANSACTION(tag, xid);
603 
604  (void) LockAcquire(&tag, ExclusiveLock, false, false);
605 }
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
#define ExclusiveLock
Definition: lockdefs.h:44
Definition: lock.h:164
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:226

◆ XactLockTableWait()

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

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

641 {
642  LOCKTAG tag;
645  bool first = true;
646 
647  /*
648  * If an operation is specified, set up our verbose error context
649  * callback.
650  */
651  if (oper != XLTW_None)
652  {
653  Assert(RelationIsValid(rel));
654  Assert(ItemPointerIsValid(ctid));
655 
656  info.rel = rel;
657  info.ctid = ctid;
658  info.oper = oper;
659 
661  callback.arg = &info;
662  callback.previous = error_context_stack;
664  }
665 
666  for (;;)
667  {
670 
671  SET_LOCKTAG_TRANSACTION(tag, xid);
672 
673  (void) LockAcquire(&tag, ShareLock, false, false);
674 
675  LockRelease(&tag, ShareLock, false);
676 
677  if (!TransactionIdIsInProgress(xid))
678  break;
679 
680  /*
681  * If the Xid belonged to a subtransaction, then the lock would have
682  * gone away as soon as it was finished; for correct tuple visibility,
683  * the right action is to wait on its parent transaction to go away.
684  * But instead of going levels up one by one, we can just wait for the
685  * topmost transaction to finish with the same end result, which also
686  * incurs less locktable traffic.
687  *
688  * Some uses of this function don't involve tuple visibility -- such
689  * as when building snapshots for logical decoding. It is possible to
690  * see a transaction in ProcArray before it registers itself in the
691  * locktable. The topmost transaction in that case is the same xid,
692  * so we try again after a short sleep. (Don't sleep the first time
693  * through, to avoid slowing down the normal case.)
694  */
695  if (!first)
696  pg_usleep(1000L);
697  first = false;
699  }
700 
701  if (oper != XLTW_None)
702  error_context_stack = callback.previous;
703 }
#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:747
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:150
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1320
Definition: lock.h:164
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
XLTW_Oper oper
Definition: lmgr.c:57
ErrorContextCallback * error_context_stack
Definition: elog.c:92
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: lmgr.h:26
#define RelationIsValid(relation)
Definition: rel.h:430
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:425
static void XactLockTableWaitErrorCb(void *arg)
Definition: lmgr.c:814
#define Assert(condition)
Definition: c.h:800
#define SET_LOCKTAG_TRANSACTION(locktag, xid)
Definition: lock.h:226
#define ShareLock
Definition: lockdefs.h:41
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
#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:382

◆ XactLockTableWaitErrorCb()

static void XactLockTableWaitErrorCb ( void *  arg)
static

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

815 {
817 
818  /*
819  * We would like to print schema name too, but that would require a
820  * syscache lookup.
821  */
822  if (info->oper != XLTW_None &&
823  ItemPointerIsValid(info->ctid) && RelationIsValid(info->rel))
824  {
825  const char *cxt;
826 
827  switch (info->oper)
828  {
829  case XLTW_Update:
830  cxt = gettext_noop("while updating tuple (%u,%u) in relation \"%s\"");
831  break;
832  case XLTW_Delete:
833  cxt = gettext_noop("while deleting tuple (%u,%u) in relation \"%s\"");
834  break;
835  case XLTW_Lock:
836  cxt = gettext_noop("while locking tuple (%u,%u) in relation \"%s\"");
837  break;
838  case XLTW_LockUpdated:
839  cxt = gettext_noop("while locking updated version (%u,%u) of tuple in relation \"%s\"");
840  break;
841  case XLTW_InsertIndex:
842  cxt = gettext_noop("while inserting index tuple (%u,%u) in relation \"%s\"");
843  break;
845  cxt = gettext_noop("while checking uniqueness of tuple (%u,%u) in relation \"%s\"");
846  break;
847  case XLTW_FetchUpdated:
848  cxt = gettext_noop("while rechecking updated tuple (%u,%u) in relation \"%s\"");
849  break;
851  cxt = gettext_noop("while checking exclusion constraint on tuple (%u,%u) in relation \"%s\"");
852  break;
853 
854  default:
855  return;
856  }
857 
858  errcontext(cxt,
862  }
863 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
Relation rel
Definition: lmgr.c:58
#define gettext_noop(x)
Definition: c.h:1193
XLTW_Oper oper
Definition: lmgr.c:57
Definition: lmgr.h:26
#define RelationIsValid(relation)
Definition: rel.h:430
#define RelationGetRelationName(relation)
Definition: rel.h:491
ItemPointer ctid
Definition: lmgr.c:59
Definition: lmgr.h:29
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define errcontext
Definition: elog.h:199
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().