Skip to content

Commit 26c4e89

Browse files
Fix a possibility of logical replication slot's restart_lsn going backwards.
Previously LogicalIncreaseRestartDecodingForSlot() accidentally accepted any LSN as the candidate_lsn and candidate_valid after the restart_lsn of the replication slot was updated, so it potentially caused the restart_lsn to move backwards. A scenario where this could happen in logical replication is: after a logical replication restart, based on previous candidate_lsn and candidate_valid values in memory, the restart_lsn advances upon receiving a subscriber acknowledgment. Then, logical decoding restarts from an older point, setting candidate_lsn and candidate_valid based on an old RUNNING_XACTS record. Subsequent subscriber acknowledgments then update the restart_lsn to an LSN older than the current value. In the reported case, after WAL files were removed by a checkpoint, the retreated restart_lsn prevented logical replication from restarting due to missing WAL segments. This change essentially modifies the 'if' condition to 'else if' condition within the function. The previous code had an asymmetry in this regard compared to LogicalIncreaseXminForSlot(), which does almost the same thing for different fields. The WAL removal issue was reported by Hubert Depesz Lubaczewski. Backpatch to all supported versions, since the bug exists since 9.4 where logical decoding was introduced. Reviewed-by: Tomas Vondra, Ashutosh Bapat, Amit Kapila Discussion: https://postgr.es/m/Yz2hivgyjS1RfMKs%40depesz.com Discussion: https://postgr.es/m/85fff40e-148b-4e86-b921-b4b846289132%40vondra.me Backpatch-through: 13
1 parent 2bdd3b2 commit 26c4e89

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/backend/replication/logical/logical.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart
16451645
/* don't overwrite if have a newer restart lsn */
16461646
if (restart_lsn <= slot->data.restart_lsn)
16471647
{
1648+
SpinLockRelease(&slot->mutex);
16481649
}
16491650

16501651
/*
@@ -1655,6 +1656,7 @@ LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart
16551656
{
16561657
slot->candidate_restart_valid = current_lsn;
16571658
slot->candidate_restart_lsn = restart_lsn;
1659+
SpinLockRelease(&slot->mutex);
16581660

16591661
/* our candidate can directly be used */
16601662
updated_lsn = true;
@@ -1665,7 +1667,7 @@ LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart
16651667
* might never end up updating if the receiver acks too slowly. A missed
16661668
* value here will just cause some extra effort after reconnecting.
16671669
*/
1668-
if (slot->candidate_restart_valid == InvalidXLogRecPtr)
1670+
else if (slot->candidate_restart_valid == InvalidXLogRecPtr)
16691671
{
16701672
slot->candidate_restart_valid = current_lsn;
16711673
slot->candidate_restart_lsn = restart_lsn;

0 commit comments

Comments
 (0)