PostgreSQL Source Code  git master
xlogfuncs.c File Reference
#include "postgres.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 "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.

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)
 

Variables

static StringInfo label_file
 
static StringInfo tblspc_map_file
 

Function Documentation

◆ nonexclusive_base_backup_cleanup()

static void nonexclusive_base_backup_cleanup ( int  code,
Datum  arg 
)
static

Definition at line 50 of file xlogfuncs.c.

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

Referenced by pg_start_backup(), and pg_stop_backup_v2().

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

◆ pg_backup_start_time()

Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

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

643 {
644  Datum xtime;
645  FILE *lfp;
646  char fline[MAXPGPATH];
647  char backup_start_time[30];
648 
649  /*
650  * See if label file is present
651  */
652  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
653  if (lfp == NULL)
654  {
655  if (errno != ENOENT)
656  ereport(ERROR,
658  errmsg("could not read file \"%s\": %m",
660  PG_RETURN_NULL();
661  }
662 
663  /*
664  * Parse the file to find the START TIME line.
665  */
666  backup_start_time[0] = '\0';
667  while (fgets(fline, sizeof(fline), lfp) != NULL)
668  {
669  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
670  break;
671  }
672 
673  /* Check for a read error. */
674  if (ferror(lfp))
675  ereport(ERROR,
677  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
678 
679  /* Close the backup label file. */
680  if (FreeFile(lfp))
681  ereport(ERROR,
683  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
684 
685  if (strlen(backup_start_time) == 0)
686  ereport(ERROR,
687  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
688  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
689 
690  /*
691  * Convert the time string read from file to TimestampTz form.
692  */
694  CStringGetDatum(backup_start_time),
696  Int32GetDatum(-1));
697 
698  PG_RETURN_DATUM(xtime);
699 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
#define CStringGetDatum(X)
Definition: postgres.h:563
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2336
#define ereport(elevel, rest)
Definition: elog.h:122
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:594
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:318
#define InvalidOid
Definition: postgres_ext.h:36
int FreeFile(FILE *file)
Definition: fd.c:2528
#define Int32GetDatum(X)
Definition: postgres.h:464
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:383
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BACKUP_LABEL_FILE
Definition: xlog.h:322
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

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

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

◆ pg_current_wal_flush_lsn()

Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 387 of file xlogfuncs.c.

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

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

◆ pg_current_wal_insert_lsn()

Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 366 of file xlogfuncs.c.

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

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

◆ pg_current_wal_lsn()

Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 345 of file xlogfuncs.c.

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

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

◆ pg_is_in_backup()

Datum pg_is_in_backup ( PG_FUNCTION_ARGS  )

Definition at line 630 of file xlogfuncs.c.

References BackupInProgress(), and PG_RETURN_BOOL.

631 {
633 }
bool BackupInProgress(void)
Definition: xlog.c:11490
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 606 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

607 {
609 }
bool RecoveryInProgress(void)
Definition: xlog.c:7949
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ pg_is_wal_replay_paused()

Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 573 of file xlogfuncs.c.

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

574 {
575  if (!RecoveryInProgress())
576  ereport(ERROR,
577  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
578  errmsg("recovery is not in progress"),
579  errhint("Recovery control functions can only be executed during recovery.")));
580 
582 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7949
bool RecoveryIsPaused(void)
Definition: xlog.c:6028
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_last_wal_receive_lsn()

Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 409 of file xlogfuncs.c.

References GetWalRcvWriteRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

410 {
411  XLogRecPtr recptr;
412 
413  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
414 
415  if (recptr == 0)
416  PG_RETURN_NULL();
417 
418  PG_RETURN_LSN(recptr);
419 }
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:310

◆ pg_last_wal_replay_lsn()

Datum pg_last_wal_replay_lsn ( PG_FUNCTION_ARGS  )

Definition at line 428 of file xlogfuncs.c.

References GetXLogReplayRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

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

◆ pg_last_xact_replay_timestamp()

Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 591 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

592 {
593  TimestampTz xtime;
594 
595  xtime = GetLatestXTime();
596  if (xtime == 0)
597  PG_RETURN_NULL();
598 
599  PG_RETURN_TIMESTAMPTZ(xtime);
600 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6159
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ pg_start_backup()

Datum pg_start_backup ( PG_FUNCTION_ARGS  )

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

71 {
72  text *backupid = PG_GETARG_TEXT_PP(0);
73  bool fast = PG_GETARG_BOOL(1);
74  bool exclusive = PG_GETARG_BOOL(2);
75  char *backupidstr;
76  XLogRecPtr startpoint;
78 
79  backupidstr = text_to_cstring(backupid);
80 
81  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
82  ereport(ERROR,
83  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
84  errmsg("a backup is already in progress in this session")));
85 
86  if (exclusive)
87  {
88  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
89  NULL, NULL, false, true);
90  }
91  else
92  {
93  MemoryContext oldcontext;
94 
95  /*
96  * Label file and tablespace map file need to be long-lived, since
97  * they are read in pg_stop_backup.
98  */
102  MemoryContextSwitchTo(oldcontext);
103 
104  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
105  NULL, tblspc_map_file, false, true);
106 
108  }
109 
110  PG_RETURN_LSN(startpoint);
111 }
static StringInfo label_file
Definition: xlogfuncs.c:42
SessionBackupState get_backup_status(void)
Definition: xlog.c:10742
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
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:10244
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:50
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
SessionBackupState
Definition: xlog.h:305
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:331
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
uintptr_t Datum
Definition: postgres.h:367
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:43
uint64 XLogRecPtr
Definition: xlogdefs.h:21
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ pg_stop_backup()

Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

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

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

◆ pg_stop_backup_v2()

Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

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

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

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 279 of file xlogfuncs.c.

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

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

◆ pg_wal_lsn_diff()

Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 615 of file xlogfuncs.c.

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

616 {
617  Datum result;
618 
620  PG_GETARG_DATUM(0),
621  PG_GETARG_DATUM(1));
622 
623  PG_RETURN_NUMERIC(result);
624 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:194
uintptr_t Datum
Definition: postgres.h:367
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 536 of file xlogfuncs.c.

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

537 {
538  if (!RecoveryInProgress())
539  ereport(ERROR,
540  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
541  errmsg("recovery is not in progress"),
542  errhint("Recovery control functions can only be executed during recovery.")));
543 
544  SetRecoveryPause(true);
545 
546  PG_RETURN_VOID();
547 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7949
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_VOID()
Definition: fmgr.h:314
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:6040
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_wal_replay_resume()

Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 556 of file xlogfuncs.c.

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

557 {
558  if (!RecoveryInProgress())
559  ereport(ERROR,
560  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
561  errmsg("recovery is not in progress"),
562  errhint("Recovery control functions can only be executed during recovery.")));
563 
564  SetRecoveryPause(false);
565 
566  PG_RETURN_VOID();
567 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7949
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_VOID()
Definition: fmgr.h:314
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:6040
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

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

512 {
513  XLogSegNo xlogsegno;
514  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
515  char xlogfilename[MAXFNAMELEN];
516 
517  if (RecoveryInProgress())
518  ereport(ERROR,
519  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
520  errmsg("recovery is in progress"),
521  errhint("pg_walfile_name() cannot be executed during recovery.")));
522 
523  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
524  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size);
525 
526  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
527 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int wal_segment_size
Definition: xlog.c:113
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7949
#define ERROR
Definition: elog.h:43
uint64 XLogSegNo
Definition: xlogdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:122
#define MAXFNAMELEN
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
TimeLineID ThisTimeLineID
Definition: xlog.c:181
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
text * cstring_to_text(const char *s)
Definition: varlena.c:149
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

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

450 {
451  XLogSegNo xlogsegno;
452  uint32 xrecoff;
453  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
454  char xlogfilename[MAXFNAMELEN];
455  Datum values[2];
456  bool isnull[2];
457  TupleDesc resultTupleDesc;
458  HeapTuple resultHeapTuple;
459  Datum result;
460 
461  if (RecoveryInProgress())
462  ereport(ERROR,
463  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
464  errmsg("recovery is in progress"),
465  errhint("pg_walfile_name_offset() cannot be executed during recovery.")));
466 
467  /*
468  * Construct a tuple descriptor for the result row. This must match this
469  * function's pg_proc entry!
470  */
471  resultTupleDesc = CreateTemplateTupleDesc(2, false);
472  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
473  TEXTOID, -1, 0);
474  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
475  INT4OID, -1, 0);
476 
477  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
478 
479  /*
480  * xlogfilename
481  */
482  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
483  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size);
484 
485  values[0] = CStringGetTextDatum(xlogfilename);
486  isnull[0] = false;
487 
488  /*
489  * offset
490  */
491  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
492 
493  values[1] = UInt32GetDatum(xrecoff);
494  isnull[1] = false;
495 
496  /*
497  * Tuple jam: Having first prepared your Datums, then squash together
498  */
499  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
500 
501  result = HeapTupleGetDatum(resultHeapTuple);
502 
503  PG_RETURN_DATUM(result);
504 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int wal_segment_size
Definition: xlog.c:113
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
bool RecoveryInProgress(void)
Definition: xlog.c:7949
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1109
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:325
#define UInt32GetDatum(X)
Definition: postgres.h:478
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:600
#define ereport(elevel, rest)
Definition: elog.h:122
#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:318
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
TimeLineID ThisTimeLineID
Definition: xlog.c:181
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:231
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:45
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:95
int16 AttrNumber
Definition: attnum.h:21

Variable Documentation

◆ label_file

StringInfo label_file
static

Definition at line 42 of file xlogfuncs.c.

◆ tblspc_map_file

StringInfo tblspc_map_file
static

Definition at line 43 of file xlogfuncs.c.

Referenced by perform_base_backup().