Skip to content

Commit 0cb8b75

Browse files
committed
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
1 parent 9d6b160 commit 0cb8b75

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/backend/catalog/partition.c

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,18 @@ RelationBuildPartitionDesc(Relation rel)
303303
}
304304
else if (key->strategy == PARTITION_STRATEGY_RANGE)
305305
{
306-
int j,
307-
k;
306+
int k;
308307
PartitionRangeBound **all_bounds,
309308
*prev;
310-
bool *distinct_indexes;
311309

312310
all_bounds = (PartitionRangeBound **) palloc0(2 * nparts *
313311
sizeof(PartitionRangeBound *));
314-
distinct_indexes = (bool *) palloc(2 * nparts * sizeof(bool));
315312

316313
/*
317314
* Create a unified list of range bounds across all the
318315
* partitions.
319316
*/
320-
i = j = 0;
317+
i = ndatums = 0;
321318
foreach(cell, boundspecs)
322319
{
323320
PartitionBoundSpec *spec = castNode(PartitionBoundSpec,
@@ -332,26 +329,25 @@ RelationBuildPartitionDesc(Relation rel)
332329
true);
333330
upper = make_one_range_bound(key, i, spec->upperdatums,
334331
false);
335-
all_bounds[j] = lower;
336-
all_bounds[j + 1] = upper;
337-
j += 2;
332+
all_bounds[ndatums++] = lower;
333+
all_bounds[ndatums++] = upper;
338334
i++;
339335
}
340-
Assert(j == 2 * nparts);
336+
337+
Assert(ndatums == nparts * 2);
341338

342339
/* Sort all the bounds in ascending order */
343340
qsort_arg(all_bounds, 2 * nparts,
344341
sizeof(PartitionRangeBound *),
345342
qsort_partition_rbound_cmp,
346343
(void *) key);
347344

348-
/*
349-
* Count the number of distinct bounds to allocate an array of
350-
* that size.
351-
*/
352-
ndatums = 0;
345+
/* Save distinct bounds from all_bounds into rbounds. */
346+
rbounds = (PartitionRangeBound **)
347+
palloc(ndatums * sizeof(PartitionRangeBound *));
348+
k = 0;
353349
prev = NULL;
354-
for (i = 0; i < 2 * nparts; i++)
350+
for (i = 0; i < ndatums; i++)
355351
{
356352
PartitionRangeBound *cur = all_bounds[i];
357353
bool is_distinct = false;
@@ -388,34 +384,18 @@ RelationBuildPartitionDesc(Relation rel)
388384
}
389385

390386
/*
391-
* Count the current bound if it is distinct from the previous
392-
* one. Also, store if the index i contains a distinct bound
393-
* that we'd like put in the relcache array.
387+
* Only if the bound is distinct save it into a temporary
388+
* array i.e. rbounds which is later copied into boundinfo
389+
* datums array.
394390
*/
395391
if (is_distinct)
396-
{
397-
distinct_indexes[i] = true;
398-
ndatums++;
399-
}
400-
else
401-
distinct_indexes[i] = false;
392+
rbounds[k++] = all_bounds[i];
402393

403394
prev = cur;
404395
}
405396

406-
/*
407-
* Finally save them in an array from where they will be copied
408-
* into the relcache.
409-
*/
410-
rbounds = (PartitionRangeBound **) palloc(ndatums *
411-
sizeof(PartitionRangeBound *));
412-
k = 0;
413-
for (i = 0; i < 2 * nparts; i++)
414-
{
415-
if (distinct_indexes[i])
416-
rbounds[k++] = all_bounds[i];
417-
}
418-
Assert(k == ndatums);
397+
/* Update ndatums to hold the count of distinct datums. */
398+
ndatums = k;
419399
}
420400
else
421401
elog(ERROR, "unexpected partition strategy: %d",

0 commit comments

Comments
 (0)