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 "access/xlogutils.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/ipc.h"
#include "storage/smgr.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
#include "utils/timestamp.h"
#include "utils/tuplestore.h"
Include dependency graph for xlogfuncs.c:

Go to the source code of this file.

Macros

#define PG_BACKUP_STOP_V2_COLS   3
 
#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_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_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

◆ WAITS_PER_SECOND

#define WAITS_PER_SECOND   10

Function Documentation

◆ pg_backup_start()

Datum pg_backup_start ( PG_FUNCTION_ARGS  )

Definition at line 61 of file xlogfuncs.c.

62 {
63  text *backupid = PG_GETARG_TEXT_PP(0);
64  bool fast = PG_GETARG_BOOL(1);
65  char *backupidstr;
67  MemoryContext oldcontext;
68 
69  backupidstr = text_to_cstring(backupid);
70 
72  ereport(ERROR,
73  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
74  errmsg("a backup is already in progress in this session")));
75 
76  /*
77  * backup_state and tablespace_map need to be long-lived as they are used
78  * in pg_backup_stop(). These are allocated in a dedicated memory context
79  * child of TopMemoryContext, deleted at the end of pg_backup_stop(). If
80  * an error happens before ending the backup, memory would be leaked in
81  * this context until pg_backup_start() is called again.
82  */
83  if (backupcontext == NULL)
84  {
86  "on-line backup context",
88  }
89  else
90  {
91  backup_state = NULL;
92  tablespace_map = NULL;
94  }
95 
99  MemoryContextSwitchTo(oldcontext);
100 
102  do_pg_backup_start(backupidstr, fast, NULL, backup_state, tablespace_map);
103 
105 }
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#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:303
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * palloc0(Size size)
Definition: mcxt.c:1230
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:34
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
XLogRecPtr startpoint
Definition: xlogbackup.h:26
Definition: c.h:623
char * text_to_cstring(const text *t)
Definition: varlena.c:222
SessionBackupState get_backup_status(void)
Definition: xlog.c:8524
void register_persistent_abort_backup_handler(void)
Definition: xlog.c:8844
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8244
SessionBackupState
Definition: xlog.h:274
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:276
static BackupState * backup_state
Definition: xlogfuncs.c:45
static MemoryContext backupcontext
Definition: xlogfuncs.c:49
static StringInfo tablespace_map
Definition: xlogfuncs.c:46

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, status(), tablespace_map, text_to_cstring(), and TopMemoryContext.

◆ pg_backup_stop()

Datum pg_backup_stop ( PG_FUNCTION_ARGS  )

Definition at line 128 of file xlogfuncs.c.

129 {
130 #define PG_BACKUP_STOP_V2_COLS 3
131  TupleDesc tupdesc;
133  bool nulls[PG_BACKUP_STOP_V2_COLS] = {0};
134  bool waitforarchive = PG_GETARG_BOOL(0);
135  char *backup_label;
137 
138  /* Initialize attributes information in the tuple descriptor */
139  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
140  elog(ERROR, "return type must be a row type");
141 
143  ereport(ERROR,
144  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
145  errmsg("backup is not in progress"),
146  errhint("Did you call pg_backup_start()?")));
147 
148  Assert(backup_state != NULL);
149  Assert(tablespace_map != NULL);
150 
151  /* Stop the backup */
152  do_pg_backup_stop(backup_state, waitforarchive);
153 
154  /* Build the contents of backup_label */
155  backup_label = build_backup_content(backup_state, false);
156 
158  values[1] = CStringGetTextDatum(backup_label);
160 
161  /* Deallocate backup-related variables */
162  pfree(backup_label);
163 
164  /* Clean up the session-level state and its memory context */
165  backup_state = NULL;
166  tablespace_map = NULL;
168  backupcontext = NULL;
169 
170  /* Returns the record as Datum */
172 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
int errhint(const char *fmt,...)
Definition: elog.c:1153
#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, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1306
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:376
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
uintptr_t Datum
Definition: postgres.h:412
XLogRecPtr stoppoint
Definition: xlogbackup.h:33
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition: xlog.c:8543
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, status(), BackupState::stoppoint, tablespace_map, TYPEFUNC_COMPOSITE, and values.

