PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
xlogfuncs.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/htup_details.h"
#include "access/xlog_internal.h"
#include "access/xlogbackup.h"
#include "access/xlogrecovery.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/walreceiver.h"
#include "storage/fd.h"
#include "storage/latch.h"
#include "storage/standby.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/timestamp.h"
Include dependency graph for xlogfuncs.c:

Go to the source code of this file.

Macros

#define PG_BACKUP_STOP_V2_COLS   3
 
#define PG_SPLIT_WALFILE_NAME_COLS   2
 
#define WAITS_PER_SECOND   10
 

Functions

Datum pg_backup_start (PG_FUNCTION_ARGS)
 
Datum pg_backup_stop (PG_FUNCTION_ARGS)
 
Datum pg_switch_wal (PG_FUNCTION_ARGS)
 
Datum pg_log_standby_snapshot (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_split_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_get_wal_replay_pause_state (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_promote (PG_FUNCTION_ARGS)
 

Variables

static BackupStatebackup_state = NULL
 
static StringInfo tablespace_map = NULL
 
static MemoryContext backupcontext = NULL
 

Macro Definition Documentation

◆ PG_BACKUP_STOP_V2_COLS

#define PG_BACKUP_STOP_V2_COLS   3

◆ PG_SPLIT_WALFILE_NAME_COLS

#define PG_SPLIT_WALFILE_NAME_COLS   2

◆ WAITS_PER_SECOND

#define WAITS_PER_SECOND   10

Function Documentation

◆ pg_backup_start()

Datum pg_backup_start ( PG_FUNCTION_ARGS  )

Definition at line 57 of file xlogfuncs.c.

58 {
59  text *backupid = PG_GETARG_TEXT_PP(0);
60  bool fast = PG_GETARG_BOOL(1);
61  char *backupidstr;
63  MemoryContext oldcontext;
64 
65  backupidstr = text_to_cstring(backupid);
66 
67  if (status == SESSION_BACKUP_RUNNING)
68  ereport(ERROR,
69  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
70  errmsg("a backup is already in progress in this session")));
71 
72  /*
73  * backup_state and tablespace_map need to be long-lived as they are used
74  * in pg_backup_stop(). These are allocated in a dedicated memory context
75  * child of TopMemoryContext, deleted at the end of pg_backup_stop(). If
76  * an error happens before ending the backup, memory would be leaked in
77  * this context until pg_backup_start() is called again.
78  */
79  if (backupcontext == NULL)
80  {
82  "on-line backup context",
84  }
85  else
86  {
87  backup_state = NULL;
88  tablespace_map = NULL;
90  }
91 
95  MemoryContextSwitchTo(oldcontext);
96 
98  do_pg_backup_start(backupidstr, fast, NULL, backup_state, tablespace_map);
99 
101 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc0(Size size)
Definition: mcxt.c:1347
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:177
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:34
MemoryContextSwitchTo(old_ctx)
StringInfo makeStringInfo(void)
Definition: stringinfo.c:38
XLogRecPtr startpoint
Definition: xlogbackup.h:26
Definition: c.h:666
char * text_to_cstring(const text *t)
Definition: varlena.c:217
SessionBackupState get_backup_status(void)
Definition: xlog.c:9101
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:9421
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8792
SessionBackupState
Definition: xlog.h:286
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:288
static BackupState * backup_state
Definition: xlogfuncs.c:41
static MemoryContext backupcontext
Definition: xlogfuncs.c:45
static StringInfo tablespace_map
Definition: xlogfuncs.c:42

References ALLOCSET_START_SMALL_SIZES, AllocSetContextCreate, backup_state, backupcontext, do_pg_backup_start(), ereport, errcode(), errmsg(), ERROR, get_backup_status(), makeStringInfo(), MemoryContextReset(), MemoryContextSwitchTo(), palloc0(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_LSN, register_persistent_abort_backup_handler(), SESSION_BACKUP_RUNNING, BackupState::startpoint, tablespace_map, text_to_cstring(), and TopMemoryContext.

◆ pg_backup_stop()

Datum pg_backup_stop ( PG_FUNCTION_ARGS  )

Definition at line 124 of file xlogfuncs.c.

125 {
126 #define PG_BACKUP_STOP_V2_COLS 3
127  TupleDesc tupdesc;
129  bool nulls[PG_BACKUP_STOP_V2_COLS] = {0};
130  bool waitforarchive = PG_GETARG_BOOL(0);
131  char *backup_label;
133 
134  /* Initialize attributes information in the tuple descriptor */
135  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
136  elog(ERROR, "return type must be a row type");
137 
138  if (status != SESSION_BACKUP_RUNNING)
139  ereport(ERROR,
140  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
141  errmsg("backup is not in progress"),
142  errhint("Did you call pg_backup_start()?")));
143 
144  Assert(backup_state != NULL);
145  Assert(tablespace_map != NULL);
146 
147  /* Stop the backup */
148  do_pg_backup_stop(backup_state, waitforarchive);
149 
150  /* Build the contents of backup_label */
151  backup_label = build_backup_content(backup_state, false);
152 
154  values[1] = CStringGetTextDatum(backup_label);
156 
157  /* Deallocate backup-related variables */
158  pfree(backup_label);
159 
160  /* Clean up the session-level state and its memory context */
161  backup_state = NULL;
162  tablespace_map = NULL;
164  backupcontext = NULL;
165 
166  /* Returns the record as Datum */
168 }
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define Assert(condition)
Definition: c.h:837
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define elog(elevel,...)
Definition: elog.h:225
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void pfree(void *pointer)
Definition: mcxt.c:1521
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
uintptr_t Datum
Definition: postgres.h:64
XLogRecPtr stoppoint
Definition: xlogbackup.h:35
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition: xlog.c:9120
char * build_backup_content(BackupState *state, bool ishistoryfile)
Definition: xlogbackup.c:29
#define PG_BACKUP_STOP_V2_COLS

References Assert, backup_state, backupcontext, build_backup_content(), CStringGetTextDatum, StringInfoData::data, do_pg_backup_stop(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, get_backup_status(), get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), LSNGetDatum(), MemoryContextDelete(), pfree(), PG_BACKUP_STOP_V2_COLS, PG_GETARG_BOOL, PG_RETURN_DATUM, SESSION_BACKUP_RUNNING, BackupState::stoppoint, tablespace_map, TYPEFUNC_COMPOSITE, and values.

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 233 of file xlogfuncs.c.

234 {
235  text *restore_name = PG_GETARG_TEXT_PP(0);
236  char *restore_name_str;
237  XLogRecPtr restorepoint;
238 
239  if (RecoveryInProgress())
240  ereport(ERROR,
241  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
242  errmsg("recovery is in progress"),
243  errhint("WAL control functions cannot be executed during recovery.")));
244 
245  if (!XLogIsNeeded())
246  ereport(ERROR,
247  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
248  errmsg("WAL level not sufficient for creating a restore point"),
249  errhint("\"wal_level\" must be set to \"replica\" or \"logical\" at server start.")));
250 
251  restore_name_str = text_to_cstring(restore_name);
252 
253  if (strlen(restore_name_str) >= MAXFNAMELEN)
254  ereport(ERROR,
255  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
256  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
257 
258  restorepoint = XLogRestorePoint(restore_name_str);
259 
260  /*
261  * As a convenience, return the WAL location of the restore point record
262  */
263  PG_RETURN_LSN(restorepoint);
264 }
bool RecoveryInProgress(void)
Definition: xlog.c:6334
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:8078
#define XLogIsNeeded()
Definition: xlog.h:109
#define MAXFNAMELEN
uint64 XLogRecPtr
Definition: xlogdefs.h:21

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

◆ pg_current_wal_flush_lsn()

Datum pg_current_wal_flush_lsn ( PG_FUNCTION_ARGS  )

Definition at line 316 of file xlogfuncs.c.

317 {
318  XLogRecPtr current_recptr;
319 
320  if (RecoveryInProgress())
321  ereport(ERROR,
322  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
323  errmsg("recovery is in progress"),
324  errhint("WAL control functions cannot be executed during recovery.")));
325 
326  current_recptr = GetFlushRecPtr(NULL);
327 
328  PG_RETURN_LSN(current_recptr);
329 }
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6499

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

◆ pg_current_wal_insert_lsn()

Datum pg_current_wal_insert_lsn ( PG_FUNCTION_ARGS  )

Definition at line 295 of file xlogfuncs.c.

296 {
297  XLogRecPtr current_recptr;
298 
299  if (RecoveryInProgress())
300  ereport(ERROR,
301  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
302  errmsg("recovery is in progress"),
303  errhint("WAL control functions cannot be executed during recovery.")));
304 
305  current_recptr = GetXLogInsertRecPtr();
306 
307  PG_RETURN_LSN(current_recptr);
308 }
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:9435

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

◆ pg_current_wal_lsn()

Datum pg_current_wal_lsn ( PG_FUNCTION_ARGS  )

Definition at line 274 of file xlogfuncs.c.

275 {
276  XLogRecPtr current_recptr;
277 
278  if (RecoveryInProgress())
279  ereport(ERROR,
280  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
281  errmsg("recovery is in progress"),
282  errhint("WAL control functions cannot be executed during recovery.")));
283 
284  current_recptr = GetXLogWriteRecPtr();
285 
286  PG_RETURN_LSN(current_recptr);
287 }
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:9451

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

◆ pg_get_wal_replay_pause_state()

Datum pg_get_wal_replay_pause_state ( PG_FUNCTION_ARGS  )

