PostgreSQL Source Code  git master
xlogfuncs.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/htup_details.h"
#include "access/xlog_internal.h"
#include "access/xlogbackup.h"
#include "access/xlogrecovery.h"
#include "catalog/pg_type.h"
#include "commands/waitlsn.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/walreceiver.h"
#include "storage/fd.h"
#include "storage/proc.h"
#include "storage/standby.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/snapmgr.h"
#include "utils/timestamp.h"
Include dependency graph for xlogfuncs.c:

Go to the source code of this file.

Macros

#define PG_BACKUP_STOP_V2_COLS   3
 
#define PG_SPLIT_WALFILE_NAME_COLS   2
 
#define WAITS_PER_SECOND   10
 

Functions

Datum pg_backup_start (PG_FUNCTION_ARGS)
 
Datum pg_backup_stop (PG_FUNCTION_ARGS)
 
Datum pg_switch_wal (PG_FUNCTION_ARGS)
 
Datum pg_log_standby_snapshot (PG_FUNCTION_ARGS)
 
Datum pg_create_restore_point (PG_FUNCTION_ARGS)
 
Datum pg_current_wal_lsn (PG_FUNCTION_ARGS)
 
Datum pg_current_wal_insert_lsn (PG_FUNCTION_ARGS)
 
Datum pg_current_wal_flush_lsn (PG_FUNCTION_ARGS)
 
Datum pg_last_wal_receive_lsn (PG_FUNCTION_ARGS)
 
Datum pg_last_wal_replay_lsn (PG_FUNCTION_ARGS)
 
Datum pg_walfile_name_offset (PG_FUNCTION_ARGS)
 
Datum pg_walfile_name (PG_FUNCTION_ARGS)
 
Datum pg_split_walfile_name (PG_FUNCTION_ARGS)
 
Datum pg_wal_replay_pause (PG_FUNCTION_ARGS)
 
Datum pg_wal_replay_resume (PG_FUNCTION_ARGS)
 
