From: Nitin Jadhav Date: Sat, 15 May 2021 14:40:45 +0530 Subject: [PATCH 1/5] Removed extra memory allocations from create_list_bounds --- src/backend/partitioning/partbounds.c | 64 +++++++++++++++------------ 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c index 7925fcce3b..d0c263d435 100644 --- a/src/backend/partitioning/partbounds.c +++ b/src/backend/partitioning/partbounds.c @@ -432,6 +432,32 @@ create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts, return boundinfo; } +/* + * get_non_null_count_list_bounds + * Calculates the total number of non null bound values of + * all the partitions. + */ +static int +get_non_null_count_list_bounds(PartitionBoundSpec **boundspecs, int nparts) +{ + int i = 0; + int count = 0; + + for (i = 0; i < nparts; i++) + { + ListCell *c; + + foreach(c, boundspecs[i]->listdatums) + { + Const *val = castNode(Const, lfirst(c)); + + if (!val->constisnull) + count++; + } + } + + return count; +} /* * create_list_bounds * Create a PartitionBoundInfo for a list partitioned table @@ -442,13 +468,12 @@ create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, { PartitionBoundInfo boundinfo; PartitionListValue **all_values = NULL; - ListCell *cell; int i = 0; + int j = 0; int ndatums = 0; int next_index = 0; int default_index = -1; int null_index = -1; - List *non_null_values = NIL; boundinfo = (PartitionBoundInfoData *) palloc0(sizeof(PartitionBoundInfoData)); @@ -457,6 +482,10 @@ create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, boundinfo->null_index = -1; boundinfo->default_index = -1; + ndatums = get_non_null_count_list_bounds(boundspecs, nparts); + all_values = (PartitionListValue **) + palloc(ndatums * sizeof(PartitionListValue *)); + /* Create a unified list of non-null values across all partitions. */ for (i = 0; i < nparts; i++) { @@ -480,14 +509,14 @@ create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, foreach(c, spec->listdatums) { Const *val = castNode(Const, lfirst(c)); - PartitionListValue *list_value = NULL; if (!val->constisnull) { - list_value = (PartitionListValue *) + all_values[j] = (PartitionListValue *) palloc0(sizeof(PartitionListValue)); - list_value->index = i; - list_value->value = val->constvalue; + all_values[j]->index = i; + all_values[j]->value = val->constvalue; + j++; } else { @@ -499,32 +528,9 @@ create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, elog(ERROR, "found null more than once"); null_index = i; } - - if (list_value) - non_null_values = lappend(non_null_values, list_value); } } - ndatums = list_length(non_null_values); - - /* - * Collect all list values in one array. Alongside the value, we also save - * the index of partition the value comes from. - */ - all_values = (PartitionListValue **) - palloc(ndatums * sizeof(PartitionListValue *)); - i = 0; - foreach(cell, non_null_values) - { - PartitionListValue *src = lfirst(cell); - - all_values[i] = (PartitionListValue *) - palloc(sizeof(PartitionListValue)); - all_values[i]->value = src->value; - all_values[i]->index = src->index; - i++; - } - qsort_arg(all_values, ndatums, sizeof(PartitionListValue *), qsort_partition_list_value_cmp, (void *) key); -- 2.17.0 --T4sUOijqQbZv57TR Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-allocate-the-PartitionListValue-as-a-single-chunk.patch"