Definition at line 593 of file xlogfuncs.c.

594 {
595  char *statestr = NULL;
596 
597  if (!RecoveryInProgress())
598  ereport(ERROR,
599  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
600  errmsg("recovery is not in progress"),
601  errhint("Recovery control functions can only be executed during recovery.")));
602 
603  /* get the recovery pause state */
604  switch (GetRecoveryPauseState())
605  {
606  case RECOVERY_NOT_PAUSED:
607  statestr = "not paused";
608  break;
610  statestr = "pause requested";
611  break;
612  case RECOVERY_PAUSED:
613  statestr = "paused";
614  break;
615  }
616 
617  Assert(statestr != NULL);
619 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:184
RecoveryPauseState GetRecoveryPauseState(void)
@ RECOVERY_PAUSED
Definition: xlogrecovery.h:48
@ RECOVERY_NOT_PAUSED
Definition: xlogrecovery.h:46
@ RECOVERY_PAUSE_REQUESTED
Definition: xlogrecovery.h:47

References Assert, cstring_to_text(), ereport, errcode(), errhint(), errmsg(), ERROR, GetRecoveryPauseState(), PG_RETURN_TEXT_P, RECOVERY_NOT_PAUSED, RECOVERY_PAUSE_REQUESTED, RECOVERY_PAUSED, and RecoveryInProgress().

◆ pg_is_in_recovery()

Datum pg_is_in_recovery ( PG_FUNCTION_ARGS  )

Definition at line 643 of file xlogfuncs.c.

644 {
646 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_RETURN_BOOL, and RecoveryInProgress().

◆ pg_is_wal_replay_paused()

Datum pg_is_wal_replay_paused ( PG_FUNCTION_ARGS  )

Definition at line 572 of file xlogfuncs.c.

573 {
574  if (!RecoveryInProgress())
575  ereport(ERROR,
576  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
577  errmsg("recovery is not in progress"),
578  errhint("Recovery control functions can only be executed during recovery.")));
579 
581 }

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

◆ pg_last_wal_receive_lsn()

Datum pg_last_wal_receive_lsn ( PG_FUNCTION_ARGS  )

Definition at line 338 of file xlogfuncs.c.

339 {
340  XLogRecPtr recptr;
341 
342  recptr = GetWalRcvFlushRecPtr(NULL, NULL);
343 
344  if (recptr == 0)
345  PG_RETURN_NULL();
346 
347  PG_RETURN_LSN(recptr);
348 }
#define PG_RETURN_NULL()
Definition: fmgr.h:345
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)

References GetWalRcvFlushRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

◆ pg_last_wal_replay_lsn()

Datum pg_last_wal_replay_lsn ( PG_FUNCTION_ARGS  )

Definition at line 357 of file xlogfuncs.c.

358 {
359  XLogRecPtr recptr;
360 
361  recptr = GetXLogReplayRecPtr(NULL);
362 
363  if (recptr == 0)
364  PG_RETURN_NULL();
365 
366  PG_RETURN_LSN(recptr);
367 }
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References GetXLogReplayRecPtr(), PG_RETURN_LSN, and PG_RETURN_NULL.

◆ pg_last_xact_replay_timestamp()

Datum pg_last_xact_replay_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 628 of file xlogfuncs.c.

629 {
630  TimestampTz xtime;
631 
632  xtime = GetLatestXTime();
633  if (xtime == 0)
634  PG_RETURN_NULL();
635 
636  PG_RETURN_TIMESTAMPTZ(xtime);
637 }
int64 TimestampTz
Definition: timestamp.h:39
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68
TimestampTz GetLatestXTime(void)