Datum pg_is_wal_replay_paused (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_replay_pause_state (PG_FUNCTION_ARGS)
 
Datum pg_last_xact_replay_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_is_in_recovery (PG_FUNCTION_ARGS)
 
Datum pg_wal_lsn_diff (PG_FUNCTION_ARGS)
 
Datum pg_promote (PG_FUNCTION_ARGS)
 
Datum pg_wal_replay_wait (PG_FUNCTION_ARGS)
 

Variables

static BackupStatebackup_state = NULL
 
static StringInfo tablespace_map = NULL
 
static MemoryContext backupcontext = NULL
 

Macro Definition Documentation

◆ PG_BACKUP_STOP_V2_COLS

#define PG_BACKUP_STOP_V2_COLS   3

◆ PG_SPLIT_WALFILE_NAME_COLS

#define PG_SPLIT_WALFILE_NAME_COLS   2

◆ WAITS_PER_SECOND

#define WAITS_PER_SECOND   10

Function Documentation

◆ pg_backup_start()

Datum pg_backup_start ( PG_FUNCTION_ARGS  )

Definition at line 59 of file xlogfuncs.c.

60 {
61  text *backupid = PG_GETARG_TEXT_PP(0);
62  bool fast = PG_GETARG_BOOL(1);
63  char *backupidstr;
65  MemoryContext oldcontext;
66 
67  backupidstr = text_to_cstring(backupid);
68 
69  if (status == SESSION_BACKUP_RUNNING)
70  ereport(ERROR,
71  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
72  errmsg("a backup is already in progress in this session")));
73 
74  /*
75  * backup_state and tablespace_map need to be long-lived as they are used
76  * in pg_backup_stop(). These are allocated in a dedicated memory context
77  * child of TopMemoryContext, deleted at the end of pg_backup_stop(). If
78  * an error happens before ending the backup, memory would be leaked in
79  * this context until pg_backup_start() is called again.
80  */
81  if (backupcontext == NULL)
82  {
84  "on-line backup context",
86  }
87  else
88  {
89  backup_state = NULL;
90  tablespace_map = NULL;
92  }
93 
97  MemoryContextSwitchTo(oldcontext);
98 
100  do_pg_backup_start(backupidstr, fast, NULL, backup_state, tablespace_map);
101 
103 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc0(Size size)
Definition: mcxt.c:1347
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:177
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:34
MemoryContextSwitchTo(old_ctx)
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
XLogRecPtr startpoint
Definition: xlogbackup.h:26
Definition: c.h:687
char * text_to_cstring(const text *t)
Definition: varlena.c:217
SessionBackupState get_backup_status(void)
Definition: xlog.c:9093
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:9413
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8784
SessionBackupState
Definition: xlog.h:285
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:287
static BackupState * backup_state
Definition: xlogfuncs.c:43
static MemoryContext backupcontext
Definition: xlogfuncs.c:47
static StringInfo tablespace_map
Definition: xlogfuncs.c:44

References ALLOCSET_START_SMALL_SIZES, AllocSetContextCreate, backup_state, backupcontext, do_pg_backup_start(), ereport, errcode(), errmsg(), ERROR, get_backup_status(), makeStringInfo(), MemoryContextReset(), MemoryContextSwitchTo(), palloc0(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_LSN, register_persistent_abort_backup_handler(), SESSION_BACKUP_RUNNING, BackupState::startpoint, tablespace_map, text_to_cstring(), and TopMemoryContext.

◆ pg_backup_stop()

Datum pg_backup_stop ( PG_FUNCTION_ARGS  )

Definition at line 126 of file xlogfuncs.c.

127 {
128 #define PG_BACKUP_STOP_V2_COLS 3
129  TupleDesc tupdesc;
131  bool nulls[PG_BACKUP_STOP_V2_COLS] = {0};
132  bool waitforarchive = PG_GETARG_BOOL(0);
133  char *backup_label;
135 
136  /* Initialize attributes information in the tuple descriptor */
137  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
138  elog(ERROR, "return type must be a row type");
139 
140  if (status != SESSION_BACKUP_RUNNING)
141  ereport(ERROR,
142  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
143  errmsg("backup is not in progress"),
144  errhint("Did you call pg_backup_start()?")));
145 
146  Assert(backup_state != NULL);
147  Assert(tablespace_map != NULL);
148 
149  /* Stop the backup */
150  do_pg_backup_stop(backup_state, waitforarchive);
151 
152  /* Build the contents of backup_label */
153  backup_label = build_backup_content(backup_state, false);
154 
156  values[1] = CStringGetTextDatum(backup_label);
158 
159  /* Deallocate backup-related variables */
160  pfree(backup_label);
161 
162  /* Clean up the session-level state and its memory context */
163  backup_state = NULL;
164  tablespace_map = NULL;
166  backupcontext = NULL;
167 
168  /* Returns the record as Datum */
170 }
static Datum values[MAXATTR]
Definition: bootstrap.c:150
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define Assert(condition)
Definition: c.h:858
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define elog(elevel,...)
Definition: elog.h:225
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void pfree(void *pointer)
Definition: mcxt.c:1521
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
uintptr_t Datum
Definition: postgres.h:64
XLogRecPtr stoppoint
Definition: xlogbackup.h:35
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition: xlog.c:9112
char * build_backup_content(BackupState *state, bool ishistoryfile)
Definition: xlogbackup.c:29
#define PG_BACKUP_STOP_V2_COLS

References Assert, backup_state, backupcontext, build_backup_content(), CStringGetTextDatum, StringInfoData::data, do_pg_backup_stop(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, get_backup_status(), get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), LSNGetDatum(), MemoryContextDelete(), pfree(), PG_BACKUP_STOP_V2_COLS, PG_GETARG_BOOL, PG_RETURN_DATUM, SESSION_BACKUP_RUNNING, BackupState::stoppoint, tablespace_map, TYPEFUNC_COMPOSITE, and values.

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 235 of file xlogfuncs.c.

236 {
237  text *restore_name = PG_GETARG_TEXT_PP(0);
238  char *restore_name_str;
239  XLogRecPtr restorepoint;
240 
241  if (RecoveryInProgress())
242  ereport(ERROR,
243  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
244  errmsg("recovery is in progress"),
245  errhint("WAL control functions cannot be executed during recovery.")));
246 
247  if (!XLogIsNeeded())
248  ereport(ERROR,
249  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
250  errmsg("WAL level not sufficient for creating a restore point"),
251  errhint("\"wal_level\" must be set to \"replica\" or \"logical\" at server start.")));
252 
253  restore_name_str = text_to_cstring(restore_name);
254 
255  if (strlen(restore_name_str) >= MAXFNAMELEN)
256  ereport(ERROR,
257  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
258  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
259 
260  restorepoint = XLogRestorePoint(restore_name_str);
261 
262  /*
263  * As a convenience, return the WAL location of the restore point record
264  */
265  PG_RETURN_LSN(restorepoint);
266 }
bool RecoveryInProgress(void)
Definition: xlog.c:6333
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:8070
#define XLogIsNeeded()
Definition: xlog.h:109
#define MAXFNAMELEN
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References ereport, errcode(), errhint(), errmsg(), ERROR, MAXFNAMELEN, PG_GETARG_TEXT_PP, PG_RETURN_LSN, RecoveryInProgress(), text_to_cstring(), XLogIsNeeded, and XLogRestorePoint().

