Fix relation statistics around transactional TRUNCATE

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, Alvaro Herrera <alvherre(at)kurilemu(dot)de>, Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Fix relation statistics around transactional TRUNCATE
Date: 2026-11-23 09:22:39
Message-ID: awQF360iTx5Q4xEA@bdtpg
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

While reviewing [1], I noticed that n_ins_since_vacuum can incorrectly retain
inserts performed before a committed TRUNCATE.

While preparing a fix, I found similar problems with n_dead_tup: committing or
aborting a TRUNCATE can retain counts belonging to the wrong relfilenode.

PFA 4 patches to $SUBJECT:

0001 fixes n_ins_since_vacuum for regular transactions (oversight in b07642dbcd8d)
0002 adds the corresponding 2PC handling.
0003 fixes n_dead_tup for regular transactions (oversight in d42358efb16)
0004 adds the corresponding 2PC handling.

The 2PC cases require storing both the current and pre-TRUNCATE counts:
COMMIT PREPARED uses the former, while ROLLBACK PREPARED restores the latter.

Since prepared transactions can survive a minor upgrade, recovery accepts both
record formats and distinguishes them by length. Both 2PC changes are intended
for the same minor release, so no intermediate format needs to be supported.

I propose adapting and backpatching them through 15. On the back branches, I think
that the new PgStat_Counter fields should be appended to the older structures (
PgStat_TableCounts and PgStat_TableXactStatus) to preserve existing member offsets.

[1]: https://postgr.es/m/avwEcRxMmL7o/hfm%40bdtpg

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachment Content-Type Size
v1-0001-Fix-n_ins_since_vacuum-after-aborted-inserts-and-.patch text/x-diff 17.7 KB
v1-0002-Handle-n_ins_since_vacuum-in-prepared-transaction.patch text/x-diff 21.8 KB
v1-0003-Fix-n_dead_tup-after-aborted-changes-and-TRUNCATE.patch text/x-diff 20.6 KB
v1-0004-Handle-n_dead_tup-in-prepared-transactions.patch text/x-diff 13.5 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeevan Chalke 2026-11-23 09:22:51 Re: Use-after-free of a shared Path in add_path()/add_partial_path()
Previous Message Tender Wang 2026-11-23 09:11:23 Re: Fix stale source values in MERGE RETURNING after a cross-partition update