pg.ddx.io pgsql-committers@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Alexander Korotkov <aekorotkov@gmail.com>
To: Peter Eisentraut <peter@eisentraut.org>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Implement pg_wal_replay_wait() stored procedure
Date: Sun, 20 Oct 2024 20:21:50 +0300
Message-ID: <CAPpHfdut8AvtU9PhXBOd0r5LPYkLyPeiWS+sfZ0FRoUBrS3_TQ@mail.gmail.com> (raw )
In-Reply-To: <CAPpHfdvbcTBk_yuvvg=HvWJeLOWBtdmh1_K4j4btZ70ES_HWuw@mail.gmail.com >
References: <E1sZwuz-002NPQ-Lc@gemulon.postgresql.org >
<18c0fa64-0475-415e-a1bd-665d922c5201@eisentraut.org >
<CAPpHfduQPYWATA2rfbYXsXM+Yspn1nQVwrEs=p303O428J57Yg@mail.gmail.com >
<ZtT4dhlxuxdPPI8t@paquier.xyz >
<CAPpHfdubNR746cbxRvs8C0MmNvUTtrjWwZ=RzcB5i_4kUz+RuQ@mail.gmail.com >
<1f5b399a-76b1-48e1-87b0-f511e61818f3@eisentraut.org >
<CAPpHfdvbcTBk_yuvvg=HvWJeLOWBtdmh1_K4j4btZ70ES_HWuw@mail.gmail.com >
On Wed, Oct 16, 2024 at 11:20 PM Alexander Korotkov
<aekorotkov@gmail.com> wrote:
>
> On Wed, Oct 16, 2024 at 10:35 PM Peter Eisentraut <peter@eisentraut.org> wrote:
> > On 02.09.24 01:55, Alexander Korotkov wrote:
> > > On Mon, Sep 2, 2024 at 2:28 AM Michael Paquier <michael@paquier.xyz> wrote:
> > >> On Sun, Sep 01, 2024 at 10:35:27PM +0300, Alexander Korotkov wrote:
> > >>> This path hasn't changes since the patch revision when it was a
> > >>> utility command. I agree that this doesn't look like proper path for
> > >>> stored procedure. But I don't think src/backend/utils/adt is
> > >>> appropriate path either, because it's not really about data type.
> > >>> pg_wal_replay_wait() looks a good neighbor for
> > >>> pg_wal_replay_pause()/pg_wal_replay_resume() and other WAL-related
> > >>> functions. So, what about moving it to src/backend/access/transam?
> > >>
> > >> Moving the new function to xlogfuncs.c while publishing
> > >> WaitForLSNReplay() makes sense to me.
> > >
> > > Thank you for proposal. I like this.
> > >
> > > Could you, please, check the attached patch?
> >
> > We still have stuff in src/backend/commands/waitlsn.c that is nothing
> > like a "command". You have moved some stuff elsewhere, but what are you
> > planning to do with the rest?
>
> Thank you for spotting this another time. What about moving that
> somewhere like src/backend/access/transam/xlogwait.c ?
Implemented this as a separate patch (0001). Also rebased other
pending patches on that. 0004 now revises header comment of
xlogwait.c with new procedure signature.
------
Regards,
Alexander Korotkov
Supabase
Attachments: download all (4 files, .tar.gz)
v5-0002-Make-WaitForLSNReplay-issue-FATAL-on-postmaster-d.patch (application/octet-stream, 1.6K) download
From 6614c6d5dc8907f5ca5e683edab5f7703de78dfd Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Sun, 29 Sep 2024 12:29:31 +0300
Subject: [PATCH v5 2/4] Make WaitForLSNReplay() issue FATAL on postmaster
death
Reported-by: Michael Paquier
Discussion: https://postgr.es/m/ZvY2C8N4ZqgCFaLu%40paquier.xyz
---
src/backend/access/transam/xlogwait.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index eef58ce69ce..353b7854dc8 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -222,7 +222,7 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
{
XLogRecPtr currentLSN;
TimestampTz endtime = 0;
- int wake_events = WL_LATCH_SET | WL_EXIT_ON_PM_DEATH;
+ int wake_events = WL_LATCH_SET | WL_POSTMASTER_DEATH;
/* Shouldn't be called when shmem isn't initialized */
Assert(waitLSNState);
@@ -313,6 +313,16 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
rc = WaitLatch(MyLatch, wake_events, delay_ms,
WAIT_EVENT_WAIT_FOR_WAL_REPLAY);
+ /*
+ * Emergency bailout if postmaster has died. This is to avoid the
+ * necessity for manual cleanup of all postmaster children.
+ */
+ if (rc & WL_POSTMASTER_DEATH)
+ ereport(FATAL,
+ (errcode(ERRCODE_ADMIN_SHUTDOWN),
+ errmsg("terminating connection due to unexpected postmaster exit"),
+ errcontext("while waiting for LSN replay")));
+
if (rc & WL_LATCH_SET)
ResetLatch(MyLatch);
}
--
2.39.5 (Apple Git-154)
v5-0001-Move-LSN-waiting-declarations-and-definitions-to-.patch (application/octet-stream, 8.6K) download
From 7fe904de12d8f00fa5535c834c63f0285017ab20 Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Sun, 20 Oct 2024 20:05:01 +0300
Subject: [PATCH v5 1/4] Move LSN waiting declarations and definitions to
better place
3c5db1d6b implemented the pg_wal_replay_wait() stored procedure. Due to
the patch development history, the implementation resided in
src/backend/commands/waitlsn.c (src/include/commands/waitlsn.h for headers).
014f9f34d moved pg_wal_replay_wait() itself to
src/backend/access/transam/xlogfuncs.c near to the WAL-manipulation functions.
But most of the implementation stayed in place.
The code in src/backend/commands/waitlsn.c has nothing to do with commands,
but is related to WAL. So, this commit moves this code into
src/backend/access/transam/xlogwait.c (src/include/access/xlogwait.h for
headers).
Reported-by: Peter Eisentraut
Discussion: https://postgr.es/m/18c0fa64-0475-415e-a1bd-665d922c5201%40eisentraut.org
---
src/backend/access/transam/Makefile | 3 ++-
src/backend/access/transam/meson.build | 1 +
src/backend/access/transam/xact.c | 2 +-
src/backend/access/transam/xlog.c | 2 +-
src/backend/access/transam/xlogfuncs.c | 2 +-
src/backend/access/transam/xlogrecovery.c | 2 +-
.../{commands/waitlsn.c => access/transam/xlogwait.c} | 6 +++---
src/backend/commands/Makefile | 3 +--
src/backend/commands/meson.build | 1 -
src/backend/storage/ipc/ipci.c | 2 +-
src/backend/storage/lmgr/proc.c | 2 +-
src/include/{commands/waitlsn.h => access/xlogwait.h} | 10 +++++-----
12 files changed, 18 insertions(+), 18 deletions(-)
rename src/backend/{commands/waitlsn.c => access/transam/xlogwait.c} (99%)
rename src/include/{commands/waitlsn.h => access/xlogwait.h} (94%)
diff --git a/src/backend/access/transam/Makefile b/src/backend/access/transam/Makefile
index 661c55a9db7..a32f473e0a2 100644
--- a/src/backend/access/transam/Makefile
+++ b/src/backend/access/transam/Makefile
@@ -36,7 +36,8 @@ OBJS = \
xlogreader.o \
xlogrecovery.o \
xlogstats.o \
- xlogutils.o
+ xlogutils.o \
+ xlogwait.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/access/transam/meson.build b/src/backend/access/transam/meson.build
index 8a3522557cd..91d258f9df1 100644
--- a/src/backend/access/transam/meson.build
+++ b/src/backend/access/transam/meson.build
@@ -24,6 +24,7 @@ backend_sources += files(
'xlogrecovery.c',
'xlogstats.c',
'xlogutils.c',
+ 'xlogwait.c',
)
# used by frontend programs to build a frontend xlogreader
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 87700c7c5c7..cbeced71a02 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -31,6 +31,7 @@
#include "access/xloginsert.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
+#include "access/xlogwait.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/pg_enum.h"
@@ -38,7 +39,6 @@
#include "commands/async.h"
#include "commands/tablecmds.h"
#include "commands/trigger.h"
-#include "commands/waitlsn.h"
#include "common/pg_prng.h"
#include "executor/spi.h"
#include "libpq/be-fsstubs.h"
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 9102c8d772e..ad9b0b612f4 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -62,11 +62,11 @@
#include "access/xlogreader.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
+#include "access/xlogwait.h"
#include "backup/basebackup.h"
#include "catalog/catversion.h"
#include "catalog/pg_control.h"
#include "catalog/pg_database.h"
-#include "commands/waitlsn.h"
#include "common/controldata_utils.h"
#include "common/file_utils.h"
#include "executor/instrument.h"
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index 3e3d2bb6189..cbf84ef7d8f 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -22,8 +22,8 @@
#include "access/xlog_internal.h"
#include "access/xlogbackup.h"
#include "access/xlogrecovery.h"
+#include "access/xlogwait.h"
#include "catalog/pg_type.h"
-#include "commands/waitlsn.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pgstat.h"
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 320b14add1a..31caa49d6c3 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -40,10 +40,10 @@
#include "access/xlogreader.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
+#include "access/xlogwait.h"
#include "backup/basebackup.h"
#include "catalog/pg_control.h"
#include "commands/tablespace.h"
-#include "commands/waitlsn.h"
#include "common/file_utils.h"
#include "miscadmin.h"
#include "pgstat.h"
diff --git a/src/backend/commands/waitlsn.c b/src/backend/access/transam/xlogwait.c
similarity index 99%
rename from src/backend/commands/waitlsn.c
rename to src/backend/access/transam/xlogwait.c
index 501938f4330..eef58ce69ce 100644
--- a/src/backend/commands/waitlsn.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
- * waitlsn.c
+ * xlogwait.c
* Implements waiting for the given replay LSN, which is used in
* CALL pg_wal_replay_wait(target_lsn pg_lsn, timeout float8).
*
* Copyright (c) 2024, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * src/backend/commands/waitlsn.c
+ * src/backend/access/transam/xlogwait.c
*
*-------------------------------------------------------------------------
*/
@@ -20,7 +20,7 @@
#include "pgstat.h"
#include "access/xlog.h"
#include "access/xlogrecovery.h"
-#include "commands/waitlsn.h"
+#include "access/xlogwait.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/latch.h"
diff --git a/src/backend/commands/Makefile b/src/backend/commands/Makefile
index cede90c3b98..48f7348f91c 100644
--- a/src/backend/commands/Makefile
+++ b/src/backend/commands/Makefile
@@ -61,7 +61,6 @@ OBJS = \
vacuum.o \
vacuumparallel.o \
variable.o \
- view.o \
- waitlsn.o
+ view.o
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/commands/meson.build b/src/backend/commands/meson.build
index 7549be5dc3b..6dd00a4abde 100644
--- a/src/backend/commands/meson.build
+++ b/src/backend/commands/meson.build
@@ -50,5 +50,4 @@ backend_sources += files(
'vacuumparallel.c',
'variable.c',
'view.c',
- 'waitlsn.c',
)
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 10fc18f2529..9ff687045f4 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -24,8 +24,8 @@
#include "access/twophase.h"
#include "access/xlogprefetcher.h"
#include "access/xlogrecovery.h"
+#include "access/xlogwait.h"
#include "commands/async.h"
-#include "commands/waitlsn.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index eaf3916f282..260e7029f50 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -36,7 +36,7 @@
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xlogutils.h"
-#include "commands/waitlsn.h"
+#include "access/xlogwait.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
diff --git a/src/include/commands/waitlsn.h b/src/include/access/xlogwait.h
similarity index 94%
rename from src/include/commands/waitlsn.h
rename to src/include/access/xlogwait.h
index bb5ac858dcc..31e208cb7ad 100644
--- a/src/include/commands/waitlsn.h
+++ b/src/include/access/xlogwait.h
@@ -1,16 +1,16 @@
/*-------------------------------------------------------------------------
*
- * waitlsn.h
+ * xlogwait.h
* Declarations for LSN replay waiting routines.
*
* Copyright (c) 2024, PostgreSQL Global Development Group
*
- * src/include/commands/waitlsn.h
+ * src/include/access/xlogwait.h
*
*-------------------------------------------------------------------------
*/
-#ifndef WAIT_LSN_H
-#define WAIT_LSN_H
+#ifndef XLOG_WAIT_H
+#define XLOG_WAIT_H
#include "lib/pairingheap.h"
#include "postgres.h"
@@ -78,4 +78,4 @@ extern void WaitLSNSetLatches(XLogRecPtr currentLSN);
extern void WaitLSNCleanup(void);
extern void WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
-#endif /* WAIT_LSN_H */
+#endif /* XLOG_WAIT_H */
--
2.39.5 (Apple Git-154)
v5-0003-Refactor-WaitForLSNReplay-to-return-the-result-of.patch (application/octet-stream, 5.7K) download
From 1f9c879800c0e1e591d490b1f9a1282f013d7696 Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Thu, 19 Sep 2024 14:48:44 +0300
Subject: [PATCH v5 3/4] Refactor WaitForLSNReplay() to return the result of
waiting
Currently WaitForLSNReplay() immediatly throws an error if waiting for LSN
replay is not successful. This commit teaches WaitForLSNReplay() to return
the result of waiting, while making pg_wal_replay_wait() responsible for
throwing an appropriate error.
This is preparation for new procedure pg_wal_replay_wait_status(), which will
expose the waiting result to the user instead of throwing an error.
---
src/backend/access/transam/xlogfuncs.c | 31 +++++++++++++++++++++++-
src/backend/access/transam/xlogwait.c | 33 +++++++++-----------------
src/include/access/xlogwait.h | 12 +++++++++-
3 files changed, 52 insertions(+), 24 deletions(-)
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index cbf84ef7d8f..ddca78d3717 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -759,6 +759,7 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
{
XLogRecPtr target_lsn = PG_GETARG_LSN(0);
int64 timeout = PG_GETARG_INT64(1);
+ WaitLSNResult result;
if (timeout < 0)
ereport(ERROR,
@@ -799,7 +800,35 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
*/
Assert(MyProc->xmin == InvalidTransactionId);
- (void) WaitForLSNReplay(target_lsn, timeout);
+ result = WaitForLSNReplay(target_lsn, timeout);
+
+ /*
+ * Process the result of WaitForLSNReplay(). Throw appropriate error if
+ * needed.
+ */
+ switch (result)
+ {
+ case WAIT_LSN_RESULT_SUCCESS:
+ /* Nothing to do on success */
+ break;
+
+ case WAIT_LSN_RESULT_TIMEOUT:
+ ereport(ERROR,
+ (errcode(ERRCODE_QUERY_CANCELED),
+ errmsg("timed out while waiting for target LSN %X/%X to be replayed; current replay LSN %X/%X",
+ LSN_FORMAT_ARGS(target_lsn),
+ LSN_FORMAT_ARGS(GetXLogReplayRecPtr(NULL)))));
+ break;
+
+ case WAIT_LSN_RESULT_NOT_IN_RECOVERY:
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("recovery is not in progress"),
+ errdetail("Recovery ended before replaying target LSN %X/%X; last replay LSN %X/%X.",
+ LSN_FORMAT_ARGS(target_lsn),
+ LSN_FORMAT_ARGS(GetXLogReplayRecPtr(NULL)))));
+ break;
+ }
PG_RETURN_VOID();
}
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 353b7854dc8..58fb10aa5a8 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -217,7 +217,7 @@ WaitLSNCleanup(void)
* Wait using MyLatch till the given LSN is replayed, the postmaster dies or
* timeout happens.
*/
-void
+WaitLSNResult
WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
{
XLogRecPtr currentLSN;
@@ -240,17 +240,14 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
* check the last replay LSN before reporting an error.
*/
if (targetLSN <= GetXLogReplayRecPtr(NULL))
- return;
- ereport(ERROR,
- (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("recovery is not in progress"),
- errhint("Waiting for LSN can only be executed during recovery.")));
+ return WAIT_LSN_RESULT_SUCCESS;
+ return WAIT_LSN_RESULT_NOT_IN_RECOVERY;
}
else
{
/* If target LSN is already replayed, exit immediately */
if (targetLSN <= GetXLogReplayRecPtr(NULL))
- return;
+ return WAIT_LSN_RESULT_SUCCESS;
}
if (timeout > 0)
@@ -276,17 +273,13 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
{
/*
* Recovery was ended, but recheck if target LSN was already
- * replayed.
+ * replayed. See the comment regarding deleteLSNWaiter() below.
*/
+ deleteLSNWaiter();
currentLSN = GetXLogReplayRecPtr(NULL);
if (targetLSN <= currentLSN)
- return;
- ereport(ERROR,
- (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("recovery is not in progress"),
- errdetail("Recovery ended before replaying target LSN %X/%X; last replay LSN %X/%X.",
- LSN_FORMAT_ARGS(targetLSN),
- LSN_FORMAT_ARGS(currentLSN))));
+ return WAIT_LSN_RESULT_SUCCESS;
+ return WAIT_LSN_RESULT_NOT_IN_RECOVERY;
}
else
{
@@ -338,11 +331,7 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
* If we didn't reach the target LSN, we must be exited by timeout.
*/
if (targetLSN > currentLSN)
- {
- ereport(ERROR,
- (errcode(ERRCODE_QUERY_CANCELED),
- errmsg("timed out while waiting for target LSN %X/%X to be replayed; current replay LSN %X/%X",
- LSN_FORMAT_ARGS(targetLSN),
- LSN_FORMAT_ARGS(currentLSN))));
- }
+ return WAIT_LSN_RESULT_TIMEOUT;
+
+ return WAIT_LSN_RESULT_SUCCESS;
}
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 31e208cb7ad..bbc67439855 100644
--- a/src/include/access/xlogwait.h
+++ b/src/include/access/xlogwait.h
@@ -70,12 +70,22 @@ typedef struct WaitLSNState
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER];
} WaitLSNState;
+/*
+ * Result statuses for WaitForLSNReplay().
+ */
+typedef enum
+{
+ WAIT_LSN_RESULT_SUCCESS, /* Target LSN is reached */
+ WAIT_LSN_RESULT_TIMEOUT, /* Timeout occured */
+ WAIT_LSN_RESULT_NOT_IN_RECOVERY, /* Recovery ended before or during our wait */
+} WaitLSNResult;
+
extern PGDLLIMPORT WaitLSNState *waitLSNState;
extern Size WaitLSNShmemSize(void);
extern void WaitLSNShmemInit(void);
extern void WaitLSNSetLatches(XLogRecPtr currentLSN);
extern void WaitLSNCleanup(void);
-extern void WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
+extern WaitLSNResult WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
#endif /* XLOG_WAIT_H */
--
2.39.5 (Apple Git-154)
v5-0004-Add-no_error-argument-to-pg_wal_replay_wait.patch (application/octet-stream, 10.5K) download
From 9225fdd5791ac7538a92cbf6e7b4564fec68fddd Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Thu, 19 Sep 2024 15:34:18 +0300
Subject: [PATCH v5 4/4] Add 'no_error' argument to pg_wal_replay_wait()
This argument allow to skip throwing an error. Instead the result status
can be obtained using pg_wal_replay_wait_status() function.
---
doc/src/sgml/func.sgml | 56 +++++++++++++++++++---
src/backend/access/transam/xlogfuncs.c | 38 +++++++++++++--
src/backend/access/transam/xlogwait.c | 3 +-
src/backend/catalog/system_functions.sql | 4 +-
src/include/catalog/pg_proc.dat | 7 ++-
src/test/recovery/t/043_wal_replay_wait.pl | 19 ++++++++
6 files changed, 114 insertions(+), 13 deletions(-)
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index ad663c94d77..1b68f284e5e 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -28989,12 +28989,15 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
</para>
<table id="recovery-synchronization-procedure-table">
- <title>Recovery Synchronization Procedure</title>
+ <title>Recovery Synchronization Procedure and Function</title>
<tgroup cols="1">
<thead>
<row>
<entry role="func_table_entry"><para role="func_signature">
- Procedure
+ Procedure or Function
+ </para>
+ <para>
+ Type
</para>
<para>
Description
@@ -29010,8 +29013,11 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
</indexterm>
<function>pg_wal_replay_wait</function> (
<parameter>target_lsn</parameter> <type>pg_lsn</type>,
- <parameter>timeout</parameter> <type>bigint</type> <literal>DEFAULT</literal> <literal>0</literal>)
- <returnvalue>void</returnvalue>
+ <parameter>timeout</parameter> <type>bigint</type> <literal>DEFAULT</literal> <literal>0</literal>,
+ <parameter>no_error</parameter> <type>bool</type> <literal>DEFAULT</literal> <literal>false</literal>)
+ </para>
+ <para>
+ Procedure
</para>
<para>
Waits until recovery replays <literal>target_lsn</literal>.
@@ -29022,7 +29028,30 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
procedure waits until <literal>target_lsn</literal> is reached or
the specified <parameter>timeout</parameter> has elapsed.
On timeout, or if the server is promoted before
- <literal>target_lsn</literal> is reached, an error is emitted.
+ <literal>target_lsn</literal> is reached, an error is emitted,
+ as soon as <parameter>no_error</parameter> is false.
+ If <parameter>no_error</parameter> is set to true, then the procedure
+ doesn't throw errors. The last result status could be read
+ with <function>pg_wal_replay_wait_status</function>.
+ </para></entry>
+ </row>
+
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
+ <primary>pg_wal_replay_wait_status</primary>
+ </indexterm>
+ <function>pg_wal_replay_wait_status</function> ()
+ <returnvalue>text</returnvalue>
+ </para>
+ <para>
+ Function
+ </para>
+ <para>
+ Returns the last result status for
+ <function>pg_wal_replay_wait</function> procedure. The possible
+ values are <literal>success</literal>, <literal>timeout</literal>,
+ and <literal>not in recovery</literal>.
</para></entry>
</row>
</tbody>
@@ -29044,7 +29073,8 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
<para>
<function>pg_wal_replay_wait</function> should be called on standby.
If a user calls <function>pg_wal_replay_wait</function> on primary, it
- will error out. However, if <function>pg_wal_replay_wait</function> is
+ will error out as soon as <parameter>no_error</parameter> is false.
+ However, if <function>pg_wal_replay_wait</function> is
called on primary promoted from standby and <literal>target_lsn</literal>
was already replayed, then <function>pg_wal_replay_wait</function> just
exits immediately.
@@ -29090,6 +29120,20 @@ postgres=# CALL pg_wal_replay_wait('0/306EE20', 100);
ERROR: timed out while waiting for target LSN 0/306EE20 to be replayed; current replay LSN 0/306EA60
</programlisting>
+ The same example uses <function>pg_wal_replay_wait</function> with
+ <parameter>no_error</parameter> set to true. In this case, the result
+ status must be read with <function>pg_wal_replay_wait_status</function>.
+
+ <programlisting>
+postgres=# CALL pg_wal_replay_wait('0/306EE20', 100, true);
+CALL
+postgres=# SELECT pg_wal_replay_wait_status();
+ pg_wal_replay_wait_status
+---------------------------
+ timeout
+(1 row)
+ </programlisting>
+
</para>
<para>
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index ddca78d3717..c46271f0364 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -751,15 +751,18 @@ pg_promote(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(false);
}
+static WaitLSNResult lastWaitLSNResult = WAIT_LSN_RESULT_SUCCESS;
+
/*
- * Waits until recovery replays the target LSN with optional timeout.
+ * Waits until recovery replays the target LSN with optional timeout. Throw
+ * an error on failure.
*/
Datum
pg_wal_replay_wait(PG_FUNCTION_ARGS)
{
XLogRecPtr target_lsn = PG_GETARG_LSN(0);
int64 timeout = PG_GETARG_INT64(1);
- WaitLSNResult result;
+ bool no_error = PG_GETARG_BOOL(2);
if (timeout < 0)
ereport(ERROR,
@@ -800,13 +803,16 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
*/
Assert(MyProc->xmin == InvalidTransactionId);
- result = WaitForLSNReplay(target_lsn, timeout);
+ lastWaitLSNResult = WaitForLSNReplay(target_lsn, timeout);
+
+ if (no_error)
+ PG_RETURN_VOID();
/*
* Process the result of WaitForLSNReplay(). Throw appropriate error if
* needed.
*/
- switch (result)
+ switch (lastWaitLSNResult)
{
case WAIT_LSN_RESULT_SUCCESS:
/* Nothing to do on success */
@@ -832,3 +838,27 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
PG_RETURN_VOID();
}
+
+Datum
+pg_wal_replay_wait_status(PG_FUNCTION_ARGS)
+{
+ const char *result_string = "";
+
+ /* Process the result of WaitForLSNReplay(). */
+ switch (lastWaitLSNResult)
+ {
+ case WAIT_LSN_RESULT_SUCCESS:
+ result_string = "success";
+ break;
+
+ case WAIT_LSN_RESULT_TIMEOUT:
+ result_string = "timeout";
+ break;
+
+ case WAIT_LSN_RESULT_NOT_IN_RECOVERY:
+ result_string = "not in recovery";
+ break;
+ }
+
+ PG_RETURN_TEXT_P(cstring_to_text(result_string));
+}
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 58fb10aa5a8..8860a9c73da 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -2,7 +2,8 @@
*
* xlogwait.c
* Implements waiting for the given replay LSN, which is used in
- * CALL pg_wal_replay_wait(target_lsn pg_lsn, timeout float8).
+ * CALL pg_wal_replay_wait(target_lsn pg_lsn,
+ * timeout float8, no_error bool).
*
* Copyright (c) 2024, PostgreSQL Global Development Group
*
diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql
index 92b6aefe7aa..3fd1dfa945e 100644
--- a/src/backend/catalog/system_functions.sql
+++ b/src/backend/catalog/system_functions.sql
@@ -414,7 +414,9 @@ CREATE OR REPLACE FUNCTION
json_populate_recordset(base anyelement, from_json json, use_json_as_text boolean DEFAULT false)
RETURNS SETOF anyelement LANGUAGE internal STABLE ROWS 100 AS 'json_populate_recordset' PARALLEL SAFE;
-CREATE OR REPLACE PROCEDURE pg_wal_replay_wait(target_lsn pg_lsn, timeout int8 DEFAULT 0)
+CREATE OR REPLACE PROCEDURE pg_wal_replay_wait(target_lsn pg_lsn,
+ timeout int8 DEFAULT 0,
+ no_error bool DEFAULT false)
LANGUAGE internal AS 'pg_wal_replay_wait';
CREATE OR REPLACE FUNCTION pg_logical_slot_get_changes(
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 7c0b74fe055..4c8adab567f 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6665,8 +6665,13 @@
{ oid => '8593',
descr => 'wait for the target LSN to be replayed on standby with an optional timeout',
proname => 'pg_wal_replay_wait', prokind => 'p', prorettype => 'void',
- proargtypes => 'pg_lsn int8', proargnames => '{target_lsn,timeout}',
+ proargtypes => 'pg_lsn int8 bool', proargnames => '{target_lsn,timeout,no_error}',
prosrc => 'pg_wal_replay_wait' },
+{ oid => '8594',
+ descr => 'the last result for pg_wal_replay_wait()',
+ proname => 'pg_wal_replay_wait_status', prorettype => 'text',
+ proargtypes => '',
+ prosrc => 'pg_wal_replay_wait_status' },
{ oid => '6224', descr => 'get resource managers loaded in system',
proname => 'pg_get_wal_resource_managers', prorows => '50', proretset => 't',
diff --git a/src/test/recovery/t/043_wal_replay_wait.pl b/src/test/recovery/t/043_wal_replay_wait.pl
index 024f1fe6488..7766010ab79 100644
--- a/src/test/recovery/t/043_wal_replay_wait.pl
+++ b/src/test/recovery/t/043_wal_replay_wait.pl
@@ -77,6 +77,18 @@ $node_standby->psql(
ok( $stderr =~ /timed out while waiting for target LSN/,
"get timeout on waiting for unreachable LSN");
+$output = $node_standby->safe_psql('postgres', qq[
+ CALL pg_wal_replay_wait('${lsn2}', 10, true);
+ SELECT pg_wal_replay_wait_status();]);
+ok( $output == "success",
+ "pg_wal_replay_wait_status() returns correct status after successful waiting");
+$output = $node_standby->psql(
+ 'postgres', qq[
+ CALL pg_wal_replay_wait('${lsn2}', 10, true);
+ SELECT pg_wal_replay_wait_status();]);
+ok( $output == "timeout",
+ "pg_wal_replay_wait_status() returns correct status after timeout");
+
# 4. Check that pg_wal_replay_wait() triggers an error if called on primary,
# within another function, or inside a transaction with an isolation level
# higher than READ COMMITTED.
@@ -193,6 +205,13 @@ $node_standby->safe_psql('postgres', "CALL pg_wal_replay_wait('${lsn5}');");
ok(1, 'wait for already replayed LSN exits immediately even after promotion');
+$output = $node_standby->psql(
+ 'postgres', qq[
+ CALL pg_wal_replay_wait('${lsn4}', 10, true);
+ SELECT pg_wal_replay_wait_status();]);
+ok( $output == "not in recovery",
+ "pg_wal_replay_wait_status() returns correct status after standby promotion");
+
$node_standby->stop;
$node_primary->stop;
--
2.39.5 (Apple Git-154)
view thread (50+ messages) latest in thread
Message-ID: <CAPpHfdut8AvtU9PhXBOd0r5LPYkLyPeiWS+sfZ0FRoUBrS3_TQ@mail.gmail.com>
Permalink: ../CAPpHfdut8AvtU9PhXBOd0r5LPYkLyPeiWS+sfZ0FRoUBrS3_TQ@mail.gmail.com/
Also on: postgresql.org/message-id/CAPpHfdut8AvtU9PhXBOd0r5LPYkLyPeiWS+sfZ0FRoUBrS3_TQ@mail.gmail.com
copy link · copy postgr.es · copy git am
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: aekorotkov@gmail.com, peter@eisentraut.org, michael@paquier.xyz
Subject: Re: pgsql: Implement pg_wal_replay_wait() stored procedure
In-Reply-To: <CAPpHfdut8AvtU9PhXBOd0r5LPYkLyPeiWS+sfZ0FRoUBrS3_TQ@mail.gmail.com>
References: <E1sZwuz-002NPQ-Lc@gemulon.postgresql.org> <18c0fa64-0475-415e-a1bd-665d922c5201@eisentraut.org> <CAPpHfduQPYWATA2rfbYXsXM+Yspn1nQVwrEs=p303O428J57Yg@mail.gmail.com> <ZtT4dhlxuxdPPI8t@paquier.xyz> <CAPpHfdubNR746cbxRvs8C0MmNvUTtrjWwZ=RzcB5i_4kUz+RuQ@mail.gmail.com> <1f5b399a-76b1-48e1-87b0-f511e61818f3@eisentraut.org> <CAPpHfdvbcTBk_yuvvg=HvWJeLOWBtdmh1_K4j4btZ70ES_HWuw@mail.gmail.com> <CAPpHfdut8AvtU9PhXBOd0r5LPYkLyPeiWS+sfZ0FRoUBrS3_TQ@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox