Re: Conflict detection for update_deleted in logical replication

Amit Kapila <amit.kapila16@gmail.com>

From: Amit Kapila <amit.kapila16@gmail.com>
To: Dilip Kumar <dilipbalaut@gmail.com>
Cc: "Zhijie Hou (Fujitsu)" <houzj.fnst@fujitsu.com>, shveta malik <shveta.malik@gmail.com>, "Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>, pgsql-hackers <pgsql-hackers@postgresql.org>, vignesh C <vignesh21@gmail.com>, Nisha Moond <nisha.moond412@gmail.com>, Masahiko Sawada <sawada.mshk@gmail.com>
Date: 2025-08-01T11:15:59Z
Lists: pgsql-hackers
On Fri, Aug 1, 2025 at 4:20 PM Amit Kapila <amit.kapila16@gmail.com> wrote:
>
> On Fri, Aug 1, 2025 at 3:58 PM Dilip Kumar <dilipbalaut@gmail.com> wrote:
> >
> > 4.
> > +   /*
> > +    * Instead of invoking GetOldestNonRemovableTransactionId() for conflict
> > +    * detection, we use the conflict detection slot.xmin. This value will be
> > +    * greater than or equal to the other threshold and provides a more direct
> > +    * and efficient way to identify recently deleted dead tuples relevant to
> > +    * the conflict detection. The oldest_nonremovable_xid is not used here,
> > +    * as it is maintained only by the leader apply worker and unavailable to
> > +    * table sync and parallel apply workers.
> > +    */
> > +   slot = SearchNamedReplicationSlot(CONFLICT_DETECTION_SLOT, true);
> >
> > This comment seems a bit confusing to me, Isn't it actually correct to
> > just use the "conflict detection slot.xmin" even without any other
> > reasoning?
> >
>
> But it is *not* wrong to use even GetOldestNonRemovableTransactionId()
> because it will anyway consider conflict detection slot's xmin.
> However, the value returned by that function could be much older, so
> slot's xmin is a better choice. Similarly, it is sufficient to use
> oldest_nonremovable_xid value of apply worker and ideally would be
> better than slot's xmin because it could give update_deleted in fewer
> cases, however, we can't use that because of reasons mentioned in the
> comments. Do you think this comment needs improvement for clarity and
> if so, do you have any proposal?
>

How about something like:
/*
* For conflict detection, we use the conflict slot's xmin value instead of
* invoking GetOldestNonRemovableTransactionId(). The slot.xmin acts as a
* threshold to identify tuples that were recently deleted. These tuples are
* not visible to concurrent transactions, but we log an update_deleted conflict
* if such a tuple matches the remote update being applied.
*
* Although GetOldestNonRemovableTransactionId() can return a value older than
* the slot's xmin, for our current purpose it is acceptable to treat tuples
* deleted by transactions prior to slot.xmin as update_missing conflicts.
*
* Ideally, we would use oldest_nonremovable_xid, which is directly maintained
* by the leader apply worker. However, this value is not available to table
* synchronization or parallel apply workers, making slot.xmin a practical
* alternative in those contexts.
*/

-- 
With Regards,
Amit Kapila.



Commits

  1. Fix intermittent BF failures in 035_conflicts.

  2. Resume conflict-relevant data retention automatically.

  3. Fix intermittent test failure introduced in 6456c6e2c4.

  4. Fix Coverity issue reported in commit a850be2fe.

  5. Add test to prevent premature removal of conflict-relevant data.

  6. Post-commit review fixes for 228c370868.

  7. Add max_retention_duration option to subscriptions.

  8. Detect and report update_deleted conflicts.

  9. Preserve conflict-relevant data during logical replication.

  10. Integrate FullTransactionIds deeper into two-phase code

  11. Improve checks for GUC recovery_target_timeline

  12. Prevent excessive delays before launching new logrep workers.

  13. Keep WAL segments by slot's last saved restart LSN

  14. Rework some code handling pg_subscription data in psql and pg_dump

  15. Use generateClonedIndexStmt to propagate CREATE INDEX to partitions.