From 6a5a7eb398dcd4c470f115cf1e6945a00cffa9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Nordstro=CC=88m?= Date: Thu, 17 Aug 2017 11:03:30 +0200 Subject: [PATCH] Reduce memory usage on long-running COPY operations This change ensures that the per-tuple exprcontext (on which per-tuple state is allocated), is reset for every new tuple processed in a long-running COPY transaction. --- src/chunk_insert_state.c | 5 +++-- src/copy.c | 17 ++++++++++------- src/hypertable.c | 6 +++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/chunk_insert_state.c b/src/chunk_insert_state.c index 11a91a590..4f87c2627 100644 --- a/src/chunk_insert_state.c +++ b/src/chunk_insert_state.c @@ -56,8 +56,7 @@ create_chunk_result_relation_info(ChunkDispatch *dispatch, Relation rel, Index r ResultRelInfo *rri, *rri_orig; - rri = palloc(sizeof(ResultRelInfo)); - MemSet(rri, 0, sizeof(ResultRelInfo)); + rri = palloc0(sizeof(ResultRelInfo)); NodeSetTag(rri, T_ResultRelInfo); InitResultRelInfo(rri, rel, rti, 0); @@ -163,4 +162,6 @@ chunk_insert_state_destroy(ChunkInsertState *state) ExecCloseIndices(state->result_relation_info); heap_close(state->rel, NoLock); + pfree(state->result_relation_info); + pfree(state); } diff --git a/src/copy.c b/src/copy.c index 697a24e0b..26653ad2c 100644 --- a/src/copy.c +++ b/src/copy.c @@ -223,6 +223,9 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp CHECK_FOR_INTERRUPTS(); + /* Reset the per-tuple exprcontext */ + ResetPerTupleExprContext(estate); + /* Switch into its memory context */ MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); @@ -241,13 +244,6 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp */ tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc); - /* Triggers and stuff need to be invoked in query context. */ - MemoryContextSwitchTo(oldcontext); - - /* Place tuple in tuple slot --- but slot shouldn't free it */ - slot = myslot; - ExecStoreTuple(tuple, slot, InvalidBuffer, false); - /* Calculate the tuple's point in the N-dimensional hyperspace */ point = hyperspace_calculate_point(ht->space, tuple, tupDesc); @@ -266,6 +262,13 @@ timescaledb_CopyFrom(CopyState cstate, Relation main_rel, List *range_table, Hyp bistate->current_buf = InvalidBuffer; } + /* Triggers and stuff need to be invoked in query context. */ + MemoryContextSwitchTo(oldcontext); + + /* Place tuple in tuple slot --- but slot shouldn't free it */ + slot = myslot; + ExecStoreTuple(tuple, slot, InvalidBuffer, false); + /* * Set the result relation in the executor state to the target chunk. * This makes sure that the tuple gets inserted into the correct diff --git a/src/hypertable.c b/src/hypertable.c index e335c5faf..a4d216183 100644 --- a/src/hypertable.c +++ b/src/hypertable.c @@ -37,9 +37,9 @@ hypertable_get_chunk(Hypertable *h, Point *point) MemoryContext old; /* - * chunk_find() must execute on the transaction memory context since - * it allocates a lot of transient data. We don't want this allocated - * on the cache's memory context. + * chunk_find() must execute on a per-tuple memory context since it + * allocates a lot of transient data. We don't want this allocated on + * the cache's memory context. */ chunk = chunk_find(h->space, point);