PostgreSQL Source Code  git master
standby.c File Reference
#include "postgres.h"
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xloginsert.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/sinvaladt.h"
#include "storage/standby.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
Include dependency graph for standby.c:

Go to the source code of this file.

Data Structures

struct  RecoveryLockEntry
 
struct  RecoveryLockXidEntry
 

Macros

#define STANDBY_INITIAL_WAIT_US   1000
 

Typedefs

typedef struct RecoveryLockEntry RecoveryLockEntry
 
typedef struct RecoveryLockXidEntry RecoveryLockXidEntry
 

Functions

static void ResolveRecoveryConflictWithVirtualXIDs (VirtualTransactionId *waitlist, ProcSignalReason reason, uint32 wait_event_info, bool report_waiting)
 
static void SendRecoveryConflictWithBufferPin (ProcSignalReason reason)
 
static XLogRecPtr LogCurrentRunningXacts (RunningTransactions CurrRunningXacts)
 
static void LogAccessExclusiveLocks (int nlocks, xl_standby_lock *locks)
 
static const char * get_recovery_conflict_desc (ProcSignalReason reason)
 
void InitRecoveryTransactionEnvironment (void)
 
void ShutdownRecoveryTransactionEnvironment (void)
 
static TimestampTz GetStandbyLimitTime (void)
 
static bool WaitExceedsMaxStandbyDelay (uint32 wait_event_info)
 
void LogRecoveryConflict (ProcSignalReason reason, TimestampTz wait_start, TimestampTz now, VirtualTransactionId *wait_list, bool still_waiting)
 
void ResolveRecoveryConflictWithSnapshot (TransactionId snapshotConflictHorizon, RelFileLocator locator)
 
void ResolveRecoveryConflictWithSnapshotFullXid (FullTransactionId snapshotConflictHorizon, 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 StandbyAcquireAccessExclusiveLock (TransactionId xid, Oid dbOid, Oid relOid)
 
static void StandbyReleaseXidEntryLocks (RecoveryLockXidEntry *xidentry)
 
static void StandbyReleaseLocks (TransactionId xid)
 
void StandbyReleaseLockTree (TransactionId xid, int nsubxids, TransactionId *subxids)
 
void StandbyReleaseAllLocks (void)
 
void StandbyReleaseOldLocks (TransactionId oldxid)
 
void standby_redo (XLogReaderState *record)
 
XLogRecPtr LogStandbySnapshot (void)
 
void LogAccessExclusiveLock (Oid dbOid, Oid relOid)
 
void LogAccessExclusiveLockPrepare (void)
 
void LogStandbyInvalidations (int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInitFileInval)
 

Variables

int vacuum_defer_cleanup_age
 
int max_standby_archive_delay = 30 * 1000
 
int max_standby_streaming_delay = 30 * 1000
 
bool log_recovery_conflict_waits = false
 
static HTABRecoveryLockHash = NULL
 
static HTABRecoveryLockXidHash = NULL
 
static volatile sig_atomic_t got_standby_deadlock_timeout = false
 
static volatile sig_atomic_t got_standby_delay_timeout = false
 
static volatile sig_atomic_t got_standby_lock_timeout = false
 
static int standbyWait_us = STANDBY_INITIAL_WAIT_US
 

Macro Definition Documentation

◆ STANDBY_INITIAL_WAIT_US

#define STANDBY_INITIAL_WAIT_US   1000

Definition at line 225 of file standby.c.

Typedef Documentation

◆ RecoveryLockEntry

◆ RecoveryLockXidEntry

Function Documentation

◆ CheckRecoveryConflictDeadlock()

void CheckRecoveryConflictDeadlock ( void  )

Definition at line 899 of file standby.c.

900 {
901  Assert(!InRecovery); /* do not call in Startup process */
902 
904  return;
905 
906  /*
907  * Error message should match ProcessInterrupts() but we avoid calling
908  * that because we aren't handling an interrupt at this point. Note that
909  * we only cancel the current transaction here, so if we are in a
910  * subtransaction and the pin is held by a parent, then the Startup
911  * process will continue to wait even though we have avoided deadlock.
912  */
913  ereport(ERROR,
915  errmsg("canceling statement due to conflict with recovery"),
916  errdetail("User transaction caused buffer deadlock with recovery.")));
917 }
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:4380
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Assert(fmt[strlen(fmt) - 1] !='\n')
#define ERRCODE_T_R_DEADLOCK_DETECTED
Definition: pgbench.c:76
bool InRecovery
Definition: xlogutils.c:53

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

Referenced by ProcSleep().

◆ get_recovery_conflict_desc()

static const char * get_recovery_conflict_desc ( ProcSignalReason  reason)
static

Definition at line 1471 of file standby.c.

1472 {
1473  const char *reasonDesc = _("unknown reason");
1474 
1475  switch (reason)
1476  {
1478  reasonDesc = _("recovery conflict on buffer pin");
1479  break;
1481  reasonDesc = _("recovery conflict on lock");
1482  break;
1484  reasonDesc = _("recovery conflict on tablespace");
1485  break;
1487  reasonDesc = _("recovery conflict on snapshot");
1488  break;
1490  reasonDesc = _("recovery conflict on buffer deadlock");
1491  break;
1493  reasonDesc = _("recovery conflict on database");
1494  break;
1495  default:
1496  break;
1497  }
1498 
1499  return reasonDesc;
1500 }
#define _(x)
Definition: elog.c:91
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
Definition: procsignal.h:44
@ PROCSIG_RECOVERY_CONFLICT_LOCK
Definition: procsignal.h:42
@ PROCSIG_RECOVERY_CONFLICT_DATABASE
Definition: procsignal.h:40
@ PROCSIG_RECOVERY_CONFLICT_SNAPSHOT
Definition: procsignal.h:43
@ PROCSIG_RECOVERY_CONFLICT_TABLESPACE
Definition: procsignal.h:41
@ PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition: procsignal.h:45

References _, PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, PROCSIG_RECOVERY_CONFLICT_DATABASE, PROCSIG_RECOVERY_CONFLICT_LOCK, PROCSIG_RECOVERY_CONFLICT_SNAPSHOT, PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK, and PROCSIG_RECOVERY_CONFLICT_TABLESPACE.

