diff --git a/src/backend/access/gist/gistbuild.c b/src/backend/access/gist/gistbuild.c index e84bc2e4c1..4a0c34b60b 100644 --- a/src/backend/access/gist/gistbuild.c +++ b/src/backend/access/gist/gistbuild.c @@ -474,6 +474,19 @@ gistBuildCallback(Relation index, itup = gistFormTuple(buildstate->giststate, index, values, isnull, true); itup->t_tid = htup->t_self; + /* Update tuple count and total size. */ + buildstate->indtuples += 1; + buildstate->indtuplesSize += IndexTupleSize(itup); + + /* + * XXX In buffering builds, the tempCxt is also reset down inside + * gistProcessEmptyingQueue(). This is not great because it risks + * confusion and possible use of dangling pointers (for example, itup + * might be already freed when control returns here). It's generally + * better that a memory context be "owned" by only one function. However, + * currently this isn't causing issues so it doesn't seem worth the amount + * of refactoring that would be needed to avoid it. + */ if (buildstate->bufferingMode == GIST_BUFFERING_ACTIVE) { /* We have buffers, so use them. */ @@ -489,10 +502,6 @@ gistBuildCallback(Relation index, buildstate->giststate, buildstate->heaprel); } - /* Update tuple count and total size. */ - buildstate->indtuples += 1; - buildstate->indtuplesSize += IndexTupleSize(itup); - MemoryContextSwitchTo(oldCtx); MemoryContextReset(buildstate->giststate->tempCxt);