Skip to content

Commit 73ed6d6

Browse files
committed
Remove lazy_update_relstats; go back to having VACUUM just record the
actual number of unremoved tuples as pg_class.reltuples. The idea of trying to estimate a steady state condition still seems attractive, but this particular implementation crashed and burned ...
1 parent adb1a6e commit 73ed6d6

File tree

1 file changed

+13
-60
lines changed

1 file changed

+13
-60
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
*
3333
* IDENTIFICATION
34-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.51 2005/03/20 22:00:52 tgl Exp $
34+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.52 2005/03/25 22:51:31 tgl Exp $
3535
*
3636
*-------------------------------------------------------------------------
3737
*/
@@ -107,10 +107,6 @@ static int lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
107107
static void lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats);
108108
static BlockNumber count_nondeletable_pages(Relation onerel,
109109
LVRelStats *vacrelstats);
110-
static void lazy_update_relstats(Relation rel, BlockNumber num_pages,
111-
BlockNumber pages_removed,
112-
double num_tuples, double tuples_removed,
113-
bool hasindex);
114110
static void lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks);
115111
static void lazy_record_dead_tuple(LVRelStats *vacrelstats,
116112
ItemPointer itemptr);
@@ -180,10 +176,10 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
180176
lazy_update_fsm(onerel, vacrelstats);
181177

182178
/* Update statistics in pg_class */
183-
lazy_update_relstats(onerel, vacrelstats->rel_pages,
184-
vacrelstats->pages_removed,
185-
vacrelstats->rel_tuples, vacrelstats->tuples_deleted,
186-
hasindex);
179+
vac_update_relstats(RelationGetRelid(onerel),
180+
vacrelstats->rel_pages,
181+
vacrelstats->rel_tuples,
182+
hasindex);
187183
}
188184

189185

@@ -622,9 +618,10 @@ lazy_scan_index(Relation indrel, LVRelStats *vacrelstats)
622618
return;
623619

624620
/* now update statistics in pg_class */
625-
lazy_update_relstats(indrel, stats->num_pages, stats->pages_removed,
626-
stats->num_index_tuples, stats->tuples_removed,
627-
false);
621+
vac_update_relstats(RelationGetRelid(indrel),
622+
stats->num_pages,
623+
stats->num_index_tuples,
624+
false);
628625

629626
ereport(elevel,
630627
(errmsg("index \"%s\" now contains %.0f row versions in %u pages",
@@ -697,9 +694,10 @@ lazy_vacuum_index(Relation indrel,
697694
*index_pages_removed += stats->pages_removed;
698695

699696
/* now update statistics in pg_class */
700-
lazy_update_relstats(indrel, stats->num_pages, *index_pages_removed,
701-
stats->num_index_tuples, *index_tups_vacuumed,
702-
false);
697+
vac_update_relstats(RelationGetRelid(indrel),
698+
stats->num_pages,
699+
stats->num_index_tuples,
700+
false);
703701

704702
ereport(elevel,
705703
(errmsg("index \"%s\" now contains %.0f row versions in %u pages",
@@ -922,51 +920,6 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
922920
return vacrelstats->nonempty_pages;
923921
}
924922

925-
/*
926-
* lazy_update_relstats - update pg_class statistics for a table or index
927-
*
928-
* We always want to set relpages to an accurate value. However, for lazy
929-
* VACUUM it seems best to set reltuples to the average of the number of
930-
* rows before vacuuming and the number after vacuuming, rather than just
931-
* using the number after vacuuming. This will result in the best average
932-
* performance in a steady-state situation where VACUUMs are performed
933-
* regularly on a table of roughly constant size, assuming that the physical
934-
* number of pages in the table stays about the same throughout. (Note that
935-
* we do not apply the same logic to VACUUM FULL, because it repacks the table
936-
* and thereby boosts the tuple density.)
937-
*
938-
* An important point is that when the table size has decreased a lot during
939-
* vacuuming, the old reltuples count might give an overestimate of the tuple
940-
* density. We handle this by scaling down the old reltuples count by the
941-
* fraction by which the table has shortened before we merge it with the
942-
* new reltuples count. In particular this means that when relpages goes to
943-
* zero, reltuples will immediately go to zero as well, causing the planner
944-
* to fall back on other estimation procedures as the table grows again.
945-
*
946-
* Because we do this math independently for the table and the indexes, it's
947-
* quite possible to end up with an index's reltuples different from the
948-
* table's, which is silly except in the case of partial indexes. We don't
949-
* worry too much about that here; the planner contains filtering logic to
950-
* ensure it only uses sane estimates.
951-
*/
952-
static void
953-
lazy_update_relstats(Relation rel, BlockNumber num_pages,
954-
BlockNumber pages_removed,
955-
double num_tuples, double tuples_removed,
956-
bool hasindex)
957-
{
958-
double old_num_tuples;
959-
960-
old_num_tuples = num_tuples + tuples_removed;
961-
if (pages_removed > 0)
962-
old_num_tuples *= (double) num_pages / (double) (num_pages + pages_removed);
963-
if (old_num_tuples > num_tuples)
964-
num_tuples = ceil((num_tuples + old_num_tuples) * 0.5);
965-
966-
vac_update_relstats(RelationGetRelid(rel), num_pages, num_tuples,
967-
hasindex);
968-
}
969-
970923
/*
971924
* lazy_space_alloc - space allocation decisions for lazy vacuum
972925
*

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