Referenced by LogRecoveryConflict().

◆ GetStandbyLimitTime()

static TimestampTz GetStandbyLimitTime ( void  )
static

Definition at line 201 of file standby.c.

202 {
203  TimestampTz rtime;
204  bool fromStream;
205 
206  /*
207  * The cutoff time is the last WAL data receipt time plus the appropriate
208  * delay variable. Delay of -1 means wait forever.
209  */
210  GetXLogReceiptTime(&rtime, &fromStream);
211  if (fromStream)
212  {
214  return 0; /* wait forever */
216  }
217  else
218  {
220  return 0; /* wait forever */
222  }
223 }
int64 TimestampTz
Definition: timestamp.h:39
int max_standby_archive_delay
Definition: standby.c:41
int max_standby_streaming_delay
Definition: standby.c:42
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:84
void GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)

References GetXLogReceiptTime(), max_standby_archive_delay, max_standby_streaming_delay, and TimestampTzPlusMilliseconds.

Referenced by ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), and WaitExceedsMaxStandbyDelay().

◆ InitRecoveryTransactionEnvironment()

void InitRecoveryTransactionEnvironment ( void  )

Definition at line 96 of file standby.c.

97 {
99  HASHCTL hash_ctl;
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  */
140  vxid.backendId = MyBackendId;
143 
145 }
uint32 TransactionId
Definition: c.h:588
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
BackendId MyBackendId
Definition: globals.c:85
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void VirtualXactLockTableInsert(VirtualTransactionId vxid)
Definition: lock.c:4476
struct xl_standby_lock xl_standby_lock
void SharedInvalBackendInit(bool sendOnly)
Definition: sinvaladt.c:266
LocalTransactionId GetNextLocalTransactionId(void)
Definition: sinvaladt.c:775
static HTAB * RecoveryLockXidHash
Definition: standby.c:67
struct RecoveryLockEntry RecoveryLockEntry
struct RecoveryLockXidEntry RecoveryLockXidEntry
static HTAB * RecoveryLockHash
Definition: standby.c:66
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
LocalTransactionId localTransactionId
Definition: lock.h:67
BackendId backendId
Definition: lock.h:66
HotStandbyState standbyState
Definition: xlogutils.c:56
@ STANDBY_INITIALIZED
Definition: xlogutils.h:50

References Assert(), VirtualTransactionId::backendId, HASHCTL::entrysize, GetNextLocalTransactionId(), HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, VirtualTransactionId::localTransactionId, MyBackendId, RecoveryLockHash, RecoveryLockXidHash, SharedInvalBackendInit(), STANDBY_INITIALIZED, standbyState, and VirtualXactLockTableInsert().

Referenced by StartupXLOG().

◆ LogAccessExclusiveLock()

void LogAccessExclusiveLock ( Oid  dbOid,
Oid  relOid 
)

Definition at line 1410 of file standby.c.

1411 {
1412  xl_standby_lock xlrec;
1413 
1414  xlrec.xid = GetCurrentTransactionId();
1415 
1416  xlrec.dbOid = dbOid;
1417  xlrec.relOid = relOid;
1418 
1419  LogAccessExclusiveLocks(1, &xlrec);
1421 }
static void LogAccessExclusiveLocks(int nlocks, xl_standby_lock *locks)
Definition: standby.c:1392
TransactionId xid
Definition: lockdefs.h:51
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:444
int MyXactFlags
Definition: xact.c:135
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK
Definition: xact.h:108

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

Referenced by LockAcquireExtended().

◆ LogAccessExclusiveLockPrepare()

void LogAccessExclusiveLockPrepare ( void  )

Definition at line 1427 of file standby.c.

1428 {
1429  /*
1430  * Ensure that a TransactionId has been assigned to this transaction, for
1431  * two reasons, both related to lock release on the standby. First, we
1432  * must assign an xid so that RecordTransactionCommit() and
1433  * RecordTransactionAbort() do not optimise away the transaction
1434  * completion record which recovery relies upon to release locks. It's a
1435  * hack, but for a corner case not worth adding code for into the main
1436  * commit path. Second, we must assign an xid before the lock is recorded
1437  * in shared memory, otherwise a concurrently executing
1438  * GetRunningTransactionLocks() might see a lock associated with an
1439  * InvalidTransactionId which we later assert cannot happen.
1440  */
1441  (void) GetCurrentTransactionId();
1442 }

References GetCurrentTransactionId().

Referenced by LockAcquireExtended().

◆ LogAccessExclusiveLocks()

static void LogAccessExclusiveLocks ( int  nlocks,
xl_standby_lock locks 
)
static

Definition at line 1392 of file standby.c.

1393 {
1394  xl_standby_locks xlrec;
1395 
1396  xlrec.nlocks = nlocks;
1397 
1398  XLogBeginInsert();
1399  XLogRegisterData((char *) &xlrec, offsetof(xl_standby_locks, locks));
1400  XLogRegisterData((char *) locks, nlocks * sizeof(xl_standby_lock));
1402 
1403  (void) XLogInsert(RM_STANDBY_ID, XLOG_STANDBY_LOCK);
1404 }
#define XLOG_STANDBY_LOCK
Definition: standbydefs.h:34
#define XLOG_MARK_UNIMPORTANT
Definition: xlog.h:150
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:351
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:451
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:433
void XLogBeginInsert(void)
Definition: xloginsert.c:150

References xl_standby_locks::nlocks, XLOG_MARK_UNIMPORTANT, XLOG_STANDBY_LOCK, XLogBeginInsert(), XLogInsert(), XLogRegisterData(), and XLogSetRecordFlags().

Referenced by LogAccessExclusiveLock(), and LogStandbySnapshot().

◆ LogCurrentRunningXacts()

static XLogRecPtr LogCurrentRunningXacts ( RunningTransactions  CurrRunningXacts)
static

Definition at line 1332 of file standby.c.

