Skip to content

Commit bf0d21e

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 9a1c6dd commit bf0d21e

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
@@ -166,7 +166,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
166166
BlockNumber possibly_freeable;
167167
PGRUsage ru0;
168168
TimestampTz starttime = 0;
169-
bool scan_all;
169+
bool scan_all; /* should we scan all pages? */
170+
bool scanned_all; /* did we actually scan all pages? */
170171
TransactionId freezeTableLimit;
171172
BlockNumber new_rel_pages;
172173
double new_rel_tuples;
@@ -209,6 +210,21 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
209210
/* Done with indexes */
210211
vac_close_indexes(nindexes, Irel, NoLock);
211212

213+
/*
214+
* Compute whether we actually scanned the whole relation. If we did, we
215+
* can adjust relfrozenxid.
216+
*
217+
* NB: We need to check this before truncating the relation, because that
218+
* will change ->rel_pages.
219+
*/
220+
if (vacrelstats->scanned_pages < vacrelstats->rel_pages)
221+
{
222+
Assert(!scan_all);
223+
scanned_all = false;
224+
}
225+
else
226+
scanned_all = true;
227+
212228
/*
213229
* Optionally truncate the relation.
214230
*
@@ -245,9 +261,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
245261
new_rel_tuples = vacrelstats->old_rel_tuples;
246262
}
247263

248-
new_frozen_xid = FreezeLimit;
249-
if (vacrelstats->scanned_pages < vacrelstats->rel_pages)
250-
new_frozen_xid = InvalidTransactionId;
264+
new_frozen_xid = scanned_all ? FreezeLimit : InvalidTransactionId;
251265

252266
vac_update_relstats(onerel,
253267
new_rel_pages, new_rel_tuples,

0 commit comments

Comments
 (0)