PostgreSQL Source Code  git master
varsup.c File Reference
#include "postgres.h"
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/subtrans.h"
#include "access/transam.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "commands/dbcommands.h"
#include "miscadmin.h"
#include "postmaster/autovacuum.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "utils/syscache.h"
Include dependency graph for varsup.c:

Go to the source code of this file.

Macros

#define VAR_OID_PREFETCH   8192
 

Functions

TransactionId GetNewTransactionId (bool isSubXact)
 
TransactionId ReadNewTransactionId (void)
 
void AdvanceOldestClogXid (TransactionId oldest_datfrozenxid)
 
void SetTransactionIdLimit (TransactionId oldest_datfrozenxid, Oid oldest_datoid)
 
bool ForceTransactionIdLimitUpdate (void)
 
Oid GetNewObjectId (void)
 

Variables

VariableCache ShmemVariableCache = NULL
 

Macro Definition Documentation

◆ VAR_OID_PREFETCH

#define VAR_OID_PREFETCH   8192

Definition at line 31 of file varsup.c.

Referenced by GetNewObjectId().

Function Documentation

◆ AdvanceOldestClogXid()

void AdvanceOldestClogXid ( TransactionId  oldest_datfrozenxid)

Definition at line 263 of file varsup.c.

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VariableCacheData::oldestClogXid, and TransactionIdPrecedes().

Referenced by BootStrapXLOG(), clog_redo(), StartupXLOG(), and TruncateCLOG().

264 {
265  LWLockAcquire(CLogTruncationLock, LW_EXCLUSIVE);
267  oldest_datfrozenxid))
268  {
269  ShmemVariableCache->oldestClogXid = oldest_datfrozenxid;
270  }
271  LWLockRelease(CLogTruncationLock);
272 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
TransactionId oldestClogXid
Definition: transam.h:141
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121

◆ ForceTransactionIdLimitUpdate()

bool ForceTransactionIdLimitUpdate ( void  )

Definition at line 421 of file varsup.c.

References DATABASEOID, LW_SHARED, LWLockAcquire(), LWLockRelease(), VariableCacheData::nextXid, ObjectIdGetDatum, VariableCacheData::oldestXid, VariableCacheData::oldestXidDB, SearchSysCacheExists1, TransactionIdFollowsOrEquals(), TransactionIdIsNormal, TransactionIdIsValid, and VariableCacheData::xidVacLimit.

Referenced by vac_update_datfrozenxid().

422 {
423  TransactionId nextXid;
424  TransactionId xidVacLimit;
425  TransactionId oldestXid;
426  Oid oldestXidDB;
427 
428  /* Locking is probably not really necessary, but let's be careful */
429  LWLockAcquire(XidGenLock, LW_SHARED);
430  nextXid = ShmemVariableCache->nextXid;
431  xidVacLimit = ShmemVariableCache->xidVacLimit;
432  oldestXid = ShmemVariableCache->oldestXid;
433  oldestXidDB = ShmemVariableCache->oldestXidDB;
434  LWLockRelease(XidGenLock);
435 
436  if (!TransactionIdIsNormal(oldestXid))
437  return true; /* shouldn't happen, but just in case */
438  if (!TransactionIdIsValid(xidVacLimit))
439  return true; /* this shouldn't happen anymore either */
440  if (TransactionIdFollowsOrEquals(nextXid, xidVacLimit))
441  return true; /* past VacLimit, don't delay updating */
443  return true; /* could happen, per comments above */
444  return false;
445 }
uint32 TransactionId
Definition: c.h:507
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
TransactionId oldestXid
Definition: transam.h:119
unsigned int Oid
Definition: postgres_ext.h:31
TransactionId xidVacLimit
Definition: transam.h:120
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:182
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

◆ GetNewObjectId()

Oid GetNewObjectId ( void  )

Definition at line 459 of file varsup.c.

