START_REPLICATION silently truncates an overlong LSN component

Started by ♂π≌2621812 days ago3 messagesbugs
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253745
psql -h localhost -U postgres

Built from patchset v3 (message #3), September 20, 2026 at 03:24 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253745_3 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253745_3 && git checkout t253745_3

Patchset v3 (message #3) is on t253745_3

Jump to latest
#1♂π≌26218
1991230470@qq.com

Hi, I found a potential bug in PostgreSQL's replication command parser where `START_REPLICATION` accepts an LSN whose high component contains more than eight hexadecimal digits and silently truncates it to a different WAL location, rather than rejecting it. Description: The replication command `START_REPLICATION` accepts an LSN in the format `X/Y`, where `X` and `Y` are hexadecimal numbers. The parser accepts an overlong high component (e.g., `123456789/0`) and silently maps it to `23456789/00000000` by truncating the excess digit. This means the server starts replication from a different WAL location than the user requested, without any error or warning. PostgreSQL version: - PostgreSQL 19beta3 (Docker-based runtime) - Source commit: not recorded in the original report - Build relationship: the tested image was not an exact-HEAD build Environment: - Docker-based PostgreSQL 19beta3 runtime - Replication connection privileges required - No special server configuration beyond standard replication setup Steps to Reproduce: Run the following command using a replication connection: ```bash psql "user=postgres dbname=postgres replication=database" \  -c 'START_REPLICATION 123456789/0;'

Then inspect the PostgreSQL server log. For a filing-quality rerun, enable log_error_verbosity=verbose and capture the complete relevant log section.

Actual Result:

The server log identifies a different starting point. For the command above, the log shows:
text

requested starting point 23456789/00000000 ... STATEMENT: START_REPLICATION 123456789/0;

The original 123456789/0 was interpreted as 23456789/00000000, with the high component truncated from nine digits to eight.

Expected Result:

The backend should reject an LSN component longer than eight hexadecimal digits with a syntax error or a specific "invalid LSN" error. It should not silently convert it to another WAL location.

Reproduction Frequency:

Positive reproduction: 2/2 on PostgreSQL 19beta3

Negative control: START_REPLICATION 0/0 remained 0/0

Additional Observations:

The issue likely originates from the replication scanner accepting an unrestricted hexadecimal token and parsing it into a 32-bit destination without checking the component width or whether the conversion was lossless. This could lead to replication starting from an unexpected LSN, potentially causing data inconsistencies or confusion during recovery.

I searched the public PostgreSQL bug archives and did not find any report specifically addressing LSN component overflow or truncation in START_REPLICATION. Please confirm whether this is considered a bug.

♂π≌26218
1991230470@qq.com

#2Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: ♂π≌26218 (#1)
Re: START_REPLICATION silently truncates an overlong LSN component

Hi,

Thanks for the report.

We introduced pg_parse_lsn() function for such issues.

It seems we should add it here to, attached patch does that.
[Added a TAP test in patch, but I dont think it needs to be committed]

I wonder if we should make the same change in libpqwalreceiver too.

Regards,
Ayush

Show quoted text

Attachments:

t253745_2
v1-0001-Use-pg_parse_lsn-for-START_REPLICATION.patchapplication/octet-stream; name=v1-0001-Use-pg_parse_lsn-for-START_REPLICATION.patchDownload+14-5
#3Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Ayush Tiwari (#2)
RE: START_REPLICATION silently truncates an overlong LSN component

Dear Ayush,

It seems we should add it here to, attached patch does that.
[Added a TAP test in patch, but I dont think it needs to be committed]

LGTM. I also think the test is not needed.

I wonder if we should make the same change in libpqwalreceiver too.

I feel it should be, see attached. It could be combined with your patch.
I grepped the source with the keyword "scanf\(.*%X/%08X", and I could
not find other places where pg_parse_lsn() is usable as-is.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachments:

t253745_3
0001-Use-pg_paser_lsn-in-libpqwalreceiver.patchapplication/octet-stream; name=0001-Use-pg_paser_lsn-in-libpqwalreceiver.patchDownload+2-6