1/*-------------------------------------------------------------------------
5 * PostgreSQL write-ahead log manager user interface functions
7 * This file contains WAL control and information functions.
10 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/backend/access/transam/xlogfuncs.c
15 *-------------------------------------------------------------------------
39 * Backup-related variables.
44/* Session-level context for the SQL-callable backup functions */
48 * pg_backup_start: set up for taking an on-line backup dump
50 * Essentially what this does is to create the contents required for the
51 * backup_label file and the tablespace map.
53 * Permission checking for this function is managed through the normal
69 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
70 errmsg(
"a backup is already in progress in this session")));
73 * backup_state and tablespace_map need to be long-lived as they are used
74 * in pg_backup_stop(). These are allocated in a dedicated memory context
75 * child of TopMemoryContext, deleted at the end of pg_backup_stop(). If
76 * an error happens before ending the backup, memory would be leaked in
77 * this context until pg_backup_start() is called again.
82 "on-line backup context",
105 * pg_backup_stop: finish taking an on-line backup.
107 * The first parameter (variable 'waitforarchive'), which is optional,
108 * allows the user to choose if they want to wait for the WAL to be archived
109 * or if we should just return as soon as the WAL record is written.
111 * This function stops an in-progress backup, creates backup_label contents and
112 * it returns the backup stop LSN, backup_label and tablespace_map contents.
114 * The backup_label contains the user-supplied label string (typically this
115 * would be used to tell where the backup dump will be stored), the starting
116 * time, starting WAL location for the dump and so on. It is the caller's
117 * responsibility to write the backup_label and tablespace_map files in the
118 * data folder that will be restored from this backup.
120 * Permission checking for this function is managed through the normal
126#define PG_BACKUP_STOP_V2_COLS 3
134 /* Initialize attributes information in the tuple descriptor */
136 elog(
ERROR,
"return type must be a row type");
140 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
141 errmsg(
"backup is not in progress"),
142 errhint(
"Did you call pg_backup_start()?")));
147 /* Stop the backup */
150 /* Build the contents of backup_label */
157 /* Deallocate backup-related variables */
160 /* Clean up the session-level state and its memory context */
166 /* Returns the record as Datum */
171 * pg_switch_wal: switch to next xlog file
173 * Permission checking for this function is managed through the normal
183 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
184 errmsg(
"recovery is in progress"),
185 errhint(
"WAL control functions cannot be executed during recovery.")));
190 * As a convenience, return the WAL location of the switch record
196 * pg_log_standby_snapshot: call LogStandbySnapshot()
198 * Permission checking for this function is managed through the normal
208 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
209 errmsg(
"recovery is in progress"),
210 errhint(
"%s cannot be executed during recovery.",
211 "pg_log_standby_snapshot()")));
215 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
216 errmsg(
"pg_log_standby_snapshot() can only be used if \"wal_level\" >= \"replica\"")));
221 * As a convenience, return the WAL location of the last inserted record
227 * pg_create_restore_point: a named point for restore
229 * Permission checking for this function is managed through the normal
236 char *restore_name_str;
241 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
242 errmsg(
"recovery is in progress"),
243 errhint(
"WAL control functions cannot be executed during recovery.")));
247 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
248 errmsg(
"WAL level not sufficient for creating a restore point"),
249 errhint(
"\"wal_level\" must be set to \"replica\" or \"logical\" at server start.")));
255 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
256 errmsg(
"value too long for restore point (maximum %d characters)",
MAXFNAMELEN - 1)));
261 * As a convenience, return the WAL location of the restore point record
267 * Report the current WAL write location (same format as pg_backup_start etc)
269 * This is useful for determining how much of WAL is visible to an external
270 * archiving process. Note that the data before this point is written out
271 * to the kernel, but is not necessarily synced to disk.
280 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
281 errmsg(
"recovery is in progress"),
282 errhint(
"WAL control functions cannot be executed during recovery.")));
290 * Report the current WAL insert location (same format as pg_backup_start etc)
292 * This function is mostly for debugging purposes.
301 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
302 errmsg(
"recovery is in progress"),
303 errhint(
"WAL control functions cannot be executed during recovery.")));
311 * Report the current WAL flush location (same format as pg_backup_start etc)
313 * This function is mostly for debugging purposes.
322 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
323 errmsg(
"recovery is in progress"),
324 errhint(
"WAL control functions cannot be executed during recovery.")));
332 * Report the last WAL receive location (same format as pg_backup_start etc)
334 * This is useful for determining how much of WAL is guaranteed to be received
335 * and synced to disk by walreceiver.
351 * Report the last WAL replay location (same format as pg_backup_start etc)
353 * This is useful for determining how much of WAL is visible to read-only
354 * connections during recovery.
370 * Compute an xlog file name and decimal byte offset given a WAL location,
371 * such as is returned by pg_backup_stop() or pg_switch_wal().
388 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
389 errmsg(
"recovery is in progress"),
390 errhint(
"%s cannot be executed during recovery.",
391 "pg_walfile_name_offset()")));
394 * Construct a tuple descriptor for the result row. This must match this
395 * function's pg_proc entry!
424 * Tuple jam: Having first prepared your Datums, then squash together
434 * Compute an xlog file name given a WAL location,
435 * such as is returned by pg_backup_stop() or pg_switch_wal().
446 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
447 errmsg(
"recovery is in progress"),
448 errhint(
"%s cannot be executed during recovery.",
449 "pg_walfile_name()")));
459 * Extract the sequence number and the timeline ID from given a WAL file
465#define PG_SPLIT_WALFILE_NAME_COLS 2
480 /* Capitalize WAL file name. */
481 for (p = fname_upper; *p; p++)
486 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
487 errmsg(
"invalid WAL file name \"%s\"", fname)));
492 elog(
ERROR,
"return type must be a row type");
494 /* Convert to numeric. */
508#undef PG_SPLIT_WALFILE_NAME_COLS
512 * pg_wal_replay_pause - Request to pause recovery
514 * Permission checking for this function is managed through the normal
522 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
523 errmsg(
"recovery is not in progress"),
524 errhint(
"Recovery control functions can only be executed during recovery.")));
528 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
529 errmsg(
"standby promotion is ongoing"),
530 errhint(
"%s cannot be executed after promotion is triggered.",
531 "pg_wal_replay_pause()")));
535 /* wake up the recovery process so that it can process the pause request */
542 * pg_wal_replay_resume - resume recovery now
544 * Permission checking for this function is managed through the normal
552 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
553 errmsg(
"recovery is not in progress"),
554 errhint(
"Recovery control functions can only be executed during recovery.")));
558 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
559 errmsg(
"standby promotion is ongoing"),
560 errhint(
"%s cannot be executed after promotion is triggered.",
561 "pg_wal_replay_resume()")));
569 * pg_is_wal_replay_paused
576 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
577 errmsg(
"recovery is not in progress"),
578 errhint(
"Recovery control functions can only be executed during recovery.")));
584 * pg_get_wal_replay_pause_state - Returns the recovery pause state.
588 * 'not paused' - if pause is not requested
589 * 'pause requested' - if pause is requested but recovery is not yet paused
590 * 'paused' - if recovery is paused
595 char *statestr = NULL;
599 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
600 errmsg(
"recovery is not in progress"),
601 errhint(
"Recovery control functions can only be executed during recovery.")));
603 /* get the recovery pause state */
607 statestr =
"not paused";
610 statestr =
"pause requested";
622 * Returns timestamp of latest processed commit/abort record.
624 * When the server has been started normally without recovery the function
640 * Returns bool with current recovery mode, a global state.
649 * Compute the difference in bytes between two WAL locations.
664 * Promotes a standby server.
666 * A result of "true" means that promotion has been completed if "wait" is
667 * "true", or initiated if "wait" is false.
679 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
680 errmsg(
"recovery is not in progress"),
681 errhint(
"Recovery control functions can only be executed during recovery.")));
685 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
686 errmsg(
"\"wait_seconds\" must not be negative or zero")));
688 /* create the promote signal file */
693 errmsg(
"could not create file \"%s\": %m",
699 errmsg(
"could not write file \"%s\": %m",
702 /* signal the postmaster */
707 (
errcode(ERRCODE_SYSTEM_ERROR),
708 errmsg(
"failed to send signal to postmaster: %m")));
711 /* return immediately if waiting was not requested */
715 /* wait for the amount of time wanted until promotion */
716#define WAITS_PER_SECOND 10
734 * Emergency bailout if postmaster has died. This is to avoid the
735 * necessity for manual cleanup of all postmaster children.
739 (
errcode(ERRCODE_ADMIN_SHUTDOWN),
740 errmsg(
"terminating connection due to unexpected postmaster exit"),
746 "server did not promote within %d seconds",
Datum numeric_in(PG_FUNCTION_ARGS)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
FILE * AllocateFile(const char *name, const char *mode)
#define PG_GETARG_TEXT_PP(n)
#define DirectFunctionCall2(func, arg1, arg2)
#define PG_GETARG_DATUM(n)
#define PG_RETURN_TEXT_P(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define PG_RETURN_BOOL(x)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void ResetLatch(Latch *latch)
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
void MemoryContextReset(MemoryContext context)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext TopMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_START_SMALL_SIZES
#define CHECK_FOR_INTERRUPTS()
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static char promote_file[MAXPGPATH]
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
static Datum LSNGetDatum(XLogRecPtr X)
unsigned char pg_toupper(unsigned char ch)
static Datum Int64GetDatum(int64 X)
static Datum ObjectIdGetDatum(Oid X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
static Datum UInt32GetDatum(uint32 X)
XLogRecPtr LogStandbySnapshot(void)
StringInfo makeStringInfo(void)
TupleDesc CreateTemplateTupleDesc(int natts)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
#define PG_RETURN_TIMESTAMPTZ(x)
text * cstring_to_text(const char *s)
char * text_to_cstring(const text *t)
#define WL_POSTMASTER_DEATH
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool RecoveryInProgress(void)
TimeLineID GetWALInsertionTimeLine(void)
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
SessionBackupState get_backup_status(void)
XLogRecPtr GetXLogInsertRecPtr(void)
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
void register_persistent_abort_backup_handler(void)
XLogRecPtr GetXLogWriteRecPtr(void)
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
XLogRecPtr XLogRestorePoint(const char *rpName)
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
#define PROMOTE_SIGNAL_FILE
#define XLogStandbyInfoActive()
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static bool IsXLogFileName(const char *fname)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
char * build_backup_content(BackupState *state, bool ishistoryfile)
Datum pg_is_wal_replay_paused(PG_FUNCTION_ARGS)
Datum pg_wal_lsn_diff(PG_FUNCTION_ARGS)
Datum pg_backup_start(PG_FUNCTION_ARGS)
Datum pg_create_restore_point(PG_FUNCTION_ARGS)
Datum pg_current_wal_insert_lsn(PG_FUNCTION_ARGS)
Datum pg_switch_wal(PG_FUNCTION_ARGS)
Datum pg_is_in_recovery(PG_FUNCTION_ARGS)
#define PG_SPLIT_WALFILE_NAME_COLS
Datum pg_split_walfile_name(PG_FUNCTION_ARGS)
Datum pg_backup_stop(PG_FUNCTION_ARGS)
Datum pg_last_xact_replay_timestamp(PG_FUNCTION_ARGS)
Datum pg_log_standby_snapshot(PG_FUNCTION_ARGS)
static BackupState * backup_state
Datum pg_current_wal_lsn(PG_FUNCTION_ARGS)
Datum pg_last_wal_receive_lsn(PG_FUNCTION_ARGS)
#define PG_BACKUP_STOP_V2_COLS
Datum pg_walfile_name(PG_FUNCTION_ARGS)
Datum pg_current_wal_flush_lsn(PG_FUNCTION_ARGS)
Datum pg_walfile_name_offset(PG_FUNCTION_ARGS)
static MemoryContext backupcontext
static StringInfo tablespace_map
Datum pg_promote(PG_FUNCTION_ARGS)
Datum pg_get_wal_replay_pause_state(PG_FUNCTION_ARGS)
Datum pg_last_wal_replay_lsn(PG_FUNCTION_ARGS)
Datum pg_wal_replay_pause(PG_FUNCTION_ARGS)
Datum pg_wal_replay_resume(PG_FUNCTION_ARGS)
void SetRecoveryPause(bool recoveryPause)
void WakeupRecovery(void)
bool PromoteIsTriggered(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
RecoveryPauseState GetRecoveryPauseState(void)
TimestampTz GetLatestXTime(void)
@ RECOVERY_PAUSE_REQUESTED