Fix relation statistics around transactional TRUNCATE

Started by Bertrand Drouvot6 days ago1 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Bertrand Drouvot
bertranddrouvot.pg@gmail.com

Hi hackers,

While reviewing [1]/messages/by-id/avwEcRxMmL7o/hfm@bdtpg, 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]: /messages/by-id/avwEcRxMmL7o/hfm@bdtpg

Regards,

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

Attachments:

t259656_1
v1-0001-Fix-n_ins_since_vacuum-after-aborted-inserts-and-.patchtext/x-diff; charset=us-asciiDownload+154-21
v1-0002-Handle-n_ins_since_vacuum-in-prepared-transaction.patchtext/x-diff; charset=us-asciiDownload+161-21
v1-0003-Fix-n_dead_tup-after-aborted-changes-and-TRUNCATE.patchtext/x-diff; charset=us-asciiDownload+308-33
v1-0004-Handle-n_dead_tup-in-prepared-transactions.patchtext/x-diff; charset=us-asciiDownload+76-18