Fix errors in contrib/bloom 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.)

Fix counting of tuples in current index page, too.  This error would
have led to failing to write out the final page of the index if it
contained exactly one tuple, so that the last tuple of the relation
would not get indexed.

Back-patch to 9.6 where contrib/bloom was added.

Tomas Vondra and Tom Lane

Discussion: https://postgr.es/m/3b3d8eac-c709-0d25-088e-b98339a1b28a@2ndquadrant.com
This commit is contained in:
Tom Lane 2018-03-22 13:13:58 -04:00
parent e2f1eb0ee3
commit c35b472869

View File

@ -33,10 +33,11 @@ PG_MODULE_MAGIC;
typedef struct typedef struct
{ {
BloomState blstate; /* bloom index state */ BloomState blstate; /* bloom index state */
int64 indtuples; /* total number of tuples indexed */
MemoryContext tmpCtx; /* temporary memory context reset after each MemoryContext tmpCtx; /* temporary memory context reset after each
* tuple */ * tuple */
char data[BLCKSZ]; /* cached page */ char data[BLCKSZ]; /* cached page */
int64 count; /* number of tuples in cached page */ int count; /* number of tuples in cached page */
} BloomBuildState; } BloomBuildState;
/* /*
@ -102,8 +103,14 @@ bloomBuildCallback(Relation index, HeapTuple htup, Datum *values,
/* We shouldn't be here since we're inserting to the empty page */ /* We shouldn't be here since we're inserting to the empty page */
elog(ERROR, "could not add new bloom tuple to empty page"); elog(ERROR, "could not add new bloom tuple to empty page");
} }
/* Next item was added successfully */
buildstate->count++;
} }
/* Update total tuple count */
buildstate->indtuples += 1;
MemoryContextSwitchTo(oldCtx); MemoryContextSwitchTo(oldCtx);
MemoryContextReset(buildstate->tmpCtx); MemoryContextReset(buildstate->tmpCtx);
} }
@ -138,17 +145,15 @@ blbuild(Relation heap, Relation index, IndexInfo *indexInfo)
bloomBuildCallback, (void *) &buildstate, bloomBuildCallback, (void *) &buildstate,
NULL); NULL);
/* /* Flush last page if needed (it will be, unless heap was empty) */
* There are could be some items in cached page. Flush this page if
* needed.
*/
if (buildstate.count > 0) if (buildstate.count > 0)
flushCachedPage(index, &buildstate); flushCachedPage(index, &buildstate);
MemoryContextDelete(buildstate.tmpCtx); MemoryContextDelete(buildstate.tmpCtx);
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult)); result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
result->heap_tuples = result->index_tuples = reltuples; result->heap_tuples = reltuples;
result->index_tuples = buildstate.indtuples;
return result; return result;
} }