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

static void nonexclusive_base_backup_cleanup (int code, Datum arg)
 
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_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

Referenced by pg_promote().

Function Documentation

◆ nonexclusive_base_backup_cleanup()

static void nonexclusive_base_backup_cleanup ( int  code,
Datum  arg 
)
static

Definition at line 52 of file xlogfuncs.c.

References do_pg_abort_backup(), ereport, errmsg(), and WARNING.

Referenced by pg_start_backup(), and pg_stop_backup_v2().

53 {
56  (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
57 }
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:822
void do_pg_abort_backup(void)
Definition: xlog.c:11144

◆ pg_backup_start_time()

Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

Definition at line 646 of file xlogfuncs.c.

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

647 {
648  Datum xtime;
649  FILE *lfp;
650  char fline[MAXPGPATH];
651  char backup_start_time[30];
652 
653  /*
654  * See if label file is present
655  */
656  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
657  if (lfp == NULL)
658  {
659  if (errno != ENOENT)
660  ereport(ERROR,
662  errmsg("could not read file \"%s\": %m",
664  PG_RETURN_NULL();
665  }
666 
667  /*
668  * Parse the file to find the START TIME line.
669  */
670  backup_start_time[0] = '\0';
671  while (fgets(fline, sizeof(fline), lfp) != NULL)
672  {
673  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
674  break;
675  }
676 
677  /* Check for a read error. */
678  if (ferror(lfp))
679  ereport(ERROR,
681  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
682 
683  /* Close the backup label file. */
684  if (FreeFile(lfp))
685  ereport(ERROR,
687  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
688 
689  if (strlen(backup_start_time) == 0)
690  ereport(ERROR,
691  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
692  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
693 
694  /*
695  * Convert the time string read from file to TimestampTz form.
696  */
698  CStringGetDatum(backup_start_time),
700  Int32GetDatum(-1));
701 
702  PG_RETURN_DATUM(xtime);
703 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:631
#define CStringGetDatum(X)
Definition: postgres.h:578
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2242
#define ereport(elevel, rest)
Definition: elog.h:141
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:619
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define InvalidOid
Definition: postgres_ext.h:36
int FreeFile(FILE *file)
Definition: fd.c:2441
#define Int32GetDatum(X)
Definition: postgres.h:479
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:400
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define BACKUP_LABEL_FILE
Definition: xlog.h:358
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 306 of file xlogfuncs.c.

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

307 {
308  text *restore_name = PG_GETARG_TEXT_PP(0);
309  char *restore_name_str;
310  XLogRecPtr restorepoint;
311 
312  if (RecoveryInProgress())
313  ereport(ERROR,
314  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
315  (errmsg("recovery is in progress"),
316  errhint("WAL control functions cannot be executed during recovery."))));
317 
318  if (!XLogIsNeeded())
319  ereport(ERROR,
320  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
321  errmsg("WAL level not sufficient for creating a restore point"),
322  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
323 
324  restore_name_str = text_to_cstring(restore_name);
325 
326  if (strlen(restore_name_str) >= MAXFNAMELEN)
327  ereport(ERROR,
328  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
329  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
330 
331  restorepoint = XLogRestorePoint(restore_name_str);
332 
333  /*
334  * As a convenience, return the WAL location of the restore point record
335  */
336  PG_RETURN_LSN(restorepoint);
337 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define XLogIsNeeded()
Definition: xlog.h:181
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define MAXFNAMELEN
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:9432
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:822
Definition: c.h:556

◆ pg_current_wal_flush_lsn()

Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 389 of file xlogfuncs.c.

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

390 {
391  XLogRecPtr current_recptr;
392 
393  if (RecoveryInProgress())
394  ereport(ERROR,
395  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
396  errmsg("recovery is in progress"),
397  errhint("WAL control functions cannot be executed during recovery.")));
398 
399  current_recptr = GetFlushRecPtr();
400 
401  PG_RETURN_LSN(current_recptr);
402 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8267
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_current_wal_insert_lsn()

Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 368 of file xlogfuncs.c.

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

369 {
370  XLogRecPtr current_recptr;
371 
372  if (RecoveryInProgress())
373  ereport(ERROR,
374  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
375  errmsg("recovery is in progress"),
376  errhint("WAL control functions cannot be executed during recovery.")));
377 
378  current_recptr = GetXLogInsertRecPtr();
379 
380  PG_RETURN_LSN(current_recptr);
381 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11191
#define ereport(elevel, rest)
Definition: elog.h:141
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_current_wal_lsn()

Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 347 of file xlogfuncs.c.

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

348 {
349  XLogRecPtr current_recptr;
350 
351  if (RecoveryInProgress())
352  ereport(ERROR,
353  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
354  errmsg("recovery is in progress"),
355  errhint("WAL control functions cannot be executed during recovery.")));
356 
357  current_recptr = GetXLogWriteRecPtr();
358 
359  PG_RETURN_LSN(current_recptr);
360 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:11207
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_is_in_backup()

Datum pg_is_in_backup ( PG_FUNCTION_ARGS  )

Definition at line 634 of file xlogfuncs.c.

References BackupInProgress(), and PG_RETURN_BOOL.

635 {
637 }
bool BackupInProgress(void)
Definition: xlog.c:11468
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 610 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

611 {
613 }
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ pg_is_wal_replay_paused()

Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 577 of file xlogfuncs.c.

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

578 {
579  if (!RecoveryInProgress())
580  ereport(ERROR,
581  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
582  errmsg("recovery is not in progress"),
583  errhint("Recovery control functions can only be executed during recovery.")));
584 
586 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
bool RecoveryIsPaused(void)
Definition: xlog.c:5936
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_last_wal_receive_lsn()

Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 411 of file xlogfuncs.c.

References GetWalRcvWriteRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

412 {
413  XLogRecPtr recptr;
414 
415  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
416 
417  if (recptr == 0)
418  PG_RETURN_NULL();
419 
420  PG_RETURN_LSN(recptr);
421 }
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ pg_last_wal_replay_lsn()

Datum pg_last_wal_replay_lsn ( PG_FUNCTION_ARGS  )

Definition at line 430 of file xlogfuncs.c.

References GetXLogReplayRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

431 {
432  XLogRecPtr recptr;
433 
434  recptr = GetXLogReplayRecPtr(NULL);
435 
436  if (recptr == 0)
437  PG_RETURN_NULL();
438 
439  PG_RETURN_LSN(recptr);
440 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11172
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ pg_last_xact_replay_timestamp()

Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 595 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

596 {
597  TimestampTz xtime;
598 
599  xtime = GetLatestXTime();
600  if (xtime == 0)
601  PG_RETURN_NULL();
602 
603  PG_RETURN_TIMESTAMPTZ(xtime);
604 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6074
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ pg_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 712 of file xlogfuncs.c.

References AllocateFile(), CHECK_FOR_INTERRUPTS, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), 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.

713 {
714  bool wait = PG_GETARG_BOOL(0);
715  int wait_seconds = PG_GETARG_INT32(1);
716  FILE *promote_file;
717  int i;
718 
719  if (!RecoveryInProgress())
720  ereport(ERROR,
721  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
722  errmsg("recovery is not in progress"),
723  errhint("Recovery control functions can only be executed during recovery.")));
724 
725  if (wait_seconds <= 0)
726  ereport(ERROR,
727  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
728  errmsg("\"wait_seconds\" must not be negative or zero")));
729 
730  /* create the promote signal file */
731  promote_file = AllocateFile(PROMOTE_SIGNAL_FILE, "w");
732  if (!promote_file)
733  ereport(ERROR,
735  errmsg("could not create file \"%s\": %m",
737 
738  if (FreeFile(promote_file))
739  ereport(ERROR,
741  errmsg("could not write file \"%s\": %m",
743 
744  /* signal the postmaster */
745  if (kill(PostmasterPid, SIGUSR1) != 0)
746  {
748  (errmsg("failed to send signal to postmaster: %m")));
749  (void) unlink(PROMOTE_SIGNAL_FILE);
750  PG_RETURN_BOOL(false);
751  }
752 
753  /* return immediately if waiting was not requested */
754  if (!wait)
755  PG_RETURN_BOOL(true);
756 
757  /* wait for the amount of time wanted until promotion */
758 #define WAITS_PER_SECOND 10
759  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
760  {
761  int rc;
762 
764 
765  if (!RecoveryInProgress())
766  PG_RETURN_BOOL(true);
767 
769 
770  rc = WaitLatch(MyLatch,
772  1000L / WAITS_PER_SECOND,
774 
775  /*
776  * Emergency bailout if postmaster has died. This is to avoid the
777  * necessity for manual cleanup of all postmaster children.
778  */
779  if (rc & WL_POSTMASTER_DEATH)
780  PG_RETURN_BOOL(false);
781  }
782 
784  (errmsg("server did not promote within %d seconds", wait_seconds)));
785  PG_RETURN_BOOL(false);
786 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int errhint(const char *fmt,...)
Definition: elog.c:1069
static int wait_seconds
Definition: pg_ctl.c:80
#define WL_TIMEOUT
Definition: latch.h:127
#define SIGUSR1
Definition: win32_port.h:166
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:365
int errcode(int sqlerrcode)
Definition: elog.c:608
#define kill(pid, sig)
Definition: win32_port.h:426
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
bool RecoveryInProgress(void)
Definition: xlog.c:7935
void ResetLatch(Latch *latch)
Definition: latch.c:519
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
#define ERROR
Definition: elog.h:43
int errcode_for_file_access(void)
Definition: elog.c:631
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2242
#define ereport(elevel, rest)
Definition: elog.h:141
pid_t PostmasterPid
Definition: globals.c:95
#define WARNING
Definition: elog.h:40
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:105
int FreeFile(FILE *file)
Definition: fd.c:2441
#define WAITS_PER_SECOND
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define WL_LATCH_SET
Definition: latch.h:124

◆ pg_start_backup()

Datum pg_start_backup ( PG_FUNCTION_ARGS  )

Definition at line 72 of file xlogfuncs.c.

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

73 {
74  text *backupid = PG_GETARG_TEXT_PP(0);
75  bool fast = PG_GETARG_BOOL(1);
76  bool exclusive = PG_GETARG_BOOL(2);
77  char *backupidstr;
78  XLogRecPtr startpoint;
80 
81  backupidstr = text_to_cstring(backupid);
82 
83  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
84  ereport(ERROR,
85  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
86  errmsg("a backup is already in progress in this session")));
87 
88  if (exclusive)
89  {
90  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
91  NULL, NULL, false, true);
92  }
93  else
94  {
95  MemoryContext oldcontext;
96 
97  /*
98  * Label file and tablespace map file need to be long-lived, since
99  * they are read in pg_stop_backup.
100  */
104  MemoryContextSwitchTo(oldcontext);
105 
106  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
107  NULL, tblspc_map_file, false, true);
108 
110  }
111 
112  PG_RETURN_LSN(startpoint);
113 }
static StringInfo label_file
Definition: xlogfuncs.c:44
SessionBackupState get_backup_status(void)
Definition: xlog.c:10718
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, List **tablespaces, StringInfo tblspcmapfile, bool infotbssize, bool needtblspcmapfile)
Definition: xlog.c:10220
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:52
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
SessionBackupState
Definition: xlog.h:339
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
uintptr_t Datum
Definition: postgres.h:367
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:45
uint64 XLogRecPtr
Definition: xlogdefs.h:21
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:822
Definition: c.h:556
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:226

◆ pg_stop_backup()

Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

Definition at line 136 of file xlogfuncs.c.

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

137 {
138  XLogRecPtr stoppoint;
140 
141  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
142  ereport(ERROR,
143  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
144  errmsg("non-exclusive backup in progress"),
145  errhint("Did you mean to use pg_stop_backup('f')?")));
146 
147  /*
148  * Exclusive backups were typically started in a different connection, so
149  * don't try to verify that status of backup is set to
150  * SESSION_BACKUP_EXCLUSIVE in this function. Actual verification that an
151  * exclusive backup is in fact running is handled inside
152  * do_pg_stop_backup.
153  */
154  stoppoint = do_pg_stop_backup(NULL, true, NULL);
155 
156  PG_RETURN_LSN(stoppoint);
157 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
SessionBackupState get_backup_status(void)
Definition: xlog.c:10718
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:10739
SessionBackupState
Definition: xlog.h:339
#define ereport(elevel, rest)
Definition: elog.h:141
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:822
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:226

◆ pg_stop_backup_v2()

Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

Definition at line 178 of file xlogfuncs.c.

References ReturnSetInfo::allowedModes, cancel_before_shmem_exit(), 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, LSNGetDatum, MemoryContextSwitchTo(), MemSet, nonexclusive_base_backup_cleanup(), pfree(), PG_GETARG_BOOL, ReturnSetInfo::returnMode, SESSION_BACKUP_NON_EXCLUSIVE, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, status(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

179 {
180  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
181  TupleDesc tupdesc;
182  Tuplestorestate *tupstore;
183  MemoryContext per_query_ctx;
184  MemoryContext oldcontext;
185  Datum values[3];
186  bool nulls[3];
187 
188  bool exclusive = PG_GETARG_BOOL(0);
189  bool waitforarchive = PG_GETARG_BOOL(1);
190  XLogRecPtr stoppoint;
192 
193  /* check to see if caller supports us returning a tuplestore */
194  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
195  ereport(ERROR,
196  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
197  errmsg("set-valued function called in context that cannot accept a set")));
198  if (!(rsinfo->allowedModes & SFRM_Materialize))
199  ereport(ERROR,
200  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
201  errmsg("materialize mode required, but it is not " \
202  "allowed in this context")));
203 
204  /* Build a tuple descriptor for our result type */
205  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
206  elog(ERROR, "return type must be a row type");
207 
208  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
209  oldcontext = MemoryContextSwitchTo(per_query_ctx);
210 
211  tupstore = tuplestore_begin_heap(true, false, work_mem);
212  rsinfo->returnMode = SFRM_Materialize;
213  rsinfo->setResult = tupstore;
214  rsinfo->setDesc = tupdesc;
215 
216  MemoryContextSwitchTo(oldcontext);
217 
218  MemSet(values, 0, sizeof(values));
219  MemSet(nulls, 0, sizeof(nulls));
220 
221  if (exclusive)
222  {
223  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
224  ereport(ERROR,
225  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
226  errmsg("non-exclusive backup in progress"),
227  errhint("Did you mean to use pg_stop_backup('f')?")));
228 
229  /*
230  * Stop the exclusive backup, and since we're in an exclusive backup
231  * return NULL for both backup_label and tablespace_map.
232  */
233  stoppoint = do_pg_stop_backup(NULL, waitforarchive, NULL);
234 
235  nulls[1] = true;
236  nulls[2] = true;
237  }
238  else
239  {
240  if (status != SESSION_BACKUP_NON_EXCLUSIVE)
241  ereport(ERROR,
242  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
243  errmsg("non-exclusive backup is not in progress"),
244  errhint("Did you mean to use pg_stop_backup('t')?")));
245 
246  /*
247  * Stop the non-exclusive backup. Return a copy of the backup label
248  * and tablespace map so they can be written to disk by the caller.
249  */
250  stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
252 
255 
256  /* Free structures allocated in TopMemoryContext */
258  pfree(label_file);
259  label_file = NULL;
262  tblspc_map_file = NULL;
263  }
264 
265  /* Stoppoint is included on both exclusive and nonexclusive backups */
266  values[0] = LSNGetDatum(stoppoint);
267 
268  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
269  tuplestore_donestoring(tupstore);
270 
271  return (Datum) 0;
272 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
static StringInfo label_file
Definition: xlogfuncs.c:44
int errhint(const char *fmt,...)
Definition: elog.c:1069
SessionBackupState get_backup_status(void)
Definition: xlog.c:10718
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
#define MemSet(start, val, len)
Definition: c.h:962
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:52
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:10739
SessionBackupState
Definition: xlog.h:339
void cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:390
#define ereport(elevel, rest)
Definition: elog.h:141
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
int allowedModes
Definition: execnodes.h:302
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:45
SetFunctionReturnMode returnMode
Definition: execnodes.h:304
uint64 XLogRecPtr
Definition: xlogdefs.h:21
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:230
Tuplestorestate * setResult
Definition: execnodes.h:307
static Datum values[MAXATTR]
Definition: bootstrap.c:167
ExprContext * econtext
Definition: execnodes.h:300
TupleDesc setDesc
Definition: execnodes.h:308
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define CStringGetTextDatum(s)
Definition: builtins.h:83
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:226

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 281 of file xlogfuncs.c.

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

282 {
283  XLogRecPtr switchpoint;
284 
285  if (RecoveryInProgress())
286  ereport(ERROR,
287  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
288  errmsg("recovery is in progress"),
289  errhint("WAL control functions cannot be executed during recovery.")));
290 
291  switchpoint = RequestXLogSwitch(false);
292 
293  /*
294  * As a convenience, return the WAL location of the switch record
295  */
296  PG_RETURN_LSN(switchpoint);
297 }
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9414
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_wal_lsn_diff()

Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 619 of file xlogfuncs.c.

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

620 {
621  Datum result;
622 
624  PG_GETARG_DATUM(0),
625  PG_GETARG_DATUM(1));
626 
627  PG_RETURN_NUMERIC(result);
628 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:230
uintptr_t Datum
Definition: postgres.h:367
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 540 of file xlogfuncs.c.

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

541 {
542  if (!RecoveryInProgress())
543  ereport(ERROR,
544  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
545  errmsg("recovery is not in progress"),
546  errhint("Recovery control functions can only be executed during recovery.")));
547 
548  SetRecoveryPause(true);
549 
550  PG_RETURN_VOID();
551 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_VOID()
Definition: fmgr.h:339
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5948
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_wal_replay_resume()

Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 560 of file xlogfuncs.c.

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

561 {
562  if (!RecoveryInProgress())
563  ereport(ERROR,
564  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
565  errmsg("recovery is not in progress"),
566  errhint("Recovery control functions can only be executed during recovery.")));
567 
568  SetRecoveryPause(false);
569 
570  PG_RETURN_VOID();
571 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_VOID()
Definition: fmgr.h:339
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5948
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 514 of file xlogfuncs.c.

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

515 {
516  XLogSegNo xlogsegno;
517  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
518  char xlogfilename[MAXFNAMELEN];
519 
520  if (RecoveryInProgress())
521  ereport(ERROR,
522  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
523  errmsg("recovery is in progress"),
524  errhint("%s cannot be executed during recovery.",
525  "pg_walfile_name()")));
526 
527  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
528  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size);
529 
530  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
531 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int wal_segment_size
Definition: xlog.c:112
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define ERROR
Definition: elog.h:43
uint64 XLogSegNo
Definition: xlogdefs.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
#define MAXFNAMELEN
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
TimeLineID ThisTimeLineID
Definition: xlog.c:187
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 451 of file xlogfuncs.c.

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

452 {
453  XLogSegNo xlogsegno;
454  uint32 xrecoff;
455  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
456  char xlogfilename[MAXFNAMELEN];
457  Datum values[2];
458  bool isnull[2];
459  TupleDesc resultTupleDesc;
460  HeapTuple resultHeapTuple;
461  Datum result;
462 
463  if (RecoveryInProgress())
464  ereport(ERROR,
465  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
466  errmsg("recovery is in progress"),
467  errhint("%s cannot be executed during recovery.",
468  "pg_walfile_name_offset()")));
469 
470  /*
471  * Construct a tuple descriptor for the result row. This must match this
472  * function's pg_proc entry!
473  */
474  resultTupleDesc = CreateTemplateTupleDesc(2);
475  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
476  TEXTOID, -1, 0);
477  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
478  INT4OID, -1, 0);
479 
480  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
481 
482  /*
483  * xlogfilename
484  */
485  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
486  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size);
487 
488  values[0] = CStringGetTextDatum(xlogfilename);
489  isnull[0] = false;
490 
491  /*
492  * offset
493  */
494  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
495 
496  values[1] = UInt32GetDatum(xrecoff);
497  isnull[1] = false;
498 
499  /*
500  * Tuple jam: Having first prepared your Datums, then squash together
501  */
502  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
503 
504  result = HeapTupleGetDatum(resultHeapTuple);
505 
506  PG_RETURN_DATUM(result);
507 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int wal_segment_size
Definition: xlog.c:112
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
int errcode(int sqlerrcode)
Definition: elog.c:608
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:359
#define UInt32GetDatum(X)
Definition: postgres.h:493
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define ereport(elevel, rest)
Definition: elog.h:141
#define MAXFNAMELEN
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
TimeLineID ThisTimeLineID
Definition: xlog.c:187
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define CStringGetTextDatum(s)
Definition: builtins.h:83
int16 AttrNumber
Definition: attnum.h:21

Variable Documentation

◆ label_file

StringInfo label_file
static

Definition at line 44 of file xlogfuncs.c.

◆ tblspc_map_file

StringInfo tblspc_map_file
static

Definition at line 45 of file xlogfuncs.c.

Referenced by perform_base_backup().