1333 {
1334  xl_running_xacts xlrec;
1335  XLogRecPtr recptr;
1336 
1337  xlrec.xcnt = CurrRunningXacts->xcnt;
1338  xlrec.subxcnt = CurrRunningXacts->subxcnt;
1339  xlrec.subxid_overflow = CurrRunningXacts->subxid_overflow;
1340  xlrec.nextXid = CurrRunningXacts->nextXid;
1341  xlrec.oldestRunningXid = CurrRunningXacts->oldestRunningXid;
1342  xlrec.latestCompletedXid = CurrRunningXacts->latestCompletedXid;
1343 
1344  /* Header */
1345  XLogBeginInsert();
1347  XLogRegisterData((char *) (&xlrec), MinSizeOfXactRunningXacts);
1348 
1349  /* array of TransactionIds */
1350  if (xlrec.xcnt > 0)
1351  XLogRegisterData((char *) CurrRunningXacts->xids,
1352  (xlrec.xcnt + xlrec.subxcnt) * sizeof(TransactionId));
1353 
1354  recptr = XLogInsert(RM_STANDBY_ID, XLOG_RUNNING_XACTS);
1355 
1356  if (CurrRunningXacts->subxid_overflow)
1358  "snapshot of %u running transactions overflowed (lsn %X/%X oldest xid %u latest complete %u next xid %u)",
1359  CurrRunningXacts->xcnt,
1360  LSN_FORMAT_ARGS(recptr),
1361  CurrRunningXacts->oldestRunningXid,
1362  CurrRunningXacts->latestCompletedXid,
1363  CurrRunningXacts->nextXid);
1364  else
1366  "snapshot of %u+%u running transaction ids (lsn %X/%X oldest xid %u latest complete %u next xid %u)",
1367  CurrRunningXacts->xcnt, CurrRunningXacts->subxcnt,
1368  LSN_FORMAT_ARGS(recptr),
1369  CurrRunningXacts->oldestRunningXid,
1370  CurrRunningXacts->latestCompletedXid,
1371  CurrRunningXacts->nextXid);
1372 
1373  /*
1374  * Ensure running_xacts information is synced to disk not too far in the
1375  * future. We don't want to stall anything though (i.e. use XLogFlush()),
1376  * so we let the wal writer do it during normal operation.
1377  * XLogSetAsyncXactLSN() conveniently will mark the LSN as to-be-synced
1378  * and nudge the WALWriter into action if sleeping. Check
1379  * XLogBackgroundFlush() for details why a record might not be flushed
1380  * without it.
1381  */
1382  XLogSetAsyncXactLSN(recptr);
1383 
1384  return recptr;
1385 }
int trace_recovery(int trace_level)
Definition: elog.c:3749
#define DEBUG2
Definition: elog.h:29
#define MinSizeOfXactRunningXacts
Definition: standby.h:62
#define XLOG_RUNNING_XACTS
Definition: standbydefs.h:35
TransactionId oldestRunningXid
Definition: standby.h:83
TransactionId nextXid
Definition: standby.h:82
TransactionId latestCompletedXid
Definition: standby.h:84
TransactionId * xids
Definition: standby.h:86
TransactionId latestCompletedXid
Definition: standbydefs.h:54
TransactionId oldestRunningXid
Definition: standbydefs.h:53
TransactionId nextXid
Definition: standbydefs.h:52
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2357
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References DEBUG2, elog(), RunningTransactionsData::latestCompletedXid, xl_running_xacts::latestCompletedXid, LSN_FORMAT_ARGS, MinSizeOfXactRunningXacts, RunningTransactionsData::nextXid, xl_running_xacts::nextXid, RunningTransactionsData::oldestRunningXid, xl_running_xacts::oldestRunningXid, RunningTransactionsData::subxcnt, xl_running_xacts::subxcnt, RunningTransactionsData::subxid_overflow, xl_running_xacts::subxid_overflow, trace_recovery(), RunningTransactionsData::xcnt, xl_running_xacts::xcnt, RunningTransactionsData::xids, XLOG_MARK_UNIMPORTANT, XLOG_RUNNING_XACTS, XLogBeginInsert(), XLogInsert(), XLogRegisterData(), XLogSetAsyncXactLSN(), and XLogSetRecordFlags().

Referenced by LogStandbySnapshot().

◆ LogRecoveryConflict()

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

Definition at line 274 of file standby.c.