◆ pg_current_wal_flush_lsn()

Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 318 of file xlogfuncs.c.

319 {
320  XLogRecPtr current_recptr;
321 
322  if (RecoveryInProgress())
323  ereport(ERROR,
324  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
325  errmsg("recovery is in progress"),
326  errhint("WAL control functions cannot be executed during recovery.")));
327 
328  current_recptr = GetFlushRecPtr(NULL);
329 
330  PG_RETURN_LSN(current_recptr);
331 }
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6498

References ereport, errcode(), errhint(), errmsg(), ERROR, GetFlushRecPtr(), PG_RETURN_LSN, and RecoveryInProgress().

◆ pg_current_wal_insert_lsn()

Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 297 of file xlogfuncs.c.

298 {
299  XLogRecPtr current_recptr;
300 
301  if (RecoveryInProgress())
302  ereport(ERROR,
303  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
304  errmsg("recovery is in progress"),
305  errhint("WAL control functions cannot be executed during recovery.")));
306 
307  current_recptr = GetXLogInsertRecPtr();
308 
309  PG_RETURN_LSN(current_recptr);
310 }
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:9427

References ereport, errcode(), errhint(), errmsg(), ERROR, GetXLogInsertRecPtr(), PG_RETURN_LSN, and RecoveryInProgress().

◆ pg_current_wal_lsn()

Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 276 of file xlogfuncs.c.

277 {
278  XLogRecPtr current_recptr;
279 
280  if (RecoveryInProgress())
281  ereport(ERROR,
282  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
283  errmsg("recovery is in progress"),
284  errhint("WAL control functions cannot be executed during recovery.")));
285 
286  current_recptr = GetXLogWriteRecPtr();
287 
288  PG_RETURN_LSN(current_recptr);
289 }
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:9443

References ereport, errcode(), errhint(), errmsg(), ERROR, GetXLogWriteRecPtr(), PG_RETURN_LSN, and RecoveryInProgress().

◆ pg_get_wal_replay_pause_state()

Datum pg_get_wal_replay_pause_state ( PG_FUNCTION_ARGS  )

Definition at line 595 of file xlogfuncs.c.

