Skip to content

Commit 8f8a5ae

Browse files
committed
Avoid having vacuum set reltuples to 0 on non-empty relations in the
presence of page pins, which leads to serious estimation errors in the planner. This particularly affects small heavily-accessed tables, especially where locking (e.g. from FK constraints) forces frequent vacuums for mxid cleanup. Fix by keeping separate track of pages whose live tuples were actually counted vs. pages that were only scanned for freezing purposes. Thus, reltuples can only be set to 0 if all pages of the relation were actually counted. Backpatch to all supported versions. Per bug #14057 from Nicolas Baccelli, analyzed by me. Discussion: https://postgr.es/m/20160331103739.8956.94469@wrigleys.postgresql.org
1 parent a7dcd95 commit 8f8a5ae

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ typedef struct LVRelStats
103103
BlockNumber old_rel_pages; /* previous value of pg_class.relpages */
104104
BlockNumber rel_pages; /* total number of pages */
105105
BlockNumber scanned_pages; /* number of pages we examined */
106-
double scanned_tuples; /* counts only tuples on scanned pages */
106+
BlockNumber tupcount_pages; /* pages whose tuples we counted */
107+
double scanned_tuples; /* counts only tuples on tupcount_pages */
107108
double old_rel_tuples; /* previous value of pg_class.reltuples */
108109
double new_rel_tuples; /* new estimated total # of tuples */
109110
BlockNumber pages_removed;
@@ -278,6 +279,10 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
278279
* density") with nonzero relpages and reltuples=0 (which means "zero
279280
* tuple density") unless there's some actual evidence for the latter.
280281
*
282+
* It's important that we use tupcount_pages and not scanned_pages for the
283+
* check described above; scanned_pages counts pages where we could not get
284+
* cleanup lock, and which were processed only for frozenxid purposes.
285+
*
281286
* We do update relallvisible even in the corner case, since if the table
282287
* is all-visible we'd definitely like to know that. But clamp the value
283288
* to be not more than what we're setting relpages to.
@@ -287,7 +292,7 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt,
287292
*/
288293
new_rel_pages = vacrelstats->rel_pages;
289294
new_rel_tuples = vacrelstats->new_rel_tuples;
290-
if (vacrelstats->scanned_pages == 0 && new_rel_pages > 0)
295+
if (vacrelstats->tupcount_pages == 0 && new_rel_pages > 0)
291296
{
292297
new_rel_pages = vacrelstats->old_rel_pages;
293298
new_rel_tuples = vacrelstats->old_rel_tuples;
@@ -446,6 +451,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
446451
nblocks = RelationGetNumberOfBlocks(onerel);
447452
vacrelstats->rel_pages = nblocks;
448453
vacrelstats->scanned_pages = 0;
454+
vacrelstats->tupcount_pages = 0;
449455
vacrelstats->nonempty_pages = 0;
450456
vacrelstats->latestRemovedXid = InvalidTransactionId;
451457

@@ -636,6 +642,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
636642
}
637643

638644
vacrelstats->scanned_pages++;
645+
vacrelstats->tupcount_pages++;
639646

640647
page = BufferGetPage(buf);
641648

@@ -1046,7 +1053,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
10461053
/* now we can compute the new value for pg_class.reltuples */
10471054
vacrelstats->new_rel_tuples = vac_estimate_reltuples(onerel, false,
10481055
nblocks,
1049-
vacrelstats->scanned_pages,
1056+
vacrelstats->tupcount_pages,
10501057
num_tuples);
10511058

10521059
/*
@@ -1350,7 +1357,7 @@ lazy_cleanup_index(Relation indrel,
13501357

13511358
ivinfo.index = indrel;
13521359
ivinfo.analyze_only = false;
1353-
ivinfo.estimated_count = (vacrelstats->scanned_pages < vacrelstats->rel_pages);
1360+
ivinfo.estimated_count = (vacrelstats->tupcount_pages < vacrelstats->rel_pages);
13541361
ivinfo.message_level = elevel;
13551362
ivinfo.num_heap_tuples = vacrelstats->new_rel_tuples;
13561363
ivinfo.strategy = vac_strategy;

0 commit comments

Comments
 (0)