277 {
278  long secs;
279  int usecs;
280  long msecs;
282  int nprocs = 0;
283 
284  /*
285  * There must be no conflicting processes when the recovery conflict has
286  * already been resolved.
287  */
288  Assert(still_waiting || wait_list == NULL);
289 
290  TimestampDifference(wait_start, now, &secs, &usecs);
291  msecs = secs * 1000 + usecs / 1000;
292  usecs = usecs % 1000;
293 
294  if (wait_list)
295  {
296  VirtualTransactionId *vxids;
297 
298  /* Construct a string of list of the conflicting processes */
299  vxids = wait_list;
300  while (VirtualTransactionIdIsValid(*vxids))
301  {
302  PGPROC *proc = BackendIdGetProc(vxids->backendId);
303 
304  /* proc can be NULL if the target backend is not active */
305  if (proc)
306  {
307  if (nprocs == 0)
308  {
310  appendStringInfo(&buf, "%d", proc->pid);
311  }
312  else
313  appendStringInfo(&buf, ", %d", proc->pid);
314 
315  nprocs++;
316  }
317 
318  vxids++;
319  }
320  }
321 
322  /*
323  * If wait_list is specified, report the list of PIDs of active
324  * conflicting backends in a detail message. Note that if all the backends
325  * in the list are not active, no detail message is logged.
326  */
327  if (still_waiting)
328  {
329  ereport(LOG,
330  errmsg("recovery still waiting after %ld.%03d ms: %s",
331  msecs, usecs, get_recovery_conflict_desc(reason)),
332  nprocs > 0 ? errdetail_log_plural("Conflicting process: %s.",
333  "Conflicting processes: %s.",
334  nprocs, buf.data) : 0);
335  }
336  else
337  {
338  ereport(LOG,
339  errmsg("recovery finished waiting after %ld.%03d ms: %s",
340  msecs, usecs, get_recovery_conflict_desc(reason)));
341  }
342 
343  if (nprocs > 0)
344  pfree(buf.data);
345 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1676
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1549
int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1271
#define LOG
Definition: elog.h:31
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:72
void pfree(void *pointer)
Definition: mcxt.c:1306
static char * buf
Definition: pg_test_fsync.c:67
PGPROC * BackendIdGetProc(int backendID)
Definition: sinvaladt.c:385
static const char * get_recovery_conflict_desc(ProcSignalReason reason)
Definition: standby.c:1471
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Definition: proc.h:162
int pid
Definition: proc.h:186

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

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

◆ LogStandbyInvalidations()

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

Definition at line 1449 of file standby.c.

1451 {
1452  xl_invalidations xlrec;
1453 
1454  /* prepare record */
1455  memset(&xlrec, 0, sizeof(xlrec));
1456  xlrec.dbId = MyDatabaseId;
1457  xlrec.tsId = MyDatabaseTableSpace;
1458  xlrec.relcacheInitFileInval = relcacheInitFileInval;
1459  xlrec.nmsgs = nmsgs;
1460 
1461  /* perform insertion */
1462  XLogBeginInsert();
1463  XLogRegisterData((char *) (&xlrec), MinSizeOfInvalidations);
1464  XLogRegisterData((char *) msgs,
1465  nmsgs * sizeof(SharedInvalidationMessage));
1466  XLogInsert(RM_STANDBY_ID, XLOG_INVALIDATIONS);
1467 }
Oid MyDatabaseTableSpace
Definition: globals.c:91
Oid MyDatabaseId
Definition: globals.c:89
#define XLOG_INVALIDATIONS
Definition: standbydefs.h:36
#define MinSizeOfInvalidations
Definition: standbydefs.h:72
bool relcacheInitFileInval
Definition: standbydefs.h:67

References xl_invalidations::dbId, MinSizeOfInvalidations, MyDatabaseId, MyDatabaseTableSpace, xl_invalidations::nmsgs, xl_invalidations::relcacheInitFileInval, xl_invalidations::tsId, XLOG_INVALIDATIONS, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by RecordTransactionCommit().

◆ LogStandbySnapshot()

XLogRecPtr LogStandbySnapshot ( void  )

Definition at line 1272 of file standby.c.

1273 {
1274  XLogRecPtr recptr;
1275  RunningTransactions running;
1276  xl_standby_lock *locks;
1277  int nlocks;
1278 
1280 
1281  /*
1282  * Get details of any AccessExclusiveLocks being held at the moment.
1283  */
1284  locks = GetRunningTransactionLocks(&nlocks);
1285  if (nlocks > 0)
1286  LogAccessExclusiveLocks(nlocks, locks);
1287  pfree(locks);
1288 
1289  /*
1290  * Log details of all in-progress transactions. This should be the last
1291  * record we write, because standby will open up when it sees this.
1292  */
1293  running = GetRunningTransactionData();
1294 
1295  /*
1296  * GetRunningTransactionData() acquired ProcArrayLock, we must release it.
1297  * For Hot Standby this can be done before inserting the WAL record
1298  * because ProcArrayApplyRecoveryInfo() rechecks the commit status using
1299  * the clog. For logical decoding, though, the lock can't be released
1300  * early because the clog might be "in the future" from the POV of the
1301  * historic snapshot. This would allow for situations where we're waiting
1302  * for the end of a transaction listed in the xl_running_xacts record
1303  * which, according to the WAL, has committed before the xl_running_xacts
1304  * record. Fortunately this routine isn't executed frequently, and it's
1305  * only a shared lock.
1306  */
1308  LWLockRelease(ProcArrayLock);
1309 
1310  recptr = LogCurrentRunningXacts(running);
1311 
1312  /* Release lock if we kept it longer ... */
1314  LWLockRelease(ProcArrayLock);
1315 
1316  /* GetRunningTransactionData() acquired XidGenLock, we must release it */
1317  LWLockRelease(XidGenLock);
1318 
1319  return recptr;
1320 }
xl_standby_lock * GetRunningTransactionLocks(int *nlocks)
Definition: lock.c:4018
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
RunningTransactions GetRunningTransactionData(void)
Definition: procarray.c:2729
static XLogRecPtr LogCurrentRunningXacts(RunningTransactions CurrRunningXacts)
Definition: standby.c:1332
int wal_level
Definition: xlog.c:134
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:71
#define XLogStandbyInfoActive()
Definition: xlog.h:118

References Assert(), GetRunningTransactionData(), GetRunningTransactionLocks(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), LWLockRelease(), pfree(), wal_level, WAL_LEVEL_LOGICAL, and XLogStandbyInfoActive.

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

◆ ResolveRecoveryConflictWithBufferPin()

void ResolveRecoveryConflictWithBufferPin ( void  )

Definition at line 787 of file standby.c.

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

References Assert(), DeadlockTimeout, EnableTimeoutParams::delay_ms, disable_all_timeouts(), enable_timeouts(), EnableTimeoutParams::fin_time, GetCurrentTimestamp(), GetStandbyLimitTime(), got_standby_deadlock_timeout, got_standby_delay_timeout, EnableTimeoutParams::id, InHotStandby, PG_WAIT_BUFFER_PIN, PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK, ProcWaitForSignal(), SendRecoveryConflictWithBufferPin(), STANDBY_DEADLOCK_TIMEOUT, STANDBY_TIMEOUT, TMPARAM_AFTER, TMPARAM_AT, and EnableTimeoutParams::type.

Referenced by LockBufferForCleanup().

◆ ResolveRecoveryConflictWithDatabase()

void ResolveRecoveryConflictWithDatabase ( Oid  dbid)

Definition at line 563 of file standby.c.

564 {
565  /*
566  * We don't do ResolveRecoveryConflictWithVirtualXIDs() here since that
567  * only waits for transactions and completely idle sessions would block
568  * us. This is rare enough that we do this as simply as possible: no wait,
569  * just force them off immediately.
570  *
571  * No locking is required here because we already acquired
572  * AccessExclusiveLock. Anybody trying to connect while we do this will
573  * block during InitPostgres() and then disconnect when they see the
574  * database has been removed.
575  */
576  while (CountDBBackends(dbid) > 0)
577  {
579 
580  /*
581  * Wait awhile for them to die so that we avoid flooding an
582  * unresponsive backend when system is heavily loaded.
583  */
584  pg_usleep(10000);
585  }
586 }
void CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
Definition: procarray.c:3626
int CountDBBackends(Oid databaseid)
Definition: procarray.c:3565
void pg_usleep(long microsec)
Definition: signal.c:53

References CancelDBBackends(), CountDBBackends(), pg_usleep(), and PROCSIG_RECOVERY_CONFLICT_DATABASE.

Referenced by dbase_redo().

◆ ResolveRecoveryConflictWithLock()

void ResolveRecoveryConflictWithLock ( LOCKTAG  locktag,
bool  logging_conflict 
)

Definition at line 617 of file standby.c.

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

References AccessExclusiveLock, Assert(), cleanup(), DeadlockTimeout, EnableTimeoutParams::delay_ms, disable_all_timeouts(), enable_timeouts(), EnableTimeoutParams::fin_time, 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, PROCSIG_RECOVERY_CONFLICT_LOCK, PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK, ProcWaitForSignal(), ResolveRecoveryConflictWithVirtualXIDs(), SignalVirtualTransaction(), STANDBY_DEADLOCK_TIMEOUT, STANDBY_LOCK_TIMEOUT, TMPARAM_AFTER, TMPARAM_AT, EnableTimeoutParams::type, VirtualTransactionIdIsValid, and PGPROC::waitStart.

Referenced by ProcSleep().

◆ ResolveRecoveryConflictWithSnapshot()

void ResolveRecoveryConflictWithSnapshot ( TransactionId  snapshotConflictHorizon,
RelFileLocator  locator 
)

Definition at line 477 of file standby.c.

479 {
480  VirtualTransactionId *backends;
481 
482  /*
483  * If we get passed InvalidTransactionId then we do nothing (no conflict).
484  *
485  * This can happen when replaying already-applied WAL records after a
486  * standby crash or restart, or when replaying an XLOG_HEAP2_VISIBLE
487  * record that marks as frozen a page which was already all-visible. It's
488  * also quite common with records generated during index deletion
489  * (original execution of the deletion can reason that a recovery conflict
490  * which is sufficient for the deletion operation must take place before
491  * replay of the deletion record itself).
492  */
493  if (!TransactionIdIsValid(snapshotConflictHorizon))
494  return;
495 
496  backends = GetConflictingVirtualXIDs(snapshotConflictHorizon,
497  locator.dbOid);
501  true);
502 }
VirtualTransactionId * GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
Definition: procarray.c:3383
#define TransactionIdIsValid(xid)
Definition: transam.h:41
@ WAIT_EVENT_RECOVERY_CONFLICT_SNAPSHOT
Definition: wait_event.h:120