596 {
597  char *statestr = NULL;
598 
599  if (!RecoveryInProgress())
600  ereport(ERROR,
601  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
602  errmsg("recovery is not in progress"),
603  errhint("Recovery control functions can only be executed during recovery.")));
604 
605  /* get the recovery pause state */
606  switch (GetRecoveryPauseState())
607  {
608  case RECOVERY_NOT_PAUSED:
609  statestr = "not paused";
610  break;
612  statestr = "pause requested";
613  break;
614  case RECOVERY_PAUSED:
615  statestr = "paused";
616  break;
617  }
618 
619  Assert(statestr != NULL);
621 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:184
RecoveryPauseState GetRecoveryPauseState(void)
@ RECOVERY_PAUSED
Definition: xlogrecovery.h:48
@ RECOVERY_NOT_PAUSED
Definition: xlogrecovery.h:46
@ RECOVERY_PAUSE_REQUESTED
Definition: xlogrecovery.h:47

References Assert, cstring_to_text(), ereport, errcode(), errhint(), errmsg(), ERROR, GetRecoveryPauseState(), PG_RETURN_TEXT_P, RECOVERY_NOT_PAUSED, RECOVERY_PAUSE_REQUESTED, RECOVERY_PAUSED, and RecoveryInProgress().

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 645 of file xlogfuncs.c.

646 {
648 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_RETURN_BOOL, and RecoveryInProgress().

◆ pg_is_wal_replay_paused()

Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 574 of file xlogfuncs.c.

575 {
576  if (!RecoveryInProgress())
577  ereport(ERROR,
578  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
579  errmsg("recovery is not in progress"),
580  errhint("Recovery control functions can only be executed during recovery.")));
581 
583 }

References ereport, errcode(), errhint(), errmsg(), ERROR, GetRecoveryPauseState(), PG_RETURN_BOOL, RECOVERY_NOT_PAUSED, and RecoveryInProgress().

◆ pg_last_wal_receive_lsn()

Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 340 of file xlogfuncs.c.

341 {
342  XLogRecPtr recptr;
343 
344  recptr = GetWalRcvFlushRecPtr(NULL, NULL);
345 
346  if (recptr == 0)
347  PG_RETURN_NULL();
348 
349  PG_RETURN_LSN(recptr);
350 }
#define PG_RETURN_NULL()
Definition: fmgr.h:345
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)

References GetWalRcvFlushRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

◆ pg_last_wal_replay_lsn()

Datum pg_last_wal_replay_lsn ( PG_FUNCTION_ARGS  )

Definition at line 359 of file xlogfuncs.c.

360 {
361  XLogRecPtr recptr;
362 
363  recptr = GetXLogReplayRecPtr(NULL);
364 
365  if (recptr == 0)
366  PG_RETURN_NULL();
367 
368  PG_RETURN_LSN(recptr);
369 }
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References GetXLogReplayRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

◆ pg_last_xact_replay_timestamp()

Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 630 of file xlogfuncs.c.

631 {
632  TimestampTz xtime;
633 
634  xtime = GetLatestXTime();
635  if (xtime == 0)
636  PG_RETURN_NULL();
637 
638  PG_RETURN_TIMESTAMPTZ(xtime);
639 }
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68
TimestampTz GetLatestXTime(void)

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

◆ pg_log_standby_snapshot()

Datum pg_log_standby_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 204 of file xlogfuncs.c.

205 {
206  XLogRecPtr recptr;
207 
208  if (RecoveryInProgress())
209  ereport(ERROR,
210  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
211  errmsg("recovery is in progress"),
212  errhint("%s cannot be executed during recovery.",
213  "pg_log_standby_snapshot()")));
214 
215  if (!XLogStandbyInfoActive())
216  ereport(ERROR,
217  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
218  errmsg("pg_log_standby_snapshot() can only be used if \"wal_level\" >= \"replica\"")));
219 
220  recptr = LogStandbySnapshot();
221 
222  /*
223  * As a convenience, return the WAL location of the last inserted record
224  */
225  PG_RETURN_LSN(recptr);
226 }
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1285
#define XLogStandbyInfoActive()
Definition: xlog.h:123

