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  RecoveryLockListsEntry
 

Macros

#define STANDBY_INITIAL_WAIT_US   1000
 

Typedefs

typedef struct RecoveryLockListsEntry RecoveryLockListsEntry
 

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 latestRemovedXid, RelFileNode node)
 
void ResolveRecoveryConflictWithSnapshotFullXid (FullTransactionId latestRemovedFullXid, RelFileNode node)
 
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 StandbyReleaseLockList (List *locks)
 
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 HTABRecoveryLockLists
 
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 202 of file standby.c.

Typedef Documentation

◆ RecoveryLockListsEntry

Function Documentation

◆ CheckRecoveryConflictDeadlock()

void CheckRecoveryConflictDeadlock ( void  )

Definition at line 867 of file standby.c.

868 {
869  Assert(!InRecovery); /* do not call in Startup process */
870 
872  return;
873 
874  /*
875  * Error message should match ProcessInterrupts() but we avoid calling
876  * that because we aren't handling an interrupt at this point. Note that
877  * we only cancel the current transaction here, so if we are in a
878  * subtransaction and the pin is held by a parent, then the Startup
879  * process will continue to wait even though we have avoided deadlock.
880  */
881  ereport(ERROR,
883  errmsg("canceling statement due to conflict with recovery"),
884  errdetail("User transaction caused buffer deadlock with recovery.")));
885 }
bool HoldingBufferPinThatDelaysRecovery(void)
Definition: bufmgr.c:4364
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
Assert(fmt[strlen(fmt) - 1] !='\n')
#define ERRCODE_T_R_DEADLOCK_DETECTED
Definition: pgbench.c:80
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 1421 of file standby.c.

1422 {
1423  const char *reasonDesc = _("unknown reason");
1424 
1425  switch (reason)
1426  {
1428  reasonDesc = _("recovery conflict on buffer pin");
1429  break;
1431  reasonDesc = _("recovery conflict on lock");
1432  break;
1434  reasonDesc = _("recovery conflict on tablespace");
1435  break;
1437  reasonDesc = _("recovery conflict on snapshot");
1438  break;
1440  reasonDesc = _("recovery conflict on buffer deadlock");
1441  break;
1443  reasonDesc = _("recovery conflict on database");
1444  break;
1445  default:
1446  break;
1447  }
1448 
1449  return reasonDesc;
1450 }
#define _(x)
Definition: elog.c:89
@ 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 178 of file standby.c.

179 {
180  TimestampTz rtime;
181  bool fromStream;
182 
183  /*
184  * The cutoff time is the last WAL data receipt time plus the appropriate
185  * delay variable. Delay of -1 means wait forever.
186  */
187  GetXLogReceiptTime(&rtime, &fromStream);
188  if (fromStream)
189  {
191  return 0; /* wait forever */
193  }
194  else
195  {
197  return 0; /* wait forever */
199  }
200 }
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:56
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 83 of file standby.c.