References RelFileLocator::dbOid, GetConflictingVirtualXIDs(), PROCSIG_RECOVERY_CONFLICT_SNAPSHOT, ResolveRecoveryConflictWithVirtualXIDs(), TransactionIdIsValid, and WAIT_EVENT_RECOVERY_CONFLICT_SNAPSHOT.

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

◆ ResolveRecoveryConflictWithSnapshotFullXid()

void ResolveRecoveryConflictWithSnapshotFullXid ( FullTransactionId  snapshotConflictHorizon,
RelFileLocator  locator 
)

Definition at line 509 of file standby.c.

511 {
512  /*
513  * ResolveRecoveryConflictWithSnapshot operates on 32-bit TransactionIds,
514  * so truncate the logged FullTransactionId. If the logged value is very
515  * old, so that XID wrap-around already happened on it, there can't be any
516  * snapshots that still see it.
517  */
519  uint64 diff;
520 
521  diff = U64FromFullTransactionId(nextXid) -
522  U64FromFullTransactionId(snapshotConflictHorizon);
523  if (diff < MaxTransactionId / 2)
524  {
525  TransactionId truncated;
526 
527  truncated = XidFromFullTransactionId(snapshotConflictHorizon);
528  ResolveRecoveryConflictWithSnapshot(truncated, locator);
529  }
530 }
void ResolveRecoveryConflictWithSnapshot(TransactionId snapshotConflictHorizon, RelFileLocator locator)
Definition: standby.c:477
#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:261

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

Referenced by btree_xlog_reuse_page(), and gistRedoPageReuse().

◆ ResolveRecoveryConflictWithTablespace()

void ResolveRecoveryConflictWithTablespace ( Oid  tsid)

Definition at line 533 of file standby.c.

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

References GetConflictingVirtualXIDs(), InvalidOid, InvalidTransactionId, PROCSIG_RECOVERY_CONFLICT_TABLESPACE, ResolveRecoveryConflictWithVirtualXIDs(), and WAIT_EVENT_RECOVERY_CONFLICT_TABLESPACE.

Referenced by tblspc_redo().

◆ ResolveRecoveryConflictWithVirtualXIDs()

static void ResolveRecoveryConflictWithVirtualXIDs ( VirtualTransactionId waitlist,
ProcSignalReason  reason,
uint32  wait_event_info,
bool  report_waiting 
)
static

Definition at line 360 of file standby.c.

