PostgreSQL Source Code  git master
xlogfuncs.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/htup_details.h"
#include "access/xlog.h"
#include "access/xlog_internal.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 "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 WAITS_PER_SECOND   10
 

Functions

Datum pg_start_backup (PG_FUNCTION_ARGS)
 
Datum pg_stop_backup (PG_FUNCTION_ARGS)
 
Datum pg_stop_backup_v2 (PG_FUNCTION_ARGS)
 
Datum pg_switch_wal (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_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_is_in_backup (PG_FUNCTION_ARGS)
 
Datum pg_backup_start_time (PG_FUNCTION_ARGS)
 
Datum pg_promote (PG_FUNCTION_ARGS)
 

Variables

static StringInfo label_file
 
static StringInfo tblspc_map_file
 

Macro Definition Documentation

◆ WAITS_PER_SECOND

#define WAITS_PER_SECOND   10

Function Documentation

◆ pg_backup_start_time()

Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

Definition at line 689 of file xlogfuncs.c.

690 {
691  Datum xtime;
692  FILE *lfp;
693  char fline[MAXPGPATH];
694  char backup_start_time[30];
695 
696  /*
697  * See if label file is present
698  */
699  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
700  if (lfp == NULL)
701  {
702  if (errno != ENOENT)
703  ereport(ERROR,
705  errmsg("could not read file \"%s\": %m",
707  PG_RETURN_NULL();
708  }
709 
710  /*
711  * Parse the file to find the START TIME line.
712  */
713  backup_start_time[0] = '\0';
714  while (fgets(fline, sizeof(fline), lfp) != NULL)
715  {
716  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
717  break;
718  }
719 
720  /* Check for a read error. */
721  if (ferror(lfp))
722  ereport(ERROR,
724  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
725 
726  /* Close the backup label file. */
727  if (FreeFile(lfp))
728  ereport(ERROR,
730  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
731 
732  if (strlen(backup_start_time) == 0)
733  ereport(ERROR,
734  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
735  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
736 
737  /*
738  * Convert the time string read from file to TimestampTz form.
739  */
741  CStringGetDatum(backup_start_time),
743  Int32GetDatum(-1));
744 
745  PG_RETURN_DATUM(xtime);
746 }
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:403
int errcode_for_file_access(void)
Definition: elog.c:716
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2461
int FreeFile(FILE *file)
Definition: fd.c:2660
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:635
#define MAXPGPATH
#define CStringGetDatum(X)
Definition: postgres.h:622
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define Int32GetDatum(X)
Definition: postgres.h:523
#define InvalidOid
Definition: postgres_ext.h:36
#define BACKUP_LABEL_FILE
Definition: xlog.h:359

References AllocateFile(), BACKUP_LABEL_FILE, CStringGetDatum, DirectFunctionCall3, ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, FreeFile(), Int32GetDatum, InvalidOid, MAXPGPATH, ObjectIdGetDatum, PG_RETURN_DATUM, PG_RETURN_NULL, and timestamptz_in().

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 292 of file xlogfuncs.c.

293 {
294  text *restore_name = PG_GETARG_TEXT_PP(0);
295  char *restore_name_str;
296  XLogRecPtr restorepoint;
297 
298  if (RecoveryInProgress())
299  ereport(ERROR,
300  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
301  errmsg("recovery is in progress"),
302  errhint("WAL control functions cannot be executed during recovery.")));
303 
304  if (!XLogIsNeeded())
305  ereport(ERROR,
306  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
307  errmsg("WAL level not sufficient for creating a restore point"),
308  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
309 
310  restore_name_str = text_to_cstring(restore_name);
311 
312  if (strlen(restore_name_str) >= MAXFNAMELEN)
313  ereport(ERROR,
314  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
315  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
316 
317  restorepoint = XLogRestorePoint(restore_name_str);
318 
319  /*
320  * As a convenience, return the WAL location of the restore point record
321  */
322  PG_RETURN_LSN(restorepoint);
323 }
int errhint(const char *fmt,...)
Definition: elog.c:1151
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
Definition: c.h:622
char * text_to_cstring(const text *t)
Definition: varlena.c:222
bool RecoveryInProgress(void)
Definition: xlog.c:8404
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:10102
#define XLogIsNeeded()
Definition: xlog.h:164
#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 375 of file xlogfuncs.c.

376 {
377  XLogRecPtr current_recptr;
378 
379  if (RecoveryInProgress())
380  ereport(ERROR,
381  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
382  errmsg("recovery is in progress"),
383  errhint("WAL control functions cannot be executed during recovery.")));
384 
385  current_recptr = GetFlushRecPtr(NULL);
386 
387  PG_RETURN_LSN(current_recptr);
388 }
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:8713

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

355 {
356  XLogRecPtr current_recptr;
357 
358  if (RecoveryInProgress())
359  ereport(ERROR,
360  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
361  errmsg("recovery is in progress"),
362  errhint("WAL control functions cannot be executed during recovery.")));
363 
364  current_recptr = GetXLogInsertRecPtr();
365 
366  PG_RETURN_LSN(current_recptr);
367 }
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11985

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

334 {
335  XLogRecPtr current_recptr;
336 
337  if (RecoveryInProgress())
338  ereport(ERROR,
339  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
340  errmsg("recovery is in progress"),
341  errhint("WAL control functions cannot be executed during recovery.")));
342 
343  current_recptr = GetXLogWriteRecPtr();
344 
345  PG_RETURN_LSN(current_recptr);
346 }
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:12001

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

604 {
605  char *statestr = NULL;
606 
607  if (!RecoveryInProgress())
608  ereport(ERROR,
609  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
610  errmsg("recovery is not in progress"),
611  errhint("Recovery control functions can only be executed during recovery.")));
612 
613  /* get the recovery pause state */
614  switch (GetRecoveryPauseState())
615  {
616  case RECOVERY_NOT_PAUSED:
617  statestr = "not paused";
618  break;
620  statestr = "pause requested";
621  break;
622  case RECOVERY_PAUSED:
623  statestr = "paused";
624  break;
625  }
626 
627  Assert(statestr != NULL);
629 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Assert(fmt[strlen(fmt) - 1] !='\n')
text * cstring_to_text(const char *s)
Definition: varlena.c:189
RecoveryPauseState GetRecoveryPauseState(void)
Definition: xlog.c:6320
@ RECOVERY_PAUSED
Definition: xlog.h:147
@ RECOVERY_NOT_PAUSED
Definition: xlog.h:145
@ RECOVERY_PAUSE_REQUESTED
Definition: xlog.h:146

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_backup()

Datum pg_is_in_backup ( PG_FUNCTION_ARGS  )

Definition at line 677 of file xlogfuncs.c.

678 {
680 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
bool BackupInProgress(void)
Definition: xlog.c:12285

References BackupInProgress(), and PG_RETURN_BOOL.

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 653 of file xlogfuncs.c.

654 {
656 }

References PG_RETURN_BOOL, and RecoveryInProgress().

◆ pg_is_wal_replay_paused()

Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 582 of file xlogfuncs.c.

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

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

398 {
399  XLogRecPtr recptr;
400 
401  recptr = GetWalRcvFlushRecPtr(NULL, NULL);
402 
403  if (recptr == 0)
404  PG_RETURN_NULL();
405 
406  PG_RETURN_LSN(recptr);
407 }
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 416 of file xlogfuncs.c.

417 {
418  XLogRecPtr recptr;
419 
420  recptr = GetXLogReplayRecPtr(NULL);
421 
422  if (recptr == 0)
423  PG_RETURN_NULL();
424 
425  PG_RETURN_LSN(recptr);
426 }
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11966

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

639 {
640  TimestampTz xtime;
641 
642  xtime = GetLatestXTime();
643  if (xtime == 0)
644  PG_RETURN_NULL();
645 
646  PG_RETURN_TIMESTAMPTZ(xtime);
647 }
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6489

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

◆ pg_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 755 of file xlogfuncs.c.

756 {
757  bool wait = PG_GETARG_BOOL(0);
758  int wait_seconds = PG_GETARG_INT32(1);
759  FILE *promote_file;
760  int i;
761 
762  if (!RecoveryInProgress())
763  ereport(ERROR,
764  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
765  errmsg("recovery is not in progress"),
766  errhint("Recovery control functions can only be executed during recovery.")));
767 
768  if (wait_seconds <= 0)
769  ereport(ERROR,
770  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
771  errmsg("\"wait_seconds\" must not be negative or zero")));
772 
773  /* create the promote signal file */
775  if (!promote_file)
776  ereport(ERROR,
778  errmsg("could not create file \"%s\": %m",
780 
781  if (FreeFile(promote_file))
782  ereport(ERROR,
784  errmsg("could not write file \"%s\": %m",
786 
787  /* signal the postmaster */
788  if (kill(PostmasterPid, SIGUSR1) != 0)
789  {
791  (errmsg("failed to send signal to postmaster: %m")));
792  (void) unlink(PROMOTE_SIGNAL_FILE);
793  PG_RETURN_BOOL(false);
794  }
795 
796  /* return immediately if waiting was not requested */
797  if (!wait)
798  PG_RETURN_BOOL(true);
799 
800  /* wait for the amount of time wanted until promotion */
801 #define WAITS_PER_SECOND 10
802  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
803  {
804  int rc;
805 
807 
808  if (!RecoveryInProgress())
809  PG_RETURN_BOOL(true);
810 
812 
813  rc = WaitLatch(MyLatch,
815  1000L / WAITS_PER_SECOND,
817 
818  /*
819  * Emergency bailout if postmaster has died. This is to avoid the
820  * necessity for manual cleanup of all postmaster children.
821  */
822  if (rc & WL_POSTMASTER_DEATH)
823  PG_RETURN_BOOL(false);
824  }
825 
827  (errmsg_plural("server did not promote within %d second",
828  "server did not promote within %d seconds",
829  wait_seconds,
830  wait_seconds)));
831  PG_RETURN_BOOL(false);
832 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1014
#define WARNING
Definition: elog.h:30
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
pid_t PostmasterPid
Definition: globals.c:98
struct Latch * MyLatch
Definition: globals.c:57
int i
Definition: isn.c:73
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
#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:120
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:105
static int wait_seconds
Definition: pg_ctl.c:80
@ WAIT_EVENT_PROMOTE
Definition: wait_event.h:120
#define kill(pid, sig)
Definition: win32_port.h:464
#define SIGUSR1
Definition: win32_port.h:179
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:366
#define WAITS_PER_SECOND

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

◆ pg_start_backup()

Datum pg_start_backup ( PG_FUNCTION_ARGS  )

Definition at line 60 of file xlogfuncs.c.

61 {
62  text *backupid = PG_GETARG_TEXT_PP(0);
63  bool fast = PG_GETARG_BOOL(1);
64  bool exclusive = PG_GETARG_BOOL(2);
65  char *backupidstr;
66  XLogRecPtr startpoint;
68 
69  backupidstr = text_to_cstring(backupid);
70 
72  ereport(ERROR,
73  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
74  errmsg("a backup is already in progress in this session")));
75 
76  if (exclusive)
77  {
78  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
79  NULL, NULL);
80  }
81  else
82  {
83  MemoryContext oldcontext;
84 
85  /*
86  * Label file and tablespace map file need to be long-lived, since
87  * they are read in pg_stop_backup.
88  */
92  MemoryContextSwitchTo(oldcontext);
93 
95 
96  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
97  NULL, tblspc_map_file);
98  }
99 
100  PG_RETURN_LSN(startpoint);
101 }
MemoryContext TopMemoryContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, List **tablespaces, StringInfo tblspcmapfile)
Definition: xlog.c:10980
SessionBackupState get_backup_status(void)
Definition: xlog.c:11477
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:11950
SessionBackupState
Definition: xlog.h:341
@ SESSION_BACKUP_NON_EXCLUSIVE
Definition: xlog.h:344
static StringInfo label_file
Definition: xlogfuncs.c:44
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:45

References do_pg_start_backup(), ereport, errcode(), errmsg(), ERROR, get_backup_status(), label_file, makeStringInfo(), MemoryContextSwitchTo(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_LSN, register_persistent_abort_backup_handler(), SESSION_BACKUP_NON_EXCLUSIVE, status(), tblspc_map_file, text_to_cstring(), and TopMemoryContext.

◆ pg_stop_backup()

Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

Definition at line 124 of file xlogfuncs.c.

125 {
126  XLogRecPtr stoppoint;
128 
130  ereport(ERROR,
131  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
132  errmsg("non-exclusive backup in progress"),
133  errhint("Did you mean to use pg_stop_backup('f')?")));
134 
135  /*
136  * Exclusive backups were typically started in a different connection, so
137  * don't try to verify that status of backup is set to
138  * SESSION_BACKUP_EXCLUSIVE in this function. Actual verification that an
139  * exclusive backup is in fact running is handled inside
140  * do_pg_stop_backup.
141  */
142  stoppoint = do_pg_stop_backup(NULL, true, NULL);
143 
144  PG_RETURN_LSN(stoppoint);
145 }
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:11498

References do_pg_stop_backup(), ereport, errcode(), errhint(), errmsg(), ERROR, get_backup_status(), PG_RETURN_LSN, SESSION_BACKUP_NON_EXCLUSIVE, and status().

◆ pg_stop_backup_v2()

Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

Definition at line 166 of file xlogfuncs.c.

167 {
168  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
169  TupleDesc tupdesc;
170  Tuplestorestate *tupstore;
171  MemoryContext per_query_ctx;
172  MemoryContext oldcontext;
173  Datum values[3];
174  bool nulls[3];
175 
176  bool exclusive = PG_GETARG_BOOL(0);
177  bool waitforarchive = PG_GETARG_BOOL(1);
178  XLogRecPtr stoppoint;
180 
181  /* check to see if caller supports us returning a tuplestore */
182  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
183  ereport(ERROR,
184  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
185  errmsg("set-valued function called in context that cannot accept a set")));
186  if (!(rsinfo->allowedModes & SFRM_Materialize))
187  ereport(ERROR,
188  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
189  errmsg("materialize mode required, but it is not allowed in this context")));
190 
191  /* Build a tuple descriptor for our result type */
192  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
193  elog(ERROR, "return type must be a row type");
194 
195  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
196  oldcontext = MemoryContextSwitchTo(per_query_ctx);
197 
198  tupstore = tuplestore_begin_heap(true, false, work_mem);
199  rsinfo->returnMode = SFRM_Materialize;
200  rsinfo->setResult = tupstore;
201  rsinfo->setDesc = tupdesc;
202 
203  MemoryContextSwitchTo(oldcontext);
204 
205  MemSet(values, 0, sizeof(values));
206  MemSet(nulls, 0, sizeof(nulls));
207 
208  if (exclusive)
209  {
211  ereport(ERROR,
212  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
213  errmsg("non-exclusive backup in progress"),
214  errhint("Did you mean to use pg_stop_backup('f')?")));
215 
216  /*
217  * Stop the exclusive backup, and since we're in an exclusive backup
218  * return NULL for both backup_label and tablespace_map.
219  */
220  stoppoint = do_pg_stop_backup(NULL, waitforarchive, NULL);
221 
222  nulls[1] = true;
223  nulls[2] = true;
224  }
225  else
226  {
228  ereport(ERROR,
229  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
230  errmsg("non-exclusive backup is not in progress"),
231  errhint("Did you mean to use pg_stop_backup('t')?")));
232 
233  /*
234  * Stop the non-exclusive backup. Return a copy of the backup label
235  * and tablespace map so they can be written to disk by the caller.
236  */
237  stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
238 
241 
242  /* Free structures allocated in TopMemoryContext */
244  pfree(label_file);
245  label_file = NULL;
248  tblspc_map_file = NULL;
249  }
250 
251  /* Stoppoint is included on both exclusive and nonexclusive backups */
252  values[0] = LSNGetDatum(stoppoint);
253 
254  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
255  tuplestore_donestoring(tupstore);
256 
257  return (Datum) 0;
258 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
#define MemSet(start, val, len)
Definition: c.h:1008
#define elog(elevel,...)
Definition: elog.h:218
@ SFRM_Materialize
Definition: execnodes.h:293
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
int work_mem
Definition: globals.c:124
void pfree(void *pointer)
Definition: mcxt.c:1169
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:238
SetFunctionReturnMode returnMode
Definition: execnodes.h:312
ExprContext * econtext
Definition: execnodes.h:308
TupleDesc setDesc
Definition: execnodes.h:316
Tuplestorestate * setResult
Definition: execnodes.h:315
int allowedModes
Definition: execnodes.h:310
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60