84 {
86  HASHCTL hash_ctl;
87 
88  /*
89  * Initialize the hash table for tracking the list of locks held by each
90  * transaction.
91  */
92  hash_ctl.keysize = sizeof(TransactionId);
93  hash_ctl.entrysize = sizeof(RecoveryLockListsEntry);
94  RecoveryLockLists = hash_create("RecoveryLockLists",
95  64,
96  &hash_ctl,
98 
99  /*
100  * Initialize shared invalidation management for Startup process, being
101  * careful to register ourselves as a sendOnly process so we don't need to
102  * read messages, nor will we get signaled when the queue starts filling
103  * up.
104  */
106 
107  /*
108  * Lock a virtual transaction id for Startup process.
109  *
110  * We need to do GetNextLocalTransactionId() because
111  * SharedInvalBackendInit() leaves localTransactionId invalid and the lock
112  * manager doesn't like that at all.
113  *
114  * Note that we don't need to run XactLockTableInsert() because nobody
115  * needs to wait on xids. That sounds a little strange, but table locks
116  * are held by vxids and row level locks are held by xids. All queries
117  * hold AccessShareLocks so never block while we write or lock new rows.
118  */
119  vxid.backendId = MyBackendId;
122 
124 }
uint32 TransactionId
Definition: c.h:587
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
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
void SharedInvalBackendInit(bool sendOnly)
Definition: sinvaladt.c:266
LocalTransactionId GetNextLocalTransactionId(void)
Definition: sinvaladt.c:775
struct RecoveryLockListsEntry RecoveryLockListsEntry
static HTAB * RecoveryLockLists
Definition: standby.c:45
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 VirtualTransactionId::backendId, HASHCTL::entrysize, GetNextLocalTransactionId(), HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, VirtualTransactionId::localTransactionId, MyBackendId, RecoveryLockLists, SharedInvalBackendInit(), STANDBY_INITIALIZED, standbyState, and VirtualXactLockTableInsert().

Referenced by StartupXLOG().

◆ LogAccessExclusiveLock()

void LogAccessExclusiveLock ( Oid  dbOid,
Oid  relOid 
)

Definition at line 1360 of file standby.c.

1361 {
1362  xl_standby_lock xlrec;
1363 
1364  xlrec.xid = GetCurrentTransactionId();
1365 
1366  xlrec.dbOid = dbOid;
1367  xlrec.relOid = relOid;
1368 
1369  LogAccessExclusiveLocks(1, &xlrec);
1371 }
static void LogAccessExclusiveLocks(int nlocks, xl_standby_lock *locks)
Definition: standby.c:1342
TransactionId xid
Definition: lockdefs.h:51
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:441
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 1377 of file standby.c.

1378 {
1379  /*
1380  * Ensure that a TransactionId has been assigned to this transaction, for
1381  * two reasons, both related to lock release on the standby. First, we
1382  * must assign an xid so that RecordTransactionCommit() and
1383  * RecordTransactionAbort() do not optimise away the transaction
1384  * completion record which recovery relies upon to release locks. It's a
1385  * hack, but for a corner case not worth adding code for into the main
1386  * commit path. Second, we must assign an xid before the lock is recorded
1387  * in shared memory, otherwise a concurrently executing
1388  * GetRunningTransactionLocks() might see a lock associated with an
1389  * InvalidTransactionId which we later assert cannot happen.
1390  */
1391  (void) GetCurrentTransactionId();
1392 }

References GetCurrentTransactionId().

Referenced by LockAcquireExtended().

◆ LogAccessExclusiveLocks()

static void LogAccessExclusiveLocks ( int  nlocks,
xl_standby_lock locks 
)
static

Definition at line 1342 of file standby.c.

1343 {
1344  xl_standby_locks xlrec;
1345 
1346  xlrec.nlocks = nlocks;
1347 
1348  XLogBeginInsert();
1349  XLogRegisterData((char *) &xlrec, offsetof(xl_standby_locks, locks));
1350  XLogRegisterData((char *) locks, nlocks * sizeof(xl_standby_lock));
1352 
1353  (void) XLogInsert(RM_STANDBY_ID, XLOG_STANDBY_LOCK);
1354 }
#define offsetof(type, field)
Definition: c.h:727
#define XLOG_STANDBY_LOCK
Definition: standbydefs.h:34
#define XLOG_MARK_UNIMPORTANT
Definition: xlog.h:150
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:443
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:425
void XLogBeginInsert(void)
Definition: xloginsert.c:150
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:351

References xl_standby_locks::nlocks, offsetof, 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 1282 of file standby.c.

1283 {
1284  xl_running_xacts xlrec;
1285  XLogRecPtr recptr;
1286 
1287  xlrec.xcnt = CurrRunningXacts->xcnt;
1288  xlrec.subxcnt = CurrRunningXacts->subxcnt;
1289  xlrec.subxid_overflow = CurrRunningXacts->subxid_overflow;
1290  xlrec.nextXid = CurrRunningXacts->nextXid;
1291  xlrec.oldestRunningXid = CurrRunningXacts->oldestRunningXid;
1292  xlrec.latestCompletedXid = CurrRunningXacts->latestCompletedXid;
1293 
1294  /* Header */
1295  XLogBeginInsert();
1297  XLogRegisterData((char *) (&xlrec), MinSizeOfXactRunningXacts);
1298 
1299  /* array of TransactionIds */
1300  if (xlrec.xcnt > 0)
1301  XLogRegisterData((char *) CurrRunningXacts->xids,
1302  (xlrec.xcnt + xlrec.subxcnt) * sizeof(TransactionId));
1303 
1304  recptr = XLogInsert(RM_STANDBY_ID, XLOG_RUNNING_XACTS);
1305 
1306  if (CurrRunningXacts->subxid_overflow)
1308  "snapshot of %u running transactions overflowed (lsn %X/%X oldest xid %u latest complete %u next xid %u)",
1309  CurrRunningXacts->xcnt,
1310  LSN_FORMAT_ARGS(recptr),
1311  CurrRunningXacts->oldestRunningXid,
1312  CurrRunningXacts->latestCompletedXid,
1313  CurrRunningXacts->nextXid);
1314  else
1316  "snapshot of %u+%u running transaction ids (lsn %X/%X oldest xid %u latest complete %u next xid %u)",
1317  CurrRunningXacts->xcnt, CurrRunningXacts->subxcnt,
1318  LSN_FORMAT_ARGS(recptr),
1319  CurrRunningXacts->oldestRunningXid,
1320  CurrRunningXacts->latestCompletedXid,
1321  CurrRunningXacts->nextXid);
1322 
1323  /*
1324  * Ensure running_xacts information is synced to disk not too far in the
1325  * future. We don't want to stall anything though (i.e. use XLogFlush()),
1326  * so we let the wal writer do it during normal operation.
1327  * XLogSetAsyncXactLSN() conveniently will mark the LSN as to-be-synced
1328  * and nudge the WALWriter into action if sleeping. Check
1329  * XLogBackgroundFlush() for details why a record might not be flushed
1330  * without it.
1331  */
1332  XLogSetAsyncXactLSN(recptr);
1333 
1334  return recptr;
1335 }
int trace_recovery(int trace_level)
Definition: elog.c:3423
#define DEBUG2
Definition: elog.h:23
#define elog(elevel,...)
Definition: elog.h:218
#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:2354
#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 251 of file standby.c.

