Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f8a5ae

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 theplanner. This particularly affects small heavily-accessed tables,especially where locking (e.g. from FK constraints) forces frequentvacuums for mxid cleanup.Fix by keeping separate track of pages whose live tuples were actuallycounted vs. pages that were only scanned for freezing purposes. Thus,reltuples can only be set to 0 if all pages of the relation wereactually 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 parenta7dcd95 commit8f8a5ae

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
BlockNumberold_rel_pages;/* previous value of pg_class.relpages */
104104
BlockNumberrel_pages;/* total number of pages */
105105
BlockNumberscanned_pages;/* number of pages we examined */
106-
doublescanned_tuples;/* counts only tuples on scanned pages */
106+
BlockNumbertupcount_pages;/* pages whose tuples we counted */
107+
doublescanned_tuples;/* counts only tuples on tupcount_pages */
107108
doubleold_rel_tuples;/* previous value of pg_class.reltuples */
108109
doublenew_rel_tuples;/* new estimated total # of tuples */
109110
BlockNumberpages_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)

[8]ページ先頭

©2009-2025 Movatter.jp