PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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/catalog.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

static void nonexclusive_base_backup_cleanup ( int  code,
Datum  arg 
)
static

Definition at line 51 of file xlogfuncs.c.

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

Referenced by pg_start_backup(), and pg_stop_backup_v2().

52 {
55  (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
56 }
#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:11074
Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

Definition at line 652 of file xlogfuncs.c.

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

653 {
654  Datum xtime;
655  FILE *lfp;
656  char fline[MAXPGPATH];
657  char backup_start_time[30];
658 
659  /*
660  * See if label file is present
661  */
662  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
663  if (lfp == NULL)
664  {
665  if (errno != ENOENT)
666  ereport(ERROR,
668  errmsg("could not read file \"%s\": %m",
670  PG_RETURN_NULL();
671  }
672 
673  /*
674  * Parse the file to find the START TIME line.
675  */
676  backup_start_time[0] = '\0';
677  while (fgets(fline, sizeof(fline), lfp) != NULL)
678  {
679  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
680  break;
681  }
682 
683  /* Check for a read error. */
684  if (ferror(lfp))
685  ereport(ERROR,
687  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
688 
689  /* Close the backup label file. */
690  if (FreeFile(lfp))
691  ereport(ERROR,
693  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
694 
695  if (strlen(backup_start_time) == 0)
696  ereport(ERROR,
697  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
698  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
699 
700  /*
701  * Convert the time string read from file to TimestampTz form.
702  */
704  CStringGetDatum(backup_start_time),
706  Int32GetDatum(-1));
707 
708  PG_RETURN_DATUM(xtime);
709 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
#define CStringGetDatum(X)
Definition: postgres.h:584
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2094
#define ereport(elevel, rest)
Definition: elog.h:122
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
int FreeFile(FILE *file)
Definition: fd.c:2277
#define Int32GetDatum(X)
Definition: postgres.h:485
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:320
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

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

315 {
316  text *restore_name = PG_GETARG_TEXT_PP(0);
317  char *restore_name_str;
318  XLogRecPtr restorepoint;
319 
320  if (RecoveryInProgress())
321  ereport(ERROR,
322  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
323  (errmsg("recovery is in progress"),
324  errhint("WAL control functions cannot be executed during recovery."))));
325 
326  if (!XLogIsNeeded())
327  ereport(ERROR,
328  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
329  errmsg("WAL level not sufficient for creating a restore point"),
330  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
331 
332  restore_name_str = text_to_cstring(restore_name);
333 
334  if (strlen(restore_name_str) >= MAXFNAMELEN)
335  ereport(ERROR,
336  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
337  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
338 
339  restorepoint = XLogRestorePoint(restore_name_str);
340 
341  /*
342  * As a convenience, return the WAL location of the restore point record
343  */
344  PG_RETURN_LSN(restorepoint);
345 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define XLogIsNeeded()
Definition: xlog.h:145
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#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:9429
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 397 of file xlogfuncs.c.

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

398 {
399  XLogRecPtr current_recptr;
400 
401  if (RecoveryInProgress())
402  ereport(ERROR,
403  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
404  errmsg("recovery is in progress"),
405  errhint("WAL control functions cannot be executed during recovery.")));
406 
407  current_recptr = GetFlushRecPtr();
408 
409  PG_RETURN_LSN(current_recptr);
410 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8227
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#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
Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 376 of file xlogfuncs.c.

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

377 {
378  XLogRecPtr current_recptr;
379 
380  if (RecoveryInProgress())
381  ereport(ERROR,
382  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
383  errmsg("recovery is in progress"),
384  errhint("WAL control functions cannot be executed during recovery.")));
385 
386  current_recptr = GetXLogInsertRecPtr();
387 
388  PG_RETURN_LSN(current_recptr);
389 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11113
#define ereport(elevel, rest)
Definition: elog.h:122
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 355 of file xlogfuncs.c.

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

356 {
357  XLogRecPtr current_recptr;
358 
359  if (RecoveryInProgress())
360  ereport(ERROR,
361  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
362  errmsg("recovery is in progress"),
363  errhint("WAL control functions cannot be executed during recovery.")));
364 
365  current_recptr = GetXLogWriteRecPtr();
366 
367  PG_RETURN_LSN(current_recptr);
368 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#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:11129
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_is_in_backup ( PG_FUNCTION_ARGS  )

Definition at line 640 of file xlogfuncs.c.

References BackupInProgress(), and PG_RETURN_BOOL.

641 {
643 }
bool BackupInProgress(void)
Definition: xlog.c:11350
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 616 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

617 {
619 }
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 583 of file xlogfuncs.c.

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

584 {
585  if (!RecoveryInProgress())
586  ereport(ERROR,
587  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
588  errmsg("recovery is not in progress"),
589  errhint("Recovery control functions can only be executed during recovery.")));
590 
592 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
bool RecoveryIsPaused(void)
Definition: xlog.c:5935
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 419 of file xlogfuncs.c.

References GetWalRcvWriteRecPtr(), NULL, PG_RETURN_LSN, and PG_RETURN_NULL.

420 {
421  XLogRecPtr recptr;
422 
423  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
424 
425  if (recptr == 0)
426  PG_RETURN_NULL();
427 
428  PG_RETURN_LSN(recptr);
429 }
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum pg_last_wal_replay_lsn ( PG_FUNCTION_ARGS  )

Definition at line 438 of file xlogfuncs.c.

References GetXLogReplayRecPtr(), NULL, PG_RETURN_LSN, and PG_RETURN_NULL.

439 {
440  XLogRecPtr recptr;
441 
442  recptr = GetXLogReplayRecPtr(NULL);
443 
444  if (recptr == 0)
445  PG_RETURN_NULL();
446 
447  PG_RETURN_LSN(recptr);
448 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11094
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 601 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

602 {
603  TimestampTz xtime;
604 
605  xtime = GetLatestXTime();
606  if (xtime == 0)
607  PG_RETURN_NULL();
608 
609  PG_RETURN_TIMESTAMPTZ(xtime);
610 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6066
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum pg_start_backup ( PG_FUNCTION_ARGS  )

Definition at line 71 of file xlogfuncs.c.

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

72 {
73  text *backupid = PG_GETARG_TEXT_PP(0);
74  bool fast = PG_GETARG_BOOL(1);
75  bool exclusive = PG_GETARG_BOOL(2);
76  char *backupidstr;
77  XLogRecPtr startpoint;
78  DIR *dir;
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  /* Make sure we can open the directory with tablespaces in it */
89  dir = AllocateDir("pg_tblspc");
90  if (!dir)
91  ereport(ERROR,
92  (errmsg("could not open directory \"%s\": %m", "pg_tblspc")));
93 
94  if (exclusive)
95  {
96  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
97  dir, NULL, NULL, false, true);
98  }
99  else
100  {
101  MemoryContext oldcontext;
102 
103  /*
104  * Label file and tablespace map file need to be long-lived, since
105  * they are read in pg_stop_backup.
106  */
110  MemoryContextSwitchTo(oldcontext);
111 
112  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
113  dir, NULL, tblspc_map_file, false, true);
114 
116  }
117 
118  FreeDir(dir);
119 
120  PG_RETURN_LSN(startpoint);
121 }
static StringInfo label_file
Definition: xlogfuncs.c:43
SessionBackupState get_backup_status(void)
Definition: xlog.c:10672
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:239
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:51
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
SessionBackupState
Definition: xlog.h:303
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2335
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
uintptr_t Datum
Definition: postgres.h:372
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:44
#define NULL
Definition: c.h:229
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:439
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
int FreeDir(DIR *dir)
Definition: fd.c:2444
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, DIR *tblspcdir, List **tablespaces, StringInfo tblspcmapfile, bool infotbssize, bool needtblspcmapfile)
Definition: xlog.c:10183
Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

Definition at line 144 of file xlogfuncs.c.

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

145 {
146  XLogRecPtr stoppoint;
148 
149  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
150  ereport(ERROR,
151  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
152  errmsg("non-exclusive backup in progress"),
153  errhint("Did you mean to use pg_stop_backup('f')?")));
154 
155  /*
156  * Exclusive backups were typically started in a different connection, so
157  * don't try to verify that status of backup is set to
158  * SESSION_BACKUP_EXCLUSIVE in this function. Actual verification that an
159  * exclusive backup is in fact running is handled inside
160  * do_pg_stop_backup.
161  */
162  stoppoint = do_pg_stop_backup(NULL, true, NULL);
163 
164  PG_RETURN_LSN(stoppoint);
165 }
int errhint(const char *fmt,...)
Definition: elog.c:987
SessionBackupState get_backup_status(void)
Definition: xlog.c:10672
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:10691
SessionBackupState
Definition: xlog.h:303
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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:224
Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

Definition at line 186 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(), NULL, 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.

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

Definition at line 289 of file xlogfuncs.c.

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

290 {
291  XLogRecPtr switchpoint;
292 
293  if (RecoveryInProgress())
294  ereport(ERROR,
295  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
296  errmsg("recovery is in progress"),
297  errhint("WAL control functions cannot be executed during recovery.")));
298 
299  switchpoint = RequestXLogSwitch(false);
300 
301  /*
302  * As a convenience, return the WAL location of the switch record
303  */
304  PG_RETURN_LSN(switchpoint);
305 }
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9411
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#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
Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 625 of file xlogfuncs.c.

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

626 {
627  Datum result;
628 
630  PG_GETARG_DATUM(0),
631  PG_GETARG_DATUM(1));
632 
633  PG_RETURN_NUMERIC(result);
634 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:188
return result
Definition: formatting.c:1633
uintptr_t Datum
Definition: postgres.h:372
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 546 of file xlogfuncs.c.

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

547 {
548  if (!RecoveryInProgress())
549  ereport(ERROR,
550  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
551  errmsg("recovery is not in progress"),
552  errhint("Recovery control functions can only be executed during recovery.")));
553 
554  SetRecoveryPause(true);
555 
556  PG_RETURN_VOID();
557 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_VOID()
Definition: fmgr.h:309
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5947
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 566 of file xlogfuncs.c.

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

567 {
568  if (!RecoveryInProgress())
569  ereport(ERROR,
570  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
571  errmsg("recovery is not in progress"),
572  errhint("Recovery control functions can only be executed during recovery.")));
573 
574  SetRecoveryPause(false);
575 
576  PG_RETURN_VOID();
577 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_VOID()
Definition: fmgr.h:309
void SetRecoveryPause(bool recoveryPause)
Definition: xlog.c:5947
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 521 of file xlogfuncs.c.

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

522 {
523  XLogSegNo xlogsegno;
524  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
525  char xlogfilename[MAXFNAMELEN];
526 
527  if (RecoveryInProgress())
528  ereport(ERROR,
529  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
530  errmsg("recovery is in progress"),
531  errhint("pg_walfile_name() cannot be executed during recovery.")));
532 
533  XLByteToPrevSeg(locationpoint, xlogsegno);
534  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno);
535 
536  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
537 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define XLogFileName(fname, tli, logSegNo)
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define ERROR
Definition: elog.h:43
uint64 XLogSegNo
Definition: xlogdefs.h:34
#define XLByteToPrevSeg(xlrp, logSegNo)
#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:179
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
text * cstring_to_text(const char *s)
Definition: varlena.c:149
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 459 of file xlogfuncs.c.

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

460 {
461  XLogSegNo xlogsegno;
462  uint32 xrecoff;
463  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
464  char xlogfilename[MAXFNAMELEN];
465  Datum values[2];
466  bool isnull[2];
467  TupleDesc resultTupleDesc;
468  HeapTuple resultHeapTuple;
469  Datum result;
470 
471  if (RecoveryInProgress())
472  ereport(ERROR,
473  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
474  errmsg("recovery is in progress"),
475  errhint("pg_walfile_name_offset() cannot be executed during recovery.")));
476 
477  /*
478  * Construct a tuple descriptor for the result row. This must match this
479  * function's pg_proc entry!
480  */
481  resultTupleDesc = CreateTemplateTupleDesc(2, false);
482  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
483  TEXTOID, -1, 0);
484  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
485  INT4OID, -1, 0);
486 
487  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
488 
489  /*
490  * xlogfilename
491  */
492  XLByteToPrevSeg(locationpoint, xlogsegno);
493  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno);
494 
495  values[0] = CStringGetTextDatum(xlogfilename);
496  isnull[0] = false;
497 
498  /*
499  * offset
500  */
501  xrecoff = locationpoint % XLogSegSize;
502 
503  values[1] = UInt32GetDatum(xrecoff);
504  isnull[1] = false;
505 
506  /*
507  * Tuple jam: Having first prepared your Datums, then squash together
508  */
509  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
510 
511  result = HeapTupleGetDatum(resultHeapTuple);
512 
513  PG_RETURN_DATUM(result);
514 }
#define XLogSegSize
Definition: xlog_internal.h:92
int errhint(const char *fmt,...)
Definition: elog.c:987
#define TEXTOID
Definition: pg_type.h:324
#define XLogFileName(fname, tli, logSegNo)
#define INT4OID
Definition: pg_type.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
bool RecoveryInProgress(void)
Definition: xlog.c:7878
#define ERROR
Definition: elog.h:43
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:268
#define UInt32GetDatum(X)
Definition: postgres.h:499
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:497
#define XLByteToPrevSeg(xlrp, logSegNo)
#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:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
TimeLineID ThisTimeLineID
Definition: xlog.c:179
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
int16 AttrNumber
Definition: attnum.h:21

Variable Documentation

StringInfo label_file
static

Definition at line 43 of file xlogfuncs.c.

StringInfo tblspc_map_file
static

Definition at line 44 of file xlogfuncs.c.

Referenced by perform_base_backup().