]> granicus.if.org Git - postgresql/commitdiff
Fix tuple counting in SP-GiST index build.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 22 Mar 2018 17:23:48 +0000 (13:23 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 22 Mar 2018 17:23:48 +0000 (13:23 -0400)
Count the number of tuples in the index honestly, instead of assuming
that it's the same as the number of tuples in the heap.  (It might be
different if the index is partial.)

Back-patch to all supported versions.

Tomas Vondra

Discussion: https://postgr.es/m/3b3d8eac-c709-0d25-088e-b98339a1b28a@2ndquadrant.com

src/backend/access/spgist/spginsert.c

index e4b2c29b0ed3fbf97304e997109f9fd8eddc2a6e..bc96b4f1acaa8ee9da613d87b50668226f695b6b 100644 (file)
@@ -32,6 +32,7 @@
 typedef struct
 {
        SpGistState spgstate;           /* SPGiST's working state */
+       int64           indtuples;              /* total number of tuples indexed */
        MemoryContext tmpCtx;           /* per-tuple temporary context */
 } SpGistBuildState;
 
@@ -59,6 +60,9 @@ spgistBuildCallback(Relation index, HeapTuple htup, Datum *values,
                MemoryContextReset(buildstate->tmpCtx);
        }
 
+       /* Update total tuple count */
+       buildstate->indtuples += 1;
+
        MemoryContextSwitchTo(oldCtx);
        MemoryContextReset(buildstate->tmpCtx);
 }
@@ -132,6 +136,7 @@ spgbuild(Relation heap, Relation index, IndexInfo *indexInfo)
         */
        initSpGistState(&buildstate.spgstate, index);
        buildstate.spgstate.isBuild = true;
+       buildstate.indtuples = 0;
 
        buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
                                                                                          "SP-GiST build temporary context",
@@ -145,7 +150,8 @@ spgbuild(Relation heap, Relation index, IndexInfo *indexInfo)
        SpGistUpdateMetaPage(index);
 
        result = (IndexBuildResult *) palloc0(sizeof(IndexBuildResult));
-       result->heap_tuples = result->index_tuples = reltuples;
+       result->heap_tuples = reltuples;
+       result->index_tuples = buildstate.indtuples;
 
        return result;
 }