PostgreSQL Source Code git master
Loading...
Searching...
No Matches
standby.h File Reference
Include dependency graph for standby.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RunningTransactionsData
 

Macros

#define NUM_RECOVERY_CONFLICT_REASONS   (RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK + 1)
 
#define MinSizeOfXactRunningXacts   offsetof(xl_running_xacts, xids)
 

Typedefs

typedef struct RunningTransactionsData RunningTransactionsData
 
typedef RunningTransactionsDataRunningTransactions
 

Enumerations

enum  RecoveryConflictReason {
  RECOVERY_CONFLICT_DATABASE , RECOVERY_CONFLICT_TABLESPACE , RECOVERY_CONFLICT_LOCK , RECOVERY_CONFLICT_SNAPSHOT ,
  RECOVERY_CONFLICT_LOGICALSLOT , RECOVERY_CONFLICT_BUFFERPIN , RECOVERY_CONFLICT_STARTUP_DEADLOCK , RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK
}
 
enum  subxids_array_status { SUBXIDS_IN_ARRAY , SUBXIDS_MISSING , SUBXIDS_IN_SUBTRANS }
 

Functions

void InitRecoveryTransactionEnvironment (void)
 
void ShutdownRecoveryTransactionEnvironment (void)
 
void ResolveRecoveryConflictWithSnapshot (TransactionId snapshotConflictHorizon, bool isCatalogRel, RelFileLocator locator)
 
void ResolveRecoveryConflictWithSnapshotFullXid (FullTransactionId snapshotConflictHorizon, bool isCatalogRel, RelFileLocator locator)
 
void ResolveRecoveryConflictWithTablespace (Oid tsid)
 
void ResolveRecoveryConflictWithDatabase (Oid dbid)
 
void ResolveRecoveryConflictWithLock (LOCKTAG locktag, bool logging_conflict)
 
void ResolveRecoveryConflictWithBufferPin (void)
 
void CheckRecoveryConflictDeadlock (void)
 
void StandbyDeadLockHandler (void)
 
void StandbyTimeoutHandler (void)
 
void StandbyLockTimeoutHandler (void)
 
void LogRecoveryConflict (RecoveryConflictReason reason, TimestampTz wait_start, TimestampTz now, VirtualTransactionId *wait_list, bool still_waiting)
 
void StandbyAcquireAccessExclusiveLock (TransactionId xid, Oid dbOid, Oid relOid)
 
void StandbyReleaseLockTree (TransactionId xid, int nsubxids, TransactionId *subxids)
 
void StandbyReleaseAllLocks (void)
 
void StandbyReleaseOldLocks (TransactionId oldxid)
 
void LogAccessExclusiveLock (Oid dbOid, Oid relOid)
 
void LogAccessExclusiveLockPrepare (void)
 
XLogRecPtr LogStandbySnapshot (void)
 
void LogStandbyInvalidations (int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInitFileInval)
 

Variables

PGDLLIMPORT int max_standby_archive_delay
 
PGDLLIMPORT int max_standby_streaming_delay
 
PGDLLIMPORT bool log_recovery_conflict_waits
 

Macro Definition Documentation

◆ MinSizeOfXactRunningXacts

#define MinSizeOfXactRunningXacts   offsetof(xl_running_xacts, xids)

Definition at line 101 of file standby.h.

◆ NUM_RECOVERY_CONFLICT_REASONS

#define NUM_RECOVERY_CONFLICT_REASONS   (RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK + 1)

Definition at line 64 of file standby.h.

Typedef Documentation

◆ RunningTransactions

◆ RunningTransactionsData

Enumeration Type Documentation

◆ RecoveryConflictReason

Enumerator
RECOVERY_CONFLICT_DATABASE 
RECOVERY_CONFLICT_TABLESPACE 
RECOVERY_CONFLICT_LOCK 
RECOVERY_CONFLICT_SNAPSHOT 
RECOVERY_CONFLICT_LOGICALSLOT 
RECOVERY_CONFLICT_BUFFERPIN 
RECOVERY_CONFLICT_STARTUP_DEADLOCK 
RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK 

Definition at line 28 of file standby.h.

