Prevent underflow in KeepLogSeg().
authorNathan Bossart
Thu, 27 Apr 2023 20:43:48 +0000 (13:43 -0700)
committerNathan Bossart
Thu, 27 Apr 2023 21:32:55 +0000 (14:32 -0700)
The call to XLogGetReplicationSlotMinimumLSN() might return a
greater LSN than the one given to the function.  Subsequent segment
number calculations might then underflow, which could result in
unexpected behavior when removing or recyling WAL files.  This was
introduced with max_slot_wal_keep_size in c655077639.  To fix, skip
the block of code for replication slots if the LSN is greater.

Reported-by: Xu Xingwang
Author: Kyotaro Horiguchi
Reviewed-by: Junwang Zhao
Discussion: https://postgr.es/m/17903-4288d439dee856c6%40postgresql.org
Backpatch-through: 13

src/backend/access/transam/xlog.c

index d53a8afd380e3b48d05f3dc459169a2291fce302..a1ceded9e092f40e2559a020bd9675856a428edb 100644 (file)
@@ -9836,7 +9836,7 @@ KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
     * max_slot_wal_keep_size.
     */
    keep = XLogGetReplicationSlotMinimumLSN();
-   if (keep != InvalidXLogRecPtr)
+   if (keep != InvalidXLogRecPtr && keep < recptr)
    {
        XLByteToSeg(keep, segno, wal_segment_size);