Skip to content

Commit 8a7f446

Browse files
committed
Don't update relfrozenxid if any pages were skipped.
Vacuum recognizes that it can update relfrozenxid by checking whether it has processed all pages of a relation. Unfortunately it performed that check after truncating the dead pages at the end of the relation, and used the new number of pages to decide whether all pages have been scanned. If the new number of pages happened to be smaller or equal to the number of pages scanned, it incorrectly decided that all pages were scanned. This can lead to relfrozenxid being updated, even though some pages were skipped that still contain old XIDs. That can lead to data loss due to xid wraparounds with some rows suddenly missing. This likely has escaped notice so far because it takes a large number (~2^31) of xids being used to see the effect, while a full-table vacuum before that would fix the issue. The incorrect logic was introduced by commit b4b6923. Backpatch this fix down to 8.4, like that commit. Andres Freund, with some modifications by me.
1 parent 3b41a7c commit 8a7f446

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
156156
BlockNumber possibly_freeable;
157157
PGRUsage ru0;
158158
TimestampTz starttime = 0;
159-
bool scan_all;
159+
bool scan_all; /* should we scan all pages? */
160+
bool scanned_all; /* did we actually scan all pages? */
160161
TransactionId freezeTableLimit;
161162
BlockNumber new_rel_pages;
162163
double new_rel_tuples;
@@ -198,6 +199,21 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
198199
/* Done with indexes */
199200
vac_close_indexes(nindexes, Irel, NoLock);
200201

202+
/*
203+
* Compute whether we actually scanned the whole relation. If we did, we
204+
* can adjust relfrozenxid.
205+
*
206+
* NB: We need to check this before truncating the relation, because that
207+
* will change ->rel_pages.
208+
*/
209+
if (vacrelstats->scanned_pages < vacrelstats->rel_pages)
210+
{
211+
Assert(!scan_all);
212+
scanned_all = false;
213+
}
214+
else
215+
scanned_all = true;
216+
201217
/*
202218
* Optionally truncate the relation.
203219
*
@@ -244,9 +260,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
244260
new_rel_tuples = vacrelstats->old_rel_tuples;
245261
}
246262

247-
new_frozen_xid = FreezeLimit;
248-
if (vacrelstats->scanned_pages < vacrelstats->rel_pages)
249-
new_frozen_xid = InvalidTransactionId;
263+
new_frozen_xid = scanned_all ? FreezeLimit : InvalidTransactionId;
250264

251265
vac_update_relstats(onerel,
252266
new_rel_pages, new_rel_tuples,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy