Report
pg_xact_commit_timestamp() fails with "could not open file pg_commit_ts/..." when VACUUM truncates pg_commit_ts concurrently, instead of returning NULL, because the lookup checks the lower bound and then reads the SLRU page with no interlock against truncation
- Severity
- 22
- Blamed commit
- 73c986adde5d73a5e2555da9b5c8facedb146dcd
On 13 branches
REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
TransactionIdGetCommitTsData() reads oldestCommitTsXid under CommitTsLock, releases the lock, compares the XID against it, and only then reads the pg_commit_ts page. vac_truncate_clog() can advance oldestCommitTsXid and unlink the segment in between (and so can COMMIT_TS_TRUNCATE replay on a standby). The lookup then reads a segment that no longer exists and raises an ERROR. If it ran a moment later it would see the new bound and return NULL.
pg_xact_status() had the same race against CLOG truncation. That was fixed by holding XactTruncationLock across the bound check and the read. Commit timestamps have no equivalent interlock.
Reproduction
Use one cluster with track_commit_timestamp = on. Run a stock build on a machine with several CPUs.
- Run
UPDATE pg_database SET datallowconn = true WHERE datname = 'template0'; - Repeat the following a few times:
-
Use about 28000 XIDs, for example by running pgbench -n -c 4 -t 7000 with a script containing
SELECT txid_current();. -
Let lo =
SELECT min(datfrozenxid::text::bigint) FROM pg_database; -
Start 12 sessions, each running:
SELECT count(*) FROM generate_series(1, 3000000) g, LATERAL (SELECT pg_xact_commit_timestamp( (lo + (g * 7919 + n) % 26000)::text::xid)) z; -
While they run, execute
VACUUM (FREEZE)in template0, template1 and postgres, which truncates pg_commit_ts.
-
Expected: every lookup returns a timestamp, or NULL for XIDs below the new oldest bound.
Actual: in about one round in three, a reader fails with:
ERROR: could not open file "pg_commit_ts/0002": No such file or
directory
DETAIL: Could not access commit timestamp of transaction <xid>.
Run on its own afterwards, the same call returns NULL. Adding a short sleep before SimpleLruReadPage_ReadOnly() in TransactionIdGetCommitTsData() makes this deterministic. With that delay, the same error also occurs for a query on a hot standby (with hot_standby_feedback on) while it replays COMMIT_TS_TRUNCATE.
Analysis
commit_ts.c, TransactionIdGetCommitTsData(): the code copies oldestCommitTsXid and newestCommitTsXid under CommitTsLock, then releases the lock before the range test and before SimpleLruReadPage_ReadOnly(). vac_truncate_clog() calls AdvanceOldestCommitTsXid() and then TruncateCommitTs(), and SimpleLruTruncate() drops the buffered pages and unlinks the segment. A reader that passed the test with the old bound then misses in the SLRU buffers and fails to open the file. commit_ts_redo() does the same on a standby.
For CLOG, TransactionIdInRecentPast() requires XactTruncationLock in share mode across the check and the lookup, and TruncateCLOG() advances oldestClogXid under that lock in exclusive mode. Commit timestamps have kept the unlocked check-then-read since they were added in 73c986adde5d73a5e2555da9b5c8facedb146dcd.
Notes
Tested on master b7822f9992216f9aa85422337c15f1e7a8d6db72. This is separate from the existing report about the COMMIT_TS_TRUNCATE record not being flushed before unlink (01a1f1fd-6b01-755d-9fd1-db71fd64bdba). That one needs a crash; this race happens on a running server. Logical replication conflict detection looks up commit timestamps only for unfrozen tuple xmins, which are above the truncation point, so it is not affected.
Branches
Reproduced on master. The unlocked check-then-read dates from the commit that added commit timestamps, which is in every supported branch; the release branches were not tested.