PostgreSQL Source Code  git master
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/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 56 of file xlogfuncs.c.

57 {
58  text *backupid = PG_GETARG_TEXT_PP(0);
59  bool fast = PG_GETARG_BOOL(1);
60  char *backupidstr;
62  MemoryContext oldcontext;
63 
64  backupidstr = text_to_cstring(backupid);
65 
66  if (status == SESSION_BACKUP_RUNNING)
67  ereport(ERROR,
68  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
69  errmsg("a backup is already in progress in this session")));
70 
71  /*
72  * backup_state and tablespace_map need to be long-lived as they are used
73  * in pg_backup_stop(). These are allocated in a dedicated memory context
74  * child of TopMemoryContext, deleted at the end of pg_backup_stop(). If
75  * an error happens before ending the backup, memory would be leaked in
76  * this context until pg_backup_start() is called again.
77  */
78  if (backupcontext == NULL)
79  {
81  "on-line backup context",
83  }
84  else
85  {
86  backup_state = NULL;
87  tablespace_map = NULL;
89  }
90 
94  MemoryContextSwitchTo(oldcontext);
95 
97  do_pg_backup_start(backupidstr, fast, NULL, backup_state, tablespace_map);
98 
100 }
int errcode(int sqlerrcode)
Definition: elog.c:857
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:1346
#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:41
XLogRecPtr startpoint
Definition: xlogbackup.h:26
Definition: c.h:687
char * text_to_cstring(const text *t)
Definition: varlena.c:217
SessionBackupState get_backup_status(void)
Definition: xlog.c:9021
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:9341
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8712
SessionBackupState
Definition: xlog.h:282
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:284
static BackupState * backup_state
Definition: xlogfuncs.c:40
static MemoryContext backupcontext
Definition: xlogfuncs.c:44
static StringInfo tablespace_map
Definition: xlogfuncs.c:41

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 123 of file xlogfuncs.c.

124 {
125 #define PG_BACKUP_STOP_V2_COLS 3
126  TupleDesc tupdesc;
128  bool nulls[PG_BACKUP_STOP_V2_COLS] = {0};
129  bool waitforarchive = PG_GETARG_BOOL(0);
130  char *backup_label;
132 
133  /* Initialize attributes information in the tuple descriptor */
134  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
135  elog(ERROR, "return type must be a row type");
136 
137  if (status != SESSION_BACKUP_RUNNING)
138  ereport(ERROR,
139  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
140  errmsg("backup is not in progress"),
141  errhint("Did you call pg_backup_start()?")));
142 
143  Assert(backup_state != NULL);
144  Assert(tablespace_map != NULL);
145 
146  /* Stop the backup */
147  do_pg_backup_stop(backup_state, waitforarchive);
148 
149  /* Build the contents of backup_label */
150  backup_label = build_backup_content(backup_state, false);
151 
153  values[1] = CStringGetTextDatum(backup_label);
155 
156  /* Deallocate backup-related variables */
157  pfree(backup_label);
158 
159  /* Clean up the session-level state and its memory context */
160  backup_state = NULL;
161  tablespace_map = NULL;
163  backupcontext = NULL;
164 
165  /* Returns the record as Datum */
167 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define Assert(condition)
Definition: c.h:858
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define elog(elevel,...)
Definition: elog.h:224
#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:1520
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:9040
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 232 of file xlogfuncs.c.

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

316 {
317  XLogRecPtr current_recptr;
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  current_recptr = GetFlushRecPtr(NULL);
326 
327  PG_RETURN_LSN(current_recptr);
328 }
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6455

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 294 of file xlogfuncs.c.

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

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 273 of file xlogfuncs.c.

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

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 592 of file xlogfuncs.c.

