From aec84d36ca063b53c2a3651ed981b51869f83203 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Mon, 14 Nov 2016 17:37:11 +0900 Subject: [PATCH] Reduce memory usage on static index construction --- src/pgroonga.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/pgroonga.c b/src/pgroonga.c index 3c5f70bb..44431cce 100644 --- a/src/pgroonga.c +++ b/src/pgroonga.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -84,6 +85,7 @@ typedef struct PGrnBuildStateData double nIndexedTuples; bool needMaxRecordSizeUpdate; uint32_t maxRecordSize; + MemoryContext memoryContext; } PGrnBuildStateData; typedef PGrnBuildStateData *PGrnBuildState; @@ -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, @@ -3578,6 +3583,9 @@ PGrnBuildCallbackRaw(Relation index, bs->maxRecordSize = recordSize; } bs->nIndexedTuples++; + + MemoryContextSwitchTo(oldMemoryContext); + MemoryContextReset(bs->memoryContext); } #ifdef PGRN_IS_GREENPLUM @@ -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); @@ -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);