363 {
364  TimestampTz waitStart = 0;
365  char *new_status = NULL;
366  bool logged_recovery_conflict = false;
367 
368  /* Fast exit, to avoid a kernel call if there's no work to be done. */
369  if (!VirtualTransactionIdIsValid(*waitlist))
370  return;
371 
372  /* Set the wait start timestamp for reporting */
373  if (report_waiting && (log_recovery_conflict_waits || update_process_title))
374  waitStart = GetCurrentTimestamp();
375 
376  while (VirtualTransactionIdIsValid(*waitlist))
377  {
378  /* reset standbyWait_us for each xact we wait for */
380 
381  /* wait until the virtual xid is gone */
382  while (!VirtualXactLock(*waitlist, false))
383  {
384  /* Is it time to kill it? */
385  if (WaitExceedsMaxStandbyDelay(wait_event_info))
386  {
387  pid_t pid;
388 
389  /*
390  * Now find out who to throw out of the balloon.
391  */
393  pid = CancelVirtualTransaction(*waitlist, reason);
394 
395  /*
396  * Wait a little bit for it to die so that we avoid flooding
397  * an unresponsive backend when system is heavily loaded.
398  */
399  if (pid != 0)
400  pg_usleep(5000L);
401  }
402 
403  if (waitStart != 0 && (!logged_recovery_conflict || new_status == NULL))
404  {
405  TimestampTz now = 0;
406  bool maybe_log_conflict;
407  bool maybe_update_title;
408 
409  maybe_log_conflict = (log_recovery_conflict_waits && !logged_recovery_conflict);
410  maybe_update_title = (update_process_title && new_status == NULL);
411 
412  /* Get the current timestamp if not report yet */
413  if (maybe_log_conflict || maybe_update_title)
415 
416  /*
417  * Report via ps if we have been waiting for more than 500
418  * msec (should that be configurable?)
419  */
420  if (maybe_update_title &&
421  TimestampDifferenceExceeds(waitStart, now, 500))
422  {
423  const char *old_status;
424  int len;
425 
426  old_status = get_ps_display(&len);
427  new_status = (char *) palloc(len + 8 + 1);
428  memcpy(new_status, old_status, len);
429  strcpy(new_status + len, " waiting");
430  set_ps_display(new_status);
431  new_status[len] = '\0'; /* truncate off " waiting" */
432  }
433 
434  /*
435  * Emit the log message if the startup process is waiting
436  * longer than deadlock_timeout for recovery conflict.
437  */
438  if (maybe_log_conflict &&
440  {
441  LogRecoveryConflict(reason, waitStart, now, waitlist, true);
442  logged_recovery_conflict = true;
443  }
444  }
445  }
446 
447  /* The virtual transaction is gone now, wait for the next one */
448  waitlist++;
449  }
450 
451  /*
452  * Emit the log message if recovery conflict was resolved but the startup
453  * process waited longer than deadlock_timeout for it.
454  */
455  if (logged_recovery_conflict)
456  LogRecoveryConflict(reason, waitStart, GetCurrentTimestamp(),
457  NULL, false);
458 
459  /* Reset ps display if we changed it */
460  if (new_status)
461  {
462  set_ps_display(new_status);
463  pfree(new_status);
464  }
465 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1731
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4599
void * palloc(Size size)
Definition: mcxt.c:1199
const void size_t len
pid_t CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
Definition: procarray.c:3457
const char * get_ps_display(int *displen)
Definition: ps_status.c:414
bool update_process_title
Definition: ps_status.c:35
void set_ps_display(const char *activity)
Definition: ps_status.c:342
static bool WaitExceedsMaxStandbyDelay(uint32 wait_event_info)
Definition: standby.c:234
static int standbyWait_us
Definition: standby.c:226
bool log_recovery_conflict_waits
Definition: standby.c:43
#define STANDBY_INITIAL_WAIT_US
Definition: standby.c:225
void LogRecoveryConflict(ProcSignalReason reason, TimestampTz wait_start, TimestampTz now, VirtualTransactionId *wait_list, bool still_waiting)
Definition: standby.c:274

References Assert(), CancelVirtualTransaction(), DeadlockTimeout, get_ps_display(), GetCurrentTimestamp(), len, log_recovery_conflict_waits, LogRecoveryConflict(), now(), palloc(), pfree(), pg_usleep(), set_ps_display(), STANDBY_INITIAL_WAIT_US, standbyWait_us, TimestampDifferenceExceeds(), update_process_title, VirtualTransactionIdIsValid, VirtualXactLock(), and WaitExceedsMaxStandbyDelay().

Referenced by ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithSnapshot(), and ResolveRecoveryConflictWithTablespace().

◆ SendRecoveryConflictWithBufferPin()

static void SendRecoveryConflictWithBufferPin ( ProcSignalReason  reason)
static

Definition at line 871 of file standby.c.

872 {
875 
876  /*
877  * We send signal to all backends to ask them if they are holding the
878  * buffer pin which is delaying the Startup process. We must not set the
879  * conflict flag yet, since most backends will be innocent. Let the
880  * SIGUSR1 handling in each backend decide their own fate.
881  */
882  CancelDBBackends(InvalidOid, reason, false);
883 }

References Assert(), CancelDBBackends(), InvalidOid, PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, and PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK.

Referenced by ResolveRecoveryConflictWithBufferPin().

◆ ShutdownRecoveryTransactionEnvironment()

void ShutdownRecoveryTransactionEnvironment ( void  )

Definition at line 161 of file standby.c.

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

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

Referenced by StartupProcExit(), and StartupXLOG().

◆ standby_redo()

void standby_redo ( XLogReaderState record)

Definition at line 1155 of file standby.c.

1156 {
1157  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1158 
1159  /* Backup blocks are not used in standby records */
1160  Assert(!XLogRecHasAnyBlockRefs(record));
1161 
1162  /* Do nothing if we're not in hot standby mode */
1164  return;
1165 
1166  if (info == XLOG_STANDBY_LOCK)
1167  {
1168  xl_standby_locks *xlrec = (xl_standby_locks *) XLogRecGetData(record);
1169  int i;
1170 
1171  for (i = 0; i < xlrec->nlocks; i++)
1173  xlrec->locks[i].dbOid,
1174  xlrec->locks[i].relOid);
1175  }
1176  else if (info == XLOG_RUNNING_XACTS)
1177  {
1178  xl_running_xacts *xlrec = (xl_running_xacts *) XLogRecGetData(record);
1179  RunningTransactionsData running;
1180 
1181  running.xcnt = xlrec->xcnt;
1182  running.subxcnt = xlrec->subxcnt;
1183  running.subxid_overflow = xlrec->subxid_overflow;
1184  running.nextXid = xlrec->nextXid;
1185  running.latestCompletedXid = xlrec->latestCompletedXid;
1186  running.oldestRunningXid = xlrec->oldestRunningXid;
1187  running.xids = xlrec->xids;
1188 
1189  ProcArrayApplyRecoveryInfo(&running);
1190  }
1191  else if (info == XLOG_INVALIDATIONS)
1192  {
1193  xl_invalidations *xlrec = (xl_invalidations *) XLogRecGetData(record);
1194 
1196  xlrec->nmsgs,
1197  xlrec->relcacheInitFileInval,
1198  xlrec->dbId,
1199  xlrec->tsId);
1200  }
1201  else
1202  elog(PANIC, "standby_redo: unknown op code %u", info);
1203 }
unsigned char uint8
Definition: c.h:440
#define PANIC
Definition: elog.h:42
void ProcessCommittedInvalidationMessages(SharedInvalidationMessage *msgs, int nmsgs, bool RelcacheInitFileInval, Oid dbid, Oid tsid)
Definition: inval.c:963
int i
Definition: isn.c:73
void ProcArrayApplyRecoveryInfo(RunningTransactions running)
Definition: procarray.c:1056
void StandbyAcquireAccessExclusiveLock(TransactionId xid, Oid dbOid, Oid relOid)
Definition: standby.c:980
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: standbydefs.h:69
TransactionId xids[FLEXIBLE_ARRAY_MEMBER]
Definition: standbydefs.h:56
xl_standby_lock locks[FLEXIBLE_ARRAY_MEMBER]
Definition: standbydefs.h:41
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:411
#define XLogRecGetData(decoder)
Definition: xlogreader.h:416
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:418
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
@ STANDBY_DISABLED
Definition: xlogutils.h:49