593 {
594  char *statestr = NULL;
595 
596  if (!RecoveryInProgress())
597  ereport(ERROR,
598  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
599  errmsg("recovery is not in progress"),
600  errhint("Recovery control functions can only be executed during recovery.")));
601 
602  /* get the recovery pause state */
603  switch (GetRecoveryPauseState())
604  {
605  case RECOVERY_NOT_PAUSED:
606  statestr = "not paused";
607  break;
609  statestr = "pause requested";
610  break;
611  case RECOVERY_PAUSED:
612  statestr = "paused";
613  break;
614  }
615 
616  Assert(statestr != NULL);
618 }
#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 642 of file xlogfuncs.c.

643 {
645 }
#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 571 of file xlogfuncs.c.

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

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 337 of file xlogfuncs.c.

338 {
339  XLogRecPtr recptr;
340 
341  recptr = GetWalRcvFlushRecPtr(NULL, NULL);
342 
343  if (recptr == 0)
344  PG_RETURN_NULL();
345 
346  PG_RETURN_LSN(recptr);
347 }
#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 356 of file xlogfuncs.c.

357 {
358  XLogRecPtr recptr;
359 
360  recptr = GetXLogReplayRecPtr(NULL);
361 
362  if (recptr == 0)
363  PG_RETURN_NULL();
364 
365  PG_RETURN_LSN(recptr);
366 }
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 627 of file xlogfuncs.c.

628 {
629  TimestampTz xtime;
630 
631  xtime = GetLatestXTime();
632  if (xtime == 0)
633  PG_RETURN_NULL();
634 
635  PG_RETURN_TIMESTAMPTZ(xtime);
636 }
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 201 of file xlogfuncs.c.

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

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

◆ pg_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 669 of file xlogfuncs.c.