29{
30 /* Backend is connected to a database that is being dropped */
32
33 /* Backend is using a tablespace that is being dropped */
35
36 /* Backend is holding a lock that is blocking recovery */
38
39 /* Backend is holding a snapshot that is blocking recovery */
41
42 /* Backend is using a logical replication slot that must be invalidated */
44
45 /* Backend is holding a pin on a buffer that is blocking recovery */
47
48 /*
49 * The backend is requested to check for deadlocks. The startup process
50 * doesn't check for deadlock directly, because we want to kill one of the
51 * other backends instead of the startup process.
52 */
54
55 /*
56 * Like RECOVERY_CONFLICT_STARTUP_DEADLOCK is, but the suspected deadlock
57 * involves a buffer pin that some other backend is holding. That needs
58 * special checking because the normal deadlock detector doesn't track the
59 * buffer pins.
60 */
RecoveryConflictReason
Definition standby.h:29
@ RECOVERY_CONFLICT_TABLESPACE
Definition standby.h:34
@ RECOVERY_CONFLICT_SNAPSHOT
Definition standby.h:40
@ RECOVERY_CONFLICT_LOCK
Definition standby.h:37
@ RECOVERY_CONFLICT_DATABASE
Definition standby.h:31
@ RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition standby.h:53
@ RECOVERY_CONFLICT_BUFFERPIN
Definition standby.h:46
@ RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK
Definition standby.h:61
@ RECOVERY_CONFLICT_LOGICALSLOT
Definition standby.h:43

◆ subxids_array_status

Enumerator
SUBXIDS_IN_ARRAY 
SUBXIDS_MISSING 
SUBXIDS_IN_SUBTRANS 

Definition at line 116 of file standby.h.

117{
118 SUBXIDS_IN_ARRAY, /* xids array includes all running subxids */
119 SUBXIDS_MISSING, /* snapshot overflowed, subxids are missing */
120 SUBXIDS_IN_SUBTRANS, /* subxids are not included in 'xids', but
121 * pg_subtrans is fully up-to-date */
subxids_array_status
Definition standby.h:117
@ SUBXIDS_IN_SUBTRANS
Definition standby.h:120
@ SUBXIDS_MISSING
Definition standby.h:119
@ SUBXIDS_IN_ARRAY
Definition standby.h:118

Function Documentation

◆ CheckRecoveryConflictDeadlock()

void CheckRecoveryConflictDeadlock ( void  )
extern

Definition at line 906 of file standby.c.

907{
908 Assert(!InRecovery); /* do not call in Startup process */
909
911 return;
912
913 /*
914 * Error message should match ProcessInterrupts() but we avoid calling
915 * that because we aren't handling an interrupt at this point. Note that
916 * we only cancel the current transaction here, so if we are in a
917 * subtransaction and the pin is held by a parent, then the Startup
918 * process will continue to wait even though we have avoided deadlock.
919 */
922 errmsg("canceling statement due to conflict with recovery"),
923 errdetail("User transaction caused buffer deadlock with recovery.")));
924}
bool HoldingBufferPinThatDelaysRecovery(void)
Definition bufmgr.c:6684
#define Assert(condition)
Definition c.h:945
int errcode(int sqlerrcode)
Definition elog.c:874
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
static char * errmsg
#define ERRCODE_T_R_DEADLOCK_DETECTED
Definition pgbench.c:78
bool InRecovery
Definition xlogutils.c:50

References Assert, ereport, errcode(), ERRCODE_T_R_DEADLOCK_DETECTED, errdetail(), errmsg, ERROR, HoldingBufferPinThatDelaysRecovery(), and InRecovery.

Referenced by ProcSleep().

◆ InitRecoveryTransactionEnvironment()

void InitRecoveryTransactionEnvironment ( void  )
extern

Definition at line 96 of file standby.c.

