Skip to content

Commit

Permalink
Reduce memory usage on static index construction
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Nov 14, 2016
1 parent 3777d97 commit aec84d3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/pgroonga.c
Expand Up @@ -42,6 +42,7 @@
#include <utils/array.h>
#include <utils/builtins.h>
#include <utils/lsyscache.h>
#include <utils/memutils.h>
#include <utils/selfuncs.h>
#include <utils/syscache.h>
#include <utils/timestamp.h>
Expand Down Expand Up @@ -84,6 +85,7 @@ typedef struct PGrnBuildStateData
double nIndexedTuples;
bool needMaxRecordSizeUpdate;
uint32_t maxRecordSize;
MemoryContext memoryContext;
} PGrnBuildStateData;

typedef PGrnBuildStateData *PGrnBuildState;
Expand Down Expand Up @@ -3561,11 +3563,14 @@ PGrnBuildCallbackRaw(Relation index,
void *state)
{
PGrnBuildState bs = (PGrnBuildState) state;
MemoryContext oldMemoryContext;
uint32_t recordSize;

if (!tupleIsAlive)
return;

oldMemoryContext = MemoryContextSwitchTo(bs->memoryContext);

recordSize = PGrnInsert(index,
bs->sourcesTable,
bs->sourcesCtidColumn,
Expand All @@ -3578,6 +3583,9 @@ PGrnBuildCallbackRaw(Relation index,
bs->maxRecordSize = recordSize;
}
bs->nIndexedTuples++;

MemoryContextSwitchTo(oldMemoryContext);
MemoryContextReset(bs->memoryContext);
}

#ifdef PGRN_IS_GREENPLUM
Expand Down Expand Up @@ -3634,6 +3642,10 @@ pgroonga_build_raw(Relation heap,
bs.nIndexedTuples = 0.0;
bs.needMaxRecordSizeUpdate = PGrnNeedMaxRecordSizeUpdate(index);
bs.maxRecordSize = 0;
bs.memoryContext =
AllocSetContextCreate(CurrentMemoryContext,
"PGroonga index build temporay context",
ALLOCSET_DEFAULT_SIZES);

GRN_PTR_INIT(&supplementaryTables, GRN_OBJ_VECTOR, GRN_ID_NIL);
GRN_PTR_INIT(&lexicons, GRN_OBJ_VECTOR, GRN_ID_NIL);
Expand Down Expand Up @@ -3683,6 +3695,8 @@ pgroonga_build_raw(Relation heap,
result->heap_tuples = nHeapTuples;
result->index_tuples = bs.nIndexedTuples;

MemoryContextDelete(bs.memoryContext);

if (bs.needMaxRecordSizeUpdate)
{
PGrnUpdateMaxRecordSize(index, bs.maxRecordSize);
Expand Down

0 comments on commit aec84d3

Please sign in to comment.