pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedtimeout value overflow in wait for lsn
25+ messages / 5 participants
[nested] [flat]
Commits from this thread:
2026-09-14 6c44473c2d f260c03bcc Fix timeout overflow in WAIT FOR LSN
* timeout value overflow in wait for lsn
@ 2026-08-19 13:40 cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: cca5507 @ 2026-08-19 13:40 UTC (permalink / raw)
To: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi,
The valid range of the timeout value in wait for lsn command is int64, which can overflow
later in WaitForLSN():
```
postgres=# wait for lsn '99/99999999' with (mode 'primary_flush', timeout '10000000000000000ms');
ERROR: timed out while waiting for target LSN 99/99999999 to be flushed; current primary_flush LSN 0/017CA348
Time: 0.584 ms
```
To fix it, change the valid range to int32, just like deadlock_timeout and many other GUCs.
Thoughts?
--
Regards,
ChangAo Chen
Attachments:
v1-0001-Fix-timeout-value-overflow-in-wait-for-lsn.patch (application/octet-stream, 865B) download
From 86581687a051bd25262f506bc87cc7922aee896b Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Wed, 19 Aug 2026 21:26:43 +0800
Subject: [PATCH v1] Fix timeout value overflow in wait for lsn.
---
src/backend/commands/wait.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/backend/commands/wait.c b/src/backend/commands/wait.c
index 40a6ffde16b..c53f35aff4b 100644
--- a/src/backend/commands/wait.c
+++ b/src/backend/commands/wait.c
@@ -116,7 +116,7 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
dval = rint(dval);
/* Range check */
- if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT64(dval)))
+ if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
ereport(ERROR,
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("timeout value is out of range"));
--
2.34.1
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-08-19 15:09 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-19 15:09 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
Hi ChangAo,
On Wed, Aug 19, 2026 at 9:41 PM cca5507 <cca5507@qq.com> wrote:
>
> Hi,
>
> The valid range of the timeout value in wait for lsn command is int64, which can overflow
> later in WaitForLSN():
>
> ```
> postgres=# wait for lsn '99/99999999' with (mode 'primary_flush', timeout '10000000000000000ms');
> ERROR: timed out while waiting for target LSN 99/99999999 to be flushed; current primary_flush LSN 0/017CA348
> Time: 0.584 ms
> ```
>
> To fix it, change the valid range to int32, just like deadlock_timeout and many other GUCs.
>
> Thoughts?
Good catch. I am not sure about the fix. The likely cause of overflow is:
#define TimestampTzPlusMilliseconds(tz, ms) \
((tz) + ((ms) * (int64) 1000))
values far greater than int32 could be a problem. But does this
warrant a truncation to int32? Yeah, from a pragmatic perspective,
these off-charts values are not expected in practice since the users
don't have and better not have this amount of patience for latency.
But truncating it alone like
+ if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
seems not adequate to me -- the interface supports int 64, it seems
not good to accept it first and then reject it loudly later. If this
change is desired, we might need to change the interface as well.
Another direction is to prevent the overflow while preserving the
current value by checking the timeout with
if (pg_mul_s64_overflow(timeout, USECS_PER_MSEC, &timeout_us) ||
pg_add_s64_overflow(now, timeout_us, &endtime) ||
!IS_VALID_TIMESTAMP(endtime))
but this seems unprecedented for a timeout value. It would be helpful
to hear Alexander's thoughts on this.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
Attachments:
v2-0001-Fix-overflow-in-WAIT-FOR-LSN-timeout-handling.patch (application/octet-stream, 3.0K) download
From 93e69498b78d09261602aa4f4cf7c7c8fe0ee790 Mon Sep 17 00:00:00 2001
From: alterego655 <824662526@qq.com>
Date: Wed, 19 Aug 2026 23:01:53 +0800
Subject: [PATCH v2] Fix overflow in WAIT FOR LSN timeout handling
WAIT FOR accepted timeout values up to int64, but WaitForLSN converted
milliseconds to microseconds without overflow checking. A sufficiently
large value could therefore produce an invalid deadline and an immediate
false timeout.
Limit the timeout to the int range and use int consistently throughout
the WaitForLSN interface, matching PostgreSQL's other timeout facilities.
---
src/backend/access/transam/xlogwait.c | 2 +-
src/backend/commands/wait.c | 6 +++---
src/include/access/xlogwait.h | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index ea1d2afba27..d3606f40acd 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -439,7 +439,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/wait.c b/src/backend/commands/wait.c
index 40a6ffde16b..d401d400e97 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;
@@ -116,7 +116,7 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
dval = rint(dval);
/* Range check */
- if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT64(dval)))
+ if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
ereport(ERROR,
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("timeout value is out of range"));
@@ -126,7 +126,7 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timeout cannot be negative"));
- timeout = (int64) dval;
+ timeout = (int) dval;
}
else if (strcmp(defel->defname, "no_throw") == 0)
{
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 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 */
--
2.39.5 (Apple Git-154)
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-20 01:58 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-20 01:58 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Wed, Aug 19, 2026 at 11:09 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:
>
> Hi ChangAo,
>
> On Wed, Aug 19, 2026 at 9:41 PM cca5507 <cca5507@qq.com> wrote:
> >
> > Hi,
> >
> > The valid range of the timeout value in wait for lsn command is int64, which can overflow
> > later in WaitForLSN():
> >
> > ```
> > postgres=# wait for lsn '99/99999999' with (mode 'primary_flush', timeout '10000000000000000ms');
> > ERROR: timed out while waiting for target LSN 99/99999999 to be flushed; current primary_flush LSN 0/017CA348
> > Time: 0.584 ms
> > ```
> >
> > To fix it, change the valid range to int32, just like deadlock_timeout and many other GUCs.
> >
> > Thoughts?
>
> Good catch. I am not sure about the fix. The likely cause of overflow is:
>
> #define TimestampTzPlusMilliseconds(tz, ms) \
> ((tz) + ((ms) * (int64) 1000))
>
> values far greater than int32 could be a problem. But does this
> warrant a truncation to int32? Yeah, from a pragmatic perspective,
> these off-charts values are not expected in practice since the users
> don't have and better not have this amount of patience for latency.
> But truncating it alone like
>
> + if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
>
> seems not adequate to me -- the interface supports int 64, it seems
> not good to accept it first and then reject it loudly later. If this
> change is desired, we might need to change the interface as well.
> Another direction is to prevent the overflow while preserving the
> current value by checking the timeout with
>
> if (pg_mul_s64_overflow(timeout, USECS_PER_MSEC, &timeout_us) ||
> pg_add_s64_overflow(now, timeout_us, &endtime) ||
> !IS_VALID_TIMESTAMP(endtime))
Sleeping with it for a night, the above writing seems to be somewhat
confusing. Here's a version improved by Sol:
Changing the check to FLOAT8_FITS_IN_INT32 would reject larger values.
If we choose that limit, the timeout variable and the WaitForLSN()
argument should also use int so that the interface matches the
accepted range.
Alternatively, we can preserve the int64 interface and check the
deadline calculation in WaitForLSN():
if (pg_mul_s64_overflow(timeout, USECS_PER_MSEC, &timeout_us) ||
pg_add_s64_overflow(now, timeout_us, &endtime) ||
!IS_VALID_TIMESTAMP(endtime))
-----------------
Another option is to reject values greater than or equal with
INT64_MAX/1000, which seems a bit hacky to me.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-20 03:12 ` cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: cca5507 @ 2026-08-20 03:12 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
> Changing the check to FLOAT8_FITS_IN_INT32 would reject larger values.
> If we choose that limit, the timeout variable and the WaitForLSN()
> argument should also use int so that the interface matches the
> accepted range.
>
> Alternatively, we can preserve the int64 interface and check the
> deadline calculation in WaitForLSN():
>
> if (pg_mul_s64_overflow(timeout, USECS_PER_MSEC, &timeout_us) ||
> pg_add_s64_overflow(now, timeout_us, &endtime) ||
> !IS_VALID_TIMESTAMP(endtime))
>
> -----------------
>
> Another option is to reject values greater than or equal with
> INT64_MAX/1000, which seems a bit hacky to me.
The max timeout value supported by WaitLatch() is INT_MAX, so I think
it's reasonable to limit the range to int32. And I think it's ok to use int64
as the argument in WaitForLSN() because convert int32 to int64 is always
safe.
--
Regards,
ChangAo Chen
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-08-20 04:20 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-20 04:20 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Thu, Aug 20, 2026 at 11:12 AM cca5507 <cca5507@qq.com> wrote:
>
> > Changing the check to FLOAT8_FITS_IN_INT32 would reject larger values.
> > If we choose that limit, the timeout variable and the WaitForLSN()
> > argument should also use int so that the interface matches the
> > accepted range.
> >
> > Alternatively, we can preserve the int64 interface and check the
> > deadline calculation in WaitForLSN():
> >
> > if (pg_mul_s64_overflow(timeout, USECS_PER_MSEC, &timeout_us) ||
> > pg_add_s64_overflow(now, timeout_us, &endtime) ||
> > !IS_VALID_TIMESTAMP(endtime))
> >
> > -----------------
> >
> > Another option is to reject values greater than or equal with
> > INT64_MAX/1000, which seems a bit hacky to me.
>
> The max timeout value supported by WaitLatch() is INT_MAX, so I think
> it's reasonable to limit the range to int32.
We have a loop in the wait infra, which means that the waiter could
fall asleep several times. Each time takes a INT_MAX maximumly, added
up toward a value larger than INT_MAX. That is why I was wondering
whether the bug itself warrants a truncation from 64 to 32. If there
are user needs like absurdly long timeouts, then we better satisfy
them and there're ways to do so. But in my poor imagination, waiting
greater than 25 days seems unlikely in reality. So I voted for the
limitation of the range.
> And I think it's ok to use int64
> as the argument in WaitForLSN() because convert int32 to int64 is always
> safe.
Yeah, it is safe only if we handle the checking/rejection properly.
The wait for infra is also used by repack, which uses timeout as zero
for an indefinite wait and 100 milliseconds for a finite wait. So it
is not vulnerable to edge cases like this. But the infra could have
more potential callers in the future, we better not let them bother
the extra safety if we can deal with it easily. Sorry if this seems
nitpicking to you.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-20 06:17 ` cca5507 <cca5507@qq.com>
2026-08-20 08:08 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 2 replies; 25+ messages in thread
From: cca5507 @ 2026-08-20 06:17 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
> We have a loop in the wait infra, which means that the waiter could
> fall asleep several times. Each time takes a INT_MAX maximumly, added
> up toward a value larger than INT_MAX. That is why I was wondering
> whether the bug itself warrants a truncation from 64 to 32. If there
> are user needs like absurdly long timeouts, then we better satisfy
> them and there're ways to do so. But in my poor imagination, waiting
> greater than 25 days seems unlikely in reality. So I voted for the
> limitation of the range.
>
> > And I think it's ok to use int64
> > as the argument in WaitForLSN() because convert int32 to int64 is always
> > safe.
>
> Yeah, it is safe only if we handle the checking/rejection properly.
> The wait for infra is also used by repack, which uses timeout as zero
> for an indefinite wait and 100 milliseconds for a finite wait. So it
> is not vulnerable to edge cases like this. But the infra could have
> more potential callers in the future, we better not let them bother
> the extra safety if we can deal with it easily. Sorry if this seems
> nitpicking to you.
Make sense to me. The v2 patch forgets to update the type of the timeout
variable in repack worker, fixed in v3.
--
Regards,
ChangAo Chen
Attachments:
v3-0001-Fix-overflow-in-WAIT-FOR-LSN-timeout-handling.patch (application/octet-stream, 3.7K) download
From c8b2073d8f8e5e3be5263c4df6a2822ff2ae499a Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Thu, 20 Aug 2026 14:06:54 +0800
Subject: [PATCH v3] Fix overflow in WAIT FOR LSN timeout handling.
WAIT FOR accepted timeout values up to int64, but WaitForLSN converted
milliseconds to microseconds without overflow checking. A sufficiently
large value could therefore produce an invalid deadline and an immediate
false timeout.
Limit the timeout to the int range and use int consistently throughout
the WaitForLSN interface, matching PostgreSQL's other timeout facilities.
---
src/backend/access/transam/xlogwait.c | 2 +-
src/backend/commands/repack_worker.c | 4 ++--
src/backend/commands/wait.c | 6 +++---
src/include/access/xlogwait.h | 2 +-
4 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 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.c
index 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.c
index 40a6ffde16b..d401d400e97 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;
@@ -116,7 +116,7 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
dval = rint(dval);
/* Range check */
- if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT64(dval)))
+ if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
ereport(ERROR,
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("timeout value is out of range"));
@@ -126,7 +126,7 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timeout cannot be negative"));
- timeout = (int64) dval;
+ timeout = (int) dval;
}
else if (strcmp(defel->defname, "no_throw") == 0)
{
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 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 */
--
2.34.1
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-08-20 08:08 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-25 19:51 ` Re: timeout value overflow in wait for lsn Nathan Bossart <nathandbossart@gmail.com>
1 sibling, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-20 08:08 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Thu, Aug 20, 2026 at 2:17 PM cca5507 <cca5507@qq.com> wrote:
>
> > We have a loop in the wait infra, which means that the waiter could
> > fall asleep several times. Each time takes a INT_MAX maximumly, added
> > up toward a value larger than INT_MAX. That is why I was wondering
> > whether the bug itself warrants a truncation from 64 to 32. If there
> > are user needs like absurdly long timeouts, then we better satisfy
> > them and there're ways to do so. But in my poor imagination, waiting
> > greater than 25 days seems unlikely in reality. So I voted for the
> > limitation of the range.
> >
> > > And I think it's ok to use int64
> > > as the argument in WaitForLSN() because convert int32 to int64 is always
> > > safe.
> >
> > Yeah, it is safe only if we handle the checking/rejection properly.
> > The wait for infra is also used by repack, which uses timeout as zero
> > for an indefinite wait and 100 milliseconds for a finite wait. So it
> > is not vulnerable to edge cases like this. But the infra could have
> > more potential callers in the future, we better not let them bother
> > the extra safety if we can deal with it easily. Sorry if this seems
> > nitpicking to you.
>
> Make sense to me. The v2 patch forgets to update the type of the timeout
> variable in repack worker, fixed in v3.
Thanks. WFM.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 08:08 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-25 19:51 ` Nathan Bossart <nathandbossart@gmail.com>
2026-08-26 13:51 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Nathan Bossart @ 2026-08-25 19:51 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
Does this one deserve a mention on the open items wiki [0]?
[0] https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items
--
nathan
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 08:08 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-25 19:51 ` Re: timeout value overflow in wait for lsn Nathan Bossart <nathandbossart@gmail.com>
@ 2026-08-26 13:51 ` Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 0 replies; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-26 13:51 UTC (permalink / raw)
To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
Hi Nathan,
On Wed, Aug 26, 2026 at 3:51 AM Nathan Bossart <nathandbossart@gmail.com> wrote:
>
> Does this one deserve a mention on the open items wiki [0]?
>
> [0] https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items
This seems to be a simple issue for me. I am not sure what qualifies
one to be listed in that wiki. Apart from this, I don't have access to
edit that page. I sent an email to the RMT team but did not get a
reply yet.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-08-28 07:17 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
1 sibling, 1 reply; 25+ messages in thread
From: Masahiko Sawada @ 2026-08-28 07:17 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Xuneng Zhou <xunengzhou@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Wed, Aug 19, 2026 at 11:18 PM cca5507 <cca5507@qq.com> wrote:
>
> > We have a loop in the wait infra, which means that the waiter could
> > fall asleep several times. Each time takes a INT_MAX maximumly, added
> > up toward a value larger than INT_MAX. That is why I was wondering
> > whether the bug itself warrants a truncation from 64 to 32. If there
> > are user needs like absurdly long timeouts, then we better satisfy
> > them and there're ways to do so. But in my poor imagination, waiting
> > greater than 25 days seems unlikely in reality. So I voted for the
> > limitation of the range.
> >
> > > And I think it's ok to use int64
> > > as the argument in WaitForLSN() because convert int32 to int64 is always
> > > safe.
> >
> > Yeah, it is safe only if we handle the checking/rejection properly.
> > The wait for infra is also used by repack, which uses timeout as zero
> > for an indefinite wait and 100 milliseconds for a finite wait. So it
> > is not vulnerable to edge cases like this. But the infra could have
> > more potential callers in the future, we better not let them bother
> > the extra safety if we can deal with it easily. Sorry if this seems
> > nitpicking to you.
>
> Make sense to me. The v2 patch forgets to update the type of the timeout
> variable in repack worker, fixed in v3.
I found another issue around timeout value handling: if we specify a
timeout in [-0.5, 0.5], the WAIT FOR command waits forever. A negative
timeout in [-0.5, 0) should be rejected. ISTM a timeout in (0, 0.5] is
rounded down to 0, disabling the timeout essentially, which would
surprise users. I think we can either round up timeout in (0, 1] to 1,
or reject sub-millisecond values. I think we can fix both in the same
patch that fixes the overflow issue.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-08-28 09:11 ` cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: cca5507 @ 2026-08-28 09:11 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Xuneng Zhou <xunengzhou@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
> I found another issue around timeout value handling: if we specify a
> timeout in [-0.5, 0.5], the WAIT FOR command waits forever. A negative
> timeout in [-0.5, 0) should be rejected. ISTM a timeout in (0, 0.5] is
> rounded down to 0, disabling the timeout essentially, which would
> surprise users. I think we can either round up timeout in (0, 1] to 1,
> or reject sub-millisecond values. I think we can fix both in the same
> patch that fixes the overflow issue.
Good catch! Fixed by moving the negative check before rint() and rounding
timeout in (0, 1) to 1.
Please see the v4 patch.
--
Regards,
ChangAo Chen
Attachments:
v4-0001-Fix-WAIT-FOR-LSN-timeout-handling.patch (application/octet-stream, 4.3K) download
From 41035d97254e06582903aff2d569cca5f0c52263 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Fri, 28 Aug 2026 17:01:23 +0800
Subject: [PATCH v4] Fix WAIT FOR LSN timeout handling.
Limit WAIT FOR LSN timeouts to the int range and update the
WaitForLSN() interface and its callers accordingly. This prevents
large timeout values from overflowing while calculating the deadline.
Check for negative values before rounding, so negative sub-millisecond
timeouts cannot be rounded to zero and interpreted as an indefinite
wait. Likewise, round positive values smaller than one millisecond up
to one millisecond, since zero means waiting indefinitely.
---
src/backend/access/transam/xlogwait.c | 2 +-
src/backend/commands/repack_worker.c | 4 ++--
src/backend/commands/wait.c | 24 ++++++++++++++----------
src/include/access/xlogwait.h | 2 +-
4 files changed, 18 insertions(+), 14 deletions(-)
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 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.c
index 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.c
index 9ba4c75021e..2f4b91a7c48 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;
@@ -108,25 +108,29 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
hintmsg ? errhint("%s", _(hintmsg)) : 0);
}
+ if (dval < 0.0)
+ ereport(ERROR,
+ errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("timeout cannot be negative"));
+
/*
* 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.
+ * range. Round values in (0, 1) up to 1 to avoid treating them as
+ * zero, which means waiting indefinitely.
*/
- dval = rint(dval);
+ if (dval > 0.0 && dval < 1.0)
+ dval = 1.0;
+ else
+ dval = rint(dval);
/* Range check */
- if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT64(dval)))
+ if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
ereport(ERROR,
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("timeout value is out of range"));
- if (dval < 0)
- ereport(ERROR,
- errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("timeout cannot be negative"));
-
- timeout = (int64) dval;
+ timeout = (int) dval;
}
else if (strcmp(defel->defname, "no_throw") == 0)
{
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 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 */
--
2.53.0
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-08-28 19:11 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 25+ messages in thread
From: Masahiko Sawada @ 2026-08-28 19:11 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Xuneng Zhou <xunengzhou@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Fri, Aug 28, 2026 at 2:11 AM cca5507 <cca5507@qq.com> wrote:
>
> > I found another issue around timeout value handling: if we specify a
> > timeout in [-0.5, 0.5], the WAIT FOR command waits forever. A negative
> > timeout in [-0.5, 0) should be rejected. ISTM a timeout in (0, 0.5] is
> > rounded down to 0, disabling the timeout essentially, which would
> > surprise users. I think we can either round up timeout in (0, 1] to 1,
> > or reject sub-millisecond values. I think we can fix both in the same
> > patch that fixes the overflow issue.
>
> Good catch! Fixed by moving the negative check before rint() and rounding
> timeout in (0, 1) to 1.
>
> Please see the v4 patch.
Thank you for updating the patch! Here are review comments:
+ if (dval < 0.0)
+ ereport(ERROR,
+ errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("timeout cannot be negative"));
Let's add parser_errposition() here.
Probably we can add the same to other ereport(ERROR) handling a
timeout option value.
---
/*
* 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.
+ * range. Round values in (0, 1) up to 1 to avoid treating them as
+ * zero, which means waiting indefinitely.
*/
- dval = rint(dval);
+ if (dval > 0.0 && dval < 1.0)
+ dval = 1.0;
+ else
+ dval = rint(dval);
The first paragraph is for the else branch whereas the second
paragraph is for the if branch. I think we can write these comments
separately in each branch instead.
---
The documentation says "The timeout might be given as integer number
of milliseconds. Also it might be given as string literal with integer
number of milliseconds or a number with unit (see Section 19.1.1).",
which seems incorrect to me as we parse the timeout value using
parse_real(), clearly accepting real values. Or should we have used
parse_int() in the first place?
Also, I think it's better to mention the maximum value accepted as a
timeout value.
---
I think it's better to add regression tests for the timeout option.
049_wait_for_lsn.pl would be a good place to have them.
---
The patch needs to run pgindent.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-08-29 10:02 ` cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: cca5507 @ 2026-08-29 10:02 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Xuneng Zhou <xunengzhou@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
> Thank you for updating the patch! Here are review comments:
>
> + if (dval < 0.0)
> + ereport(ERROR,
> + errcode(ERRCODE_INVALID_PARAMETER_VALUE),
> + errmsg("timeout cannot be negative"));
>
> Let's add parser_errposition() here.
>
> Probably we can add the same to other ereport(ERROR) handling a
> timeout option value.
Fixed.
> ---
> /*
> * 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.
> + * range. Round values in (0, 1) up to 1 to avoid treating them as
> + * zero, which means waiting indefinitely.
> */
> - dval = rint(dval);
> + if (dval > 0.0 && dval < 1.0)
> + dval = 1.0;
> + else
> + dval = rint(dval);
>
> The first paragraph is for the else branch whereas the second
> paragraph is for the if branch. I think we can write these comments
> separately in each branch instead.
Fixed.
> ---
> The documentation says "The timeout might be given as integer number
> of milliseconds. Also it might be given as string literal with integer
> number of milliseconds or a number with unit (see Section 19.1.1).",
> which seems incorrect to me as we parse the timeout value using
> parse_real(), clearly accepting real values. Or should we have used
> parse_int() in the first place?
I think it's ok to use parse_real() here because parse_int() also accepts
real values.
> Also, I think it's better to mention the maximum value accepted as a
> timeout value.
Fixed.
> ---
> I think it's better to add regression tests for the timeout option.
> 049_wait_for_lsn.pl would be a good place to have them.
Fixed.
> ---
> The patch needs to run pgindent.
Fixed.
Please see the v5 patch.
--
Regards,
ChangAo Chen
Attachments:
v5-0001-Fix-WAIT-FOR-LSN-timeout-handling.patch (application/octet-stream, 7.3K) download
From 5a14d4d4f4885b60051693dd7af93c80268520e6 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Fri, 28 Aug 2026 17:01:23 +0800
Subject: [PATCH v5] Fix WAIT FOR LSN timeout handling.
Limit WAIT FOR LSN timeouts to the int range and update the
WaitForLSN() interface and its callers accordingly. This prevents
large timeout values from overflowing while calculating the deadline.
Check for negative values before rounding, so negative sub-millisecond
timeouts cannot be rounded to zero and interpreted as an indefinite
wait. Likewise, round positive values smaller than one millisecond up
to one millisecond, since zero means 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 | 46 ++++++++++++++++---------
src/include/access/xlogwait.h | 2 +-
src/test/recovery/t/049_wait_for_lsn.pl | 27 +++++++++++++++
6 files changed, 63 insertions(+), 20 deletions(-)
diff --git a/doc/src/sgml/ref/wait_for.sgml b/doc/src/sgml/ref/wait_for.sgml
index 01dc2a84a1a..9177d29c75a 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 from 0 to 2,147,483,647 milliseconds, inclusive.
+ 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.c
index 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.c
index 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.c
index 9ba4c75021e..36b83c62250 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;
@@ -105,28 +105,42 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid timeout value: \"%s\"", timeout_str),
- hintmsg ? errhint("%s", _(hintmsg)) : 0);
+ hintmsg ? errhint("%s", _(hintmsg)) : 0,
+ parser_errposition(pstate, defel->location));
}
- /*
- * 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);
+ if (dval < 0.0)
+ ereport(ERROR,
+ errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("timeout cannot be negative"),
+ parser_errposition(pstate, defel->location));
+
+ if (dval > 0.0 && dval < 1.0)
+ {
+ /*
+ * Round values in (0, 1) up to 1 to avoid treating them as
+ * zero, which means waiting indefinitely.
+ */
+ dval = 1.0;
+ }
+ else
+ {
+ /*
+ * 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);
+ }
/* Range check */
- if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT64(dval)))
+ if (unlikely(isnan(dval) || !FLOAT8_FITS_IN_INT32(dval)))
ereport(ERROR,
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("timeout value is out of range"));
-
- if (dval < 0)
- ereport(ERROR,
- errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("timeout cannot be negative"));
+ errmsg("timeout value is out of range"),
+ parser_errposition(pstate, defel->location));
- timeout = (int64) dval;
+ timeout = (int) dval;
}
else if (strcmp(defel->defname, "no_throw") == 0)
{
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 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.pl
index cb7d4d461de..758a2e20fb6 100644
--- a/src/test/recovery/t/049_wait_for_lsn.pl
+++ b/src/test/recovery/t/049_wait_for_lsn.pl
@@ -343,6 +343,20 @@ $node_standby->psql(
stderr => \$stderr);
ok($stderr =~ /timeout cannot be negative/, "get error for negative timeout");
+# Test negative timeout in [-0.5, 0)
+$node_standby->psql(
+ 'postgres',
+ "WAIT FOR LSN '${test_lsn}' WITH (timeout '-0.1ms');",
+ stderr => \$stderr);
+ok($stderr =~ /timeout cannot be negative/, "get error for negative timeout");
+
+# Test timeout out of range
+$node_standby->psql(
+ 'postgres',
+ "WAIT FOR LSN '${test_lsn}' WITH (timeout '2147483648ms');",
+ stderr => \$stderr);
+ok($stderr =~ /timeout value is out of range/, "get error for out of range timeout");
+
# Test unknown parameter with WITH clause
$node_standby->psql(
'postgres',
@@ -407,6 +421,19 @@ $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 sub-millisecond timeout doesn't become an indefinite wait
+$output = $node_standby->safe_psql(
+ 'postgres', qq[
+ SET statement_timeout = '1s';
+ WAIT FOR LSN '${lsn3}' WITH (timeout '0.1ms', no_throw);]);
+ok($output eq "timeout", "sub-millisecond timeout works correctly");
+
# Test WITH clause error case - invalid option
$node_standby->psql(
'postgres',
--
2.53.0
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-08-30 03:04 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-30 03:04 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
Hi Changao, Sawada-san,
On Sat, Aug 29, 2026 at 6:02 PM cca5507 <cca5507@qq.com> wrote:
>
> > Thank you for updating the patch! Here are review comments:
> >
> > + if (dval < 0.0)
> > + ereport(ERROR,
> > + errcode(ERRCODE_INVALID_PARAMETER_VALUE),
> > + errmsg("timeout cannot be negative"));
> >
> > Let's add parser_errposition() here.
> >
> > Probably we can add the same to other ereport(ERROR) handling a
> > timeout option value.
>
> Fixed.
>
> > ---
> > /*
> > * 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.
> > + * range. Round values in (0, 1) up to 1 to avoid treating them as
> > + * zero, which means waiting indefinitely.
> > */
> > - dval = rint(dval);
> > + if (dval > 0.0 && dval < 1.0)
> > + dval = 1.0;
> > + else
> > + dval = rint(dval);
> >
> > The first paragraph is for the else branch whereas the second
> > paragraph is for the if branch. I think we can write these comments
> > separately in each branch instead.
>
> Fixed.
>
> > ---
> > The documentation says "The timeout might be given as integer number
> > of milliseconds. Also it might be given as string literal with integer
> > number of milliseconds or a number with unit (see Section 19.1.1).",
> > which seems incorrect to me as we parse the timeout value using
> > parse_real(), clearly accepting real values. Or should we have used
> > parse_int() in the first place?
>
> I think it's ok to use parse_real() here because parse_int() also accepts
> real values.
I think there's still subtlety regarding the use of parse_real() or
parse_int() and how to handle fractional values. I'll reply later for
this.
> > Also, I think it's better to mention the maximum value accepted as a
> > timeout value.
>
> Fixed.
Mentioning the max value in doc seems useful to me, despite no
precedents of timeout have done so even if they share the same
capping.
+ The valid range is from 0 to 2,147,483,647 milliseconds, inclusive.
+ A value of zero means waiting indefinitely.
2,147,483,647 milliseconds seems ok for agents to read but not very
interpretable to humans. I doubt that few people would actually type
it manually. The main use here seems to let users have a vague concept
of the max value, so it might be helpful to convert that value to
something that humans can read like xx days.
> > ---
> > I think it's better to add regression tests for the timeout option.
> > 049_wait_for_lsn.pl would be a good place to have them.
>
> Fixed.
>
> > ---
> > The patch needs to run pgindent.
>
> Fixed.
>
> Please see the v5 patch.
>
> --
> Regards,
> ChangAo Chen
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-31 01:47 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 07:05 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
0 siblings, 2 replies; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-31 01:47 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Sun, Aug 30, 2026 at 11:04 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:
>
> Hi Changao, Sawada-san,
>
> On Sat, Aug 29, 2026 at 6:02 PM cca5507 <cca5507@qq.com> wrote:
> >
> > > Thank you for updating the patch! Here are review comments:
> > >
> > > + if (dval < 0.0)
> > > + ereport(ERROR,
> > > + errcode(ERRCODE_INVALID_PARAMETER_VALUE),
> > > + errmsg("timeout cannot be negative"));
> > >
> > > Let's add parser_errposition() here.
> > >
> > > Probably we can add the same to other ereport(ERROR) handling a
> > > timeout option value.
> >
> > Fixed.
> >
> > > ---
> > > /*
> > > * 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.
> > > + * range. Round values in (0, 1) up to 1 to avoid treating them as
> > > + * zero, which means waiting indefinitely.
> > > */
> > > - dval = rint(dval);
> > > + if (dval > 0.0 && dval < 1.0)
> > > + dval = 1.0;
> > > + else
> > > + dval = rint(dval);
> > >
> > > The first paragraph is for the else branch whereas the second
> > > paragraph is for the if branch. I think we can write these comments
> > > separately in each branch instead.
> >
> > Fixed.
> >
> > > ---
> > > The documentation says "The timeout might be given as integer number
> > > of milliseconds. Also it might be given as string literal with integer
> > > number of milliseconds or a number with unit (see Section 19.1.1).",
> > > which seems incorrect to me as we parse the timeout value using
> > > parse_real(), clearly accepting real values. Or should we have used
> > > parse_int() in the first place?
> >
> > I think it's ok to use parse_real() here because parse_int() also accepts
> > real values.
>
> I think there's still subtlety regarding the use of parse_real() or
> parse_int() and how to handle fractional values. I'll reply later for
> this.
--- The issue
Patch v5 does not fully prevent nonzero timeouts from becoming
indefinite waits. For example:
timeout '0.0001s' → 0.0ms
timeout '-0.0001s' → -0.0ms
Both bypass the new positive/negative checks and become int zero. The
added 0.1ms tests miss this because that spelling retains microsecond
precision during conversion.
postgres=# \timing on
Timing is on.
postgres=# SET statement_timeout = '1s';
SET
Time: 4.071 ms
postgres=#
postgres=# WAIT FOR LSN 'FFFFFFFF/FFFFFFFE'
postgres-# WITH (timeout '0.0001s', no_throw);
ERROR: canceling statement due to statement timeout
Time: 1002.685 ms (00:01.003)
postgres=# WAIT FOR LSN 'FFFFFFFF/FFFFFFFE'
postgres-# WITH (timeout '-0.0001s', no_throw);
ERROR: canceling statement due to statement timeout
Time: 1001.470 ms (00:01.001)
The commands above are canceled for statement timeout rather than the
timeout of WAIT FOR.
--- The likely cause
The main cause for this seems to be parse_real() calls
convert_to_base_unit(), which follows GUC semantics and rounds
fractional values to the next smaller unit. For a value expressed in
seconds, conversion rounds to whole milliseconds before returning:
0.0001s → 0.1ms → 0.0ms
convert_to_base_unit:
for (int i = 0; *table[i].unit; i++)
{
if (base_unit == table[i].base_unit &&
strcmp(unitstr, table[i].unit) == 0)
{
double cvalue = value * table[i].multiplier;
/*
* If the user gave a fractional value such as "30.1GB", round it
* off to the nearest multiple of the next smaller unit, if there
* is one.
*/
if (*table[i + 1].unit &&
base_unit == table[i + 1].base_unit)
cvalue = rint(cvalue / table[i + 1].multiplier) *
table[i + 1].multiplier;
*base_value = cvalue;
return true;
}
}
For 0.0001s, substitute the table values:
value = 0.0001;
table[i].multiplier = 1000; /* seconds → milliseconds */
table[i + 1].multiplier = 1; /* next smaller unit is ms */
cvalue = value * 1000;
= 0.1;
cvalue = rint(cvalue / 1) * 1;
= rint(0.1);
= 0.0;
Thus information is already lost before v5 checks dval < 0.0 or dval >
0.0. Negative zero compares neither less nor greater than zero, so
additional conditions on the returned dval cannot recover the original
value.
This issue seems also to be true for parse_int since it also makes use
of convert_to_base_unit.
Moreover, even 0.1ms, which parse_real() returns as 0.1, is rounded by
parse_int() before the caller sees it:
parse_int("0.1ms", GUC_UNIT_MS)
0.1ms → 0.1ms # unit conversion preserves it
→ rint(0.1)
→ 0
The relevant code is:
if (!convert_to_base_unit(val,
endptr, (flags & GUC_UNIT),
&val))
...
/* Round to int, then check for overflow */
val = rint(val);
if (result)
*result = (int) val;
--- 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.
> > > Also, I think it's better to mention the maximum value accepted as a
> > > timeout value.
> >
> > Fixed.
>
> Mentioning the max value in doc seems useful to me, despite no
> precedents of timeout have done so even if they share the same
> capping.
>
> + The valid range is from 0 to 2,147,483,647 milliseconds, inclusive.
> + A value of zero means waiting indefinitely.
>
> 2,147,483,647 milliseconds seems ok for agents to read but not very
> interpretable to humans. I doubt that few people would actually type
> it manually. The main use here seems to let users have a vague concept
> of the max value, so it might be helpful to convert that value to
> something that humans can read like xx days.
>
>
> > > ---
> > > I think it's better to add regression tests for the timeout option.
> > > 049_wait_for_lsn.pl would be a good place to have them.
> >
> > Fixed.
> >
> > > ---
> > > The patch needs to run pgindent.
> >
> > Fixed.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-31 07:05 ` Xuneng Zhou <xunengzhou@gmail.com>
1 sibling, 0 replies; 25+ messages in thread
From: Xuneng Zhou @ 2026-08-31 07:05 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>
On Mon, Aug 31, 2026 at 9:47 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:
>
> On Sun, Aug 30, 2026 at 11:04 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:
> >
> > Hi Changao, Sawada-san,
> >
> > On Sat, Aug 29, 2026 at 6:02 PM cca5507 <cca5507@qq.com> wrote:
> > >
> > > > Thank you for updating the patch! Here are review comments:
> > > >
> > > > + if (dval < 0.0)
> > > > + ereport(ERROR,
> > > > + errcode(ERRCODE_INVALID_PARAMETER_VALUE),
> > > > + errmsg("timeout cannot be negative"));
> > > >
> > > > Let's add parser_errposition() here.
> > > >
> > > > Probably we can add the same to other ereport(ERROR) handling a
> > > > timeout option value.
> > >
> > > Fixed.
> > >
> > > > ---
> > > > /*
> > > > * 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.
> > > > + * range. Round values in (0, 1) up to 1 to avoid treating them as
> > > > + * zero, which means waiting indefinitely.
> > > > */
> > > > - dval = rint(dval);
> > > > + if (dval > 0.0 && dval < 1.0)
> > > > + dval = 1.0;
> > > > + else
> > > > + dval = rint(dval);
> > > >
> > > > The first paragraph is for the else branch whereas the second
> > > > paragraph is for the if branch. I think we can write these comments
> > > > separately in each branch instead.
> > >
> > > Fixed.
> > >
> > > > ---
> > > > The documentation says "The timeout might be given as integer number
> > > > of milliseconds. Also it might be given as string literal with integer
> > > > number of milliseconds or a number with unit (see Section 19.1.1).",
> > > > which seems incorrect to me as we parse the timeout value using
> > > > parse_real(), clearly accepting real values. Or should we have used
> > > > parse_int() in the first place?
> > >
> > > I think it's ok to use parse_real() here because parse_int() also accepts
> > > real values.
> >
> > I think there's still subtlety regarding the use of parse_real() or
> > parse_int() and how to handle fractional values. I'll reply later for
> > this.
>
> --- The issue
> Patch v5 does not fully prevent nonzero timeouts from becoming
> indefinite waits. For example:
> timeout '0.0001s' → 0.0ms
> timeout '-0.0001s' → -0.0ms
>
> Both bypass the new positive/negative checks and become int zero. The
> added 0.1ms tests miss this because that spelling retains microsecond
> precision during conversion.
>
> postgres=# \timing on
> Timing is on.
> postgres=# SET statement_timeout = '1s';
> SET
> Time: 4.071 ms
> postgres=#
> postgres=# WAIT FOR LSN 'FFFFFFFF/FFFFFFFE'
> postgres-# WITH (timeout '0.0001s', no_throw);
> ERROR: canceling statement due to statement timeout
> Time: 1002.685 ms (00:01.003)
> postgres=# WAIT FOR LSN 'FFFFFFFF/FFFFFFFE'
> postgres-# WITH (timeout '-0.0001s', no_throw);
> ERROR: canceling statement due to statement timeout
> Time: 1001.470 ms (00:01.001)
>
> The commands above are canceled for statement timeout rather than the
> timeout of WAIT FOR.
>
> --- The likely cause
> The main cause for this seems to be parse_real() calls
> convert_to_base_unit(), which follows GUC semantics and rounds
> fractional values to the next smaller unit. For a value expressed in
> seconds, conversion rounds to whole milliseconds before returning:
>
> 0.0001s → 0.1ms → 0.0ms
>
> convert_to_base_unit:
>
> for (int i = 0; *table[i].unit; i++)
> {
> if (base_unit == table[i].base_unit &&
> strcmp(unitstr, table[i].unit) == 0)
> {
> double cvalue = value * table[i].multiplier;
>
> /*
> * If the user gave a fractional value such as "30.1GB", round it
> * off to the nearest multiple of the next smaller unit, if there
> * is one.
> */
> if (*table[i + 1].unit &&
> base_unit == table[i + 1].base_unit)
> cvalue = rint(cvalue / table[i + 1].multiplier) *
> table[i + 1].multiplier;
>
> *base_value = cvalue;
> return true;
> }
> }
>
> For 0.0001s, substitute the table values:
> value = 0.0001;
> table[i].multiplier = 1000; /* seconds → milliseconds */
> table[i + 1].multiplier = 1; /* next smaller unit is ms */
>
> cvalue = value * 1000;
> = 0.1;
>
> cvalue = rint(cvalue / 1) * 1;
> = rint(0.1);
> = 0.0;
>
> Thus information is already lost before v5 checks dval < 0.0 or dval >
> 0.0. Negative zero compares neither less nor greater than zero, so
> additional conditions on the returned dval cannot recover the original
> value.
>
> This issue seems also to be true for parse_int since it also makes use
> of convert_to_base_unit.
>
> Moreover, even 0.1ms, which parse_real() returns as 0.1, is rounded by
> parse_int() before the caller sees it:
> parse_int("0.1ms", GUC_UNIT_MS)
> 0.1ms → 0.1ms # unit conversion preserves it
> → rint(0.1)
> → 0
> The relevant code is:
> if (!convert_to_base_unit(val,
> endptr, (flags & GUC_UNIT),
> &val))
> ...
>
> /* Round to int, then check for overflow */
> val = rint(val);
>
> if (result)
> *result = (int) val;
>
> --- 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.
Here's a further investigation report from Sol with instructions but
*without* much audit from me -- it may hallucinate in some places. It
seems that materials gathered so far deserve a separate treatment.
> > > > Also, I think it's better to mention the maximum value accepted as a
> > > > timeout value.
> > >
> > > Fixed.
> >
> > Mentioning the max value in doc seems useful to me, despite no
> > precedents of timeout have done so even if they share the same
> > capping.
> >
> > + The valid range is from 0 to 2,147,483,647 milliseconds, inclusive.
> > + A value of zero means waiting indefinitely.
> >
> > 2,147,483,647 milliseconds seems ok for agents to read but not very
> > interpretable to humans. I doubt that few people would actually type
> > it manually. The main use here seems to let users have a vague concept
> > of the max value, so it might be helpful to convert that value to
> > something that humans can read like xx days.
> >
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
Attachments:
timeout-rounding-report.md (text/markdown, 21.6K) download
# Time-unit GUC rounding can bypass range checks and change zero-sentinel semantics
## Summary and reproduction
PostgreSQL rounds time-unit GUC input before checking the parameter's declared
range. A nonzero duration can therefore become zero before validation. The
result is both sign-sensitive and spelling-dependent: a negative value below
the declared minimum can be accepted as zero, while a positive timeout can
silently turn into a disabled timeout or another zero-sentinel behavior.
A minimal reproduction is:
```sql
SET statement_timeout = '0.0001d'; SHOW statement_timeout; -- 0, although 0.0001d is 8.64 seconds
SET statement_timeout = '-0.0001d'; SHOW statement_timeout; -- 0, although the declared minimum is 0
SET statement_timeout = '-1ms'; -- ERROR: outside the valid range
```
Thus a negative 8.64-second value is accepted as zero, while negative one
millisecond is rejected. The first line also changes a requested positive
8.64-second timeout into zero, which disables `statement_timeout`.
The same mechanism affects other time-unit GUCs. Whether the resulting zero
is merely lossy or operationally dangerous depends on the parameter's minimum
and on what zero means to its consumer.
## Shared mechanism and the unit-spelling invariant
The tracked source of the parameter definitions is
[`src/backend/utils/misc/guc_parameters.dat`](src/backend/utils/misc/guc_parameters.dat).
The parsing and conversion logic is in
[`src/backend/utils/misc/guc.c`](src/backend/utils/misc/guc.c).
### First rounding stage: the next smaller unit
`convert_to_base_unit()` first converts the numeric value to the parameter's
base unit:
```c
double cvalue = value * table[i].multiplier;
```
It then rounds the result to a multiple of the next smaller unit:
```c
if (*table[i + 1].unit &&
base_unit == table[i + 1].base_unit)
cvalue = rint(cvalue / table[i + 1].multiplier) *
table[i + 1].multiplier;
```
Under the default round-to-nearest, ties-to-even mode, these nonzero
magnitudes can become zero during this first stage:
| Input suffix | Rounding quantum | Nonzero magnitude that can become zero | Physical duration |
|---|---|---:|---:|
| `d` | one hour | `abs(value) <= 1/48 d` | up to 30 minutes |
| `h` | one minute | `abs(value) <= 1/120 h` | up to 30 seconds |
| `min` | one second | `abs(value) <= 1/120 min` | up to 500 milliseconds |
| `s` | one millisecond | `abs(value) <= 0.0005 s` | up to 0.5 milliseconds |
| `ms` | one microsecond | `abs(value) <= 0.0005 ms` | up to 0.5 microseconds |
| `us` | no smaller table entry | none at this stage | none at this stage |
The same windows apply on the negative side. For example:
```text
0.0001d -> 0 hours -> 0
-0.0001d -> -0 hours -> -0.0
0.001h -> 0 minutes -> 0
-0.001h -> -0 minutes -> -0.0
```
### Second rounding stage: integer GUC storage
For an integer GUC, `parse_int()` performs a final rounding after unit
conversion:
```c
val = rint(val);
```
The parameter-specific minimum and maximum are checked only after this value
has been converted to an integer. The nominal final zero windows are:
| Integer base unit | Converted magnitude that can become integer zero |
|---|---:|
| milliseconds | up to 0.5 milliseconds |
| seconds | up to 0.5 seconds |
| minutes | up to 0.5 minutes |
The two stages compose. For a millisecond GUC, for example, an input written
as `0.1ms` becomes zero at the final integer rounding, while `0.001min`
becomes zero earlier by rounding 0.06 seconds to zero seconds. The latter is
a physical duration of 60 milliseconds, well above the final half-millisecond
window.
Values very close to a half-unit boundary can also be affected by double
rounding. The exact floating-point edge depends on representation and the
active rounding mode; the tables state the nominal mathematical boundaries.
### Unit-spelling invariant
The first-stage loss window is a property of the input spelling, not of the
parameter. The suffix selects the next-smaller-unit rounding quantum before
the parameter's integer base unit adds the second rounding stage.
For example, `0.001h` is first rounded to a whole number of minutes whether
the target parameter is millisecond-based or second-based:
```text
0.001h = 3.6 seconds = 0.06 minutes -> 0 minutes -> 0
```
Equivalent physical durations can therefore behave differently:
```text
statement_timeout = '0.001h' -> 0 ms
statement_timeout = '3.6s' -> 3600 ms
statement_timeout = '3600ms' -> 3600 ms
```
## 1. Negative values can bypass declared minimums
### Parameters with `min = 0`
For a parameter whose minimum is zero, every negative input should be below
the declared range. If conversion produces `-0.0`, however, the final stored
integer is zero and the range check succeeds.
```sql
SET statement_timeout = '-0.0001d';
SHOW statement_timeout;
```
Result:
```text
0
```
The original value is -8.64 seconds, but rounding days to whole hours erases
both its magnitude and its effective sign before the minimum is checked.
The following time-unit GUCs have `min = 0`. All can receive zero from a
negative input within one of the relevant loss windows. The operational
effect depends on the meaning of zero.
| Parameter | Type and base unit | Meaning of zero |
|---|---|---|
| `archive_timeout` | integer seconds | disable forced WAL switches |
| `client_connection_check_interval` | integer milliseconds | disable connection checks |
| `idle_in_transaction_session_timeout` | integer milliseconds | disable the timeout |
| `idle_replication_slot_timeout` | integer seconds | disable idle-slot invalidation |
| `idle_session_timeout` | integer milliseconds | disable the timeout |
| `io_worker_idle_timeout` | integer milliseconds | immediate idle deadline |
| `lock_timeout` | integer milliseconds | disable the timeout |
| `statement_timeout` | integer milliseconds | disable the timeout |
| `tcp_user_timeout` | integer milliseconds | use the operating-system default |
| `transaction_timeout` | integer milliseconds | disable the timeout |
| `vacuum_cost_delay` | real milliseconds | disable cost-based vacuum delay |
| `wal_receiver_timeout` | integer milliseconds | disable the timeout |
| `wal_sender_timeout` | integer milliseconds | disable the timeout |
Other time-unit GUCs with `min = 0` are affected by the same range-check
ordering even though they are not timeout controls:
- `checkpoint_warning`
- `io_worker_launch_interval`
- `log_rotation_age`
- `log_startup_progress_interval`
- `password_expiration_warning_threshold`
- `recovery_min_apply_delay`
- `tcp_keepalives_idle`
- `tcp_keepalives_interval`
- `wal_receiver_status_interval`
- `wal_summary_keep_time`
### Parameters with `min = -1`
Several time-unit GUCs reserve `-1` for a special behavior. They can accept a
value far below `-1` if a coarse input suffix rounds it to zero before the
range check.
For example:
```sql
SET log_min_duration_statement = '-0.001h';
SHOW log_min_duration_statement;
```
Result:
```text
0
```
`-0.001h` is `-3600ms`, below the declared `-1ms` minimum. It nevertheless
becomes zero because -0.001 hours rounds to zero whole minutes. The semantic
result is also reversed: `-1` disables duration logging, while `0` logs every
statement duration.
The affected `min = -1` time-unit GUCs are:
| Parameter | Type and base unit | Meaning of `-1` | Meaning of `0` |
|---|---|---|---|
| `autovacuum_vacuum_cost_delay` | real milliseconds | inherit `vacuum_cost_delay` | disable cost-based delay |
| `log_autoanalyze_min_duration` | integer milliseconds | disable logging | log every qualifying action |
| `log_autovacuum_min_duration` | integer milliseconds | disable logging | log every qualifying action |
| `log_min_duration_sample` | integer milliseconds | disable sampling | sample all statements |
| `log_min_duration_statement` | integer milliseconds | disable duration logging | log every statement duration |
| `max_standby_archive_delay` | integer milliseconds | wait forever | no grace period before cancellation |
| `max_standby_streaming_delay` | integer milliseconds | wait forever | no grace period before cancellation |
| `wal_sender_shutdown_timeout` | integer milliseconds | wait indefinitely for the receiver | do not wait for the receiver |
### Parameters protected from the zero case
Integer timeouts with a positive minimum reject a value that has become zero:
| Parameter | Base unit | Minimum |
|---|---|---:|
| `authentication_timeout` | seconds | 1 second |
| `deadlock_timeout` | milliseconds | 1 millisecond |
| `checkpoint_timeout` | seconds | 30 seconds |
For example, `deadlock_timeout = '0.001min'` is converted to zero but rejected
because zero is below its one-millisecond minimum. A positive minimum
protects against this particular zero-sentinel collision; it does not prevent
all just-out-of-range fractional inputs from rounding into range.
### Independent correction and tests
The negative-range problem can be handled independently of the policy for
positive timeouts. Validation needs access to the converted value before the
lossy next-smaller-unit and integer rounding stages. A narrowly scoped
ordering is:
```text
parse numeric input
-> convert to the base unit without rounding
-> reject a value below the parameter's declared minimum
-> apply the documented unit and integer rounding for storage
-> perform the existing range checks on the stored value
```
Checking `signbit()` after conversion is sufficient for a caller that rejects
all negative values, including negative zero. It is not a complete generic
GUC fix because `min = -1` parameters legitimately accept some negative
values; those parameters require comparison with the unrounded converted
value.
Regression coverage for this section should include:
```sql
-- min = 0: negative values must not enter the range through zero.
SET statement_timeout = '-0.0001d';
SET statement_timeout = '-0.001h';
SET statement_timeout = '-0.001min';
-- min = -1: values below -1ms must not become zero.
SET log_min_duration_statement = '-0.001h';
SET log_min_duration_sample = '-0.001h';
-- The actual sentinel remains valid.
SET log_min_duration_statement = '-1ms';
```
Tests should cover several suffixes because the magnitude lost before range
validation is determined by the spelling. SIGHUP-only parameters such as
`max_standby_streaming_delay` require configuration-file and reload or startup
coverage rather than session-level `SET` tests.
## 2. Positive nonzero values can collapse into zero sentinels
Positive input presents a distinct policy question. The same rounding rule
can turn a requested nonzero timeout into a zero sentinel even though no range
constraint is violated.
```sql
SET statement_timeout = '0.0001d';
SHOW statement_timeout;
```
The requested duration is 8.64 seconds, but the stored value is zero and the
timeout is disabled.
### Disabled or unbounded timeout mechanisms
These settings accept zero and use it to disable a timeout or closely related
mechanism:
| Parameter | Base unit | Example positive input that becomes zero | Resulting zero behavior |
|---|---|---|---|
| `statement_timeout` | milliseconds | `0.0001d` (8.64 s) | timeout disabled |
| `lock_timeout` | milliseconds | `0.001min` (60 ms) | timeout disabled |
| `transaction_timeout` | milliseconds | `0.001h` (3.6 s) | timeout disabled |
| `idle_in_transaction_session_timeout` | milliseconds | `0.001min` (60 ms) | timeout disabled |
| `idle_session_timeout` | milliseconds | `0.001min` (60 ms) | timeout disabled |
| `wal_receiver_timeout` | milliseconds | `0.001h` (3.6 s) | timeout disabled |
| `wal_sender_timeout` | milliseconds | `0.001h` (3.6 s) | timeout disabled |
| `archive_timeout` | seconds | `0.001min` (60 ms) | timeout disabled |
| `idle_replication_slot_timeout` | seconds | `0.001min` (60 ms) | invalidation disabled |
| `client_connection_check_interval` | milliseconds | `0.001min` (60 ms) | connection checks disabled |
| `vacuum_cost_delay` | real milliseconds | `0.0001s` (0.1 ms) | cost-based delay disabled |
### Zero can mean the operational opposite
For other settings, zero is active rather than disabled. A positive value
that rounds to zero can remove a grace period or enable the most aggressive
behavior:
| Parameter | Example positive input that becomes zero | Meaning of zero |
|---|---|---|
| `log_min_duration_statement` | `0.001h` (3.6 s) | log every statement duration |
| `log_min_duration_sample` | `0.001h` (3.6 s) | sample all statements |
| `log_autovacuum_min_duration` | `0.001h` (3.6 s) | log every qualifying vacuum action |
| `log_autoanalyze_min_duration` | `0.001h` (3.6 s) | log every qualifying analyze action |
| `max_standby_archive_delay` | `0.001h` (3.6 s) | cancel conflicts with no grace period |
| `max_standby_streaming_delay` | `0.001h` (3.6 s) | cancel conflicts with no grace period |
| `wal_sender_shutdown_timeout` | `0.001h` (3.6 s) | do not wait for the receiver |
| `tcp_user_timeout` | `0.001min` (60 ms) | use the operating-system default |
| `recovery_min_apply_delay` | `0.001min` (60 ms) | apply with no delay |
| `io_worker_idle_timeout` | `0.0001s` (0.1 ms) | immediate idle deadline |
### Compatibility considerations
Fractional GUC rounding is documented behavior. Existing configurations can
therefore depend on a fractional value being rounded to zero, and a global
change from nearest rounding to upward rounding would alter stored values for
more than timeout settings.
The positive case consequently requires a policy decision separate from the
negative range-check problem. Possible policies include:
- retain the current rounding and document the zero-sentinel consequence;
- reject a positive nonzero input when rounding would select a zero sentinel;
- clamp such inputs to the smallest positive value supported by that
parameter's base unit; or
- adopt upward rounding for parameters representing deadlines or minimum
delays while retaining existing behavior for other numeric GUCs.
Any targeted policy needs parameter metadata or a caller-specific decision;
`convert_to_base_unit()` does not know whether zero is disabled, immediate,
inherited, or otherwise special.
## Historical context
The next-smaller-unit rule was introduced by commit
[`1a83a80a2fe`](https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=1a83a80a2fe5b559f85ed4830acb92d5124b7a9a),
"Allow fractional input values for integer GUCs, and improve rounding logic."
Its motivating example was:
```sql
SET work_mem = '30.1GB';
```
The commit explains that directly rounding to the parameter's native unit
would produce a visually awkward value such as `31562138kB`, so it instead
rounds to the nearest multiple of the next smaller unit, producing `30822MB`.
The rule was implemented in the common conversion function and documented for
both memory and time units.
The historical evidence supports the narrower statement that the rounding
rule was motivated with a memory-size example and that its interaction with
zero-sentinel time settings is not discussed in the commit rationale. It does
not establish that time-unit behavior was accidental: the
[originating discussion](https://www.postgresql.org/message-id/1798.1552165479%40sss.pgh.pa.us)
also concerned time-based vacuum delays, and the committed documentation
deliberately describes the rule generically.
There is contrasting precedent in runtime wait code. For example:
- [`TimestampDifferenceMilliseconds()`](src/backend/utils/adt/timestamp.c#L1764)
rounds a fractional millisecond upward because waiting for less than the
intended timeout is undesirable.
- [`pg_sleep()`](src/backend/utils/adt/misc.c#L331) uses `ceil()` when
converting seconds to microseconds.
Those functions demonstrate an established reason to round actual waits
upward, but they do not by themselves establish a universal rounding rule for
stored GUC values. The GUC rule and runtime-wait rules serve different
interfaces, which is why changing the positive-input behavior requires an
explicit compatibility decision.
## Recommendations and regression matrix
The two defects should remain separate in both implementation and regression
coverage. Rejecting an out-of-range negative value is a range-validation fix;
deciding what to do when a valid positive duration becomes a zero sentinel is
a compatibility policy.
### Recommendations
1. Preserve both the exact converted value and the legacy-rounded value during
parsing. The exact value is needed for validation and for recovering the
duration when legacy rounding would otherwise erase it. The rounded value
remains available wherever compatibility requires the documented behavior.
2. Validate the declared lower bound against the exact converted value before
either lossy rounding stage. This closes the `min = 0` and `min = -1`
bypasses without changing the rounding of valid values.
3. Give parameters whose zero value has control semantics an explicit policy
for positive-nonzero-to-zero collisions. The preferred behavior is:
```text
if exact_value > 0 and legacy_stored_value == 0:
integer GUC: store max(1, rint(exact_value))
real GUC: store exact_value
```
This policy first restores the requested duration in the parameter's native
unit and only clamps when that duration is genuinely below the native
integer resolution. A blanket post-rounding clamp is not sufficient:
`0.0001d` means 8.64 seconds, so `statement_timeout` should become `8640ms`,
not `1ms`.
4. Preserve existing results outside those two cases. In particular, normal
nonzero rounding, explicit zero, valid `-1` sentinels, overflow handling,
and invalid-unit errors should not change. Parameters with a positive
minimum already reject zero and should not acquire the new positive policy
unless that is an intentional, separately reviewed change.
The `min = -1` interval between `-1` and zero is a separate edge case. For
example, `-0.5ms` is within the declared range but can round to zero. The
lower-bound fix does not change that result; changing it requires an explicit
decision about the meaning of valid fractional negative input. Likewise,
whether a textual `-0` should be rejected must be decided explicitly rather
than inferred from the numeric lower bound.
### Regression matrix
| Case | Representative input | Expected result | Purpose |
|---|---|---|---|
| Explicit zero | `statement_timeout = '0'` | stored as `0` | Preserve the documented zero sentinel. |
| `min = 0`, day suffix | `statement_timeout = '-0.0001d'` | error | Negative 8.64 seconds must not become zero. |
| `min = 0`, hour suffix | `statement_timeout = '-0.001h'` | error | Cover the hour-to-minute loss window. |
| `min = 0`, minute suffix | `statement_timeout = '-0.001min'` | error | Cover the minute-to-second loss window. |
| `min = 0`, second suffix | `statement_timeout = '-0.0001s'` | error | Cover the second-to-millisecond loss window. |
| `min = 0`, millisecond suffix | `statement_timeout = '-0.1ms'` | error | Cover the millisecond-to-microsecond conversion plus integer rounding. |
| `min = 0`, microsecond suffix | `statement_timeout = '-100us'` | error | Cover a suffix with no next-smaller-unit rounding. |
| `min = 0`, no suffix | `statement_timeout = '-0.1'` | error | Cover native-unit integer rounding. |
| Real-valued `min = 0` | `vacuum_cost_delay = '-0.0001s'` | error | Prove the fix does not depend on final integer rounding. |
| Below `min = -1` | `log_min_duration_statement = '-0.001h'` | error | A value below `-1ms` must not enter the range as zero. |
| Valid `-1` sentinel | `log_min_duration_statement = '-1ms'` | stored as `-1` | Preserve the disable sentinel. |
| Valid fractional negative | `log_min_duration_statement = '-0.5ms'` | unchanged unless separately specified | Expose the unresolved within-range policy explicitly. |
| Positive coarse spelling | `statement_timeout = '0.0001d'` | stored as `8640ms` | Recover the exact native-unit duration, not merely `1ms`. |
| Equivalent duration across suffixes | `0.0001d`; `0.0024h`; `0.144min`; `8.64s`; `8640ms`; `8640000us` | respectively `8640ms`; `8640ms`; `9000ms`; `8640ms`; `8640ms`; `8640ms` | Repair the zero collisions while preserving the existing nonzero rounding of `0.144min`. |
| Positive below integer resolution | `statement_timeout = '0.1ms'` | stored as `1ms` | Do not turn a requested nonzero timeout into disabled. |
| Positive real value | `vacuum_cost_delay = '0.0001s'` | stored as `0.1ms` | Preserve exact real-valued duration rather than zero. |
| Zero means immediate | `max_standby_streaming_delay = '0.001h'` | stored as `3600ms` | Prevent a positive grace period from becoming immediate cancellation. |
| Normal nonzero value | `statement_timeout = '1.5ms'` | stored as `2ms` | Preserve ordinary documented integer rounding. |
| Positive-min control | `deadlock_timeout = '0.001min'` | existing out-of-range error | Avoid broadening the policy to already-protected parameters. |
| Overflow | an existing over-range time value | error | Preserve range and overflow handling. |
| Invalid unit | `statement_timeout = '1fortnight'` | error | Preserve unit validation. |
The sub-millisecond integer boundary deserves direct coverage because it is
where ties-to-even and the proposed positive policy meet:
| Exact duration | Legacy stored value | Expected opted-in result |
|---:|---:|---:|
| `0.499ms` | `0ms` | `1ms` |
| `0.5ms` | `0ms` | `1ms` |
| `0.501ms` | `1ms` | `1ms` |
| `1.5ms` | `2ms` | `2ms` |
The corresponding negative values should error for a `min = 0` parameter
before either rounding stage.
Session-settable parameters can be exercised in
`src/test/regress/sql/guc.sql` with matching expected output. SIGHUP-only
parameters need configuration-file plus reload or startup coverage, most
naturally in a TAP test. At least one test should express the same physical
duration with every accepted suffix so that the intended suffix-specific
outcomes are explicit and any future change is visible directly.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-08-31 14:06 ` cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
1 sibling, 1 reply; 25+ messages in thread
From: cca5507 @ 2026-08-31 14:06 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@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.sgml
index 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.c
index 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.c
index 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.c
index 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.h
index 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.pl
index 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
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-09-01 01:17 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-09-01 01:17 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
On Mon, Aug 31, 2026 at 10:07 PM cca5507 <cca5507@qq.com> wrote:
>
> > --- 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.
TBH, I am not a fan of this workaround unless we deal with the
reported GUC issue later. The strange behaviors reported earlier seems
relatively hard to hit since it requires relatively uncommon input and
it is also not that easy to notice even if it is hit. But the
operational consequence and the scope of affected GUCs seems
non-trivial to me. That said, I don't have enough concentrated time to
dig into the issue, figure out a proper fix and write it down for now,
since I am on a vacation. Attached file is a response from Sol
regarding the anomaly which somewhat makes sense to me. It would be
helpful to hear Sawada-san and Alexander's thoughts on it. Also CCed
the members of RMT.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
Attachments:
GUC semantic anomaly.pdf (application/pdf, 278.1K) download
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-09-01 21:08 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Masahiko Sawada @ 2026-09-01 21:08 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
On Mon, Aug 31, 2026 at 6:17 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:
>
> On Mon, Aug 31, 2026 at 10:07 PM cca5507 <cca5507@qq.com> wrote:
> >
> > > --- 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.
>
> TBH, I am not a fan of this workaround unless we deal with the
> reported GUC issue later. The strange behaviors reported earlier seems
> relatively hard to hit since it requires relatively uncommon input and
> it is also not that easy to notice even if it is hit. But the
> operational consequence and the scope of affected GUCs seems
> non-trivial to me. That said, I don't have enough concentrated time to
> dig into the issue, figure out a proper fix and write it down for now,
> since I am on a vacation.
I would prefer using parse_int() here.
It does lead to some slightly surprising cases (for instance '-0.4ms'
converts to 0, which means waiting forever), including the case I
raised earlier in this thread. But existing integer GUC parameters
such as statement_timeout have been behaving that way, so I don't
think it would be a problem in practice. And if we fix the common
parsing infrastructure so that it doesn't produce such surprising
results, that would fix both places at once.
Using parse_int() also addresses the reported overflow at its root,
since the value is then capped at INT_MAX milliseconds, which is all
that WaitLatch() accepts anyway.
As for the currently proposed idea of adding our own checks and
rounding, while it would be flexible enough to deal with corner cases
that parse_int() and parse_real() don't handle today, I think the GUC
side would then have to be fixed as well, and PG19 is not the right
time to work on that.
> Attached file is a response from Sol
> regarding the anomaly which somewhat makes sense to me. It would be
> helpful to hear Sawada-san and Alexander's thoughts on it. Also CCed
> the members of RMT.
FYI I don't think attaching an LLM response as a PDF is a good way to
bring material to this list. A most part of it is not about the unit
conversion at all, but advice to you about how to revise your report
and how to frame the argument here. For instance one section is headed
"This changes how I would revise your report", and another closes by
noting "makes the proposal much harder to object to". That is odd
material to send to the people on this thread who are working on this
problem, and it leaves the readers to figure out which parts are
claims about PostgreSQL and which are not, and to verify the contents.
I'd rather see the parts you have verified yourself, stated in your
own words and in plain text.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-09-02 01:40 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-09-10 18:59 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-09-02 01:40 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
On Wed, Sep 2, 2026 at 5:09 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
>
> On Mon, Aug 31, 2026 at 6:17 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:
> >
> > On Mon, Aug 31, 2026 at 10:07 PM cca5507 <cca5507@qq.com> wrote:
> > >
> > > > --- 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.
> >
> > TBH, I am not a fan of this workaround unless we deal with the
> > reported GUC issue later. The strange behaviors reported earlier seems
> > relatively hard to hit since it requires relatively uncommon input and
> > it is also not that easy to notice even if it is hit. But the
> > operational consequence and the scope of affected GUCs seems
> > non-trivial to me. That said, I don't have enough concentrated time to
> > dig into the issue, figure out a proper fix and write it down for now,
> > since I am on a vacation.
>
> I would prefer using parse_int() here.
>
> It does lead to some slightly surprising cases (for instance '-0.4ms'
> converts to 0, which means waiting forever), including the case I
> raised earlier in this thread. But existing integer GUC parameters
> such as statement_timeout have been behaving that way, so I don't
> think it would be a problem in practice. And if we fix the common
> parsing infrastructure so that it doesn't produce such surprising
> results, that would fix both places at once.
>
> Using parse_int() also addresses the reported overflow at its root,
> since the value is then capped at INT_MAX milliseconds, which is all
> that WaitLatch() accepts anyway.
>
> As for the currently proposed idea of adding our own checks and
> rounding, while it would be flexible enough to deal with corner cases
> that parse_int() and parse_real() don't handle today, I think the GUC
> side would then have to be fixed as well, and PG19 is not the right
> time to work on that.
Agree. Maybe it is not a good time to do so for v19, which means that
the timeout in wait for could be a new victim of that. I still think
that it needs to be fixed in HEAD. The current generation of LLM keeps
finding embarrassing bugs as we can see in the pre-release season.
This is one example of that. At first, it seems trivial to me.
However, it turns out to not be the case as I looked into it. Fixing
bugs is not fun and I don't like to do it all the time. But some bugs
left unreported and unfixed would certainly surprise and disappoint
users one day. I guess the number and the frequency of bugs reporting
to the mailing list could only be used as one aspect of criteria for
judging whether the bug needs to be fixed or not, given lots of users
are not accustomed to the working style of mailing lists and are not
able to write decent reports in English, which demotivate them to
report, or simply don't know the existence of the channel.
> > Attached file is a response from Sol
> > regarding the anomaly which somewhat makes sense to me. It would be
> > helpful to hear Sawada-san and Alexander's thoughts on it. Also CCed
> > the members of RMT.
>
> FYI I don't think attaching an LLM response as a PDF is a good way to
> bring material to this list. A most part of it is not about the unit
> conversion at all, but advice to you about how to revise your report
> and how to frame the argument here. For instance one section is headed
> "This changes how I would revise your report", and another closes by
> noting "makes the proposal much harder to object to". That is odd
> material to send to the people on this thread who are working on this
> problem, and it leaves the readers to figure out which parts are
> claims about PostgreSQL and which are not, and to verify the contents.
> I'd rather see the parts you have verified yourself, stated in your
> own words and in plain text.
Also agree. I didn't have a laptop at hand and enough concentrated
time at that time. I could've done better on myself later but thought
I needed to report it early at a faster pace. The conversation
happened on my mobile phone. I don't like the tone and expression it
used, which I ask explicitly not to do so in Codex on the computer.
Sorry if this seems a disrespect of your time.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-09-10 18:59 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-11 05:30 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-11 11:42 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
0 siblings, 2 replies; 25+ messages in thread
From: Masahiko Sawada @ 2026-09-10 18:59 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
On Tue, Sep 1, 2026 at 6:40 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:
>
> On Wed, Sep 2, 2026 at 5:09 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
> >
> > On Mon, Aug 31, 2026 at 6:17 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:
> > >
> > > On Mon, Aug 31, 2026 at 10:07 PM cca5507 <cca5507@qq.com> wrote:
> > > >
> > > > > --- 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.
> > >
> > > TBH, I am not a fan of this workaround unless we deal with the
> > > reported GUC issue later. The strange behaviors reported earlier seems
> > > relatively hard to hit since it requires relatively uncommon input and
> > > it is also not that easy to notice even if it is hit. But the
> > > operational consequence and the scope of affected GUCs seems
> > > non-trivial to me. That said, I don't have enough concentrated time to
> > > dig into the issue, figure out a proper fix and write it down for now,
> > > since I am on a vacation.
> >
> > I would prefer using parse_int() here.
> >
> > It does lead to some slightly surprising cases (for instance '-0.4ms'
> > converts to 0, which means waiting forever), including the case I
> > raised earlier in this thread. But existing integer GUC parameters
> > such as statement_timeout have been behaving that way, so I don't
> > think it would be a problem in practice. And if we fix the common
> > parsing infrastructure so that it doesn't produce such surprising
> > results, that would fix both places at once.
> >
> > Using parse_int() also addresses the reported overflow at its root,
> > since the value is then capped at INT_MAX milliseconds, which is all
> > that WaitLatch() accepts anyway.
> >
> > As for the currently proposed idea of adding our own checks and
> > rounding, while it would be flexible enough to deal with corner cases
> > that parse_int() and parse_real() don't handle today, I think the GUC
> > side would then have to be fixed as well, and PG19 is not the right
> > time to work on that.
>
> Agree. Maybe it is not a good time to do so for v19, which means that
> the timeout in wait for could be a new victim of that.
So I've reviewed the v6 patch, and here are some comments:
wait.c no longer calls rint() or isnan(), so we don't need to include math.h.
---
+ The valid range is <literal>0 .. INT_MAX</literal> milliseconds.
+ A value of zero means waiting indefinitely.
INT_MAX is a C identifier and it's better to avoid using it in that section.
Also, the current description "The timeout might be given as integer
number of milliseconds." seems not correct to me since pares_int()
falls back to strtod() when the value has a decimal point. ANd the
paragraph also doesn't mention the rounding. How about rewriting it to
something like:
<para>
When specified and <parameter>timeout</parameter> is
greater than zero,
the command waits until <parameter>lsn</parameter> is reached or
- the specified <parameter>timeout</parameter> has elapsed.
+ the specified <parameter>timeout</parameter> has elapsed. A value
+ of zero (the default) means the command waits indefinitely.
</para>
<para>
- The <parameter>timeout</parameter> might be given as
integer number of
- 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 <parameter>timeout</parameter> is an amount of time in
+ milliseconds. It may also be specified as a string containing the
+ numerical value followed by a time unit
+ (see <xref linkend="config-setting-names-values"/>). The maximum
+ value is <literal>2147483647 ms</literal>.
+ </para>
+ <para>
+ Fractional values are rounded to the nearest millisecond. Note
+ that a <parameter>timeout</parameter> of half a millisecond or
+ less therefore rounds down to zero, which means waiting
+ indefinitely.
</para>
I've attached the updated patch that incorporated the above points.
Please review it.
> I still think that it needs to be fixed in HEAD.
Agreed, and I'd rather we did that on its own thread.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Attachments:
v7-0001-Fix-timeout-overflow-in-WAIT-FOR-LSN.patch (text/x-patch, 8.0K) download
From 9de876a7ef09301bee4ddb4c22251fdd44794e62 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Thu, 10 Sep 2026 11:11:15 -0700
Subject: [PATCH v7] Fix timeout overflow in WAIT FOR LSN.
Commit 447aae13b03 accepted TIMEOUT values up to INT64_MAX
milliseconds, but computing the deadline multiplies by 1000, which
caused an int64 overflow. And the command reported a timeout at once
instead of waiting.
Parse the value as an int instead, which simplifies the code and is
consistent with other timeout values such as the statement_timeout GUC.
The specified values are now rounded to the nearest millisecond, so a
positive timeout below half a millisecond becomes zero and waits
indefinitely.
Backpatch to v19, where the TIMEOUT option was introduced.
Reported-by: ChangAo Chen <cca5507@qq.com>
Author: ChangAo Chen <cca5507@qq.com>
Reviewed-by: Xuneng Zhou <xunengzhou@gmail.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Discussion: https://postgr.es/m/tencent_86B83240785807077600A1778566B5C12908@qq.com
Backpatch-through: 19
---
doc/src/sgml/ref/wait_for.sgml | 18 ++++++++++----
src/backend/access/transam/xlogwait.c | 2 +-
src/backend/commands/repack_worker.c | 4 ++--
src/backend/commands/wait.c | 32 ++++++-------------------
src/include/access/xlogwait.h | 2 +-
src/test/recovery/t/049_wait_for_lsn.pl | 14 +++++++++++
6 files changed, 38 insertions(+), 34 deletions(-)
diff --git a/doc/src/sgml/ref/wait_for.sgml b/doc/src/sgml/ref/wait_for.sgml
index 04ca9400426..36b9ab61976 100644
--- a/doc/src/sgml/ref/wait_for.sgml
+++ b/doc/src/sgml/ref/wait_for.sgml
@@ -145,13 +145,21 @@ WAIT FOR LSN '<replaceable class="parameter">lsn</replaceable>'
<para>
When specified and <parameter>timeout</parameter> is greater than zero,
the command waits until <parameter>lsn</parameter> is reached or
- the specified <parameter>timeout</parameter> has elapsed.
+ the specified <parameter>timeout</parameter> has elapsed. A value
+ of zero (the default) means the command waits indefinitely.
</para>
<para>
- The <parameter>timeout</parameter> might be given as integer number of
- 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 <parameter>timeout</parameter> is an amount of time in
+ milliseconds. It may also be specified as a string containing the
+ numerical value followed by a time unit
+ (see <xref linkend="config-setting-names-values"/>). The maximum
+ value is <literal>2147483647 ms</literal>.
+ </para>
+ <para>
+ Fractional values are rounded to the nearest millisecond. Note
+ that a <parameter>timeout</parameter> of half a millisecond or
+ less therefore rounds down to zero, which means waiting
+ indefinitely.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 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.c
index b4ba9cfc67b..bf2bc2dca13 100644
--- a/src/backend/commands/repack_worker.c
+++ b/src/backend/commands/repack_worker.c
@@ -453,7 +453,7 @@ decode_concurrent_changes(LogicalDecodingContext *ctx,
if (record == NULL)
{
- int64 timeout = 0;
+ int timeout = 0;
WaitLSNResult res;
/*
@@ -472,7 +472,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.c
index 9ba4c75021e..bac38d17726 100644
--- a/src/backend/commands/wait.c
+++ b/src/backend/commands/wait.c
@@ -13,8 +13,6 @@
*/
#include "postgres.h"
-#include <math.h>
-
#include "access/xlog.h"
#include "access/xlogrecovery.h"
#include "access/xlogwait.h"
@@ -35,7 +33,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 +90,6 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
{
char *timeout_str;
const char *hintmsg;
- double dval;
if (timeout_specified)
errorConflictingDefElem(defel, pstate);
@@ -100,33 +97,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.h
index 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.pl
index cb7d4d461de..50c08dfac8f 100644
--- a/src/test/recovery/t/049_wait_for_lsn.pl
+++ b/src/test/recovery/t/049_wait_for_lsn.pl
@@ -343,6 +343,14 @@ $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 +415,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.55.0
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-10 18:59 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-09-11 05:30 ` cca5507 <cca5507@qq.com>
2026-09-11 11:43 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
1 sibling, 1 reply; 25+ messages in thread
From: cca5507 @ 2026-09-11 05:30 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; Xuneng Zhou <xunengzhou@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
> I've attached the updated patch that incorporated the above points.
> Please review it.
Thanks for the patch, LGTM!
--
Regards,
ChangAo Chen
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-10 18:59 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-11 05:30 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
@ 2026-09-11 11:43 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-09-12 19:18 ` Re: timeout value overflow in wait for lsn Alexander Korotkov <aekorotkov@gmail.com>
0 siblings, 1 reply; 25+ messages in thread
From: Xuneng Zhou @ 2026-09-11 11:43 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
On Fri, Sep 11, 2026 at 1:30 PM cca5507 <cca5507@qq.com> wrote:
>
> > I've attached the updated patch that incorporated the above points.
> > Please review it.
>
> Thanks for the patch, LGTM!
Thanks for reporting and dealing with it!
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-10 18:59 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-11 05:30 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-11 11:43 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
@ 2026-09-12 19:18 ` Alexander Korotkov <aekorotkov@gmail.com>
0 siblings, 0 replies; 25+ messages in thread
From: Alexander Korotkov @ 2026-09-12 19:18 UTC (permalink / raw)
To: Xuneng Zhou <xunengzhou@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
On Fri, Sep 11, 2026 at 2:43 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:
>
> On Fri, Sep 11, 2026 at 1:30 PM cca5507 <cca5507@qq.com> wrote:
> >
> > > I've attached the updated patch that incorporated the above points.
> > > Please review it.
> >
> > Thanks for the patch, LGTM!
>
> Thanks for reporting and dealing with it!
I skim through the thread. LGTM as well. I'm going to push (and
backpatch) this if no objection.
------
Regards,
Alexander Korotkov
Supabase
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: timeout value overflow in wait for lsn
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 07:17 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` Re: timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Re: timeout value overflow in wait for lsn Xuneng Zhou <xunengzhou@gmail.com>
2026-09-10 18:59 ` Re: timeout value overflow in wait for lsn Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-09-11 11:42 ` Xuneng Zhou <xunengzhou@gmail.com>
1 sibling, 0 replies; 25+ messages in thread
From: Xuneng Zhou @ 2026-09-11 11:42 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; Alexander Korotkov <aekorotkov@gmail.com>; Melanie Plageman <melanieplageman@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>
> So I've reviewed the v6 patch, and here are some comments:
>
> wait.c no longer calls rint() or isnan(), so we don't need to include math.h.
>
> ---
> + The valid range is <literal>0 .. INT_MAX</literal> milliseconds.
> + A value of zero means waiting indefinitely.
>
> INT_MAX is a C identifier and it's better to avoid using it in that section.
>
> Also, the current description "The timeout might be given as integer
> number of milliseconds." seems not correct to me since pares_int()
> falls back to strtod() when the value has a decimal point. ANd the
> paragraph also doesn't mention the rounding. How about rewriting it to
> something like:
>
> <para>
> When specified and <parameter>timeout</parameter> is
> greater than zero,
> the command waits until <parameter>lsn</parameter> is reached or
> - the specified <parameter>timeout</parameter> has elapsed.
> + the specified <parameter>timeout</parameter> has elapsed. A value
> + of zero (the default) means the command waits indefinitely.
> </para>
> <para>
> - The <parameter>timeout</parameter> might be given as
> integer number of
> - 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 <parameter>timeout</parameter> is an amount of time in
> + milliseconds. It may also be specified as a string containing the
> + numerical value followed by a time unit
> + (see <xref linkend="config-setting-names-values"/>). The maximum
> + value is <literal>2147483647 ms</literal>.
> + </para>
> + <para>
> + Fractional values are rounded to the nearest millisecond. Note
> + that a <parameter>timeout</parameter> of half a millisecond or
> + less therefore rounds down to zero, which means waiting
> + indefinitely.
> </para>
>
> I've attached the updated patch that incorporated the above points.
> Please review it.
Thanks for updating/reviewing the patch!
LGTM.
> > I still think that it needs to be fixed in HEAD.
>
> Agreed, and I'd rather we did that on its own thread.
OK. I'll start a new thread for it when the somewhat overwhelmed v19
clean-up phase is over. I'd also be happy to help if fellow hackers
passing by decide to tackle it.
--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.
^ permalink raw reply [nested|flat] 25+ messages in thread
end of thread, other threads:[~2026-09-12 19:18 UTC | newest]
Thread overview: 25+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-19 13:40 timeout value overflow in wait for lsn cca5507 <cca5507@qq.com>
2026-08-19 15:09 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 01:58 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 03:12 ` cca5507 <cca5507@qq.com>
2026-08-20 04:20 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-20 06:17 ` cca5507 <cca5507@qq.com>
2026-08-20 08:08 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-25 19:51 ` Nathan Bossart <nathandbossart@gmail.com>
2026-08-26 13:51 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-28 07:17 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-28 09:11 ` cca5507 <cca5507@qq.com>
2026-08-28 19:11 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-08-29 10:02 ` cca5507 <cca5507@qq.com>
2026-08-30 03:04 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 01:47 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 07:05 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-08-31 14:06 ` cca5507 <cca5507@qq.com>
2026-09-01 01:17 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-09-01 21:08 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-02 01:40 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-09-10 18:59 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-11 05:30 ` cca5507 <cca5507@qq.com>
2026-09-11 11:43 ` Xuneng Zhou <xunengzhou@gmail.com>
2026-09-12 19:18 ` Alexander Korotkov <aekorotkov@gmail.com>
2026-09-11 11:42 ` Xuneng Zhou <xunengzhou@gmail.com>
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