254 {
255  long secs;
256  int usecs;
257  long msecs;
259  int nprocs = 0;
260 
261  /*
262  * There must be no conflicting processes when the recovery conflict has
263  * already been resolved.
264  */
265  Assert(still_waiting || wait_list == NULL);
266 
267  TimestampDifference(wait_start, now, &secs, &usecs);
268  msecs = secs * 1000 + usecs / 1000;
269  usecs = usecs % 1000;
270 
271  if (wait_list)
272  {
273  VirtualTransactionId *vxids;
274 
275  /* Construct a string of list of the conflicting processes */
276  vxids = wait_list;
277  while (VirtualTransactionIdIsValid(*vxids))
278  {
279  PGPROC *proc = BackendIdGetProc(vxids->backendId);
280 
281  /* proc can be NULL if the target backend is not active */
282  if (proc)
283  {
284  if (nprocs == 0)
285  {
287  appendStringInfo(&buf, "%d", proc->pid);
288  }
289  else
290  appendStringInfo(&buf, ", %d", proc->pid);
291 
292  nprocs++;
293  }
294 
295  vxids++;
296  }
297  }
298 
299  /*
300  * If wait_list is specified, report the list of PIDs of active
301  * conflicting backends in a detail message. Note that if all the backends
302  * in the list are not active, no detail message is logged.
303  */
304  if (still_waiting)
305  {
306  ereport(LOG,
307  errmsg("recovery still waiting after %ld.%03d ms: %s",
308  msecs, usecs, get_recovery_conflict_desc(reason)),
309  nprocs > 0 ? errdetail_log_plural("Conflicting process: %s.",
310  "Conflicting processes: %s.",
311  nprocs, buf.data) : 0);
312  }
313  else
314  {
315  ereport(LOG,
316  errmsg("recovery finished waiting after %ld.%03d ms: %s",
317  msecs, usecs, get_recovery_conflict_desc(reason)));
318  }
319 
320  if (nprocs > 0)
321  pfree(buf.data);
322 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1650
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1106
#define LOG
Definition: elog.h:25
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:72
void pfree(void *pointer)
Definition: mcxt.c:1175
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:1421
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Definition: proc.h:160
int pid
Definition: proc.h:184

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 1399 of file standby.c.

1401 {
1402  xl_invalidations xlrec;
1403 
1404  /* prepare record */
1405  memset(&xlrec, 0, sizeof(xlrec));
1406  xlrec.dbId = MyDatabaseId;
1407  xlrec.tsId = MyDatabaseTableSpace;
1408  xlrec.relcacheInitFileInval = relcacheInitFileInval;
1409  xlrec.nmsgs = nmsgs;
1410 
1411  /* perform insertion */
1412  XLogBeginInsert();
1413  XLogRegisterData((char *) (&xlrec), MinSizeOfInvalidations);
1414  XLogRegisterData((char *) msgs,
1415  nmsgs * sizeof(SharedInvalidationMessage));
1416  XLogInsert(RM_STANDBY_ID, XLOG_INVALIDATIONS);
1417 }
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 1222 of file standby.c.

1223 {
1224  XLogRecPtr recptr;
1225  RunningTransactions running;
1226  xl_standby_lock *locks;
1227  int nlocks;
1228 
1230 
1231  /*
1232  * Get details of any AccessExclusiveLocks being held at the moment.
1233  */
1234  locks = GetRunningTransactionLocks(&nlocks);
1235  if (nlocks > 0)
1236  LogAccessExclusiveLocks(nlocks, locks);
1237  pfree(locks);
1238 
1239  /*
1240  * Log details of all in-progress transactions. This should be the last
1241  * record we write, because standby will open up when it sees this.
1242  */
1243  running = GetRunningTransactionData();
1244 
1245  /*
1246  * GetRunningTransactionData() acquired ProcArrayLock, we must release it.
1247  * For Hot Standby this can be done before inserting the WAL record
1248  * because ProcArrayApplyRecoveryInfo() rechecks the commit status using
1249  * the clog. For logical decoding, though, the lock can't be released
1250  * early because the clog might be "in the future" from the POV of the
1251  * historic snapshot. This would allow for situations where we're waiting
1252  * for the end of a transaction listed in the xl_running_xacts record
1253  * which, according to the WAL, has committed before the xl_running_xacts
1254  * record. Fortunately this routine isn't executed frequently, and it's
1255  * only a shared lock.
1256  */
1258  LWLockRelease(ProcArrayLock);
1259 
1260  recptr = LogCurrentRunningXacts(running);
1261 
1262  /* Release lock if we kept it longer ... */
1264  LWLockRelease(ProcArrayLock);
1265 
1266  /* GetRunningTransactionData() acquired XidGenLock, we must release it */
1267  LWLockRelease(XidGenLock);
1268 
1269  return recptr;
1270 }
xl_standby_lock * GetRunningTransactionLocks(int *nlocks)
Definition: lock.c:4018
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
RunningTransactions GetRunningTransactionData(void)
Definition: procarray.c:2737
static XLogRecPtr LogCurrentRunningXacts(RunningTransactions CurrRunningXacts)
Definition: standby.c:1282
int wal_level
Definition: xlog.c:132
@ 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 755 of file standby.c.

756 {
757  TimestampTz ltime;
758 
760 
761  ltime = GetStandbyLimitTime();
762 
763  if (GetCurrentTimestamp() >= ltime && ltime != 0)
764  {
765  /*
766  * We're already behind, so clear a path as quickly as possible.
767  */
769  }
770  else
771  {
772  /*
773  * Wake up at ltime, and check for deadlocks as well if we will be
774  * waiting longer than deadlock_timeout
775  */
776  EnableTimeoutParams timeouts[2];
777  int cnt = 0;
778 
779  if (ltime != 0)
780  {
781  timeouts[cnt].id = STANDBY_TIMEOUT;
782  timeouts[cnt].type = TMPARAM_AT;
783  timeouts[cnt].fin_time = ltime;
784  cnt++;
785  }
786 
788  timeouts[cnt].id = STANDBY_DEADLOCK_TIMEOUT;
789  timeouts[cnt].type = TMPARAM_AFTER;
790  timeouts[cnt].delay_ms = DeadlockTimeout;
791  cnt++;
792 
793  enable_timeouts(timeouts, cnt);
794  }
795 
796  /*
797  * Wait to be signaled by UnpinBuffer() or for the wait to be interrupted
798  * by one of the timeouts established above.
799  *
800  * We assume that only UnpinBuffer() and the timeout requests established
801  * above can wake us up here. WakeupRecovery() called by walreceiver or
802  * SIGHUP signal handler, etc cannot do that because it uses the different
803  * latch from that ProcWaitForSignal() waits on.
804  */
806 
810  {
811  /*
812  * Send out a request for hot-standby backends to check themselves for
813  * deadlocks.
814  *
815  * XXX The subsequent ResolveRecoveryConflictWithBufferPin() will wait
816  * to be signaled by UnpinBuffer() again and send a request for
817  * deadlocks check if deadlock_timeout happens. This causes the
818  * request to continue to be sent every deadlock_timeout until the
819  * buffer is unpinned or ltime is reached. This would increase the
820  * workload in the startup process and backends. In practice it may
821  * not be so harmful because the period that the buffer is kept pinned
822  * is basically no so long. But we should fix this?
823  */
825  }
826 
827  /*
828  * Clear any timeout requests established above. We assume here that the
829  * Startup process doesn't have any other timeouts than what this function
830  * uses. If that stops being true, we could cancel the timeouts
831  * individually, but that'd be slower.
832  */
833  disable_all_timeouts(false);
836 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
int DeadlockTimeout
Definition: proc.c:60
void ProcWaitForSignal(uint32 wait_event_info)
Definition: proc.c:1873
static volatile sig_atomic_t got_standby_deadlock_timeout
Definition: standby.c:48
static TimestampTz GetStandbyLimitTime(void)
Definition: standby.c:178
static volatile sig_atomic_t got_standby_delay_timeout
Definition: standby.c:49
static void SendRecoveryConflictWithBufferPin(ProcSignalReason reason)
Definition: standby.c:839
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 531 of file standby.c.

532 {
533  /*
534  * We don't do ResolveRecoveryConflictWithVirtualXIDs() here since that
535  * only waits for transactions and completely idle sessions would block
536  * us. This is rare enough that we do this as simply as possible: no wait,
537  * just force them off immediately.
538  *
539  * No locking is required here because we already acquired
540  * AccessExclusiveLock. Anybody trying to connect while we do this will
541  * block during InitPostgres() and then disconnect when they see the
542  * database has been removed.
543  */
544  while (CountDBBackends(dbid) > 0)
545  {
547 
548  /*
549  * Wait awhile for them to die so that we avoid flooding an
550  * unresponsive backend when system is heavily loaded.
551  */
552  pg_usleep(10000);
553  }
554 }
void CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
Definition: procarray.c:3629
int CountDBBackends(Oid databaseid)
Definition: procarray.c:3568
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 585 of file standby.c.

586 {
587  TimestampTz ltime;
589 
591 
592  ltime = GetStandbyLimitTime();
594 
595  /*
596  * Update waitStart if first time through after the startup process
597  * started waiting for the lock. It should not be updated every time
598  * ResolveRecoveryConflictWithLock() is called during the wait.
599  *
600  * Use the current time obtained for comparison with ltime as waitStart
601  * (i.e., the time when this process started waiting for the lock). Since
602  * getting the current time newly can cause overhead, we reuse the
603  * already-obtained time to avoid that overhead.
604  *
605  * Note that waitStart is updated without holding the lock table's
606  * partition lock, to avoid the overhead by additional lock acquisition.
607  * This can cause "waitstart" in pg_locks to become NULL for a very short
608  * period of time after the wait started even though "granted" is false.
609  * This is OK in practice because we can assume that users are likely to
610  * look at "waitstart" when waiting for the lock for a long time.
611  */
612  if (pg_atomic_read_u64(&MyProc->waitStart) == 0)
614 
615  if (now >= ltime && ltime != 0)
616  {
617  /*
618  * We're already behind, so clear a path as quickly as possible.
619  */
620  VirtualTransactionId *backends;
621 
622  backends = GetLockConflicts(&locktag, AccessExclusiveLock, NULL);
623 
624  /*
625  * Prevent ResolveRecoveryConflictWithVirtualXIDs() from reporting
626  * "waiting" in PS display by disabling its argument report_waiting
627  * because the caller, WaitOnLock(), has already reported that.
628  */
631  PG_WAIT_LOCK | locktag.locktag_type,
632  false);
633  }
634  else
635  {
636  /*
637  * Wait (or wait again) until ltime, and check for deadlocks as well
638  * if we will be waiting longer than deadlock_timeout
639  */
640  EnableTimeoutParams timeouts[2];
641  int cnt = 0;
642 
643  if (ltime != 0)
644  {
645  got_standby_lock_timeout = false;
646  timeouts[cnt].id = STANDBY_LOCK_TIMEOUT;
647  timeouts[cnt].type = TMPARAM_AT;
648  timeouts[cnt].fin_time = ltime;
649  cnt++;
650  }
651 
653  timeouts[cnt].id = STANDBY_DEADLOCK_TIMEOUT;
654  timeouts[cnt].type = TMPARAM_AFTER;
655  timeouts[cnt].delay_ms = DeadlockTimeout;
656  cnt++;
657 
658  enable_timeouts(timeouts, cnt);
659  }
660 
661  /* Wait to be signaled by the release of the Relation Lock */
663 
664  /*
665  * Exit if ltime is reached. Then all the backends holding conflicting
666  * locks will be canceled in the next ResolveRecoveryConflictWithLock()
667  * call.
668  */
670  goto cleanup;
671 
673  {
674  VirtualTransactionId *backends;
675 
676  backends = GetLockConflicts(&locktag, AccessExclusiveLock, NULL);
677 
678  /* Quick exit if there's no work to be done */
679  if (!VirtualTransactionIdIsValid(*backends))
680  goto cleanup;
681 
682  /*
683  * Send signals to all the backends holding the conflicting locks, to
684  * ask them to check themselves for deadlocks.
685  */
686  while (VirtualTransactionIdIsValid(*backends))
687  {
688  SignalVirtualTransaction(*backends,
690  false);
691  backends++;
692  }
693 
694  /*
695  * Exit if the recovery conflict has not been logged yet even though
696  * logging is enabled, so that the caller can log that. Then
697  * RecoveryConflictWithLock() is called again and we will wait again
698  * for the lock to be released.
699  */
700  if (logging_conflict)
701  goto cleanup;
702 
703  /*
704  * Wait again here to be signaled by the release of the Relation Lock,
705  * to prevent the subsequent RecoveryConflictWithLock() from causing
706  * deadlock_timeout and sending a request for deadlocks check again.
707  * Otherwise the request continues to be sent every deadlock_timeout
708  * until the relation locks are released or ltime is reached.
709  */
712  }
713 
714 cleanup:
715 
716  /*
717  * Clear any timeout requests established above. We assume here that the
718  * Startup process doesn't have any other outstanding timeouts than those
719  * used by this function. If that stops being true, we could cancel the
720  * timeouts individually, but that'd be slower.
721  */
722  disable_all_timeouts(false);
723  got_standby_lock_timeout = false;
725 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:438
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:429
static void cleanup(void)
Definition: bootstrap.c:697
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:3466
PGPROC * MyProc
Definition: proc.c:68
static void ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist, ProcSignalReason reason, uint32 wait_event_info, bool report_waiting)
Definition: standby.c:337
static volatile sig_atomic_t got_standby_lock_timeout
Definition: standby.c:50
uint8 locktag_type
Definition: lock.h:173
pg_atomic_uint64 waitStart
Definition: proc.h:222
@ 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  latestRemovedXid,
RelFileNode  node 
)

