Skip to content

Commit 7f6f8cc

Browse files
committed
Fix tuple counting in SP-GiST index build.
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
1 parent 67e02cd commit 7f6f8cc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/backend/access/spgist/spginsert.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
typedef struct
3131
{
3232
SpGistState spgstate; /* SPGiST's working state */
33+
int64 indtuples; /* total number of tuples indexed */
3334
MemoryContext tmpCtx; /* per-tuple temporary context */
3435
} SpGistBuildState;
3536

@@ -57,6 +58,9 @@ spgistBuildCallback(Relation index, HeapTuple htup, Datum *values,
5758
MemoryContextReset(buildstate->tmpCtx);
5859
}
5960

61+
/* Update total tuple count */
62+
buildstate->indtuples += 1;
63+
6064
MemoryContextSwitchTo(oldCtx);
6165
MemoryContextReset(buildstate->tmpCtx);
6266
}
@@ -130,6 +134,7 @@ spgbuild(PG_FUNCTION_ARGS)
130134
*/
131135
initSpGistState(&buildstate.spgstate, index);
132136
buildstate.spgstate.isBuild = true;
137+
buildstate.indtuples = 0;
133138

134139
buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
135140
"SP-GiST build temporary context",
@@ -145,7 +150,8 @@ spgbuild(PG_FUNCTION_ARGS)
145150
SpGistUpdateMetaPage(index);
146151

147152
result = (IndexBuildResult *) palloc0(sizeof(IndexBuildResult));
148-
result->heap_tuples = result->index_tuples = reltuples;
153+
result->heap_tuples = reltuples;
154+
result->index_tuples = buildstate.indtuples;
149155

150156
PG_RETURN_POINTER(result);
151157
}

0 commit comments

Comments
 (0)