Skip to content

Commit 0b132b9

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 0cedfa7 commit 0b132b9

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
@@ -172,7 +172,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
172172
int usecs;
173173
double read_rate,
174174
write_rate;
175-
bool scan_all;
175+
bool scan_all; /* should we scan all pages? */
176+
bool scanned_all; /* did we actually scan all pages? */
176177
TransactionId freezeTableLimit;
177178
BlockNumber new_rel_pages;
178179
double new_rel_tuples;
@@ -217,6 +218,21 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
217218
/* Done with indexes */
218219
vac_close_indexes(nindexes, Irel, NoLock);
219220

221+
/*
222+
* Compute whether we actually scanned the whole relation. If we did, we
223+
* can adjust relfrozenxid.
224+
*
225+
* NB: We need to check this before truncating the relation, because that
226+
* will change ->rel_pages.
227+
*/
228+
if (vacrelstats->scanned_pages < vacrelstats->rel_pages)
229+
{
230+
Assert(!scan_all);
231+
scanned_all = false;
232+
}
233+
else
234+
scanned_all = true;
235+
220236
/*
221237
* Optionally truncate the relation.
222238
*
@@ -261,9 +277,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
261277
if (new_rel_allvisible > new_rel_pages)
262278
new_rel_allvisible = new_rel_pages;
263279

264-
new_frozen_xid = FreezeLimit;
265-
if (vacrelstats->scanned_pages < vacrelstats->rel_pages)
266-
new_frozen_xid = InvalidTransactionId;
280+
new_frozen_xid = scanned_all ? FreezeLimit : InvalidTransactionId;
267281

268282
vac_update_relstats(onerel,
269283
new_rel_pages,

0 commit comments

Comments
 (0)