◆ pg_create_restore_point()

Datum pg_create_restore_point ( PG_FUNCTION_ARGS  )

Definition at line 206 of file xlogfuncs.c.

207 {
208  text *restore_name = PG_GETARG_TEXT_PP(0);
209  char *restore_name_str;
210  XLogRecPtr restorepoint;
211 
212  if (RecoveryInProgress())
213  ereport(ERROR,
214  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
215  errmsg("recovery is in progress"),
216  errhint("WAL control functions cannot be executed during recovery.")));
217 
218  if (!XLogIsNeeded())
219  ereport(ERROR,
220  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
221  errmsg("WAL level not sufficient for creating a restore point"),
222  errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
223 
224  restore_name_str = text_to_cstring(restore_name);
225 
226  if (strlen(restore_name_str) >= MAXFNAMELEN)
227  ereport(ERROR,
228  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
229  errmsg("value too long for restore point (maximum %d characters)", MAXFNAMELEN - 1)));
230 
231  restorepoint = XLogRestorePoint(restore_name_str);
232 
233  /*
234  * As a convenience, return the WAL location of the restore point record
235  */
236  PG_RETURN_LSN(restorepoint);
237 }
bool RecoveryInProgress(void)
Definition: xlog.c:5912
XLogRecPtr XLogRestorePoint(const char *rpName)
Definition: xlog.c:7545
#define XLogIsNeeded()
Definition: xlog.h:104
#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 289 of file xlogfuncs.c.

290 {
291  XLogRecPtr current_recptr;
292 
293  if (RecoveryInProgress())
294  ereport(ERROR,
295  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
296  errmsg("recovery is in progress"),
297  errhint("WAL control functions cannot be executed during recovery.")));
298 
299  current_recptr = GetFlushRecPtr(NULL);
300 
301  PG_RETURN_LSN(current_recptr);
302 }
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6077

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

269 {
270  XLogRecPtr current_recptr;
271 
272  if (RecoveryInProgress())
273  ereport(ERROR,
274  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
275  errmsg("recovery is in progress"),
276  errhint("WAL control functions cannot be executed during recovery.")));
277 
278  current_recptr = GetXLogInsertRecPtr();
279 
280  PG_RETURN_LSN(current_recptr);
281 }
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:8858

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

248 {
249  XLogRecPtr current_recptr;
250 
251  if (RecoveryInProgress())
252  ereport(ERROR,
253  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
254  errmsg("recovery is in progress"),
255  errhint("WAL control functions cannot be executed during recovery.")));
256 
257  current_recptr = GetXLogWriteRecPtr();
258 
259  PG_RETURN_LSN(current_recptr);
260 }
XLogRecPtr GetXLogWriteRecPtr(void)
Definition: xlog.c:8874

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

518 {
519  char *statestr = NULL;
520 
521  if (!RecoveryInProgress())
522  ereport(ERROR,
523  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
524  errmsg("recovery is not in progress"),
525  errhint("Recovery control functions can only be executed during recovery.")));
526 
527  /* get the recovery pause state */
528  switch (GetRecoveryPauseState())
529  {
530  case RECOVERY_NOT_PAUSED:
531  statestr = "not paused";
532  break;
534  statestr = "pause requested";
535  break;
536  case RECOVERY_PAUSED:
537  statestr = "paused";
538  break;
539  }
540 
541  Assert(statestr != NULL);
543 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:189
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 567 of file xlogfuncs.c.

568 {
570 }
#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 496 of file xlogfuncs.c.

497 {
498  if (!RecoveryInProgress())
499  ereport(ERROR,
500  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
501  errmsg("recovery is not in progress"),
502  errhint("Recovery control functions can only be executed during recovery.")));
503 
505 }

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

312 {
313  XLogRecPtr recptr;
314 
315  recptr = GetWalRcvFlushRecPtr(NULL, NULL);
316 
317  if (recptr == 0)
318  PG_RETURN_NULL();
319 
320  PG_RETURN_LSN(recptr);
321 }
#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 330 of file xlogfuncs.c.

