pg_walinspect: fix LSN validation messages and empty range handling

Started by Chao Li2 months ago18 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

While working on patch [1]/messages/by-id/80E9F0AD-CFC5-4BE5-81DE-D8FE35E10A1C@gmail.com, I noticed two small issues with pg_walinspect.

1. In pg_get_wal_record_info() as well as a few other functions, there are checks like:
```
if (lsn > curr_lsn)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("WAL input LSN must be less than current LSN"),
errdetail("Current WAL LSN on the database system is at %X/%08X.",
LSN_FORMAT_ARGS(curr_lsn))));
```

The check itself uses a > comparison, so equality is accepted by this validation check. However, the error message says "must be less than", which implies that equality is not accepted. Thus, the check and the error message are inconsistent.

Commit 5c1b6628075a changed the check from >= to > and changed the error message from "cannot accept future input LSN" to "WAL input LSN must be less than current LSN". This seems to have been an oversight.

The error message can be changed to say "must be less than or equal to", matching the actual validation condition.

2. pg_get_wal_records_info() accepts an end_lsn equal to start_lsn, but the same fixed range can produce different results. For example:
```
evantest=# select pg_current_wal_flush_lsn();
pg_current_wal_flush_lsn
--------------------------
0/01D61428
(1 row)
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428', '0/01D61428');
ERROR: could not find a valid record after 0/01D61428

evantest=# checkpoint;
CHECKPOINT
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428', '0/01D61428');
start_lsn | end_lsn | prev_lsn | xid | resource_manager | record_type | record_length | main_data_length | fpi_length | description | block_ref
-----------+---------+----------+-----+------------------+-------------+---------------+------------------+------------+-------------+-----------
(0 rows)
```

When I passed the current flushed LSN to pg_get_wal_records_info() as both start_lsn and end_lsn, it raised an error because no record was available at or after that LSN. After I ran CHECKPOINT to generate more WAL records, the same query returned zero rows.

Thus, the same fixed range can either raise an error or return zero rows depending on whether WAL exists after end_lsn, even though WAL after end_lsn cannot belong to the requested range. This may confuse users.

To fix, I think an empty LSN range cannot contain a complete WAL record, so it can be handled without initializing a WAL reader. So that, the record and block information functions can return zero rows, while pg_get_wal_stats() can preserve its zero-valued aggregate output.