References elog, ERROR, FirstBootstrapObjectId, FirstNormalObjectId, IsPostmasterEnvironment, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VariableCacheData::nextOid, VariableCacheData::oidCount, RecoveryInProgress(), VAR_OID_PREFETCH, and XLogPutNextOid().

Referenced by GetNewOidWithIndex(), and GetNewRelFileNode().

460 {
461  Oid result;
462 
463  /* safety check, we should never get this far in a HS standby */
464  if (RecoveryInProgress())
465  elog(ERROR, "cannot assign OIDs during recovery");
466 
467  LWLockAcquire(OidGenLock, LW_EXCLUSIVE);
468 
469  /*
470  * Check for wraparound of the OID counter. We *must* not return 0
471  * (InvalidOid), and in normal operation we mustn't return anything below
472  * FirstNormalObjectId since that range is reserved for initdb (see
473  * IsCatalogClass()). Note we are relying on unsigned comparison.
474  *
475  * During initdb, we start the OID generator at FirstBootstrapObjectId, so
476  * we only wrap if before that point when in bootstrap or standalone mode.
477  * The first time through this routine after normal postmaster start, the
478  * counter will be forced up to FirstNormalObjectId. This mechanism
479  * leaves the OIDs between FirstBootstrapObjectId and FirstNormalObjectId
480  * available for automatic assignment during initdb, while ensuring they
481  * will never conflict with user-assigned OIDs.
482  */
484  {
486  {
487  /* wraparound, or first post-initdb assignment, in normal mode */
490  }
491  else
492  {
493  /* we may be bootstrapping, so don't enforce the full range */
495  {
496  /* wraparound in standalone mode (unlikely but possible) */
499  }
500  }
501  }
502 
503  /* If we run out of logged for use oids then we must log more */
504  if (ShmemVariableCache->oidCount == 0)
505  {
508  }
509 
510  result = ShmemVariableCache->nextOid;
511 
514 
515  LWLockRelease(OidGenLock);
516 
517  return result;
518 }
bool IsPostmasterEnvironment
Definition: globals.c:108
uint32 oidCount
Definition: transam.h:112
#define VAR_OID_PREFETCH
Definition: varsup.c:31
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:7888
#define FirstNormalObjectId
Definition: transam.h:94
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define ERROR
Definition: elog.h:43
void XLogPutNextOid(Oid nextOid)
Definition: xlog.c:9371
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define FirstBootstrapObjectId
Definition: transam.h:93
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
#define elog(elevel,...)
Definition: elog.h:226

◆ GetNewTransactionId()

TransactionId GetNewTransactionId ( bool  isSubXact)

Definition at line 48 of file varsup.c.

References Assert, BootstrapTransactionId, elog, ereport, errcode(), errhint(), errmsg(), ERROR, ExtendCLOG(), ExtendCommitTs(), ExtendSUBTRANS(), get_database_name(), IsBootstrapProcessingMode, IsInParallelMode(), IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyPgXact, MyProc, VariableCacheData::nextXid, PGXACT::nxids, VariableCacheData::oldestXidDB, PGXACT::overflowed, pg_write_barrier, PGPROC_MAX_CACHED_SUBXIDS, PMSIGNAL_START_AUTOVAC_LAUNCHER, RecoveryInProgress(), SendPostmasterSignal(), PGPROC::subxids, TransactionIdAdvance, TransactionIdFollowsOrEquals(), WARNING, PGXACT::xid, XidCache::xids, VariableCacheData::xidStopLimit, VariableCacheData::xidVacLimit, VariableCacheData::xidWarnLimit, and VariableCacheData::xidWrapLimit.

Referenced by AssignTransactionId().