References GetLatestXTime(), PG_RETURN_NULL, and PG_RETURN_TIMESTAMPTZ.

◆ pg_log_standby_snapshot()

Datum pg_log_standby_snapshot ( PG_FUNCTION_ARGS  )

Definition at line 202 of file xlogfuncs.c.

203 {
204  XLogRecPtr recptr;
205 
206  if (RecoveryInProgress())
207  ereport(ERROR,
208  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
209  errmsg("recovery is in progress"),
210  errhint("%s cannot be executed during recovery.",
211  "pg_log_standby_snapshot()")));
212 
213  if (!XLogStandbyInfoActive())
214  ereport(ERROR,
215  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
216  errmsg("pg_log_standby_snapshot() can only be used if \"wal_level\" >= \"replica\"")));
217 
218  recptr = LogStandbySnapshot();
219 
220  /*
221  * As a convenience, return the WAL location of the last inserted record
222  */
223  PG_RETURN_LSN(recptr);
224 }
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1281
#define XLogStandbyInfoActive()
Definition: xlog.h:123

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

◆ pg_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 670 of file xlogfuncs.c.

671 {
672  bool wait = PG_GETARG_BOOL(0);
673  int wait_seconds = PG_GETARG_INT32(1);
674  FILE *promote_file;
675  int i;
676 
677  if (!RecoveryInProgress())
678  ereport(ERROR,
679  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
680  errmsg("recovery is not in progress"),
681  errhint("Recovery control functions can only be executed during recovery.")));
682 
683  if (wait_seconds <= 0)
684  ereport(ERROR,
685  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
686  errmsg("\"wait_seconds\" must not be negative or zero")));
687 
688  /* create the promote signal file */
690  if (!promote_file)
691  ereport(ERROR,
693  errmsg("could not create file \"%s\": %m",
695 
696  if (FreeFile(promote_file))
697  ereport(ERROR,
699  errmsg("could not write file \"%s\": %m",
701 
702  /* signal the postmaster */
703  if (kill(PostmasterPid, SIGUSR1) != 0)
704  {
705  (void) unlink(PROMOTE_SIGNAL_FILE);
706  ereport(ERROR,
707  (errcode(ERRCODE_SYSTEM_ERROR),
708  errmsg("failed to send signal to postmaster: %m")));
709  }
710 
711  /* return immediately if waiting was not requested */
712  if (!wait)
713  PG_RETURN_BOOL(true);
714 
715  /* wait for the amount of time wanted until promotion */
716 #define WAITS_PER_SECOND 10
717  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
718  {
719  int rc;
720 
722 
723  if (!RecoveryInProgress())
724  PG_RETURN_BOOL(true);
725 
727 
728  rc = WaitLatch(MyLatch,
730  1000L / WAITS_PER_SECOND,
731  WAIT_EVENT_PROMOTE);
732 
733  /*
734  * Emergency bailout if postmaster has died. This is to avoid the
735  * necessity for manual cleanup of all postmaster children.
736  */
737  if (rc & WL_POSTMASTER_DEATH)
738  ereport(FATAL,
739  (errcode(ERRCODE_ADMIN_SHUTDOWN),
740  errmsg("terminating connection due to unexpected postmaster exit"),
741  errcontext("while waiting on promotion")));
742  }
743 
745  (errmsg_plural("server did not promote within %d second",
746  "server did not promote within %d seconds",
747  wait_seconds,
748  wait_seconds)));
749  PG_RETURN_BOOL(false);
750 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
int errcode_for_file_access(void)
Definition: elog.c:876
#define errcontext
Definition: elog.h:196
#define FATAL
Definition: elog.h:41
#define WARNING
Definition: elog.h:36
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2605
int FreeFile(FILE *file)
Definition: fd.c:2803
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
pid_t PostmasterPid
Definition: globals.c:105
struct Latch * MyLatch
Definition: globals.c:62
int i
Definition: isn.c:72
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:100
static int wait_seconds
Definition: pg_ctl.c:76
#define kill(pid, sig)
Definition: win32_port.h:503
#define SIGUSR1
Definition: win32_port.h:180
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:309
#define WAITS_PER_SECOND

References AllocateFile(), CHECK_FOR_INTERRUPTS, ereport, errcode(), errcode_for_file_access(), errcontext, errhint(), errmsg(), errmsg_plural(), ERROR, FATAL, FreeFile(), i, kill, MyLatch, PG_GETARG_BOOL, PG_GETARG_INT32, PG_RETURN_BOOL, PostmasterPid, promote_file, PROMOTE_SIGNAL_FILE, RecoveryInProgress(), ResetLatch(), SIGUSR1, wait_seconds, WaitLatch(), WAITS_PER_SECOND, WARNING, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

◆ pg_split_walfile_name()

Datum pg_split_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 463 of file xlogfuncs.c.

464 {
465 #define PG_SPLIT_WALFILE_NAME_COLS 2
466  char *fname = text_to_cstring(PG_GETARG_TEXT_PP(0));
467  char *fname_upper;
468  char *p;
469  TimeLineID tli;
470  XLogSegNo segno;
472  bool isnull[PG_SPLIT_WALFILE_NAME_COLS] = {0};
473  TupleDesc tupdesc;
474  HeapTuple tuple;
475  char buf[256];
476  Datum result;
477 
478  fname_upper = pstrdup(fname);
479 
480  /* Capitalize WAL file name. */
481  for (p = fname_upper; *p; p++)
482  *p = pg_toupper((unsigned char) *p);
483 
484  if (!IsXLogFileName(fname_upper))
485  ereport(ERROR,
486  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
487  errmsg("invalid WAL file name \"%s\"", fname)));
488 
489  XLogFromFileName(fname_upper, &tli, &segno, wal_segment_size);
490 
491  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
492  elog(ERROR, "return type must be a row type");
493 
494  /* Convert to numeric. */
495  snprintf(buf, sizeof buf, UINT64_FORMAT, segno);
498  ObjectIdGetDatum(0),
499  Int32GetDatum(-1));
500 
501  values[1] = Int64GetDatum(tli);
502 
503  tuple = heap_form_tuple(tupdesc, values, isnull);
504  result = HeapTupleGetDatum(tuple);
505 
506  PG_RETURN_DATUM(result);
507 
508 #undef PG_SPLIT_WALFILE_NAME_COLS
509 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:637
#define UINT64_FORMAT
Definition: c.h:526
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:645
char * pstrdup(const char *in)
Definition: mcxt.c:1696
static char * buf
Definition: pg_test_fsync.c:72
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
#define snprintf
Definition: port.h:238
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
int wal_segment_size
Definition: xlog.c:143
static bool IsXLogFileName(const char *fname)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
uint32 TimeLineID
Definition: xlogdefs.h:59
uint64 XLogSegNo
Definition: xlogdefs.h:48
#define PG_SPLIT_WALFILE_NAME_COLS

References buf, CStringGetDatum(), DirectFunctionCall3, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), IsXLogFileName(), numeric_in(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_RETURN_DATUM, PG_SPLIT_WALFILE_NAME_COLS, pg_toupper(), pstrdup(), snprintf, text_to_cstring(), TYPEFUNC_COMPOSITE, UINT64_FORMAT, values, wal_segment_size, and XLogFromFileName().

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 177 of file xlogfuncs.c.

178 {
179  XLogRecPtr switchpoint;
180 
181  if (RecoveryInProgress())
182  ereport(ERROR,
183  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
184  errmsg("recovery is in progress"),
185  errhint("WAL control functions cannot be executed during recovery.")));
186 
187  switchpoint = RequestXLogSwitch(false);
188 
189  /*
190  * As a convenience, return the WAL location of the switch record
191  */
192  PG_RETURN_LSN(switchpoint);
193 }
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:8060

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

◆ pg_wal_lsn_diff()

Datum pg_wal_lsn_diff ( PG_FUNCTION_ARGS  )

Definition at line 652 of file xlogfuncs.c.

653 {
654  Datum result;
655 
657  PG_GETARG_DATUM(0),
658  PG_GETARG_DATUM(1));
659 
660  PG_RETURN_DATUM(result);
661 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum pg_lsn_mi(PG_FUNCTION_ARGS)
Definition: pg_lsn.c:224

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

◆ pg_wal_replay_pause()

Datum pg_wal_replay_pause ( PG_FUNCTION_ARGS  )

Definition at line 518 of file xlogfuncs.c.

519 {
520  if (!RecoveryInProgress())
521  ereport(ERROR,
522  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
523  errmsg("recovery is not in progress"),
524  errhint("Recovery control functions can only be executed during recovery.")));
525 
526  if (PromoteIsTriggered())
527  ereport(ERROR,
528  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
529  errmsg("standby promotion is ongoing"),
530  errhint("%s cannot be executed after promotion is triggered.",
531  "pg_wal_replay_pause()")));
532 
533  SetRecoveryPause(true);
534 
535  /* wake up the recovery process so that it can process the pause request */
536  WakeupRecovery();
537 
538  PG_RETURN_VOID();
539 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void SetRecoveryPause(bool recoveryPause)
void WakeupRecovery(void)
bool PromoteIsTriggered(void)

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

