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_location (PG_FUNCTION_ARGS)
 
Datum pg_current_wal_insert_location (PG_FUNCTION_ARGS)
 
Datum pg_current_wal_flush_location (PG_FUNCTION_ARGS)
 
Datum pg_last_wal_receive_location (PG_FUNCTION_ARGS)
 
Datum pg_last_wal_replay_location (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_location_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
 
static bool exclusive_backup_running = false
 
static bool nonexclusive_backup_running = false
 

Function Documentation

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: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:10964
Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

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

649 {
650  Datum xtime;
651  FILE *lfp;
652  char fline[MAXPGPATH];
653  char backup_start_time[30];
654 
655  /*
656  * See if label file is present
657  */
658  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
659  if (lfp == NULL)
660  {
661  if (errno != ENOENT)
662  ereport(ERROR,
664  errmsg("could not read file \"%s\": %m",
666  PG_RETURN_NULL();
667  }
668 
669  /*
670  * Parse the file to find the START TIME line.
671  */
672  backup_start_time[0] = '\0';
673  while (fgets(fline, sizeof(fline), lfp) != NULL)
674  {
675  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
676  break;
677  }
678 
679  /* Check for a read error. */
680  if (ferror(lfp))
681  ereport(ERROR,
683  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
684 
685  /* Close the backup label file. */
686  if (FreeFile(lfp))
687  ereport(ERROR,
689  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
690 
691  if (strlen(backup_start_time) == 0)
692  ereport(ERROR,
693  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
694  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
695 
696  /*
697  * Convert the time string read from file to TimestampTz form.
698  */
700  CStringGetDatum(backup_start_time),
702  Int32GetDatum(-1));
703 
704  PG_RETURN_DATUM(xtime);
705 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
#define CStringGetDatum(X)
Definition: postgres.h:586
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2043
#define ereport(elevel, rest)
Definition: elog.h:122
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:559
uintptr_t Datum
Definition: postgres.h:374
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
int FreeFile(FILE *file)
Definition: fd.c:2226
#define Int32GetDatum(X)
Definition: postgres.h:487
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:382
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BACKUP_LABEL_FILE
Definition: xlog.h:301
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 310 of file xlogfuncs.c.

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

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

Definition at line 393 of file xlogfuncs.c.

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

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

Definition at line 372 of file xlogfuncs.c.

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

373 {
374  XLogRecPtr current_recptr;
375 
376  if (RecoveryInProgress())
377  ereport(ERROR,
378  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
379  errmsg("recovery is in progress"),
380  errhint("WAL control functions cannot be executed during recovery.")));
381 
382  current_recptr = GetXLogInsertRecPtr();
383 
384  PG_RETURN_LSN(current_recptr);
385 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7805
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11003
#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_location ( PG_FUNCTION_ARGS  )

Definition at line 351 of file xlogfuncs.c.

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

352 {
353  XLogRecPtr current_recptr;
354 
355  if (RecoveryInProgress())
356  ereport(ERROR,
357  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
358  errmsg("recovery is in progress"),
359  errhint("WAL control functions cannot be executed during recovery.")));
360 
361  current_recptr = GetXLogWriteRecPtr();
362 
363  PG_RETURN_LSN(current_recptr);
364 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7805
#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:11019
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 636 of file xlogfuncs.c.

References BackupInProgress(), and PG_RETURN_BOOL.

637 {
639 }
bool BackupInProgress(void)
Definition: xlog.c:11240
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 612 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

613 {
615 }
bool RecoveryInProgress(void)
Definition: xlog.c:7805
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 579 of file xlogfuncs.c.

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

580 {
581  if (!RecoveryInProgress())
582  ereport(ERROR,
583  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
584  errmsg("recovery is not in progress"),
585  errhint("Recovery control functions can only be executed during recovery.")));
586 
588 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7805
bool RecoveryIsPaused(void)
Definition: xlog.c:5873
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_last_wal_receive_location ( PG_FUNCTION_ARGS  )

Definition at line 415 of file xlogfuncs.c.

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

416 {
417  XLogRecPtr recptr;
418 
419  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
420 
421  if (recptr == 0)
422  PG_RETURN_NULL();
423 
424  PG_RETURN_LSN(recptr);
425 }
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum pg_last_wal_replay_location ( PG_FUNCTION_ARGS  )

Definition at line 434 of file xlogfuncs.c.

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

435 {
436  XLogRecPtr recptr;
437 
438  recptr = GetXLogReplayRecPtr(NULL);
439 
440  if (recptr == 0)
441  PG_RETURN_NULL();
442 
443  PG_RETURN_LSN(recptr);
444 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10984
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 597 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

598 {
599  TimestampTz xtime;
600 
601  xtime = GetLatestXTime();
602  if (xtime == 0)
603  PG_RETURN_NULL();
604 
605  PG_RETURN_TIMESTAMPTZ(xtime);
606 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6004
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum pg_start_backup ( PG_FUNCTION_ARGS  )

Definition at line 73 of file xlogfuncs.c.

References AllocateDir(), before_shmem_exit(), do_pg_start_backup(), ereport, errcode(), errmsg(), ERROR, exclusive_backup_running, FreeDir(), makeStringInfo(), MemoryContextSwitchTo(), nonexclusive_backup_running, nonexclusive_base_backup_cleanup(), NULL, PG_GETARG_BOOL, PG_GETARG_TEXT_P, PG_RETURN_LSN, text_to_cstring(), and TopMemoryContext.

74 {
75  text *backupid = PG_GETARG_TEXT_P(0);
76  bool fast = PG_GETARG_BOOL(1);
77  bool exclusive = PG_GETARG_BOOL(2);
78  char *backupidstr;
79  XLogRecPtr startpoint;
80  DIR *dir;
81 
82  backupidstr = text_to_cstring(backupid);
83 
85  ereport(ERROR,
86  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
87  errmsg("a backup is already in progress in this session")));
88 
89  /* Make sure we can open the directory with tablespaces in it */
90  dir = AllocateDir("pg_tblspc");
91  if (!dir)
92  ereport(ERROR,
93  (errmsg("could not open directory \"%s\": %m", "pg_tblspc")));
94 
95  if (exclusive)
96  {
97  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
98  dir, NULL, NULL, false, true);
100  }
101  else
102  {
103  MemoryContext oldcontext;
104 
105  /*
106  * Label file and tablespace map file need to be long-lived, since
107  * they are read in pg_stop_backup.
108  */
112  MemoryContextSwitchTo(oldcontext);
113 
114  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
115  dir, NULL, tblspc_map_file, false, true);
117 
119  }
120 
121  FreeDir(dir);
122 
123  PG_RETURN_LSN(startpoint);
124 }
static bool exclusive_backup_running
Definition: xlogfuncs.c:45
static StringInfo label_file
Definition: xlogfuncs.c:43
static bool nonexclusive_backup_running
Definition: xlogfuncs.c:46
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
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:230
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:53
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2284
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
uintptr_t Datum
Definition: postgres.h:374
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:44
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
char * text_to_cstring(const text *t)
Definition: varlena.c:184
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:435
int FreeDir(DIR *dir)
Definition: fd.c:2393
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:10087
Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

Definition at line 147 of file xlogfuncs.c.

References do_pg_stop_backup(), ereport, errcode(), errhint(), errmsg(), ERROR, exclusive_backup_running, nonexclusive_backup_running, NULL, and PG_RETURN_LSN.

148 {
149  XLogRecPtr stoppoint;
150 
152  ereport(ERROR,
153  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
154  errmsg("non-exclusive backup in progress"),
155  errhint("Did you mean to use pg_stop_backup('f')?")));
156 
157  /*
158  * Exclusive backups were typically started in a different connection, so
159  * don't try to verify that exclusive_backup_running is set in this one.
160  * Actual verification that an exclusive backup is in fact running is
161  * handled inside do_pg_stop_backup.
162  */
163  stoppoint = do_pg_stop_backup(NULL, true, NULL);
164 
165  exclusive_backup_running = false;
166 
167  PG_RETURN_LSN(stoppoint);
168 }
static bool exclusive_backup_running
Definition: xlogfuncs.c:45
int errhint(const char *fmt,...)
Definition: elog.c:987
static bool nonexclusive_backup_running
Definition: xlogfuncs.c:46
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:10581
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

Definition at line 182 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, exclusive_backup_running, get_call_result_type(), IsA, LSNGetDatum, MemoryContextSwitchTo(), MemSet, nonexclusive_backup_running, nonexclusive_base_backup_cleanup(), NULL, pfree(), PG_GETARG_BOOL, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

183 {
184  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
185  TupleDesc tupdesc;
186  Tuplestorestate *tupstore;
187  MemoryContext per_query_ctx;
188  MemoryContext oldcontext;
189  Datum values[3];
190  bool nulls[3];
191 
192  bool exclusive = PG_GETARG_BOOL(0);
193  XLogRecPtr stoppoint;
194 
195  /* check to see if caller supports us returning a tuplestore */
196  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
197  ereport(ERROR,
198  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
199  errmsg("set-valued function called in context that cannot accept a set")));
200  if (!(rsinfo->allowedModes & SFRM_Materialize))
201  ereport(ERROR,
202  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
203  errmsg("materialize mode required, but it is not " \
204  "allowed in this context")));
205 
206  /* Build a tuple descriptor for our result type */
207  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
208  elog(ERROR, "return type must be a row type");
209 
210  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
211  oldcontext = MemoryContextSwitchTo(per_query_ctx);
212 
213  tupstore = tuplestore_begin_heap(true, false, work_mem);
214  rsinfo->returnMode = SFRM_Materialize;
215  rsinfo->setResult = tupstore;
216  rsinfo->setDesc = tupdesc;
217 
218  MemoryContextSwitchTo(oldcontext);
219 
220  MemSet(values, 0, sizeof(values));
221  MemSet(nulls, 0, sizeof(nulls));
222 
223  if (exclusive)
224  {
226  ereport(ERROR,
227  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
228  errmsg("non-exclusive backup in progress"),
229  errhint("Did you mean to use pg_stop_backup('f')?")));
230 
231  /*
232  * Stop the exclusive backup, and since we're in an exclusive backup
233  * return NULL for both backup_label and tablespace_map.
234  */
235  stoppoint = do_pg_stop_backup(NULL, true, NULL);
236  exclusive_backup_running = false;
237 
238  nulls[1] = true;
239  nulls[2] = true;
240  }
241  else
242  {
244  ereport(ERROR,
245  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
246  errmsg("non-exclusive backup is not in progress"),
247  errhint("Did you mean to use pg_stop_backup('t')?")));
248 
249  /*
250  * Stop the non-exclusive backup. Return a copy of the backup label
251  * and tablespace map so they can be written to disk by the caller.
252  */
253  stoppoint = do_pg_stop_backup(label_file->data, true, NULL);
256 
259 
260  /* Free structures allocated in TopMemoryContext */
262  pfree(label_file);
263  label_file = NULL;
267  }
268 
269  /* Stoppoint is included on both exclusive and nonexclusive backups */
270  values[0] = LSNGetDatum(stoppoint);
271 
272  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
273  tuplestore_donestoring(typstore);
274 
275  return (Datum) 0;
276 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:735
static bool exclusive_backup_running
Definition: xlogfuncs.c:45
#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
static bool nonexclusive_backup_running
Definition: xlogfuncs.c:46
#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:853
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
static void nonexclusive_base_backup_cleanup(int code, Datum arg)
Definition: xlogfuncs.c:53
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:10581
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:316
uintptr_t Datum
Definition: postgres.h:374
int work_mem
Definition: globals.c:112
int allowedModes
Definition: execnodes.h:199
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:44
SetFunctionReturnMode returnMode
Definition: execnodes.h:201
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:133
Tuplestorestate * setResult
Definition: execnodes.h:204
static Datum values[MAXATTR]
Definition: bootstrap.c:162
ExprContext * econtext
Definition: execnodes.h:197
TupleDesc setDesc
Definition: execnodes.h:205
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define elog
Definition: elog.h:219
Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 285 of file xlogfuncs.c.

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

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

Definition at line 621 of file xlogfuncs.c.

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

622 {
623  Datum result;
624 
626  PG_GETARG_DATUM(0),
627  PG_GETARG_DATUM(1));
628 
629  PG_RETURN_NUMERIC(result);
630 }
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:54
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:188
uintptr_t Datum
Definition: postgres.h:374
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 542 of file xlogfuncs.c.

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

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