97{
100
101 Assert(RecoveryLockHash == NULL); /* don't run this twice */
102
103 /*
104 * Initialize the hash tables for tracking the locks held by each
105 * transaction.
106 */
107 hash_ctl.keysize = sizeof(xl_standby_lock);
108 hash_ctl.entrysize = sizeof(RecoveryLockEntry);
109 RecoveryLockHash = hash_create("RecoveryLockHash",
110 64,
111 &hash_ctl,
113 hash_ctl.keysize = sizeof(TransactionId);
114 hash_ctl.entrysize = sizeof(RecoveryLockXidEntry);
115 RecoveryLockXidHash = hash_create("RecoveryLockXidHash",
116 64,
117 &hash_ctl,
119
120 /*
121 * Initialize shared invalidation management for Startup process, being
122 * careful to register ourselves as a sendOnly process so we don't need to
123 * read messages, nor will we get signaled when the queue starts filling
124 * up.
125 */
127
128 /*
129 * Lock a virtual transaction id for Startup process.
130 *
131 * We need to do GetNextLocalTransactionId() because
132 * SharedInvalBackendInit() leaves localTransactionId invalid and the lock
133 * manager doesn't like that at all.
134 *
135 * Note that we don't need to run XactLockTableInsert() because nobody
136 * needs to wait on xids. That sounds a little strange, but table locks
137 * are held by vxids and row level locks are held by xids. All queries
138 * hold AccessShareLocks so never block while we write or lock new rows.
139 */
144
146}
uint32 TransactionId
Definition c.h:738
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
ProcNumber MyProcNumber
Definition globals.c:90
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
void VirtualXactLockTableInsert(VirtualTransactionId vxid)
Definition lock.c:4619
static int fb(int x)
void SharedInvalBackendInit(bool sendOnly)
Definition sinvaladt.c:272
LocalTransactionId GetNextLocalTransactionId(void)
Definition sinvaladt.c:701
PGPROC * MyProc
Definition proc.c:68
static HTAB * RecoveryLockXidHash
Definition standby.c:67
static HTAB * RecoveryLockHash
Definition standby.c:66
struct PGPROC::@133 vxid
ProcNumber procNumber
Definition proc.h:223
LocalTransactionId localTransactionId
Definition lock.h:64
ProcNumber procNumber
Definition lock.h:63
HotStandbyState standbyState
Definition xlogutils.c:53
@ STANDBY_INITIALIZED
Definition xlogutils.h:53

References Assert, fb(), GetNextLocalTransactionId(), HASH_BLOBS, hash_create(), HASH_ELEM, VirtualTransactionId::localTransactionId, MyProc, MyProcNumber, VirtualTransactionId::procNumber, PGPROC::procNumber, RecoveryLockHash, RecoveryLockXidHash, SharedInvalBackendInit(), STANDBY_INITIALIZED, standbyState, VirtualXactLockTableInsert(), and PGPROC::vxid.

Referenced by StartupXLOG().

◆ LogAccessExclusiveLock()

void LogAccessExclusiveLock ( Oid  dbOid,
Oid  relOid 
)
extern

Definition at line 1433 of file standby.c.

1434{
1436
1438
1439 xlrec.dbOid = dbOid;
1440 xlrec.relOid = relOid;
1441
1444}
static void LogAccessExclusiveLocks(int nlocks, xl_standby_lock *locks)
Definition standby.c:1415
TransactionId xid
Definition lockdefs.h:53
TransactionId GetCurrentTransactionId(void)
Definition xact.c:456
int MyXactFlags
Definition xact.c:138
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK
Definition xact.h:109

References fb(), GetCurrentTransactionId(), LogAccessExclusiveLocks(), MyXactFlags, XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK, and xl_standby_lock::xid.

Referenced by LockAcquireExtended().

◆ LogAccessExclusiveLockPrepare()

void LogAccessExclusiveLockPrepare ( void  )
extern

Definition at line 1450 of file standby.c.

1451{
1452 /*
1453 * Ensure that a TransactionId has been assigned to this transaction, for
1454 * two reasons, both related to lock release on the standby. First, we
1455 * must assign an xid so that RecordTransactionCommit() and
1456 * RecordTransactionAbort() do not optimise away the transaction
1457 * completion record which recovery relies upon to release locks. It's a
1458 * hack, but for a corner case not worth adding code for into the main
1459 * commit path. Second, we must assign an xid before the lock is recorded
1460 * in shared memory, otherwise a concurrently executing
1461 * GetRunningTransactionLocks() might see a lock associated with an
1462 * InvalidTransactionId which we later assert cannot happen.
1463 */
1465}

References fb(), and GetCurrentTransactionId().

Referenced by LockAcquireExtended().

◆ LogRecoveryConflict()

void LogRecoveryConflict ( RecoveryConflictReason  reason,
TimestampTz  wait_start,
TimestampTz  now,
VirtualTransactionId wait_list,
bool  still_waiting 
)
extern

Definition at line 275 of file standby.c.

278{
279 long secs;
280 int usecs;
281 long msecs;
283 int nprocs = 0;
284
285 /*
286 * There must be no conflicting processes when the recovery conflict has
287 * already been resolved.
288 */
290
292 msecs = secs * 1000 + usecs / 1000;
293 usecs = usecs % 1000;
294
295 if (wait_list)
296 {
298
299 /* Construct a string of list of the conflicting processes */
302 {
303 PGPROC *proc = ProcNumberGetProc(vxids->procNumber);
304
305 /* proc can be NULL if the target backend is not active */
306 if (proc)
307 {
308 if (nprocs == 0)
309 {
311 appendStringInfo(&buf, "%d", proc->pid);
312 }
313 else
314 appendStringInfo(&buf, ", %d", proc->pid);
315
316 nprocs++;
317 }
318
319 vxids++;
320 }
321 }
322
323 /*
324 * If wait_list is specified, report the list of PIDs of active
325 * conflicting backends in a detail message. Note that if all the backends
326 * in the list are not active, no detail message is logged.
327 */
328 if (still_waiting)
329 {
330 ereport(LOG,
331 errmsg("recovery still waiting after %ld.%03d ms: %s",
333 nprocs > 0 ? errdetail_log_plural("Conflicting process: %s.",
334 "Conflicting processes: %s.",
335 nprocs, buf.data) : 0);
336 }
337 else
338 {
339 ereport(LOG,
340 errmsg("recovery finished waiting after %ld.%03d ms: %s",
342 }
343
344 if (nprocs > 0)
345 pfree(buf.data);
346}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1712
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1600
#define LOG
Definition elog.h:31
int int int int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
#define VirtualTransactionIdIsValid(vxid)
Definition lock.h:69
void pfree(void *pointer)
Definition mcxt.c:1616
static char buf[DEFAULT_XLOG_SEG_SIZE]
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
Definition procarray.c:3098
static const char * get_recovery_conflict_desc(RecoveryConflictReason reason)
Definition standby.c:1494
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Definition proc.h:176
int pid
Definition proc.h:194

References appendStringInfo(), Assert, buf, ereport, errdetail_log_plural(), errmsg, fb(), get_recovery_conflict_desc(), initStringInfo(), LOG, now(), pfree(), PGPROC::pid, ProcNumberGetProc(), TimestampDifference(), and VirtualTransactionIdIsValid.

Referenced by LockBufferForCleanup(), ProcSleep(), and ResolveRecoveryConflictWithVirtualXIDs().

◆ LogStandbyInvalidations()

void LogStandbyInvalidations ( int  nmsgs,
SharedInvalidationMessage msgs,
bool  relcacheInitFileInval 
)
extern

Definition at line 1472 of file standby.c.

1474{
1476
1477 /* prepare record */
1478 memset(&xlrec, 0, sizeof(xlrec));
1479 xlrec.dbId = MyDatabaseId;
1481 xlrec.relcacheInitFileInval = relcacheInitFileInval;
1482 xlrec.nmsgs = nmsgs;
1483
1484 /* perform insertion */
1487 XLogRegisterData(msgs,
1488 nmsgs * sizeof(SharedInvalidationMessage));
1490}
Oid MyDatabaseTableSpace
Definition globals.c:96
Oid MyDatabaseId
Definition globals.c:94
#define XLOG_INVALIDATIONS
Definition standbydefs.h:36
#define MinSizeOfInvalidations
Definition standbydefs.h:72
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition xloginsert.c:479
void XLogRegisterData(const void *data, uint32 len)
Definition xloginsert.c:369
void XLogBeginInsert(void)
Definition xloginsert.c:153

References fb(), MinSizeOfInvalidations, MyDatabaseId, MyDatabaseTableSpace, XLOG_INVALIDATIONS, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by RecordTransactionCommit().

◆ LogStandbySnapshot()

XLogRecPtr LogStandbySnapshot ( void  )
extern

Definition at line 1283 of file standby.c.

1284{
1286 RunningTransactions running;
1287 xl_standby_lock *locks;
1288 int nlocks;
1289 bool logical_decoding_enabled = IsLogicalDecodingEnabled();
1290
1292
1293#ifdef USE_INJECTION_POINTS
1294 if (IS_INJECTION_POINT_ATTACHED("skip-log-running-xacts"))
1295 {
1296 /*
1297 * This record could move slot's xmin forward during decoding, leading
1298 * to unpredictable results, so skip it when requested by the test.
1299 */
1300 return GetInsertRecPtr();
1301 }
1302#endif
1303
1304 /*
1305 * Get details of any AccessExclusiveLocks being held at the moment.
1306 */
1307 locks = GetRunningTransactionLocks(&nlocks);
1308 if (nlocks > 0)
1309 LogAccessExclusiveLocks(nlocks, locks);
1310 pfree(locks);
1311
1312 /*
1313 * Log details of all in-progress transactions. This should be the last
1314 * record we write, because standby will open up when it sees this.
1315 */
1316 running = GetRunningTransactionData();
1317
1318 /*
1319 * GetRunningTransactionData() acquired ProcArrayLock, we must release it.
1320 * For Hot Standby this can be done before inserting the WAL record
1321 * because ProcArrayApplyRecoveryInfo() rechecks the commit status using
1322 * the clog. For logical decoding, though, the lock can't be released
1323 * early because the clog might be "in the future" from the POV of the
1324 * historic snapshot. This would allow for situations where we're waiting
1325 * for the end of a transaction listed in the xl_running_xacts record
1326 * which, according to the WAL, has committed before the xl_running_xacts
1327 * record. Fortunately this routine isn't executed frequently, and it's
1328 * only a shared lock.
1329 */
1330 if (!logical_decoding_enabled)
1332
1333 recptr = LogCurrentRunningXacts(running);
1334
1335 /* Release lock if we kept it longer ... */
1336 if (logical_decoding_enabled)
1338
1339 /* GetRunningTransactionData() acquired XidGenLock, we must release it */
1341
1342 return recptr;
1343}
#define IS_INJECTION_POINT_ATTACHED(name)
xl_standby_lock * GetRunningTransactionLocks(int *nlocks)
Definition lock.c:4170
bool IsLogicalDecodingEnabled(void)
Definition logicalctl.c:205
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
RunningTransactions GetRunningTransactionData(void)
Definition procarray.c:2636
static XLogRecPtr LogCurrentRunningXacts(RunningTransactions CurrRunningXacts)
Definition standby.c:1355
XLogRecPtr GetInsertRecPtr(void)
Definition xlog.c:6592
#define XLogStandbyInfoActive()
Definition xlog.h:125
uint64 XLogRecPtr
Definition xlogdefs.h:21

References Assert, fb(), GetInsertRecPtr(), GetRunningTransactionData(), GetRunningTransactionLocks(), IS_INJECTION_POINT_ATTACHED, IsLogicalDecodingEnabled(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), LWLockRelease(), pfree(), and XLogStandbyInfoActive.

Referenced by BackgroundWriterMain(), CreateCheckPoint(), pg_log_standby_snapshot(), ReplicationSlotReserveWal(), and SnapBuildWaitSnapshot().

◆ ResolveRecoveryConflictWithBufferPin()

void ResolveRecoveryConflictWithBufferPin ( void  )
extern

Definition at line 794 of file standby.c.

795{
797
799
801
802 if (GetCurrentTimestamp() >= ltime && ltime != 0)
803 {
804 /*
805 * We're already behind, so clear a path as quickly as possible.
806 */
808 }
809 else
810 {
811 /*
812 * Wake up at ltime, and check for deadlocks as well if we will be
813 * waiting longer than deadlock_timeout
814 */
816 int cnt = 0;
817
818 if (ltime != 0)
819 {
821 timeouts[cnt].type = TMPARAM_AT;
822 timeouts[cnt].fin_time = ltime;
823 cnt++;
824 }
825
828 timeouts[cnt].type = TMPARAM_AFTER;
829 timeouts[cnt].delay_ms = DeadlockTimeout;
830 cnt++;
831
833 }
834
835 /*
836 * Wait to be signaled by UnpinBuffer() or for the wait to be interrupted
837 * by one of the timeouts established above.
838 *
839 * We assume that only UnpinBuffer() and the timeout requests established
840 * above can wake us up here. WakeupRecovery() called by walreceiver or
841 * SIGHUP signal handler, etc cannot do that because it uses the different
842 * latch from that ProcWaitForSignal() waits on.
843 */
845
849 {
850 /*
851 * Send out a request for hot-standby backends to check themselves for
852 * deadlocks.
853 *
854 * XXX The subsequent ResolveRecoveryConflictWithBufferPin() will wait
855 * to be signaled by UnpinBuffer() again and send a request for
856 * deadlocks check if deadlock_timeout happens. This causes the
857 * request to continue to be sent every deadlock_timeout until the
858 * buffer is unpinned or ltime is reached. This would increase the
859 * workload in the startup process and backends. In practice it may
860 * not be so harmful because the period that the buffer is kept pinned
861 * is basically no so long. But we should fix this?
862 */
864 }
865
866 /*
867 * Clear any timeout requests established above. We assume here that the
868 * Startup process doesn't have any other timeouts than what this function
869 * uses. If that stops being true, we could cancel the timeouts
870 * individually, but that'd be slower.
871 */
875}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1636
int64 TimestampTz
Definition timestamp.h:39
int DeadlockTimeout
Definition proc.c:59
void ProcWaitForSignal(uint32 wait_event_info)
Definition proc.c:1991
static volatile sig_atomic_t got_standby_deadlock_timeout
Definition standby.c:70
static TimestampTz GetStandbyLimitTime(void)
Definition standby.c:202
static volatile sig_atomic_t got_standby_delay_timeout
Definition standby.c:71
static void SendRecoveryConflictWithBufferPin(RecoveryConflictReason reason)
Definition standby.c:878
void disable_all_timeouts(bool keep_indicators)
Definition timeout.c:751
void enable_timeouts(const EnableTimeoutParams *timeouts, int count)
Definition timeout.c:630
@ STANDBY_DEADLOCK_TIMEOUT
Definition timeout.h:30
@ STANDBY_TIMEOUT
Definition timeout.h:31
@ TMPARAM_AT
Definition timeout.h:54
@ TMPARAM_AFTER
Definition timeout.h:53
#define InHotStandby
Definition xlogutils.h:60

References Assert, DeadlockTimeout, disable_all_timeouts(), enable_timeouts(), fb(), GetCurrentTimestamp(), GetStandbyLimitTime(), got_standby_deadlock_timeout, got_standby_delay_timeout, EnableTimeoutParams::id, InHotStandby, ProcWaitForSignal(), RECOVERY_CONFLICT_BUFFERPIN, RECOVERY_CONFLICT_BUFFERPIN_DEADLOCK, SendRecoveryConflictWithBufferPin(), STANDBY_DEADLOCK_TIMEOUT, STANDBY_TIMEOUT, TMPARAM_AFTER, and TMPARAM_AT.

Referenced by LockBufferForCleanup().

◆ ResolveRecoveryConflictWithDatabase()

void ResolveRecoveryConflictWithDatabase ( Oid  dbid)
extern

Definition at line 571 of file standby.c.

572{
573 /*
574 * We don't do ResolveRecoveryConflictWithVirtualXIDs() here since that
575 * only waits for transactions and completely idle sessions would block
576 * us. This is rare enough that we do this as simply as possible: no wait,
577 * just force them off immediately.
578 *
579 * No locking is required here because we already acquired
580 * AccessExclusiveLock. Anybody trying to connect while we do this will
581 * block during InitPostgres() and then disconnect when they see the
582 * database has been removed.
583 */
584 while (CountDBBackends(dbid) > 0)
585 {
587
588 /*
589 * Wait awhile for them to die so that we avoid flooding an
590 * unresponsive backend when system is heavily loaded.
591 */
592 pg_usleep(10000);
593 }
594}
void SignalRecoveryConflictWithDatabase(Oid databaseid, RecoveryConflictReason reason)
Definition procarray.c:3529
int CountDBBackends(Oid databaseid)
Definition procarray.c:3629
void pg_usleep(long microsec)
Definition signal.c:53

References CountDBBackends(), pg_usleep(), RECOVERY_CONFLICT_DATABASE, and SignalRecoveryConflictWithDatabase().

Referenced by dbase_redo().

◆ ResolveRecoveryConflictWithLock()

void ResolveRecoveryConflictWithLock ( LOCKTAG  locktag,
bool  logging_conflict 
)
extern

Definition at line 625 of file standby.c.

626{
629
631
634
635 /*
636 * Update waitStart if first time through after the startup process
637 * started waiting for the lock. It should not be updated every time
638 * ResolveRecoveryConflictWithLock() is called during the wait.
639 *
640 * Use the current time obtained for comparison with ltime as waitStart
641 * (i.e., the time when this process started waiting for the lock). Since
642 * getting the current time newly can cause overhead, we reuse the
643 * already-obtained time to avoid that overhead.
644 *
645 * Note that waitStart is updated without holding the lock table's
646 * partition lock, to avoid the overhead by additional lock acquisition.
647 * This can cause "waitstart" in pg_locks to become NULL for a very short
648 * period of time after the wait started even though "granted" is false.
649 * This is OK in practice because we can assume that users are likely to
650 * look at "waitstart" when waiting for the lock for a long time.
651 */
654
655 if (now >= ltime && ltime != 0)
656 {
657 /*
658 * We're already behind, so clear a path as quickly as possible.
659 */
661
663
664 /*
665 * Prevent ResolveRecoveryConflictWithVirtualXIDs() from reporting
666 * "waiting" in PS display by disabling its argument report_waiting
667 * because the caller, WaitOnLock(), has already reported that.
668 */
671 PG_WAIT_LOCK | locktag.locktag_type,
672 false);
673 }
674 else
675 {
676 /*
677 * Wait (or wait again) until ltime, and check for deadlocks as well
678 * if we will be waiting longer than deadlock_timeout
679 */
681 int cnt = 0;
682
683 if (ltime != 0)
684 {
687 timeouts[cnt].type = TMPARAM_AT;
688 timeouts[cnt].fin_time = ltime;
689 cnt++;
690 }
691
694 timeouts[cnt].type = TMPARAM_AFTER;
695 timeouts[cnt].delay_ms = DeadlockTimeout;
696 cnt++;
697
699 }
700
701 /* Wait to be signaled by the release of the Relation Lock */
703
704 /*
705 * Exit if ltime is reached. Then all the backends holding conflicting
706 * locks will be canceled in the next ResolveRecoveryConflictWithLock()
707 * call.
708 */
710 goto cleanup;
711
713 {
715
717
718 /* Quick exit if there's no work to be done */
720 goto cleanup;
721
722 /*
723 * Send signals to all the backends holding the conflicting locks, to
724 * ask them to check themselves for deadlocks.
725 */
727 {
730 backends++;
731 }
732
733 /*
734 * Exit if the recovery conflict has not been logged yet even though
735 * logging is enabled, so that the caller can log that. Then
736 * RecoveryConflictWithLock() is called again and we will wait again
737 * for the lock to be released.
738 */
740 goto cleanup;
741
742 /*
743 * Wait again here to be signaled by the release of the Relation Lock,
744 * to prevent the subsequent RecoveryConflictWithLock() from causing
745 * deadlock_timeout and sending a request for deadlocks check again.
746 * Otherwise the request continues to be sent every deadlock_timeout
747 * until the relation locks are released or ltime is reached.
748 */
751 }
752
753cleanup:
754
755 /*
756 * Clear any timeout requests established above. We assume here that the
757 * Startup process doesn't have any other outstanding timeouts than those
758 * used by this function. If that stops being true, we could cancel the
759 * timeouts individually, but that'd be slower.
760 */
764}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
static void cleanup(void)
Definition bootstrap.c:879
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition lock.c:3069
#define AccessExclusiveLock
Definition lockdefs.h:43
bool SignalRecoveryConflictWithVirtualXID(VirtualTransactionId vxid, RecoveryConflictReason reason)
Definition procarray.c:3484
static void ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist, RecoveryConflictReason reason, uint32 wait_event_info, bool report_waiting)
Definition standby.c:361
static volatile sig_atomic_t got_standby_lock_timeout
Definition standby.c:72
uint8 locktag_type
Definition lock.h:172
pg_atomic_uint64 waitStart
Definition proc.h:308
@ STANDBY_LOCK_TIMEOUT
Definition timeout.h:32
#define PG_WAIT_LOCK

References AccessExclusiveLock, Assert, cleanup(), DeadlockTimeout, disable_all_timeouts(), enable_timeouts(), fb(), GetCurrentTimestamp(), GetLockConflicts(), GetStandbyLimitTime(), got_standby_deadlock_timeout, got_standby_lock_timeout, EnableTimeoutParams::id, InHotStandby, LOCKTAG::locktag_type, MyProc, now(), pg_atomic_read_u64(), pg_atomic_write_u64(), PG_WAIT_LOCK, ProcWaitForSignal(), RECOVERY_CONFLICT_LOCK, RECOVERY_CONFLICT_STARTUP_DEADLOCK, ResolveRecoveryConflictWithVirtualXIDs(), SignalRecoveryConflictWithVirtualXID(), STANDBY_DEADLOCK_TIMEOUT, STANDBY_LOCK_TIMEOUT, TMPARAM_AFTER, TMPARAM_AT, VirtualTransactionIdIsValid, and PGPROC::waitStart.

Referenced by ProcSleep().

◆ ResolveRecoveryConflictWithSnapshot()

void ResolveRecoveryConflictWithSnapshot ( TransactionId  snapshotConflictHorizon,
bool  isCatalogRel,
RelFileLocator  locator 
)
extern

Definition at line 470 of file standby.c.

473{
475
476 /*
477 * If we get passed InvalidTransactionId then we do nothing (no conflict).
478 *
479 * This can happen when replaying already-applied WAL records after a
480 * standby crash or restart, or when replaying an XLOG_HEAP2_VISIBLE
481 * record that marks as frozen a page which was already all-visible. It's
482 * also quite common with records generated during index deletion
483 * (original execution of the deletion can reason that a recovery conflict
484 * which is sufficient for the deletion operation must take place before
485 * replay of the deletion record itself).
486 */
487 if (!TransactionIdIsValid(snapshotConflictHorizon))
488 return;
489
490 Assert(TransactionIdIsNormal(snapshotConflictHorizon));
491 backends = GetConflictingVirtualXIDs(snapshotConflictHorizon,
492 locator.dbOid);
496 true);
497
498 /*
499 * Note that WaitExceedsMaxStandbyDelay() is not taken into account here
500 * (as opposed to ResolveRecoveryConflictWithVirtualXIDs() above). That
501 * seems OK, given that this kind of conflict should not normally be
502 * reached, e.g. due to using a physical replication slot.
503 */
504 if (IsLogicalDecodingEnabled() && isCatalogRel)
506 snapshotConflictHorizon);
507}
VirtualTransactionId * GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
Definition procarray.c:3376
bool InvalidateObsoleteReplicationSlots(uint32 possible_causes, XLogSegNo oldestSegno, Oid dboid, TransactionId snapshotConflictHorizon)
Definition slot.c:2206
@ RS_INVAL_HORIZON
Definition slot.h:64
#define TransactionIdIsValid(xid)
Definition transam.h:41
#define TransactionIdIsNormal(xid)
Definition transam.h:42

References Assert, RelFileLocator::dbOid, fb(), GetConflictingVirtualXIDs(), InvalidateObsoleteReplicationSlots(), IsLogicalDecodingEnabled(), RECOVERY_CONFLICT_SNAPSHOT, ResolveRecoveryConflictWithVirtualXIDs(), RS_INVAL_HORIZON, TransactionIdIsNormal, and TransactionIdIsValid.

Referenced by btree_xlog_delete(), gistRedoDeleteRecord(), hash_xlog_vacuum_one_page(), heap_xlog_prune_freeze(), heap_xlog_visible(), ResolveRecoveryConflictWithSnapshotFullXid(), and spgRedoVacuumRedirect().

◆ ResolveRecoveryConflictWithSnapshotFullXid()

void ResolveRecoveryConflictWithSnapshotFullXid ( FullTransactionId  snapshotConflictHorizon,
bool  isCatalogRel,
RelFileLocator  locator 
)
extern

Definition at line 514 of file standby.c.

517{
518 /*
519 * ResolveRecoveryConflictWithSnapshot operates on 32-bit TransactionIds,
520 * so truncate the logged FullTransactionId. If the logged value is very
521 * old, so that XID wrap-around already happened on it, there can't be any
522 * snapshots that still see it.
523 */
525 uint64 diff;
526
527 diff = U64FromFullTransactionId(nextXid) -
528 U64FromFullTransactionId(snapshotConflictHorizon);
529 if (diff < MaxTransactionId / 2)
530 {
531 TransactionId truncated;
532
533 truncated = XidFromFullTransactionId(snapshotConflictHorizon);
535 isCatalogRel,
536 locator);
537 }
538}
uint64_t uint64
Definition c.h:619
void ResolveRecoveryConflictWithSnapshot(TransactionId snapshotConflictHorizon, bool isCatalogRel, RelFileLocator locator)
Definition standby.c:470
#define U64FromFullTransactionId(x)
Definition transam.h:49
#define XidFromFullTransactionId(x)
Definition transam.h:48
#define MaxTransactionId
Definition transam.h:35
FullTransactionId ReadNextFullTransactionId(void)
Definition varsup.c:288

