Skip to content

Commit ad337c7

Browse files
committed
Update relation's stats in pg_class during vacuum full.
Hash index depends on estimation of numbers of tuples and pages of relations, incorrect value could be a reason of significantly growing of index. Vacuum full recreates heap and reindex all indexes before renewal stats. The patch fixes that, so indexes will see correct values. Backpatch to v10 only because earlier versions haven't usable hash index and growing of hash index is a single user-visible symptom. Author: Amit Kapila Reviewed-by: Ashutosh Sharma, me Discussion: https://www.postgresql.org/message-id/flat/20171115232922.5tomkxnw3iq6jsg7@inml.weebeastie.net
1 parent a2c8e5c commit ad337c7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/backend/commands/cluster.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
738738
Relation NewHeap,
739739
OldHeap,
740740
OldIndex;
741+
Relation relRelation;
742+
HeapTuple reltup;
743+
Form_pg_class relform;
741744
TupleDesc oldTupDesc;
742745
TupleDesc newTupDesc;
743746
int natts;
@@ -756,6 +759,7 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
756759
double num_tuples = 0,
757760
tups_vacuumed = 0,
758761
tups_recently_dead = 0;
762+
BlockNumber num_pages;
759763
int elevel = verbose ? INFO : DEBUG2;
760764
PGRUsage ru0;
761765

@@ -1079,6 +1083,8 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
10791083
/* Reset rd_toastoid just to be tidy --- it shouldn't be looked at again */
10801084
NewHeap->rd_toastoid = InvalidOid;
10811085

1086+
num_pages = RelationGetNumberOfBlocks(NewHeap);
1087+
10821088
/* Log what we did */
10831089
ereport(elevel,
10841090
(errmsg("\"%s\": found %.0f removable, %.0f nonremovable row versions in %u pages",
@@ -1098,6 +1104,30 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
10981104
index_close(OldIndex, NoLock);
10991105
heap_close(OldHeap, NoLock);
11001106
heap_close(NewHeap, NoLock);
1107+
1108+
/* Update pg_class to reflect the correct values of pages and tuples. */
1109+
relRelation = heap_open(RelationRelationId, RowExclusiveLock);
1110+
1111+
reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(OIDNewHeap));
1112+
if (!HeapTupleIsValid(reltup))
1113+
elog(ERROR, "cache lookup failed for relation %u", OIDNewHeap);
1114+
relform = (Form_pg_class) GETSTRUCT(reltup);
1115+
1116+
relform->relpages = num_pages;
1117+
relform->reltuples = num_tuples;
1118+
1119+
/* Don't update the stats for pg_class. See swap_relation_files. */
1120+
if (OIDOldHeap != RelationRelationId)
1121+
CatalogTupleUpdate(relRelation, &reltup->t_self, reltup);
1122+
else
1123+
CacheInvalidateRelcacheByTuple(reltup);
1124+
1125+
/* Clean up. */
1126+
heap_freetuple(reltup);
1127+
heap_close(relRelation, RowExclusiveLock);
1128+
1129+
/* Make the update visible */
1130+
CommandCounterIncrement();
11011131
}
11021132

11031133
/*

0 commit comments

Comments
 (0)