References ReturnSetInfo::allowedModes, CStringGetTextDatum, StringInfoData::data, do_pg_stop_backup(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errhint(), errmsg(), ERROR, get_backup_status(), get_call_result_type(), IsA, label_file, LSNGetDatum, MemoryContextSwitchTo(), MemSet, pfree(), PG_GETARG_BOOL, ReturnSetInfo::returnMode, SESSION_BACKUP_NON_EXCLUSIVE, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, status(), tblspc_map_file, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 267 of file xlogfuncs.c.

268 {
269  XLogRecPtr switchpoint;
270 
271  if (RecoveryInProgress())
272  ereport(ERROR,
273  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
274  errmsg("recovery is in progress"),
275  errhint("WAL control functions cannot be executed during recovery.")));
276 
277  switchpoint = RequestXLogSwitch(false);
278 
279  /*
280  * As a convenience, return the WAL location of the switch record
281  */
282  PG_RETURN_LSN(switchpoint);
283 }
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:10084

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

663 {
664  Datum result;
665 
667  PG_GETARG_DATUM(0),
668  PG_GETARG_DATUM(1));
669 
670  PG_RETURN_NUMERIC(result);
671 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:64
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:225

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

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 528 of file xlogfuncs.c.

529 {
530  if (!RecoveryInProgress())
531  ereport(ERROR,
532  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
533  errmsg("recovery is not in progress"),
534  errhint("Recovery control functions can only be executed during recovery.")));
535 
536  if (PromoteIsTriggered())
537  ereport(ERROR,
538  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
539  errmsg("standby promotion is ongoing"),
540  errhint("%s cannot be executed after promotion is triggered.",
541  "pg_wal_replay_pause()")));
542 
543  SetRecoveryPause(true);
544 
545  /* wake up the recovery process so that it can process the pause request */
546  WakeupRecovery();
547 
548  PG_RETURN_VOID();
549 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:6340
void WakeupRecovery(void)
Definition: xlog.c:13222
bool PromoteIsTriggered(void)
Definition: xlog.c:13118

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

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

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

