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 "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/walreceiver.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/smgr.h"
#include "storage/standby.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
#include "utils/timestamp.h"
#include "utils/tuplestore.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)
 

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 62 of file xlogfuncs.c.

63 {
64  text *backupid = PG_GETARG_TEXT_PP(0);
65  bool fast = PG_GETARG_BOOL(1);
66  char *backupidstr;
68  MemoryContext oldcontext;
69 
70  backupidstr = text_to_cstring(backupid);
71 
72  if (status == SESSION_BACKUP_RUNNING)
73  ereport(ERROR,
74  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
75  errmsg("a backup is already in progress in this session")));
76 
77  /*
78  * backup_state and tablespace_map need to be long-lived as they are used
79  * in pg_backup_stop(). These are allocated in a dedicated memory context
80  * child of TopMemoryContext, deleted at the end of pg_backup_stop(). If
81  * an error happens before ending the backup, memory would be leaked in
82  * this context until pg_backup_start() is called again.
83  */
84  if (backupcontext == NULL)
85  {
87  "on-line backup context",
89  }
90  else
91  {
92  backup_state = NULL;
93  tablespace_map = NULL;
95  }
96 
100  MemoryContextSwitchTo(oldcontext);
101 
103  do_pg_backup_start(backupidstr, fast, NULL, backup_state, tablespace_map);
104 
106 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#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:330
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * palloc0(Size size)
Definition: mcxt.c:1257
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:34
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
XLogRecPtr startpoint
Definition: xlogbackup.h:26
Definition: c.h:676
char * text_to_cstring(const text *t)
Definition: varlena.c:215
SessionBackupState get_backup_status(void)
Definition: xlog.c:8589
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:8909
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8293
SessionBackupState
Definition: xlog.h:275
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:277
static BackupState * backup_state
Definition: xlogfuncs.c:46
static MemoryContext backupcontext
Definition: xlogfuncs.c:50
static StringInfo tablespace_map
Definition: xlogfuncs.c:47

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 129 of file xlogfuncs.c.

130 {
131 #define PG_BACKUP_STOP_V2_COLS 3
132  TupleDesc tupdesc;
134  bool nulls[PG_BACKUP_STOP_V2_COLS] = {0};
135  bool waitforarchive = PG_GETARG_BOOL(0);
136  char *backup_label;
138 
139  /* Initialize attributes information in the tuple descriptor */
140  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
141  elog(ERROR, "return type must be a row type");
142 
143  if (status != SESSION_BACKUP_RUNNING)
144  ereport(ERROR,
145  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
146  errmsg("backup is not in progress"),
147  errhint("Did you call pg_backup_start()?")));
148 
149  Assert(backup_state != NULL);
150  Assert(tablespace_map != NULL);
151 
152  /* Stop the backup */
153  do_pg_backup_stop(backup_state, waitforarchive);
154 
155  /* Build the contents of backup_label */
156  backup_label = build_backup_content(backup_state, false);
157 
159  values[1] = CStringGetTextDatum(backup_label);
161 
162  /* Deallocate backup-related variables */
163  pfree(backup_label);
164 
165  /* Clean up the session-level state and its memory context */
166  backup_state = NULL;
167  tablespace_map = NULL;
169  backupcontext = NULL;
170 
171  /* Returns the record as Datum */
173 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:94
int errhint(const char *fmt,...)
Definition: elog.c:1316
#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, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1456
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
uintptr_t Datum
Definition: postgres.h:64
XLogRecPtr stoppoint
Definition: xlogbackup.h:33
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition: xlog.c:8608
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 238 of file xlogfuncs.c.

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

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

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 300 of file xlogfuncs.c.

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

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 279 of file xlogfuncs.c.

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

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 602 of file xlogfuncs.c.

603 {
604  char *statestr = NULL;
605 
606  if (!RecoveryInProgress())
607  ereport(ERROR,
608  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
609  errmsg("recovery is not in progress"),
610  errhint("Recovery control functions can only be executed during recovery.")));
611 
612  /* get the recovery pause state */
613  switch (GetRecoveryPauseState())
614  {
615  case RECOVERY_NOT_PAUSED:
616  statestr = "not paused";
617  break;
619  statestr = "pause requested";
620  break;
621  case RECOVERY_PAUSED:
622  statestr = "paused";
623  break;
624  }
625 
626  Assert(statestr != NULL);
628 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:182
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 652 of file xlogfuncs.c.

653 {
655 }
#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 581 of file xlogfuncs.c.

582 {
583  if (!RecoveryInProgress())
584  ereport(ERROR,
585  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
586  errmsg("recovery is not in progress"),
587  errhint("Recovery control functions can only be executed during recovery.")));
588 
590 }

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 343 of file xlogfuncs.c.

344 {
345  XLogRecPtr recptr;
346 
347  recptr = GetWalRcvFlushRecPtr(NULL, NULL);
348 
349  if (recptr == 0)
350  PG_RETURN_NULL();
351 
352  PG_RETURN_LSN(recptr);
353 }
#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 362 of file xlogfuncs.c.

363 {
364  XLogRecPtr recptr;
365 
366  recptr = GetXLogReplayRecPtr(NULL);
367 
368  if (recptr == 0)
369  PG_RETURN_NULL();
370 
371  PG_RETURN_LSN(recptr);
372 }
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 637 of file xlogfuncs.c.

638 {
639  TimestampTz xtime;
640 
641  xtime = GetLatestXTime();
642  if (xtime == 0)
643  PG_RETURN_NULL();
644 
645  PG_RETURN_TIMESTAMPTZ(xtime);
646 }
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 207 of file xlogfuncs.c.

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

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

◆ pg_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 679 of file xlogfuncs.c.

680 {
681  bool wait = PG_GETARG_BOOL(0);
682  int wait_seconds = PG_GETARG_INT32(1);
683  FILE *promote_file;
684  int i;
685 
686  if (!RecoveryInProgress())
687  ereport(ERROR,
688  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
689  errmsg("recovery is not in progress"),
690  errhint("Recovery control functions can only be executed during recovery.")));
691 
692  if (wait_seconds <= 0)
693  ereport(ERROR,
694  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
695  errmsg("\"wait_seconds\" must not be negative or zero")));
696 
697  /* create the promote signal file */
699  if (!promote_file)
700  ereport(ERROR,
702  errmsg("could not create file \"%s\": %m",
704 
705  if (FreeFile(promote_file))
706  ereport(ERROR,
708  errmsg("could not write file \"%s\": %m",
710 
711  /* signal the postmaster */
712  if (kill(PostmasterPid, SIGUSR1) != 0)
713  {
714  (void) unlink(PROMOTE_SIGNAL_FILE);
715  ereport(ERROR,
716  (errcode(ERRCODE_SYSTEM_ERROR),
717  errmsg("failed to send signal to postmaster: %m")));
718  }
719 
720  /* return immediately if waiting was not requested */
721  if (!wait)
722  PG_RETURN_BOOL(true);
723 
724  /* wait for the amount of time wanted until promotion */
725 #define WAITS_PER_SECOND 10
726  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
727  {
728  int rc;
729 
731 
732  if (!RecoveryInProgress())
733  PG_RETURN_BOOL(true);
734 
736 
737  rc = WaitLatch(MyLatch,
739  1000L / WAITS_PER_SECOND,
740  WAIT_EVENT_PROMOTE);
741 
742  /*
743  * Emergency bailout if postmaster has died. This is to avoid the
744  * necessity for manual cleanup of all postmaster children.
745  */
746  if (rc & WL_POSTMASTER_DEATH)
747  ereport(FATAL,
748  (errcode(ERRCODE_ADMIN_SHUTDOWN),
749  errmsg("terminating connection due to unexpected postmaster exit"),
750  errcontext("while waiting on promotion")));
751  }
752 
754  (errmsg_plural("server did not promote within %d second",
755  "server did not promote within %d seconds",
756  wait_seconds,
757  wait_seconds)));
758  PG_RETURN_BOOL(false);
759 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1179
int errcode_for_file_access(void)
Definition: elog.c:881
#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:2528
int FreeFile(FILE *file)
Definition: fd.c:2726
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
pid_t PostmasterPid
Definition: globals.c:99
struct Latch * MyLatch
Definition: globals.c:58
int i
Definition: isn.c:73
void ResetLatch(Latch *latch)
Definition: latch.c:697
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:490
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:100
static int wait_seconds
Definition: pg_ctl.c:75
#define kill(pid, sig)
Definition: win32_port.h:485
#define SIGUSR1
Definition: win32_port.h:180
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:298
#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 472 of file xlogfuncs.c.

473 {
474 #define PG_SPLIT_WALFILE_NAME_COLS 2
475  char *fname = text_to_cstring(PG_GETARG_TEXT_PP(0));
476  char *fname_upper;
477  char *p;
478  TimeLineID tli;
479  XLogSegNo segno;
481  bool isnull[PG_SPLIT_WALFILE_NAME_COLS] = {0};
482  TupleDesc tupdesc;
483  HeapTuple tuple;
484  char buf[256];
485  Datum result;
486 
487  fname_upper = pstrdup(fname);
488 
489  /* Capitalize WAL file name. */
490  for (p = fname_upper; *p; p++)
491  *p = pg_toupper((unsigned char) *p);
492 
493  if (!IsXLogFileName(fname_upper))
494  ereport(ERROR,
495  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
496  errmsg("invalid WAL file name \"%s\"", fname)));
497 
498  XLogFromFileName(fname_upper, &tli, &segno, wal_segment_size);
499 
500  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
501  elog(ERROR, "return type must be a row type");
502 
503  /* Convert to numeric. */
504  snprintf(buf, sizeof buf, UINT64_FORMAT, segno);
507  ObjectIdGetDatum(0),
508  Int32GetDatum(-1));
509 
510  values[1] = Int64GetDatum(tli);
511 
512  tuple = heap_form_tuple(tupdesc, values, isnull);
513  result = HeapTupleGetDatum(tuple);
514 
515  PG_RETURN_DATUM(result);
516 
517 #undef PG_SPLIT_WALFILE_NAME_COLS
518 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:627
#define UINT64_FORMAT
Definition: c.h:538
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1790
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
char * pstrdup(const char *in)
Definition: mcxt.c:1644
static char * buf
Definition: pg_test_fsync.c:67
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:146
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 182 of file xlogfuncs.c.

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

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 661 of file xlogfuncs.c.

662 {
663  Datum result;
664 
666  PG_GETARG_DATUM(0),
667  PG_GETARG_DATUM(1));
668 
669  PG_RETURN_DATUM(result);
670 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:225

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 527 of file xlogfuncs.c.

528 {
529  if (!RecoveryInProgress())
530  ereport(ERROR,
531  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
532  errmsg("recovery is not in progress"),
533  errhint("Recovery control functions can only be executed during recovery.")));
534 
535  if (PromoteIsTriggered())
536  ereport(ERROR,
537  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
538  errmsg("standby promotion is ongoing"),
539  errhint("%s cannot be executed after promotion is triggered.",
540  "pg_wal_replay_pause()")));
541 
542  SetRecoveryPause(true);
543 
544  /* wake up the recovery process so that it can process the pause request */
545  WakeupRecovery();
546 
547  PG_RETURN_VOID();
548 }
#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 557 of file xlogfuncs.c.

558 {
559  if (!RecoveryInProgress())
560  ereport(ERROR,
561  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
562  errmsg("recovery is not in progress"),
563  errhint("Recovery control functions can only be executed during recovery.")));
564 
565  if (PromoteIsTriggered())
566  ereport(ERROR,
567  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
568  errmsg("standby promotion is ongoing"),
569  errhint("%s cannot be executed after promotion is triggered.",
570  "pg_wal_replay_resume()")));
571 
572  SetRecoveryPause(false);
573 
574  PG_RETURN_VOID();
575 }

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

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 447 of file xlogfuncs.c.

448 {
449  XLogSegNo xlogsegno;
450  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
451  char xlogfilename[MAXFNAMELEN];
452 
453  if (RecoveryInProgress())
454  ereport(ERROR,
455  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
456  errmsg("recovery is in progress"),
457  errhint("%s cannot be executed during recovery.",
458  "pg_walfile_name()")));
459 
460  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
461  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
463 
464  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
465 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6136
#define XLByteToPrevSeg(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, XLByteToPrevSeg, and XLogFileName().

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 383 of file xlogfuncs.c.

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

Variable Documentation

◆ backup_state

BackupState* backup_state = NULL
static

Definition at line 46 of file xlogfuncs.c.

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

◆ backupcontext

MemoryContext backupcontext = NULL
static

Definition at line 50 of file xlogfuncs.c.

Referenced by pg_backup_start(), and pg_backup_stop().

◆ tablespace_map

StringInfo tablespace_map = NULL
static

Definition at line 47 of file xlogfuncs.c.

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