88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.254 2005/05/06 17 :24:52 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.255 2005/05/11 06 :24:54 neilc Exp $
1212 *
1313 *
1414 * INTERFACE ROUTINES
@@ -62,6 +62,7 @@ static void UpdateIndexRelation(Oid indexoid, Oid heapoid,
6262 Oid * classOids ,
6363 bool primary );
6464static Oid IndexGetRelation (Oid indexId );
65+ static void UpdateStats (Oid relid , double reltuples );
6566
6667
6768/*
@@ -1149,6 +1150,36 @@ setNewRelfilenode(Relation relation)
11491150}
11501151
11511152
1153+ /*
1154+ * This is invoked by the various index AMs once they have finished
1155+ * constructing an index. Constructing an index involves counting the
1156+ * number of tuples in both the relation and the index, so we take
1157+ * advantage of the opportunity to update pg_class to ensure that the
1158+ * planner takes advantage of the index we just created. But, only
1159+ * update statistics during normal index definitions, not for indices
1160+ * on system catalogs created during bootstrap processing. We must
1161+ * close the relations before updating statistics to guarantee that
1162+ * the relcache entries are flushed when we increment the command
1163+ * counter in UpdateStats(). But we do not release any locks on the
1164+ * relations; those will be held until end of transaction.
1165+ */
1166+ void
1167+ IndexCloseAndUpdateStats (Relation heap , double heapTuples ,
1168+ Relation index , double indexTuples )
1169+ {
1170+ Oid hrelid = RelationGetRelid (heap );
1171+ Oid irelid = RelationGetRelid (index );
1172+
1173+ if (!IsNormalProcessingMode ())
1174+ return ;
1175+
1176+ heap_close (heap , NoLock );
1177+ index_close (index );
1178+ UpdateStats (hrelid , heapTuples );
1179+ UpdateStats (irelid , indexTuples );
1180+ }
1181+
1182+
11521183/* ----------------
11531184 * UpdateStats
11541185 *
@@ -1157,7 +1188,7 @@ setNewRelfilenode(Relation relation)
11571188 * in the context of VACUUM, only CREATE INDEX.
11581189 * ----------------
11591190 */
1160- void
1191+ static void
11611192UpdateStats (Oid relid , double reltuples )
11621193{
11631194 Relation whichRel ;
0 commit comments