502 {
503  XLogSegNo xlogsegno;
504  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
505  char xlogfilename[MAXFNAMELEN];
506 
507  if (RecoveryInProgress())
508  ereport(ERROR,
509  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
510  errmsg("recovery is in progress"),
511  errhint("%s cannot be executed during recovery.",
512  "pg_walfile_name()")));
513 
514  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
515  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
517 
518  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
519 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:8736
int wal_segment_size
Definition: xlog.c:123
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
uint64 XLogSegNo
Definition: xlogdefs.h:48

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

438 {
439  XLogSegNo xlogsegno;
440  uint32 xrecoff;
441  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
442  char xlogfilename[MAXFNAMELEN];
443  Datum values[2];
444  bool isnull[2];
445  TupleDesc resultTupleDesc;
446  HeapTuple resultHeapTuple;
447  Datum result;
448 
449  if (RecoveryInProgress())
450  ereport(ERROR,
451  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
452  errmsg("recovery is in progress"),
453  errhint("%s cannot be executed during recovery.",
454  "pg_walfile_name_offset()")));
455 
456  /*
457  * Construct a tuple descriptor for the result row. This must match this
458  * function's pg_proc entry!
459  */
460  resultTupleDesc = CreateTemplateTupleDesc(2);
461  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
462  TEXTOID, -1, 0);
463  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
464  INT4OID, -1, 0);
465 
466  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
467 
468  /*
469  * xlogfilename
470  */
471  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
472  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
474 
475  values[0] = CStringGetTextDatum(xlogfilename);
476  isnull[0] = false;
477 
478  /*
479  * offset
480  */
481  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
482 
483  values[1] = UInt32GetDatum(xrecoff);
484  isnull[1] = false;
485 
486  /*
487  * Tuple jam: Having first prepared your Datums, then squash together
488  */
489  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
490 
491  result = HeapTupleGetDatum(resultHeapTuple);
492 
493  PG_RETURN_DATUM(result);
494 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int uint32
Definition: c.h:441
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define UInt32GetDatum(X)
Definition: postgres.h:537
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

◆ label_file

StringInfo label_file
static

Definition at line 44 of file xlogfuncs.c.

Referenced by pg_start_backup(), and pg_stop_backup_v2().

◆ tblspc_map_file

StringInfo tblspc_map_file
static

Definition at line 45 of file xlogfuncs.c.

Referenced by perform_base_backup(), pg_start_backup(), and pg_stop_backup_v2().