References fb(), MaxTransactionId, ReadNextFullTransactionId(), ResolveRecoveryConflictWithSnapshot(), U64FromFullTransactionId, and XidFromFullTransactionId.

Referenced by btree_xlog_reuse_page(), and gistRedoPageReuse().

◆ ResolveRecoveryConflictWithTablespace()

void ResolveRecoveryConflictWithTablespace ( Oid  tsid)
extern

Definition at line 541 of file standby.c.

542{
544
545 /*
546 * Standby users may be currently using this tablespace for their
547 * temporary files. We only care about current users because
548 * temp_tablespace parameter will just ignore tablespaces that no longer
549 * exist.
550 *
551 * Ask everybody to cancel their queries immediately so we can ensure no
552 * temp files remain and we can remove the tablespace. Nuke the entire
553 * site from orbit, it's the only way to be sure.
554 *
555 * XXX: We could work out the pids of active backends using this
556 * tablespace by examining the temp filenames in the directory. We would
557 * then convert the pids into VirtualXIDs before attempting to cancel
558 * them.
559 *
560 * We don't wait for commit because drop tablespace is non-transactional.
561 */
563 InvalidOid);
567 true);
568}
#define InvalidOid
#define InvalidTransactionId
Definition transam.h:31

References fb(), GetConflictingVirtualXIDs(), InvalidOid, InvalidTransactionId, RECOVERY_CONFLICT_TABLESPACE, and ResolveRecoveryConflictWithVirtualXIDs().

