prevent memory leak during ConvertTable with a lot of partitions (#6693)

Prevents memory leak during ConvertTable call for a table with a lot of
partitions.

DESCRIPTION: Fixes memory leak during undistribution and alteration of a
table with a lot of partitions.
pull/6710/head
aykut-bozkurt 2023-02-13 15:22:13 +03:00 committed by GitHub
parent 3200187757
commit 273911ac7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -800,9 +800,21 @@ ConvertTable(TableConversionState *con)
ExecuteQueryViaSPI(tableConstructionSQL, SPI_OK_UTILITY);
}
/*
* when there are many partitions, each call to ProcessUtilityParseTree
* accumulates used memory. Free context after each call.
*/
MemoryContext citusPerPartitionContext =
AllocSetContextCreate(CurrentMemoryContext,
"citus_per_partition_context",
ALLOCSET_DEFAULT_SIZES);
MemoryContext oldContext = MemoryContextSwitchTo(citusPerPartitionContext);
char *attachPartitionCommand = NULL;
foreach_ptr(attachPartitionCommand, attachPartitionCommands)
{
MemoryContextReset(citusPerPartitionContext);
Node *parseTree = ParseTreeNode(attachPartitionCommand);
ProcessUtilityParseTree(parseTree, attachPartitionCommand,
@ -810,6 +822,9 @@ ConvertTable(TableConversionState *con)
NULL, None_Receiver, NULL);
}
MemoryContextSwitchTo(oldContext);
MemoryContextDelete(citusPerPartitionContext);
if (isPartitionTable)
{
ExecuteQueryViaSPI(attachToParentCommand, SPI_OK_UTILITY);