Commit 0cb8b753 authored by Robert Haas's avatar Robert Haas

Tighten up some code in RelationBuildPartitionDesc.

This probably doesn't save anything meaningful in terms of
performance, but making the code simpler is a good idea anyway.

Code by Beena Emerson, extracted from a larger patch by Jeevan
Ladhe, slightly adjusted by me.

Discussion: http://postgr.es/m/CAOgcT0ONgwajdtkoq+AuYkdTPY9cLWWLjxt_k4SXue3eieAr+g@mail.gmail.com
parent 9d6b160d
...@@ -303,21 +303,18 @@ RelationBuildPartitionDesc(Relation rel) ...@@ -303,21 +303,18 @@ RelationBuildPartitionDesc(Relation rel)
} }
else if (key->strategy == PARTITION_STRATEGY_RANGE) else if (key->strategy == PARTITION_STRATEGY_RANGE)
{ {
int j, int k;
k;
PartitionRangeBound **all_bounds, PartitionRangeBound **all_bounds,
*prev; *prev;
bool *distinct_indexes;
all_bounds = (PartitionRangeBound **) palloc0(2 * nparts * all_bounds = (PartitionRangeBound **) palloc0(2 * nparts *
sizeof(PartitionRangeBound *)); sizeof(PartitionRangeBound *));
distinct_indexes = (bool *) palloc(2 * nparts * sizeof(bool));
/* /*
* Create a unified list of range bounds across all the * Create a unified list of range bounds across all the
* partitions. * partitions.
*/ */
i = j = 0; i = ndatums = 0;
foreach(cell, boundspecs) foreach(cell, boundspecs)
{ {
PartitionBoundSpec *spec = castNode(PartitionBoundSpec, PartitionBoundSpec *spec = castNode(PartitionBoundSpec,
...@@ -332,12 +329,12 @@ RelationBuildPartitionDesc(Relation rel) ...@@ -332,12 +329,12 @@ RelationBuildPartitionDesc(Relation rel)
true); true);
upper = make_one_range_bound(key, i, spec->upperdatums, upper = make_one_range_bound(key, i, spec->upperdatums,
false); false);
all_bounds[j] = lower; all_bounds[ndatums++] = lower;
all_bounds[j + 1] = upper; all_bounds[ndatums++] = upper;
j += 2;
i++; i++;
} }
Assert(j == 2 * nparts);
Assert(ndatums == nparts * 2);
/* Sort all the bounds in ascending order */ /* Sort all the bounds in ascending order */
qsort_arg(all_bounds, 2 * nparts, qsort_arg(all_bounds, 2 * nparts,
...@@ -345,13 +342,12 @@ RelationBuildPartitionDesc(Relation rel) ...@@ -345,13 +342,12 @@ RelationBuildPartitionDesc(Relation rel)
qsort_partition_rbound_cmp, qsort_partition_rbound_cmp,
(void *) key); (void *) key);
/* /* Save distinct bounds from all_bounds into rbounds. */
* Count the number of distinct bounds to allocate an array of rbounds = (PartitionRangeBound **)
* that size. palloc(ndatums * sizeof(PartitionRangeBound *));
*/ k = 0;
ndatums = 0;
prev = NULL; prev = NULL;
for (i = 0; i < 2 * nparts; i++) for (i = 0; i < ndatums; i++)
{ {
PartitionRangeBound *cur = all_bounds[i]; PartitionRangeBound *cur = all_bounds[i];
bool is_distinct = false; bool is_distinct = false;
...@@ -388,34 +384,18 @@ RelationBuildPartitionDesc(Relation rel) ...@@ -388,34 +384,18 @@ RelationBuildPartitionDesc(Relation rel)
} }
/* /*
* Count the current bound if it is distinct from the previous * Only if the bound is distinct save it into a temporary
* one. Also, store if the index i contains a distinct bound * array i.e. rbounds which is later copied into boundinfo
* that we'd like put in the relcache array. * datums array.
*/ */
if (is_distinct) if (is_distinct)
{ rbounds[k++] = all_bounds[i];
distinct_indexes[i] = true;
ndatums++;
}
else
distinct_indexes[i] = false;
prev = cur; prev = cur;
} }
/* /* Update ndatums to hold the count of distinct datums. */
* Finally save them in an array from where they will be copied ndatums = k;
* into the relcache.
*/
rbounds = (PartitionRangeBound **) palloc(ndatums *
sizeof(PartitionRangeBound *));
k = 0;
for (i = 0; i < 2 * nparts; i++)
{
if (distinct_indexes[i])
rbounds[k++] = all_bounds[i];
}
Assert(k == ndatums);
} }
else else
elog(ERROR, "unexpected partition strategy: %d", elog(ERROR, "unexpected partition strategy: %d",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment