PostgreSQL Source Code  git master
xlogfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "catalog/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

◆ nonexclusive_base_backup_cleanup()

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:11130

◆ pg_backup_start_time()

Datum pg_backup_start_time ( PG_FUNCTION_ARGS  )

Definition at line 643 of file xlogfuncs.c.

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

644 {
645  Datum xtime;
646  FILE *lfp;
647  char fline[MAXPGPATH];
648  char backup_start_time[30];
649 
650  /*
651  * See if label file is present
652  */
653  lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
654  if (lfp == NULL)
655  {
656  if (errno != ENOENT)
657  ereport(ERROR,
659  errmsg("could not read file \"%s\": %m",
661  PG_RETURN_NULL();
662  }
663 
664  /*
665  * Parse the file to find the START TIME line.
666  */
667  backup_start_time[0] = '\0';
668  while (fgets(fline, sizeof(fline), lfp) != NULL)
669  {
670  if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
671  break;
672  }
673 
674  /* Check for a read error. */
675  if (ferror(lfp))
676  ereport(ERROR,
678  errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
679 
680  /* Close the backup label file. */
681  if (FreeFile(lfp))
682  ereport(ERROR,
684  errmsg("could not close file \"%s\": %m", BACKUP_LABEL_FILE)));
685 
686  if (strlen(backup_start_time) == 0)
687  ereport(ERROR,
688  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
689  errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
690 
691  /*
692  * Convert the time string read from file to TimestampTz form.
693  */
695  CStringGetDatum(backup_start_time),
697  Int32GetDatum(-1));
698 
699  PG_RETURN_DATUM(xtime);
700 }
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:2343
#define ereport(elevel, rest)
Definition: elog.h:122
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:589
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define InvalidOid
Definition: postgres_ext.h:36
int FreeFile(FILE *file)
Definition: fd.c:2535
#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:322
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

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

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

◆ pg_current_wal_flush_lsn()

Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 388 of file xlogfuncs.c.

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

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

◆ pg_current_wal_insert_lsn()

Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 367 of file xlogfuncs.c.

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

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

◆ pg_current_wal_lsn()

Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 346 of file xlogfuncs.c.

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

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

◆ pg_is_in_backup()

Datum pg_is_in_backup ( PG_FUNCTION_ARGS  )

Definition at line 631 of file xlogfuncs.c.

References BackupInProgress(), and PG_RETURN_BOOL.

632 {
634 }
bool BackupInProgress(void)
Definition: xlog.c:11453
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 607 of file xlogfuncs.c.

References PG_RETURN_BOOL, and RecoveryInProgress().

608 {
610 }
bool RecoveryInProgress(void)
Definition: xlog.c:7922
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319

◆ pg_is_wal_replay_paused()

Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 574 of file xlogfuncs.c.

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

575 {
576  if (!RecoveryInProgress())
577  ereport(ERROR,
578  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
579  errmsg("recovery is not in progress"),
580  errhint("Recovery control functions can only be executed during recovery.")));
581 
583 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7922
bool RecoveryIsPaused(void)
Definition: xlog.c:5998
#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

◆ pg_last_wal_receive_lsn()

Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 410 of file xlogfuncs.c.

References GetWalRcvWriteRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

411 {
412  XLogRecPtr recptr;
413 
414  recptr = GetWalRcvWriteRecPtr(NULL, NULL);
415 
416  if (recptr == 0)
417  PG_RETURN_NULL();
418 
419  PG_RETURN_LSN(recptr);
420 }
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ pg_last_wal_replay_lsn()

Datum pg_last_wal_replay_lsn ( PG_FUNCTION_ARGS  )

Definition at line 429 of file xlogfuncs.c.

References GetXLogReplayRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

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

◆ pg_last_xact_replay_timestamp()

Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 592 of file xlogfuncs.c.

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

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

◆ pg_start_backup()

Datum pg_start_backup ( PG_FUNCTION_ARGS  )

Definition at line 71 of file xlogfuncs.c.

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

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;
79 
80  backupidstr = text_to_cstring(backupid);
81 
82  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
83  ereport(ERROR,
84  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
85  errmsg("a backup is already in progress in this session")));
86 
87  if (exclusive)
88  {
89  startpoint = do_pg_start_backup(backupidstr, fast, NULL, NULL,
90  NULL, NULL, false, true);
91  }
92  else
93  {
94  MemoryContext oldcontext;
95 
96  /*
97  * Label file and tablespace map file need to be long-lived, since
98  * they are read in pg_stop_backup.
99  */
103  MemoryContextSwitchTo(oldcontext);
104 
105  startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
106  NULL, tblspc_map_file, false, true);
107 
109  }
110 
111  PG_RETURN_LSN(startpoint);
112 }
static StringInfo label_file
Definition: xlogfuncs.c:43
SessionBackupState get_backup_status(void)
Definition: xlog.c:10707
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
XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, StringInfo labelfile, List **tablespaces, StringInfo tblspcmapfile, bool infotbssize, bool needtblspcmapfile)
Definition: xlog.c:10208
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
#define ERROR
Definition: elog.h:43
SessionBackupState
Definition: xlog.h:305
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
#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
uint64 XLogRecPtr
Definition: xlogdefs.h:21
char * text_to_cstring(const text *t)
Definition: varlena.c:183
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:497
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ pg_stop_backup()

Datum pg_stop_backup ( PG_FUNCTION_ARGS  )

Definition at line 135 of file xlogfuncs.c.

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

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

◆ pg_stop_backup_v2()

Datum pg_stop_backup_v2 ( PG_FUNCTION_ARGS  )

Definition at line 177 of file xlogfuncs.c.

References ReturnSetInfo::allowedModes, cancel_before_shmem_exit(), CStringGetTextDatum, StringInfoData::data, do_pg_stop_backup(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errhint(), errmsg(), ERROR, get_backup_status(), get_call_result_type(), IsA, LSNGetDatum, MemoryContextSwitchTo(), MemSet, nonexclusive_base_backup_cleanup(), pfree(), PG_GETARG_BOOL, ReturnSetInfo::returnMode, SESSION_BACKUP_NON_EXCLUSIVE, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, status(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

178 {
179  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
180  TupleDesc tupdesc;
181  Tuplestorestate *tupstore;
182  MemoryContext per_query_ctx;
183  MemoryContext oldcontext;
184  Datum values[3];
185  bool nulls[3];
186 
187  bool exclusive = PG_GETARG_BOOL(0);
188  bool waitforarchive = PG_GETARG_BOOL(1);
189  XLogRecPtr stoppoint;
191 
192  /* check to see if caller supports us returning a tuplestore */
193  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
194  ereport(ERROR,
195  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
196  errmsg("set-valued function called in context that cannot accept a set")));
197  if (!(rsinfo->allowedModes & SFRM_Materialize))
198  ereport(ERROR,
199  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
200  errmsg("materialize mode required, but it is not " \
201  "allowed in this context")));
202 
203  /* Build a tuple descriptor for our result type */
204  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
205  elog(ERROR, "return type must be a row type");
206 
207  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
208  oldcontext = MemoryContextSwitchTo(per_query_ctx);
209 
210  tupstore = tuplestore_begin_heap(true, false, work_mem);
211  rsinfo->returnMode = SFRM_Materialize;
212  rsinfo->setResult = tupstore;
213  rsinfo->setDesc = tupdesc;
214 
215  MemoryContextSwitchTo(oldcontext);
216 
217  MemSet(values, 0, sizeof(values));
218  MemSet(nulls, 0, sizeof(nulls));
219 
220  if (exclusive)
221  {
222  if (status == SESSION_BACKUP_NON_EXCLUSIVE)
223  ereport(ERROR,
224  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
225  errmsg("non-exclusive backup in progress"),
226  errhint("Did you mean to use pg_stop_backup('f')?")));
227 
228  /*
229  * Stop the exclusive backup, and since we're in an exclusive backup
230  * return NULL for both backup_label and tablespace_map.
231  */
232  stoppoint = do_pg_stop_backup(NULL, waitforarchive, NULL);
233 
234  nulls[1] = true;
235  nulls[2] = true;
236  }
237  else
238  {
239  if (status != SESSION_BACKUP_NON_EXCLUSIVE)
240  ereport(ERROR,
241  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
242  errmsg("non-exclusive backup is not in progress"),
243  errhint("Did you mean to use pg_stop_backup('t')?")));
244 
245  /*
246  * Stop the non-exclusive backup. Return a copy of the backup label
247  * and tablespace map so they can be written to disk by the caller.
248  */
249  stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
251 
254 
255  /* Free structures allocated in TopMemoryContext */
257  pfree(label_file);
258  label_file = NULL;
261  tblspc_map_file = NULL;
262  }
263 
264  /* Stoppoint is included on both exclusive and nonexclusive backups */
265  values[0] = LSNGetDatum(stoppoint);
266 
267  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
268  tuplestore_donestoring(typstore);
269 
270  return (Datum) 0;
271 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
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:10707
#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:877
#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:936
#define ERROR
Definition: elog.h:43
XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
Definition: xlog.c:10726
SessionBackupState
Definition: xlog.h:305
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:279
static StringInfo tblspc_map_file
Definition: xlogfuncs.c:44
SetFunctionReturnMode returnMode
Definition: execnodes.h:281
uint64 XLogRecPtr
Definition: xlogdefs.h:21
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:213
Tuplestorestate * setResult
Definition: execnodes.h:284
static Datum values[MAXATTR]
Definition: bootstrap.c:164
ExprContext * econtext
Definition: execnodes.h:277
TupleDesc setDesc
Definition: execnodes.h:285
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:225

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 280 of file xlogfuncs.c.

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

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

◆ pg_wal_lsn_diff()

Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 616 of file xlogfuncs.c.

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

617 {
618  Datum result;
619 
621  PG_GETARG_DATUM(0),
622  PG_GETARG_DATUM(1));
623 
624  PG_RETURN_NUMERIC(result);
625 }
#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:194
uintptr_t Datum
Definition: postgres.h:372
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 537 of file xlogfuncs.c.

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

538 {
539  if (!RecoveryInProgress())
540  ereport(ERROR,
541  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
542  errmsg("recovery is not in progress"),
543  errhint("Recovery control functions can only be executed during recovery.")));
544 
545  SetRecoveryPause(true);
546 
547  PG_RETURN_VOID();
548 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7922
#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:6010
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_wal_replay_resume()

Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 557 of file xlogfuncs.c.

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

558 {
559  if (!RecoveryInProgress())
560  ereport(ERROR,
561  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
562  errmsg("recovery is not in progress"),
563  errhint("Recovery control functions can only be executed during recovery.")));
564 
565  SetRecoveryPause(false);
566 
567  PG_RETURN_VOID();
568 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7922
#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:6010
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 512 of file xlogfuncs.c.

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

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

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 450 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, wal_segment_size, XLByteToPrevSeg, XLogFileName, and XLogSegmentOffset.

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

Variable Documentation

◆ label_file

StringInfo label_file
static

Definition at line 43 of file xlogfuncs.c.

◆ tblspc_map_file

StringInfo tblspc_map_file
static

Definition at line 44 of file xlogfuncs.c.

Referenced by perform_base_backup().