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
 

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

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

652 {
653  Datum xtime;
654  FILE *lfp;
655  char fline[MAXPGPATH];
656  char backup_start_time[30];
657 
658  /*
659  * See if label file is present
660  */
661  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
662  if (lfp == NULL)
663  {
664  if (errno != ENOENT)
665  ereport(ERROR,
667  errmsg("could not read file \"%s\": %m",
669  PG_RETURN_NULL();
670  }
671 
672  /*
673  * Parse the file to find the START TIME line.
674  */
675  backup_start_time[0] = '\0';
676  while (fgets(fline, sizeof(fline), lfp) != NULL)
677  {
678  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
679  break;
680  }
681 
682  /* Check for a read error. */
683  if (ferror(lfp))
684  ereport(ERROR,
686  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
687 
688  /* Close the backup label file. */
689  if (FreeFile(lfp))
690  ereport(ERROR,
692  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
693 
694  if (strlen(backup_start_time) == 0)
695  ereport(ERROR,
696  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
697  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
698 
699  /*
700  * Convert the time string read from file to TimestampTz form.
701  */
703  CStringGetDatum(backup_start_time),
705  Int32GetDatum(-1));
706 
707  PG_RETURN_DATUM(xtime);
708 }
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:321
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

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

314 {
315  text *restore_name = PG_GETARG_TEXT_PP(0);
316  char *restore_name_str;
317  XLogRecPtr restorepoint;
318 
319  if (RecoveryInProgress())
320  ereport(ERROR,
321  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
322  (errmsg("recovery is in progress"),
323  errhint("WAL control functions cannot be executed during recovery."))));
324 
325  if (!XLogIsNeeded())
326  ereport(ERROR,
327  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
328  errmsg("WAL level not sufficient for creating a restore point"),
329  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
330 
331  restore_name_str = text_to_cstring(restore_name);
332 
333  if (strlen(restore_name_str) >= MAXFNAMELEN)
334  ereport(ERROR,
335  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
336  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
337 
338  restorepoint = XLogRestorePoint(restore_name_str);
339 
340  /*
341  * As a convenience, return the WAL location of the restore point record
342  */
343  PG_RETURN_LSN(restorepoint);
344 }
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:7874
#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:9414
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_location ( PG_FUNCTION_ARGS  )

Definition at line 396 of file xlogfuncs.c.

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

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

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

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

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

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

References BackupInProgress(), and PG_RETURN_BOOL.

640 {
642 }
bool BackupInProgress(void)
Definition: xlog.c:11333
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 615 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

616 {
618 }
bool RecoveryInProgress(void)
Definition: xlog.c:7874
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 582 of file xlogfuncs.c.

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

583 {
584  if (!RecoveryInProgress())
585  ereport(ERROR,
586  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
587  errmsg("recovery is not in progress"),
588  errhint("Recovery control functions can only be executed during recovery.")));
589 
591 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7874
bool RecoveryIsPaused(void)
Definition: xlog.c:5931
#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_location ( PG_FUNCTION_ARGS  )

Definition at line 418 of file xlogfuncs.c.

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

419 {
420  XLogRecPtr recptr;
421 
422  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
423 
424  if (recptr == 0)
425  PG_RETURN_NULL();
426 
427  PG_RETURN_LSN(recptr);
428 }
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_location ( PG_FUNCTION_ARGS  )

Definition at line 437 of file xlogfuncs.c.

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

438 {
439  XLogRecPtr recptr;
440 
441  recptr = GetXLogReplayRecPtr(NULL);
442 
443  if (recptr == 0)
444  PG_RETURN_NULL();
445 
446  PG_RETURN_LSN(recptr);
447 }
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:11077
#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 600 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

601 {
602  TimestampTz xtime;
603 
604  xtime = GetLatestXTime();
605  if (xtime == 0)
606  PG_RETURN_NULL();
607 
608  PG_RETURN_TIMESTAMPTZ(xtime);
609 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetLatestXTime(void)
Definition: xlog.c:6062
#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:10655
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: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:304
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:10167
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 do_pg_stop_backup.
160  */
161  stoppoint = do_pg_stop_backup(NULL, true, NULL);
162 
163  PG_RETURN_LSN(stoppoint);
164 }
int errhint(const char *fmt,...)
Definition: elog.c:987
SessionBackupState get_backup_status(void)
Definition: xlog.c:10655
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:10674
SessionBackupState
Definition: xlog.h:304
#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 185 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.

186 {
187  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
188  TupleDesc tupdesc;
189  Tuplestorestate *tupstore;
190  MemoryContext per_query_ctx;
191  MemoryContext oldcontext;
192  Datum values[3];
193  bool nulls[3];
194 
195  bool exclusive = PG_GETARG_BOOL(0);
196  bool waitforarchive = PG_GETARG_BOOL(1);
197  XLogRecPtr stoppoint;
199 
200  /* check to see if caller supports us returning a tuplestore */
201  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
202  ereport(ERROR,
203  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
204  errmsg("set-valued function called in context that cannot accept a set")));
205  if (!(rsinfo->allowedModes & SFRM_Materialize))
206  ereport(ERROR,
207  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
208  errmsg("materialize mode required, but it is not " \
209  "allowed in this context")));
210 
211  /* Build a tuple descriptor for our result type */
212  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
213  elog(ERROR, "return type must be a row type");
214 
215  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
216  oldcontext = MemoryContextSwitchTo(per_query_ctx);
217 
218  tupstore = tuplestore_begin_heap(true, false, work_mem);
219  rsinfo->returnMode = SFRM_Materialize;
220  rsinfo->setResult = tupstore;
221  rsinfo->setDesc = tupdesc;
222 
223  MemoryContextSwitchTo(oldcontext);
224 
225  MemSet(values, 0, sizeof(values));
226  MemSet(nulls, 0, sizeof(nulls));
227 
228  if (exclusive)
229  {
230  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
231  ereport(ERROR,
232  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
233  errmsg("non-exclusive backup in progress"),
234  errhint("Did you mean to use pg_stop_backup('f')?")));
235 
236  /*
237  * Stop the exclusive backup, and since we're in an exclusive backup
238  * return NULL for both backup_label and tablespace_map.
239  */
240  stoppoint = do_pg_stop_backup(NULL, waitforarchive, NULL);
241 
242  nulls[1] = true;
243  nulls[2] = true;
244  }
245  else
246  {
247  if (status != SESSION_BACKUP_NON_EXCLUSIVE)
248  ereport(ERROR,
249  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
250  errmsg("non-exclusive backup is not in progress"),
251  errhint("Did you mean to use pg_stop_backup('t')?")));
252 
253  /*
254  * Stop the non-exclusive backup. Return a copy of the backup label
255  * and tablespace map so they can be written to disk by the caller.
256  */
257  stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
259 
262 
263  /* Free structures allocated in TopMemoryContext */
265  pfree(label_file);
266  label_file = NULL;
270  }
271 
272  /* Stoppoint is included on both exclusive and nonexclusive backups */
273  values[0] = LSNGetDatum(stoppoint);
274 
275  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
276  tuplestore_donestoring(typstore);
277 
278  return (Datum) 0;
279 }
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:10655
#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:10674
SessionBackupState
Definition: xlog.h:304
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:112
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 288 of file xlogfuncs.c.

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

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

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

625 {
626  Datum result;
627 
629  PG_GETARG_DATUM(0),
630  PG_GETARG_DATUM(1));
631 
632  PG_RETURN_NUMERIC(result);
633 }
#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:1618
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 545 of file xlogfuncs.c.

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

546 {
547  if (!RecoveryInProgress())
548  ereport(ERROR,
549  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
550  errmsg("recovery is not in progress"),
551  errhint("Recovery control functions can only be executed during recovery.")));
552 
553  SetRecoveryPause(true);
554 
555  PG_RETURN_VOID();
556 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7874
#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:5943
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 565 of file xlogfuncs.c.

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

566 {
567  if (!RecoveryInProgress())
568  ereport(ERROR,
569  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
570  errmsg("recovery is not in progress"),
571  errhint("Recovery control functions can only be executed during recovery.")));
572 
573  SetRecoveryPause(false);
574 
575  PG_RETURN_VOID();
576 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7874
#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:5943
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

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

521 {
522  XLogSegNo xlogsegno;
523  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
524  char xlogfilename[MAXFNAMELEN];
525 
526  if (RecoveryInProgress())
527  ereport(ERROR,
528  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
529  errmsg("recovery is in progress"),
530  errhint("pg_walfile_name() cannot be executed during recovery.")));
531 
532  XLByteToPrevSeg(locationpoint, xlogsegno);
533  XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno);
534 
535  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
536 }
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:7874
#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 458 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.

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