References ereport, errcode(), errhint(), errmsg(), ERROR, LogStandbySnapshot(), PG_RETURN_LSN, RecoveryInProgress(), and XLogStandbyInfoActive.

◆ pg_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 672 of file xlogfuncs.c.

673 {
674  bool wait = PG_GETARG_BOOL(0);
675  int wait_seconds = PG_GETARG_INT32(1);
676  FILE *promote_file;
677  int i;
678 
679  if (!RecoveryInProgress())
680  ereport(ERROR,
681  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
682  errmsg("recovery is not in progress"),
683  errhint("Recovery control functions can only be executed during recovery.")));
684 
685  if (wait_seconds <= 0)
686  ereport(ERROR,
687  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
688  errmsg("\"wait_seconds\" must not be negative or zero")));
689 
690  /* create the promote signal file */
692  if (!promote_file)
693  ereport(ERROR,
695  errmsg("could not create file \"%s\": %m",
697 
698  if (FreeFile(promote_file))
699  ereport(ERROR,
701  errmsg("could not write file \"%s\": %m",
703 
704  /* signal the postmaster */
705  if (kill(PostmasterPid, SIGUSR1) != 0)
706  {
707  (void) unlink(PROMOTE_SIGNAL_FILE);
708  ereport(ERROR,
709  (errcode(ERRCODE_SYSTEM_ERROR),
710  errmsg("failed to send signal to postmaster: %m")));
711  }
712 
713  /* return immediately if waiting was not requested */
714  if (!wait)
715  PG_RETURN_BOOL(true);
716 
717  /* wait for the amount of time wanted until promotion */
718 #define WAITS_PER_SECOND 10
719  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
720  {
721  int rc;
722 
724 
725  if (!RecoveryInProgress())
726  PG_RETURN_BOOL(true);
727 
729 
730  rc = WaitLatch(MyLatch,
732  1000L / WAITS_PER_SECOND,
733  WAIT_EVENT_PROMOTE);
734 
735  /*
736  * Emergency bailout if postmaster has died. This is to avoid the
737  * necessity for manual cleanup of all postmaster children.
738  */
739  if (rc & WL_POSTMASTER_DEATH)
740  ereport(FATAL,
741  (errcode(ERRCODE_ADMIN_SHUTDOWN),
742  errmsg("terminating connection due to unexpected postmaster exit"),
743  errcontext("while waiting on promotion")));
744  }
745 
747  (errmsg_plural("server did not promote within %d second",
748  "server did not promote within %d seconds",
749  wait_seconds,
750  wait_seconds)));
751  PG_RETURN_BOOL(false);
752 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
int errcode_for_file_access(void)
Definition: elog.c:876
#define errcontext
Definition: elog.h:196
#define FATAL
Definition: elog.h:41
#define WARNING
Definition: elog.h:36
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2606
int FreeFile(FILE *file)
Definition: fd.c:2804
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
pid_t PostmasterPid
Definition: globals.c:105
struct Latch * MyLatch
Definition: globals.c:62
int i
Definition: isn.c:73
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:100
static int wait_seconds
Definition: pg_ctl.c:76
#define kill(pid, sig)
Definition: win32_port.h:503
#define SIGUSR1
Definition: win32_port.h:180
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:308
#define WAITS_PER_SECOND

References AllocateFile(), CHECK_FOR_INTERRUPTS, ereport, errcode(), errcode_for_file_access(), errcontext, errhint(), errmsg(), errmsg_plural(), ERROR, FATAL, FreeFile(), i, kill, MyLatch, PG_GETARG_BOOL, PG_GETARG_INT32, PG_RETURN_BOOL, PostmasterPid, promote_file, PROMOTE_SIGNAL_FILE, RecoveryInProgress(), ResetLatch(), SIGUSR1, wait_seconds, WaitLatch(), WAITS_PER_SECOND, WARNING, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

◆ pg_split_walfile_name()

Datum pg_split_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 465 of file xlogfuncs.c.

466 {
467 #define PG_SPLIT_WALFILE_NAME_COLS 2
468  char *fname = text_to_cstring(PG_GETARG_TEXT_PP(0));
469  char *fname_upper;
470  char *p;
471  TimeLineID tli;
472  XLogSegNo segno;
474  bool isnull[PG_SPLIT_WALFILE_NAME_COLS] = {0};
475  TupleDesc tupdesc;
476  HeapTuple tuple;
477  char buf[256];
478  Datum result;
479 
480  fname_upper = pstrdup(fname);
481 
482  /* Capitalize WAL file name. */
483  for (p = fname_upper; *p; p++)
484  *p = pg_toupper((unsigned char) *p);
485 
486  if (!IsXLogFileName(fname_upper))
487  ereport(ERROR,
488  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
489  errmsg("invalid WAL file name \"%s\"", fname)));
490 
491  XLogFromFileName(fname_upper, &tli, &segno, wal_segment_size);
492 
493  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
494  elog(ERROR, "return type must be a row type");
495 
496  /* Convert to numeric. */
497  snprintf(buf, sizeof buf, UINT64_FORMAT, segno);
500  ObjectIdGetDatum(0),
501  Int32GetDatum(-1));
502 
503  values[1] = Int64GetDatum(tli);
504 
505  tuple = heap_form_tuple(tupdesc, values, isnull);
506  result = HeapTupleGetDatum(tuple);
507 
508  PG_RETURN_DATUM(result);
509 
510 #undef PG_SPLIT_WALFILE_NAME_COLS
511 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:639
#define UINT64_FORMAT
Definition: c.h:549
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:645
char * pstrdup(const char *in)
Definition: mcxt.c:1696
static char * buf
Definition: pg_test_fsync.c:73
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
#define snprintf
Definition: port.h:238
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
int wal_segment_size
Definition: xlog.c:142
static bool IsXLogFileName(const char *fname)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
uint32 TimeLineID
Definition: xlogdefs.h:59
uint64 XLogSegNo
Definition: xlogdefs.h:48
#define PG_SPLIT_WALFILE_NAME_COLS

References buf, CStringGetDatum(), DirectFunctionCall3, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), IsXLogFileName(), numeric_in(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_RETURN_DATUM, PG_SPLIT_WALFILE_NAME_COLS, pg_toupper(), pstrdup(), snprintf, text_to_cstring(), TYPEFUNC_COMPOSITE, UINT64_FORMAT, values, wal_segment_size, and XLogFromFileName().

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 179 of file xlogfuncs.c.

180 {
181  XLogRecPtr switchpoint;
182 
183  if (RecoveryInProgress())
184  ereport(ERROR,
185  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
186  errmsg("recovery is in progress"),
187  errhint("WAL control functions cannot be executed during recovery.")));
188 
189  switchpoint = RequestXLogSwitch(false);
190 
191  /*
192  * As a convenience, return the WAL location of the switch record
193  */
194  PG_RETURN_LSN(switchpoint);
195 }
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:8052

References ereport, errcode(), errhint(), errmsg(), ERROR, PG_RETURN_LSN, RecoveryInProgress(), and RequestXLogSwitch().

◆ pg_wal_lsn_diff()

Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 654 of file xlogfuncs.c.

655 {
656  Datum result;
657 
659  PG_GETARG_DATUM(0),
660  PG_GETARG_DATUM(1));
661 
662  PG_RETURN_DATUM(result);
663 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:224

References DirectFunctionCall2, PG_GETARG_DATUM, pg_lsn_mi(), and PG_RETURN_DATUM.

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 520 of file xlogfuncs.c.

521 {
522  if (!RecoveryInProgress())
523  ereport(ERROR,
524  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
525  errmsg("recovery is not in progress"),
526  errhint("Recovery control functions can only be executed during recovery.")));
527 
528  if (PromoteIsTriggered())
529  ereport(ERROR,
530  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
531  errmsg("standby promotion is ongoing"),
532  errhint("%s cannot be executed after promotion is triggered.",
533  "pg_wal_replay_pause()")));
534 
535  SetRecoveryPause(true);
536 
537  /* wake up the recovery process so that it can process the pause request */
538  WakeupRecovery();
539 
540  PG_RETURN_VOID();
541 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void SetRecoveryPause(bool recoveryPause)
void WakeupRecovery(void)
bool PromoteIsTriggered(void)