References Assert(), xl_invalidations::dbId, xl_standby_lock::dbOid, elog(), i, RunningTransactionsData::latestCompletedXid, xl_running_xacts::latestCompletedXid, xl_standby_locks::locks, xl_invalidations::msgs, RunningTransactionsData::nextXid, xl_running_xacts::nextXid, xl_standby_locks::nlocks, xl_invalidations::nmsgs, RunningTransactionsData::oldestRunningXid, xl_running_xacts::oldestRunningXid, PANIC, ProcArrayApplyRecoveryInfo(), ProcessCommittedInvalidationMessages(), xl_invalidations::relcacheInitFileInval, xl_standby_lock::relOid, STANDBY_DISABLED, StandbyAcquireAccessExclusiveLock(), standbyState, RunningTransactionsData::subxcnt, xl_running_xacts::subxcnt, RunningTransactionsData::subxid_overflow, xl_running_xacts::subxid_overflow, xl_invalidations::tsId, RunningTransactionsData::xcnt, xl_running_xacts::xcnt, xl_standby_lock::xid, RunningTransactionsData::xids, xl_running_xacts::xids, XLOG_INVALIDATIONS, XLOG_RUNNING_XACTS, XLOG_STANDBY_LOCK, XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, and XLR_INFO_MASK.

◆ StandbyAcquireAccessExclusiveLock()

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

Definition at line 980 of file standby.c.

981 {
982  RecoveryLockXidEntry *xidentry;
983  RecoveryLockEntry *lockentry;
985  LOCKTAG locktag;
986  bool found;
987 
988  /* Already processed? */
989  if (!TransactionIdIsValid(xid) ||
990  TransactionIdDidCommit(xid) ||
992  return;
993 
995  "adding recovery lock: db %u rel %u", dbOid, relOid);
996 
997  /* dbOid is InvalidOid when we are locking a shared relation. */
998  Assert(OidIsValid(relOid));
999 
1000  /* Create a hash entry for this xid, if we don't have one already. */
1001  xidentry = hash_search(RecoveryLockXidHash, &xid, HASH_ENTER, &found);
1002  if (!found)
1003  {
1004  Assert(xidentry->xid == xid); /* dynahash should have set this */
1005  xidentry->head = NULL;
1006  }
1007 
1008  /* Create a hash entry for this lock, unless we have one already. */
1009  key.xid = xid;
1010  key.dbOid = dbOid;
1011  key.relOid = relOid;
1012  lockentry = hash_search(RecoveryLockHash, &key, HASH_ENTER, &found);
1013  if (!found)
1014  {
1015  /* It's new, so link it into the XID's list ... */
1016  lockentry->next = xidentry->head;
1017  xidentry->head = lockentry;
1018 
1019  /* ... and acquire the lock locally. */
1020  SET_LOCKTAG_RELATION(locktag, dbOid, relOid);
1021 
1022  (void) LockAcquire(&locktag, AccessExclusiveLock, true, false);
1023  }
1024 }
#define OidIsValid(objectId)
Definition: c.h:711
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
#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:747
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition: lock.h:184
Definition: lock.h:168
struct RecoveryLockEntry * next
Definition: standby.c:57
TransactionId xid
Definition: standby.c:62
struct RecoveryLockEntry * head
Definition: standby.c:63
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:181

References AccessExclusiveLock, Assert(), DEBUG4, elog(), HASH_ENTER, hash_search(), RecoveryLockXidEntry::head, sort-test::key, LockAcquire(), RecoveryLockEntry::next, OidIsValid, RecoveryLockHash, RecoveryLockXidHash, SET_LOCKTAG_RELATION, trace_recovery(), TransactionIdDidAbort(), TransactionIdDidCommit(), TransactionIdIsValid, and RecoveryLockXidEntry::xid.

Referenced by lock_twophase_standby_recover(), and standby_redo().

◆ StandbyDeadLockHandler()

void StandbyDeadLockHandler ( void  )

Definition at line 930 of file standby.c.

931 {
933 }

References got_standby_deadlock_timeout.

Referenced by StartupProcessMain().

◆ StandbyLockTimeoutHandler()

void StandbyLockTimeoutHandler ( void  )

Definition at line 948 of file standby.c.

949 {
951 }

References got_standby_lock_timeout.

Referenced by StartupProcessMain().

◆ StandbyReleaseAllLocks()

void StandbyReleaseAllLocks ( void  )

Definition at line 1101 of file standby.c.

1102 {
1104  RecoveryLockXidEntry *entry;
1105 
1106  elog(trace_recovery(DEBUG2), "release all standby locks");
1107 
1109  while ((entry = hash_seq_search(&status)))
1110  {
1113  }
1114 }
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1431
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1421
@ HASH_REMOVE
Definition: hsearch.h:115
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
static void StandbyReleaseXidEntryLocks(RecoveryLockXidEntry *xidentry)
Definition: standby.c:1030

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

Referenced by ShutdownRecoveryTransactionEnvironment(), and StandbyReleaseLocks().

◆ StandbyReleaseLocks()

static void StandbyReleaseLocks ( TransactionId  xid)
static

Definition at line 1063 of file standby.c.

1064 {
1065  RecoveryLockXidEntry *entry;
1066 
1067  if (TransactionIdIsValid(xid))
1068  {
1069  if ((entry = hash_search(RecoveryLockXidHash, &xid, HASH_FIND, NULL)))
1070  {
1073  }
1074  }
1075  else
1077 }
@ HASH_FIND
Definition: hsearch.h:113

References HASH_FIND, HASH_REMOVE, hash_search(), RecoveryLockXidHash, StandbyReleaseAllLocks(), StandbyReleaseXidEntryLocks(), and TransactionIdIsValid.

