Skip to content

Commit e684594

Browse files
author
Amit Kapila
committed
Don't retreat slot's confirmed_flush LSN.
Prevent moving the confirmed_flush backwards, as this could lead to data duplication issues caused by replicating already replicated changes. This can happen when a client acknowledges an LSN it doesn't have to do anything for, and thus didn't store persistently. After a restart, the client can send the prior LSN that it stored persistently as an acknowledgement, but we need to ignore such an LSN to avoid retreating confirm_flush LSN. Diagnosed-by: Zhijie Hou <houzj.fnst@fujitsu.com> Author: shveta malik <shveta.malik@gmail.com> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com> Tested-by: Nisha Moond <nisha.moond412@gmail.com> Backpatch-through: 13 Discussion: https://postgr.es/m/CAJpy0uDZ29P=BYB1JDWMCh-6wXaNqMwG1u1mB4=10Ly0x7HhwQ@mail.gmail.com Discussion: https://postgr.es/m/OS0PR01MB57164AB5716AF2E477D53F6F9489A@OS0PR01MB5716.jpnprd01.prod.outlook.com
1 parent a1e0e70 commit e684594

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/backend/replication/logical/logical.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,19 @@ LogicalConfirmReceivedLocation(XLogRecPtr lsn)
17181718

17191719
SpinLockAcquire(&MyReplicationSlot->mutex);
17201720

1721-
MyReplicationSlot->data.confirmed_flush = lsn;
1721+
/*
1722+
* Prevent moving the confirmed_flush backwards, as this could lead to
1723+
* data duplication issues caused by replicating already replicated
1724+
* changes.
1725+
*
1726+
* This can happen when a client acknowledges an LSN it doesn't have
1727+
* to do anything for, and thus didn't store persistently. After a
1728+
* restart, the client can send the prior LSN that it stored
1729+
* persistently as an acknowledgement, but we need to ignore such an
1730+
* LSN. See similar case handling in CreateDecodingContext.
1731+
*/
1732+
if (lsn > MyReplicationSlot->data.confirmed_flush)
1733+
MyReplicationSlot->data.confirmed_flush = lsn;
17221734

17231735
/* if we're past the location required for bumping xmin, do so */
17241736
if (MyReplicationSlot->candidate_xmin_lsn != InvalidXLogRecPtr &&
@@ -1783,7 +1795,14 @@ LogicalConfirmReceivedLocation(XLogRecPtr lsn)
17831795
else
17841796
{
17851797
SpinLockAcquire(&MyReplicationSlot->mutex);
1786-
MyReplicationSlot->data.confirmed_flush = lsn;
1798+
1799+
/*
1800+
* Prevent moving the confirmed_flush backwards. See comments above
1801+
* for the details.
1802+
*/
1803+
if (lsn > MyReplicationSlot->data.confirmed_flush)
1804+
MyReplicationSlot->data.confirmed_flush = lsn;
1805+
17871806
SpinLockRelease(&MyReplicationSlot->mutex);
17881807
}
17891808
}

0 commit comments

Comments
 (0)