Referenced by tblspc_redo().

◆ ShutdownRecoveryTransactionEnvironment()

void ShutdownRecoveryTransactionEnvironment ( void  )
extern

Definition at line 162 of file standby.c.

163{
164 /*
165 * Do nothing if RecoveryLockHash is NULL because that means that
166 * transaction tracking has not yet been initialized or has already been
167 * shut down. This makes it safe to have possibly-redundant calls of this
168 * function during process exit.
169 */
170 if (RecoveryLockHash == NULL)
171 return;
172
173 /* Mark all tracked in-progress transactions as finished. */
175
176 /* Release all locks the tracked transactions were holding */
178
179 /* Destroy the lock hash tables. */
184
185 /* Cleanup our VirtualTransaction */
187}
void hash_destroy(HTAB *hashp)
Definition dynahash.c:865
void VirtualXactLockTableCleanup(void)
Definition lock.c:4642
void ExpireAllKnownAssignedTransactionIds(void)
Definition procarray.c:4505
void StandbyReleaseAllLocks(void)
Definition standby.c:1107

References ExpireAllKnownAssignedTransactionIds(), fb(), hash_destroy(), RecoveryLockHash, RecoveryLockXidHash, StandbyReleaseAllLocks(), and VirtualXactLockTableCleanup().

