From 26fdcb68f007015797bab3070e2d2404bb42bc5c Mon Sep 17 00:00:00 2001 From: Onder Kalaci Date: Tue, 26 Jul 2022 13:44:42 +0200 Subject: [PATCH] Optimize StringJoin() for when prefix-postfix is needed Before this commit, we required multiple copies of the same stringInfo if we needed to append/prepend data to the stringInfo. Now, we optionally get prefix/postfix. For large string operations, this can save up to %10 memory. --- src/backend/distributed/utils/listutils.c | 25 ++++++++++++++++++- .../utils/multi_partitioning_utils.c | 10 +++----- src/include/distributed/listutils.h | 2 ++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/backend/distributed/utils/listutils.c b/src/backend/distributed/utils/listutils.c index ce2920748..3279193ef 100644 --- a/src/backend/distributed/utils/listutils.c +++ b/src/backend/distributed/utils/listutils.c @@ -161,13 +161,31 @@ GeneratePositiveIntSequenceList(int upTo) /* * StringJoin gets a list of char * and then simply * returns a newly allocated char * joined with the - * given delimiter. + * given delimiter. It uses ';' as the delimiter by + * default. */ char * StringJoin(List *stringList, char delimiter) +{ + return StringJoinParams(stringList, delimiter, NULL, NULL); +} + + +/* + * StringJoin gets a list of char * and then simply + * returns a newly allocated char * joined with the + * given delimiter, prefix and postfix. + */ +char * +StringJoinParams(List *stringList, char delimiter, char *prefix, char *postfix) { StringInfo joinedString = makeStringInfo(); + if (prefix != NULL) + { + appendStringInfoString(joinedString, prefix); + } + const char *command = NULL; int curIndex = 0; foreach_ptr(command, stringList) @@ -180,6 +198,11 @@ StringJoin(List *stringList, char delimiter) curIndex++; } + if (postfix != NULL) + { + appendStringInfoString(joinedString, postfix); + } + return joinedString->data; } diff --git a/src/backend/distributed/utils/multi_partitioning_utils.c b/src/backend/distributed/utils/multi_partitioning_utils.c index 59876e34d..9dfa285a2 100644 --- a/src/backend/distributed/utils/multi_partitioning_utils.c +++ b/src/backend/distributed/utils/multi_partitioning_utils.c @@ -569,13 +569,11 @@ CreateFixPartitionShardIndexNames(Oid parentRelationId, Oid partitionRelationId, task->taskId = taskId++; task->taskType = DDL_TASK; + char *prefix = "SELECT pg_catalog.citus_run_local_command($$"; + char *postfix = "$$)"; + char *string = StringJoinParams(queryStringList, ';', prefix, postfix); - char *string = StringJoin(queryStringList, ';'); - StringInfo commandToRun = makeStringInfo(); - - appendStringInfo(commandToRun, - "SELECT pg_catalog.citus_run_local_command($$%s$$)", string); - SetTaskQueryString(task, commandToRun->data); + SetTaskQueryString(task, string); task->dependentTaskList = NULL; diff --git a/src/include/distributed/listutils.h b/src/include/distributed/listutils.h index c3facf76f..aa6a0e96b 100644 --- a/src/include/distributed/listutils.h +++ b/src/include/distributed/listutils.h @@ -166,6 +166,8 @@ extern List * SortList(List *pointerList, extern void ** PointerArrayFromList(List *pointerList); extern HTAB * ListToHashSet(List *pointerList, Size keySize, bool isStringList); extern char * StringJoin(List *stringList, char delimiter); +extern char * StringJoinParams(List *stringList, char delimiter, + char *prefix, char *postfix); extern List * ListTake(List *pointerList, int size); extern void * safe_list_nth(const List *list, int index); extern List * GeneratePositiveIntSequenceList(int upTo);