670 {
671  bool wait = PG_GETARG_BOOL(0);
672  int wait_seconds = PG_GETARG_INT32(1);
673  FILE *promote_file;
674  int i;
675 
676  if (!RecoveryInProgress())
677  ereport(ERROR,
678  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
679  errmsg("recovery is not in progress"),
680  errhint("Recovery control functions can only be executed during recovery.")));
681 
682  if (wait_seconds <= 0)
683  ereport(ERROR,
684  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
685  errmsg("\"wait_seconds\" must not be negative or zero")));
686 
687  /* create the promote signal file */
689  if (!promote_file)
690  ereport(ERROR,
692  errmsg("could not create file \"%s\": %m",
694 
695  if (FreeFile(promote_file))
696  ereport(ERROR,
698  errmsg("could not write file \"%s\": %m",
700 
701  /* signal the postmaster */
702  if (kill(PostmasterPid, SIGUSR1) != 0)
703  {
704  (void) unlink(PROMOTE_SIGNAL_FILE);
705  ereport(ERROR,
706  (errcode(ERRCODE_SYSTEM_ERROR),
707  errmsg("failed to send signal to postmaster: %m")));
708  }
709 
710  /* return immediately if waiting was not requested */
711  if (!wait)
712  PG_RETURN_BOOL(true);
713 
714  /* wait for the amount of time wanted until promotion */
715 #define WAITS_PER_SECOND 10
716  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
717  {
718  int rc;
719 
721 
722  if (!RecoveryInProgress())
723  PG_RETURN_BOOL(true);
724 
726 
727  rc = WaitLatch(MyLatch,
729  1000L / WAITS_PER_SECOND,
730  WAIT_EVENT_PROMOTE);
731 
732  /*
733  * Emergency bailout if postmaster has died. This is to avoid the
734  * necessity for manual cleanup of all postmaster children.
735  */
736  if (rc & WL_POSTMASTER_DEATH)
737  ereport(FATAL,
738  (errcode(ERRCODE_ADMIN_SHUTDOWN),
739  errmsg("terminating connection due to unexpected postmaster exit"),
740  errcontext("while waiting on promotion")));
741  }
742 
744  (errmsg_plural("server did not promote within %d second",
745  "server did not promote within %d seconds",
746  wait_seconds,
747  wait_seconds)));
748  PG_RETURN_BOOL(false);
749 }
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:880
#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:2583
int FreeFile(FILE *file)
Definition: fd.c:2781
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
pid_t PostmasterPid
Definition: globals.c:103
struct Latch * MyLatch
Definition: globals.c:60
int i
Definition: isn.c:73
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:99
static int wait_seconds
Definition: pg_ctl.c:75
#define kill(pid, sig)
Definition: win32_port.h:485
#define SIGUSR1
Definition: win32_port.h:180
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:305
#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 462 of file xlogfuncs.c.

463 {
464 #define PG_SPLIT_WALFILE_NAME_COLS 2
465  char *fname = text_to_cstring(PG_GETARG_TEXT_PP(0));
466  char *fname_upper;
467  char *p;
468  TimeLineID tli;
469  XLogSegNo segno;
471  bool isnull[PG_SPLIT_WALFILE_NAME_COLS] = {0};
472  TupleDesc tupdesc;
473  HeapTuple tuple;
474  char buf[256];
475  Datum result;
476 
477  fname_upper = pstrdup(fname);
478 
479  /* Capitalize WAL file name. */
480  for (p = fname_upper; *p; p++)
481  *p = pg_toupper((unsigned char) *p);
482 
483  if (!IsXLogFileName(fname_upper))
484  ereport(ERROR,
485  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
486  errmsg("invalid WAL file name \"%s\"", fname)));
487 
488  XLogFromFileName(fname_upper, &tli, &segno, wal_segment_size);
489 
490  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
491  elog(ERROR, "return type must be a row type");
492 
493  /* Convert to numeric. */
494  snprintf(buf, sizeof buf, UINT64_FORMAT, segno);
497  ObjectIdGetDatum(0),
498  Int32GetDatum(-1));
499 
500  values[1] = Int64GetDatum(tli);
501 
502  tuple = heap_form_tuple(tupdesc, values, isnull);
503  result = HeapTupleGetDatum(tuple);
504 
505  PG_RETURN_DATUM(result);
506 
507 #undef PG_SPLIT_WALFILE_NAME_COLS
508 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:628
#define UINT64_FORMAT
Definition: c.h:549
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
char * pstrdup(const char *in)
Definition: mcxt.c:1695
static char * buf
Definition: pg_test_fsync.c:73
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 176 of file xlogfuncs.c.

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

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 651 of file xlogfuncs.c.

652 {
653  Datum result;
654 
656  PG_GETARG_DATUM(0),
657  PG_GETARG_DATUM(1));
658 
659  PG_RETURN_DATUM(result);
660 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#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 517 of file xlogfuncs.c.

518 {
519  if (!RecoveryInProgress())
520  ereport(ERROR,
521  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
522  errmsg("recovery is not in progress"),
523  errhint("Recovery control functions can only be executed during recovery.")));
524 
525  if (PromoteIsTriggered())
526  ereport(ERROR,
527  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
528  errmsg("standby promotion is ongoing"),
529  errhint("%s cannot be executed after promotion is triggered.",
530  "pg_wal_replay_pause()")));
531 
532  SetRecoveryPause(true);
533 
534  /* wake up the recovery process so that it can process the pause request */
535  WakeupRecovery();
536 
537  PG_RETURN_VOID();
538 }
#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 547 of file xlogfuncs.c.

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

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 437 of file xlogfuncs.c.

438 {
439  XLogSegNo xlogsegno;
440  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
441  char xlogfilename[MAXFNAMELEN];
442 
443  if (RecoveryInProgress())
444  ereport(ERROR,
445  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
446  errmsg("recovery is in progress"),
447  errhint("%s cannot be executed during recovery.",
448  "pg_walfile_name()")));
449 
450  XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
451  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
453 
454  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
455 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6476
#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 373 of file xlogfuncs.c.

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

Referenced by pg_backup_start(), and pg_backup_stop().

◆ tablespace_map

StringInfo tablespace_map = NULL
static

Definition at line 41 of file xlogfuncs.c.

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