Referenced by StartupProcExit(), and StartupXLOG().

◆ StandbyAcquireAccessExclusiveLock()

void StandbyAcquireAccessExclusiveLock ( TransactionId  xid,
Oid  dbOid,
Oid  relOid 
)
extern

Definition at line 987 of file standby.c.

988{
992 LOCKTAG locktag;
993 bool found;
994
995 /* Already processed? */
996 if (!TransactionIdIsValid(xid) ||
999 return;
1000
1001 elog(DEBUG4, "adding recovery lock: db %u rel %u", dbOid, relOid);
1002
1003 /* dbOid is InvalidOid when we are locking a shared relation. */
1004 Assert(OidIsValid(relOid));
1005
1006 /* Create a hash entry for this xid, if we don't have one already. */
1008 if (!found)
1009 {
1010 Assert(xidentry->xid == xid); /* dynahash should have set this */
1011 xidentry->head = NULL;
1012 }
1013
1014 /* Create a hash entry for this lock, unless we have one already. */
1015 key.xid = xid;
1016 key.dbOid = dbOid;
1017 key.relOid = relOid;
1019 if (!found)
1020 {
1021 /* It's new, so link it into the XID's list ... */
1022 lockentry->next = xidentry->head;
1023 xidentry->head = lockentry;
1024
1025 /* ... and acquire the lock locally. */
1026 SET_LOCKTAG_RELATION(locktag, dbOid, relOid);
1027
1028 (void) LockAcquire(&locktag, AccessExclusiveLock, true, false);
1029 }
1030}
#define OidIsValid(objectId)
Definition c.h:860
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
#define elog(elevel,...)
Definition elog.h:226
#define DEBUG4
Definition elog.h:27
@ HASH_ENTER
Definition hsearch.h:114
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition lock.c:809
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition lock.h:183
bool TransactionIdDidCommit(TransactionId transactionId)
Definition transam.c:126
bool TransactionIdDidAbort(TransactionId transactionId)
Definition transam.c:188

References AccessExclusiveLock, Assert, xl_standby_lock::dbOid, DEBUG4, elog, fb(), HASH_ENTER, hash_search(), RecoveryLockEntry::key, LockAcquire(), OidIsValid, RecoveryLockHash, RecoveryLockXidHash, xl_standby_lock::relOid, SET_LOCKTAG_RELATION, TransactionIdDidAbort(), TransactionIdDidCommit(), TransactionIdIsValid, and xl_standby_lock::xid.

Referenced by lock_twophase_standby_recover(), and standby_redo().

◆ StandbyDeadLockHandler()

void StandbyDeadLockHandler ( void  )
extern

Definition at line 937 of file standby.c.

938{
940}

References got_standby_deadlock_timeout.

Referenced by StartupProcessMain().

◆ StandbyLockTimeoutHandler()

void StandbyLockTimeoutHandler ( void  )
extern

Definition at line 955 of file standby.c.

956{
958}

References got_standby_lock_timeout.

Referenced by StartupProcessMain().

◆ StandbyReleaseAllLocks()

void StandbyReleaseAllLocks ( void  )
extern

Definition at line 1107 of file standby.c.

1108{
1109 HASH_SEQ_STATUS status;
1110 RecoveryLockXidEntry *entry;
1111
1112 elog(DEBUG2, "release all standby locks");
1113
1115 while ((entry = hash_seq_search(&status)))
1116 {
1119 }
1120}
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
#define DEBUG2
Definition elog.h:29
@ HASH_REMOVE
Definition hsearch.h:115
static void StandbyReleaseXidEntryLocks(RecoveryLockXidEntry *xidentry)
Definition standby.c:1036

References DEBUG2, elog, fb(), HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), RecoveryLockXidHash, and StandbyReleaseXidEntryLocks().