◆ pg_wal_replay_resume()

Datum pg_wal_replay_resume ( PG_FUNCTION_ARGS  )

Definition at line 548 of file xlogfuncs.c.

549 {
550  if (!RecoveryInProgress())
551  ereport(ERROR,
552  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
553  errmsg("recovery is not in progress"),
554  errhint("Recovery control functions can only be executed during recovery.")));
555 
556  if (PromoteIsTriggered())
557  ereport(ERROR,
558  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
559  errmsg("standby promotion is ongoing"),
560  errhint("%s cannot be executed after promotion is triggered.",
561  "pg_wal_replay_resume()")));
562 
563  SetRecoveryPause(false);
564 
565  PG_RETURN_VOID();
566 }

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

◆ pg_walfile_name()

Datum pg_walfile_name ( PG_FUNCTION_ARGS  )

Definition at line 438 of file xlogfuncs.c.

439 {
440  XLogSegNo xlogsegno;
441  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
442  char xlogfilename[MAXFNAMELEN];
443 
444  if (RecoveryInProgress())
445  ereport(ERROR,
446  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
447  errmsg("recovery is in progress"),
448  errhint("%s cannot be executed during recovery.",
449  "pg_walfile_name()")));
450 
451  XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
452  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
454 
455  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
456 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6520
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

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

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 374 of file xlogfuncs.c.