49 {
50  TransactionId xid;
51 
52  /*
53  * Workers synchronize transaction state at the beginning of each parallel
54  * operation, so we can't account for new XIDs after that point.
55  */
56  if (IsInParallelMode())
57  elog(ERROR, "cannot assign TransactionIds during a parallel operation");
58 
59  /*
60  * During bootstrap initialization, we return the special bootstrap
61  * transaction id.
62  */
64  {
65  Assert(!isSubXact);
68  }
69 
70  /* safety check, we should never get this far in a HS standby */
71  if (RecoveryInProgress())
72  elog(ERROR, "cannot assign TransactionIds during recovery");
73 
74  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
75 
77 
78  /*----------
79  * Check to see if it's safe to assign another XID. This protects against
80  * catastrophic data loss due to XID wraparound. The basic rules are:
81  *
82  * If we're past xidVacLimit, start trying to force autovacuum cycles.
83  * If we're past xidWarnLimit, start issuing warnings.
84  * If we're past xidStopLimit, refuse to execute transactions, unless
85  * we are running in single-user mode (which gives an escape hatch
86  * to the DBA who somehow got past the earlier defenses).
87  *
88  * Note that this coding also appears in GetNewMultiXactId.
89  *----------
90  */
92  {
93  /*
94  * For safety's sake, we release XidGenLock while sending signals,
95  * warnings, etc. This is not so much because we care about
96  * preserving concurrency in this situation, as to avoid any
97  * possibility of deadlock while doing get_database_name(). First,
98  * copy all the shared values we'll need in this path.
99  */
103  Oid oldest_datoid = ShmemVariableCache->oldestXidDB;
104 
105  LWLockRelease(XidGenLock);
106 
107  /*
108  * To avoid swamping the postmaster with signals, we issue the autovac
109  * request only once per 64K transaction starts. This still gives
110  * plenty of chances before we get into real trouble.
111  */
112  if (IsUnderPostmaster && (xid % 65536) == 0)
114 
115  if (IsUnderPostmaster &&
116  TransactionIdFollowsOrEquals(xid, xidStopLimit))
117  {
118  char *oldest_datname = get_database_name(oldest_datoid);
119 
120  /* complain even if that DB has disappeared */
121  if (oldest_datname)
122  ereport(ERROR,
123  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
124  errmsg("database is not accepting commands to avoid wraparound data loss in database \"%s\"",
125  oldest_datname),
126  errhint("Stop the postmaster and vacuum that database in single-user mode.\n"
127  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
128  else
129  ereport(ERROR,
130  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
131  errmsg("database is not accepting commands to avoid wraparound data loss in database with OID %u",
132  oldest_datoid),
133  errhint("Stop the postmaster and vacuum that database in single-user mode.\n"
134  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
135  }
136  else if (TransactionIdFollowsOrEquals(xid, xidWarnLimit))
137  {
138  char *oldest_datname = get_database_name(oldest_datoid);
139 
140  /* complain even if that DB has disappeared */
141  if (oldest_datname)
143  (errmsg("database \"%s\" must be vacuumed within %u transactions",
144  oldest_datname,
145  xidWrapLimit - xid),
146  errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
147  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
148  else
150  (errmsg("database with OID %u must be vacuumed within %u transactions",
151  oldest_datoid,
152  xidWrapLimit - xid),
153  errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
154  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
155  }
156 
157  /* Re-acquire lock and start over */
158  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
160  }
161 
162  /*
163  * If we are allocating the first XID of a new page of the commit log,
164  * zero out that commit-log page before returning. We must do this while
165  * holding XidGenLock, else another xact could acquire and commit a later
166  * XID before we zero the page. Fortunately, a page of the commit log
167  * holds 32K or more transactions, so we don't have to do this very often.
168  *
169  * Extend pg_subtrans and pg_commit_ts too.
170  */
171  ExtendCLOG(xid);
172  ExtendCommitTs(xid);
173  ExtendSUBTRANS(xid);
174 
175  /*
176  * Now advance the nextXid counter. This must not happen until after we
177  * have successfully completed ExtendCLOG() --- if that routine fails, we
178  * want the next incoming transaction to try it again. We cannot assign
179  * more XIDs until there is CLOG space for them.
180  */
182 
183  /*
184  * We must store the new XID into the shared ProcArray before releasing
185  * XidGenLock. This ensures that every active XID older than
186  * latestCompletedXid is present in the ProcArray, which is essential for
187  * correct OldestXmin tracking; see src/backend/access/transam/README.
188  *
189  * Note that readers of PGXACT xid fields should be careful to fetch the
190  * value only once, rather than assume they can read a value multiple
191  * times and get the same answer each time. Note we are assuming that
192  * TransactionId and int fetch/store are atomic.
193  *
194  * The same comments apply to the subxact xid count and overflow fields.
195  *
196  * Use of a write barrier prevents dangerous code rearrangement in this
197  * function; other backends could otherwise e.g. be examining my subxids
198  * info concurrently, and we don't want them to see an invalid
199  * intermediate state, such as an incremented nxids before the array entry
200  * is filled.
201  *
202  * Other processes that read nxids should do so before reading xids
203  * elements with a pg_read_barrier() in between, so that they can be sure
204  * not to read an uninitialized array element; see
205  * src/backend/storage/lmgr/README.barrier.
206  *
207  * If there's no room to fit a subtransaction XID into PGPROC, set the
208  * cache-overflowed flag instead. This forces readers to look in
209  * pg_subtrans to map subtransaction XIDs up to top-level XIDs. There is a
210  * race-condition window, in that the new XID will not appear as running
211  * until its parent link has been placed into pg_subtrans. However, that
212  * will happen before anyone could possibly have a reason to inquire about
213  * the status of the XID, so it seems OK. (Snapshots taken during this
214  * window *will* include the parent XID, so they will deliver the correct
215  * answer later on when someone does have a reason to inquire.)
216  */
217  if (!isSubXact)
218  MyPgXact->xid = xid; /* LWLockRelease acts as barrier */
219  else
220  {
221  int nxids = MyPgXact->nxids;
222 
223  if (nxids < PGPROC_MAX_CACHED_SUBXIDS)
224  {
225  MyProc->subxids.xids[nxids] = xid;
227  MyPgXact->nxids = nxids + 1;
228  }
229  else
230  MyPgXact->overflowed = true;
231  }
232 
233  LWLockRelease(XidGenLock);
234 
235  return xid;
236 }
#define TransactionIdAdvance(dest)
Definition: transam.h:48
int errhint(const char *fmt,...)
Definition: elog.c:974
uint32 TransactionId
Definition: c.h:507
PGPROC * MyProc
Definition: proc.c:67
TransactionId xid
Definition: proc.h:224
void ExtendCLOG(TransactionId newestXact)
Definition: clog.c:867
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:7888
TransactionId xidVacLimit
Definition: transam.h:120
PGXACT * MyPgXact
Definition: proc.c:68
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
bool IsInParallelMode(void)
Definition: xact.c:909
#define ERROR
Definition: elog.h:43
void ExtendSUBTRANS(TransactionId newestXact)
Definition: subtrans.c:324
TransactionId nextXid
Definition: transam.h:117
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2058
#define BootstrapTransactionId
Definition: transam.h:32
TransactionId xidStopLimit
Definition: transam.h:122
bool IsUnderPostmaster
Definition: globals.c:109
uint8 nxids
Definition: proc.h:238
VariableCache ShmemVariableCache
Definition: varsup.c:34
struct XidCache subxids
Definition: proc.h:162
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
bool overflowed
Definition: proc.h:234
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:40
#define Assert(condition)
Definition: c.h:732
void ExtendCommitTs(TransactionId newestXact)
Definition: commit_ts.c:784
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374
TransactionId xidWarnLimit
Definition: transam.h:121
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
TransactionId xidWrapLimit
Definition: transam.h:123
#define pg_write_barrier()
Definition: atomics.h:162
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:146

◆ ReadNewTransactionId()

TransactionId ReadNewTransactionId ( void  )

Definition at line 242 of file varsup.c.

References LW_SHARED, LWLockAcquire(), LWLockRelease(), and VariableCacheData::nextXid.

Referenced by _bt_unlink_halfdead_page(), ActivateCommitTs(), AutoVacWorkerMain(), do_start_worker(), GetNextXidAndEpoch(), GetStableLatestTransactionId(), vac_truncate_clog(), vac_update_datfrozenxid(), vac_update_relstats(), and vacuum_set_xid_limits().

243 {
244  TransactionId xid;
245 
246  LWLockAcquire(XidGenLock, LW_SHARED);
248  LWLockRelease(XidGenLock);
249 
250  return xid;
251 }
uint32 TransactionId
Definition: c.h:507
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121

◆ SetTransactionIdLimit()

void SetTransactionIdLimit ( TransactionId  oldest_datfrozenxid,
Oid  oldest_datoid 
)

Definition at line 280 of file varsup.c.

References Assert, autovacuum_freeze_max_age, DEBUG1, ereport, errhint(), errmsg(), FirstNormalTransactionId, get_database_name(), InRecovery, IsTransactionState(), IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxTransactionId, VariableCacheData::nextXid, VariableCacheData::oldestXid, VariableCacheData::oldestXidDB, PMSIGNAL_START_AUTOVAC_LAUNCHER, SendPostmasterSignal(), TransactionIdFollowsOrEquals(), TransactionIdIsNormal, WARNING, VariableCacheData::xidStopLimit, VariableCacheData::xidVacLimit, VariableCacheData::xidWarnLimit, and VariableCacheData::xidWrapLimit.

Referenced by BootStrapXLOG(), StartupXLOG(), vac_truncate_clog(), and xlog_redo().

281 {
282  TransactionId xidVacLimit;
283  TransactionId xidWarnLimit;
284  TransactionId xidStopLimit;
285  TransactionId xidWrapLimit;
286  TransactionId curXid;
287 
288  Assert(TransactionIdIsNormal(oldest_datfrozenxid));
289 
290  /*
291  * The place where we actually get into deep trouble is halfway around
292  * from the oldest potentially-existing XID. (This calculation is
293  * probably off by one or two counts, because the special XIDs reduce the
294  * size of the loop a little bit. But we throw in plenty of slop below,
295  * so it doesn't matter.)
296  */
297  xidWrapLimit = oldest_datfrozenxid + (MaxTransactionId >> 1);
298  if (xidWrapLimit < FirstNormalTransactionId)
299  xidWrapLimit += FirstNormalTransactionId;
300 
301  /*
302  * We'll refuse to continue assigning XIDs in interactive mode once we get
303  * within 1M transactions of data loss. This leaves lots of room for the
304  * DBA to fool around fixing things in a standalone backend, while not
305  * being significant compared to total XID space. (Note that since
306  * vacuuming requires one transaction per table cleaned, we had better be
307  * sure there's lots of XIDs left...)
308  */
309  xidStopLimit = xidWrapLimit - 1000000;
310  if (xidStopLimit < FirstNormalTransactionId)
311  xidStopLimit -= FirstNormalTransactionId;
312 
313  /*
314  * We'll start complaining loudly when we get within 10M transactions of
315  * the stop point. This is kind of arbitrary, but if you let your gas
316  * gauge get down to 1% of full, would you be looking for the next gas
317  * station? We need to be fairly liberal about this number because there
318  * are lots of scenarios where most transactions are done by automatic
319  * clients that won't pay attention to warnings. (No, we're not gonna make
320  * this configurable. If you know enough to configure it, you know enough
321  * to not get in this kind of trouble in the first place.)
322  */
323  xidWarnLimit = xidStopLimit - 10000000;
324  if (xidWarnLimit < FirstNormalTransactionId)
325  xidWarnLimit -= FirstNormalTransactionId;
326 
327  /*
328  * We'll start trying to force autovacuums when oldest_datfrozenxid gets
329  * to be more than autovacuum_freeze_max_age transactions old.
330  *
331  * Note: guc.c ensures that autovacuum_freeze_max_age is in a sane range,
332  * so that xidVacLimit will be well before xidWarnLimit.
333  *
334  * Note: autovacuum_freeze_max_age is a PGC_POSTMASTER parameter so that
335  * we don't have to worry about dealing with on-the-fly changes in its
336  * value. It doesn't look practical to update shared state from a GUC
337  * assign hook (too many processes would try to execute the hook,
338  * resulting in race conditions as well as crashes of those not connected
339  * to shared memory). Perhaps this can be improved someday. See also
340  * SetMultiXactIdLimit.
341  */
342  xidVacLimit = oldest_datfrozenxid + autovacuum_freeze_max_age;
343  if (xidVacLimit < FirstNormalTransactionId)
344  xidVacLimit += FirstNormalTransactionId;
345 
346  /* Grab lock for just long enough to set the new limit values */
347  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
348  ShmemVariableCache->oldestXid = oldest_datfrozenxid;
349  ShmemVariableCache->xidVacLimit = xidVacLimit;
350  ShmemVariableCache->xidWarnLimit = xidWarnLimit;
351  ShmemVariableCache->xidStopLimit = xidStopLimit;
352  ShmemVariableCache->xidWrapLimit = xidWrapLimit;
353  ShmemVariableCache->oldestXidDB = oldest_datoid;
354  curXid = ShmemVariableCache->nextXid;
355  LWLockRelease(XidGenLock);
356 
357  /* Log the info */
358  ereport(DEBUG1,
359  (errmsg("transaction ID wrap limit is %u, limited by database with OID %u",
360  xidWrapLimit, oldest_datoid)));
361 
362  /*
363  * If past the autovacuum force point, immediately signal an autovac
364  * request. The reason for this is that autovac only processes one
365  * database per invocation. Once it's finished cleaning up the oldest
366  * database, it'll call here, and we'll signal the postmaster to start
367  * another iteration immediately if there are still any old databases.
368  */
369  if (TransactionIdFollowsOrEquals(curXid, xidVacLimit) &&
372 
373  /* Give an immediate warning if past the wrap warn point */
374  if (TransactionIdFollowsOrEquals(curXid, xidWarnLimit) && !InRecovery)
375  {
376  char *oldest_datname;
377 
378  /*
379  * We can be called when not inside a transaction, for example during
380  * StartupXLOG(). In such a case we cannot do database access, so we
381  * must just report the oldest DB's OID.
382  *
383  * Note: it's also possible that get_database_name fails and returns
384  * NULL, for example because the database just got dropped. We'll
385  * still warn, even though the warning might now be unnecessary.
386  */
387  if (IsTransactionState())
388  oldest_datname = get_database_name(oldest_datoid);
389  else
390  oldest_datname = NULL;
391 
392  if (oldest_datname)
394  (errmsg("database \"%s\" must be vacuumed within %u transactions",
395  oldest_datname,
396  xidWrapLimit - curXid),
397  errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
398  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
399  else
401  (errmsg("database with OID %u must be vacuumed within %u transactions",
402  oldest_datoid,
403  xidWrapLimit - curXid),
404  errhint("To avoid a database shutdown, execute a database-wide VACUUM in that database.\n"
405  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
406  }
407 }
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:974
uint32 TransactionId
Definition: c.h:507
bool InRecovery
Definition: xlog.c:197
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
TransactionId oldestXid
Definition: transam.h:119
TransactionId xidVacLimit
Definition: transam.h:120
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define FirstNormalTransactionId
Definition: transam.h:34
int autovacuum_freeze_max_age
Definition: autovacuum.c:121
TransactionId nextXid
Definition: transam.h:117
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2058
TransactionId xidStopLimit
Definition: transam.h:122
bool IsUnderPostmaster
Definition: globals.c:109
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define MaxTransactionId
Definition: transam.h:35
#define Assert(condition)
Definition: c.h:732
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
bool IsTransactionState(void)
Definition: xact.c:332
TransactionId xidWarnLimit
Definition: transam.h:121
int errmsg(const char *fmt,...)
Definition: elog.c:784
TransactionId xidWrapLimit
Definition: transam.h:123
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:146
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

Variable Documentation

◆ ShmemVariableCache