Referenced by ShutdownRecoveryTransactionEnvironment(), and StandbyReleaseLocks().

◆ StandbyReleaseLockTree()

void StandbyReleaseLockTree ( TransactionId  xid,
int  nsubxids,
TransactionId subxids 
)
extern

Definition at line 1093 of file standby.c.

1094{
1095 int i;
1096
1098
1099 for (i = 0; i < nsubxids; i++)
1100 StandbyReleaseLocks(subxids[i]);
1101}
int i
Definition isn.c:77
static void StandbyReleaseLocks(TransactionId xid)
Definition standby.c:1069

References fb(), i, and StandbyReleaseLocks().

Referenced by RecoverPreparedTransactions(), xact_redo_abort(), and xact_redo_commit().

◆ StandbyReleaseOldLocks()

void StandbyReleaseOldLocks ( TransactionId  oldxid)
extern

Definition at line 1131 of file standby.c.

1132{
1133 HASH_SEQ_STATUS status;
1134 RecoveryLockXidEntry *entry;
1135
1137 while ((entry = hash_seq_search(&status)))
1138 {
1140
1141 /* Skip if prepared transaction. */
1143 continue;
1144
1145 /* Skip if >= oldxid. */
1146 if (!TransactionIdPrecedes(entry->xid, oldxid))
1147 continue;
1148
1149 /* Remove all locks and hash table entry. */
1152 }
1153}
TransactionId xid
Definition standby.c:62
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
bool StandbyTransactionIdIsPrepared(TransactionId xid)
Definition twophase.c:1470

References Assert, fb(), HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), RecoveryLockXidHash, StandbyReleaseXidEntryLocks(), StandbyTransactionIdIsPrepared(), TransactionIdIsValid, TransactionIdPrecedes(), and RecoveryLockXidEntry::xid.

Referenced by ProcArrayApplyRecoveryInfo().

◆ StandbyTimeoutHandler()

void StandbyTimeoutHandler ( void  )
extern

Definition at line 946 of file standby.c.

947{
949}

References got_standby_delay_timeout.

Referenced by StartupProcessMain().

Variable Documentation

◆ log_recovery_conflict_waits

PGDLLIMPORT bool log_recovery_conflict_waits
extern

◆ max_standby_archive_delay

PGDLLIMPORT int max_standby_archive_delay
extern

Definition at line 41 of file standby.c.

Referenced by GetStandbyLimitTime().

◆ max_standby_streaming_delay

PGDLLIMPORT int max_standby_streaming_delay
extern

Definition at line 42 of file standby.c.

Referenced by GetStandbyLimitTime().