Referenced by StandbyReleaseLockTree().

◆ StandbyReleaseLockTree()

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

Definition at line 1087 of file standby.c.

1088 {
1089  int i;
1090 
1091  StandbyReleaseLocks(xid);
1092 
1093  for (i = 0; i < nsubxids; i++)
1094  StandbyReleaseLocks(subxids[i]);
1095 }
static void StandbyReleaseLocks(TransactionId xid)
Definition: standby.c:1063

References i, and StandbyReleaseLocks().

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

◆ StandbyReleaseOldLocks()

void StandbyReleaseOldLocks ( TransactionId  oldxid)

Definition at line 1122 of file standby.c.

1123 {
1125  RecoveryLockXidEntry *entry;
1126 
1128  while ((entry = hash_seq_search(&status)))
1129  {
1130  Assert(TransactionIdIsValid(entry->xid));
1131 
1132  /* Skip if prepared transaction. */
1133  if (StandbyTransactionIdIsPrepared(entry->xid))
1134  continue;
1135 
1136  /* Skip if >= oldxid. */
1137  if (!TransactionIdPrecedes(entry->xid, oldxid))
1138  continue;
1139 
1140  /* Remove all locks and hash table entry. */
1143  }
1144 }
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:273
bool StandbyTransactionIdIsPrepared(TransactionId xid)
Definition: twophase.c:1452

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

Referenced by ProcArrayApplyRecoveryInfo().

◆ StandbyReleaseXidEntryLocks()

static void StandbyReleaseXidEntryLocks ( RecoveryLockXidEntry xidentry)
static

Definition at line 1030 of file standby.c.

1031 {
1032  RecoveryLockEntry *entry;
1034 
1035  for (entry = xidentry->head; entry != NULL; entry = next)
1036  {
1037  LOCKTAG locktag;
1038 
1040  "releasing recovery lock: xid %u db %u rel %u",
1041  entry->key.xid, entry->key.dbOid, entry->key.relOid);
1042  /* Release the lock ... */
1043  SET_LOCKTAG_RELATION(locktag, entry->key.dbOid, entry->key.relOid);
1044  if (!LockRelease(&locktag, AccessExclusiveLock, true))
1045  {
1046  elog(LOG,
1047  "RecoveryLockHash contains entry for lock no longer recorded by lock manager: xid %u database %u relation %u",
1048  entry->key.xid, entry->key.dbOid, entry->key.relOid);
1049  Assert(false);
1050  }
1051  /* ... and remove the per-lock hash entry */
1052  next = entry->next;
1053  hash_search(RecoveryLockHash, entry, HASH_REMOVE, NULL);
1054  }
1055 
1056  xidentry->head = NULL; /* just for paranoia */
1057 }
static int32 next
Definition: blutils.c:219
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
xl_standby_lock key
Definition: standby.c:56

References AccessExclusiveLock, Assert(), xl_standby_lock::dbOid, DEBUG4, elog(), HASH_REMOVE, hash_search(), RecoveryLockXidEntry::head, RecoveryLockEntry::key, LockRelease(), LOG, next, RecoveryLockEntry::next, RecoveryLockHash, xl_standby_lock::relOid, SET_LOCKTAG_RELATION, trace_recovery(), and xl_standby_lock::xid.

Referenced by StandbyReleaseAllLocks(), StandbyReleaseLocks(), and StandbyReleaseOldLocks().

◆ StandbyTimeoutHandler()

void StandbyTimeoutHandler ( void  )

Definition at line 939 of file standby.c.

940 {
942 }

References got_standby_delay_timeout.

Referenced by StartupProcessMain().

◆ WaitExceedsMaxStandbyDelay()

static bool WaitExceedsMaxStandbyDelay ( uint32  wait_event_info)
static

Definition at line 234 of file standby.c.

235 {
236  TimestampTz ltime;
237 
239 
240  /* Are we past the limit time? */
241  ltime = GetStandbyLimitTime();
242  if (ltime && GetCurrentTimestamp() >= ltime)
243  return true;
244 
245  /*
246  * Sleep a bit (this is essential to avoid busy-waiting).
247  */
248  pgstat_report_wait_start(wait_event_info);
251 
252  /*
253  * Progressively increase the sleep times, but not to more than 1s, since
254  * pg_usleep isn't interruptible on some platforms.
255  */
256  standbyWait_us *= 2;
257  if (standbyWait_us > 1000000)
258  standbyWait_us = 1000000;
259 
260  return false;
261 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:268
static void pgstat_report_wait_end(void)
Definition: wait_event.h:284

References CHECK_FOR_INTERRUPTS, GetCurrentTimestamp(), GetStandbyLimitTime(), pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), and standbyWait_us.

Referenced by ResolveRecoveryConflictWithVirtualXIDs().

Variable Documentation

◆ got_standby_deadlock_timeout

volatile sig_atomic_t got_standby_deadlock_timeout = false
static

◆ got_standby_delay_timeout

volatile sig_atomic_t got_standby_delay_timeout = false
static

Definition at line 71 of file standby.c.

Referenced by ResolveRecoveryConflictWithBufferPin(), and StandbyTimeoutHandler().

◆ got_standby_lock_timeout

volatile sig_atomic_t got_standby_lock_timeout = false
static

Definition at line 72 of file standby.c.

Referenced by ResolveRecoveryConflictWithLock(), and StandbyLockTimeoutHandler().

◆ log_recovery_conflict_waits

bool log_recovery_conflict_waits = false

◆ max_standby_archive_delay

int max_standby_archive_delay = 30 * 1000

Definition at line 41 of file standby.c.

Referenced by GetStandbyLimitTime().

◆ max_standby_streaming_delay

int max_standby_streaming_delay = 30 * 1000

Definition at line 42 of file standby.c.

Referenced by GetStandbyLimitTime().

◆ RecoveryLockHash

◆ RecoveryLockXidHash

◆ standbyWait_us

int standbyWait_us = STANDBY_INITIAL_WAIT_US
static

◆ vacuum_defer_cleanup_age

int vacuum_defer_cleanup_age

Definition at line 40 of file standby.c.

Referenced by ComputeXidHorizons(), and GetSnapshotData().