Fix safe_wal_size for slots without restart_lsn

Started by Xuneng Zhou6 months ago5 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Xuneng Zhou
xunengzhou@gmail.com

Hi,

Currently, pg_replication_slots can report a non-null safe_wal_size
for a replication slot that has never reserved WAL, if
max_slot_wal_keep_size is finite.

Such a slot has restart_lsn = NULL and wal_status = NULL. safe_wal_size is
derived from restart_lsn, so it's not meaningful in this state. The
current code handles WALAVAIL_REMOVED but not WALAVAIL_INVALID_LSN
before computing safe_wal_size, leading arithmetic on InvalidXLogRecPtr.

Fix this by returning NULL for safe_wal_size when WAL availability is
WALAVAIL_INVALID_LSN.

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

Attachments:

0001-Fix-safe_wal_size-for-slots-without-restart_lsn.patchapplication/octet-stream; name=0001-Fix-safe_wal_size-for-slots-without-restart_lsn.patchDownload+1-2
#2Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#1)
Re: Fix safe_wal_size for slots without restart_lsn

On Wed, May 27, 2026 at 6:55 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

Hi,

Currently, pg_replication_slots can report a non-null safe_wal_size
for a replication slot that has never reserved WAL, if
max_slot_wal_keep_size is finite.

Such a slot has restart_lsn = NULL and wal_status = NULL. safe_wal_size is
derived from restart_lsn, so it's not meaningful in this state. The
current code handles WALAVAIL_REMOVED but not WALAVAIL_INVALID_LSN
before computing safe_wal_size, leading arithmetic on InvalidXLogRecPtr.

Fix this by returning NULL for safe_wal_size when WAL availability is
WALAVAIL_INVALID_LSN.

Fixed the indentation issue in v1 and updated the comment and doc. Not
sure this small issue warrants an additional test, still added one in
case someone wants it.

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

Attachments:

v2-0001-Fix-safe_wal_size-for-slots-without-restart_lsn.patchapplication/octet-stream; name=v2-0001-Fix-safe_wal_size-for-slots-without-restart_lsn.patchDownload+31-10
#3Xuneng Zhou
xunengzhou@gmail.com
In reply to: Xuneng Zhou (#2)
Re: Fix safe_wal_size for slots without restart_lsn

On Thu, May 28, 2026 at 9:01 AM Xuneng Zhou <xunengzhou@gmail.com> wrote:

On Wed, May 27, 2026 at 6:55 PM Xuneng Zhou <xunengzhou@gmail.com> wrote:

Hi,

Currently, pg_replication_slots can report a non-null safe_wal_size
for a replication slot that has never reserved WAL, if
max_slot_wal_keep_size is finite.

Such a slot has restart_lsn = NULL and wal_status = NULL. safe_wal_size is
derived from restart_lsn, so it's not meaningful in this state. The
current code handles WALAVAIL_REMOVED but not WALAVAIL_INVALID_LSN
before computing safe_wal_size, leading arithmetic on InvalidXLogRecPtr.

Fix this by returning NULL for safe_wal_size when WAL availability is
WALAVAIL_INVALID_LSN.

Fixed the indentation issue in v1 and updated the comment and doc. Not
sure this small issue warrants an additional test, still added one in
case someone wants it.

Improved the readability of the new test.

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

Attachments:

t139758_3
v3-0001-Fix-safe_wal_size-for-slots-without-restart_lsn.patchapplication/octet-stream; name=v3-0001-Fix-safe_wal_size-for-slots-without-restart_lsn.patchDownload+40-11
#4Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Xuneng Zhou (#3)
Re: Fix safe_wal_size for slots without restart_lsn

Hi Xuneng,

Fixed the indentation issue in v1 and updated the comment and doc. Not
sure this small issue warrants an additional test, still added one in
case someone wants it.

Improved the readability of the new test.

I noticed that patch on the commitfest and decided to take a look.

The patch looks correct to me. Changing the status to RfC.

--
Best regards,
Aleksander Alekseev

#5Xuneng Zhou
xunengzhou@gmail.com
In reply to: Aleksander Alekseev (#4)
Re: Fix safe_wal_size for slots without restart_lsn

Hi Aleksander,

On Mon, Nov 9, 2026 at 10:20 PM Aleksander Alekseev
<aleksander@tigerdata.com> wrote:

I noticed that patch on the commitfest and decided to take a look.

The patch looks correct to me. Changing the status to RfC.

Thanks for the review!

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