This patch refactors away some duplicated code in the index AM build
authorNeil Conway <[email protected]>
Wed, 11 May 2005 06:24:55 +0000 (06:24 +0000)
committerNeil Conway <[email protected]>
Wed, 11 May 2005 06:24:55 +0000 (06:24 +0000)
methods: they all invoke UpdateStats() since they have computed the
number of heap tuples, so I created a function in catalog/index.c that
each AM now calls.

src/backend/access/gist/gist.c
src/backend/access/hash/hash.c
src/backend/access/nbtree/nbtree.c
src/backend/access/rtree/rtree.c
src/backend/catalog/index.c
src/include/catalog/index.h

index 008054f0c1fb1b6281c5090c42e88df415e0197c..506884458dfbe13835ea6af5ad0e9187cea57a1f 100644 (file)
@@ -184,27 +184,8 @@ gistbuild(PG_FUNCTION_ARGS)
 
        /* okay, all heap tuples are indexed */
 
-       /*
-        * Since we just counted the tuples in the heap, we update its stats
-        * in pg_class to guarantee that the planner takes advantage of the
-        * index we just created.  But, only update statistics during normal
-        * index definitions, not for indices on system catalogs created
-        * during bootstrap processing.  We must close the relations before
-        * updating statistics to guarantee that the relcache entries are
-        * flushed when we increment the command counter in UpdateStats(). But
-        * we do not release any locks on the relations; those will be held
-        * until end of transaction.
-        */
-       if (IsNormalProcessingMode())
-       {
-               Oid                     hrelid = RelationGetRelid(heap);
-               Oid                     irelid = RelationGetRelid(index);
-
-               heap_close(heap, NoLock);
-               index_close(index);
-               UpdateStats(hrelid, reltuples);
-               UpdateStats(irelid, buildstate.indtuples);
-       }
+       /* since we just counted the # of tuples, may as well update stats */
+       IndexCloseAndUpdateStats(heap, reltuples, index, buildstate.indtuples);
 
        freeGISTstate(&buildstate.giststate);
 #ifdef GISTDEBUG
index e449a64736da26334cc55aa0d5c552c1ee99dd18..be08b6c1af4eca555ef341b9d7e3bc4af70c055a 100644 (file)
@@ -72,27 +72,8 @@ hashbuild(PG_FUNCTION_ARGS)
        reltuples = IndexBuildHeapScan(heap, index, indexInfo,
                                                                hashbuildCallback, (void *) &buildstate);
 
-       /*
-        * Since we just counted the tuples in the heap, we update its stats
-        * in pg_class to guarantee that the planner takes advantage of the
-        * index we just created.  But, only update statistics during normal
-        * index definitions, not for indices on system catalogs created
-        * during bootstrap processing.  We must close the relations before
-        * updating statistics to guarantee that the relcache entries are
-        * flushed when we increment the command counter in UpdateStats(). But
-        * we do not release any locks on the relations; those will be held
-        * until end of transaction.
-        */
-       if (IsNormalProcessingMode())
-       {
-               Oid                     hrelid = RelationGetRelid(heap);
-               Oid                     irelid = RelationGetRelid(index);
-
-               heap_close(heap, NoLock);
-               index_close(index);
-               UpdateStats(hrelid, reltuples);
-               UpdateStats(irelid, buildstate.indtuples);
-       }
+       /* since we just counted the # of tuples, may as well update stats */
+       IndexCloseAndUpdateStats(heap, reltuples, index, buildstate.indtuples);
 
        PG_RETURN_VOID();
 }
index 83999612460685a59ad0803e1ce6764c38a40212..d9eb88247239572c08f2a077a5a815e2fcce92e1 100644 (file)
@@ -148,27 +148,8 @@ btbuild(PG_FUNCTION_ARGS)
        }
 #endif   /* BTREE_BUILD_STATS */
 
-       /*
-        * Since we just counted the tuples in the heap, we update its stats
-        * in pg_class to guarantee that the planner takes advantage of the
-        * index we just created.  But, only update statistics during normal
-        * index definitions, not for indices on system catalogs created
-        * during bootstrap processing.  We must close the relations before
-        * updating statistics to guarantee that the relcache entries are
-        * flushed when we increment the command counter in UpdateStats(). But
-        * we do not release any locks on the relations; those will be held
-        * until end of transaction.
-        */
-       if (IsNormalProcessingMode())
-       {
-               Oid                     hrelid = RelationGetRelid(heap);
-               Oid                     irelid = RelationGetRelid(index);
-
-               heap_close(heap, NoLock);
-               index_close(index);
-               UpdateStats(hrelid, reltuples);
-               UpdateStats(irelid, buildstate.indtuples);
-       }
+       /* since we just counted the # of tuples, may as well update stats */
+       IndexCloseAndUpdateStats(heap, reltuples, index, buildstate.indtuples);
 
        PG_RETURN_VOID();
 }