Definition at line 445 of file standby.c.

446 {
447  VirtualTransactionId *backends;
448 
449  /*
450  * If we get passed InvalidTransactionId then we do nothing (no conflict).
451  *
452  * This can happen when replaying already-applied WAL records after a
453  * standby crash or restart, or when replaying an XLOG_HEAP2_VISIBLE
454  * record that marks as frozen a page which was already all-visible. It's
455  * also quite common with records generated during index deletion
456  * (original execution of the deletion can reason that a recovery conflict
457  * which is sufficient for the deletion operation must take place before
458  * replay of the deletion record itself).
459  */
460  if (!TransactionIdIsValid(latestRemovedXid))
461  return;
462 
463  backends = GetConflictingVirtualXIDs(latestRemovedXid,
464  node.dbNode);
465 
469  true);
470 }
VirtualTransactionId * GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
Definition: procarray.c:3386
#define TransactionIdIsValid(xid)
Definition: transam.h:41
@ WAIT_EVENT_RECOVERY_CONFLICT_SNAPSHOT
Definition: wait_event.h:120

References RelFileNode::dbNode, 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  latestRemovedFullXid,
RelFileNode  node 
)

Definition at line 477 of file standby.c.

479 {
480  /*
481  * ResolveRecoveryConflictWithSnapshot operates on 32-bit TransactionIds,
482  * so truncate the logged FullTransactionId. If the logged value is very
483  * old, so that XID wrap-around already happened on it, there can't be any
484  * snapshots that still see it.
485  */
487  uint64 diff;
488 
489  diff = U64FromFullTransactionId(nextXid) -
490  U64FromFullTransactionId(latestRemovedFullXid);
491  if (diff < MaxTransactionId / 2)
492  {
493  TransactionId latestRemovedXid;
494 
495  latestRemovedXid = XidFromFullTransactionId(latestRemovedFullXid);
496  ResolveRecoveryConflictWithSnapshot(latestRemovedXid, node);
497  }
498 }
void ResolveRecoveryConflictWithSnapshot(TransactionId latestRemovedXid, RelFileNode node)
Definition: standby.c:445
#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 501 of file standby.c.