331 {
332  XLogRecPtr recptr;
333 
334  recptr = GetXLogReplayRecPtr(NULL);
335 
336  if (recptr == 0)
337  PG_RETURN_NULL();
338 
339  PG_RETURN_LSN(recptr);
340 }
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 552 of file xlogfuncs.c.

553 {
554  TimestampTz xtime;
555 
556  xtime = GetLatestXTime();
557  if (xtime == 0)
558  PG_RETURN_NULL();
559 
560  PG_RETURN_TIMESTAMPTZ(xtime);
561 }
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_promote()

Datum pg_promote ( PG_FUNCTION_ARGS  )

Definition at line 594 of file xlogfuncs.c.

595 {
596  bool wait = PG_GETARG_BOOL(0);
597  int wait_seconds = PG_GETARG_INT32(1);
598  FILE *promote_file;
599  int i;
600 
601  if (!RecoveryInProgress())
602  ereport(ERROR,
603  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
604  errmsg("recovery is not in progress"),
605  errhint("Recovery control functions can only be executed during recovery.")));
606 
607  if (wait_seconds <= 0)
608  ereport(ERROR,
609  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
610  errmsg("\"wait_seconds\" must not be negative or zero")));
611 
612  /* create the promote signal file */
614  if (!promote_file)
615  ereport(ERROR,
617  errmsg("could not create file \"%s\": %m",
619 
620  if (FreeFile(promote_file))
621  ereport(ERROR,
623  errmsg("could not write file \"%s\": %m",
625 
626  /* signal the postmaster */
627  if (kill(PostmasterPid, SIGUSR1) != 0)
628  {
630  (errmsg("failed to send signal to postmaster: %m")));
631  (void) unlink(PROMOTE_SIGNAL_FILE);
632  PG_RETURN_BOOL(false);
633  }
634 
635  /* return immediately if waiting was not requested */
636  if (!wait)
637  PG_RETURN_BOOL(true);
638 
639  /* wait for the amount of time wanted until promotion */
640 #define WAITS_PER_SECOND 10
641  for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++)
642  {
643  int rc;
644 
646 
647  if (!RecoveryInProgress())
648  PG_RETURN_BOOL(true);
649 
651 
652  rc = WaitLatch(MyLatch,
654  1000L / WAITS_PER_SECOND,
656 
657  /*
658  * Emergency bailout if postmaster has died. This is to avoid the
659  * necessity for manual cleanup of all postmaster children.
660  */
661  if (rc & WL_POSTMASTER_DEATH)
662  PG_RETURN_BOOL(false);
663  }
664 
666  (errmsg_plural("server did not promote within %d second",
667  "server did not promote within %d seconds",
668  wait_seconds,
669  wait_seconds)));
670  PG_RETURN_BOOL(false);
671 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1016
int errcode_for_file_access(void)
Definition: elog.c:718
#define WARNING
Definition: elog.h:32
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2383
int FreeFile(FILE *file)
Definition: fd.c:2581
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
pid_t PostmasterPid
Definition: globals.c:99
struct Latch * MyLatch
Definition: globals.c:58
int i
Definition: isn.c:73
void ResetLatch(Latch *latch)
Definition: latch.c:683
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:476
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:100
static int wait_seconds
Definition: pg_ctl.c:75
@ WAIT_EVENT_PROMOTE
Definition: wait_event.h:119
#define kill(pid, sig)
Definition: win32_port.h:482
#define SIGUSR1
Definition: win32_port.h:188
#define PROMOTE_SIGNAL_FILE
Definition: xlog.h:297
#define WAITS_PER_SECOND

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

◆ pg_switch_wal()

Datum pg_switch_wal ( PG_FUNCTION_ARGS  )

Definition at line 181 of file xlogfuncs.c.

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

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

577 {
578  Datum result;
579 
581  PG_GETARG_DATUM(0),
582  PG_GETARG_DATUM(1));
583 
584  PG_RETURN_DATUM(result);
585 }
#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:225

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

443 {
444  if (!RecoveryInProgress())
445  ereport(ERROR,
446  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
447  errmsg("recovery is not in progress"),
448  errhint("Recovery control functions can only be executed during recovery.")));
449 
450  if (PromoteIsTriggered())
451  ereport(ERROR,
452  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
453  errmsg("standby promotion is ongoing"),
454  errhint("%s cannot be executed after promotion is triggered.",
455  "pg_wal_replay_pause()")));
456 
457  SetRecoveryPause(true);
458 
459  /* wake up the recovery process so that it can process the pause request */
460  WakeupRecovery();
461 
462  PG_RETURN_VOID();
463 }
#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 472 of file xlogfuncs.c.

