pg_commit_ts pages can be written before WAL is flushed
Thread notes
1 note#bug#not-a-bug
hackorum-bot#bug#not-a-bug
@global #bug #not-a-bug
Hi,
While re-testing Jeff's patch [1]/messages/by-id/62b070d14df4d9d5f44cb8da6f15546c30259053.camel@j-davis.com, I found a similar issue with
pg_commit_ts. After a crash, the row I had inserted was gone and
pg_xact_status() said aborted, which is fine for an async commit, but
pg_xact_commit_timestamp() still returned its commit timestamp
instead of NULL.
I used track_commit_timestamp = on, commit_timestamp_buffers = 16 and
wal_writer_delay = 10s on the primary. I set synchronous_commit = off
for the transaction that did the INSERT and left it off for the next
20,000 commits to evict its timestamp page. Then I did an immediate
shutdown. Happy to share the script if it's useful.
AFAICS the commit_ts page just got written out before the commit
record. I think setting the timestamp in memory early is fine, since
redo would set it again from the commit record, but the page shouldn't
reach disk before the commit record does. For async commits, pg_xact
passes the commit LSN to SLRU so SlruPhysicalWritePage() can flush WAL
first. pg_commit_ts doesn't pass an LSN.
On a standby, writing the timestamp page doesn't advance
minRecoveryPoint, much like the pg_xact case Jeff fixed. If the
standby then crashes and I recover the same data directory to just
before the commit, the timestamp from the earlier replay can still be
there. A fresh restore that never replayed the commit wouldn't have it.
IIUC, pg_commit_ts doesn't need to flush WAL at every commit. The
attached patch keeps the highest commit LSN for each dirty page. Before
writing the page, SLRU makes sure WAL is flushed at least through the
end of that commit record.
I don't know the pg_commit_ts code that well, so I may be missing
something. Is there a reason not to use the same SLRU flush as pg_xact
here?
[1]: /messages/by-id/62b070d14df4d9d5f44cb8da6f15546c30259053.camel@j-davis.com
/messages/by-id/62b070d14df4d9d5f44cb8da6f15546c30259053.camel@j-davis.com
Regards,
Ayush
Hello
On 2026-Nov-25, Ayush Tiwari wrote:
While re-testing Jeff's patch [1], I found a similar issue with
pg_commit_ts. After a crash, the row I had inserted was gone and
pg_xact_status() said aborted, which is fine for an async commit, but
pg_xact_commit_timestamp() still returned its commit timestamp
instead of NULL.
Hmm, I vaguely recall that the reason for this is that it's not supposed
to matter. pg_clog is the authoritative source to indicate whether a
transaction committed; if you inquire for the commit timestamp of a
transaction that didn't commit, it's your fault. When the transaction
commits again (I mean, the next time a transaction gets the same Xid
value), the commit_ts record would be updated to the new value, so
there's no harm.
I'm not opposed to consider this change in master, but unless there's a
reason to think that this is a problem in practice, I wouldn't bother
backpatching.
--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
"Puedes vivir sólo una vez, pero si lo haces bien, una vez es suficiente"
Hi,
On Wed, 25 Nov 2026 at 19:18, Álvaro Herrera <alvherre@kurilemu.de> wrote:
Hello
On 2026-Nov-25, Ayush Tiwari wrote:
While re-testing Jeff's patch [1], I found a similar issue with
pg_commit_ts. After a crash, the row I had inserted was gone and
pg_xact_status() said aborted, which is fine for an async commit, but
pg_xact_commit_timestamp() still returned its commit timestamp
instead of NULL.Hmm, I vaguely recall that the reason for this is that it's not supposed
to matter. pg_clog is the authoritative source to indicate whether a
transaction committed; if you inquire for the commit timestamp of a
transaction that didn't commit, it's your fault. When the transaction
commits again (I mean, the next time a transaction gets the same Xid
value), the commit_ts record would be updated to the new value, so
there's no harm.I'm not opposed to consider this change in master, but unless there's a
reason to think that this is a problem in practice, I wouldn't bother
backpatching.
Yeah, that makes sense. I'd expected NULL for an aborted xid, but I
don't have a case where this causes trouble in practice. If the change
seems worth doing, I'm happy to keep it to master.
Regards,
Ayush