502 {
503  VirtualTransactionId *temp_file_users;
504 
505  /*
506  * Standby users may be currently using this tablespace for their
507  * temporary files. We only care about current users because
508  * temp_tablespace parameter will just ignore tablespaces that no longer
509  * exist.
510  *
511  * Ask everybody to cancel their queries immediately so we can ensure no
512  * temp files remain and we can remove the tablespace. Nuke the entire
513  * site from orbit, it's the only way to be sure.
514  *
515  * XXX: We could work out the pids of active backends using this
516  * tablespace by examining the temp filenames in the directory. We would
517  * then convert the pids into VirtualXIDs before attempting to cancel
518  * them.
519  *
520  * We don't wait for commit because drop tablespace is non-transactional.
521  */
523  InvalidOid);
527  true);
528 }
#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 337 of file standby.c.

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

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 839 of file standby.c.

840 {
843 
844  /*
845  * We send signal to all backends to ask them if they are holding the
846  * buffer pin which is delaying the Startup process. We must not set the
847  * conflict flag yet, since most backends will be innocent. Let the
848  * SIGUSR1 handling in each backend decide their own fate.
849  */
850  CancelDBBackends(InvalidOid, reason, false);
851 }

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

Referenced by ResolveRecoveryConflictWithBufferPin().

