From: cca5507 <cca5507@qq.com>
To: Xuneng Zhou <xunengzhou@gmail.com>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Cc: Alexander Korotkov <aekorotkov@gmail.com>
Subject: Re: timeout value overflow in wait for lsn
Date: Mon, 31 Aug 2026 22:06:38 +0800
Message-ID: <tencent_F49415993E3CAA832ABB9C5088C2DFDAF807@qq.com> (raw)
In-Reply-To: <CABPTF7V7R1JT2cr3xZRFWv5MUBgNU8Pjd96aSPGav0BfMwLVXg@mail.gmail.com>
References: <tencent_86B83240785807077600A1778566B5C12908@qq.com>
<CABPTF7Ue4oKmXgqm4-0+KZLrYFSw4O+e1iOpSE1+FbdP-GXUFw@mail.gmail.com>
<CABPTF7UvxPOLPrajRJmKhkfR7ZYJHJGJaJKX7-V8KPViV5RdVA@mail.gmail.com>
<tencent_59EDE0C0CE2334BB30B0D63B0542A6022F05@qq.com>
<CABPTF7VfYoPRVnswmvB+9-VLRtNP2cb5rLM-T4kU+wqnsJNQ+A@mail.gmail.com>
<tencent_F6CA80E0991D68DBE587AA157ED582A5F00A@qq.com>
<CAD21AoC-rMqs0Wxovh0X1+tkd-SDMTh9kMGovb615OAJ8W02xA@mail.gmail.com>
<tencent_E0E142ED4F0A930C349EF9C1B99D5CA84B06@qq.com>
<CAD21AoB3jJsXFPjaRHWC_A26u26DK0=rF9x2SynT6mr_owtTwA@mail.gmail.com>
<tencent_91A9363E2558061D8FEA20D78E3494BF9005@qq.com>
<CABPTF7U3Yo3Tp6MVArwQwsWkWTBUf9j9azgf=oFDuY2btBrCAQ@mail.gmail.com>
<CABPTF7V7R1JT2cr3xZRFWv5MUBgNU8Pjd96aSPGav0BfMwLVXg@mail.gmail.com>
> --- The divergence
> Rounding like this does not make a lot of sense to me in the first
> place, especially when the value rounded down to is zero, which
> disables the timeout in lots of places. I don't know whether users
> have ever been surprised by this behavior and why it was designed like
> this. One reason for this seems to be that the value is small enough
> to do so.
>
> The general GUC doc says:
> fractional values are rounded to the nearest integer if the parameter
> is of integer type.
> If a fractional value is specified with a unit, it will be rounded to
> a multiple of the next smaller unit if there is one.
>
> However, the individual statement_timeout and lock_timeout
> descriptions do not clearly warn that a nonzero or negative spelling
> can round to zero and consequently disable the timeout. They only
> state that zero disables it.
>
> I am wondering whether the timeout in WAIT FOR needs to take a
> different path since it is not constrained with backward
> compatibility.
How about just keeping it consistent with GUC: use parse_int() and
error out if timeout < 0. I didn't see users complaining about this
behavior.
--
Regards,
ChangAo Chen
Attachments:v6-0001-Prevent-overflow-in-WAIT-FOR-LSN-timeout-handling.patch (application/octet-stream, 6.4K) download
From e9110429207558b8977a7ddc768240756a669bfd Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Mon, 31 Aug 2026 21:45:05 +0800
Subject: [PATCH v6] Prevent overflow in WAIT FOR LSN timeout handling
WAIT FOR LSN accepted timeout values up to INT64_MAX milliseconds, but
converting such values to an absolute timestamp could overflow when
multiplying milliseconds by 1000.
Parse timeout values as int and use int throughout the waiting
interface, limiting the valid range to 0..INT_MAX milliseconds. Zero
continues to mean waiting indefinitely.
---
doc/src/sgml/ref/wait_for.sgml | 2 ++
src/backend/access/transam/xlogwait.c | 2 +-
src/backend/commands/repack_worker.c | 4 ++--
src/backend/commands/wait.c | 30 ++++++-------------------
src/include/access/xlogwait.h | 2 +-
src/test/recovery/t/049_wait_for_lsn.pl | 13 +++++++++++
6 files changed, 26 insertions(+), 27 deletions(-)
diff --git a/doc/src/sgml/ref/wait_for.sgml b/doc/src/sgml/ref/wait_for.sgmlindex 01dc2a84a1a..cae4cebbf1f 100644--- a/doc/src/sgml/ref/wait_for.sgml+++ b/doc/src/sgml/ref/wait_for.sgml@@ -152,6 +152,8 @@ WAIT FOR LSN '<replaceable class="parameter">lsn</replaceable>'
milliseconds. Also it might be given as string literal with
integer number of milliseconds or a number with unit
(see <xref linkend="config-setting-names-values"/>).
+ The valid range is <literal>0 .. INT_MAX</literal> milliseconds.+ A value of zero means waiting indefinitely.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.cindex eee90e7f626..2ea8c24a74f 100644--- a/src/backend/access/transam/xlogwait.c+++ b/src/backend/access/transam/xlogwait.c@@ -437,7 +437,7 @@ WaitLSNTypeRequiresRecovery(WaitLSNType t)
* or replica got promoted before the target LSN reached.
*/
WaitLSNResult
-WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN, int64 timeout)+WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN, int timeout)
{
XLogRecPtr currentLSN;
WaitLSNProcInfo *procInfo;
diff --git a/src/backend/commands/repack_worker.c b/src/backend/commands/repack_worker.cindex af7e2a94764..a9870d9c8f2 100644--- a/src/backend/commands/repack_worker.c+++ b/src/backend/commands/repack_worker.c@@ -447,7 +447,7 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
if (record == NULL)
{
- int64 timeout = 0;+ int timeout = 0;
WaitLSNResult res;
/*
@@ -466,7 +466,7 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
* should already have been flushed to disk.
*/
if (!XLogRecPtrIsValid(lsn_upto))
- timeout = 100L;+ timeout = 100;
res = WaitForLSN(WAIT_LSN_TYPE_PRIMARY_FLUSH,
ctx->reader->EndRecPtr + 1,
timeout);
diff --git a/src/backend/commands/wait.c b/src/backend/commands/wait.cindex 9ba4c75021e..23d6b593cb5 100644--- a/src/backend/commands/wait.c+++ b/src/backend/commands/wait.c@@ -35,7 +35,7 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
DestReceiver *dest)
{
XLogRecPtr lsn;
- int64 timeout = 0;+ int timeout = 0;
WaitLSNResult waitLSNResult;
WaitLSNType lsnType = WAIT_LSN_TYPE_STANDBY_REPLAY; /* default */
bool throw = true;
@@ -92,7 +92,6 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
{
char *timeout_str;
const char *hintmsg;
- double dval;
if (timeout_specified)
errorConflictingDefElem(defel, pstate);
@@ -100,33 +99,18 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
timeout_str = defGetString(defel);
- if (!parse_real(timeout_str, &dval, GUC_UNIT_MS, &hintmsg))- {+ if (!parse_int(timeout_str, &timeout, GUC_UNIT_MS, &hintmsg))
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid timeout value: \"%s\"", timeout_str),
- hintmsg ? errhint("%s", _(hintmsg)) : 0);- }-- /*- * Get rid of any fractional part in the input. This is so we- * don't fail on just-out-of-range values that would round into- * range.- */- dval = rint(dval);+ hintmsg ? errhint("%s", _(hintmsg)) : 0,+ parser_errposition(pstate, defel->location));- /* Range check */- if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT64(dval)))- ereport(ERROR,- errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),- errmsg("timeout value is out of range"));-- if (dval < 0)+ if (timeout < 0)
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("timeout cannot be negative"));-- timeout = (int64) dval;+ errmsg("timeout cannot be negative"),+ parser_errposition(pstate, defel->location));
}
else if (strcmp(defel->defname, "no_throw") == 0)
{
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.hindex 07157f220ea..2bf0263e9e2 100644--- a/src/include/access/xlogwait.h+++ b/src/include/access/xlogwait.h@@ -104,6 +104,6 @@ extern XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType);
extern void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN);
extern void WaitLSNCleanup(void);
extern WaitLSNResult WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN,
- int64 timeout);+ int timeout);
#endif /* XLOG_WAIT_H */
diff --git a/src/test/recovery/t/049_wait_for_lsn.pl b/src/test/recovery/t/049_wait_for_lsn.plindex cb7d4d461de..c8644e09af3 100644--- a/src/test/recovery/t/049_wait_for_lsn.pl+++ b/src/test/recovery/t/049_wait_for_lsn.pl@@ -343,6 +343,13 @@ $node_standby->psql(
stderr => \$stderr);
ok($stderr =~ /timeout cannot be negative/, "get error for negative timeout");
+# Test out of range timeout+$node_standby->psql(+ 'postgres',+ "WAIT FOR LSN '${test_lsn}' WITH (timeout '2147483648ms');",+ stderr => \$stderr);+ok($stderr =~ /invalid timeout value: "2147483648ms"/, "get error for out of range timeout");+
# Test unknown parameter with WITH clause
$node_standby->psql(
'postgres',
@@ -407,6 +414,12 @@ $output = $node_standby->safe_psql(
ok($output eq "timeout",
"WAIT FOR WITH clause returns correct timeout status");
+# Test maximum timeout+$output = $node_standby->safe_psql(+ 'postgres', qq[+ WAIT FOR LSN '${lsn2}' WITH (timeout '2147483647ms', no_throw);]);+ok($output eq "success", "maximum timeout value is accepted");+
# Test WITH clause error case - invalid option
$node_standby->psql(
'postgres',
--
2.53.0
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-hackers@postgresql.org
Cc: cca5507@qq.com, xunengzhou@gmail.com, sawada.mshk@gmail.com, aekorotkov@gmail.com
Subject: Re: timeout value overflow in wait for lsn
In-Reply-To: <tencent_F49415993E3CAA832ABB9C5088C2DFDAF807@qq.com>
References: <tencent_86B83240785807077600A1778566B5C12908@qq.com> <CABPTF7Ue4oKmXgqm4-0+KZLrYFSw4O+e1iOpSE1+FbdP-GXUFw@mail.gmail.com> <CABPTF7UvxPOLPrajRJmKhkfR7ZYJHJGJaJKX7-V8KPViV5RdVA@mail.gmail.com> <tencent_59EDE0C0CE2334BB30B0D63B0542A6022F05@qq.com> <CABPTF7VfYoPRVnswmvB+9-VLRtNP2cb5rLM-T4kU+wqnsJNQ+A@mail.gmail.com> <tencent_F6CA80E0991D68DBE587AA157ED582A5F00A@qq.com> <CAD21AoC-rMqs0Wxovh0X1+tkd-SDMTh9kMGovb615OAJ8W02xA@mail.gmail.com> <tencent_E0E142ED4F0A930C349EF9C1B99D5CA84B06@qq.com> <CAD21AoB3jJsXFPjaRHWC_A26u26DK0=rF9x2SynT6mr_owtTwA@mail.gmail.com> <tencent_91A9363E2558061D8FEA20D78E3494BF9005@qq.com> <CABPTF7U3Yo3Tp6MVArwQwsWkWTBUf9j9azgf=oFDuY2btBrCAQ@mail.gmail.com> <CABPTF7V7R1JT2cr3xZRFWv5MUBgNU8Pjd96aSPGav0BfMwLVXg@mail.gmail.com> <tencent_F49415993E3CAA832ABB9C5088C2DFDAF807@qq.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