References ereport, errcode(), errhint(), errmsg(), ERROR, PG_RETURN_VOID, PromoteIsTriggered(), RecoveryInProgress(), SetRecoveryPause(), and WakeupRecovery().

◆ pg_wal_replay_resume()

Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 550 of file xlogfuncs.c.

551 {
552  if (!RecoveryInProgress())
553  ereport(ERROR,
554  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
555  errmsg("recovery is not in progress"),
556  errhint("Recovery control functions can only be executed during recovery.")));
557 
558  if (PromoteIsTriggered())
559  ereport(ERROR,
560  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
561  errmsg("standby promotion is ongoing"),
562  errhint("%s cannot be executed after promotion is triggered.",
563  "pg_wal_replay_resume()")));
564 
565  SetRecoveryPause(false);
566 
567  PG_RETURN_VOID();
568 }

References ereport, errcode(), errhint(), errmsg(), ERROR, PG_RETURN_VOID, PromoteIsTriggered(), RecoveryInProgress(), and SetRecoveryPause().

◆ pg_wal_replay_wait()

Datum pg_wal_replay_wait ( PG_FUNCTION_ARGS  )

Definition at line 758 of file xlogfuncs.c.

759 {
760  XLogRecPtr target_lsn = PG_GETARG_LSN(0);
761  int64 timeout = PG_GETARG_INT64(1);
762 
763  if (timeout < 0)
764  ereport(ERROR,
765  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
766  errmsg("\"timeout\" must not be negative")));
767 
768  /*
769  * We are going to wait for the LSN replay. We should first care that we
770  * don't hold a snapshot and correspondingly our MyProc->xmin is invalid.
771  * Otherwise, our snapshot could prevent the replay of WAL records
772  * implying a kind of self-deadlock. This is the reason why
773  * pg_wal_replay_wait() is a procedure, not a function.
774  *
775  * At first, we should check there is no active snapshot. According to
776  * PlannedStmtRequiresSnapshot(), even in an atomic context, CallStmt is
777  * processed with a snapshot. Thankfully, we can pop this snapshot,
778  * because PortalRunUtility() can tolerate this.
779  */
780  if (ActiveSnapshotSet())
782 
783  /*
784  * At second, invalidate a catalog snapshot if any. And we should be done
785  * with the preparation.
786  */
788 
789  /* Give up if there is still an active or registered snapshot. */
790  if (GetOldestSnapshot())
791  ereport(ERROR,
792  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
793  errmsg("pg_wal_replay_wait() must be only called without an active or registered snapshot"),
794  errdetail("Make sure pg_wal_replay_wait() isn't called within a transaction with an isolation level higher than READ COMMITTED, another procedure, or a function.")));
795 
796  /*
797  * As the result we should hold no snapshot, and correspondingly our xmin
798  * should be unset.
799  */
801 
802  (void) WaitForLSNReplay(target_lsn, timeout);
803 
804  PG_RETURN_VOID();
805 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:782
void PopActiveSnapshot(void)
Definition: snapmgr.c:743
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:422
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:323
PGPROC * MyProc
Definition: proc.c:67
TransactionId xmin
Definition: proc.h:172
#define InvalidTransactionId
Definition: transam.h:31
void WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
Definition: waitlsn.c:221

