| From: | Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Jeff Davis <pgsql(at)j-davis(dot)com>, Álvaro Herrera <alvherre(at)kurilemu(dot)de> |
| Subject: | pg_commit_ts pages can be written before WAL is flushed |
| Date: | 2026-11-25 13:19:47 |
| Message-ID: | CAJTYsWUz62v3S9qbm0tKFJMfExvQudd8mU273DBcQrh+KynXdA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
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.
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]
https://postgr.es/m/62b070d14df4d9d5f44cb8da6f15546c30259053.camel@j-davis.com
Regards,
Ayush
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Flush-WAL-before-writing-commit-timestamp-pages.patch | application/octet-stream | 9.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Jim Jones | 2026-11-25 13:34:57 | Re: OOM kill in xmlelement() with large arrays |
| Previous Message | Abraham Pérez | 2026-11-25 13:13:29 | Re: Fix trigger memory leak |