473 {
474  if (!RecoveryInProgress())
475  ereport(ERROR,
476  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
477  errmsg("recovery is not in progress"),
478  errhint("Recovery control functions can only be executed during recovery.")));
479 
480  if (PromoteIsTriggered())
481  ereport(ERROR,
482  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
483  errmsg("standby promotion is ongoing"),
484  errhint("%s cannot be executed after promotion is triggered.",
485  "pg_wal_replay_resume()")));
486 
487  SetRecoveryPause(false);
488 
489  PG_RETURN_VOID();
490 }

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

416 {
417  XLogSegNo xlogsegno;
418  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
419  char xlogfilename[MAXFNAMELEN];
420 
421  if (RecoveryInProgress())
422  ereport(ERROR,
423  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
424  errmsg("recovery is in progress"),
425  errhint("%s cannot be executed during recovery.",
426  "pg_walfile_name()")));
427 
428  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
429  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
431 
432  PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
433 }
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
TimeLineID GetWALInsertionTimeLine(void)
Definition: xlog.c:6100
int wal_segment_size
Definition: xlog.c:146
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
uint64 XLogSegNo
Definition: xlogdefs.h:48

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

◆ pg_walfile_name_offset()

Datum pg_walfile_name_offset ( PG_FUNCTION_ARGS  )

Definition at line 351 of file xlogfuncs.c.

352 {
353  XLogSegNo xlogsegno;
354  uint32 xrecoff;
355  XLogRecPtr locationpoint = PG_GETARG_LSN(0);
356  char xlogfilename[MAXFNAMELEN];
357  Datum values[2];
358  bool isnull[2];
359  TupleDesc resultTupleDesc;
360  HeapTuple resultHeapTuple;
361  Datum result;
362 
363  if (RecoveryInProgress())
364  ereport(ERROR,
365  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
366  errmsg("recovery is in progress"),
367  errhint("%s cannot be executed during recovery.",
368  "pg_walfile_name_offset()")));
369 
370  /*
371  * Construct a tuple descriptor for the result row. This must match this
372  * function's pg_proc entry!
373  */
374  resultTupleDesc = CreateTemplateTupleDesc(2);
375  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
376  TEXTOID, -1, 0);
377  TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
378  INT4OID, -1, 0);
379 
380  resultTupleDesc = BlessTupleDesc(resultTupleDesc);
381 
382  /*
383  * xlogfilename
384  */
385  XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
386  XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
388 
389  values[0] = CStringGetTextDatum(xlogfilename);
390  isnull[0] = false;
391 
392  /*
393  * offset
394  */
395  xrecoff = XLogSegmentOffset(locationpoint, wal_segment_size);
396 
397  values[1] = UInt32GetDatum(xrecoff);
398  isnull[1] = false;
399 
400  /*
401  * Tuple jam: Having first prepared your Datums, then squash together
402  */
403  resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
404 
405  result = HeapTupleGetDatum(resultHeapTuple);
406 
407  PG_RETURN_DATUM(result);
408 }
int16 AttrNumber
Definition: attnum.h:21
unsigned int uint32
Definition: c.h:442
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:580
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
#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, XLByteToPrevSeg, XLogFileName(), and XLogSegmentOffset.

Variable Documentation

◆ backup_state

BackupState* backup_state = NULL
static

Definition at line 45 of file xlogfuncs.c.

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

◆ backupcontext

MemoryContext backupcontext = NULL
static

Definition at line 49 of file xlogfuncs.c.

Referenced by pg_backup_start(), and pg_backup_stop().

◆ tablespace_map

StringInfo tablespace_map = NULL
static

Definition at line 46 of file xlogfuncs.c.

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