References ActiveSnapshotSet(), Assert, ereport, errcode(), errdetail(), errmsg(), ERROR, GetOldestSnapshot(), InvalidateCatalogSnapshot(), InvalidTransactionId, MyProc, PG_GETARG_INT64, PG_GETARG_LSN, PG_RETURN_VOID, PopActiveSnapshot(), WaitForLSNReplay(), and PGPROC::xmin.

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 440 of file xlogfuncs.c.

441 {
442  XLogSegNo xlogsegno;
443  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
444  char xlogfilename[MAXFNAMELEN];
445 
446  if (RecoveryInProgress())
447  ereport(ERROR,
448  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
449  errmsg("recovery is in progress"),
450  errhint("%s cannot be executed during recovery.",
451  "pg_walfile_name()")));
452 
453  XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
454  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
456 
457  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
458 }
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6519
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

References cstring_to_text(), ereport, errcode(), errhint(), errmsg(), ERROR, GetWALInsertionTimeLine(), MAXFNAMELEN, PG_GETARG_LSN, PG_RETURN_TEXT_P, RecoveryInProgress(), wal_segment_size, XLByteToSeg, and XLogFileName().

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 376 of file xlogfuncs.c.

377 {
378  XLogSegNo xlogsegno;
379  uint32 xrecoff;
380  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
381  char xlogfilename[MAXFNAMELEN];
382  Datum values[2];
383  bool isnull[2];
384  TupleDesc resultTupleDesc;
385  HeapTuple resultHeapTuple;
386  Datum result;
387 
388  if (RecoveryInProgress())
389  ereport(ERROR,
390  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
391  errmsg("recovery is in progress"),
392  errhint("%s cannot be executed during recovery.",
393  "pg_walfile_name_offset()")));
394 
395  /*
396  * Construct a tuple descriptor for the result row. This must match this
397  * function's pg_proc entry!
398  */
399  resultTupleDesc = CreateTemplateTupleDesc(2);
400  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
401  TEXTOID, -1, 0);
402  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
403  INT4OID, -1, 0);
404 
405  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
406 
407  /*
408  * xlogfilename
409  */
410  XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
411  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
413 
414  values[0] = CStringGetTextDatum(xlogfilename);
415  isnull[0] = false;
416 
417  /*
418  * offset
419  */
420  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
421 
422  values[1] = UInt32GetDatum(xrecoff);
423  isnull[1] = false;
424 
425  /*
426  * Tuple jam: Having first prepared your Datums, then squash together
427  */
428  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
429 
430  result = HeapTupleGetDatum(resultHeapTuple);
431 
432  PG_RETURN_DATUM(result);
433 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int uint32
Definition: c.h:506
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2158
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:232
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)

References BlessTupleDesc(), CreateTemplateTupleDesc(), CStringGetTextDatum, ereport, errcode(), errhint(), errmsg(), ERROR, GetWALInsertionTimeLine(), heap_form_tuple(), HeapTupleGetDatum(), MAXFNAMELEN, PG_GETARG_LSN, PG_RETURN_DATUM, RecoveryInProgress(), TupleDescInitEntry(), UInt32GetDatum(), values, wal_segment_size, XLByteToSeg, XLogFileName(), and XLogSegmentOffset.

Variable Documentation

◆ backup_state

BackupState* backup_state = NULL
static

◆ backupcontext

MemoryContext backupcontext = NULL
static

Definition at line 47 of file xlogfuncs.c.

Referenced by pg_backup_start(), and pg_backup_stop().

◆ tablespace_map

StringInfo tablespace_map = NULL
static

Definition at line 44 of file xlogfuncs.c.

Referenced by perform_base_backup(), pg_backup_start(), and pg_backup_stop().