postgresql/src/backend/access
Melanie Plageman fd4f12df5e Ensure vacuum removes all visibly dead tuples older than OldestXmin
If vacuum fails to remove a tuple with xmax older than
VacuumCutoffs->OldestXmin and younger than GlobalVisState->maybe_needed,
it may attempt to freeze the tuple's xmax and then ERROR out in
pre-freeze checks with "cannot freeze committed xmax".

Fix this by having vacuum always remove tuples older than OldestXmin.

It is possible for GlobalVisState->maybe_needed to precede OldestXmin if
maybe_needed is forced to go backward while vacuum is running. This can
happen if a disconnected standby with a running transaction older than
VacuumCutoffs->OldestXmin reconnects to the primary after vacuum
initially calculates GlobalVisState and OldestXmin.

In back branches starting with 14, the first version using
GlobalVisState, failing to remove tuples older than OldestXmin during
pruning caused vacuum to infinitely loop in lazy_scan_prune(), as
investigated on this [1] thread. After 1ccc1e05ae removed the retry loop
in lazy_scan_prune() and stopped comparing tuples to OldestXmin, the
hang could no longer happen, but we could still attempt to freeze dead
tuples with xmax older than OldestXmin -- resulting in an ERROR.

Fix this by always removing dead tuples with xmax older than
VacuumCutoffs->OldestXmin. This is okay because the standby won't replay
the tuple removal until the tuple is removable. Thus, the worst that can
happen is a recovery conflict.

[1] https://postgr.es/m/20240415173913.4zyyrwaftujxthf2%40awork3.anarazel.de#1b216b7768b5bd577a3d3d51bd5aadee

Back-patch through 14

Author: Melanie Plageman
Reviewed-by: Peter Geoghegan, Robert Haas, Andres Freund, Heikki Linnakangas, and Noah Misch
Discussion: https://postgr.es/m/CAAKRu_bDD7oq9ZwB2OJqub5BovMG6UjEYsoK2LVttadjEqyRGg%40mail.gmail.com
2024-07-19 12:12:03 -04:00
..
brin Harmonize function parameter names for Postgres 17. 2024-06-12 17:01:51 -04:00
common Use PqMsg_* macros in more places. 2024-07-17 10:51:00 -05:00
gin Revise GUC names quoting in messages again 2024-05-17 11:44:26 +02:00
gist Revert temporal primary keys and foreign keys 2024-05-16 08:17:46 +02:00
hash Fix inconsistency with replay of hash squeeze record for clean buffers 2024-04-11 09:20:51 +09:00
heap Ensure vacuum removes all visibly dead tuples older than OldestXmin 2024-07-19 12:12:03 -04:00
index Add missing index_insert_cleanup calls 2024-04-19 16:08:34 +02:00
nbtree Fix nbtree array unsatisfied inequality check. 2024-06-26 10:45:52 -04:00
rmgrdesc Do not summarize WAL if generated with wal_level=minimal. 2024-07-18 12:19:29 -04:00
sequence Fix comment thinko in sequence.c 2024-02-27 08:19:39 +09:00
spgist Fix insertion of SP-GiST REDIRECT tuples during REINDEX CONCURRENTLY. 2024-06-17 14:30:59 -04:00
table Revise GUC names quoting in messages again 2024-05-17 11:44:26 +02:00
tablesample Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
transam Do not summarize WAL if generated with wal_level=minimal. 2024-07-18 12:19:29 -04:00
Makefile Introduce sequence_*() access functions 2024-02-26 16:04:59 +09:00
meson.build Introduce sequence_*() access functions 2024-02-26 16:04:59 +09:00