diff options
author | Robert Haas <rhaas@postgresql.org> | 2018-11-19 12:10:41 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2018-11-19 12:10:41 -0500 |
commit | 7ee5f88e650619ee619f7e2854ac50ff59d0b0c7 (patch) | |
tree | 75dd9c87255fe195b5eb623e6cc49b38aac7ce6c /src/backend/partitioning/partbounds.c | |
parent | 16fbac39ff68a737317f6fc7be78cc93a14856af (diff) | |
download | postgresql-7ee5f88e650619ee619f7e2854ac50ff59d0b0c7.tar.gz postgresql-7ee5f88e650619ee619f7e2854ac50ff59d0b0c7.zip |
Reduce unnecessary list construction in RelationBuildPartitionDesc.
The 'partoids' list which was constructed by the previous version
of this code was necessarily identical to 'inhoids'. There's no
point to duplicating the list, so avoid that. Instead, construct
the array representation directly from the original 'inhoids' list.
Also, use an array rather than a list for 'boundspecs'. We know
exactly how many items we need to store, so there's really no
reason to use a list. Using an array instead reduces the number
of memory allocations we perform.
Patch by me, reviewed by Michael Paquier and Amit Langote, the
latter of whom also helped with rebasing.
Diffstat (limited to 'src/backend/partitioning/partbounds.c')
-rw-r--r-- | src/backend/partitioning/partbounds.c | 66 |
1 files changed, 28 insertions, 38 deletions
diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c index be9fd49cd2e..eeaab2f4c9b 100644 --- a/src/backend/partitioning/partbounds.c +++ b/src/backend/partitioning/partbounds.c @@ -70,15 +70,12 @@ static int32 qsort_partition_list_value_cmp(const void *a, const void *b, void *arg); static int32 qsort_partition_rbound_cmp(const void *a, const void *b, void *arg); -static PartitionBoundInfo create_hash_bounds(List *boundspecs, - PartitionKey key, - int **mapping); -static PartitionBoundInfo create_list_bounds(List *boundspecs, - PartitionKey key, - int **mapping); -static PartitionBoundInfo create_range_bounds(List *boundspecs, - PartitionKey key, - int **mapping); +static PartitionBoundInfo create_hash_bounds(PartitionBoundSpec **boundspecs, + int nparts, PartitionKey key, int **mapping); +static PartitionBoundInfo create_list_bounds(PartitionBoundSpec **boundspecs, + int nparts, PartitionKey key, int **mapping); +static PartitionBoundInfo create_range_bounds(PartitionBoundSpec **boundspecs, + int nparts, PartitionKey key, int **mapping); static PartitionRangeBound *make_one_partition_rbound(PartitionKey key, int index, List *datums, bool lower); static int32 partition_hbound_cmp(int modulus1, int remainder1, int modulus2, @@ -169,9 +166,9 @@ get_qual_from_partbound(Relation rel, Relation parent, * current memory context. */ PartitionBoundInfo -partition_bounds_create(List *boundspecs, PartitionKey key, int **mapping) +partition_bounds_create(PartitionBoundSpec **boundspecs, int nparts, + PartitionKey key, int **mapping) { - int nparts = list_length(boundspecs); int i; Assert(nparts > 0); @@ -199,13 +196,13 @@ partition_bounds_create(List *boundspecs, PartitionKey key, int **mapping) switch (key->strategy) { case PARTITION_STRATEGY_HASH: - return create_hash_bounds(boundspecs, key, mapping); + return create_hash_bounds(boundspecs, nparts, key, mapping); case PARTITION_STRATEGY_LIST: - return create_list_bounds(boundspecs, key, mapping); + return create_list_bounds(boundspecs, nparts, key, mapping); case PARTITION_STRATEGY_RANGE: - return create_range_bounds(boundspecs, key, mapping); + return create_range_bounds(boundspecs, nparts, key, mapping); default: elog(ERROR, "unexpected partition strategy: %d", @@ -222,13 +219,12 @@ partition_bounds_create(List *boundspecs, PartitionKey key, int **mapping) * Create a PartitionBoundInfo for a hash partitioned table */ static PartitionBoundInfo -create_hash_bounds(List *boundspecs, PartitionKey key, int **mapping) +create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts, + PartitionKey key, int **mapping) { PartitionBoundInfo boundinfo; PartitionHashBound **hbounds = NULL; - ListCell *cell; - int i, - nparts = list_length(boundspecs); + int i; int ndatums = 0; int greatest_modulus; @@ -244,10 +240,9 @@ create_hash_bounds(List *boundspecs, PartitionKey key, int **mapping) palloc(nparts * sizeof(PartitionHashBound *)); /* Convert from node to the internal representation */ - i = 0; - foreach(cell, boundspecs) + for (i = 0; i < nparts; i++) { - PartitionBoundSpec *spec = castNode(PartitionBoundSpec, lfirst(cell)); + PartitionBoundSpec *spec = boundspecs[i]; if (spec->strategy != PARTITION_STRATEGY_HASH) elog(ERROR, "invalid strategy in partition bound spec"); @@ -256,7 +251,6 @@ create_hash_bounds(List *boundspecs, PartitionKey key, int **mapping) hbounds[i]->modulus = spec->modulus; hbounds[i]->remainder = spec->remainder; hbounds[i]->index = i; - i++; } /* Sort all the bounds in ascending order */ @@ -307,7 +301,8 @@ create_hash_bounds(List *boundspecs, PartitionKey key, int **mapping) * Create a PartitionBoundInfo for a list partitioned table */ static PartitionBoundInfo -create_list_bounds(List *boundspecs, PartitionKey key, int **mapping) +create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, + PartitionKey key, int **mapping) { PartitionBoundInfo boundinfo; PartitionListValue **all_values = NULL; @@ -327,9 +322,9 @@ create_list_bounds(List *boundspecs, PartitionKey key, int **mapping) boundinfo->default_index = -1; /* Create a unified list of non-null values across all partitions. */ - foreach(cell, boundspecs) + for (i = 0; i < nparts; i++) { - PartitionBoundSpec *spec = castNode(PartitionBoundSpec, lfirst(cell)); + PartitionBoundSpec *spec = boundspecs[i]; ListCell *c; if (spec->strategy != PARTITION_STRATEGY_LIST) @@ -343,7 +338,6 @@ create_list_bounds(List *boundspecs, PartitionKey key, int **mapping) if (spec->is_default) { default_index = i; - i++; continue; } @@ -374,8 +368,6 @@ create_list_bounds(List *boundspecs, PartitionKey key, int **mapping) if (list_value) non_null_values = lappend(non_null_values, list_value); } - - i++; } ndatums = list_length(non_null_values); @@ -458,7 +450,7 @@ create_list_bounds(List *boundspecs, PartitionKey key, int **mapping) } /* All partition must now have been assigned canonical indexes. */ - Assert(next_index == list_length(boundspecs)); + Assert(next_index == nparts); return boundinfo; } @@ -467,16 +459,15 @@ create_list_bounds(List *boundspecs, PartitionKey key, int **mapping) * Create a PartitionBoundInfo for a range partitioned table */ static PartitionBoundInfo -create_range_bounds(List *boundspecs, PartitionKey key, int **mapping) +create_range_bounds(PartitionBoundSpec **boundspecs, int nparts, + PartitionKey key, int **mapping) { PartitionBoundInfo boundinfo; PartitionRangeBound **rbounds = NULL; PartitionRangeBound **all_bounds, *prev; - ListCell *cell; int i, - k, - nparts = list_length(boundspecs); + k; int ndatums = 0; int default_index = -1; int next_index = 0; @@ -493,10 +484,10 @@ create_range_bounds(List *boundspecs, PartitionKey key, int **mapping) palloc0(2 * nparts * sizeof(PartitionRangeBound *)); /* Create a unified list of range bounds across all the partitions. */ - i = ndatums = 0; - foreach(cell, boundspecs) + ndatums = 0; + for (i = 0; i < nparts; i++) { - PartitionBoundSpec *spec = castNode(PartitionBoundSpec, lfirst(cell)); + PartitionBoundSpec *spec = boundspecs[i]; PartitionRangeBound *lower, *upper; @@ -510,7 +501,7 @@ create_range_bounds(List *boundspecs, PartitionKey key, int **mapping) */ if (spec->is_default) { - default_index = i++; + default_index = i; continue; } @@ -518,7 +509,6 @@ create_range_bounds(List *boundspecs, PartitionKey key, int **mapping) upper = make_one_partition_rbound(key, i, spec->upperdatums, false); all_bounds[ndatums++] = lower; all_bounds[ndatums++] = upper; - i++; } Assert(ndatums == nparts * 2 || |