◆ ShutdownRecoveryTransactionEnvironment()

void ShutdownRecoveryTransactionEnvironment ( void  )

Definition at line 140 of file standby.c.

141 {
142  /*
143  * Do nothing if RecoveryLockLists is NULL because which means that
144  * transaction tracking has not been yet initialized or has been already
145  * shutdowned. This prevents transaction tracking from being shutdowned
146  * unexpectedly more than once.
147  */
148  if (RecoveryLockLists == NULL)
149  return;
150 
151  /* Mark all tracked in-progress transactions as finished. */
153 
154  /* Release all locks the tracked transactions were holding */
156 
157  /* Destroy the hash table of locks. */
159  RecoveryLockLists = NULL;
160 
161  /* Cleanup our VirtualTransaction */
163 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
void VirtualXactLockTableCleanup(void)
Definition: lock.c:4499
void ExpireAllKnownAssignedTransactionIds(void)
Definition: procarray.c:4487
void StandbyReleaseAllLocks(void)
Definition: standby.c:1051

References ExpireAllKnownAssignedTransactionIds(), hash_destroy(), RecoveryLockLists, StandbyReleaseAllLocks(), and VirtualXactLockTableCleanup().

Referenced by StartupProcExit(), and StartupXLOG().

◆ standby_redo()

void standby_redo ( XLogReaderState record)

Definition at line 1105 of file standby.c.

1106 {
1107  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1108 
1109  /* Backup blocks are not used in standby records */
1110  Assert(!XLogRecHasAnyBlockRefs(record));
1111 
1112  /* Do nothing if we're not in hot standby mode */
1114  return;
1115 
1116  if (info == XLOG_STANDBY_LOCK)
1117  {
1118  xl_standby_locks *xlrec = (xl_standby_locks *) XLogRecGetData(record);
1119  int i;
1120 
1121  for (i = 0; i < xlrec->nlocks; i++)
1123  xlrec->locks[i].dbOid,
1124  xlrec->locks[i].relOid);
1125  }
1126  else if (info == XLOG_RUNNING_XACTS)
1127  {
1128  xl_running_xacts *xlrec = (xl_running_xacts *) XLogRecGetData(record);
1129  RunningTransactionsData running;
1130 
1131  running.xcnt = xlrec->xcnt;
1132  running.subxcnt = xlrec->subxcnt;
1133  running.subxid_overflow = xlrec->subxid_overflow;
1134  running.nextXid = xlrec->nextXid;
1135  running.latestCompletedXid = xlrec->latestCompletedXid;
1136  running.oldestRunningXid = xlrec->oldestRunningXid;
1137  running.xids = xlrec->xids;
1138 
1139  ProcArrayApplyRecoveryInfo(&running);
1140  }
1141  else if (info == XLOG_INVALIDATIONS)
1142  {
1143  xl_invalidations *xlrec = (xl_invalidations *) XLogRecGetData(record);
1144 
1146  xlrec->nmsgs,
1147  xlrec->relcacheInitFileInval,
1148  xlrec->dbId,
1149  xlrec->tsId);
1150  }
1151  else
1152  elog(PANIC, "standby_redo: unknown op code %u", info);
1153 }
unsigned char uint8
Definition: c.h:439
#define PANIC
Definition: elog.h:36
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:1039
void StandbyAcquireAccessExclusiveLock(TransactionId xid, Oid dbOid, Oid relOid)
Definition: standby.c:948
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:408
#define XLogRecGetData(decoder)
Definition: xlogreader.h:413
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:415
#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 948 of file standby.c.

