Skip to content

Commit e55fd6e

Browse files
Mikhail LitsarevCommitfest Bot
Mikhail Litsarev
authored and
Commitfest Bot
committed
Wrapper function to extract whole text array from recovery flags bitset.
It returns SX_PROMOTE_IS_TRIGGERED, SX_STANDBY_MODE_REQUESTED flags for recovery states.
1 parent 3936d57 commit e55fd6e

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

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+
}

src/backend/access/transam/xlogrecovery.c

+16
Original file line numberDiff line numberDiff line change
@@ -4376,6 +4376,22 @@ StartupRequestWalReceiverRestart(void)
43764376
}
43774377
}
43784378

4379+
/*
4380+
* Return SX_PROMOTE_IS_TRIGGERED, SX_STANDBY_MODE_REQUESTED flags for
4381+
* recovery states.
4382+
*/
4383+
bits32 GetXLogRecoveryFlags(void)
4384+
{
4385+
bits32 flags = 0;
4386+
4387+
SpinLockAcquire(&XLogRecoveryCtl->info_lck);
4388+
flags = XLogRecoveryCtl->sharedRecoveryFlags;
4389+
SpinLockRelease(&XLogRecoveryCtl->info_lck);
4390+
4391+
flags |= (localRecoveryFlags & SX_STANDBY_MODE_REQUESTED);
4392+
4393+
return flags;
4394+
}
43794395

43804396
/*
43814397
* Has a standby promotion already been triggered?

src/include/access/xlogrecovery.h

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ extern TimestampTz GetLatestXTime(void);
172172
extern TimestampTz GetCurrentChunkReplayStartTime(void);
173173
extern XLogRecPtr GetCurrentReplayRecPtr(TimeLineID *replayEndTLI);
174174

175+
extern bits32 GetXLogRecoveryFlags(void);
175176
extern bool PromoteIsTriggered(void);
176177
extern bool CheckPromoteSignal(void);
177178
extern void WakeupRecovery(void);

src/include/catalog/pg_proc.dat

+5
Original file line numberDiff line numberDiff line change
@@ -6757,6 +6757,11 @@
67576757
proname => 'pg_is_in_recovery', provolatile => 'v', prorettype => 'bool',
67586758
proargtypes => '', prosrc => 'pg_is_in_recovery' },
67596759

6760+
{ oid => '8439',
6761+
descr => 'return flags for recovery states',
6762+
proname => 'pg_get_recovery_flags', provolatile => 'v', prorettype => '_text',
6763+
proargtypes => '', prosrc => 'pg_get_recovery_flags' },
6764+
67606765
{ oid => '3820', descr => 'current wal flush location',
67616766
proname => 'pg_last_wal_receive_lsn', provolatile => 'v',
67626767
prorettype => 'pg_lsn', proargtypes => '',

0 commit comments

Comments
 (0)