index 88b9a3524883ec7bbded38d064de61dd054d43cd..e7e731a3e5f22052dac15602a5c271907ebcc1fd 100644 (file)
@@ -142,27 +142,8 @@ rtbuild(PG_FUNCTION_ARGS)
 
        /* okay, all heap tuples are indexed */
 
-       /*
-        * Since we just counted the tuples in the heap, we update its stats
-        * in pg_class to guarantee that the planner takes advantage of the
-        * index we just created.  But, only update statistics during normal
-        * index definitions, not for indices on system catalogs created
-        * during bootstrap processing.  We must close the relations before
-        * updating statistics to guarantee that the relcache entries are
-        * flushed when we increment the command counter in UpdateStats(). But
-        * we do not release any locks on the relations; those will be held
-        * until end of transaction.
-        */
-       if (IsNormalProcessingMode())
-       {
-               Oid                     hrelid = RelationGetRelid(heap);
-               Oid                     irelid = RelationGetRelid(index);
-
-               heap_close(heap, NoLock);
-               index_close(index);
-               UpdateStats(hrelid, reltuples);
-               UpdateStats(irelid, buildstate.indtuples);
-       }
+       /* since we just counted the # of tuples, may as well update stats */
+       IndexCloseAndUpdateStats(heap, reltuples, index, buildstate.indtuples);
 
        PG_RETURN_VOID();
 }
index a42cdfff6ee3d8d403d86db7d293936af65ed0be..1ec282484532b6ec11eb81365a189f9ad75e46d5 100644 (file)
@@ -62,6 +62,7 @@ static void UpdateIndexRelation(Oid indexoid, Oid heapoid,
                                        Oid *classOids,
                                        bool primary);
 static Oid     IndexGetRelation(Oid indexId);
+static void UpdateStats(Oid relid, double reltuples);
 
 
 /*
@@ -1149,6 +1150,36 @@ setNewRelfilenode(Relation relation)
 }
 
 
+/*
+ * This is invoked by the various index AMs once they have finished
+ * constructing an index. Constructing an index involves counting the
+ * number of tuples in both the relation and the index, so we take
+ * advantage of the opportunity to update pg_class to ensure that the
+ * planner takes advantage of the index we just created.  But, only
+ * update statistics during normal index definitions, not for indices
+ * on system catalogs created during bootstrap processing.  We must
+ * close the relations before updating statistics to guarantee that
+ * the relcache entries are flushed when we increment the command
+ * counter in UpdateStats(). But we do not release any locks on the
+ * relations; those will be held until end of transaction.
+ */
+void
+IndexCloseAndUpdateStats(Relation heap, double heapTuples,
+                                                Relation index, double indexTuples)
+{
+       Oid             hrelid = RelationGetRelid(heap);
+       Oid             irelid = RelationGetRelid(index);
+
+       if (!IsNormalProcessingMode())
+               return;
+
+       heap_close(heap, NoLock);
+       index_close(index);
+       UpdateStats(hrelid, heapTuples);
+       UpdateStats(irelid, indexTuples);
+}
+
+
 /* ----------------
  *             UpdateStats
  *
@@ -1157,7 +1188,7 @@ setNewRelfilenode(Relation relation)
  * in the context of VACUUM, only CREATE INDEX.
  * ----------------
  */
-void
+static void
 UpdateStats(Oid relid, double reltuples)
 {
        Relation        whichRel;
index 77a0c8c44030056fcd348c74662a3714f78e0992..edadb494fa1fffaec8722d1aae7b2e35270b84ea 100644 (file)
@@ -52,7 +52,8 @@ extern void FormIndexDatum(IndexInfo *indexInfo,
                           Datum *values,
                           bool *isnull);
 
-extern void UpdateStats(Oid relid, double reltuples);
+extern void IndexCloseAndUpdateStats(Relation heap, double heapTuples,
+                                                                        Relation index, double indexTuples);
 
 extern void setRelhasindex(Oid relid, bool hasindex,
                           bool isprimary, Oid reltoastidxid);