[1]: /messages/by-id/80E9F0AD-CFC5-4BE5-81DE-D8FE35E10A1C@gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253860_1
v1-0001-Fix-inaccurate-LSN-validation-messages-in-pg_wali.patchapplication/octet-stream; name=v1-0001-Fix-inaccurate-LSN-validation-messages-in-pg_wali.patch; x-unix-mode=0644Download+14-15
v1-0002-Handle-empty-LSN-ranges-in-pg_walinspect.patchapplication/octet-stream; name=v1-0002-Handle-empty-LSN-ranges-in-pg_walinspect.patch; x-unix-mode=0644Download+56-10
#2Kiran Kaki
itskkpg@gmail.com
In reply to: Chao Li (#1)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi Chao

On Sat, Sep 19, 2026 at 8:08 PM Chao Li <li.evan.chao@gmail.com> wrote:

Hi,

While working on patch [1], I noticed two small issues with pg_walinspect.

1. In pg_get_wal_record_info() as well as a few other functions, there are checks like:
```
if (lsn > curr_lsn)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("WAL input LSN must be less than current LSN"),
errdetail("Current WAL LSN on the database system is at %X/%08X.",
LSN_FORMAT_ARGS(curr_lsn))));
```

The check itself uses a > comparison, so equality is accepted by this validation check. However, the error message says "must be less than", which implies that equality is not accepted. Thus, the check and the error message are inconsistent.

Commit 5c1b6628075a changed the check from >= to > and changed the error message from "cannot accept future input LSN" to "WAL input LSN must be less than current LSN". This seems to have been an oversight.

The error message can be changed to say "must be less than or equal to", matching the actual validation condition.

Thanks for the patch. 0001 looks good to me.

As I see 5c1b6628075a relaxed these checks from >= to > but left the messages
saying "less than", so the wording no longer matches the code. I
checked the thread behind that commit to confirm accepting equality
was deliberate.

Tested on master (9e17d25e79d4):

- Applies cleanly, builds with no new warnings
- pg_walinspect/regress passes, and so does the full suite
- Expected-output changes match the new messages exactly
- No other LSN validation message has the same problem

Also, the existing tests already cover every message that changed.

2. pg_get_wal_records_info() accepts an end_lsn equal to start_lsn, but the same fixed range can produce different results. For example:
```
evantest=# select pg_current_wal_flush_lsn();
pg_current_wal_flush_lsn
--------------------------
0/01D61428
(1 row)
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428', '0/01D61428');
ERROR: could not find a valid record after 0/01D61428

evantest=# checkpoint;
CHECKPOINT
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428', '0/01D61428');
start_lsn | end_lsn | prev_lsn | xid | resource_manager | record_type | record_length | main_data_length | fpi_length | description | block_ref
-----------+---------+----------+-----+------------------+-------------+---------------+------------------+------------+-------------+-----------
(0 rows)
```

When I passed the current flushed LSN to pg_get_wal_records_info() as both start_lsn and end_lsn, it raised an error because no record was available at or after that LSN. After I ran CHECKPOINT to generate more WAL records, the same query returned zero rows.

Thus, the same fixed range can either raise an error or return zero rows depending on whether WAL exists after end_lsn, even though WAL after end_lsn cannot belong to the requested range. This may confuse users.

To fix, I think an empty LSN range cannot contain a complete WAL record, so it can be handled without initializing a WAL reader. So that, the record and block information functions can return zero rows, while pg_get_wal_stats() can preserve its zero-valued aggregate output.

[1] /messages/by-id/80E9F0AD-CFC5-4BE5-81DE-D8FE35E10A1C@gmail.com

0002 looks right to me with two suggestions

I reproduced the problem on an unpatched build: an empty range at the
end of WAL errors with "could not find a valid record after * ",
while the same empty range mid-WAL returns 0 rows. So the result
depends on whether anything wrote WAL afterwards, which is worth
fixing.

Tested on master (9e17d25e79d4):

- Applies cleanly on top of 0001, builds with no new warnings
- pg_walinspect/regress passes, and so does the full suite
- Empty ranges now return 0 rows instead of erroring;
- Non-empty ranges are unaffected

Two small things:

1. Minor: In GetWalStats() the "An empty range cannot contain any WAL
records" comment sits above "if (start_lsn < end_lsn)", the
opposite sense from the other two sites. The structure has to
differ there since it still calls GetXLogSummaryStats(), but the
comment reads the wrong way round. Maybe "Read records only if the
range is non-empty."

2. One thing: pg_get_wal_record_info() isn't covered. Passing it the
current LSN still errors in the reader. Same "validation accepts it,
then the reader fails" shape, though the right answer is less clear
there: it returns a single row rather than a set, so returning nothing
isn't an option, and the error isn't inaccurate. I think it would be worth
handling alongside this, if you agree the shape is the same..

Thanks,
Kiran Kaki.

#3Chao Li
li.evan.chao@gmail.com
In reply to: Kiran Kaki (#2)
Re: pg_walinspect: fix LSN validation messages and empty range handling

On Sep 21, 2026, at 02:40, Kiran Kaki <itskkpg@gmail.com> wrote:

Hi Chao

On Sat, Sep 19, 2026 at 8:08 PM Chao Li <li.evan.chao@gmail.com> wrote:

Hi,

While working on patch [1], I noticed two small issues with pg_walinspect.

1. In pg_get_wal_record_info() as well as a few other functions, there are checks like:
```
if (lsn > curr_lsn)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("WAL input LSN must be less than current LSN"),
errdetail("Current WAL LSN on the database system is at %X/%08X.",
LSN_FORMAT_ARGS(curr_lsn))));
```

The check itself uses a > comparison, so equality is accepted by this validation check. However, the error message says "must be less than", which implies that equality is not accepted. Thus, the check and the error message are inconsistent.

Commit 5c1b6628075a changed the check from >= to > and changed the error message from "cannot accept future input LSN" to "WAL input LSN must be less than current LSN". This seems to have been an oversight.

The error message can be changed to say "must be less than or equal to", matching the actual validation condition.

Thanks for the patch. 0001 looks good to me.

Thank you very much for the review.

As I see 5c1b6628075a relaxed these checks from >= to > but left the messages
saying "less than", so the wording no longer matches the code. I
checked the thread behind that commit to confirm accepting equality
was deliberate.

Tested on master (9e17d25e79d4):

- Applies cleanly, builds with no new warnings
- pg_walinspect/regress passes, and so does the full suite
- Expected-output changes match the new messages exactly
- No other LSN validation message has the same problem

Also, the existing tests already cover every message that changed.

2. pg_get_wal_records_info() accepts an end_lsn equal to start_lsn, but the same fixed range can produce different results. For example:
```
evantest=# select pg_current_wal_flush_lsn();
pg_current_wal_flush_lsn
--------------------------
0/01D61428
(1 row)
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428', '0/01D61428');
ERROR: could not find a valid record after 0/01D61428

evantest=# checkpoint;
CHECKPOINT
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428', '0/01D61428');
start_lsn | end_lsn | prev_lsn | xid | resource_manager | record_type | record_length | main_data_length | fpi_length | description | block_ref
-----------+---------+----------+-----+------------------+-------------+---------------+------------------+------------+-------------+-----------
(0 rows)
```

When I passed the current flushed LSN to pg_get_wal_records_info() as both start_lsn and end_lsn, it raised an error because no record was available at or after that LSN. After I ran CHECKPOINT to generate more WAL records, the same query returned zero rows.

Thus, the same fixed range can either raise an error or return zero rows depending on whether WAL exists after end_lsn, even though WAL after end_lsn cannot belong to the requested range. This may confuse users.

To fix, I think an empty LSN range cannot contain a complete WAL record, so it can be handled without initializing a WAL reader. So that, the record and block information functions can return zero rows, while pg_get_wal_stats() can preserve its zero-valued aggregate output.

[1] /messages/by-id/80E9F0AD-CFC5-4BE5-81DE-D8FE35E10A1C@gmail.com

0002 looks right to me with two suggestions

I reproduced the problem on an unpatched build: an empty range at the
end of WAL errors with "could not find a valid record after * ",
while the same empty range mid-WAL returns 0 rows. So the result
depends on whether anything wrote WAL afterwards, which is worth
fixing.

Tested on master (9e17d25e79d4):

- Applies cleanly on top of 0001, builds with no new warnings
- pg_walinspect/regress passes, and so does the full suite
- Empty ranges now return 0 rows instead of erroring;
- Non-empty ranges are unaffected

Two small things:

1. Minor: In GetWalStats() the "An empty range cannot contain any WAL
records" comment sits above "if (start_lsn < end_lsn)", the
opposite sense from the other two sites. The structure has to
differ there since it still calls GetXLogSummaryStats(), but the
comment reads the wrong way round. Maybe "Read records only if the
range is non-empty."

Accepted.

2. One thing: pg_get_wal_record_info() isn't covered. Passing it the
current LSN still errors in the reader. Same "validation accepts it,
then the reader fails" shape, though the right answer is less clear
there: it returns a single row rather than a set, so returning nothing
isn't an option, and the error isn't inaccurate. I think it would be worth
handling alongside this, if you agree the shape is the same..

I don’t think we should change pg_get_wal_record_info(). Its doc says:
```
<para>
If <replaceable>in_lsn</replaceable> isn't at the start of a WAL
record, information about the next valid WAL record is shown
instead. If there is no next valid WAL record, the function
raises an error.
</para>
```

The doc predates 5c1b6628075a, so I don’t think we should change the doc behavior.

However, your comment led me to notice that the documentation for pg_get_wal_records_info() says:
```
<para>
The function raises an error if
<replaceable>start_lsn</replaceable> is not available.
</para>
```

This makes the issue trickier. Before 5c1b6628075a, start_lsn had to be less than end_lsn, so an empty range was not accepted and this inconsistency could not arise. Based on the current doc, however, it seems reasonable for start_lsn == end_lsn to return zero rows when start_lsn is available and raise an error when it is not.

From this perspective, I think I should withdraw 0002.

PFA v2:

* 0001 is unchanged
* Withdraw 0002

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253860_3
v2-0001-Fix-inaccurate-LSN-validation-messages-in-pg_wali.patchapplication/octet-stream; name=v2-0001-Fix-inaccurate-LSN-validation-messages-in-pg_wali.patch; x-unix-mode=0644Download+14-15
#4Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Chao Li (#3)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi,

On Sun, Sep 20, 2026 at 11:56 PM Chao Li <li.evan.chao@gmail.com> wrote:

PFA v2:

Thanks for reporting and sending the patch.

Yes, it's an oversight in 5c1b6628075a. +1 for "must be less than or
equal to", since that is the wording used elsewhere in the code.

That said, an error is raised only when no valid record is found at or
after the start LSN (or the input LSN), either because that WAL is
already removed or because nothing valid follows it, which is what the
documentation already mentions. Once a record is found,
pg_get_wal_record_info() emits it, whereas the range functions emit
only the records ending at or before the end LSN, so equal start and
end LSNs emit nothing. A start LSN equal to the current LSN ends up
the same way, since the end LSN is capped at the current LSN, making
the two equal, and it errors because nothing follows the current LSN.

The v2 patch looks good to me. I adjusted the commit message and
re-attached the patch, which I think is ready for commit. I'm fine not
back-patching this for a couple of reasons. The error is still
reported in the back-branches, just with slightly incorrect wording
matching the condition the code uses, and it went unnoticed for many
years. CC-ing Michael for any thoughts.

While here, do we also need to fix AlterSubscription()'s skip WAL
location and ParseVariableDouble()'s min and max bound messages? Maybe
separately.

[1]: DROP TBALE t1; CREATE TABLE t1 (a int); SELECT pg_current_wal_lsn() AS lsn \gset INSERT INTO t1 VALUES (1);
DROP TBALE t1;
CREATE TABLE t1 (a int);
SELECT pg_current_wal_lsn() AS lsn \gset
INSERT INTO t1 VALUES (1);

-- start = end, the record there ends past end, no rows
SELECT * FROM pg_get_wal_records_info(:'lsn', :'lsn');

-- input at a valid record, one row
SELECT * FROM pg_get_wal_record_info(:'lsn');

-- start = end = current, nothing follows, error
SELECT * FROM pg_get_wal_records_info(pg_current_wal_lsn(),
pg_current_wal_lsn());

-- input = current, nothing follows, error
SELECT * FROM pg_get_wal_record_info(pg_current_wal_lsn());

-- start = current, end capped to current, so start = end again, error
SELECT * FROM pg_get_wal_records_info(pg_current_wal_lsn(),
'FFFFFFFF/FFFFFFFF');

-- recycle the segment holding :lsn
SELECT pg_switch_wal();
CHECKPOINT;

-- start = end, WAL already removed, error
SELECT * FROM pg_get_wal_records_info(:'lsn', :'lsn');

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

Attachments:

t253860_4
v2-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patchapplication/octet-stream; name=v2-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patchDownload+14-15
#5Kiran Kaki
itskkpg@gmail.com
In reply to: Bharath Rupireddy (#4)
Re: pg_walinspect: fix LSN validation messages and empty range handling

👍

Kiran reacted via Gmail
<https://www.google.com/gmail/about/?utm_source=gmail-in-product&utm_medium=et&utm_campaign=emojireactionemail#app>

On Mon, Sep 21, 2026, 1:55 AM Bharath Rupireddy <
bharath.rupireddyforpostgres@gmail.com> wrote:

Show quoted text

Hi,

On Sun, Sep 20, 2026 at 11:56 PM Chao Li <li.evan.chao@gmail.com> wrote:

PFA v2:

Thanks for reporting and sending the patch.

Yes, it's an oversight in 5c1b6628075a. +1 for "must be less than or
equal to", since that is the wording used elsewhere in the code.

That said, an error is raised only when no valid record is found at or
after the start LSN (or the input LSN), either because that WAL is
already removed or because nothing valid follows it, which is what the
documentation already mentions. Once a record is found,
pg_get_wal_record_info() emits it, whereas the range functions emit
only the records ending at or before the end LSN, so equal start and
end LSNs emit nothing. A start LSN equal to the current LSN ends up
the same way, since the end LSN is capped at the current LSN, making
the two equal, and it errors because nothing follows the current LSN.

The v2 patch looks good to me. I adjusted the commit message and
re-attached the patch, which I think is ready for commit. I'm fine not
back-patching this for a couple of reasons. The error is still
reported in the back-branches, just with slightly incorrect wording
matching the condition the code uses, and it went unnoticed for many
years. CC-ing Michael for any thoughts.

While here, do we also need to fix AlterSubscription()'s skip WAL
location and ParseVariableDouble()'s min and max bound messages? Maybe
separately.

[1]
DROP TBALE t1;
CREATE TABLE t1 (a int);
SELECT pg_current_wal_lsn() AS lsn \gset
INSERT INTO t1 VALUES (1);

-- start = end, the record there ends past end, no rows
SELECT * FROM pg_get_wal_records_info(:'lsn', :'lsn');

-- input at a valid record, one row
SELECT * FROM pg_get_wal_record_info(:'lsn');

-- start = end = current, nothing follows, error
SELECT * FROM pg_get_wal_records_info(pg_current_wal_lsn(),
pg_current_wal_lsn());

-- input = current, nothing follows, error
SELECT * FROM pg_get_wal_record_info(pg_current_wal_lsn());

-- start = current, end capped to current, so start = end again, error
SELECT * FROM pg_get_wal_records_info(pg_current_wal_lsn(),
'FFFFFFFF/FFFFFFFF');

-- recycle the segment holding :lsn
SELECT pg_switch_wal();
CHECKPOINT;

-- start = end, WAL already removed, error
SELECT * FROM pg_get_wal_records_info(:'lsn', :'lsn');

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

#6Kiran Kaki
itskkpg@gmail.com
In reply to: Chao Li (#3)
Re: pg_walinspect: fix LSN validation messages and empty range handling

👍

Kiran reacted via Gmail
<https://www.google.com/gmail/about/?utm_source=gmail-in-product&utm_medium=et&utm_campaign=emojireactionemail#app>

On Sun, Sep 20, 2026, 11:56 PM Chao Li <li.evan.chao@gmail.com> wrote:

Show quoted text

On Sep 21, 2026, at 02:40, Kiran Kaki <itskkpg@gmail.com> wrote:

Hi Chao

On Sat, Sep 19, 2026 at 8:08 PM Chao Li <li.evan.chao@gmail.com> wrote:

Hi,

While working on patch [1], I noticed two small issues with

pg_walinspect.

1. In pg_get_wal_record_info() as well as a few other functions, there

are checks like:

```
if (lsn > curr_lsn)
ereport(ERROR,

(errcode(ERRCODE_INVALID_PARAMETER_VALUE),

errmsg("WAL input LSN must be less than

current LSN"),

errdetail("Current WAL LSN on the

database system is at %X/%08X.",

LSN_FORMAT_ARGS(curr_lsn))));

```

The check itself uses a > comparison, so equality is accepted by this

validation check. However, the error message says "must be less than",
which implies that equality is not accepted. Thus, the check and the error
message are inconsistent.

Commit 5c1b6628075a changed the check from >= to > and changed the

error message from "cannot accept future input LSN" to "WAL input LSN must
be less than current LSN". This seems to have been an oversight.

The error message can be changed to say "must be less than or equal

to", matching the actual validation condition.

Thanks for the patch. 0001 looks good to me.

Thank you very much for the review.

As I see 5c1b6628075a relaxed these checks from >= to > but left the

messages

saying "less than", so the wording no longer matches the code. I
checked the thread behind that commit to confirm accepting equality
was deliberate.

Tested on master (9e17d25e79d4):

- Applies cleanly, builds with no new warnings
- pg_walinspect/regress passes, and so does the full suite
- Expected-output changes match the new messages exactly
- No other LSN validation message has the same problem

Also, the existing tests already cover every message that changed.

2. pg_get_wal_records_info() accepts an end_lsn equal to start_lsn, but

the same fixed range can produce different results. For example:

```
evantest=# select pg_current_wal_flush_lsn();
pg_current_wal_flush_lsn
--------------------------
0/01D61428
(1 row)
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428',

'0/01D61428');

ERROR: could not find a valid record after 0/01D61428

evantest=# checkpoint;
CHECKPOINT
evantest=# SELECT * FROM pg_get_wal_records_info('0/01D61428',

'0/01D61428');

start_lsn | end_lsn | prev_lsn | xid | resource_manager | record_type |

record_length | main_data_length | fpi_length | description | block_ref

-----------+---------+----------+-----+------------------+-------------+---------------+------------------+------------+-------------+-----------

(0 rows)
```

When I passed the current flushed LSN to pg_get_wal_records_info() as

both start_lsn and end_lsn, it raised an error because no record was
available at or after that LSN. After I ran CHECKPOINT to generate more WAL
records, the same query returned zero rows.

Thus, the same fixed range can either raise an error or return zero

rows depending on whether WAL exists after end_lsn, even though WAL after
end_lsn cannot belong to the requested range. This may confuse users.

To fix, I think an empty LSN range cannot contain a complete WAL

record, so it can be handled without initializing a WAL reader. So that,
the record and block information functions can return zero rows, while
pg_get_wal_stats() can preserve its zero-valued aggregate output.

[1]

/messages/by-id/80E9F0AD-CFC5-4BE5-81DE-D8FE35E10A1C@gmail.com

0002 looks right to me with two suggestions

I reproduced the problem on an unpatched build: an empty range at the
end of WAL errors with "could not find a valid record after * ",
while the same empty range mid-WAL returns 0 rows. So the result
depends on whether anything wrote WAL afterwards, which is worth
fixing.

Tested on master (9e17d25e79d4):

- Applies cleanly on top of 0001, builds with no new warnings
- pg_walinspect/regress passes, and so does the full suite
- Empty ranges now return 0 rows instead of erroring;
- Non-empty ranges are unaffected

Two small things:

1. Minor: In GetWalStats() the "An empty range cannot contain any WAL
records" comment sits above "if (start_lsn < end_lsn)", the
opposite sense from the other two sites. The structure has to
differ there since it still calls GetXLogSummaryStats(), but the
comment reads the wrong way round. Maybe "Read records only if the
range is non-empty."

Accepted.

2. One thing: pg_get_wal_record_info() isn't covered. Passing it the
current LSN still errors in the reader. Same "validation accepts it,
then the reader fails" shape, though the right answer is less clear
there: it returns a single row rather than a set, so returning nothing
isn't an option, and the error isn't inaccurate. I think it would be

worth

handling alongside this, if you agree the shape is the same..

I don’t think we should change pg_get_wal_record_info(). Its doc says:
```
<para>
If <replaceable>in_lsn</replaceable> isn't at the start of a WAL
record, information about the next valid WAL record is shown
instead. If there is no next valid WAL record, the function
raises an error.
</para>
```

The doc predates 5c1b6628075a, so I don’t think we should change the doc
behavior.

However, your comment led me to notice that the documentation for
pg_get_wal_records_info() says:
```
<para>
The function raises an error if
<replaceable>start_lsn</replaceable> is not available.
</para>
```

This makes the issue trickier. Before 5c1b6628075a, start_lsn had to be
less than end_lsn, so an empty range was not accepted and this
inconsistency could not arise. Based on the current doc, however, it seems
reasonable for start_lsn == end_lsn to return zero rows when start_lsn is
available and raise an error when it is not.

From this perspective, I think I should withdraw 0002.

PFA v2:

* 0001 is unchanged
* Withdraw 0002

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#7Chao Li
li.evan.chao@gmail.com
In reply to: Bharath Rupireddy (#4)
Re: pg_walinspect: fix LSN validation messages and empty range handling

On Sep 21, 2026, at 16:55, Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> wrote:

Hi,

On Sun, Sep 20, 2026 at 11:56 PM Chao Li <li.evan.chao@gmail.com> wrote:

PFA v2:

Thanks for reporting and sending the patch.

Yes, it's an oversight in 5c1b6628075a. +1 for "must be less than or
equal to", since that is the wording used elsewhere in the code.

That said, an error is raised only when no valid record is found at or
after the start LSN (or the input LSN), either because that WAL is
already removed or because nothing valid follows it, which is what the
documentation already mentions. Once a record is found,
pg_get_wal_record_info() emits it, whereas the range functions emit
only the records ending at or before the end LSN, so equal start and
end LSNs emit nothing. A start LSN equal to the current LSN ends up
the same way, since the end LSN is capped at the current LSN, making
the two equal, and it errors because nothing follows the current LSN.

The v2 patch looks good to me. I adjusted the commit message and
re-attached the patch, which I think is ready for commit. I'm fine not
back-patching this for a couple of reasons. The error is still
reported in the back-branches, just with slightly incorrect wording
matching the condition the code uses, and it went unnoticed for many
years. CC-ing Michael for any thoughts.

Thanks for reviewing and updating v2.

While here, do we also need to fix AlterSubscription()'s skip WAL
location and ParseVariableDouble()'s min and max bound messages? Maybe
separately.

Yeah, we can do that. As those two functions are in core, and 0001 changes the extension, I put the new changes to 0002.

PFA v3.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253860_7
v3-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patchapplication/octet-stream; name=v3-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patch; x-unix-mode=0644Download+14-15
v3-0002-Fix-inaccurate-inclusive-bound-error-messages.patchapplication/octet-stream; name=v3-0002-Fix-inaccurate-inclusive-bound-error-messages.patch; x-unix-mode=0644Download+29-4
#8Kiran Kaki
itskkpg@gmail.com
In reply to: Chao Li (#7)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi @Chao Li

On Mon, Sep 21, 2026 at 3:51 PM Chao Li <li.evan.chao@gmail.com> wrote:

Thanks for reviewing and updating v2.

While here, do we also need to fix AlterSubscription()'s skip WAL
location and ParseVariableDouble()'s min and max bound messages? Maybe
separately.

Yeah, we can do that. As those two functions are in core, and 0001 changes the extension, I put the new changes to 0002.

PFA v3.

Tested both patches on master (d39fda1cc4). They apply cleanly, build
without warnings, and the full suite passes (363 ok, 0 failed). I also
confirmed each reworded message at runtime, including the inclusive
boundaries they describe.

0001 is unchanged since v1, only the commit message differs. Looks good to me.

For 0002, the comment above that check already says "at least a future
LSN" and came in with the code in 208c5d65bb, so the reworded message
matches what the check was always meant to do. Looks good to me.

One suggestion, not a blocker. The new tests only cover the cases that
fail. The lower bound is already covered by the existing "Test zero
interval" case in 001_basic.pl, but the upper bound and the equal LSN
case are not, so they would still pass if those bounds were made
exclusive. Two additions would close that:

psql_like(
$node,
'\set WATCH_INTERVAL 1000000' . "\n" . '\echo :WATCH_INTERVAL',
qr/^1000000$/m,
'WATCH_INTERVAL accepts its upper bound');

-- ok - LSN equal to the replication origin is accepted
ALTER SUBSCRIPTION regress_testsub SKIP (lsn = '0/12346');
ALTER SUBSCRIPTION regress_testsub SKIP (lsn = NONE);

The second line just resets the value so the rest of the file's expected
output is unaffected.

Both patches are correct as they stand, so this is ready for a committer
either way. The test additions are optional, so please see if you want
to take them before committing.

Thanks Bharath for the review comments upthread, they made the scope of
these changes much easier to follow.

Regards,
Kiran Kaki

#9Chao Li
li.evan.chao@gmail.com
In reply to: Kiran Kaki (#8)
Re: pg_walinspect: fix LSN validation messages and empty range handling

On Sep 22, 2026, at 10:06, Kiran Kaki <itskkpg@gmail.com> wrote:

Hi @Chao Li

On Mon, Sep 21, 2026 at 3:51 PM Chao Li <li.evan.chao@gmail.com> wrote:

Thanks for reviewing and updating v2.

While here, do we also need to fix AlterSubscription()'s skip WAL
location and ParseVariableDouble()'s min and max bound messages? Maybe
separately.

Yeah, we can do that. As those two functions are in core, and 0001 changes the extension, I put the new changes to 0002.

PFA v3.

Tested both patches on master (d39fda1cc4). They apply cleanly, build
without warnings, and the full suite passes (363 ok, 0 failed). I also
confirmed each reworded message at runtime, including the inclusive
boundaries they describe.

0001 is unchanged since v1, only the commit message differs. Looks good to me.

For 0002, the comment above that check already says "at least a future
LSN" and came in with the code in 208c5d65bb, so the reworded message
matches what the check was always meant to do. Looks good to me.

One suggestion, not a blocker. The new tests only cover the cases that
fail. The lower bound is already covered by the existing "Test zero
interval" case in 001_basic.pl, but the upper bound and the equal LSN
case are not, so they would still pass if those bounds were made
exclusive. Two additions would close that:

psql_like(
$node,
'\set WATCH_INTERVAL 1000000' . "\n" . '\echo :WATCH_INTERVAL',
qr/^1000000$/m,
'WATCH_INTERVAL accepts its upper bound');

-- ok - LSN equal to the replication origin is accepted
ALTER SUBSCRIPTION regress_testsub SKIP (lsn = '0/12346');
ALTER SUBSCRIPTION regress_testsub SKIP (lsn = NONE);

The second line just resets the value so the rest of the file's expected
output is unaffected.

Integrated the tests into v4.

PFA v4:

* 0001 unchanged
* 0002 added the tests that Kiran suggested

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253860_9
v4-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patchapplication/octet-stream; name=v4-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patch; x-unix-mode=0644Download+14-15
v4-0002-Fix-inaccurate-inclusive-bound-error-messages.patchapplication/octet-stream; name=v4-0002-Fix-inaccurate-inclusive-bound-error-messages.patch; x-unix-mode=0644Download+42-4
#10surya poondla
suryapoondla4@gmail.com
In reply to: Chao Li (#1)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi Chao,

Thanks for the patches. I reviewed and tested v4 on master (e8a3ee5b197).
Both apply cleanly with git am, build without new
warnings, and the below things pass:
- contrib/pg_walinspect: 2/2 (pg_walinspect, oldextversions)
- src/test/regress: 239/239, including subscription
- src/bin/psql TAP: 4 files, 237 tests, including 001_basic.pl

0001 looks good to me, no comments. A few things I checked specifically:
1) All five affected messages in pg_walinspect.c are covered (lines 491,
533, 540, 839 and 858 after the patch).
Grepping the tree for the old wording across *.c, *.out, *.sql and *.sgml
turns up nothing, so no expected file or doc text is left stale.
2) The characterisation of 5c1b6628075a is accurate: it really did change
"lsn >= *curr_lsn" to "lsn > curr_lsn" and
"start_lsn >= end_lsn" to "start_lsn > *end_lsn" while only rewording the
messages.
3) pgwalinspect.sgml never states the strict relation, so no doc change is
owed here.
4) Both new wordings have precedent, which matches Bharath's preference
upthread: "must be less than or equal to" appears in
pseudorandomfuncs.c, rangetypes.c and numeric.c, and "must be greater than
or equal to" in pg_upgrade/check.c, pg_resetwal.c
and xlogrecovery.c.

0002 also looks correct to me. Both message changes match the conditions
they guard, and the two new failure-case tests pin the new
wording, so they would not pass against the old messages.
The comments below are all about the test additions rather than the fix
itself; none of them affect correctness, so please treat them as
suggestions.

1. In subscription.sql, I don't think the session-authorization is needed.
regress_subscription_user is created as LOGIN SUPERUSER at the top of the
file and is the active role
from line 190 onwards, so the session is already superuser when the new
block runs.
I removed the RESET/SET pair locally and the subscription test still
passes, so the superuser bypass over
pg_replication_origin_advance's restricted ACL is enough.
Dropping them also keeps the new test running as the subscription owner,
which is what every other SKIP test in the file does.

2. The first comment in the new block looks misplaced:
-- fail - LSN must not be behind the replication origin
RESET SESSION AUTHORIZATION;

RESET SESSION AUTHORIZATION neither fails nor has anything to do with the
origin, and the comment duplicates the one further down above the statement
it actually describes. I think it can just be removed.

3. Still in that block, pg_replication_origin_advance() returns
void, so its output is stable across runs and there is nothing for \gset to
suppress. A plain terminated statement would read more naturally and
wouldn't leave a psql variable behind.

4. Very minor: the new WATCH_INTERVAL test uses

'\set WATCH_INTERVAL 1000000' . "\n" . '\echo :WATCH_INTERVAL'
where the surrounding tests in 001_basic.pl use a literal multiline
single-quoted string.

Finally, on Bharath's question upthread about whether anything else needs
the same treatment: I went through the remaining
"must be less than" / "must be greater than" messages in the tree and each
one is consistent with the condition it guards, so I believe the
change is complete. The ones I looked at were postinit.c, postmaster.c,
partbounds.c, parse_utilcmd.c, sequence.c, wparser_def.c and pl_exec.c.

Regards,
Surya Poondla

#11Kiran Kaki
itskkpg@gmail.com
In reply to: surya poondla (#10)
Re: pg_walinspect: fix LSN validation messages and empty range handling

👍

Kiran Kaki reacted via Gmail
<https://www.google.com/gmail/about/?utm_source=gmail-in-product&utm_medium=et&utm_campaign=emojireactionemail#app>

On Tue, Sep 22, 2026 at 11:10 AM surya poondla <suryapoondla4@gmail.com>
wrote:

Show quoted text

Hi Chao,

Thanks for the patches. I reviewed and tested v4 on master
(e8a3ee5b197). Both apply cleanly with git am, build without new
warnings, and the below things pass:
- contrib/pg_walinspect: 2/2 (pg_walinspect, oldextversions)
- src/test/regress: 239/239, including subscription
- src/bin/psql TAP: 4 files, 237 tests, including 001_basic.pl

0001 looks good to me, no comments. A few things I checked specifically:
1) All five affected messages in pg_walinspect.c are covered (lines 491,
533, 540, 839 and 858 after the patch).
Grepping the tree for the old wording across *.c, *.out, *.sql and *.sgml
turns up nothing, so no expected file or doc text is left stale.
2) The characterisation of 5c1b6628075a is accurate: it really did change
"lsn >= *curr_lsn" to "lsn > curr_lsn" and
"start_lsn >= end_lsn" to "start_lsn > *end_lsn" while only rewording the
messages.
3) pgwalinspect.sgml never states the strict relation, so no doc change is
owed here.
4) Both new wordings have precedent, which matches Bharath's preference
upthread: "must be less than or equal to" appears in
pseudorandomfuncs.c, rangetypes.c and numeric.c, and "must be greater than
or equal to" in pg_upgrade/check.c, pg_resetwal.c
and xlogrecovery.c.

0002 also looks correct to me. Both message changes match the conditions
they guard, and the two new failure-case tests pin the new
wording, so they would not pass against the old messages.
The comments below are all about the test additions rather than the fix
itself; none of them affect correctness, so please treat them as
suggestions.

1. In subscription.sql, I don't think the session-authorization is needed.

regress_subscription_user is created as LOGIN SUPERUSER at the top of the
file and is the active role
from line 190 onwards, so the session is already superuser when the new
block runs.
I removed the RESET/SET pair locally and the subscription test still
passes, so the superuser bypass over
pg_replication_origin_advance's restricted ACL is enough.
Dropping them also keeps the new test running as the subscription owner,
which is what every other SKIP test in the file does.

2. The first comment in the new block looks misplaced:
-- fail - LSN must not be behind the replication origin
RESET SESSION AUTHORIZATION;

RESET SESSION AUTHORIZATION neither fails nor has anything to do with the
origin, and the comment duplicates the one further down above the statement
it actually describes. I think it can just be removed.

3. Still in that block, pg_replication_origin_advance() returns
void, so its output is stable across runs and there is nothing for \gset
to suppress. A plain terminated statement would read more naturally and
wouldn't leave a psql variable behind.

4. Very minor: the new WATCH_INTERVAL test uses

'\set WATCH_INTERVAL 1000000' . "\n" . '\echo :WATCH_INTERVAL'
where the surrounding tests in 001_basic.pl use a literal multiline
single-quoted string.

Finally, on Bharath's question upthread about whether anything else needs
the same treatment: I went through the remaining
"must be less than" / "must be greater than" messages in the tree and each
one is consistent with the condition it guards, so I believe the
change is complete. The ones I looked at were postinit.c, postmaster.c,
partbounds.c, parse_utilcmd.c, sequence.c, wparser_def.c and pl_exec.c.

Regards,
Surya Poondla

#12Kiran Kaki
itskkpg@gmail.com
In reply to: surya poondla (#10)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi Surya, Chao

PFA v4:

* 0001 unchanged
* 0002 added the tests that Kiran suggested

Thanks for the review, Surya. I tested v4 on the same base
(e8a3ee5b197) and results are inline with your
findings

1. In subscription.sql, I don't think the session-authorization is needed.
regress_subscription_user is created as LOGIN SUPERUSER at the top of the file and is the active role
from line 190 onwards, so the session is already superuser when the new block runs.
I removed the RESET/SET pair locally and the subscription test still passes, so the superuser bypass over
pg_replication_origin_advance's restricted ACL is enough.
Dropping them also keeps the new test running as the subscription owner, which is what every other SKIP test in the file does.

Agree on this!

2. The first comment in the new block looks misplaced:
-- fail - LSN must not be behind the replication origin
RESET SESSION AUTHORIZATION;

RESET SESSION AUTHORIZATION neither fails nor has anything to do with the origin, and the comment duplicates the one further down above the statement it actually describes. I think it can just be removed.

Agree on this!

3. Still in that block, pg_replication_origin_advance() returns
void, so its output is stable across runs and there is nothing for \gset to suppress. A plain terminated statement would read more naturally and wouldn't leave a psql variable behind.

Agree on 3. The \gset here is only hiding an empty one-row result, and
a plain statement says what it means. Worth noting it adds five lines to
the expected file, which seems a fair trade.

4. Very minor: the new WATCH_INTERVAL test uses

'\set WATCH_INTERVAL 1000000' . "\n" . '\echo :WATCH_INTERVAL'
where the surrounding tests in 001_basic.pl use a literal multiline single-quoted string.

Point 4 is my mistake, not Chao's. That concatenation came from the
snippet I sent upthread and he may have used it as-is. Sorry for the noise.

Thanks,
Kiran Kaki.

#13Chao Li
li.evan.chao@gmail.com
In reply to: Kiran Kaki (#12)
Re: pg_walinspect: fix LSN validation messages and empty range handling

On Sep 23, 2026, at 05:35, Kiran Kaki <itskkpg@gmail.com> wrote:

Hi Surya, Chao

PFA v4:

* 0001 unchanged
* 0002 added the tests that Kiran suggested

Thanks for the review, Surya. I tested v4 on the same base
(e8a3ee5b197) and results are inline with your
findings

1. In subscription.sql, I don't think the session-authorization is needed.
regress_subscription_user is created as LOGIN SUPERUSER at the top of the file and is the active role
from line 190 onwards, so the session is already superuser when the new block runs.
I removed the RESET/SET pair locally and the subscription test still passes, so the superuser bypass over
pg_replication_origin_advance's restricted ACL is enough.
Dropping them also keeps the new test running as the subscription owner, which is what every other SKIP test in the file does.

Agree on this!

Fixed

2. The first comment in the new block looks misplaced:
-- fail - LSN must not be behind the replication origin
RESET SESSION AUTHORIZATION;

RESET SESSION AUTHORIZATION neither fails nor has anything to do with the origin, and the comment duplicates the one further down above the statement it actually describes. I think it can just be removed.

Agree on this!

My bad. I initially put the comment before the code block, but later moved it to before the exact failure statement but forgot to delete that one.

3. Still in that block, pg_replication_origin_advance() returns
void, so its output is stable across runs and there is nothing for \gset to suppress. A plain terminated statement would read more naturally and wouldn't leave a psql variable behind.

Agree on 3. The \gset here is only hiding an empty one-row result, and
a plain statement says what it means. Worth noting it adds five lines to
the expected file, which seems a fair trade.

Fixed.

4. Very minor: the new WATCH_INTERVAL test uses

'\set WATCH_INTERVAL 1000000' . "\n" . '\echo :WATCH_INTERVAL'
where the surrounding tests in 001_basic.pl use a literal multiline single-quoted string.

Point 4 is my mistake, not Chao's. That concatenation came from the
snippet I sent upthread and he may have used it as-is. Sorry for the noise.

Fixed.

PFA v5:

* 0001: only added Surya as a reviewed in the commit message
* 0002: addressed Surya’s review comments

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253860_13
v5-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patchapplication/octet-stream; name=v5-0001-Fix-input-LSN-validation-error-messages-in-pg_wal.patch; x-unix-mode=0644Download+14-15
v5-0002-Fix-inaccurate-inclusive-bound-error-messages.patchapplication/octet-stream; name=v5-0002-Fix-inaccurate-inclusive-bound-error-messages.patch; x-unix-mode=0644Download+42-4
#14Kiran Kaki
itskkpg@gmail.com
In reply to: Chao Li (#13)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi Chao

PFA v5:

* 0001: only added Surya as a reviewed in the commit message
* 0002: addressed Surya’s review comments

Thanks for addressing the review comments . All four issues are fixed in v5.

Reviewed and tested on master (d39fda1cc4):

- Both patches apply cleanly.
- Build succeeds.
- pg_walinspect tests pass.
- psql tests pass.
- Core regression tests pass.
- Manually verified: ERROR: skip WAL location (LSN 0/00012345) must be
greater than or equal to origin LSN 0/00012346

Looks ready for a committer.

Regards,
Kiran Kaki

#15Xuneng Zhou
xunengzhou@gmail.com
In reply to: Kiran Kaki (#14)
Re: pg_walinspect: fix LSN validation messages and empty range handling

On Thu, Sep 24, 2026 at 7:14 AM Kiran Kaki <itskkpg@gmail.com> wrote:

Hi Chao

PFA v5:

* 0001: only added Surya as a reviewed in the commit message
* 0002: addressed Surya’s review comments

Thanks for addressing the review comments . All four issues are fixed in
v5.

Reviewed and tested on master (d39fda1cc4):

- Both patches apply cleanly.
- Build succeeds.
- pg_walinspect tests pass.
- psql tests pass.
- Core regression tests pass.
- Manually verified: ERROR: skip WAL location (LSN 0/00012345) must be
greater than or equal to origin LSN 0/00012346

Looks ready for a committer.

+1. v5 LGTM.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#16surya poondla
suryapoondla4@gmail.com
In reply to: Xuneng Zhou (#15)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi Chao,

Thanks for the v5 patch (all the points that were earlier raised were
addressed). Both patches apply with git am, the build is clean with no new
warnings.

I also checked that advancing the origin doesn't leak into the rest of
subscription.sql: no later test sets a skip LSN with a valid location, and
the
new block leaves subskiplsn reset to NONE, so nothing downstream depends on
the origin position.

No further comments from me, v5 looks ready for a committer. You can create
a commitfest entry for this.

Regards,
Surya Poondla

#17Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Chao Li (#13)
Re: pg_walinspect: fix LSN validation messages and empty range handling

Hi,

On Tue, Sep 22, 2026 at 6:12 PM Chao Li <li.evan.chao@gmail.com> wrote:

PFA v5:

Thanks for updating the patches.

0001 and 0002 LGTM. However, the new tests in 0002 are overkill, so I
would remove them.

I still think being consistent in the user-facing error messages is
good here. Core seems to use "less than or equal to" and "greater than
or equal to" elsewhere.

Since these are non-critical fixes, no back-patching for sure. I
marked it RfC and will leave it for the committers' interest.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

#18Michael Paquier
michael@paquier.xyz
In reply to: Bharath Rupireddy (#17)
Re: pg_walinspect: fix LSN validation messages and empty range handling

On Thu, Nov 12, 2026 at 10:03:17AM -0800, Bharath Rupireddy wrote:

Since these are non-critical fixes, no back-patching for sure. I
marked it RfC and will leave it for the committers' interest.

I'm in CC of this thread, and FWIW I don't see a point in changing
these. If somebody disagrees, feel free to ignore me, bypass my take
and go ahead.
--
Michael