2pc gxact lsn race

From: Maxim Orlov <orlovmg(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: 2pc gxact lsn race
Date: 2026-10-29 14:58:40
Message-ID: CACG=ezYJmG__qpRzgchtSe=KbU_HVTfjj3e=TV4wKT461jZ4tw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi!

While playing around with two-phase transactions, I've noticed something
strange. The prepare_start_lsn and prepare_end_lsn fields in gxact are
modified within CheckPointTwoPhase() without the use of proper locking,
which means they could become inconsistent.

The original commit by Tom (e26b0abda391, from 2005) only applied a
SHARED lock. Later, Simon Riggs added commit 728bd991c3c4, "Speedup 2PC
recovery," which moved the invalidating of these fields from the
MarkAsPreparing() to CheckPointTwoPhase(), but no locking was
implemented in this new location.

I suspected that this issue would be easy to reproduce using injection
points, and that turned out to be the case. There are three problematic
places: the calls to FinishPreparedTransaction(), EndPrepare(), and
LookupGXact(). In one instance, running the tests triggers an assertion
failure; in others, the logic governing two-phase transactions is
compromised. I totally understand that this is a *very* rare scenario
in real-world use, but it is still something worth being aware of.

As for a fix, I’ve implemented some ad-hoc solutions, PFA patch set.
Actually, I don't really understand why the invalidation of the
prepare_start_lsn and prepare_end_lsn fields is required within
CheckPointTwoPhase(). Is it possible to get rid of them entirely without
breaking something?

Thoughts?

--
Best regards,
Maxim Orlov.

Attachment Content-Type Size
v1-0002-Add-2pc-race-test-between-CheckPointTwoPhase-and-.patch application/octet-stream 4.7 KB
v1-0003-Add-2pc-race-test-between-CheckPointTwoPhase-and-.patch application/octet-stream 7.2 KB
v1-0001-Add-2pc-race-test-between-CheckPointTwoPhase-and-.patch application/octet-stream 4.5 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhijie Hou 2026-10-29 15:14:32 Re: Avoid resetting subscription two_phase state to pending when already enabled
Previous Message Robert Haas 2026-10-29 14:55:20 Re: pg_*_advice: tsv load failure, etc.