949 {
950  RecoveryLockListsEntry *entry;
951  xl_standby_lock *newlock;
952  LOCKTAG locktag;
953  bool found;
954 
955  /* Already processed? */
956  if (!TransactionIdIsValid(xid) ||
957  TransactionIdDidCommit(xid) ||
959  return;
960 
962  "adding recovery lock: db %u rel %u", dbOid, relOid);
963 
964  /* dbOid is InvalidOid when we are locking a shared relation. */
965  Assert(OidIsValid(relOid));
966 
967  /* Create a new list for this xid, if we don't have one already. */
968  entry = hash_search(RecoveryLockLists, &xid, HASH_ENTER, &found);
969  if (!found)
970  {
971  entry->xid = xid;
972  entry->locks = NIL;
973  }
974 
975  newlock = palloc(sizeof(xl_standby_lock));
976  newlock->xid = xid;
977  newlock->dbOid = dbOid;
978  newlock->relOid = relOid;
979  entry->locks = lappend(entry->locks, newlock);
980 
981  SET_LOCKTAG_RELATION(locktag, newlock->dbOid, newlock->relOid);
982 
983  (void) LockAcquire(&locktag, AccessExclusiveLock, true, false);
984 }
#define OidIsValid(objectId)
Definition: c.h:710
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
#define DEBUG4
Definition: elog.h:21
@ HASH_ENTER
Definition: hsearch.h:114
List * lappend(List *list, void *datum)
Definition: list.c:336
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
#define NIL
Definition: pg_list.h:65
Definition: lock.h:168
TransactionId xid
Definition: standby.c:66
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:125
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:181

References AccessExclusiveLock, Assert(), xl_standby_lock::dbOid, DEBUG4, elog, HASH_ENTER, hash_search(), lappend(), LockAcquire(), RecoveryLockListsEntry::locks, NIL, OidIsValid, palloc(), RecoveryLockLists, xl_standby_lock::relOid, SET_LOCKTAG_RELATION, trace_recovery(), TransactionIdDidAbort(), TransactionIdDidCommit(), TransactionIdIsValid, RecoveryLockListsEntry::xid, and xl_standby_lock::xid.

Referenced by lock_twophase_standby_recover(), and standby_redo().

◆ StandbyDeadLockHandler()

void StandbyDeadLockHandler ( void  )

Definition at line 898 of file standby.c.

899 {
901 }

References got_standby_deadlock_timeout.

Referenced by StartupProcessMain().

◆ StandbyLockTimeoutHandler()

void StandbyLockTimeoutHandler ( void  )

Definition at line 916 of file standby.c.

917 {
919 }

References got_standby_lock_timeout.

Referenced by StartupProcessMain().

◆ StandbyReleaseAllLocks()

void StandbyReleaseAllLocks ( void  )

Definition at line 1051 of file standby.c.

1052 {
1054  RecoveryLockListsEntry *entry;
1055 
1056  elog(trace_recovery(DEBUG2), "release all standby locks");
1057 
1059  while ((entry = hash_seq_search(&status)))
1060  {
1061  StandbyReleaseLockList(entry->locks);
1062  hash_search(RecoveryLockLists, entry, HASH_REMOVE, NULL);
1063  }
1064 }
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
@ HASH_REMOVE
Definition: hsearch.h:115
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
static void StandbyReleaseLockList(List *locks)
Definition: standby.c:987

References DEBUG2, elog, HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), RecoveryLockListsEntry::locks, RecoveryLockLists, StandbyReleaseLockList(), status(), and trace_recovery().

Referenced by ShutdownRecoveryTransactionEnvironment(), and StandbyReleaseLocks().

◆ StandbyReleaseLockList()

static void StandbyReleaseLockList ( List locks)
static

Definition at line 987 of file standby.c.

