Skip to content

Commit 177f1d3

Browse files
author
Commitfest Bot
committed
[CF 5513] v6 - SQL function which allows to distinguish a server being in point in time recovery mode and an ordinary replica
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5513 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Mikhail Litsarev
2 parents 682c5be + 553b6ee commit 177f1d3

File tree

10 files changed

+215
-140
lines changed

10 files changed

+215
-140
lines changed

src/backend/access/transam/timeline.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ readTimeLineHistory(TimeLineID targetTLI)
9393
return list_make1(entry);
9494
}
9595

96-
if (ArchiveRecoveryRequested)
96+
if (ArchiveRecoveryRequested())
9797
{
9898
TLHistoryFileName(histfname, targetTLI);
9999
fromArchive =
@@ -229,7 +229,7 @@ existsTimeLineHistory(TimeLineID probeTLI)
229229
if (probeTLI == 1)
230230
return false;
231231

232-
if (ArchiveRecoveryRequested)
232+
if (ArchiveRecoveryRequested())
233233
{
234234
TLHistoryFileName(histfname, probeTLI);
235235
RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0, false);
@@ -331,7 +331,7 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
331331
/*
332332
* If a history file exists for the parent, copy it verbatim
333333
*/
334-
if (ArchiveRecoveryRequested)
334+
if (ArchiveRecoveryRequested())
335335
{
336336
TLHistoryFileName(histfname, parentTLI);
337337
RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0, false);

src/backend/access/transam/xlog.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -5434,7 +5434,7 @@ CheckRequiredParameterValues(void)
54345434
* For archive recovery, the WAL must be generated with at least 'replica'
54355435
* wal_level.
54365436
*/
5437-
if (ArchiveRecoveryRequested && ControlFile->wal_level == WAL_LEVEL_MINIMAL)
5437+
if (ArchiveRecoveryRequested() && ControlFile->wal_level == WAL_LEVEL_MINIMAL)
54385438
{
54395439
ereport(FATAL,
54405440
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
@@ -5447,7 +5447,7 @@ CheckRequiredParameterValues(void)
54475447
* For Hot Standby, the WAL must be generated with 'replica' mode, and we
54485448
* must have at least as many backend slots as the primary.
54495449
*/
5450-
if (ArchiveRecoveryRequested && EnableHotStandby)
5450+
if (ArchiveRecoveryRequested() && EnableHotStandby)
54515451
{
54525452
/* We ignore autovacuum_worker_slots when we make this test. */
54535453
RecoveryRequiresIntParameter("max_connections",
@@ -5607,8 +5607,8 @@ StartupXLOG(void)
56075607
*
56085608
* InitWalRecovery analyzes the control file and the backup label file, if
56095609
* any. It updates the in-memory ControlFile buffer according to the
5610-
* starting checkpoint, and sets InRecovery and ArchiveRecoveryRequested.
5611-
* It also applies the tablespace map file, if any.
5610+
* starting checkpoint, and sets SX_ARCHIVE_RECOVERY_REQUESTED and
5611+
* InRecovery. It also applies the tablespace map file, if any.
56125612
*/
56135613
InitWalRecovery(ControlFile, &wasShutdown,
56145614
&haveBackupLabel, &haveTblspcMap);
@@ -5740,7 +5740,7 @@ StartupXLOG(void)
57405740
{
57415741
/* Initialize state for RecoveryInProgress() */
57425742
SpinLockAcquire(&XLogCtl->info_lck);
5743-
if (InArchiveRecovery)
5743+
if (InArchiveRecovery())
57445744
XLogCtl->SharedRecoveryState = RECOVERY_STATE_ARCHIVE;
57455745
else
57465746
XLogCtl->SharedRecoveryState = RECOVERY_STATE_CRASH;
@@ -5793,7 +5793,7 @@ StartupXLOG(void)
57935793
* startup process to think that there are still invalid page
57945794
* references when checking for data consistency.
57955795
*/
5796-
if (InArchiveRecovery)
5796+
if (InArchiveRecovery())
57975797
{
57985798
LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
57995799
LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
@@ -5827,7 +5827,7 @@ StartupXLOG(void)
58275827
* control file and we've established a recovery snapshot from a
58285828
* running-xacts WAL record.
58295829
*/
5830-
if (ArchiveRecoveryRequested && EnableHotStandby)
5830+
if (ArchiveRecoveryRequested() && EnableHotStandby)
58315831
{
58325832
TransactionId *xids;
58335833
int nxids;
@@ -5940,7 +5940,7 @@ StartupXLOG(void)
59405940
* recover from an online backup but never called pg_backup_stop(), or
59415941
* you didn't archive all the WAL needed.
59425942
*/
5943-
if (ArchiveRecoveryRequested || ControlFile->backupEndRequired)
5943+
if (ArchiveRecoveryRequested() || ControlFile->backupEndRequired)
59445944
{
59455945
if (!XLogRecPtrIsInvalid(ControlFile->backupStartPoint) || ControlFile->backupEndRequired)
59465946
ereport(FATAL,
@@ -5992,7 +5992,7 @@ StartupXLOG(void)
59925992
* In a normal crash recovery, we can just extend the timeline we were in.
59935993
*/
59945994
newTLI = endOfRecoveryInfo->lastRecTLI;
5995-
if (ArchiveRecoveryRequested)
5995+
if (ArchiveRecoveryRequested())
59965996
{
59975997
newTLI = findNewestTimeLine(recoveryTargetTLI) + 1;
59985998
ereport(LOG,
@@ -6185,7 +6185,7 @@ StartupXLOG(void)
61856185
XLogReportParameters();
61866186

61876187
/* If this is archive recovery, perform post-recovery cleanup actions. */
6188-
if (ArchiveRecoveryRequested)
6188+
if (ArchiveRecoveryRequested())
61896189
CleanupAfterArchiveRecovery(EndOfLogTLI, EndOfLog, newTLI);
61906190

61916191
/*
@@ -6344,7 +6344,7 @@ PerformRecoveryXLogAction(void)
63446344
* of a full checkpoint. A checkpoint is requested later, after we're
63456345
* fully out of recovery mode and already accepting queries.
63466346
*/
6347-
if (ArchiveRecoveryRequested && IsUnderPostmaster &&
6347+
if (ArchiveRecoveryRequested() && IsUnderPostmaster &&
63486348
PromoteIsTriggered())
63496349
{
63506350
promoted = true;
@@ -8338,7 +8338,7 @@ xlog_redo(XLogReaderState *record)
83388338
* record, the backup was canceled and the end-of-backup record will
83398339
* never arrive.
83408340
*/
8341-
if (ArchiveRecoveryRequested &&
8341+
if (ArchiveRecoveryRequested() &&
83428342
!XLogRecPtrIsInvalid(ControlFile->backupStartPoint) &&
83438343
XLogRecPtrIsInvalid(ControlFile->backupEndPoint))
83448344
ereport(PANIC,
@@ -8579,7 +8579,7 @@ xlog_redo(XLogReaderState *record)
85798579
* local copies cannot be updated as long as crash recovery is
85808580
* happening and we expect all the WAL to be replayed.
85818581
*/
8582-
if (InArchiveRecovery)
8582+
if (InArchiveRecovery())
85838583
{
85848584
LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
85858585
LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;

src/backend/access/transam/xlogarchive.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ RestoreArchivedFile(char *path, const char *xlogfname,
6868
* Ignore restore_command when not in archive recovery (meaning we are in
6969
* crash recovery).
7070
*/
71-
if (!ArchiveRecoveryRequested)
71+
if (!ArchiveRecoveryRequested())
7272
goto not_available;
7373

7474
/* In standby mode, restore_command might not be supplied */
@@ -205,7 +205,7 @@ RestoreArchivedFile(char *path, const char *xlogfname,
205205
* incorrectly conclude we've reached the end of WAL and we're
206206
* done recovering ...
207207
*/
208-
if (StandbyMode && stat_buf.st_size < expectedSize)
208+
if (InStandbyMode() && stat_buf.st_size < expectedSize)
209209
elevel = DEBUG1;
210210
else
211211
elevel = FATAL;

src/backend/access/transam/xlogfuncs.c

+31
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "storage/fd.h"
3131
#include "storage/latch.h"
3232
#include "storage/standby.h"
33+
#include "utils/array.h"
3334
#include "utils/builtins.h"
3435
#include "utils/memutils.h"
3536
#include "utils/pg_lsn.h"
@@ -748,3 +749,33 @@ pg_promote(PG_FUNCTION_ARGS)
748749
wait_seconds)));
749750
PG_RETURN_BOOL(false);
750751
}
752+
753+
Datum
754+
pg_get_recovery_flags(PG_FUNCTION_ARGS)
755+
{
756+
/*
757+
* Currently supported number of recovery flags is equal to two:
758+
* {SX_PROMOTE_IS_TRIGGERED, SX_STANDBY_MODE_REQUESTED}.
759+
* The SX_STANDBY_MODE_REQUESTED is valid only in the startup process.
760+
*/
761+
#define MAX_RECOVERY_FLAGS 2
762+
763+
bits32 recovery_flags;
764+
int cnt = 0;
765+
Datum flags[MAX_RECOVERY_FLAGS];
766+
ArrayType *txt_arr;
767+
768+
recovery_flags = GetXLogRecoveryFlags();
769+
770+
if (recovery_flags & SX_PROMOTE_IS_TRIGGERED)
771+
flags[cnt++] = CStringGetTextDatum("PROMOTE_IS_TRIGGERED");
772+
773+
if (recovery_flags & SX_STANDBY_MODE_REQUESTED)
774+
flags[cnt++] = CStringGetTextDatum("STANDBY_MODE_REQUESTED");
775+
776+
Assert(cnt <= MAX_RECOVERY_FLAGS);
777+
778+
/* Returns bit array as Datum */
779+
txt_arr = construct_array_builtin(flags, cnt, TEXTOID);
780+
PG_RETURN_ARRAYTYPE_P(txt_arr);
781+
}

0 commit comments

Comments
 (0)