Definition at line 562 of file xlogfuncs.c.

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

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

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

518 {
519  XLogSegNo xlogsegno;
520  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
521  char xlogfilename[MAXFNAMELEN];
522 
523  if (RecoveryInProgress())
524  ereport(ERROR,
525  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
526  errmsg("recovery is in progress"),
527  errhint("pg_walfile_name() cannot be executed during recovery.")));
528 
529  XLByteToPrevSeg(locationpoint, xlogsegno);
530  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno);
531 
532  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
533 }
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:7805
#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:178
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:151
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 455 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(), TEXTOID, ThisTimeLineID, TupleDescInitEntry(), UInt32GetDatum, values, XLByteToPrevSeg, XLogFileName, and XLogSegSize.

456 {
457  XLogSegNo xlogsegno;
458  uint32 xrecoff;
459  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
460  char xlogfilename[MAXFNAMELEN];
461  Datum values[2];
462  bool isnull[2];
463  TupleDesc resultTupleDesc;
464  HeapTuple resultHeapTuple;
465  Datum result;
466 
467  if (RecoveryInProgress())
468  ereport(ERROR,
469  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
470  errmsg("recovery is in progress"),
471  errhint("pg_walfile_name_offset() cannot be executed during recovery.")));
472 
473  /*
474  * Construct a tuple descriptor for the result row. This must match this
475  * function's pg_proc entry!
476  */
477  resultTupleDesc = CreateTemplateTupleDesc(2, false);
478  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
479  TEXTOID, -1, 0);
480  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
481  INT4OID, -1, 0);
482 
483  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
484 
485  /*
486  * xlogfilename
487  */
488  XLByteToPrevSeg(locationpoint, xlogsegno);
489  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno);
490 
491  values[0] = CStringGetTextDatum(xlogfilename);
492  isnull[0] = false;
493 
494  /*
495  * offset
496  */
497  xrecoff = locationpoint % XLogSegSize;
498 
499  values[1] = UInt32GetDatum(xrecoff);
500  isnull[1] = false;
501 
502  /*
503  * Tuple jam: Having first prepared your Datums, then squash together
504  */
505  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
506 
507  result = HeapTupleGetDatum(resultHeapTuple);
508 
509  PG_RETURN_DATUM(result);
510 }
#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
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
bool RecoveryInProgress(void)
Definition: xlog.c:7805
#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:265
#define UInt32GetDatum(X)
Definition: postgres.h:501
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:493
#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:374
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
TimeLineID ThisTimeLineID
Definition: xlog.c:178
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:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
int16 AttrNumber
Definition: attnum.h:21

Variable Documentation

bool exclusive_backup_running = false
static

Definition at line 45 of file xlogfuncs.c.

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

StringInfo label_file
static

Definition at line 43 of file xlogfuncs.c.

bool nonexclusive_backup_running = false
static

Definition at line 46 of file xlogfuncs.c.

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

StringInfo tblspc_map_file
static

Definition at line 44 of file xlogfuncs.c.

Referenced by perform_base_backup().