988 {
989  ListCell *lc;
990 
991  foreach(lc, locks)
992  {
993  xl_standby_lock *lock = (xl_standby_lock *) lfirst(lc);
994  LOCKTAG locktag;
995 
997  "releasing recovery lock: xid %u db %u rel %u",
998  lock->xid, lock->dbOid, lock->relOid);
999  SET_LOCKTAG_RELATION(locktag, lock->dbOid, lock->relOid);
1000  if (!LockRelease(&locktag, AccessExclusiveLock, true))
1001  {
1002  elog(LOG,
1003  "RecoveryLockLists contains entry for lock no longer recorded by lock manager: xid %u database %u relation %u",
1004  lock->xid, lock->dbOid, lock->relOid);
1005  Assert(false);
1006  }
1007  }
1008 
1009  list_free_deep(locks);
1010 }
void list_free_deep(List *list)
Definition: list.c:1519
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
#define lfirst(lc)
Definition: pg_list.h:169

References AccessExclusiveLock, Assert(), xl_standby_lock::dbOid, DEBUG4, elog, lfirst, list_free_deep(), LockRelease(), LOG, xl_standby_lock::relOid, SET_LOCKTAG_RELATION, trace_recovery(), and xl_standby_lock::xid.

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

◆ StandbyReleaseLocks()

static void StandbyReleaseLocks ( TransactionId  xid)
static

Definition at line 1013 of file standby.c.

1014 {
1015  RecoveryLockListsEntry *entry;
1016 
1017  if (TransactionIdIsValid(xid))
1018  {
1019  if ((entry = hash_search(RecoveryLockLists, &xid, HASH_FIND, NULL)))
1020  {
1021  StandbyReleaseLockList(entry->locks);
1022  hash_search(RecoveryLockLists, entry, HASH_REMOVE, NULL);
1023  }
1024  }
1025  else
1027 }
@ HASH_FIND
Definition: hsearch.h:113

References HASH_FIND, HASH_REMOVE, hash_search(), RecoveryLockListsEntry::locks, RecoveryLockLists, StandbyReleaseAllLocks(), StandbyReleaseLockList(), and TransactionIdIsValid.

Referenced by StandbyReleaseLockTree().

◆ StandbyReleaseLockTree()

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

Definition at line 1037 of file standby.c.

1038 {
1039  int i;
1040 
1041  StandbyReleaseLocks(xid);
1042 
1043  for (i = 0; i < nsubxids; i++)
1044  StandbyReleaseLocks(subxids[i]);
1045 }
static void StandbyReleaseLocks(TransactionId xid)
Definition: standby.c:1013

References i, and StandbyReleaseLocks().

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

◆ StandbyReleaseOldLocks()

void StandbyReleaseOldLocks ( TransactionId  oldxid)

Definition at line 1072 of file standby.c.

1073 {
1075  RecoveryLockListsEntry *entry;
1076 
1078  while ((entry = hash_seq_search(&status)))
1079  {
1080  Assert(TransactionIdIsValid(entry->xid));
1081 
1082  /* Skip if prepared transaction. */
1083  if (StandbyTransactionIdIsPrepared(entry->xid))
1084  continue;
1085 
1086  /* Skip if >= oldxid. */
1087  if (!TransactionIdPrecedes(entry->xid, oldxid))
1088  continue;
1089 
1090  /* Remove all locks and hash table entry. */
1091  StandbyReleaseLockList(entry->locks);
1092  hash_search(RecoveryLockLists, entry, HASH_REMOVE, NULL);
1093  }
1094 }
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool StandbyTransactionIdIsPrepared(TransactionId xid)
Definition: twophase.c:1454

References Assert(), HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), RecoveryLockListsEntry::locks, RecoveryLockLists, StandbyReleaseLockList(), StandbyTransactionIdIsPrepared(), status(), TransactionIdIsValid, TransactionIdPrecedes(), and RecoveryLockListsEntry::xid.

Referenced by ProcArrayApplyRecoveryInfo().

◆ StandbyTimeoutHandler()

void StandbyTimeoutHandler ( void  )

Definition at line 907 of file standby.c.

908 {
910 }

References got_standby_delay_timeout.

Referenced by StartupProcessMain().

◆ WaitExceedsMaxStandbyDelay()

static bool WaitExceedsMaxStandbyDelay ( uint32  wait_event_info)
static

Definition at line 211 of file standby.c.

212 {
213  TimestampTz ltime;
214 
216 
217  /* Are we past the limit time? */
218  ltime = GetStandbyLimitTime();
219  if (ltime && GetCurrentTimestamp() >= ltime)
220  return true;
221 
222  /*
223  * Sleep a bit (this is essential to avoid busy-waiting).
224  */
225  pgstat_report_wait_start(wait_event_info);
228 
229  /*
230  * Progressively increase the sleep times, but not to more than 1s, since
231  * pg_usleep isn't interruptible on some platforms.
232  */
233  standbyWait_us *= 2;
234  if (standbyWait_us > 1000000)
235  standbyWait_us = 1000000;
236 
237  return false;
238 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:266
static void pgstat_report_wait_end(void)
Definition: wait_event.h:282

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

◆ RecoveryLockLists

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