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/smgr.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/numeric.h"
#include "utils/guc.h"
#include "utils/pg_lsn.h"
#include "utils/timestamp.h"
#include "utils/tuplestore.h"
#include "storage/fd.h"
#include "storage/ipc.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 53 of file xlogfuncs.c.

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

Referenced by pg_start_backup(), and pg_stop_backup_v2().

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

◆ pg_backup_start_time()

Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

Definition at line 647 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().

648 {
649  Datum xtime;
650  FILE *lfp;
651  char fline[MAXPGPATH];
652  char backup_start_time[30];
653 
654  /*
655  * See if label file is present
656  */
657  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
658  if (lfp == NULL)
659  {
660  if (errno != ENOENT)
661  ereport(ERROR,
663  errmsg("could not read file \"%s\": %m",
665  PG_RETURN_NULL();
666  }
667 
668  /*
669  * Parse the file to find the START TIME line.
670  */
671  backup_start_time[0] = '\0';
672  while (fgets(fline, sizeof(fline), lfp) != NULL)
673  {
674  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
675  break;
676  }
677 
678  /* Check for a read error. */
679  if (ferror(lfp))
680  ereport(ERROR,
682  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
683 
684  /* Close the backup label file. */
685  if (FreeFile(lfp))
686  ereport(ERROR,
688  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
689 
690  if (strlen(backup_start_time) == 0)
691  ereport(ERROR,
692  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
693  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
694 
695  /*
696  * Convert the time string read from file to TimestampTz form.
697  */
699  CStringGetDatum(backup_start_time),
701  Int32GetDatum(-1));
702 
703  PG_RETURN_DATUM(xtime);
704 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:593
#define CStringGetDatum(X)
Definition: postgres.h:578
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2207
#define ereport(elevel, rest)
Definition: elog.h:141
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:620
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:2406
#define Int32GetDatum(X)
Definition: postgres.h:479
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:386
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define BACKUP_LABEL_FILE
Definition: xlog.h:359
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 307 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().

308 {
309  text *restore_name = PG_GETARG_TEXT_PP(0);
310  char *restore_name_str;
311  XLogRecPtr restorepoint;
312 
313  if (RecoveryInProgress())
314  ereport(ERROR,
315  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
316  (errmsg("recovery is in progress"),
317  errhint("WAL control functions cannot be executed during recovery."))));
318 
319  if (!XLogIsNeeded())
320  ereport(ERROR,
321  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
322  errmsg("WAL level not sufficient for creating a restore point"),
323  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
324 
325  restore_name_str = text_to_cstring(restore_name);
326 
327  if (strlen(restore_name_str) >= MAXFNAMELEN)
328  ereport(ERROR,
329  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
330  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
331 
332  restorepoint = XLogRestorePoint(restore_name_str);
333 
334  /*
335  * As a convenience, return the WAL location of the restore point record
336  */
337  PG_RETURN_LSN(restorepoint);
338 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define XLogIsNeeded()
Definition: xlog.h:181
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:9395
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ pg_current_wal_flush_lsn()

Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 390 of file xlogfuncs.c.

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

391 {
392  XLogRecPtr current_recptr;
393 
394  if (RecoveryInProgress())
395  ereport(ERROR,
396  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
397  errmsg("recovery is in progress"),
398  errhint("WAL control functions cannot be executed during recovery.")));
399 
400  current_recptr = GetFlushRecPtr();
401 
402  PG_RETURN_LSN(current_recptr);
403 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8230
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:784

◆ pg_current_wal_insert_lsn()

Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 369 of file xlogfuncs.c.

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

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

◆ pg_current_wal_lsn()

Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 348 of file xlogfuncs.c.

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

349 {
350  XLogRecPtr current_recptr;
351 
352  if (RecoveryInProgress())
353  ereport(ERROR,
354  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
355  errmsg("recovery is in progress"),
356  errhint("WAL control functions cannot be executed during recovery.")));
357 
358  current_recptr = GetXLogWriteRecPtr();
359 
360  PG_RETURN_LSN(current_recptr);
361 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:11165
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_is_in_backup()

Datum pg_is_in_backup ( PG_FUNCTION_ARGS  )

Definition at line 635 of file xlogfuncs.c.

References BackupInProgress(), and PG_RETURN_BOOL.

636 {
638 }
bool BackupInProgress(void)
Definition: xlog.c:11426
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 611 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

612 {
614 }
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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 578 of file xlogfuncs.c.

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

579 {
580  if (!RecoveryInProgress())
581  ereport(ERROR,
582  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
583  errmsg("recovery is not in progress"),
584  errhint("Recovery control functions can only be executed during recovery.")));
585 
587 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
bool RecoveryIsPaused(void)
Definition: xlog.c:5930
#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:784

◆ pg_last_wal_receive_lsn()

Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 412 of file xlogfuncs.c.

References GetWalRcvWriteRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

413 {
414  XLogRecPtr recptr;
415 
416  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
417 
418  if (recptr == 0)
419  PG_RETURN_NULL();
420 
421  PG_RETURN_LSN(recptr);
422 }
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 431 of file xlogfuncs.c.

References GetXLogReplayRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

432 {
433  XLogRecPtr recptr;
434 
435  recptr = GetXLogReplayRecPtr(NULL);
436 
437  if (recptr == 0)
438  PG_RETURN_NULL();
439 
440  PG_RETURN_LSN(recptr);
441 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11130
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 596 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

597 {
598  TimestampTz xtime;
599 
600  xtime = GetLatestXTime();
601  if (xtime == 0)
602  PG_RETURN_NULL();
603 
604  PG_RETURN_TIMESTAMPTZ(xtime);
605 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6064
#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 713 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.

714 {
715  bool wait = PG_GETARG_BOOL(0);
716  int wait_seconds = PG_GETARG_INT32(1);
717  FILE *promote_file;
718  int i;
719 
720  if (!RecoveryInProgress())
721  ereport(ERROR,
722  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
723  errmsg("recovery is not in progress"),
724  errhint("Recovery control functions can only be executed during recovery.")));
725 
726  if (wait_seconds <= 0)
727  ereport(ERROR,
728  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
729  errmsg("\"wait_seconds\" cannot be negative or equal zero")));
730 
731  /* create the promote signal file */
732  promote_file = AllocateFile(PROMOTE_SIGNAL_FILE, "w");
733  if (!promote_file)
734  ereport(ERROR,
736  errmsg("could not create file \"%s\": %m",
738 
739  if (FreeFile(promote_file))
740  ereport(ERROR,
742  errmsg("could not write file \"%s\": %m",
744 
745  /* signal the postmaster */
746  if (kill(PostmasterPid, SIGUSR1) != 0)
747  {
749  (errmsg("failed to send signal to postmaster: %m")));
750  (void) unlink(PROMOTE_SIGNAL_FILE);
751  PG_RETURN_BOOL(false);
752  }
753 
754  /* return immediately if waiting was not requested */
755  if (!wait)
756  PG_RETURN_BOOL(true);
757 
758  /* wait for the amount of time wanted until promotion */
759 #define WAITS_PER_SECOND 10
760  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
761  {
763 
764  if (!RecoveryInProgress())
765  PG_RETURN_BOOL(true);
766 
768 
769  (void) WaitLatch(MyLatch,
771  1000L / WAITS_PER_SECOND,
773  }
774 
776  (errmsg("server did not promote within %d seconds", wait_seconds)));
777  PG_RETURN_BOOL(false);
778 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int errhint(const char *fmt,...)
Definition: elog.c:974
static int wait_seconds
Definition: pg_ctl.c:80
#define WL_TIMEOUT
Definition: latch.h:127
#define SIGUSR1
Definition: win32_port.h:175
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:366
int errcode(int sqlerrcode)
Definition: elog.c:570
#define kill(pid, sig)
Definition: win32_port.h:435
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
bool RecoveryInProgress(void)
Definition: xlog.c:7898
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:593
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2207
#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:2406
#define WAITS_PER_SECOND
int errmsg(const char *fmt,...)
Definition: elog.c:784
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 73 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.

74 {
75  text *backupid = PG_GETARG_TEXT_PP(0);
76  bool fast = PG_GETARG_BOOL(1);
77  bool exclusive = PG_GETARG_BOOL(2);
78  char *backupidstr;
79  XLogRecPtr startpoint;
81 
82  backupidstr = text_to_cstring(backupid);
83 
84  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
85  ereport(ERROR,
86  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
87  errmsg("a backup is already in progress in this session")));
88 
89  if (exclusive)
90  {
91  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
92  NULL, NULL, false, true);
93  }
94  else
95  {
96  MemoryContext oldcontext;
97 
98  /*
99  * Label file and tablespace map file need to be long-lived, since
100  * they are read in pg_stop_backup.
101  */
105  MemoryContextSwitchTo(oldcontext);
106 
107  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
108  NULL, tblspc_map_file, false, true);
109 
111  }
112 
113  PG_RETURN_LSN(startpoint);
114 }
static StringInfo label_file
Definition: xlogfuncs.c:45
SessionBackupState get_backup_status(void)
Definition: xlog.c:10676
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
#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:10178
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:53
#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:340
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:46
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:784
Definition: c.h:549
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ pg_stop_backup()

Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

Definition at line 137 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().

138 {
139  XLogRecPtr stoppoint;
141 
142  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
143  ereport(ERROR,
144  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
145  errmsg("non-exclusive backup in progress"),
146  errhint("Did you mean to use pg_stop_backup('f')?")));
147 
148  /*
149  * Exclusive backups were typically started in a different connection, so
150  * don't try to verify that status of backup is set to
151  * SESSION_BACKUP_EXCLUSIVE in this function. Actual verification that an
152  * exclusive backup is in fact running is handled inside
153  * do_pg_stop_backup.
154  */
155  stoppoint = do_pg_stop_backup(NULL, true, NULL);
156 
157  PG_RETURN_LSN(stoppoint);
158 }
int errhint(const char *fmt,...)
Definition: elog.c:974
SessionBackupState get_backup_status(void)
Definition: xlog.c:10676
int errcode(int sqlerrcode)
Definition: elog.c:570
#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:10697
SessionBackupState
Definition: xlog.h:340
#define ereport(elevel, rest)
Definition: elog.h:141
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:784
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ pg_stop_backup_v2()

Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

Definition at line 179 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.

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

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 282 of file xlogfuncs.c.

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

283 {
284  XLogRecPtr switchpoint;
285 
286  if (RecoveryInProgress())
287  ereport(ERROR,
288  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
289  errmsg("recovery is in progress"),
290  errhint("WAL control functions cannot be executed during recovery.")));
291 
292  switchpoint = RequestXLogSwitch(false);
293 
294  /*
295  * As a convenience, return the WAL location of the switch record
296  */
297  PG_RETURN_LSN(switchpoint);
298 }
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9377
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:784

◆ pg_wal_lsn_diff()

Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 620 of file xlogfuncs.c.

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

621 {
622  Datum result;
623 
625  PG_GETARG_DATUM(0),
626  PG_GETARG_DATUM(1));
627 
628  PG_RETURN_NUMERIC(result);
629 }
#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:618

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 541 of file xlogfuncs.c.

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

542 {
543  if (!RecoveryInProgress())
544  ereport(ERROR,
545  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
546  errmsg("recovery is not in progress"),
547  errhint("Recovery control functions can only be executed during recovery.")));
548 
549  SetRecoveryPause(true);
550 
551  PG_RETURN_VOID();
552 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:5942
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_wal_replay_resume()

Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 561 of file xlogfuncs.c.

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

562 {
563  if (!RecoveryInProgress())
564  ereport(ERROR,
565  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
566  errmsg("recovery is not in progress"),
567  errhint("Recovery control functions can only be executed during recovery.")));
568 
569  SetRecoveryPause(false);
570 
571  PG_RETURN_VOID();
572 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:5942
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 515 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.

516 {
517  XLogSegNo xlogsegno;
518  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
519  char xlogfilename[MAXFNAMELEN];
520 
521  if (RecoveryInProgress())
522  ereport(ERROR,
523  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
524  errmsg("recovery is in progress"),
525  errhint("%s cannot be executed during recovery.",
526  "pg_walfile_name()")));
527 
528  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
529  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size);
530 
531  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
532 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int wal_segment_size
Definition: xlog.c:112
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#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:784

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 452 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.

453 {
454  XLogSegNo xlogsegno;
455  uint32 xrecoff;
456  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
457  char xlogfilename[MAXFNAMELEN];
458  Datum values[2];
459  bool isnull[2];
460  TupleDesc resultTupleDesc;
461  HeapTuple resultHeapTuple;
462  Datum result;
463 
464  if (RecoveryInProgress())
465  ereport(ERROR,
466  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
467  errmsg("recovery is in progress"),
468  errhint("%s cannot be executed during recovery.",
469  "pg_walfile_name_offset()")));
470 
471  /*
472  * Construct a tuple descriptor for the result row. This must match this
473  * function's pg_proc entry!
474  */
475  resultTupleDesc = CreateTemplateTupleDesc(2);
476  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
477  TEXTOID, -1, 0);
478  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
479  INT4OID, -1, 0);
480 
481  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
482 
483  /*
484  * xlogfilename
485  */
486  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
487  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size);
488 
489  values[0] = CStringGetTextDatum(xlogfilename);
490  isnull[0] = false;
491 
492  /*
493  * offset
494  */
495  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
496 
497  values[1] = UInt32GetDatum(xrecoff);
498  isnull[1] = false;
499 
500  /*
501  * Tuple jam: Having first prepared your Datums, then squash together
502  */
503  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
504 
505  result = HeapTupleGetDatum(resultHeapTuple);
506 
507  PG_RETURN_DATUM(result);
508 }
int errhint(const char *fmt,...)
Definition: elog.c:974
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:570
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
bool RecoveryInProgress(void)
Definition: xlog.c:7898
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2048
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:358
#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:221
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define CStringGetTextDatum(s)
Definition: builtins.h:83
int16 AttrNumber
Definition: attnum.h:21

Variable Documentation

◆ label_file

StringInfo label_file
static

Definition at line 45 of file xlogfuncs.c.

◆ tblspc_map_file

StringInfo tblspc_map_file
static

Definition at line 46 of file xlogfuncs.c.

Referenced by perform_base_backup().