375 {
376  XLogSegNo xlogsegno;
377  uint32 xrecoff;
378  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
379  char xlogfilename[MAXFNAMELEN];
380  Datum values[2];
381  bool isnull[2];
382  TupleDesc resultTupleDesc;
383  HeapTuple resultHeapTuple;
384  Datum result;
385 
386  if (RecoveryInProgress())
387  ereport(ERROR,
388  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
389  errmsg("recovery is in progress"),
390  errhint("%s cannot be executed during recovery.",
391  "pg_walfile_name_offset()")));
392 
393  /*
394  * Construct a tuple descriptor for the result row. This must match this
395  * function's pg_proc entry!
396  */
397  resultTupleDesc = CreateTemplateTupleDesc(2);
398  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
399  TEXTOID, -1, 0);
400  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
401  INT4OID, -1, 0);
402 
403  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
404 
405  /*
406  * xlogfilename
407  */
408  XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
409  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
411 
412  values[0] = CStringGetTextDatum(xlogfilename);
413  isnull[0] = false;
414 
415  /*
416  * offset
417  */
418  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
419 
420  values[1] = UInt32GetDatum(xrecoff);
421  isnull[1] = false;
422 
423  /*
424  * Tuple jam: Having first prepared your Datums, then squash together
425  */
426  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
427 
428  result = HeapTupleGetDatum(resultHeapTuple);
429 
430  PG_RETURN_DATUM(result);
431 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int uint32
Definition: c.h:492
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2158
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:232
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)

References BlessTupleDesc(), CreateTemplateTupleDesc(), CStringGetTextDatum, ereport, errcode(), errhint(), errmsg(), ERROR, GetWALInsertionTimeLine(), heap_form_tuple(), HeapTupleGetDatum(), MAXFNAMELEN, PG_GETARG_LSN, PG_RETURN_DATUM, RecoveryInProgress(), TupleDescInitEntry(), UInt32GetDatum(), values, wal_segment_size, XLByteToSeg, XLogFileName(), and XLogSegmentOffset.

Variable Documentation

◆ backup_state

BackupState* backup_state = NULL
static

◆ backupcontext

MemoryContext backupcontext = NULL
static

Definition at line 45 of file xlogfuncs.c.

Referenced by pg_backup_start(), and pg_backup_stop().

◆ tablespace_map

StringInfo tablespace_map = NULL
static

Definition at line 42 of file xlogfuncs.c.

Referenced by perform_base_backup(), pg_backup_start(), and pg_backup_stop().