From 6f5da9b5eb01b94e31ddb7add95d5711760fa7ab Mon Sep 17 00:00:00 2001 From: Dmitry Simonenko Date: Tue, 10 Dec 2019 16:42:34 +0300 Subject: [PATCH] Fix memory leak during long distributed insert Tuple expression context memory context is not properly reset during chunk dispatch execution which eventually consumes all available memory during the query execution: INSERT INTO test_table SELECT now() - random() * interval '2 years', (i/100)::text, random() FROM generate_series(1,700000) AS sub(i); This problem does not reproduces for a distributed hypertables with disabled batching and for a regular hypertables. Because luckly the tuple expression context got freed during the ModifyTable node execution. --- src/chunk_dispatch_state.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/chunk_dispatch_state.c b/src/chunk_dispatch_state.c index b7ea67001..ffb516540 100644 --- a/src/chunk_dispatch_state.c +++ b/src/chunk_dispatch_state.c @@ -116,6 +116,9 @@ chunk_dispatch_exec(CustomScanState *node) if (TupIsNull(slot)) return NULL; + /* Reset the per-tuple exprcontext */ + ResetPerTupleExprContext(estate); + /* Switch to the executor's per-tuple memory context */ old = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));