PostgreSQL Source Code git master
Loading...
Searching...
No Matches
partbounds.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * partbounds.c
4 * Support routines for manipulating partition bounds
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/backend/partitioning/partbounds.c
11 *
12 *-------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "access/relation.h"
18#include "access/table.h"
19#include "access/tableam.h"
20#include "catalog/namespace.h"
21#include "catalog/partition.h"
22#include "catalog/pg_inherits.h"
23#include "catalog/pg_type.h"
24#include "commands/tablecmds.h"
25#include "common/hashfn.h"
26#include "executor/executor.h"
27#include "miscadmin.h"
28#include "nodes/makefuncs.h"
29#include "nodes/nodeFuncs.h"
30#include "nodes/pathnodes.h"
31#include "parser/parse_coerce.h"
34#include "utils/array.h"
35#include "utils/builtins.h"
36#include "utils/datum.h"
37#include "utils/fmgroids.h"
38#include "utils/lsyscache.h"
39#include "utils/partcache.h"
40#include "utils/ruleutils.h"
41#include "utils/snapmgr.h"
42#include "utils/syscache.h"
43
44/*
45 * When qsort'ing partition bounds after reading from the catalog, each bound
46 * is represented with one of the following structs.
47 */
48
49/* One bound of a hash partition */
56
57/* One value coming from some (index'th) list partition */
63
64/* One bound of a range partition */
65typedef struct PartitionRangeBound
66{
67 int index;
68 Datum *datums; /* range bound datums */
69 PartitionRangeDatumKind *kind; /* the kind of each datum */
70 bool lower; /* this is the lower (vs upper) bound */
72
73/*
74 * Mapping from partitions of a joining relation to partitions of a join
75 * relation being computed (a.k.a merged partitions)
76 */
77typedef struct PartitionMap
78{
79 int nparts; /* number of partitions */
80 int *merged_indexes; /* indexes of merged partitions */
81 bool *merged; /* flags to indicate whether partitions are
82 * merged with non-dummy partitions */
83 bool did_remapping; /* did we re-map partitions? */
84 int *old_indexes; /* old indexes of merged partitions if
85 * did_remapping */
87
88/* Macro for comparing two range bounds */
89#define compare_range_bounds(partnatts, partsupfunc, partcollations, \
90 bound1, bound2) \
91 (partition_rbound_cmp(partnatts, partsupfunc, partcollations, \
92 (bound1)->datums, (bound1)->kind, (bound1)->lower, \
93 bound2))
94
95static int32 qsort_partition_hbound_cmp(const void *a, const void *b);
96static int32 qsort_partition_list_value_cmp(const void *a, const void *b,
97 void *arg);
98static int32 qsort_partition_rbound_cmp(const void *a, const void *b,
99 void *arg);
101 int nparts, PartitionKey key, int **mapping);
103 int nparts, PartitionKey key, int **mapping);
105 int nparts, PartitionKey key, int **mapping);
107 Oid *partcollation,
110 JoinType jointype,
112 List **inner_parts);
113static PartitionBoundInfo merge_range_bounds(int partnatts,
118 JoinType jointype,
120 List **inner_parts);
121static void init_partition_map(RelOptInfo *rel, PartitionMap *map);
122static void free_partition_map(PartitionMap *map);
123static bool is_dummy_partition(RelOptInfo *rel, int part_index);
126 int outer_index,
127 int inner_index,
128 int *next_index);
133 int outer_index,
134 int inner_default,
135 JoinType jointype,
136 int *next_index,
137 int *default_index);
142 int inner_index,
143 int outer_default,
144 JoinType jointype,
145 int *next_index,
146 int *default_index);
149 bool outer_has_null,
150 bool inner_has_null,
151 int outer_null,
152 int inner_null,
153 JoinType jointype,
154 int *next_index,
155 int *null_index);
160 int outer_default,
161 int inner_default,
162 JoinType jointype,
163 int *next_index,
164 int *default_index);
166 int *next_index);
169 int nmerged, List *merged_indexes);
174 int nmerged,
176 List **inner_parts);
180 List *merged_indexes,
181 int null_index,
182 int default_index);
183static int get_range_partition(RelOptInfo *rel,
185 int *lb_pos,
189 int *lb_pos,
192static bool compare_range_partitions(int partnatts, FmgrInfo *partsupfuncs,
198 int *lb_cmpval, int *ub_cmpval);
199static void get_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
200 Oid *partcollations, JoinType jointype,
205 int lb_cmpval, int ub_cmpval,
208static void add_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
212 int merged_index,
215 List **merged_indexes);
217 List *datums, bool lower);
219 int remainder2);
220static int32 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc,
221 Oid *partcollation, Datum *datums1,
224static int partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
225 Oid *partcollation,
226 PartitionBoundInfo boundinfo,
229 uint16 strategy, Expr *arg1, Expr *arg2);
231 StrategyNumber strategy, bool *need_relabel);
235 bool for_default);
236static void get_range_key_properties(PartitionKey key, int keynum,
237 PartitionRangeDatum *ldatum,
240 Expr **keyCol,
243
244/*
245 * get_qual_from_partbound
246 * Given a parser node for partition bound, return the list of executable
247 * expressions as partition constraint
248 */
249List *
251{
253 List *my_qual = NIL;
254
255 Assert(key != NULL);
256
257 switch (key->strategy)
258 {
260 Assert(spec->strategy == PARTITION_STRATEGY_HASH);
261 my_qual = get_qual_for_hash(parent, spec);
262 break;
263
265 Assert(spec->strategy == PARTITION_STRATEGY_LIST);
266 my_qual = get_qual_for_list(parent, spec);
267 break;
268
271 my_qual = get_qual_for_range(parent, spec, false);
272 break;
273 }
274
275 return my_qual;
276}
277
278/*
279 * partition_bounds_create
280 * Build a PartitionBoundInfo struct from a list of PartitionBoundSpec
281 * nodes
282 *
283 * This function creates a PartitionBoundInfo and fills the values of its
284 * various members based on the input list. Importantly, 'datums' array will
285 * contain Datum representation of individual bounds (possibly after
286 * de-duplication as in case of range bounds), sorted in a canonical order
287 * defined by qsort_partition_* functions of respective partitioning methods.
288 * 'indexes' array will contain as many elements as there are bounds (specific
289 * exceptions to this rule are listed in the function body), which represent
290 * the 0-based canonical positions of partitions.
291 *
292 * Upon return from this function, *mapping is set to an array of
293 * list_length(boundspecs) elements, each of which maps the original index of
294 * a partition to its canonical index.
295 *
296 * Note: The objects returned by this function are wholly allocated in the
297 * current memory context.
298 */
301 PartitionKey key, int **mapping)
302{
303 int i;
304
305 Assert(nparts > 0);
306
307 /*
308 * For each partitioning method, we first convert the partition bounds
309 * from their parser node representation to the internal representation,
310 * along with any additional preprocessing (such as de-duplicating range
311 * bounds). Resulting bound datums are then added to the 'datums' array
312 * in PartitionBoundInfo. For each datum added, an integer indicating the
313 * canonical partition index is added to the 'indexes' array.
314 *
315 * For each bound, we remember its partition's position (0-based) in the
316 * original list to later map it to the canonical index.
317 */
318
319 /*
320 * Initialize mapping array with invalid values, this is filled within
321 * each sub-routine below depending on the bound type.
322 */
323 *mapping = palloc_array(int, nparts);
324 for (i = 0; i < nparts; i++)
325 (*mapping)[i] = -1;
326
327 switch (key->strategy)
328 {
330 return create_hash_bounds(boundspecs, nparts, key, mapping);
331
333 return create_list_bounds(boundspecs, nparts, key, mapping);
334
336 return create_range_bounds(boundspecs, nparts, key, mapping);
337 }
338
339 Assert(false);
340 return NULL; /* keep compiler quiet */
341}
342
343/*
344 * create_hash_bounds
345 * Create a PartitionBoundInfo for a hash partitioned table
346 */
349 PartitionKey key, int **mapping)
350{
351 PartitionBoundInfo boundinfo;
353 int i;
356
358 boundinfo->strategy = key->strategy;
359 /* No special hash partitions. */
360 boundinfo->null_index = -1;
361 boundinfo->default_index = -1;
362
364
365 /* Convert from node to the internal representation */
366 for (i = 0; i < nparts; i++)
367 {
369
370 if (spec->strategy != PARTITION_STRATEGY_HASH)
371 elog(ERROR, "invalid strategy in partition bound spec");
372
373 hbounds[i].modulus = spec->modulus;
374 hbounds[i].remainder = spec->remainder;
375 hbounds[i].index = i;
376 }
377
378 /* Sort all the bounds in ascending order */
379 qsort(hbounds, nparts, sizeof(PartitionHashBound),
381
382 /* After sorting, moduli are now stored in ascending order. */
383 greatest_modulus = hbounds[nparts - 1].modulus;
384
385 boundinfo->ndatums = nparts;
386 boundinfo->datums = palloc0_array(Datum *, nparts);
387 boundinfo->kind = NULL;
388 boundinfo->interleaved_parts = NULL;
389 boundinfo->nindexes = greatest_modulus;
390 boundinfo->indexes = (int *) palloc(greatest_modulus * sizeof(int));
391 for (i = 0; i < greatest_modulus; i++)
392 boundinfo->indexes[i] = -1;
393
394 /*
395 * In the loop below, to save from allocating a series of small datum
396 * arrays, here we just allocate a single array and below we'll just
397 * assign a portion of this array per partition.
398 */
399 boundDatums = (Datum *) palloc(nparts * 2 * sizeof(Datum));
400
401 /*
402 * For hash partitioning, there are as many datums (modulus and remainder
403 * pairs) as there are partitions. Indexes are simply values ranging from
404 * 0 to (nparts - 1).
405 */
406 for (i = 0; i < nparts; i++)
407 {
408 int modulus = hbounds[i].modulus;
409 int remainder = hbounds[i].remainder;
410
411 boundinfo->datums[i] = &boundDatums[i * 2];
412 boundinfo->datums[i][0] = Int32GetDatum(modulus);
413 boundinfo->datums[i][1] = Int32GetDatum(remainder);
414
416 {
417 /* overlap? */
418 Assert(boundinfo->indexes[remainder] == -1);
419 boundinfo->indexes[remainder] = i;
420 remainder += modulus;
421 }
422
423 (*mapping)[hbounds[i].index] = i;
424 }
425 pfree(hbounds);
426
427 return boundinfo;
428}
429
430/*
431 * get_non_null_list_datum_count
432 * Counts the number of non-null Datums in each partition.
433 */
434static int
436{
437 int i;
438 int count = 0;
439
440 for (i = 0; i < nparts; i++)
441 {
442 ListCell *lc;
443
444 foreach(lc, boundspecs[i]->listdatums)
445 {
447
448 if (!val->constisnull)
449 count++;
450 }
451 }
452
453 return count;
454}
455
456/*
457 * create_list_bounds
458 * Create a PartitionBoundInfo for a list partitioned table
459 */
462 PartitionKey key, int **mapping)
463{
464 PartitionBoundInfo boundinfo;
466 int i;
467 int j;
468 int ndatums;
469 int next_index = 0;
470 int default_index = -1;
471 int null_index = -1;
473
475 boundinfo->strategy = key->strategy;
476 /* Will be set correctly below. */
477 boundinfo->null_index = -1;
478 boundinfo->default_index = -1;
479
480 ndatums = get_non_null_list_datum_count(boundspecs, nparts);
482 palloc(ndatums * sizeof(PartitionListValue));
483
484 /* Create a unified list of non-null values across all partitions. */
485 for (j = 0, i = 0; i < nparts; i++)
486 {
488 ListCell *c;
489
490 if (spec->strategy != PARTITION_STRATEGY_LIST)
491 elog(ERROR, "invalid strategy in partition bound spec");
492
493 /*
494 * Note the index of the partition bound spec for the default
495 * partition. There's no datum to add to the list on non-null datums
496 * for this partition.
497 */
498 if (spec->is_default)
499 {
500 default_index = i;
501 continue;
502 }
503
504 foreach(c, spec->listdatums)
505 {
507
508 if (!val->constisnull)
509 {
510 all_values[j].index = i;
511 all_values[j].value = val->constvalue;
512 j++;
513 }
514 else
515 {
516 /*
517 * Never put a null into the values array; save the index of
518 * the partition that stores nulls, instead.
519 */
520 if (null_index != -1)
521 elog(ERROR, "found null more than once");
522 null_index = i;
523 }
524 }
525 }
526
527 /* ensure we found a Datum for every slot in the all_values array */
528 Assert(j == ndatums);
529
530 qsort_arg(all_values, ndatums, sizeof(PartitionListValue),
532
533 boundinfo->ndatums = ndatums;
534 boundinfo->datums = palloc0_array(Datum *, ndatums);
535 boundinfo->kind = NULL;
536 boundinfo->interleaved_parts = NULL;
537 boundinfo->nindexes = ndatums;
538 boundinfo->indexes = (int *) palloc(ndatums * sizeof(int));
539
540 /*
541 * In the loop below, to save from allocating a series of small datum
542 * arrays, here we just allocate a single array and below we'll just
543 * assign a portion of this array per datum.
544 */
545 boundDatums = (Datum *) palloc(ndatums * sizeof(Datum));
546
547 /*
548 * Copy values. Canonical indexes are values ranging from 0 to (nparts -
549 * 1) assigned to each partition such that all datums of a given partition
550 * receive the same value. The value for a given partition is the index of
551 * that partition's smallest datum in the all_values[] array.
552 */
553 for (i = 0; i < ndatums; i++)
554 {
555 int orig_index = all_values[i].index;
556
557 boundinfo->datums[i] = &boundDatums[i];
558 boundinfo->datums[i][0] = datumCopy(all_values[i].value,
559 key->parttypbyval[0],
560 key->parttyplen[0]);
561
562 /* If the old index has no mapping, assign one */
563 if ((*mapping)[orig_index] == -1)
564 (*mapping)[orig_index] = next_index++;
565
566 boundinfo->indexes[i] = (*mapping)[orig_index];
567 }
568
570
571 /*
572 * Set the canonical value for null_index, if any.
573 *
574 * It is possible that the null-accepting partition has not been assigned
575 * an index yet, which could happen if such partition accepts only null
576 * and hence not handled in the above loop which only looked at non-null
577 * values.
578 */
579 if (null_index != -1)
580 {
581 Assert(null_index >= 0);
582 if ((*mapping)[null_index] == -1)
583 (*mapping)[null_index] = next_index++;
584 boundinfo->null_index = (*mapping)[null_index];
585 }
586
587 /* Set the canonical value for default_index, if any. */
588 if (default_index != -1)
589 {
590 /*
591 * The default partition accepts any value not specified in the lists
592 * of other partitions, hence it should not get mapped index while
593 * assigning those for non-null datums.
594 */
595 Assert(default_index >= 0);
596 Assert((*mapping)[default_index] == -1);
597 (*mapping)[default_index] = next_index++;
598 boundinfo->default_index = (*mapping)[default_index];
599 }
600
601 /*
602 * Calculate interleaved partitions. Here we look for partitions which
603 * might be interleaved with other partitions and set a bit in
604 * interleaved_parts for any partitions which may be interleaved with
605 * another partition.
606 */
607
608 /*
609 * There must be multiple partitions to have any interleaved partitions,
610 * otherwise there's nothing to interleave with.
611 */
612 if (nparts > 1)
613 {
614 /*
615 * Short-circuit check to see if only 1 Datum is allowed per
616 * partition. When this is true there's no need to do the more
617 * expensive checks to look for interleaved values.
618 */
619 if (boundinfo->ndatums +
621 partition_bound_has_default(boundinfo) != nparts)
622 {
623 int last_index = -1;
624
625 /*
626 * Since the indexes array is sorted in Datum order, if any
627 * partitions are interleaved then it will show up by the
628 * partition indexes not being in ascending order. Here we check
629 * for that and record all partitions that are out of order.
630 */
631 for (i = 0; i < boundinfo->nindexes; i++)
632 {
633 int index = boundinfo->indexes[i];
634
635 if (index < last_index)
636 boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
637 index);
638
639 /*
640 * Otherwise, if the null_index exists in the indexes array,
641 * then the NULL partition must also allow some other Datum,
642 * therefore it's "interleaved".
643 */
644 else if (partition_bound_accepts_nulls(boundinfo) &&
645 index == boundinfo->null_index)
646 boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
647 index);
648
650 }
651 }
652
653 /*
654 * The DEFAULT partition is the "catch-all" partition that can contain
655 * anything that does not belong to any other partition. If there are
656 * any other partitions then the DEFAULT partition must be marked as
657 * interleaved.
658 */
659 if (partition_bound_has_default(boundinfo))
660 boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
661 boundinfo->default_index);
662 }
663
664
665 /* All partitions must now have been assigned canonical indexes. */
666 Assert(next_index == nparts);
667 return boundinfo;
668}
669
670/*
671 * create_range_bounds
672 * Create a PartitionBoundInfo for a range partitioned table
673 */
676 PartitionKey key, int **mapping)
677{
678 PartitionBoundInfo boundinfo;
681 *prev;
682 int i,
683 k,
684 partnatts;
685 int ndatums = 0;
686 int default_index = -1;
687 int next_index = 0;
690
692 boundinfo->strategy = key->strategy;
693 /* There is no special null-accepting range partition. */
694 boundinfo->null_index = -1;
695 /* Will be set correctly below. */
696 boundinfo->default_index = -1;
697
699
700 /* Create a unified list of range bounds across all the partitions. */
701 ndatums = 0;
702 for (i = 0; i < nparts; i++)
703 {
706 *upper;
707
708 if (spec->strategy != PARTITION_STRATEGY_RANGE)
709 elog(ERROR, "invalid strategy in partition bound spec");
710
711 /*
712 * Note the index of the partition bound spec for the default
713 * partition. There's no datum to add to the all_bounds array for
714 * this partition.
715 */
716 if (spec->is_default)
717 {
718 default_index = i;
719 continue;
720 }
721
722 lower = make_one_partition_rbound(key, i, spec->lowerdatums, true);
723 upper = make_one_partition_rbound(key, i, spec->upperdatums, false);
724 all_bounds[ndatums++] = lower;
725 all_bounds[ndatums++] = upper;
726 }
727
728 Assert(ndatums == nparts * 2 ||
729 (default_index != -1 && ndatums == (nparts - 1) * 2));
730
731 /* Sort all the bounds in ascending order */
732 qsort_arg(all_bounds, ndatums,
733 sizeof(PartitionRangeBound *),
735 key);
736
737 /* Save distinct bounds from all_bounds into rbounds. */
739 palloc(ndatums * sizeof(PartitionRangeBound *));
740 k = 0;
741 prev = NULL;
742 for (i = 0; i < ndatums; i++)
743 {
745 bool is_distinct = false;
746 int j;
747
748 /* Is the current bound distinct from the previous one? */
749 for (j = 0; j < key->partnatts; j++)
750 {
752
753 if (prev == NULL || cur->kind[j] != prev->kind[j])
754 {
755 is_distinct = true;
756 break;
757 }
758
759 /*
760 * If the bounds are both MINVALUE or MAXVALUE, stop now and treat
761 * them as equal, since any values after this point must be
762 * ignored.
763 */
764 if (cur->kind[j] != PARTITION_RANGE_DATUM_VALUE)
765 break;
766
767 cmpval = FunctionCall2Coll(&key->partsupfunc[j],
768 key->partcollation[j],
769 cur->datums[j],
770 prev->datums[j]);
771 if (DatumGetInt32(cmpval) != 0)
772 {
773 is_distinct = true;
774 break;
775 }
776 }
777
778 /*
779 * Only if the bound is distinct save it into a temporary array, i.e,
780 * rbounds which is later copied into boundinfo datums array.
781 */
782 if (is_distinct)
783 rbounds[k++] = all_bounds[i];
784
785 prev = cur;
786 }
787
789
790 /* Update ndatums to hold the count of distinct datums. */
791 ndatums = k;
792
793 /*
794 * Add datums to boundinfo. Canonical indexes are values ranging from 0
795 * to nparts - 1, assigned in that order to each partition's upper bound.
796 * For 'datums' elements that are lower bounds, there is -1 in the
797 * 'indexes' array to signify that no partition exists for the values less
798 * than such a bound and greater than or equal to the previous upper
799 * bound.
800 */
801 boundinfo->ndatums = ndatums;
802 boundinfo->datums = palloc0_array(Datum *, ndatums);
803 boundinfo->kind = palloc0_array(PartitionRangeDatumKind *, ndatums);
804 boundinfo->interleaved_parts = NULL;
805
806 /*
807 * For range partitioning, an additional value of -1 is stored as the last
808 * element of the indexes[] array.
809 */
810 boundinfo->nindexes = ndatums + 1;
811 boundinfo->indexes = palloc_array(int, (ndatums + 1));
812
813 /*
814 * In the loop below, to save from allocating a series of small arrays,
815 * here we just allocate a single array for Datums and another for
816 * PartitionRangeDatumKinds, below we'll just assign a portion of these
817 * arrays in each loop.
818 */
819 partnatts = key->partnatts;
820 boundDatums = (Datum *) palloc(ndatums * partnatts * sizeof(Datum));
821 boundKinds = palloc_array(PartitionRangeDatumKind, ndatums * partnatts);
822
823 for (i = 0; i < ndatums; i++)
824 {
825 int j;
826
827 boundinfo->datums[i] = &boundDatums[i * partnatts];
828 boundinfo->kind[i] = &boundKinds[i * partnatts];
829 for (j = 0; j < partnatts; j++)
830 {
831 if (rbounds[i]->kind[j] == PARTITION_RANGE_DATUM_VALUE)
832 boundinfo->datums[i][j] =
833 datumCopy(rbounds[i]->datums[j],
834 key->parttypbyval[j],
835 key->parttyplen[j]);
836 boundinfo->kind[i][j] = rbounds[i]->kind[j];
837 }
838
839 /*
840 * There is no mapping for invalid indexes.
841 *
842 * Any lower bounds in the rbounds array have invalid indexes
843 * assigned, because the values between the previous bound (if there
844 * is one) and this (lower) bound are not part of the range of any
845 * existing partition.
846 */
847 if (rbounds[i]->lower)
848 boundinfo->indexes[i] = -1;
849 else
850 {
851 int orig_index = rbounds[i]->index;
852
853 /* If the old index has no mapping, assign one */
854 if ((*mapping)[orig_index] == -1)
855 (*mapping)[orig_index] = next_index++;
856
857 boundinfo->indexes[i] = (*mapping)[orig_index];
858 }
859 }
860
861 pfree(rbounds);
862
863 /* Set the canonical value for default_index, if any. */
864 if (default_index != -1)
865 {
866 Assert(default_index >= 0 && (*mapping)[default_index] == -1);
867 (*mapping)[default_index] = next_index++;
868 boundinfo->default_index = (*mapping)[default_index];
869 }
870
871 /* The extra -1 element. */
872 Assert(i == ndatums);
873 boundinfo->indexes[i] = -1;
874
875 /* All partitions must now have been assigned canonical indexes. */
876 Assert(next_index == nparts);
877 return boundinfo;
878}
879
880/*
881 * Are two partition bound collections logically equal?
882 *
883 * Used in the keep logic of relcache.c (ie, in RelationClearRelation()).
884 * This is also useful when b1 and b2 are bound collections of two separate
885 * relations, respectively, because PartitionBoundInfo is a canonical
886 * representation of partition bounds.
887 */
888bool
889partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
891{
892 int i;
893
894 if (b1->strategy != b2->strategy)
895 return false;
896
897 if (b1->ndatums != b2->ndatums)
898 return false;
899
900 if (b1->nindexes != b2->nindexes)
901 return false;
902
903 if (b1->null_index != b2->null_index)
904 return false;
905
906 if (b1->default_index != b2->default_index)
907 return false;
908
909 /* For all partition strategies, the indexes[] arrays have to match */
910 for (i = 0; i < b1->nindexes; i++)
911 {
912 if (b1->indexes[i] != b2->indexes[i])
913 return false;
914 }
915
916 /* Finally, compare the datums[] arrays */
917 if (b1->strategy == PARTITION_STRATEGY_HASH)
918 {
919 /*
920 * We arrange the partitions in the ascending order of their moduli
921 * and remainders. Also every modulus is factor of next larger
922 * modulus. Therefore we can safely store index of a given partition
923 * in indexes array at remainder of that partition. Also entries at
924 * (remainder + N * modulus) positions in indexes array are all same
925 * for (modulus, remainder) specification for any partition. Thus the
926 * datums arrays from the given bounds are the same, if and only if
927 * their indexes arrays are the same. So, it suffices to compare the
928 * indexes arrays.
929 *
930 * Nonetheless make sure that the bounds are indeed the same when the
931 * indexes match. Hash partition bound stores modulus and remainder
932 * at b1->datums[i][0] and b1->datums[i][1] position respectively.
933 */
934#ifdef USE_ASSERT_CHECKING
935 for (i = 0; i < b1->ndatums; i++)
936 Assert((b1->datums[i][0] == b2->datums[i][0] &&
937 b1->datums[i][1] == b2->datums[i][1]));
938#endif
939 }
940 else
941 {
942 for (i = 0; i < b1->ndatums; i++)
943 {
944 int j;
945
946 for (j = 0; j < partnatts; j++)
947 {
948 /* For range partitions, the bounds might not be finite. */
949 if (b1->kind != NULL)
950 {
951 /* The different kinds of bound all differ from each other */
952 if (b1->kind[i][j] != b2->kind[i][j])
953 return false;
954
955 /*
956 * Non-finite bounds are equal without further
957 * examination.
958 */
959 if (b1->kind[i][j] != PARTITION_RANGE_DATUM_VALUE)
960 continue;
961 }
962
963 /*
964 * Compare the actual values. Note that it would be both
965 * incorrect and unsafe to invoke the comparison operator
966 * derived from the partitioning specification here. It would
967 * be incorrect because we want the relcache entry to be
968 * updated for ANY change to the partition bounds, not just
969 * those that the partitioning operator thinks are
970 * significant. It would be unsafe because we might reach
971 * this code in the context of an aborted transaction, and an
972 * arbitrary partitioning operator might not be safe in that
973 * context. datumIsEqual() should be simple enough to be
974 * safe.
975 */
976 if (!datumIsEqual(b1->datums[i][j], b2->datums[i][j],
977 parttypbyval[j], parttyplen[j]))
978 return false;
979 }
980 }
981 }
982 return true;
983}
984
985/*
986 * Return a copy of given PartitionBoundInfo structure. The data types of bounds
987 * are described by given partition key specification.
988 *
989 * Note: it's important that this function and its callees not do any catalog
990 * access, nor anything else that would result in allocating memory other than
991 * the returned data structure. Since this is called in a long-lived context,
992 * that would result in unwanted memory leaks.
993 */
996 PartitionKey key)
997{
999 int i;
1000 int ndatums;
1001 int nindexes;
1002 int partnatts;
1003
1005
1006 dest->strategy = src->strategy;
1007 ndatums = dest->ndatums = src->ndatums;
1008 nindexes = dest->nindexes = src->nindexes;
1009 partnatts = key->partnatts;
1010
1011 /* List partitioned tables have only a single partition key. */
1012 Assert(key->strategy != PARTITION_STRATEGY_LIST || partnatts == 1);
1013
1014 dest->datums = palloc_array(Datum *, ndatums);
1015
1016 if (src->kind != NULL && ndatums > 0)
1017 {
1019
1020 /* only RANGE partition should have a non-NULL kind */
1021 Assert(key->strategy == PARTITION_STRATEGY_RANGE);
1022
1023 dest->kind = (PartitionRangeDatumKind **) palloc(ndatums *
1024 sizeof(PartitionRangeDatumKind *));
1025
1026 /*
1027 * In the loop below, to save from allocating a series of small arrays
1028 * for storing the PartitionRangeDatumKind, we allocate a single chunk
1029 * here and use a smaller portion of it for each datum.
1030 */
1031 boundKinds = (PartitionRangeDatumKind *) palloc(ndatums * partnatts *
1032 sizeof(PartitionRangeDatumKind));
1033
1034 for (i = 0; i < ndatums; i++)
1035 {
1036 dest->kind[i] = &boundKinds[i * partnatts];
1037 memcpy(dest->kind[i], src->kind[i],
1038 sizeof(PartitionRangeDatumKind) * partnatts);
1039 }
1040 }
1041 else
1042 dest->kind = NULL;
1043
1044 /* copy interleaved partitions for LIST partitioned tables */
1045 dest->interleaved_parts = bms_copy(src->interleaved_parts);
1046
1047 /*
1048 * For hash partitioning, datums array will have two elements - modulus
1049 * and remainder.
1050 */
1051 if (ndatums > 0)
1052 {
1053 bool hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
1054 int natts = hash_part ? 2 : partnatts;
1055 Datum *boundDatums = palloc(ndatums * natts * sizeof(Datum));
1056
1057 for (i = 0; i < ndatums; i++)
1058 {
1059 int j;
1060
1061 dest->datums[i] = &boundDatums[i * natts];
1062
1063 for (j = 0; j < natts; j++)
1064 {
1065 if (dest->kind == NULL ||
1066 dest->kind[i][j] == PARTITION_RANGE_DATUM_VALUE)
1067 {
1068 bool byval;
1069 int typlen;
1070
1071 if (hash_part)
1072 {
1073 typlen = sizeof(int32); /* Always int4 */
1074 byval = true; /* int4 is pass-by-value */
1075 }
1076 else
1077 {
1078 byval = key->parttypbyval[j];
1079 typlen = key->parttyplen[j];
1080 }
1081 dest->datums[i][j] = datumCopy(src->datums[i][j],
1082 byval, typlen);
1083 }
1084 }
1085 }
1086 }
1087
1088 dest->indexes = palloc_array(int, nindexes);
1089 memcpy(dest->indexes, src->indexes, sizeof(int) * nindexes);
1090
1091 dest->null_index = src->null_index;
1092 dest->default_index = src->default_index;
1093
1094 return dest;
1095}
1096
1097/*
1098 * partition_bounds_merge
1099 * Check to see whether every partition of 'outer_rel' matches/overlaps
1100 * one partition of 'inner_rel' at most, and vice versa; and if so, build
1101 * and return the partition bounds for a join relation between the rels,
1102 * generating two lists of the matching/overlapping partitions, which are
1103 * returned to *outer_parts and *inner_parts respectively.
1104 *
1105 * The lists contain the same number of partitions, and the partitions at the
1106 * same positions in the lists indicate join pairs used for partitioned join.
1107 * If a partition on one side matches/overlaps multiple partitions on the other
1108 * side, this function returns NULL, setting *outer_parts and *inner_parts to
1109 * NIL.
1110 */
1113 FmgrInfo *partsupfunc, Oid *partcollation,
1115 JoinType jointype,
1117{
1118 /*
1119 * Currently, this function is called only from try_partitionwise_join(),
1120 * so the join type should be INNER, LEFT, FULL, SEMI, or ANTI.
1121 */
1122 Assert(jointype == JOIN_INNER || jointype == JOIN_LEFT ||
1123 jointype == JOIN_FULL || jointype == JOIN_SEMI ||
1124 jointype == JOIN_ANTI);
1125
1126 /* The partitioning strategies should be the same. */
1127 Assert(outer_rel->boundinfo->strategy == inner_rel->boundinfo->strategy);
1128
1130 switch (outer_rel->boundinfo->strategy)
1131 {
1133
1134 /*
1135 * For hash partitioned tables, we currently support partitioned
1136 * join only when they have exactly the same partition bounds.
1137 *
1138 * XXX: it might be possible to relax the restriction to support
1139 * cases where hash partitioned tables have missing partitions
1140 * and/or different moduli, but it's not clear if it would be
1141 * useful to support the former case since it's unusual to have
1142 * missing partitions. On the other hand, it would be useful to
1143 * support the latter case, but in that case, there is a high
1144 * probability that a partition on one side will match multiple
1145 * partitions on the other side, which is the scenario the current
1146 * implementation of partitioned join can't handle.
1147 */
1148 return NULL;
1149
1151 return merge_list_bounds(partsupfunc,
1152 partcollation,
1153 outer_rel,
1154 inner_rel,
1155 jointype,
1157 inner_parts);
1158
1160 return merge_range_bounds(partnatts,
1161 partsupfunc,
1162 partcollation,
1163 outer_rel,
1164 inner_rel,
1165 jointype,
1167 inner_parts);
1168 }
1169
1170 return NULL;
1171}
1172
1173/*
1174 * merge_list_bounds
1175 * Create the partition bounds for a join relation between list
1176 * partitioned tables, if possible
1177 *
1178 * In this function we try to find sets of matching partitions from both sides
1179 * by comparing list values stored in their partition bounds. Since the list
1180 * values appear in the ascending order, an algorithm similar to merge join is
1181 * used for that. If a partition on one side doesn't have a matching
1182 * partition on the other side, the algorithm tries to match it with the
1183 * default partition on the other side if any; if not, the algorithm tries to
1184 * match it with a dummy partition on the other side if it's on the
1185 * non-nullable side of an outer join. Also, if both sides have the default
1186 * partitions, the algorithm tries to match them with each other. We give up
1187 * if the algorithm finds a partition matching multiple partitions on the
1188 * other side, which is the scenario the current implementation of partitioned
1189 * join can't handle.
1190 */
1191static PartitionBoundInfo
1192merge_list_bounds(FmgrInfo *partsupfunc, Oid *partcollation,
1194 JoinType jointype,
1196{
1202 int outer_default = outer_bi->default_index;
1203 int inner_default = inner_bi->default_index;
1208 int outer_pos;
1209 int inner_pos;
1210 int next_index = 0;
1211 int null_index = -1;
1212 int default_index = -1;
1214 List *merged_indexes = NIL;
1215
1216 Assert(*outer_parts == NIL);
1217 Assert(*inner_parts == NIL);
1218 Assert(outer_bi->strategy == inner_bi->strategy &&
1219 outer_bi->strategy == PARTITION_STRATEGY_LIST);
1220 /* List partitioning doesn't require kinds. */
1221 Assert(!outer_bi->kind && !inner_bi->kind);
1222
1225
1226 /*
1227 * If the default partitions (if any) have been proven empty, deem them
1228 * non-existent.
1229 */
1231 outer_has_default = false;
1233 inner_has_default = false;
1234
1235 /*
1236 * Merge partitions from both sides. In each iteration we compare a pair
1237 * of list values, one from each side, and decide whether the
1238 * corresponding partitions match or not. If the two values match
1239 * exactly, move to the next pair of list values, otherwise move to the
1240 * next list value on the side with a smaller list value.
1241 */
1242 outer_pos = inner_pos = 0;
1243 while (outer_pos < outer_bi->ndatums || inner_pos < inner_bi->ndatums)
1244 {
1245 int outer_index = -1;
1246 int inner_index = -1;
1249 int cmpval;
1251 int merged_index = -1;
1252
1253 if (outer_pos < outer_bi->ndatums)
1254 {
1255 /*
1256 * If the partition on the outer side has been proven empty,
1257 * ignore it and move to the next datum on the outer side.
1258 */
1259 outer_index = outer_bi->indexes[outer_pos];
1261 {
1262 outer_pos++;
1263 continue;
1264 }
1265 }
1266 if (inner_pos < inner_bi->ndatums)
1267 {
1268 /*
1269 * If the partition on the inner side has been proven empty,
1270 * ignore it and move to the next datum on the inner side.
1271 */
1272 inner_index = inner_bi->indexes[inner_pos];
1274 {
1275 inner_pos++;
1276 continue;
1277 }
1278 }
1279
1280 /* Get the list values. */
1281 outer_datums = outer_pos < outer_bi->ndatums ?
1282 outer_bi->datums[outer_pos] : NULL;
1283 inner_datums = inner_pos < inner_bi->ndatums ?
1284 inner_bi->datums[inner_pos] : NULL;
1285
1286 /*
1287 * We run this loop till both sides finish. This allows us to avoid
1288 * duplicating code to handle the remaining values on the side which
1289 * finishes later. For that we set the comparison parameter cmpval in
1290 * such a way that it appears as if the side which finishes earlier
1291 * has an extra value higher than any other value on the unfinished
1292 * side. That way we advance the values on the unfinished side till
1293 * all of its values are exhausted.
1294 */
1295 if (outer_pos >= outer_bi->ndatums)
1296 cmpval = 1;
1297 else if (inner_pos >= inner_bi->ndatums)
1298 cmpval = -1;
1299 else
1300 {
1302 cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
1303 partcollation[0],
1304 outer_datums[0],
1305 inner_datums[0]));
1306 }
1307
1308 if (cmpval == 0)
1309 {
1310 /* Two list values match exactly. */
1313 Assert(outer_index >= 0);
1314 Assert(inner_index >= 0);
1315
1316 /*
1317 * Try merging both partitions. If successful, add the list value
1318 * and index of the merged partition below.
1319 */
1322 &next_index);
1323 if (merged_index == -1)
1324 goto cleanup;
1325
1327
1328 /* Move to the next pair of list values. */
1329 outer_pos++;
1330 inner_pos++;
1331 }
1332 else if (cmpval < 0)
1333 {
1334 /* A list value missing from the inner side. */
1336
1337 /*
1338 * If the inner side has the default partition, or this is an
1339 * outer join, try to assign a merged partition to the outer
1340 * partition (see process_outer_partition()). Otherwise, the
1341 * outer partition will not contribute to the result.
1342 */
1343 if (inner_has_default || IS_OUTER_JOIN(jointype))
1344 {
1345 /* Get the outer partition. */
1346 outer_index = outer_bi->indexes[outer_pos];
1347 Assert(outer_index >= 0);
1349 &inner_map,
1354 jointype,
1355 &next_index,
1356 &default_index);
1357 if (merged_index == -1)
1358 goto cleanup;
1360 }
1361
1362 /* Move to the next list value on the outer side. */
1363 outer_pos++;
1364 }
1365 else
1366 {
1367 /* A list value missing from the outer side. */
1368 Assert(cmpval > 0);
1370
1371 /*
1372 * If the outer side has the default partition, or this is a FULL
1373 * join, try to assign a merged partition to the inner partition
1374 * (see process_inner_partition()). Otherwise, the inner
1375 * partition will not contribute to the result.
1376 */
1377 if (outer_has_default || jointype == JOIN_FULL)
1378 {
1379 /* Get the inner partition. */
1380 inner_index = inner_bi->indexes[inner_pos];
1381 Assert(inner_index >= 0);
1383 &inner_map,
1388 jointype,
1389 &next_index,
1390 &default_index);
1391 if (merged_index == -1)
1392 goto cleanup;
1394 }
1395
1396 /* Move to the next list value on the inner side. */
1397 inner_pos++;
1398 }
1399
1400 /*
1401 * If we assigned a merged partition, add the list value and index of
1402 * the merged partition if appropriate.
1403 */
1404 if (merged_index >= 0 && merged_index != default_index)
1405 {
1407 merged_indexes = lappend_int(merged_indexes, merged_index);
1408 }
1409 }
1410
1411 /*
1412 * If the NULL partitions (if any) have been proven empty, deem them
1413 * non-existent.
1414 */
1415 if (outer_has_null &&
1417 outer_has_null = false;
1418 if (inner_has_null &&
1420 inner_has_null = false;
1421
1422 /* Merge the NULL partitions if any. */
1426 outer_bi->null_index, inner_bi->null_index,
1427 jointype, &next_index, &null_index);
1428 else
1429 Assert(null_index == -1);
1430
1431 /* Merge the default partitions if any. */
1436 jointype, &next_index, &default_index);
1437 else
1438 Assert(default_index == -1);
1439
1440 /* If we have merged partitions, create the partition bounds. */
1441 if (next_index > 0)
1442 {
1443 /* Fix the merged_indexes list if necessary. */
1444 if (outer_map.did_remapping || inner_map.did_remapping)
1445 {
1446 Assert(jointype == JOIN_FULL);
1448 next_index, merged_indexes);
1449 }
1450
1451 /* Use maps to match partitions from inputs. */
1454 next_index,
1456 Assert(*outer_parts != NIL);
1457 Assert(*inner_parts != NIL);
1460
1461 /* Make a PartitionBoundInfo struct to return. */
1464 NIL,
1465 merged_indexes,
1466 null_index,
1467 default_index);
1469 }
1470
1471cleanup:
1472 /* Free local memory before returning. */
1474 list_free(merged_indexes);
1477
1478 return merged_bounds;
1479}
1480
1481/*
1482 * merge_range_bounds
1483 * Create the partition bounds for a join relation between range
1484 * partitioned tables, if possible
1485 *
1486 * In this function we try to find sets of overlapping partitions from both
1487 * sides by comparing ranges stored in their partition bounds. Since the
1488 * ranges appear in the ascending order, an algorithm similar to merge join is
1489 * used for that. If a partition on one side doesn't have an overlapping
1490 * partition on the other side, the algorithm tries to match it with the
1491 * default partition on the other side if any; if not, the algorithm tries to
1492 * match it with a dummy partition on the other side if it's on the
1493 * non-nullable side of an outer join. Also, if both sides have the default
1494 * partitions, the algorithm tries to match them with each other. We give up
1495 * if the algorithm finds a partition overlapping multiple partitions on the
1496 * other side, which is the scenario the current implementation of partitioned
1497 * join can't handle.
1498 */
1499static PartitionBoundInfo
1503 JoinType jointype,
1505{
1511 int outer_default = outer_bi->default_index;
1512 int inner_default = inner_bi->default_index;
1515 int outer_index;
1516 int inner_index;
1517 int outer_lb_pos;
1518 int inner_lb_pos;
1523 int next_index = 0;
1524 int default_index = -1;
1527 List *merged_indexes = NIL;
1528
1529 Assert(*outer_parts == NIL);
1530 Assert(*inner_parts == NIL);
1531 Assert(outer_bi->strategy == inner_bi->strategy &&
1532 outer_bi->strategy == PARTITION_STRATEGY_RANGE);
1533
1536
1537 /*
1538 * If the default partitions (if any) have been proven empty, deem them
1539 * non-existent.
1540 */
1542 outer_has_default = false;
1544 inner_has_default = false;
1545
1546 /*
1547 * Merge partitions from both sides. In each iteration we compare a pair
1548 * of ranges, one from each side, and decide whether the corresponding
1549 * partitions match or not. If the two ranges overlap, move to the next
1550 * pair of ranges, otherwise move to the next range on the side with a
1551 * lower range. outer_lb_pos/inner_lb_pos keep track of the positions of
1552 * lower bounds in the datums arrays in the outer/inner
1553 * PartitionBoundInfos respectively.
1554 */
1557 &outer_lb, &outer_ub);
1559 &inner_lb, &inner_ub);
1560 while (outer_index >= 0 || inner_index >= 0)
1561 {
1562 bool overlap;
1563 int ub_cmpval;
1564 int lb_cmpval;
1565 PartitionRangeBound merged_lb = {-1, NULL, NULL, true};
1566 PartitionRangeBound merged_ub = {-1, NULL, NULL, false};
1567 int merged_index = -1;
1568
1569 /*
1570 * We run this loop till both sides finish. This allows us to avoid
1571 * duplicating code to handle the remaining ranges on the side which
1572 * finishes later. For that we set the comparison parameter cmpval in
1573 * such a way that it appears as if the side which finishes earlier
1574 * has an extra range higher than any other range on the unfinished
1575 * side. That way we advance the ranges on the unfinished side till
1576 * all of its ranges are exhausted.
1577 */
1578 if (outer_index == -1)
1579 {
1580 overlap = false;
1581 lb_cmpval = 1;
1582 ub_cmpval = 1;
1583 }
1584 else if (inner_index == -1)
1585 {
1586 overlap = false;
1587 lb_cmpval = -1;
1588 ub_cmpval = -1;
1589 }
1590 else
1591 overlap = compare_range_partitions(partnatts, partsupfuncs,
1593 &outer_lb, &outer_ub,
1594 &inner_lb, &inner_ub,
1595 &lb_cmpval, &ub_cmpval);
1596
1597 if (overlap)
1598 {
1599 /* Two ranges overlap; form a join pair. */
1600
1603
1604 /* Both partitions should not have been merged yet. */
1605 Assert(outer_index >= 0);
1606 Assert(outer_map.merged_indexes[outer_index] == -1 &&
1607 outer_map.merged[outer_index] == false);
1608 Assert(inner_index >= 0);
1609 Assert(inner_map.merged_indexes[inner_index] == -1 &&
1610 inner_map.merged[inner_index] == false);
1611
1612 /*
1613 * Get the index of the merged partition. Both partitions aren't
1614 * merged yet, so the partitions should be merged successfully.
1615 */
1618 &next_index);
1619 Assert(merged_index >= 0);
1620
1621 /* Get the range bounds of the merged partition. */
1623 partcollations, jointype,
1624 &outer_lb, &outer_ub,
1625 &inner_lb, &inner_ub,
1627 &merged_lb, &merged_ub);
1628
1629 /* Save the upper bounds of both partitions for use below. */
1632
1633 /* Move to the next pair of ranges. */
1635 &outer_lb, &outer_ub);
1637 &inner_lb, &inner_ub);
1638
1639 /*
1640 * If the range of a partition on one side overlaps the range of
1641 * the next partition on the other side, that will cause the
1642 * partition on one side to match at least two partitions on the
1643 * other side, which is the case that we currently don't support
1644 * partitioned join for; give up.
1645 */
1646 if (ub_cmpval > 0 && inner_index >= 0 &&
1648 &save_outer_ub, &inner_lb) > 0)
1649 goto cleanup;
1652 &outer_lb, &save_inner_ub) < 0)
1653 goto cleanup;
1654
1655 /*
1656 * A row from a non-overlapping portion (if any) of a partition on
1657 * one side might find its join partner in the default partition
1658 * (if any) on the other side, causing the same situation as
1659 * above; give up in that case.
1660 */
1661 if ((outer_has_default && (lb_cmpval > 0 || ub_cmpval < 0)) ||
1663 goto cleanup;
1664 }
1665 else if (ub_cmpval < 0)
1666 {
1667 /* A non-overlapping outer range. */
1668
1669 /* The outer partition should not have been merged yet. */
1670 Assert(outer_index >= 0);
1671 Assert(outer_map.merged_indexes[outer_index] == -1 &&
1672 outer_map.merged[outer_index] == false);
1673
1674 /*
1675 * If the inner side has the default partition, or this is an
1676 * outer join, try to assign a merged partition to the outer
1677 * partition (see process_outer_partition()). Otherwise, the
1678 * outer partition will not contribute to the result.
1679 */
1680 if (inner_has_default || IS_OUTER_JOIN(jointype))
1681 {
1683 &inner_map,
1688 jointype,
1689 &next_index,
1690 &default_index);
1691 if (merged_index == -1)
1692 goto cleanup;
1695 }
1696
1697 /* Move to the next range on the outer side. */
1699 &outer_lb, &outer_ub);
1700 }
1701 else
1702 {
1703 /* A non-overlapping inner range. */
1704 Assert(ub_cmpval > 0);
1705
1706 /* The inner partition should not have been merged yet. */
1707 Assert(inner_index >= 0);
1708 Assert(inner_map.merged_indexes[inner_index] == -1 &&
1709 inner_map.merged[inner_index] == false);
1710
1711 /*
1712 * If the outer side has the default partition, or this is a FULL
1713 * join, try to assign a merged partition to the inner partition
1714 * (see process_inner_partition()). Otherwise, the inner
1715 * partition will not contribute to the result.
1716 */
1717 if (outer_has_default || jointype == JOIN_FULL)
1718 {
1720 &inner_map,
1725 jointype,
1726 &next_index,
1727 &default_index);
1728 if (merged_index == -1)
1729 goto cleanup;
1732 }
1733
1734 /* Move to the next range on the inner side. */
1736 &inner_lb, &inner_ub);
1737 }
1738
1739 /*
1740 * If we assigned a merged partition, add the range bounds and index
1741 * of the merged partition if appropriate.
1742 */
1743 if (merged_index >= 0 && merged_index != default_index)
1747 &merged_indexes);
1748 }
1749
1750 /* Merge the default partitions if any. */
1755 jointype, &next_index, &default_index);
1756 else
1757 Assert(default_index == -1);
1758
1759 /* If we have merged partitions, create the partition bounds. */
1760 if (next_index > 0)
1761 {
1762 /*
1763 * Unlike the case of list partitioning, we wouldn't have re-merged
1764 * partitions, so did_remapping should be left alone.
1765 */
1766 Assert(!outer_map.did_remapping);
1767 Assert(!inner_map.did_remapping);
1768
1769 /* Use maps to match partitions from inputs. */
1772 next_index,
1774 Assert(*outer_parts != NIL);
1775 Assert(*inner_parts != NIL);
1778
1779 /* Make a PartitionBoundInfo struct to return. */
1783 merged_indexes,
1784 -1,
1785 default_index);
1787 }
1788
1789cleanup:
1790 /* Free local memory before returning. */
1793 list_free(merged_indexes);
1796
1797 return merged_bounds;
1798}
1799
1800/*
1801 * init_partition_map
1802 * Initialize a PartitionMap struct for given relation
1803 */
1804static void
1806{
1807 int nparts = rel->nparts;
1808 int i;
1809
1810 map->nparts = nparts;
1811 map->merged_indexes = palloc_array(int, nparts);
1812 map->merged = palloc_array(bool, nparts);
1813 map->did_remapping = false;
1814 map->old_indexes = palloc_array(int, nparts);
1815 for (i = 0; i < nparts; i++)
1816 {
1817 map->merged_indexes[i] = map->old_indexes[i] = -1;
1818 map->merged[i] = false;
1819 }
1820}
1821
1822/*
1823 * free_partition_map
1824 */
1825static void
1827{
1828 pfree(map->merged_indexes);
1829 pfree(map->merged);
1830 pfree(map->old_indexes);
1831}
1832
1833/*
1834 * is_dummy_partition --- has partition been proven empty?
1835 */
1836static bool
1838{
1840
1841 Assert(part_index >= 0);
1842 part_rel = rel->part_rels[part_index];
1844 return true;
1845 return false;
1846}
1847
1848/*
1849 * merge_matching_partitions
1850 * Try to merge given outer/inner partitions, and return the index of a
1851 * merged partition produced from them if successful, -1 otherwise
1852 *
1853 * If the merged partition is newly created, *next_index is incremented.
1854 */
1855static int
1857 int outer_index, int inner_index, int *next_index)
1858{
1861 bool outer_merged;
1862 bool inner_merged;
1863
1865 outer_merged_index = outer_map->merged_indexes[outer_index];
1868 inner_merged_index = inner_map->merged_indexes[inner_index];
1870
1871 /*
1872 * Handle cases where we have already assigned a merged partition to each
1873 * of the given partitions.
1874 */
1875 if (outer_merged_index >= 0 && inner_merged_index >= 0)
1876 {
1877 /*
1878 * If the merged partitions are the same, no need to do anything;
1879 * return the index of the merged partitions. Otherwise, if each of
1880 * the given partitions has been merged with a dummy partition on the
1881 * other side, re-map them to either of the two merged partitions.
1882 * Otherwise, they can't be merged, so return -1.
1883 */
1885 {
1888 return outer_merged_index;
1889 }
1890 if (!outer_merged && !inner_merged)
1891 {
1892 /*
1893 * This can only happen for a list-partitioning case. We re-map
1894 * them to the merged partition with the smaller of the two merged
1895 * indexes to preserve the property that the canonical order of
1896 * list partitions is determined by the indexes assigned to the
1897 * smallest list value of each partition.
1898 */
1900 {
1901 outer_map->merged[outer_index] = true;
1902 inner_map->merged_indexes[inner_index] = outer_merged_index;
1903 inner_map->merged[inner_index] = true;
1904 inner_map->did_remapping = true;
1906 return outer_merged_index;
1907 }
1908 else
1909 {
1910 inner_map->merged[inner_index] = true;
1911 outer_map->merged_indexes[outer_index] = inner_merged_index;
1912 outer_map->merged[outer_index] = true;
1913 outer_map->did_remapping = true;
1915 return inner_merged_index;
1916 }
1917 }
1918 return -1;
1919 }
1920
1921 /* At least one of the given partitions should not have yet been merged. */
1923
1924 /*
1925 * If neither of them has been merged, merge them. Otherwise, if one has
1926 * been merged with a dummy partition on the other side (and the other
1927 * hasn't yet been merged with anything), re-merge them. Otherwise, they
1928 * can't be merged, so return -1.
1929 */
1930 if (outer_merged_index == -1 && inner_merged_index == -1)
1931 {
1932 int merged_index = *next_index;
1933
1936 outer_map->merged_indexes[outer_index] = merged_index;
1937 outer_map->merged[outer_index] = true;
1938 inner_map->merged_indexes[inner_index] = merged_index;
1939 inner_map->merged[inner_index] = true;
1940 *next_index = *next_index + 1;
1941 return merged_index;
1942 }
1943 if (outer_merged_index >= 0 && !outer_map->merged[outer_index])
1944 {
1947 inner_map->merged_indexes[inner_index] = outer_merged_index;
1948 inner_map->merged[inner_index] = true;
1949 outer_map->merged[outer_index] = true;
1950 return outer_merged_index;
1951 }
1952 if (inner_merged_index >= 0 && !inner_map->merged[inner_index])
1953 {
1956 outer_map->merged_indexes[outer_index] = inner_merged_index;
1957 outer_map->merged[outer_index] = true;
1958 inner_map->merged[inner_index] = true;
1959 return inner_merged_index;
1960 }
1961 return -1;
1962}
1963
1964/*
1965 * process_outer_partition
1966 * Try to assign given outer partition a merged partition, and return the
1967 * index of the merged partition if successful, -1 otherwise
1968 *
1969 * If the partition is newly created, *next_index is incremented. Also, if it
1970 * is the default partition of the join relation, *default_index is set to the
1971 * index if not already done.
1972 */
1973static int
1976 bool outer_has_default,
1977 bool inner_has_default,
1978 int outer_index,
1979 int inner_default,
1980 JoinType jointype,
1981 int *next_index,
1982 int *default_index)
1983{
1984 int merged_index = -1;
1985
1986 Assert(outer_index >= 0);
1987
1988 /*
1989 * If the inner side has the default partition, a row from the outer
1990 * partition might find its join partner in the default partition; try
1991 * merging the outer partition with the default partition. Otherwise,
1992 * this should be an outer join, in which case the outer partition has to
1993 * be scanned all the way anyway; merge the outer partition with a dummy
1994 * partition on the other side.
1995 */
1997 {
1998 Assert(inner_default >= 0);
1999
2000 /*
2001 * If the outer side has the default partition as well, the default
2002 * partition on the inner side will have two matching partitions on
2003 * the other side: the outer partition and the default partition on
2004 * the outer side. Partitionwise join doesn't handle this scenario
2005 * yet.
2006 */
2008 return -1;
2009
2012 next_index);
2013 if (merged_index == -1)
2014 return -1;
2015
2016 /*
2017 * If this is a FULL join, the default partition on the inner side has
2018 * to be scanned all the way anyway, so the resulting partition will
2019 * contain all key values from the default partition, which any other
2020 * partition of the join relation will not contain. Thus the
2021 * resulting partition will act as the default partition of the join
2022 * relation; record the index in *default_index if not already done.
2023 */
2024 if (jointype == JOIN_FULL)
2025 {
2026 if (*default_index == -1)
2027 *default_index = merged_index;
2028 else
2029 Assert(*default_index == merged_index);
2030 }
2031 }
2032 else
2033 {
2034 Assert(IS_OUTER_JOIN(jointype));
2035 Assert(jointype != JOIN_RIGHT);
2036
2037 /* If we have already assigned a partition, no need to do anything. */
2038 merged_index = outer_map->merged_indexes[outer_index];
2039 if (merged_index == -1)
2041 next_index);
2042 }
2043 return merged_index;
2044}
2045
2046/*
2047 * process_inner_partition
2048 * Try to assign given inner partition a merged partition, and return the
2049 * index of the merged partition if successful, -1 otherwise
2050 *
2051 * If the partition is newly created, *next_index is incremented. Also, if it
2052 * is the default partition of the join relation, *default_index is set to the
2053 * index if not already done.
2054 */
2055static int
2058 bool outer_has_default,
2059 bool inner_has_default,
2060 int inner_index,
2061 int outer_default,
2062 JoinType jointype,
2063 int *next_index,
2064 int *default_index)
2065{
2066 int merged_index = -1;
2067
2068 Assert(inner_index >= 0);
2069
2070 /*
2071 * If the outer side has the default partition, a row from the inner
2072 * partition might find its join partner in the default partition; try
2073 * merging the inner partition with the default partition. Otherwise,
2074 * this should be a FULL join, in which case the inner partition has to be
2075 * scanned all the way anyway; merge the inner partition with a dummy
2076 * partition on the other side.
2077 */
2079 {
2080 Assert(outer_default >= 0);
2081
2082 /*
2083 * If the inner side has the default partition as well, the default
2084 * partition on the outer side will have two matching partitions on
2085 * the other side: the inner partition and the default partition on
2086 * the inner side. Partitionwise join doesn't handle this scenario
2087 * yet.
2088 */
2090 return -1;
2091
2094 next_index);
2095 if (merged_index == -1)
2096 return -1;
2097
2098 /*
2099 * If this is an outer join, the default partition on the outer side
2100 * has to be scanned all the way anyway, so the resulting partition
2101 * will contain all key values from the default partition, which any
2102 * other partition of the join relation will not contain. Thus the
2103 * resulting partition will act as the default partition of the join
2104 * relation; record the index in *default_index if not already done.
2105 */
2106 if (IS_OUTER_JOIN(jointype))
2107 {
2108 Assert(jointype != JOIN_RIGHT);
2109 if (*default_index == -1)
2110 *default_index = merged_index;
2111 else
2112 Assert(*default_index == merged_index);
2113 }
2114 }
2115 else
2116 {
2117 Assert(jointype == JOIN_FULL);
2118
2119 /* If we have already assigned a partition, no need to do anything. */
2120 merged_index = inner_map->merged_indexes[inner_index];
2121 if (merged_index == -1)
2123 next_index);
2124 }
2125 return merged_index;
2126}
2127
2128/*
2129 * merge_null_partitions
2130 * Merge the NULL partitions from a join's outer and inner sides.
2131 *
2132 * If the merged partition produced from them is the NULL partition of the join
2133 * relation, *null_index is set to the index of the merged partition.
2134 *
2135 * Note: We assume here that the join clause for a partitioned join is strict
2136 * because have_partkey_equi_join() requires that the corresponding operator
2137 * be mergejoinable, and we currently assume that mergejoinable operators are
2138 * strict (see MJEvalOuterValues()/MJEvalInnerValues()).
2139 */
2140static void
2143 bool outer_has_null,
2144 bool inner_has_null,
2145 int outer_null,
2146 int inner_null,
2147 JoinType jointype,
2148 int *next_index,
2149 int *null_index)
2150{
2151 bool consider_outer_null = false;
2152 bool consider_inner_null = false;
2153
2155 Assert(*null_index == -1);
2156
2157 /*
2158 * Check whether the NULL partitions have already been merged and if so,
2159 * set the consider_outer_null/consider_inner_null flags.
2160 */
2161 if (outer_has_null)
2162 {
2164 if (outer_map->merged_indexes[outer_null] == -1)
2165 consider_outer_null = true;
2166 }
2167 if (inner_has_null)
2168 {
2170 if (inner_map->merged_indexes[inner_null] == -1)
2171 consider_inner_null = true;
2172 }
2173
2174 /* If both flags are set false, we don't need to do anything. */
2176 return;
2177
2179 {
2181
2182 /*
2183 * If this is an outer join, the NULL partition on the outer side has
2184 * to be scanned all the way anyway; merge the NULL partition with a
2185 * dummy partition on the other side. In that case
2186 * consider_outer_null means that the NULL partition only contains
2187 * NULL values as the key values, so the merged partition will do so;
2188 * treat it as the NULL partition of the join relation.
2189 */
2190 if (IS_OUTER_JOIN(jointype))
2191 {
2192 Assert(jointype != JOIN_RIGHT);
2194 next_index);
2195 }
2196 }
2198 {
2200
2201 /*
2202 * If this is a FULL join, the NULL partition on the inner side has to
2203 * be scanned all the way anyway; merge the NULL partition with a
2204 * dummy partition on the other side. In that case
2205 * consider_inner_null means that the NULL partition only contains
2206 * NULL values as the key values, so the merged partition will do so;
2207 * treat it as the NULL partition of the join relation.
2208 */
2209 if (jointype == JOIN_FULL)
2211 next_index);
2212 }
2213 else
2214 {
2218
2219 /*
2220 * If this is an outer join, the NULL partition on the outer side (and
2221 * that on the inner side if this is a FULL join) have to be scanned
2222 * all the way anyway, so merge them. Note that each of the NULL
2223 * partitions isn't merged yet, so they should be merged successfully.
2224 * Like the above, each of the NULL partitions only contains NULL
2225 * values as the key values, so the merged partition will do so; treat
2226 * it as the NULL partition of the join relation.
2227 *
2228 * Note: if this an INNER/SEMI join, the join clause will never be
2229 * satisfied by two NULL values (see comments above), so both the NULL
2230 * partitions can be eliminated.
2231 */
2232 if (IS_OUTER_JOIN(jointype))
2233 {
2234 Assert(jointype != JOIN_RIGHT);
2237 next_index);
2238 Assert(*null_index >= 0);
2239 }
2240 }
2241}
2242
2243/*
2244 * merge_default_partitions
2245 * Merge the default partitions from a join's outer and inner sides.
2246 *
2247 * If the merged partition produced from them is the default partition of the
2248 * join relation, *default_index is set to the index of the merged partition.
2249 */
2250static void
2253 bool outer_has_default,
2254 bool inner_has_default,
2255 int outer_default,
2256 int inner_default,
2257 JoinType jointype,
2258 int *next_index,
2259 int *default_index)
2260{
2261 int outer_merged_index = -1;
2262 int inner_merged_index = -1;
2263
2265
2266 /* Get the merged partition indexes for the default partitions. */
2268 {
2270 outer_merged_index = outer_map->merged_indexes[outer_default];
2271 }
2273 {
2275 inner_merged_index = inner_map->merged_indexes[inner_default];
2276 }
2277
2279 {
2280 /*
2281 * If this is an outer join, the default partition on the outer side
2282 * has to be scanned all the way anyway; if we have not yet assigned a
2283 * partition, merge the default partition with a dummy partition on
2284 * the other side. The merged partition will act as the default
2285 * partition of the join relation (see comments in
2286 * process_inner_partition()).
2287 */
2288 if (IS_OUTER_JOIN(jointype))
2289 {
2290 Assert(jointype != JOIN_RIGHT);
2291 if (outer_merged_index == -1)
2292 {
2293 Assert(*default_index == -1);
2294 *default_index = merge_partition_with_dummy(outer_map,
2296 next_index);
2297 }
2298 else
2299 Assert(*default_index == outer_merged_index);
2300 }
2301 else
2302 Assert(*default_index == -1);
2303 }
2305 {
2306 /*
2307 * If this is a FULL join, the default partition on the inner side has
2308 * to be scanned all the way anyway; if we have not yet assigned a
2309 * partition, merge the default partition with a dummy partition on
2310 * the other side. The merged partition will act as the default
2311 * partition of the join relation (see comments in
2312 * process_outer_partition()).
2313 */
2314 if (jointype == JOIN_FULL)
2315 {
2316 if (inner_merged_index == -1)
2317 {
2318 Assert(*default_index == -1);
2319 *default_index = merge_partition_with_dummy(inner_map,
2321 next_index);
2322 }
2323 else
2324 Assert(*default_index == inner_merged_index);
2325 }
2326 else
2327 Assert(*default_index == -1);
2328 }
2329 else
2330 {
2332
2333 /*
2334 * The default partitions have to be joined with each other, so merge
2335 * them. Note that each of the default partitions isn't merged yet
2336 * (see, process_outer_partition()/process_inner_partition()), so they
2337 * should be merged successfully. The merged partition will act as
2338 * the default partition of the join relation.
2339 */
2342 Assert(*default_index == -1);
2343 *default_index = merge_matching_partitions(outer_map,
2344 inner_map,
2347 next_index);
2348 Assert(*default_index >= 0);
2349 }
2350}
2351
2352/*
2353 * merge_partition_with_dummy
2354 * Assign given partition a new partition of a join relation
2355 *
2356 * Note: The caller assumes that the given partition doesn't have a non-dummy
2357 * matching partition on the other side, but if the given partition finds the
2358 * matching partition later, we will adjust the assignment.
2359 */
2360static int
2362{
2363 int merged_index = *next_index;
2364
2365 Assert(index >= 0 && index < map->nparts);
2366 Assert(map->merged_indexes[index] == -1);
2367 Assert(!map->merged[index]);
2369 /* Leave the merged flag alone! */
2370 *next_index = *next_index + 1;
2371 return merged_index;
2372}
2373
2374/*
2375 * fix_merged_indexes
2376 * Adjust merged indexes of re-merged partitions
2377 */
2378static void
2380 int nmerged, List *merged_indexes)
2381{
2382 int *new_indexes;
2383 int merged_index;
2384 int i;
2385 ListCell *lc;
2386
2387 Assert(nmerged > 0);
2388
2390 for (i = 0; i < nmerged; i++)
2391 new_indexes[i] = -1;
2392
2393 /* Build the mapping of old merged indexes to new merged indexes. */
2394 if (outer_map->did_remapping)
2395 {
2396 for (i = 0; i < outer_map->nparts; i++)
2397 {
2398 merged_index = outer_map->old_indexes[i];
2399 if (merged_index >= 0)
2400 new_indexes[merged_index] = outer_map->merged_indexes[i];
2401 }
2402 }
2403 if (inner_map->did_remapping)
2404 {
2405 for (i = 0; i < inner_map->nparts; i++)
2406 {
2407 merged_index = inner_map->old_indexes[i];
2408 if (merged_index >= 0)
2409 new_indexes[merged_index] = inner_map->merged_indexes[i];
2410 }
2411 }
2412
2413 /* Fix the merged_indexes list using the mapping. */
2414 foreach(lc, merged_indexes)
2415 {
2417 Assert(merged_index >= 0);
2418 if (new_indexes[merged_index] >= 0)
2420 }
2421
2423}
2424
2425/*
2426 * generate_matching_part_pairs
2427 * Generate a pair of lists of partitions that produce merged partitions
2428 *
2429 * The lists of partitions are built in the order of merged partition indexes,
2430 * and returned in *outer_parts and *inner_parts.
2431 */
2432static void
2435 int nmerged,
2437{
2438 int outer_nparts = outer_map->nparts;
2439 int inner_nparts = inner_map->nparts;
2440 int *outer_indexes;
2441 int *inner_indexes;
2442 int max_nparts;
2443 int i;
2444
2445 Assert(nmerged > 0);
2446 Assert(*outer_parts == NIL);
2447 Assert(*inner_parts == NIL);
2448
2451 for (i = 0; i < nmerged; i++)
2452 outer_indexes[i] = inner_indexes[i] = -1;
2453
2454 /* Set pairs of matching partitions. */
2455 Assert(outer_nparts == outer_rel->nparts);
2456 Assert(inner_nparts == inner_rel->nparts);
2458 for (i = 0; i < max_nparts; i++)
2459 {
2460 if (i < outer_nparts)
2461 {
2462 int merged_index = outer_map->merged_indexes[i];
2463
2464 if (merged_index >= 0)
2465 {
2468 }
2469 }
2470 if (i < inner_nparts)
2471 {
2472 int merged_index = inner_map->merged_indexes[i];
2473
2474 if (merged_index >= 0)
2475 {
2478 }
2479 }
2480 }
2481
2482 /* Build the list pairs. */
2483 for (i = 0; i < nmerged; i++)
2484 {
2487
2488 /*
2489 * If both partitions are dummy, it means the merged partition that
2490 * had been assigned to the outer/inner partition was removed when
2491 * re-merging the outer/inner partition in
2492 * merge_matching_partitions(); ignore the merged partition.
2493 */
2494 if (outer_index == -1 && inner_index == -1)
2495 continue;
2496
2498 outer_rel->part_rels[outer_index] : NULL);
2500 inner_rel->part_rels[inner_index] : NULL);
2501 }
2502
2505}
2506
2507/*
2508 * build_merged_partition_bounds
2509 * Create a PartitionBoundInfo struct from merged partition bounds
2510 */
2511static PartitionBoundInfo
2513 List *merged_kinds, List *merged_indexes,
2514 int null_index, int default_index)
2515{
2517 int ndatums = list_length(merged_datums);
2518 int pos;
2519 ListCell *lc;
2520
2522 merged_bounds->strategy = strategy;
2523 merged_bounds->ndatums = ndatums;
2524
2525 merged_bounds->datums = palloc_array(Datum *, ndatums);
2526 pos = 0;
2527 foreach(lc, merged_datums)
2528 merged_bounds->datums[pos++] = (Datum *) lfirst(lc);
2529
2530 if (strategy == PARTITION_STRATEGY_RANGE)
2531 {
2532 Assert(list_length(merged_kinds) == ndatums);
2534 pos = 0;
2535 foreach(lc, merged_kinds)
2536 merged_bounds->kind[pos++] = (PartitionRangeDatumKind *) lfirst(lc);
2537
2538 /* There are ndatums+1 indexes in the case of range partitioning. */
2539 merged_indexes = lappend_int(merged_indexes, -1);
2540 ndatums++;
2541 }
2542 else
2543 {
2544 Assert(strategy == PARTITION_STRATEGY_LIST);
2546 merged_bounds->kind = NULL;
2547 }
2548
2549 /* interleaved_parts is always NULL for join relations. */
2550 merged_bounds->interleaved_parts = NULL;
2551
2552 Assert(list_length(merged_indexes) == ndatums);
2553 merged_bounds->nindexes = ndatums;
2554 merged_bounds->indexes = palloc_array(int, ndatums);
2555 pos = 0;
2556 foreach(lc, merged_indexes)
2557 merged_bounds->indexes[pos++] = lfirst_int(lc);
2558
2559 merged_bounds->null_index = null_index;
2560 merged_bounds->default_index = default_index;
2561
2562 return merged_bounds;
2563}
2564
2565/*
2566 * get_range_partition
2567 * Get the next non-dummy partition of a range-partitioned relation,
2568 * returning the index of that partition
2569 *
2570 * *lb and *ub are set to the lower and upper bounds of that partition
2571 * respectively, and *lb_pos is advanced to the next lower bound, if any.
2572 */
2573static int
2576 int *lb_pos,
2579{
2580 int part_index;
2581
2582 Assert(bi->strategy == PARTITION_STRATEGY_RANGE);
2583
2584 do
2585 {
2587 if (part_index == -1)
2588 return -1;
2589 } while (is_dummy_partition(rel, part_index));
2590
2591 return part_index;
2592}
2593
2594static int
2596 int *lb_pos,
2599{
2600 /* Return the index as -1 if we've exhausted all lower bounds. */
2601 if (*lb_pos >= bi->ndatums)
2602 return -1;
2603
2604 /* A lower bound should have at least one more bound after it. */
2605 Assert(*lb_pos + 1 < bi->ndatums);
2606
2607 /* Set the lower bound. */
2608 lb->index = bi->indexes[*lb_pos];
2609 lb->datums = bi->datums[*lb_pos];
2610 lb->kind = bi->kind[*lb_pos];
2611 lb->lower = true;
2612 /* Set the upper bound. */
2613 ub->index = bi->indexes[*lb_pos + 1];
2614 ub->datums = bi->datums[*lb_pos + 1];
2615 ub->kind = bi->kind[*lb_pos + 1];
2616 ub->lower = false;
2617
2618 /* The index assigned to an upper bound should be valid. */
2619 Assert(ub->index >= 0);
2620
2621 /*
2622 * Advance the position to the next lower bound. If there are no bounds
2623 * left beyond the upper bound, we have reached the last lower bound.
2624 */
2625 if (*lb_pos + 2 >= bi->ndatums)
2626 *lb_pos = bi->ndatums;
2627 else
2628 {
2629 /*
2630 * If the index assigned to the bound next to the upper bound isn't
2631 * valid, that is the next lower bound; else, the upper bound is also
2632 * the lower bound of the next range partition.
2633 */
2634 if (bi->indexes[*lb_pos + 2] < 0)
2635 *lb_pos = *lb_pos + 2;
2636 else
2637 *lb_pos = *lb_pos + 1;
2638 }
2639
2640 return ub->index;
2641}
2642
2643/*
2644 * compare_range_partitions
2645 * Compare the bounds of two range partitions, and return true if the
2646 * two partitions overlap, false otherwise
2647 *
2648 * *lb_cmpval is set to -1, 0, or 1 if the outer partition's lower bound is
2649 * lower than, equal to, or higher than the inner partition's lower bound
2650 * respectively. Likewise, *ub_cmpval is set to -1, 0, or 1 if the outer
2651 * partition's upper bound is lower than, equal to, or higher than the inner
2652 * partition's upper bound respectively.
2653 */
2654static bool
2661 int *lb_cmpval, int *ub_cmpval)
2662{
2663 /*
2664 * Check if the outer partition's upper bound is lower than the inner
2665 * partition's lower bound; if so the partitions aren't overlapping.
2666 */
2668 outer_ub, inner_lb) < 0)
2669 {
2670 *lb_cmpval = -1;
2671 *ub_cmpval = -1;
2672 return false;
2673 }
2674
2675 /*
2676 * Check if the outer partition's lower bound is higher than the inner
2677 * partition's upper bound; if so the partitions aren't overlapping.
2678 */
2680 outer_lb, inner_ub) > 0)
2681 {
2682 *lb_cmpval = 1;
2683 *ub_cmpval = 1;
2684 return false;
2685 }
2686
2687 /* All other cases indicate overlapping partitions. */
2692 return true;
2693}
2694
2695/*
2696 * get_merged_range_bounds
2697 * Given the bounds of range partitions to be joined, determine the bounds
2698 * of a merged partition produced from the range partitions
2699 *
2700 * *merged_lb and *merged_ub are set to the lower and upper bounds of the
2701 * merged partition.
2702 */
2703static void
2705 Oid *partcollations, JoinType jointype,
2710 int lb_cmpval, int ub_cmpval,
2713{
2718
2719 switch (jointype)
2720 {
2721 case JOIN_INNER:
2722 case JOIN_SEMI:
2723
2724 /*
2725 * An INNER/SEMI join will have the rows that fit both sides, so
2726 * the lower bound of the merged partition will be the higher of
2727 * the two lower bounds, and the upper bound of the merged
2728 * partition will be the lower of the two upper bounds.
2729 */
2730 *merged_lb = (lb_cmpval > 0) ? *outer_lb : *inner_lb;
2731 *merged_ub = (ub_cmpval < 0) ? *outer_ub : *inner_ub;
2732 break;
2733
2734 case JOIN_LEFT:
2735 case JOIN_ANTI:
2736
2737 /*
2738 * A LEFT/ANTI join will have all the rows from the outer side, so
2739 * the bounds of the merged partition will be the same as the
2740 * outer bounds.
2741 */
2742 *merged_lb = *outer_lb;
2743 *merged_ub = *outer_ub;
2744 break;
2745
2746 case JOIN_FULL:
2747
2748 /*
2749 * A FULL join will have all the rows from both sides, so the
2750 * lower bound of the merged partition will be the lower of the
2751 * two lower bounds, and the upper bound of the merged partition
2752 * will be the higher of the two upper bounds.
2753 */
2754 *merged_lb = (lb_cmpval < 0) ? *outer_lb : *inner_lb;
2755 *merged_ub = (ub_cmpval > 0) ? *outer_ub : *inner_ub;
2756 break;
2757
2758 default:
2759 elog(ERROR, "unrecognized join type: %d", (int) jointype);
2760 }
2761}
2762
2763/*
2764 * add_merged_range_bounds
2765 * Add the bounds of a merged partition to the lists of range bounds
2766 */
2767static void
2772 int merged_index,
2775 List **merged_indexes)
2776{
2777 int cmpval;
2778
2779 if (!*merged_datums)
2780 {
2781 /* First merged partition */
2783 Assert(!*merged_indexes);
2784 cmpval = 1;
2785 }
2786 else
2787 {
2789
2792 Assert(*merged_indexes);
2793
2794 /* Get the last upper bound. */
2795 prev_ub.index = llast_int(*merged_indexes);
2796 prev_ub.datums = (Datum *) llast(*merged_datums);
2798 prev_ub.lower = false;
2799
2800 /*
2801 * We pass lower1 = false to partition_rbound_cmp() to prevent it from
2802 * considering the last upper bound to be smaller than the lower bound
2803 * of the merged partition when the values of the two range bounds
2804 * compare equal.
2805 */
2807 merged_lb->datums, merged_lb->kind,
2808 false, &prev_ub);
2809 Assert(cmpval >= 0);
2810 }
2811
2812 /*
2813 * If the lower bound is higher than the last upper bound, add the lower
2814 * bound with the index as -1 indicating that that is a lower bound; else,
2815 * the last upper bound will be reused as the lower bound of the merged
2816 * partition, so skip this.
2817 */
2818 if (cmpval > 0)
2819 {
2822 *merged_indexes = lappend_int(*merged_indexes, -1);
2823 }
2824
2825 /* Add the upper bound and index of the merged partition. */
2828 *merged_indexes = lappend_int(*merged_indexes, merged_index);
2829}
2830
2831/*
2832 * partitions_are_ordered
2833 * Determine whether the partitions described by 'boundinfo' are ordered,
2834 * that is partitions appearing earlier in the PartitionDesc sequence
2835 * contain partition keys strictly less than those appearing later.
2836 * Also, if NULL values are possible, they must come in the last
2837 * partition defined in the PartitionDesc. 'live_parts' marks which
2838 * partitions we should include when checking the ordering. Partitions
2839 * that do not appear in 'live_parts' are ignored.
2840 *
2841 * If out of order, or there is insufficient info to know the order,
2842 * then we return false.
2843 */
2844bool
2846{
2847 Assert(boundinfo != NULL);
2848
2849 switch (boundinfo->strategy)
2850 {
2852
2853 /*
2854 * RANGE-type partitioning guarantees that the partitions can be
2855 * scanned in the order that they're defined in the PartitionDesc
2856 * to provide sequential, non-overlapping ranges of tuples.
2857 * However, if a DEFAULT partition exists and it's contained
2858 * within live_parts, then the partitions are not ordered.
2859 */
2860 if (!partition_bound_has_default(boundinfo) ||
2861 !bms_is_member(boundinfo->default_index, live_parts))
2862 return true;
2863 break;
2864
2866
2867 /*
2868 * LIST partitioned are ordered providing none of live_parts
2869 * overlap with the partitioned table's interleaved partitions.
2870 */
2871 if (!bms_overlap(live_parts, boundinfo->interleaved_parts))
2872 return true;
2873
2874 break;
2876 break;
2877 }
2878
2879 return false;
2880}
2881
2882/*
2883 * check_new_partition_bound
2884 *
2885 * Checks if the new partition's bound overlaps any of the existing partitions
2886 * of parent. Also performs additional checks as necessary per strategy.
2887 */
2888void
2891{
2893 PartitionDesc partdesc = RelationGetPartitionDesc(parent, false);
2894 PartitionBoundInfo boundinfo = partdesc->boundinfo;
2895 int with = -1;
2896 bool overlap = false;
2897 int overlap_location = -1;
2898
2899 if (spec->is_default)
2900 {
2901 /*
2902 * The default partition bound never conflicts with any other
2903 * partition's; if that's what we're attaching, the only possible
2904 * problem is that one already exists, so check for that and we're
2905 * done.
2906 */
2907 if (boundinfo == NULL || !partition_bound_has_default(boundinfo))
2908 return;
2909
2910 /* Default partition already exists, error out. */
2911 ereport(ERROR,
2913 errmsg("partition \"%s\" conflicts with existing default partition \"%s\"",
2914 relname, get_rel_name(partdesc->oids[boundinfo->default_index])),
2915 parser_errposition(pstate, spec->location)));
2916 }
2917
2918 switch (key->strategy)
2919 {
2921 {
2922 Assert(spec->strategy == PARTITION_STRATEGY_HASH);
2923 Assert(spec->remainder >= 0 && spec->remainder < spec->modulus);
2924
2925 if (partdesc->nparts > 0)
2926 {
2927 int greatest_modulus;
2928 int remainder;
2929 int offset;
2930
2931 /*
2932 * Check rule that every modulus must be a factor of the
2933 * next larger modulus. (For example, if you have a bunch
2934 * of partitions that all have modulus 5, you can add a
2935 * new partition with modulus 10 or a new partition with
2936 * modulus 15, but you cannot add both a partition with
2937 * modulus 10 and a partition with modulus 15, because 10
2938 * is not a factor of 15.) We need only check the next
2939 * smaller and next larger existing moduli, relying on
2940 * previous enforcement of this rule to be sure that the
2941 * rest are in line.
2942 */
2943
2944 /*
2945 * Get the greatest (modulus, remainder) pair contained in
2946 * boundinfo->datums that is less than or equal to the
2947 * (spec->modulus, spec->remainder) pair.
2948 */
2949 offset = partition_hash_bsearch(boundinfo,
2950 spec->modulus,
2951 spec->remainder);
2952 if (offset < 0)
2953 {
2954 int next_modulus;
2955
2956 /*
2957 * All existing moduli are greater or equal, so the
2958 * new one must be a factor of the smallest one, which
2959 * is first in the boundinfo.
2960 */
2961 next_modulus = DatumGetInt32(boundinfo->datums[0][0]);
2962 if (next_modulus % spec->modulus != 0)
2963 ereport(ERROR,
2965 errmsg("every hash partition modulus must be a factor of the next larger modulus"),
2966 errdetail("The new modulus %d is not a factor of %d, the modulus of existing partition \"%s\".",
2967 spec->modulus, next_modulus,
2968 get_rel_name(partdesc->oids[0]))));
2969 }
2970 else
2971 {
2972 int prev_modulus;
2973
2974 /*
2975 * We found the largest (modulus, remainder) pair less
2976 * than or equal to the new one. That modulus must be
2977 * a divisor of, or equal to, the new modulus.
2978 */
2979 prev_modulus = DatumGetInt32(boundinfo->datums[offset][0]);
2980
2981 if (spec->modulus % prev_modulus != 0)
2982 ereport(ERROR,
2984 errmsg("every hash partition modulus must be a factor of the next larger modulus"),
2985 errdetail("The new modulus %d is not divisible by %d, the modulus of existing partition \"%s\".",
2986 spec->modulus,
2988 get_rel_name(partdesc->oids[offset]))));
2989
2990 if (offset + 1 < boundinfo->ndatums)
2991 {
2992 int next_modulus;
2993
2994 /*
2995 * Look at the next higher (modulus, remainder)
2996 * pair. That could have the same modulus and a
2997 * larger remainder than the new pair, in which
2998 * case we're good. If it has a larger modulus,
2999 * the new modulus must divide that one.
3000 */
3001 next_modulus = DatumGetInt32(boundinfo->datums[offset + 1][0]);
3002
3003 if (next_modulus % spec->modulus != 0)
3004 ereport(ERROR,
3006 errmsg("every hash partition modulus must be a factor of the next larger modulus"),
3007 errdetail("The new modulus %d is not a factor of %d, the modulus of existing partition \"%s\".",
3008 spec->modulus, next_modulus,
3009 get_rel_name(partdesc->oids[offset + 1]))));
3010 }
3011 }
3012
3013 greatest_modulus = boundinfo->nindexes;
3014 remainder = spec->remainder;
3015
3016 /*
3017 * Normally, the lowest remainder that could conflict with
3018 * the new partition is equal to the remainder specified
3019 * for the new partition, but when the new partition has a
3020 * modulus higher than any used so far, we need to adjust.
3021 */
3024
3025 /* Check every potentially-conflicting remainder. */
3026 do
3027 {
3028 if (boundinfo->indexes[remainder] != -1)
3029 {
3030 overlap = true;
3031 overlap_location = spec->location;
3032 with = boundinfo->indexes[remainder];
3033 break;
3034 }
3035 remainder += spec->modulus;
3036 } while (remainder < greatest_modulus);
3037 }
3038
3039 break;
3040 }
3041
3043 {
3044 Assert(spec->strategy == PARTITION_STRATEGY_LIST);
3045
3046 if (partdesc->nparts > 0)
3047 {
3048 ListCell *cell;
3049
3050 Assert(boundinfo &&
3051 boundinfo->strategy == PARTITION_STRATEGY_LIST &&
3052 (boundinfo->ndatums > 0 ||
3053 partition_bound_accepts_nulls(boundinfo) ||
3054 partition_bound_has_default(boundinfo)));
3055
3056 foreach(cell, spec->listdatums)
3057 {
3058 Const *val = lfirst_node(Const, cell);
3059
3060 overlap_location = val->location;
3061 if (!val->constisnull)
3062 {
3063 int offset;
3064 bool equal;
3065
3066 offset = partition_list_bsearch(&key->partsupfunc[0],
3067 key->partcollation,
3068 boundinfo,
3069 val->constvalue,
3070 &equal);
3071 if (offset >= 0 && equal)
3072 {
3073 overlap = true;
3074 with = boundinfo->indexes[offset];
3075 break;
3076 }
3077 }
3078 else if (partition_bound_accepts_nulls(boundinfo))
3079 {
3080 overlap = true;
3081 with = boundinfo->null_index;
3082 break;
3083 }
3084 }
3085 }
3086
3087 break;
3088 }
3089
3091 {
3093 *upper;
3094 int cmpval;
3095
3096 Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
3097 lower = make_one_partition_rbound(key, -1, spec->lowerdatums, true);
3098 upper = make_one_partition_rbound(key, -1, spec->upperdatums, false);
3099
3100 /*
3101 * First check if the resulting range would be empty with
3102 * specified lower and upper bounds. partition_rbound_cmp
3103 * cannot return zero here, since the lower-bound flags are
3104 * different.
3105 */
3106 cmpval = partition_rbound_cmp(key->partnatts,
3107 key->partsupfunc,
3108 key->partcollation,
3109 lower->datums, lower->kind,
3110 true, upper);
3111 Assert(cmpval != 0);
3112 if (cmpval > 0)
3113 {
3114 /* Point to problematic key in the lower datums list. */
3115 PartitionRangeDatum *datum = list_nth(spec->lowerdatums,
3116 cmpval - 1);
3117
3118 ereport(ERROR,
3120 errmsg("empty range bound specified for partition \"%s\"",
3121 relname),
3122 errdetail("Specified lower bound %s is greater than or equal to upper bound %s.",
3123 get_range_partbound_string(spec->lowerdatums),
3124 get_range_partbound_string(spec->upperdatums)),
3125 parser_errposition(pstate, datum->location)));
3126 }
3127
3128 if (partdesc->nparts > 0)
3129 {
3130 int offset;
3131
3132 Assert(boundinfo &&
3133 boundinfo->strategy == PARTITION_STRATEGY_RANGE &&
3134 (boundinfo->ndatums > 0 ||
3135 partition_bound_has_default(boundinfo)));
3136
3137 /*
3138 * Test whether the new lower bound (which is treated
3139 * inclusively as part of the new partition) lies inside
3140 * an existing partition, or in a gap.
3141 *
3142 * If it's inside an existing partition, the bound at
3143 * offset + 1 will be the upper bound of that partition,
3144 * and its index will be >= 0.
3145 *
3146 * If it's in a gap, the bound at offset + 1 will be the
3147 * lower bound of the next partition, and its index will
3148 * be -1. This is also true if there is no next partition,
3149 * since the index array is initialised with an extra -1
3150 * at the end.
3151 */
3152 offset = partition_range_bsearch(key->partnatts,
3153 key->partsupfunc,
3154 key->partcollation,
3155 boundinfo, lower,
3156 &cmpval);
3157
3158 if (boundinfo->indexes[offset + 1] < 0)
3159 {
3160 /*
3161 * Check that the new partition will fit in the gap.
3162 * For it to fit, the new upper bound must be less
3163 * than or equal to the lower bound of the next
3164 * partition, if there is one.
3165 */
3166 if (offset + 1 < boundinfo->ndatums)
3167 {
3168 Datum *datums;
3170 bool is_lower;
3171
3172 datums = boundinfo->datums[offset + 1];
3173 kind = boundinfo->kind[offset + 1];
3174 is_lower = (boundinfo->indexes[offset + 1] == -1);
3175
3176 cmpval = partition_rbound_cmp(key->partnatts,
3177 key->partsupfunc,
3178 key->partcollation,
3179 datums, kind,
3180 is_lower, upper);
3181 if (cmpval < 0)
3182 {
3183 /*
3184 * Point to problematic key in the upper
3185 * datums list.
3186 */
3187 PartitionRangeDatum *datum =
3188 list_nth(spec->upperdatums, abs(cmpval) - 1);
3189
3190 /*
3191 * The new partition overlaps with the
3192 * existing partition between offset + 1 and
3193 * offset + 2.
3194 */
3195 overlap = true;
3196 overlap_location = datum->location;
3197 with = boundinfo->indexes[offset + 2];
3198 }
3199 }
3200 }
3201 else
3202 {
3203 /*
3204 * The new partition overlaps with the existing
3205 * partition between offset and offset + 1.
3206 */
3207 PartitionRangeDatum *datum;
3208
3209 /*
3210 * Point to problematic key in the lower datums list;
3211 * if we have equality, point to the first one.
3212 */
3213 datum = cmpval == 0 ? linitial(spec->lowerdatums) :
3214 list_nth(spec->lowerdatums, abs(cmpval) - 1);
3215 overlap = true;
3216 overlap_location = datum->location;
3217 with = boundinfo->indexes[offset + 1];
3218 }
3219 }
3220
3221 break;
3222 }
3223 }
3224
3225 if (overlap)
3226 {
3227 Assert(with >= 0);
3228 ereport(ERROR,
3230 errmsg("partition \"%s\" would overlap partition \"%s\"",
3231 relname, get_rel_name(partdesc->oids[with])),
3233 }
3234}
3235
3236/*
3237 * check_default_partition_contents
3238 *
3239 * This function checks if there exists a row in the default partition that
3240 * would properly belong to the new partition being added. If it finds one,
3241 * it throws an error.
3242 */
3243void
3246{
3249 List *all_parts;
3250 ListCell *lc;
3251
3253 ? get_qual_for_list(parent, new_spec)
3254 : get_qual_for_range(parent, new_spec, false);
3257
3258 /*
3259 * Map the Vars in the constraint expression from parent's attnos to
3260 * default_rel's.
3261 */
3264 parent);
3265
3266 /*
3267 * If the existing constraints on the default partition imply that it will
3268 * not contain any row that would belong to the new partition, we can
3269 * avoid scanning the default partition.
3270 */
3272 {
3274 (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3276 return;
3277 }
3278
3279 /*
3280 * Scan the default partition and its subpartitions, and check for rows
3281 * that do not satisfy the revised partition constraints.
3282 */
3283 if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3286 else
3288
3289 foreach(lc, all_parts)
3290 {
3293 Expr *partition_constraint;
3294 EState *estate;
3295 ExprState *partqualstate = NULL;
3296 Snapshot snapshot;
3297 ExprContext *econtext;
3298 TableScanDesc scan;
3300 TupleTableSlot *tupslot;
3301
3302 /* Lock already taken above. */
3304 {
3306
3307 /*
3308 * Map the Vars in the constraint expression from default_rel's
3309 * the sub-partition's.
3310 */
3311 partition_constraint = make_ands_explicit(def_part_constraints);
3312 partition_constraint = (Expr *)
3313 map_partition_varattnos((List *) partition_constraint, 1,
3315
3316 /*
3317 * If the partition constraints on default partition child imply
3318 * that it will not contain any row that would belong to the new
3319 * partition, we can avoid scanning the child table.
3320 */
3323 {
3325 (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
3327
3329 continue;
3330 }
3331 }
3332 else
3333 {
3335 partition_constraint = make_ands_explicit(def_part_constraints);
3336 }
3337
3338 /*
3339 * Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
3340 * scanned.
3341 */
3342 if (part_rel->rd_rel->relkind != RELKIND_RELATION)
3343 {
3344 if (part_rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
3347 errmsg("skipped scanning foreign table \"%s\" which is a partition of default partition \"%s\"",
3350
3353
3354 continue;
3355 }
3356
3357 estate = CreateExecutorState();
3358
3359 /* Build expression execution states for partition check quals */
3360 partqualstate = ExecPrepareExpr(partition_constraint, estate);
3361
3362 econtext = GetPerTupleExprContext(estate);
3363 snapshot = RegisterSnapshot(GetLatestSnapshot());
3364 tupslot = table_slot_create(part_rel, &estate->es_tupleTable);
3365 scan = table_beginscan(part_rel, snapshot, 0, NULL,
3366 SO_NONE);
3367
3368 /*
3369 * Switch to per-tuple memory context and reset it for each tuple
3370 * produced, so we don't leak memory.
3371 */
3373
3374 while (table_scan_getnextslot(scan, ForwardScanDirection, tupslot))
3375 {
3376 econtext->ecxt_scantuple = tupslot;
3377
3378 if (!ExecCheck(partqualstate, econtext))
3379 ereport(ERROR,
3381 errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
3384
3385 ResetExprContext(econtext);
3387 }
3388
3390 table_endscan(scan);
3391 UnregisterSnapshot(snapshot);
3393 FreeExecutorState(estate);
3394
3396 table_close(part_rel, NoLock); /* keep the lock until commit */
3397 }
3398}
3399
3400/*
3401 * get_hash_partition_greatest_modulus
3402 *
3403 * Returns the greatest modulus of the hash partition bound.
3404 * This is no longer used in the core code, but we keep it around
3405 * in case external modules are using it.
3406 */
3407int
3409{
3410 Assert(bound && bound->strategy == PARTITION_STRATEGY_HASH);
3411 return bound->nindexes;
3412}
3413
3414/*
3415 * make_one_partition_rbound
3416 *
3417 * Return a PartitionRangeBound given a list of PartitionRangeDatum elements
3418 * and a flag telling whether the bound is lower or not. Made into a function
3419 * because there are multiple sites that want to use this facility.
3420 */
3421static PartitionRangeBound *
3423{
3424 PartitionRangeBound *bound;
3425 ListCell *lc;
3426 int i;
3427
3428 Assert(datums != NIL);
3429
3431 bound->index = index;
3432 bound->datums = palloc0_array(Datum, key->partnatts);
3433 bound->kind = palloc0_array(PartitionRangeDatumKind, key->partnatts);
3434 bound->lower = lower;
3435
3436 i = 0;
3437 foreach(lc, datums)
3438 {
3440
3441 /* What's contained in this range datum? */
3442 bound->kind[i] = datum->kind;
3443
3444 if (datum->kind == PARTITION_RANGE_DATUM_VALUE)
3445 {
3446 Const *val = castNode(Const, datum->value);
3447
3448 if (val->constisnull)
3449 elog(ERROR, "invalid range bound datum");
3450 bound->datums[i] = val->constvalue;
3451 }
3452
3453 i++;
3454 }
3455
3456 return bound;
3457}
3458
3459/*
3460 * partition_rbound_cmp
3461 *
3462 * For two range bounds this decides whether the 1st one (specified by
3463 * datums1, kind1, and lower1) is <, =, or > the bound specified in *b2.
3464 *
3465 * 0 is returned if they are equal, otherwise a non-zero integer whose sign
3466 * indicates the ordering, and whose absolute value gives the 1-based
3467 * partition key number of the first mismatching column.
3468 *
3469 * partnatts, partsupfunc and partcollation give the number of attributes in the
3470 * bounds to be compared, comparison function to be used and the collations of
3471 * attributes, respectively.
3472 *
3473 * Note that if the values of the two range bounds compare equal, then we take
3474 * into account whether they are upper or lower bounds, and an upper bound is
3475 * considered to be smaller than a lower bound. This is important to the way
3476 * that RelationBuildPartitionDesc() builds the PartitionBoundInfoData
3477 * structure, which only stores the upper bound of a common boundary between
3478 * two contiguous partitions.
3479 */
3480static int32
3481partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc,
3482 Oid *partcollation,
3485{
3486 int32 colnum = 0;
3487 int32 cmpval = 0; /* placate compiler */
3488 int i;
3489 Datum *datums2 = b2->datums;
3491 bool lower2 = b2->lower;
3492
3493 for (i = 0; i < partnatts; i++)
3494 {
3495 /* Track column number in case we need it for result */
3496 colnum++;
3497
3498 /*
3499 * First, handle cases where the column is unbounded, which should not
3500 * invoke the comparison procedure, and should not consider any later
3501 * columns. Note that the PartitionRangeDatumKind enum elements
3502 * compare the same way as the values they represent.
3503 */
3504 if (kind1[i] < kind2[i])
3505 return -colnum;
3506 else if (kind1[i] > kind2[i])
3507 return colnum;
3508 else if (kind1[i] != PARTITION_RANGE_DATUM_VALUE)
3509 {
3510 /*
3511 * The column bounds are both MINVALUE or both MAXVALUE. No later
3512 * columns should be considered, but we still need to compare
3513 * whether they are upper or lower bounds.
3514 */
3515 break;
3516 }
3517
3518 cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
3519 partcollation[i],
3520 datums1[i],
3521 datums2[i]));
3522 if (cmpval != 0)
3523 break;
3524 }
3525
3526 /*
3527 * If the comparison is anything other than equal, we're done. If they
3528 * compare equal though, we still have to consider whether the boundaries
3529 * are inclusive or exclusive. Exclusive one is considered smaller of the
3530 * two.
3531 */
3532 if (cmpval == 0 && lower1 != lower2)
3533 cmpval = lower1 ? 1 : -1;
3534
3535 return cmpval == 0 ? 0 : (cmpval < 0 ? -colnum : colnum);
3536}
3537
3538/*
3539 * partition_rbound_datum_cmp
3540 *
3541 * Return whether range bound (specified in rb_datums and rb_kind)
3542 * is <, =, or > partition key of tuple (tuple_datums)
3543 *
3544 * n_tuple_datums, partsupfunc and partcollation give number of attributes in
3545 * the bounds to be compared, comparison function to be used and the collations
3546 * of attributes resp.
3547 */
3548int32
3549partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation,
3551 const Datum *tuple_datums, int n_tuple_datums)
3552{
3553 int i;
3554 int32 cmpval = -1;
3555
3556 for (i = 0; i < n_tuple_datums; i++)
3557 {
3559 return -1;
3561 return 1;
3562
3563 cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
3564 partcollation[i],
3565 rb_datums[i],
3566 tuple_datums[i]));
3567 if (cmpval != 0)
3568 break;
3569 }
3570
3571 return cmpval;
3572}
3573
3574/*
3575 * partition_hbound_cmp
3576 *
3577 * Compares modulus first, then remainder if modulus is equal.
3578 */
3579static int32
3581{
3582 if (modulus1 < modulus2)
3583 return -1;
3584 if (modulus1 > modulus2)
3585 return 1;
3587 return (remainder1 > remainder2) ? 1 : -1;
3588 return 0;
3589}
3590
3591/*
3592 * partition_list_bsearch
3593 * Returns the index of the greatest bound datum that is less than equal
3594 * to the given value or -1 if all of the bound datums are greater
3595 *
3596 * *is_equal is set to true if the bound datum at the returned index is equal
3597 * to the input value.
3598 */
3599int
3600partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation,
3601 PartitionBoundInfo boundinfo,
3602 Datum value, bool *is_equal)
3603{
3604 int lo,
3605 hi,
3606 mid;
3607
3608 lo = -1;
3609 hi = boundinfo->ndatums - 1;
3610 while (lo < hi)
3611 {
3612 int32 cmpval;
3613
3614 mid = (lo + hi + 1) / 2;
3615 cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
3616 partcollation[0],
3617 boundinfo->datums[mid][0],
3618 value));
3619 if (cmpval <= 0)
3620 {
3621 lo = mid;
3622 *is_equal = (cmpval == 0);
3623 if (*is_equal)
3624 break;
3625 }
3626 else
3627 hi = mid - 1;
3628 }
3629
3630 return lo;
3631}
3632
3633/*
3634 * partition_range_bsearch
3635 * Returns the index of the greatest range bound that is less than or
3636 * equal to the given range bound or -1 if all of the range bounds are
3637 * greater
3638 *
3639 * Upon return from this function, *cmpval is set to 0 if the bound at the
3640 * returned index matches the input range bound exactly, otherwise a
3641 * non-zero integer whose sign indicates the ordering, and whose absolute
3642 * value gives the 1-based partition key number of the first mismatching
3643 * column.
3644 */
3645static int
3646partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
3647 Oid *partcollation,
3648 PartitionBoundInfo boundinfo,
3650{
3651 int lo,
3652 hi,
3653 mid;
3654
3655 lo = -1;
3656 hi = boundinfo->ndatums - 1;
3657 while (lo < hi)
3658 {
3659 mid = (lo + hi + 1) / 2;
3660 *cmpval = partition_rbound_cmp(partnatts, partsupfunc,
3661 partcollation,
3662 boundinfo->datums[mid],
3663 boundinfo->kind[mid],
3664 (boundinfo->indexes[mid] == -1),
3665 probe);
3666 if (*cmpval <= 0)
3667 {
3668 lo = mid;
3669 if (*cmpval == 0)
3670 break;
3671 }
3672 else
3673 hi = mid - 1;
3674 }
3675
3676 return lo;
3677}
3678
3679/*
3680 * partition_range_datum_bsearch
3681 * Returns the index of the greatest range bound that is less than or
3682 * equal to the given tuple or -1 if all of the range bounds are greater
3683 *
3684 * *is_equal is set to true if the range bound at the returned index is equal
3685 * to the input tuple.
3686 */
3687int
3688partition_range_datum_bsearch(FmgrInfo *partsupfunc, Oid *partcollation,
3689 PartitionBoundInfo boundinfo,
3690 int nvalues, const Datum *values, bool *is_equal)
3691{
3692 int lo,
3693 hi,
3694 mid;
3695
3696 lo = -1;
3697 hi = boundinfo->ndatums - 1;
3698 while (lo < hi)
3699 {
3700 int32 cmpval;
3701
3702 mid = (lo + hi + 1) / 2;
3703 cmpval = partition_rbound_datum_cmp(partsupfunc,
3704 partcollation,
3705 boundinfo->datums[mid],
3706 boundinfo->kind[mid],
3707 values,
3708 nvalues);
3709 if (cmpval <= 0)
3710 {
3711 lo = mid;
3712 *is_equal = (cmpval == 0);
3713
3714 if (*is_equal)
3715 break;
3716 }
3717 else
3718 hi = mid - 1;
3719 }
3720
3721 return lo;
3722}
3723
3724/*
3725 * partition_hash_bsearch
3726 * Returns the index of the greatest (modulus, remainder) pair that is
3727 * less than or equal to the given (modulus, remainder) pair or -1 if
3728 * all of them are greater
3729 */
3730int
3732 int modulus, int remainder)
3733{
3734 int lo,
3735 hi,
3736 mid;
3737
3738 lo = -1;
3739 hi = boundinfo->ndatums - 1;
3740 while (lo < hi)
3741 {
3742 int32 cmpval,
3745
3746 mid = (lo + hi + 1) / 2;
3747 bound_modulus = DatumGetInt32(boundinfo->datums[mid][0]);
3748 bound_remainder = DatumGetInt32(boundinfo->datums[mid][1]);
3750 modulus, remainder);
3751 if (cmpval <= 0)
3752 {
3753 lo = mid;
3754
3755 if (cmpval == 0)
3756 break;
3757 }
3758 else
3759 hi = mid - 1;
3760 }
3761
3762 return lo;
3763}
3764
3765/*
3766 * qsort_partition_hbound_cmp
3767 *
3768 * Hash bounds are sorted by modulus, then by remainder.
3769 */
3770static int32
3771qsort_partition_hbound_cmp(const void *a, const void *b)
3772{
3773 const PartitionHashBound *h1 = (const PartitionHashBound *) a;
3774 const PartitionHashBound *h2 = (const PartitionHashBound *) b;
3775
3776 return partition_hbound_cmp(h1->modulus, h1->remainder,
3777 h2->modulus, h2->remainder);
3778}
3779
3780/*
3781 * qsort_partition_list_value_cmp
3782 *
3783 * Compare two list partition bound datums.
3784 */
3785static int32
3786qsort_partition_list_value_cmp(const void *a, const void *b, void *arg)
3787{
3788 Datum val1 = ((const PartitionListValue *) a)->value,
3789 val2 = ((const PartitionListValue *) b)->value;
3791
3792 return DatumGetInt32(FunctionCall2Coll(&key->partsupfunc[0],
3793 key->partcollation[0],
3794 val1, val2));
3795}
3796
3797/*
3798 * qsort_partition_rbound_cmp
3799 *
3800 * Used when sorting range bounds across all range partitions.
3801 */
3802static int32
3803qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
3804{
3808
3809 return compare_range_bounds(key->partnatts, key->partsupfunc,
3810 key->partcollation,
3811 b1, b2);
3812}
3813
3814/*
3815 * get_partition_operator
3816 *
3817 * Return oid of the operator of the given strategy for the given partition
3818 * key column. It is assumed that the partitioning key is of the same type as
3819 * the chosen partitioning opclass, or at least binary-compatible. In the
3820 * latter case, *need_relabel is set to true if the opclass is not of a
3821 * polymorphic type (indicating a RelabelType node needed on top), otherwise
3822 * false.
3823 */
3824static Oid
3826 bool *need_relabel)
3827{
3828 Oid operoid;
3829
3830 /*
3831 * Get the operator in the partitioning opfamily using the opclass'
3832 * declared input type as both left- and righttype.
3833 */
3834 operoid = get_opfamily_member(key->partopfamily[col],
3835 key->partopcintype[col],
3836 key->partopcintype[col],
3837 strategy);
3838 if (!OidIsValid(operoid))
3839 elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
3840 strategy, key->partopcintype[col], key->partopcintype[col],
3841 key->partopfamily[col]);
3842
3843 /*
3844 * If the partition key column is not of the same type as the operator
3845 * class and not polymorphic, tell caller to wrap the non-Const expression
3846 * in a RelabelType. This matches what parse_coerce.c does.
3847 */
3848 *need_relabel = (key->parttypid[col] != key->partopcintype[col] &&
3849 key->partopcintype[col] != RECORDOID &&
3850 !IsPolymorphicType(key->partopcintype[col]));
3851
3852 return operoid;
3853}
3854
3855/*
3856 * make_partition_op_expr
3857 * Returns an Expr for the given partition key column with arg1 and
3858 * arg2 as its leftop and rightop, respectively
3859 */
3860static Expr *
3862 uint16 strategy, Expr *arg1, Expr *arg2)
3863{
3864 Oid operoid;
3865 bool need_relabel = false;
3866 Expr *result = NULL;
3867
3868 /* Get the correct btree operator for this partitioning column */
3870
3871 /*
3872 * Chosen operator may be such that the non-Const operand needs to be
3873 * coerced, so apply the same; see the comment in
3874 * get_partition_operator().
3875 */
3876 if (!IsA(arg1, Const) &&
3877 (need_relabel ||
3878 key->partcollation[keynum] != key->parttypcoll[keynum]))
3880 key->partopcintype[keynum],
3881 -1,
3882 key->partcollation[keynum],
3884
3885 /* Generate the actual expression */
3886 switch (key->strategy)
3887 {
3889 {
3890 List *elems = (List *) arg2;
3891 int nelems = list_length(elems);
3892
3893 Assert(nelems >= 1);
3894 Assert(keynum == 0);
3895
3896 if (nelems > 1 &&
3897 !type_is_array(key->parttypid[keynum]))
3898 {
3901
3902 /* Construct an ArrayExpr for the right-hand inputs */
3904 arrexpr->array_typeid =
3905 get_array_type(key->parttypid[keynum]);
3906 arrexpr->array_collid = key->parttypcoll[keynum];
3907 arrexpr->element_typeid = key->parttypid[keynum];
3908 arrexpr->elements = elems;
3909 arrexpr->multidims = false;
3910 arrexpr->location = -1;
3911
3912 /* Build leftop = ANY (rightop) */
3914 saopexpr->opno = operoid;
3915 saopexpr->opfuncid = get_opcode(operoid);
3916 saopexpr->hashfuncid = InvalidOid;
3917 saopexpr->negfuncid = InvalidOid;
3918 saopexpr->useOr = true;
3919 saopexpr->inputcollid = key->partcollation[keynum];
3920 saopexpr->args = list_make2(arg1, arrexpr);
3921 saopexpr->location = -1;
3922
3923 result = (Expr *) saopexpr;
3924 }
3925 else
3926 {
3927 List *elemops = NIL;
3928 ListCell *lc;
3929
3930 foreach(lc, elems)
3931 {
3932 Expr *elem = lfirst(lc),
3933 *elemop;
3934
3936 BOOLOID,
3937 false,
3938 arg1, elem,
3939 InvalidOid,
3940 key->partcollation[keynum]);
3942 }
3943
3944 result = nelems > 1 ? makeBoolExpr(OR_EXPR, elemops, -1) : linitial(elemops);
3945 }
3946 break;
3947 }
3948
3951 BOOLOID,
3952 false,
3953 arg1, arg2,
3954 InvalidOid,
3955 key->partcollation[keynum]);
3956 break;
3957
3959 Assert(false);
3960 break;
3961 }
3962
3963 return result;
3964}
3965
3966/*
3967 * get_qual_for_hash
3968 *
3969 * Returns a CHECK constraint expression to use as a hash partition's
3970 * constraint, given the parent relation and partition bound structure.
3971 *
3972 * The partition constraint for a hash partition is always a call to the
3973 * built-in function satisfies_hash_partition().
3974 */
3975static List *
3977{
3979 FuncExpr *fexpr;
3983 List *args;
3985 int i;
3986
3987 /* Fixed arguments. */
3989 -1,
3990 InvalidOid,
3991 sizeof(Oid),
3993 false,
3994 true);
3995
3997 -1,
3998 InvalidOid,
3999 sizeof(int32),
4000 Int32GetDatum(spec->modulus),
4001 false,
4002 true);
4003
4005 -1,
4006 InvalidOid,
4007 sizeof(int32),
4008 Int32GetDatum(spec->remainder),
4009 false,
4010 true);
4011
4013 partexprs_item = list_head(key->partexprs);
4014
4015 /* Add an argument for each key column. */
4016 for (i = 0; i < key->partnatts; i++)
4017 {
4018 Node *keyCol;
4019
4020 /* Left operand */
4021 if (key->partattrs[i] != 0)
4022 {
4023 keyCol = (Node *) makeVar(1,
4024 key->partattrs[i],
4025 key->parttypid[i],
4026 key->parttypmod[i],
4027 key->parttypcoll[i],
4028 0);
4029 }
4030 else
4031 {
4033 partexprs_item = lnext(key->partexprs, partexprs_item);
4034 }
4035
4036 args = lappend(args, keyCol);
4037 }
4038
4040 BOOLOID,
4041 args,
4042 InvalidOid,
4043 InvalidOid,
4045
4046 return list_make1(fexpr);
4047}
4048
4049/*
4050 * get_qual_for_list
4051 *
4052 * Returns an implicit-AND list of expressions to use as a list partition's
4053 * constraint, given the parent relation and partition bound structure.
4054 *
4055 * The function returns NIL for a default partition when it's the only
4056 * partition since in that case there is no constraint.
4057 */
4058static List *
4060{
4062 List *result;
4063 Expr *keyCol;
4064 Expr *opexpr;
4066 ListCell *cell;
4067 List *elems = NIL;
4068 bool list_has_null = false;
4069
4070 /*
4071 * Only single-column list partitioning is supported, so we are worried
4072 * only about the partition key with index 0.
4073 */
4074 Assert(key->partnatts == 1);
4075
4076 /* Construct Var or expression representing the partition column */
4077 if (key->partattrs[0] != 0)
4078 keyCol = (Expr *) makeVar(1,
4079 key->partattrs[0],
4080 key->parttypid[0],
4081 key->parttypmod[0],
4082 key->parttypcoll[0],
4083 0);
4084 else
4085 keyCol = (Expr *) copyObject(linitial(key->partexprs));
4086
4087 /*
4088 * For default list partition, collect datums for all the partitions. The
4089 * default partition constraint should check that the partition key is
4090 * equal to none of those.
4091 */
4092 if (spec->is_default)
4093 {
4094 int i;
4095 int ndatums = 0;
4097 PartitionBoundInfo boundinfo = pdesc->boundinfo;
4098
4099 if (boundinfo)
4100 {
4101 ndatums = boundinfo->ndatums;
4102
4103 if (partition_bound_accepts_nulls(boundinfo))
4104 list_has_null = true;
4105 }
4106
4107 /*
4108 * If default is the only partition, there need not be any partition
4109 * constraint on it.
4110 */
4111 if (ndatums == 0 && !list_has_null)
4112 return NIL;
4113
4114 for (i = 0; i < ndatums; i++)
4115 {
4116 Const *val;
4117
4118 /*
4119 * Construct Const from known-not-null datum. We must be careful
4120 * to copy the value, because our result has to be able to outlive
4121 * the relcache entry we're copying from.
4122 */
4123 val = makeConst(key->parttypid[0],
4124 key->parttypmod[0],
4125 key->parttypcoll[0],
4126 key->parttyplen[0],
4127 datumCopy(*boundinfo->datums[i],
4128 key->parttypbyval[0],
4129 key->parttyplen[0]),
4130 false, /* isnull */
4131 key->parttypbyval[0]);
4132
4133 elems = lappend(elems, val);
4134 }
4135 }
4136 else
4137 {
4138 /*
4139 * Create list of Consts for the allowed values, excluding any nulls.
4140 */
4141 foreach(cell, spec->listdatums)
4142 {
4143 Const *val = lfirst_node(Const, cell);
4144
4145 if (val->constisnull)
4146 list_has_null = true;
4147 else
4148 elems = lappend(elems, copyObject(val));
4149 }
4150 }
4151
4152 if (elems)
4153 {
4154 /*
4155 * Generate the operator expression from the non-null partition
4156 * values.
4157 */
4159 keyCol, (Expr *) elems);
4160 }
4161 else
4162 {
4163 /*
4164 * If there are no partition values, we don't need an operator
4165 * expression.
4166 */
4167 opexpr = NULL;
4168 }
4169
4170 if (!list_has_null)
4171 {
4172 /*
4173 * Gin up a "col IS NOT NULL" test that will be ANDed with the main
4174 * expression. This might seem redundant, but the partition routing
4175 * machinery needs it.
4176 */
4178 nulltest->arg = keyCol;
4179 nulltest->nulltesttype = IS_NOT_NULL;
4180 nulltest->argisrow = false;
4181 nulltest->location = -1;
4182
4183 result = opexpr ? list_make2(nulltest, opexpr) : list_make1(nulltest);
4184 }
4185 else
4186 {
4187 /*
4188 * Gin up a "col IS NULL" test that will be OR'd with the main
4189 * expression.
4190 */
4192 nulltest->arg = keyCol;
4193 nulltest->nulltesttype = IS_NULL;
4194 nulltest->argisrow = false;
4195 nulltest->location = -1;
4196
4197 if (opexpr)
4198 {
4199 Expr *or;
4200
4201 or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
4202 result = list_make1(or);
4203 }
4204 else
4206 }
4207
4208 /*
4209 * Note that, in general, applying NOT to a constraint expression doesn't
4210 * necessarily invert the set of rows it accepts, because NOT (NULL) is
4211 * NULL. However, the partition constraints we construct here never
4212 * evaluate to NULL, so applying NOT works as intended.
4213 */
4214 if (spec->is_default)
4215 {
4218 }
4219
4220 return result;
4221}
4222
4223/*
4224 * get_qual_for_range
4225 *
4226 * Returns an implicit-AND list of expressions to use as a range partition's
4227 * constraint, given the parent relation and partition bound structure.
4228 *
4229 * For a multi-column range partition key, say (a, b, c), with (al, bl, cl)
4230 * as the lower bound tuple and (au, bu, cu) as the upper bound tuple, we
4231 * generate an expression tree of the following form:
4232 *
4233 * (a IS NOT NULL) and (b IS NOT NULL) and (c IS NOT NULL)
4234 * AND
4235 * (a > al OR (a = al AND b > bl) OR (a = al AND b = bl AND c >= cl))
4236 * AND
4237 * (a < au OR (a = au AND b < bu) OR (a = au AND b = bu AND c < cu))
4238 *
4239 * It is often the case that a prefix of lower and upper bound tuples contains
4240 * the same values, for example, (al = au), in which case, we will emit an
4241 * expression tree of the following form:
4242 *
4243 * (a IS NOT NULL) and (b IS NOT NULL) and (c IS NOT NULL)
4244 * AND
4245 * (a = al)
4246 * AND
4247 * (b > bl OR (b = bl AND c >= cl))
4248 * AND
4249 * (b < bu OR (b = bu AND c < cu))
4250 *
4251 * If a bound datum is either MINVALUE or MAXVALUE, these expressions are
4252 * simplified using the fact that any value is greater than MINVALUE and less
4253 * than MAXVALUE. So, for example, if cu = MAXVALUE, c < cu is automatically
4254 * true, and we need not emit any expression for it, and the last line becomes
4255 *
4256 * (b < bu) OR (b = bu), which is simplified to (b <= bu)
4257 *
4258 * In most common cases with only one partition column, say a, the following
4259 * expression tree will be generated: a IS NOT NULL AND a >= al AND a < au
4260 *
4261 * For default partition, it returns the negation of the constraints of all
4262 * the other partitions.
4263 *
4264 * External callers should pass for_default as false; we set it to true only
4265 * when recursing.
4266 */
4267static List *
4269 bool for_default)
4270{
4271 List *result = NIL;
4272 ListCell *cell1,
4273 *cell2,
4276 int i,
4277 j;
4278 PartitionRangeDatum *ldatum,
4279 *udatum;
4281 Expr *keyCol;
4283 *upper_val;
4286 int num_or_arms,
4292
4293 if (spec->is_default)
4294 {
4297 Oid *inhoids = pdesc->oids;
4298 int nparts = pdesc->nparts,
4299 k;
4300
4301 for (k = 0; k < nparts; k++)
4302 {
4303 Oid inhrelid = inhoids[k];
4304 HeapTuple tuple;
4305 Datum datum;
4307
4308 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(inhrelid));
4309 if (!HeapTupleIsValid(tuple))
4310 elog(ERROR, "cache lookup failed for relation %u", inhrelid);
4311
4312 datum = SysCacheGetAttrNotNull(RELOID, tuple,
4317 elog(ERROR, "expected PartitionBoundSpec");
4318
4319 if (!bspec->is_default)
4320 {
4321 List *part_qual;
4322
4323 part_qual = get_qual_for_range(parent, bspec, true);
4324
4325 /*
4326 * AND the constraints of the partition and add to
4327 * or_expr_args
4328 */
4331 : linitial(part_qual));
4332 }
4333 ReleaseSysCache(tuple);
4334 }
4335
4336 if (or_expr_args != NIL)
4337 {
4339
4340 /*
4341 * Combine the constraints obtained for non-default partitions
4342 * using OR. As requested, each of the OR's args doesn't include
4343 * the NOT NULL test for partition keys (which is to avoid its
4344 * useless repetition). Add the same now.
4345 */
4351 -1)
4353 -1);
4354
4355 /*
4356 * Finally, the default partition contains everything *NOT*
4357 * contained in the non-default partitions.
4358 */
4361 }
4362
4363 return result;
4364 }
4365
4366 /*
4367 * If it is the recursive call for default, we skip the get_range_nulltest
4368 * to avoid accumulating the NullTest on the same keys for each partition.
4369 */
4370 if (!for_default)
4372
4373 /*
4374 * Iterate over the key columns and check if the corresponding lower and
4375 * upper datums are equal using the btree equality operator for the
4376 * column's type. If equal, we emit single keyCol = common_value
4377 * expression. Starting from the first column for which the corresponding
4378 * lower and upper bound datums are not equal, we generate OR expressions
4379 * as shown in the function's header comment.
4380 */
4381 i = 0;
4382 partexprs_item = list_head(key->partexprs);
4383 partexprs_item_saved = partexprs_item; /* placate compiler */
4384 forboth(cell1, spec->lowerdatums, cell2, spec->upperdatums)
4385 {
4386 EState *estate;
4388 Expr *test_expr;
4391 bool isNull;
4392
4395
4396 /*
4397 * Since get_range_key_properties() modifies partexprs_item, and we
4398 * might need to start over from the previous expression in the later
4399 * part of this function, save away the current value.
4400 */
4402
4403 get_range_key_properties(key, i, ldatum, udatum,
4405 &keyCol,
4406 &lower_val, &upper_val);
4407
4408 /*
4409 * If either value is NULL, the corresponding partition bound is
4410 * either MINVALUE or MAXVALUE, and we treat them as unequal, because
4411 * even if they're the same, there is no common value to equate the
4412 * key column with.
4413 */
4414 if (!lower_val || !upper_val)
4415 break;
4416
4417 /* Create the test expression */
4418 estate = CreateExecutorState();
4421 (Expr *) lower_val,
4422 (Expr *) upper_val);
4426 GetPerTupleExprContext(estate),
4427 &isNull);
4429 FreeExecutorState(estate);
4430
4431 /* If not equal, go generate the OR expressions */
4433 break;
4434
4435 /*
4436 * The bounds for the last key column can't be equal, because such a
4437 * range partition would never be allowed to be defined (it would have
4438 * an empty range otherwise).
4439 */
4440 if (i == key->partnatts - 1)
4441 elog(ERROR, "invalid range bound specification");
4442
4443 /* Equal, so generate keyCol = lower_val expression */
4446 keyCol, (Expr *) lower_val));
4447
4448 i++;
4449 }
4450
4451 /* First pair of lower_val and upper_val that are not equal. */
4454
4455 /* OR will have as many arms as there are key columns left. */
4456 num_or_arms = key->partnatts - i;
4457 current_or_arm = 0;
4460 while (current_or_arm < num_or_arms)
4461 {
4464
4465 /* Restart scan of columns from the i'th one */
4466 j = i;
4468
4470 cell2, spec->upperdatums, upper_or_start_datum)
4471 {
4473 *udatum_next = NULL;
4474
4476 if (lnext(spec->lowerdatums, cell1))
4478 lfirst(lnext(spec->lowerdatums, cell1)));
4480 if (lnext(spec->upperdatums, cell2))
4482 lfirst(lnext(spec->upperdatums, cell2)));
4483 get_range_key_properties(key, j, ldatum, udatum,
4485 &keyCol,
4486 &lower_val, &upper_val);
4487
4489 {
4490 uint16 strategy;
4491
4492 /*
4493 * For the non-last columns of this arm, use the EQ operator.
4494 * For the last column of this arm, use GT, unless this is the
4495 * last column of the whole bound check, or the next bound
4496 * datum is MINVALUE, in which case use GE.
4497 */
4498 if (j - i < current_or_arm)
4499 strategy = BTEqualStrategyNumber;
4500 else if (j == key->partnatts - 1 ||
4501 (ldatum_next &&
4504 else
4505 strategy = BTGreaterStrategyNumber;
4506
4509 strategy,
4510 keyCol,
4511 (Expr *) lower_val));
4512 }
4513
4515 {
4516 uint16 strategy;
4517
4518 /*
4519 * For the non-last columns of this arm, use the EQ operator.
4520 * For the last column of this arm, use LT, unless the next
4521 * bound datum is MAXVALUE, in which case use LE.
4522 */
4523 if (j - i < current_or_arm)
4524 strategy = BTEqualStrategyNumber;
4525 else if (udatum_next &&
4527 strategy = BTLessEqualStrategyNumber;
4528 else
4529 strategy = BTLessStrategyNumber;
4530
4533 strategy,
4534 keyCol,
4535 (Expr *) upper_val));
4536 }
4537
4538 /*
4539 * Did we generate enough of OR's arguments? First arm considers
4540 * the first of the remaining columns, second arm considers first
4541 * two of the remaining columns, and so on.
4542 */
4543 ++j;
4544 if (j - i > current_or_arm)
4545 {
4546 /*
4547 * We must not emit any more arms if the new column that will
4548 * be considered is unbounded, or this one was.
4549 */
4550 if (!lower_val || !ldatum_next ||
4552 need_next_lower_arm = false;
4553 if (!upper_val || !udatum_next ||
4555 need_next_upper_arm = false;
4556 break;
4557 }
4558 }
4559
4560 if (lower_or_arm_args != NIL)
4565
4566 if (upper_or_arm_args != NIL)
4571
4572 /* If no work to do in the next iteration, break away. */
4574 break;
4575
4577 }
4578
4579 /*
4580 * Generate the OR expressions for each of lower and upper bounds (if
4581 * required), and append to the list of implicitly ANDed list of
4582 * expressions.
4583 */
4584 if (lower_or_arms != NIL)
4589 if (upper_or_arms != NIL)
4594
4595 /*
4596 * As noted above, for non-default, we return list with constant TRUE. If
4597 * the result is NIL during the recursive call for default, it implies
4598 * this is the only other partition which can hold every value of the key
4599 * except NULL. Hence we return the NullTest result skipped earlier.
4600 */
4601 if (result == NIL)
4603 ? get_range_nulltest(key)
4604 : list_make1(makeBoolConst(true, false));
4605
4606 return result;
4607}
4608
4609/*
4610 * get_range_key_properties
4611 * Returns range partition key information for a given column
4612 *
4613 * This is a subroutine for get_qual_for_range, and its API is pretty
4614 * specialized to that caller.
4615 *
4616 * Constructs an Expr for the key column (returned in *keyCol) and Consts
4617 * for the lower and upper range limits (returned in *lower_val and
4618 * *upper_val). For MINVALUE/MAXVALUE limits, NULL is returned instead of
4619 * a Const. All of these structures are freshly palloc'd.
4620 *
4621 * *partexprs_item points to the cell containing the next expression in
4622 * the key->partexprs list, or NULL. It may be advanced upon return.
4623 */
4624static void
4626 PartitionRangeDatum *ldatum,
4629 Expr **keyCol,
4631{
4632 /* Get partition key expression for this column */
4633 if (key->partattrs[keynum] != 0)
4634 {
4635 *keyCol = (Expr *) makeVar(1,
4636 key->partattrs[keynum],
4637 key->parttypid[keynum],
4638 key->parttypmod[keynum],
4639 key->parttypcoll[keynum],
4640 0);
4641 }
4642 else
4643 {
4644 if (*partexprs_item == NULL)
4645 elog(ERROR, "wrong number of partition key expressions");
4647 *partexprs_item = lnext(key->partexprs, *partexprs_item);
4648 }
4649
4650 /* Get appropriate Const nodes for the bounds */
4651 if (ldatum->kind == PARTITION_RANGE_DATUM_VALUE)
4652 *lower_val = castNode(Const, copyObject(ldatum->value));
4653 else
4654 *lower_val = NULL;
4655
4658 else
4659 *upper_val = NULL;
4660}
4661
4662/*
4663 * get_range_nulltest
4664 *
4665 * A non-default range partition table does not currently allow partition
4666 * keys to be null, so emit an IS NOT NULL expression for each key column.
4667 */
4668static List *
4670{
4671 List *result = NIL;
4674 int i;
4675
4676 partexprs_item = list_head(key->partexprs);
4677 for (i = 0; i < key->partnatts; i++)
4678 {
4679 Expr *keyCol;
4680
4681 if (key->partattrs[i] != 0)
4682 {
4683 keyCol = (Expr *) makeVar(1,
4684 key->partattrs[i],
4685 key->parttypid[i],
4686 key->parttypmod[i],
4687 key->parttypcoll[i],
4688 0);
4689 }
4690 else
4691 {
4692 if (partexprs_item == NULL)
4693 elog(ERROR, "wrong number of partition key expressions");
4695 partexprs_item = lnext(key->partexprs, partexprs_item);
4696 }
4697
4699 nulltest->arg = keyCol;
4700 nulltest->nulltesttype = IS_NOT_NULL;
4701 nulltest->argisrow = false;
4702 nulltest->location = -1;
4704 }
4705
4706 return result;
4707}
4708
4709/*
4710 * compute_partition_hash_value
4711 *
4712 * Compute the hash value for given partition key values.
4713 */
4714uint64
4715compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc, const Oid *partcollation,
4716 const Datum *values, const bool *isnull)
4717{
4718 int i;
4719 uint64 rowHash = 0;
4721
4722 for (i = 0; i < partnatts; i++)
4723 {
4724 /* Nulls are just ignored */
4725 if (!isnull[i])
4726 {
4727 Datum hash;
4728
4729 Assert(OidIsValid(partsupfunc[i].fn_oid));
4730
4731 /*
4732 * Compute hash for each datum value by calling respective
4733 * datatype-specific hash functions of each partition key
4734 * attribute.
4735 */
4736 hash = FunctionCall2Coll(&partsupfunc[i], partcollation[i],
4737 values[i], seed);
4738
4739 /* Form a single 64-bit hash value */
4741 }
4742 }
4743
4744 return rowHash;
4745}
4746
4747/*
4748 * satisfies_hash_partition
4749 *
4750 * This is an SQL-callable function for use in hash partition constraints.
4751 * The first three arguments are the parent table OID, modulus, and remainder.
4752 * The remaining arguments are the value of the partitioning columns (or
4753 * expressions); these are hashed and the results are combined into a single
4754 * hash value by calling hash_combine64.
4755 *
4756 * Returns true if remainder produced when this computed single hash value is
4757 * divided by the given modulus is equal to given remainder, otherwise false.
4758 * NB: it's important that this never return null, as the constraint machinery
4759 * would consider that to be a "pass".
4760 *
4761 * See get_qual_for_hash() for usage.
4762 */
4763Datum
4765{
4766 typedef struct ColumnsHashData
4767 {
4768 Oid relid;
4769 int nkeys;
4772 bool variadic_typbyval;
4773 char variadic_typalign;
4775 FmgrInfo partsupfunc[FLEXIBLE_ARRAY_MEMBER];
4777 Oid parentId;
4778 int modulus;
4779 int remainder;
4782 uint64 rowHash = 0;
4783
4784 /* Return false if the parent OID, modulus, or remainder is NULL. */
4785 if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
4786 PG_RETURN_BOOL(false);
4788 modulus = PG_GETARG_INT32(1);
4790
4791 /* Sanity check modulus and remainder. */
4792 if (modulus <= 0)
4793 ereport(ERROR,
4795 errmsg("modulus for hash partition must be an integer value greater than zero")));
4796 if (remainder < 0)
4797 ereport(ERROR,
4799 errmsg("remainder for hash partition must be an integer value greater than or equal to zero")));
4800 if (remainder >= modulus)
4801 ereport(ERROR,
4803 errmsg("remainder for hash partition must be less than modulus")));
4804
4805 /*
4806 * Cache hash function information.
4807 */
4808 my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
4809 if (my_extra == NULL || my_extra->relid != parentId)
4810 {
4811 Relation parent;
4812 PartitionKey key;
4813 int j;
4814
4815 /* Open parent relation and fetch partition key info */
4817 key = RelationGetPartitionKey(parent);
4818
4819 /* Reject parent table that is not hash-partitioned. */
4820 if (key == NULL || key->strategy != PARTITION_STRATEGY_HASH)
4821 ereport(ERROR,
4823 errmsg("\"%s\" is not a hash partitioned table",
4825
4826 if (!get_fn_expr_variadic(fcinfo->flinfo))
4827 {
4828 int nargs = PG_NARGS() - 3;
4829
4830 /* complain if wrong number of column values */
4831 if (key->partnatts != nargs)
4832 ereport(ERROR,
4834 errmsg("number of partitioning columns (%d) does not match number of partition keys provided (%d)",
4835 key->partnatts, nargs)));
4836
4837 /* allocate space for our cache */
4838 fcinfo->flinfo->fn_extra =
4839 MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
4840 offsetof(ColumnsHashData, partsupfunc) +
4841 sizeof(FmgrInfo) * nargs);
4842 my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
4843 my_extra->relid = parentId;
4844 my_extra->nkeys = key->partnatts;
4845 memcpy(my_extra->partcollid, key->partcollation,
4846 key->partnatts * sizeof(Oid));
4847
4848 /* check argument types and save fmgr_infos */
4849 for (j = 0; j < key->partnatts; ++j)
4850 {
4851 Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, j + 3);
4852
4853 if (argtype != key->parttypid[j] && !IsBinaryCoercible(argtype, key->parttypid[j]))
4854 ereport(ERROR,
4856 errmsg("column %d of the partition key has type %s, but supplied value is of type %s",
4857 j + 1, format_type_be(key->parttypid[j]), format_type_be(argtype))));
4858
4859 fmgr_info_copy(&my_extra->partsupfunc[j],
4860 &key->partsupfunc[j],
4861 fcinfo->flinfo->fn_mcxt);
4862 }
4863 }
4864 else
4865 {
4867
4868 /* allocate space for our cache -- just one FmgrInfo in this case */
4869 fcinfo->flinfo->fn_extra =
4870 MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
4871 offsetof(ColumnsHashData, partsupfunc) +
4872 sizeof(FmgrInfo));
4873 my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
4874 my_extra->relid = parentId;
4875 my_extra->nkeys = key->partnatts;
4876 my_extra->variadic_type = ARR_ELEMTYPE(variadic_array);
4877 get_typlenbyvalalign(my_extra->variadic_type,
4878 &my_extra->variadic_typlen,
4879 &my_extra->variadic_typbyval,
4880 &my_extra->variadic_typalign);
4881 my_extra->partcollid[0] = key->partcollation[0];
4882
4883 /* check argument types */
4884 for (j = 0; j < key->partnatts; ++j)
4885 if (key->parttypid[j] != my_extra->variadic_type)
4886 ereport(ERROR,
4888 errmsg("column %d of the partition key has type \"%s\", but supplied value is of type \"%s\"",
4889 j + 1,
4890 format_type_be(key->parttypid[j]),
4891 format_type_be(my_extra->variadic_type))));
4892
4893 fmgr_info_copy(&my_extra->partsupfunc[0],
4894 &key->partsupfunc[0],
4895 fcinfo->flinfo->fn_mcxt);
4896 }
4897
4898 /* Hold lock until commit */
4899 relation_close(parent, NoLock);
4900 }
4901
4902 if (!OidIsValid(my_extra->variadic_type))
4903 {
4904 int nkeys = my_extra->nkeys;
4905 int i;
4906
4907 /*
4908 * For a non-variadic call, neither the number of arguments nor their
4909 * types can change across calls, so avoid the expense of rechecking
4910 * here.
4911 */
4912
4913 for (i = 0; i < nkeys; i++)
4914 {
4915 Datum hash;
4916
4917 /* keys start from fourth argument of function. */
4918 int argno = i + 3;
4919
4920 if (PG_ARGISNULL(argno))
4921 continue;
4922
4923 hash = FunctionCall2Coll(&my_extra->partsupfunc[i],
4924 my_extra->partcollid[i],
4926 seed);
4927
4928 /* Form a single 64-bit hash value */
4930 }
4931 }
4932 else
4933 {
4935 int i;
4936 int nelems;
4937 Datum *datum;
4938 bool *isnull;
4939
4941 my_extra->variadic_type,
4942 my_extra->variadic_typlen,
4943 my_extra->variadic_typbyval,
4944 my_extra->variadic_typalign,
4945 &datum, &isnull, &nelems);
4946
4947 /* complain if wrong number of column values */
4948 if (nelems != my_extra->nkeys)
4949 ereport(ERROR,
4951 errmsg("number of partitioning columns (%d) does not match number of partition keys provided (%d)",
4952 my_extra->nkeys, nelems)));
4953
4954 for (i = 0; i < nelems; i++)
4955 {
4956 Datum hash;
4957
4958 if (isnull[i])
4959 continue;
4960
4961 hash = FunctionCall2Coll(&my_extra->partsupfunc[0],
4962 my_extra->partcollid[0],
4963 datum[i],
4964 seed);
4965
4966 /* Form a single 64-bit hash value */
4968 }
4969 }
4970
4971 PG_RETURN_BOOL(rowHash % modulus == remainder);
4972}
4973
4974/*
4975 * check_two_partitions_bounds_range
4976 *
4977 * (function for BY RANGE partitioning)
4978 *
4979 * This is a helper function for check_partitions_for_split() and
4980 * calculate_partition_bound_for_merge(). This function compares the upper
4981 * bound of first_bound and the lower bound of second_bound. These bounds
4982 * should be equal except when "defaultPart == true" (this means that one of
4983 * the split partitions is DEFAULT). In this case, the upper bound of
4984 * first_bound can be less than the lower bound of second_bound because
4985 * the space between these bounds will be included in the DEFAULT partition.
4986 *
4987 * parent: partitioned table
4988 * first_name: name of the first partition
4989 * first_bound: bound of the first partition
4990 * second_name: name of the second partition
4991 * second_bound: bound of the second partition
4992 * defaultPart: true if one of the new partitions is DEFAULT
4993 * is_merge: true indicates the operation is MERGE PARTITIONS;
4994 * false indicates the operation is SPLIT PARTITION.
4995 * pstate: pointer to ParseState struct for determining error position
4996 */
4997static void
5003 bool defaultPart,
5004 bool is_merge,
5005 ParseState *pstate)
5006{
5010 int cmpval;
5011
5012 Assert(key->strategy == PARTITION_STRATEGY_RANGE);
5013
5014 first_upper = make_one_partition_rbound(key, -1, first_bound->upperdatums, false);
5015 second_lower = make_one_partition_rbound(key, -1, second_bound->lowerdatums, true);
5016
5017 /*
5018 * lower1 argument of partition_rbound_cmp() is set to false for the
5019 * correct comparison result of the lower and upper bounds.
5020 */
5021 cmpval = partition_rbound_cmp(key->partnatts,
5022 key->partsupfunc,
5023 key->partcollation,
5024 second_lower->datums, second_lower->kind,
5025 false, first_upper);
5026 if ((!defaultPart && cmpval) || (defaultPart && cmpval < 0))
5027 {
5028 PartitionRangeDatum *datum = linitial(second_bound->lowerdatums);
5029
5030 if (is_merge)
5031 ereport(ERROR,
5033 errmsg("can not merge partition \"%s\" together with partition \"%s\"",
5034 second_name->relname, first_name->relname),
5035 errdetail("lower bound of partition \"%s\" is not equal to the upper bound of partition \"%s\"",
5036 second_name->relname, first_name->relname),
5037 errhint("ALTER TABLE ... MERGE PARTITIONS requires the partition bounds to be adjacent."),
5038 parser_errposition(pstate, datum->location));
5039 else
5040 ereport(ERROR,
5042 errmsg("can not split to partition \"%s\" together with partition \"%s\"",
5043 second_name->relname, first_name->relname),
5044 errdetail("lower bound of partition \"%s\" is not equal to the upper bound of partition \"%s\"",
5045 second_name->relname, first_name->relname),
5046 errhint("ALTER TABLE ... SPLIT PARTITION requires the partition bounds to be adjacent."),
5047 parser_errposition(pstate, datum->location));
5048 }
5049}
5050
5051/*
5052 * get_partition_bound_spec
5053 *
5054 * Returns the PartitionBoundSpec for the partition with the given OID partOid.
5055 */
5056static PartitionBoundSpec *
5058{
5059 HeapTuple tuple;
5060 Datum datum;
5061 bool isnull;
5063
5064 /* Try fetching the tuple from the catcache, for speed. */
5065 tuple = SearchSysCache1(RELOID, partOid);
5066 if (!HeapTupleIsValid(tuple))
5067 elog(ERROR, "cache lookup failed for relation %u", partOid);
5068
5069 datum = SysCacheGetAttr(RELOID, tuple,
5071 &isnull);
5072 if (isnull)
5073 elog(ERROR, "partition bound for relation %u is null",
5074 partOid);
5075
5077
5079 elog(ERROR, "expected PartitionBoundSpec for relation %u",
5080 partOid);
5081
5082 ReleaseSysCache(tuple);
5083 return boundspec;
5084}
5085
5086/*
5087 * calculate_partition_bound_for_merge
5088 *
5089 * Calculates the bound of the merged partition "spec" by using the bounds of
5090 * the partitions to be merged.
5091 *
5092 * parent: partitioned table
5093 * partNames: names of partitions to be merged
5094 * partOids: Oids of partitions to be merged
5095 * spec (out): bounds specification of the merged partition
5096 * pstate: pointer to ParseState struct to determine error position
5097 */
5098void
5100 List *partNames,
5101 List *partOids,
5103 ParseState *pstate)
5104{
5106 PartitionBoundSpec *bound;
5107
5108 Assert(!spec->is_default);
5109
5110 switch (key->strategy)
5111 {
5113 {
5114 int i;
5116 int nparts = list_length(partOids);
5117 List *bounds = NIL;
5118
5120
5121 /*
5122 * Create an array of lower bounds and a list of
5123 * PartitionBoundSpec.
5124 */
5125 foreach_oid(partoid, partOids)
5126 {
5127 bound = get_partition_bound_spec(partoid);
5128 i = foreach_current_index(partoid);
5129
5130 lower_bounds[i] = make_one_partition_rbound(key, i, bound->lowerdatums, true);
5131 bounds = lappend(bounds, bound);
5132 }
5133
5134 /* Sort the array of lower bounds. */
5135 qsort_arg(lower_bounds, nparts, sizeof(PartitionRangeBound *),
5137
5138 /* Ranges of partitions should be adjacent. */
5139 for (i = 1; i < nparts; i++)
5140 {
5141 int index = lower_bounds[i]->index;
5142 int prev_index = lower_bounds[i - 1]->index;
5143
5149 false,
5150 true,
5151 pstate);
5152 }
5153
5154 /*
5155 * The lower bound of the first partition is the lower bound
5156 * of the merged partition.
5157 */
5158 spec->lowerdatums =
5159 ((PartitionBoundSpec *) list_nth(bounds, lower_bounds[0]->index))->lowerdatums;
5160
5161 /*
5162 * The upper bound of the last partition is the upper bound of
5163 * the merged partition.
5164 */
5165 spec->upperdatums =
5166 ((PartitionBoundSpec *) list_nth(bounds, lower_bounds[nparts - 1]->index))->upperdatums;
5167
5170 break;
5171 }
5172
5174 {
5175 /* Consolidate bounds for all partitions in the list. */
5176 foreach_oid(partoid, partOids)
5177 {
5178 bound = get_partition_bound_spec(partoid);
5179 spec->listdatums = list_concat(spec->listdatums, bound->listdatums);
5180 }
5181 break;
5182 }
5183
5184 default:
5185 elog(ERROR, "unexpected partition strategy: %d",
5186 (int) key->strategy);
5187 }
5188}
5189
5190/*
5191 * partitions_listdatum_intersection
5192 *
5193 * (function for BY LIST partitioning)
5194 *
5195 * Function compares lists of values for different partitions.
5196 * Return a list that contains *one* cell that is present in both list1 and
5197 * list2. The returned list is freshly allocated via palloc(), but the
5198 * cells themselves point to the same objects as the cells of the
5199 * input lists.
5200 *
5201 * Currently, there is no need to collect all common partition datums from the
5202 * two lists.
5203 */
5204static List *
5206 const List *list1, const List *list2)
5207{
5208 List *result = NIL;
5209
5210 if (list1 == NIL || list2 == NIL)
5211 return result;
5212
5214 {
5215 bool isnull1 = val1->constisnull;
5216
5218 {
5219 if (val2->constisnull)
5220 {
5221 if (isnull1)
5222 {
5224 return result;
5225 }
5226 continue;
5227 }
5228 else if (isnull1)
5229 continue;
5230
5231 /* Compare two datum values. */
5232 if (DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
5233 partcollation[0],
5234 val1->constvalue,
5235 val2->constvalue)) == 0)
5236 {
5238 return result;
5239 }
5240 }
5241 }
5242
5243 return result;
5244}
5245
5246/*
5247 * check_partitions_not_overlap_list
5248 *
5249 * (function for BY LIST partitioning)
5250 *
5251 * This is a helper function for check_partitions_for_split().
5252 * Checks that the values of the new partitions do not overlap.
5253 *
5254 * parent: partitioned table
5255 * parts: array of SinglePartitionSpec structs with info about split partitions
5256 * nparts: size of array "parts"
5257 */
5258static void
5261 int nparts,
5262 ParseState *pstate)
5263{
5265 int i,
5266 j;
5268 *sps2;
5269 List *overlap;
5270
5271 Assert(key->strategy == PARTITION_STRATEGY_LIST);
5272
5273 for (i = 0; i < nparts; i++)
5274 {
5275 sps1 = parts[i];
5276
5277 for (j = i + 1; j < nparts; j++)
5278 {
5279 sps2 = parts[j];
5280
5281 overlap = partitions_listdatum_intersection(&key->partsupfunc[0],
5282 key->partcollation,
5283 sps1->bound->listdatums,
5284 sps2->bound->listdatums);
5285 if (list_length(overlap) > 0)
5286 {
5287 Const *val = (Const *) linitial_node(Const, overlap);
5288
5289 ereport(ERROR,
5291 errmsg("new partition \"%s\" would overlap with another new partition \"%s\"",
5292 sps1->name->relname, sps2->name->relname),
5293 parser_errposition(pstate, exprLocation((Node *) val)));
5294 }
5295 }
5296 }
5297}
5298
5299/*
5300 * check_partition_bounds_for_split_range
5301 *
5302 * (function for BY RANGE partitioning)
5303 *
5304 * Checks that bounds of new partition "spec" are inside bounds of split
5305 * partition (with Oid splitPartOid). If first=true (this means that "spec" is
5306 * the first of the new partitions), then the lower bound of "spec" should be
5307 * equal (or greater than or equal in case defaultPart=true) to the lower
5308 * bound of the split partition. If last=true (this means that "spec" is the
5309 * last of the new partitions), then the upper bound of "spec" should be
5310 * equal (or less than or equal in case defaultPart=true) to the upper bound
5311 * of the split partition.
5312 *
5313 * parent: partitioned table
5314 * relname: name of the new partition
5315 * spec: bounds specification of the new partition
5316 * splitPartOid: split partition Oid
5317 * first: true iff the new partition "spec" is the first of the
5318 * new partitions
5319 * last: true iff the new partition "spec" is the last of the
5320 * new partitions
5321 * defaultPart: true iff new partitions contain the DEFAULT partition
5322 * pstate: pointer to ParseState struct to determine error position
5323 */
5324static void
5326 char *relname,
5329 bool first,
5330 bool last,
5331 bool defaultPart,
5332 ParseState *pstate)
5333{
5336 *upper;
5337 int cmpval;
5338
5339 Assert(key->strategy == PARTITION_STRATEGY_RANGE);
5340 Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
5341
5342 lower = make_one_partition_rbound(key, -1, spec->lowerdatums, true);
5343 upper = make_one_partition_rbound(key, -1, spec->upperdatums, false);
5344
5345 /*
5346 * First, check if the resulting range would be empty with the specified
5347 * lower and upper bounds. partition_rbound_cmp cannot return zero here,
5348 * since the lower-bound flags are different.
5349 */
5350 cmpval = partition_rbound_cmp(key->partnatts,
5351 key->partsupfunc,
5352 key->partcollation,
5353 lower->datums, lower->kind,
5354 true, upper);
5355 Assert(cmpval != 0);
5356 if (cmpval > 0)
5357 {
5358 /* Point to the problematic key in the lower datums list. */
5359 PartitionRangeDatum *datum = list_nth(spec->lowerdatums, cmpval - 1);
5360
5361 ereport(ERROR,
5363 errmsg("empty range bound specified for partition \"%s\"",
5364 relname),
5365 errdetail("Specified lower bound %s is greater than or equal to upper bound %s.",
5366 get_range_partbound_string(spec->lowerdatums),
5367 get_range_partbound_string(spec->upperdatums)),
5368 parser_errposition(pstate, exprLocation((Node *) datum)));
5369 }
5370
5371 /*
5372 * Need to check first and last partitions (from the set of new
5373 * partitions)
5374 */
5375 if (first || last)
5376 {
5378 PartitionRangeDatum *datum;
5379
5380 if (first)
5381 {
5383
5384 split_lower = make_one_partition_rbound(key, -1, split_spec->lowerdatums, true);
5385
5386 cmpval = partition_rbound_cmp(key->partnatts,
5387 key->partsupfunc,
5388 key->partcollation,
5389 lower->datums, lower->kind,
5390 true, split_lower);
5391 if (cmpval != 0)
5392 datum = list_nth(spec->lowerdatums, abs(cmpval) - 1);
5393
5394 /*
5395 * The lower bound of "spec" must equal the lower bound of the
5396 * split partition. However, if one of the new partitions is
5397 * DEFAULT, then it is ok for the new partition's lower bound to
5398 * be greater than that of the split partition.
5399 */
5400 if (!defaultPart)
5401 {
5402 if (cmpval != 0)
5403 ereport(ERROR,
5405 errmsg("lower bound of partition \"%s\" is not equal to lower bound of split partition \"%s\"",
5406 relname,
5408 errhint("%s require combined bounds of new partitions must exactly match the bound of the split partition",
5409 "ALTER TABLE ... SPLIT PARTITION"),
5410 parser_errposition(pstate, exprLocation((Node *) datum)));
5411 }
5412 else if (cmpval < 0)
5413 ereport(ERROR,
5415 errmsg("lower bound of partition \"%s\" is less than lower bound of split partition \"%s\"",
5416 relname,
5418 errhint("%s require combined bounds of new partitions must exactly match the bound of the split partition",
5419 "ALTER TABLE ... SPLIT PARTITION"),
5420 parser_errposition(pstate, exprLocation((Node *) datum)));
5421 }
5422 else
5423 {
5425
5426 split_upper = make_one_partition_rbound(key, -1, split_spec->upperdatums, false);
5427
5428 cmpval = partition_rbound_cmp(key->partnatts,
5429 key->partsupfunc,
5430 key->partcollation,
5431 upper->datums, upper->kind,
5432 false, split_upper);
5433 if (cmpval != 0)
5434 datum = list_nth(spec->upperdatums, abs(cmpval) - 1);
5435
5436 /*
5437 * The upper bound of "spec" must equal the upper bound of the
5438 * split partition. However, if one of the new partitions is
5439 * DEFAULT, then it is ok for the new partition's upper bound to
5440 * be less than that of the split partition.
5441 */
5442 if (!defaultPart)
5443 {
5444 if (cmpval != 0)
5445 ereport(ERROR,
5447 errmsg("upper bound of partition \"%s\" is not equal to upper bound of split partition \"%s\"",
5448 relname,
5450 errhint("%s require combined bounds of new partitions must exactly match the bound of the split partition",
5451 "ALTER TABLE ... SPLIT PARTITION"),
5452 parser_errposition(pstate, exprLocation((Node *) datum)));
5453 }
5454 else if (cmpval > 0)
5455 ereport(ERROR,
5457 errmsg("upper bound of partition \"%s\" is greater than upper bound of split partition \"%s\"",
5458 relname,
5460 errhint("%s require combined bounds of new partitions must exactly match the bound of the split partition",
5461 "ALTER TABLE ... SPLIT PARTITION"),
5462 parser_errposition(pstate, exprLocation((Node *) datum)));
5463 }
5464 }
5465}
5466
5467/*
5468 * check_partition_bounds_for_split_list
5469 *
5470 * (function for BY LIST partitioning)
5471 *
5472 * Checks that the bounds of the new partition are inside the bounds of the
5473 * split partition (with Oid splitPartOid).
5474 *
5475 * parent: partitioned table
5476 * relname: name of the new partition
5477 * spec: bounds specification of the new partition
5478 * splitPartOid: split partition Oid
5479 * pstate: pointer to ParseState struct to determine error position
5480 */
5481static void
5485 ParseState *pstate)
5486{
5488 PartitionDesc partdesc = RelationGetPartitionDesc(parent, false);
5489 PartitionBoundInfo boundinfo = partdesc->boundinfo;
5490 int with = -1;
5491 bool overlap = false;
5492 int overlap_location = -1;
5493
5494 Assert(key->strategy == PARTITION_STRATEGY_LIST);
5495 Assert(spec->strategy == PARTITION_STRATEGY_LIST);
5496 Assert(boundinfo && boundinfo->strategy == PARTITION_STRATEGY_LIST);
5497
5498 /*
5499 * Search each value of the new partition "spec" in the existing
5500 * partitions. All of them should be in the split partition (with Oid
5501 * splitPartOid).
5502 */
5503 foreach_node(Const, val, spec->listdatums)
5504 {
5506 if (!val->constisnull)
5507 {
5508 int offset;
5509 bool equal;
5510
5511 offset = partition_list_bsearch(&key->partsupfunc[0],
5512 key->partcollation,
5513 boundinfo,
5514 val->constvalue,
5515 &equal);
5516 if (offset >= 0 && equal)
5517 {
5518 with = boundinfo->indexes[offset];
5519 if (partdesc->oids[with] != splitPartOid)
5520 {
5521 overlap = true;
5522 break;
5523 }
5524 }
5525 else
5526 ereport(ERROR,
5528 errmsg("new partition \"%s\" cannot have this value because split partition \"%s\" does not have",
5529 relname,
5532 }
5533 else if (partition_bound_accepts_nulls(boundinfo))
5534 {
5535 with = boundinfo->null_index;
5536 if (partdesc->oids[with] != splitPartOid)
5537 {
5538 overlap = true;
5539 break;
5540 }
5541 }
5542 else
5543 ereport(ERROR,
5545 errmsg("new partition \"%s\" cannot have NULL value because split partition \"%s\" does not have",
5546 relname,
5549 }
5550
5551 if (overlap)
5552 {
5553 Assert(with >= 0);
5554 ereport(ERROR,
5556 errmsg("new partition \"%s\" would overlap with another (not split) partition \"%s\"",
5557 relname, get_rel_name(partdesc->oids[with])),
5559 }
5560}
5561
5562/*
5563 * find_value_in_new_partitions_list
5564 *
5565 * (function for BY LIST partitioning)
5566 *
5567 * Function returns true iff any of the new partitions contains the value
5568 * "value".
5569 *
5570 * partsupfunc: information about the comparison function associated with
5571 * the partition key
5572 * partcollation: partitioning collation
5573 * parts: pointer to an array with new partition descriptions
5574 * nparts: number of new partitions
5575 * value: the value that we are looking for
5576 * isnull: true if the value that we are looking for is NULL
5577 */
5578static bool
5580 Oid *partcollation,
5582 int nparts,
5583 Datum value,
5584 bool isnull)
5585{
5586 for (int i = 0; i < nparts; i++)
5587 {
5589
5590 foreach_node(Const, val, sps->bound->listdatums)
5591 {
5592 if (isnull && val->constisnull)
5593 return true;
5594
5595 if (!isnull && !val->constisnull)
5596 {
5597 if (DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
5598 partcollation[0],
5599 val->constvalue,
5600 value)) == 0)
5601 return true;
5602 }
5603 }
5604 }
5605 return false;
5606}
5607
5608/*
5609 * check_parent_values_in_new_partitions
5610 *
5611 * (function for BY LIST partitioning)
5612 *
5613 * Checks that all values of split partition (with Oid partOid) are contained
5614 * in new partitions.
5615 *
5616 * parent: partitioned table
5617 * partOid: split partition Oid
5618 * parts: pointer to an array with new partition descriptions
5619 * nparts: number of new partitions
5620 * pstate: pointer to ParseState struct to determine error position
5621 */
5622static void
5624 Oid partOid,
5626 int nparts,
5627 ParseState *pstate)
5628{
5630 PartitionDesc partdesc = RelationGetPartitionDesc(parent, false);
5631 PartitionBoundInfo boundinfo = partdesc->boundinfo;
5632 int i;
5633 bool found = true;
5634 Datum datum = PointerGetDatum(NULL);
5635
5636 Assert(key->strategy == PARTITION_STRATEGY_LIST);
5637
5638 /*
5639 * Special processing for NULL value. Search for a NULL value if the split
5640 * partition (partOid) contains it.
5641 */
5642 if (partition_bound_accepts_nulls(boundinfo) &&
5643 partdesc->oids[boundinfo->null_index] == partOid)
5644 {
5645 if (!find_value_in_new_partitions_list(&key->partsupfunc[0],
5646 key->partcollation, parts, nparts, datum, true))
5647 found = false;
5648 }
5649
5650 if (!found)
5651 ereport(ERROR,
5653 errmsg("new partitions combined partition bounds do not contain value (%s) but split partition \"%s\" does",
5654 "NULL",
5656 errhint("%s require combined bounds of new partitions must exactly match the bound of the split partition",
5657 "ALTER TABLE ... SPLIT PARTITION"));
5658
5659 /*
5660 * Search all values of split partition with partOid in the PartitionDesc
5661 * of partitioned table.
5662 */
5663 for (i = 0; i < boundinfo->ndatums; i++)
5664 {
5665 if (partdesc->oids[boundinfo->indexes[i]] == partOid)
5666 {
5667 /* We found the value that the split partition contains. */
5668 datum = boundinfo->datums[i][0];
5669 if (!find_value_in_new_partitions_list(&key->partsupfunc[0],
5670 key->partcollation, parts, nparts, datum, false))
5671 {
5672 found = false;
5673 break;
5674 }
5675 }
5676 }
5677
5678 if (!found)
5679 {
5681
5682 /*
5683 * Make a Const for getting the string representation of the missing
5684 * value.
5685 */
5686 notFoundVal = makeConst(key->parttypid[0],
5687 key->parttypmod[0],
5688 key->parttypcoll[0],
5689 key->parttyplen[0],
5690 datum,
5691 false, /* isnull */
5692 key->parttypbyval[0]);
5693
5694 ereport(ERROR,
5696 errmsg("new partitions combined partition bounds do not contain value (%s) but split partition \"%s\" does",
5697 deparse_expression((Node *) notFoundVal, NIL, false, false),
5699 errhint("%s require combined bounds of new partitions must exactly match the bound of the split partition",
5700 "ALTER TABLE ... SPLIT PARTITION"));
5701 }
5702}
5703
5704/*
5705 * check_partitions_for_split
5706 *
5707 * Checks new partitions for the SPLIT PARTITION command:
5708 * 1. Bounds of new partitions should not overlap with new and existing
5709 * partitions.
5710 * 2. In the case when new or existing partitions contain the DEFAULT
5711 * partition, new partitions can have any bounds inside the split partition
5712 * bound (can be spaces between partition bounds).
5713 * 3. In case new partitions don't contain the DEFAULT partition and the
5714 * partitioned table does not have the DEFAULT partition, the following
5715 * should be true: the sum of the bounds of new partitions should be equal
5716 & to the bound of the split partition.
5717 *
5718 * parent: partitioned table
5719 * splitPartOid: split partition Oid
5720 * partlist: list of new partitions after partition split
5721 * pstate: pointer to ParseState struct for determine error position
5722 */
5723void
5726 List *partlist,
5727 ParseState *pstate)
5728{
5729 PartitionKey key;
5730 char strategy;
5732 bool isSplitPartDefault;
5733 bool createDefaultPart = false;
5734 int default_index = -1;
5735 int i;
5738
5739 /*
5740 * nparts counts the number of split partitions, but it exclude the
5741 * default partition.
5742 */
5743 int nparts = 0;
5744
5745 key = RelationGetPartitionKey(parent);
5746 strategy = get_partition_strategy(key);
5747
5750
5751 Assert(strategy == PARTITION_STRATEGY_RANGE ||
5752 strategy == PARTITION_STRATEGY_LIST);
5753
5754 /*
5755 * Make an array new_parts with new partitions except the DEFAULT
5756 * partition.
5757 */
5759
5760 /* isSplitPartDefault flag: is split partition a DEFAULT partition? */
5762
5764 {
5765 if (sps->bound->is_default)
5766 default_index = foreach_current_index(sps);
5767 else
5768 new_parts[nparts++] = sps;
5769 }
5770
5771 /* An indicator that the DEFAULT partition will be created. */
5772 if (default_index != -1)
5773 {
5774 createDefaultPart = true;
5775 Assert(nparts == list_length(partlist) - 1);
5776 }
5777
5778 if (strategy == PARTITION_STRATEGY_RANGE)
5779 {
5782
5783 /*
5784 * To simplify the check for ranges of new partitions, we need to sort
5785 * all partitions in ascending order of their bounds (we compare the
5786 * lower bound only).
5787 */
5789
5790 /* Create an array of lower bounds. */
5791 for (i = 0; i < nparts; i++)
5792 {
5794 new_parts[i]->bound->lowerdatums, true);
5795 }
5796
5797 /* Sort the array of lower bounds. */
5798 qsort_arg(lower_bounds, nparts, sizeof(PartitionRangeBound *),
5799 qsort_partition_rbound_cmp, (void *) key);
5800
5801 /* Reorder the array of partitions. */
5804 for (i = 0; i < nparts; i++)
5806
5809 }
5810
5811 for (i = 0; i < nparts; i++)
5812 {
5814
5816 {
5817 /*
5818 * When the split partition is the DEFAULT partition, we can use
5819 * any free ranges - as when creating a new partition.
5820 */
5821 check_new_partition_bound(sps->name->relname, parent, sps->bound,
5822 pstate);
5823 }
5824 else
5825 {
5826 /*
5827 * Checks that the bounds of the current partition are inside the
5828 * bounds of the split partition. For range partitioning: checks
5829 * that the upper bound of the previous partition is equal to the
5830 * lower bound of the current partition. For list partitioning:
5831 * checks that the split partition contains all values of the
5832 * current partition.
5833 */
5834 if (strategy == PARTITION_STRATEGY_RANGE)
5835 {
5836 bool first = (i == 0);
5837 bool last = (i == (nparts - 1));
5838
5839 check_partition_bounds_for_split_range(parent, sps->name->relname, sps->bound,
5840 splitPartOid, first, last,
5841 createDefaultPart, pstate);
5842 }
5843 else
5844 check_partition_bounds_for_split_list(parent, sps->name->relname,
5845 sps->bound, splitPartOid, pstate);
5846 }
5847
5848 /* Ranges of new partitions should not overlap. */
5849 if (strategy == PARTITION_STRATEGY_RANGE && spsPrev)
5851 sps->name, sps->bound,
5853 false,
5854 pstate);
5855
5856 spsPrev = sps;
5857 }
5858
5859 if (strategy == PARTITION_STRATEGY_LIST)
5860 {
5861 /* Values of new partitions should not overlap. */
5863 pstate);
5864
5865 /*
5866 * Need to check that all values of the split partition are contained
5867 * in the new partitions. Skip this check if the DEFAULT partition
5868 * exists.
5869 */
5870 if (!createDefaultPart)
5872 new_parts, nparts, pstate);
5873 }
5874
5876}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
#define ARR_ELEMTYPE(a)
Definition array.h:292
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:575
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
static Datum values[MAXATTR]
Definition bootstrap.c:190
static void cleanup(void)
Definition bootstrap.c:886
#define TextDatumGetCString(d)
Definition builtins.h:99
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:249
#define Max(x, y)
Definition c.h:1085
#define Assert(condition)
Definition c.h:943
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:558
int16_t int16
Definition c.h:619
int32_t int32
Definition c.h:620
uint64_t uint64
Definition c.h:625
uint16_t uint16
Definition c.h:623
#define OidIsValid(objectId)
Definition c.h:858
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition datum.c:132
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition datum.c:223
struct cursor * cur
Definition ecpg.c:29
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
#define DEBUG1
Definition elog.h:31
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition execExpr.c:143
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition execExpr.c:786
bool ExecCheck(ExprState *state, ExprContext *econtext)
Definition execExpr.c:905
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
void FreeExecutorState(EState *estate)
Definition execUtils.c:197
EState * CreateExecutorState(void)
Definition execUtils.c:90
#define GetPerTupleExprContext(estate)
Definition executor.h:667
#define ResetExprContext(econtext)
Definition executor.h:661
#define GetPerTupleMemoryContext(estate)
Definition executor.h:672
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition executor.h:446
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition fmgr.c:1151
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition fmgr.c:2010
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition fmgr.c:1876
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition fmgr.c:582
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_NARGS()
Definition fmgr.h:203
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
char * format_type_be(Oid type_oid)
static uint64 hash_combine64(uint64 a, uint64 b)
Definition hashfn.h:80
size_t remainder
#define HeapTupleIsValid(tuple)
Definition htup.h:78
long val
Definition informix.c:689
static struct @175 value
int b
Definition isn.c:74
int a
Definition isn.c:73
int j
Definition isn.c:78
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * lappend_int(List *list, int datum)
Definition list.c:357
void list_free(List *list)
Definition list.c:1546
#define NoLock
Definition lockdefs.h:34
#define AccessExclusiveLock
Definition lockdefs.h:43
#define AccessShareLock
Definition lockdefs.h:36
char * get_rel_name(Oid relid)
Definition lsyscache.c:2148
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition lsyscache.c:2491
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1505
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition lsyscache.c:170
Oid get_array_type(Oid typid)
Definition lsyscache.c:3009
#define type_is_array(typid)
Definition lsyscache.h:220
Expr * make_ands_explicit(List *andclauses)
Definition makefuncs.c:799
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition makefuncs.c:420
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
Node * makeBoolConst(bool value, bool isnull)
Definition makefuncs.c:408
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition makefuncs.c:453
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition makefuncs.c:594
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition makefuncs.c:701
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition makefuncs.c:350
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1392
void fix_opfuncids(Node *node)
Definition nodeFuncs.c:1848
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define IS_OUTER_JOIN(jointype)
Definition nodes.h:348
#define makeNode(_type_)
Definition nodes.h:161
#define castNode(_type_, nodeptr)
Definition nodes.h:182
JoinType
Definition nodes.h:298
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_RIGHT
Definition nodes.h:306
@ JOIN_LEFT
Definition nodes.h:304
@ JOIN_ANTI
Definition nodes.h:318
static char * errmsg
Datum lower(PG_FUNCTION_ARGS)
Datum upper(PG_FUNCTION_ARGS)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
bool IsBinaryCoercible(Oid srctype, Oid targettype)
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
@ PARTITION_STRATEGY_HASH
Definition parsenodes.h:919
@ PARTITION_STRATEGY_LIST
Definition parsenodes.h:917
@ PARTITION_STRATEGY_RANGE
Definition parsenodes.h:918
PartitionRangeDatumKind
Definition parsenodes.h:968
@ PARTITION_RANGE_DATUM_MAXVALUE
Definition parsenodes.h:971
@ PARTITION_RANGE_DATUM_VALUE
Definition parsenodes.h:970
@ PARTITION_RANGE_DATUM_MINVALUE
Definition parsenodes.h:969
static int partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, PartitionRangeBound *probe, int32 *cmpval)
static int process_inner_partition(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int inner_index, int outer_default, JoinType jointype, int *next_index, int *default_index)
static void merge_default_partitions(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_default, int inner_default, JoinType jointype, int *next_index, int *default_index)
static void init_partition_map(RelOptInfo *rel, PartitionMap *map)
static void check_partition_bounds_for_split_list(Relation parent, char *relname, PartitionBoundSpec *spec, Oid splitPartOid, ParseState *pstate)
static List * get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
static void check_parent_values_in_new_partitions(Relation parent, Oid partOid, SinglePartitionSpec **parts, int nparts, ParseState *pstate)
static int32 qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
static int32 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, Datum *datums1, PartitionRangeDatumKind *kind1, bool lower1, PartitionRangeBound *b2)
static PartitionBoundInfo create_list_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition partbounds.c:461
static bool is_dummy_partition(RelOptInfo *rel, int part_index)
static PartitionBoundInfo merge_range_bounds(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition partbounds.c:889
int32 partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation, const Datum *rb_datums, PartitionRangeDatumKind *rb_kind, const Datum *tuple_datums, int n_tuple_datums)
PartitionBoundInfo partition_bounds_merge(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
static int merge_matching_partitions(PartitionMap *outer_map, PartitionMap *inner_map, int outer_index, int inner_index, int *next_index)
static List * get_qual_for_hash(Relation parent, PartitionBoundSpec *spec)
static void get_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, JoinType jointype, PartitionRangeBound *outer_lb, PartitionRangeBound *outer_ub, PartitionRangeBound *inner_lb, PartitionRangeBound *inner_ub, int lb_cmpval, int ub_cmpval, PartitionRangeBound *merged_lb, PartitionRangeBound *merged_ub)
static void check_two_partitions_bounds_range(Relation parent, RangeVar *first_name, PartitionBoundSpec *first_bound, RangeVar *second_name, PartitionBoundSpec *second_bound, bool defaultPart, bool is_merge, ParseState *pstate)
#define compare_range_bounds(partnatts, partsupfunc, partcollations, bound1, bound2)
Definition partbounds.c:89
static int32 qsort_partition_hbound_cmp(const void *a, const void *b)
static void free_partition_map(PartitionMap *map)
static int get_non_null_list_datum_count(PartitionBoundSpec **boundspecs, int nparts)
Definition partbounds.c:435
void check_new_partition_bound(char *relname, Relation parent, PartitionBoundSpec *spec, ParseState *pstate)
static void fix_merged_indexes(PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List *merged_indexes)
static PartitionBoundSpec * get_partition_bound_spec(Oid partOid)
static int merge_partition_with_dummy(PartitionMap *map, int index, int *next_index)
static void check_partition_bounds_for_split_range(Relation parent, char *relname, PartitionBoundSpec *spec, Oid splitPartOid, bool first, bool last, bool defaultPart, ParseState *pstate)
static Expr * make_partition_op_expr(PartitionKey key, int keynum, uint16 strategy, Expr *arg1, Expr *arg2)
uint64 compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc, const Oid *partcollation, const Datum *values, const bool *isnull)
PartitionBoundInfo partition_bounds_create(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition partbounds.c:300
static PartitionBoundInfo create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition partbounds.c:348
bool partitions_are_ordered(PartitionBoundInfo boundinfo, Bitmapset *live_parts)
static List * get_qual_for_range(Relation parent, PartitionBoundSpec *spec, bool for_default)
static PartitionBoundInfo merge_list_bounds(FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
static void generate_matching_part_pairs(RelOptInfo *outer_rel, RelOptInfo *inner_rel, PartitionMap *outer_map, PartitionMap *inner_map, int nmerged, List **outer_parts, List **inner_parts)
static void add_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, PartitionRangeBound *merged_lb, PartitionRangeBound *merged_ub, int merged_index, List **merged_datums, List **merged_kinds, List **merged_indexes)
Datum satisfies_hash_partition(PG_FUNCTION_ARGS)
static PartitionRangeBound * make_one_partition_rbound(PartitionKey key, int index, List *datums, bool lower)
int partition_hash_bsearch(PartitionBoundInfo boundinfo, int modulus, int remainder)
static bool find_value_in_new_partitions_list(FmgrInfo *partsupfunc, Oid *partcollation, SinglePartitionSpec **parts, int nparts, Datum value, bool isnull)
void check_partitions_for_split(Relation parent, Oid splitPartOid, List *partlist, ParseState *pstate)
static void get_range_key_properties(PartitionKey key, int keynum, PartitionRangeDatum *ldatum, PartitionRangeDatum *udatum, ListCell **partexprs_item, Expr **keyCol, Const **lower_val, Const **upper_val)
List * get_qual_from_partbound(Relation parent, PartitionBoundSpec *spec)
Definition partbounds.c:250
static int32 partition_hbound_cmp(int modulus1, int remainder1, int modulus2, int remainder2)
static PartitionBoundInfo create_range_bounds(PartitionBoundSpec **boundspecs, int nparts, PartitionKey key, int **mapping)
Definition partbounds.c:675
static List * partitions_listdatum_intersection(FmgrInfo *partsupfunc, Oid *partcollation, const List *list1, const List *list2)
void calculate_partition_bound_for_merge(Relation parent, List *partNames, List *partOids, PartitionBoundSpec *spec, ParseState *pstate)
static int32 qsort_partition_list_value_cmp(const void *a, const void *b, void *arg)
static void check_partitions_not_overlap_list(Relation parent, SinglePartitionSpec **parts, int nparts, ParseState *pstate)
static PartitionBoundInfo build_merged_partition_bounds(char strategy, List *merged_datums, List *merged_kinds, List *merged_indexes, int null_index, int default_index)
int get_hash_partition_greatest_modulus(PartitionBoundInfo bound)
static Oid get_partition_operator(PartitionKey key, int col, StrategyNumber strategy, bool *need_relabel)
static bool compare_range_partitions(int partnatts, FmgrInfo *partsupfuncs, Oid *partcollations, PartitionRangeBound *outer_lb, PartitionRangeBound *outer_ub, PartitionRangeBound *inner_lb, PartitionRangeBound *inner_ub, int *lb_cmpval, int *ub_cmpval)
static int get_range_partition(RelOptInfo *rel, PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
int partition_range_datum_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, int nvalues, const Datum *values, bool *is_equal)
static void merge_null_partitions(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_null, bool inner_has_null, int outer_null, int inner_null, JoinType jointype, int *next_index, int *null_index)
void check_default_partition_contents(Relation parent, Relation default_rel, PartitionBoundSpec *new_spec)
static int get_range_partition_internal(PartitionBoundInfo bi, int *lb_pos, PartitionRangeBound *lb, PartitionRangeBound *ub)
static List * get_range_nulltest(PartitionKey key)
static int process_outer_partition(PartitionMap *outer_map, PartitionMap *inner_map, bool outer_has_default, bool inner_has_default, int outer_index, int inner_default, JoinType jointype, int *next_index, int *default_index)
int partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation, PartitionBoundInfo boundinfo, Datum value, bool *is_equal)
PartitionBoundInfo partition_bounds_copy(PartitionBoundInfo src, PartitionKey key)
Definition partbounds.c:995
#define partition_bound_has_default(bi)
Definition partbounds.h:99
#define partition_bound_accepts_nulls(bi)
Definition partbounds.h:98
PartitionKey RelationGetPartitionKey(Relation rel)
Definition partcache.c:51
static int get_partition_strategy(PartitionKey key)
Definition partcache.h:59
struct PartitionKeyData * PartitionKey
Definition partdefs.h:18
struct PartitionBoundInfoData * PartitionBoundInfo
Definition partdefs.h:16
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition partdesc.c:71
Oid get_default_oid_from_partdesc(PartitionDesc partdesc)
Definition partdesc.c:501
List * map_partition_varattnos(List *expr, int fromrel_varno, Relation to_rel, Relation from_rel)
Definition partition.c:222
List * get_proposed_default_constraint(List *new_part_constraints)
Definition partition.c:370
#define HASH_PARTITION_SEED
Definition partition.h:20
#define IS_DUMMY_REL(r)
Definition pathnodes.h:2299
NameData relname
Definition pg_class.h:40
#define PARTITION_MAX_KEYS
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
#define lfirst(lc)
Definition pg_list.h:172
#define llast_int(l)
Definition pg_list.h:199
#define llast(l)
Definition pg_list.h:198
#define lfirst_node(type, lc)
Definition pg_list.h:176
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
#define NIL
Definition pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define foreach_current_index(var_or_cell)
Definition pg_list.h:435
#define lfirst_int(lc)
Definition pg_list.h:173
#define list_make1_oid(x1)
Definition pg_list.h:274
#define list_make1(x1)
Definition pg_list.h:244
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
#define linitial(l)
Definition pg_list.h:178
#define list_make3(x1, x2, x3)
Definition pg_list.h:248
#define foreach_node(type, var, lst)
Definition pg_list.h:528
static ListCell * list_head(const List *l)
Definition pg_list.h:128
#define for_both_cell(cell1, list1, initcell1, cell2, list2, initcell2)
Definition pg_list.h:572
#define foreach_oid(var, lst)
Definition pg_list.h:503
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
#define lfirst_oid(lc)
Definition pg_list.h:174
#define list_make2(x1, x2)
Definition pg_list.h:246
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
#define qsort(a, b, c, d)
Definition port.h:495
static uint64 DatumGetUInt64(Datum X)
Definition postgres.h:423
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Datum UInt64GetDatum(uint64 X)
Definition postgres.h:433
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
#define InvalidOid
unsigned int Oid
char * c
static int fb(int x)
@ AND_EXPR
Definition primnodes.h:964
@ OR_EXPR
Definition primnodes.h:964
@ NOT_EXPR
Definition primnodes.h:964
@ COERCE_EXPLICIT_CAST
Definition primnodes.h:768
@ COERCE_EXPLICIT_CALL
Definition primnodes.h:767
@ IS_NULL
Definition primnodes.h:1980
@ IS_NOT_NULL
Definition primnodes.h:1980
void * stringToNode(const char *str)
Definition read.c:90
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:715
#define RelationGetRelid(relation)
Definition rel.h:516
#define RelationGetRelationName(relation)
Definition rel.h:550
int errtable(Relation rel)
Definition relcache.c:6063
char * deparse_expression(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit)
Definition ruleutils.c:4007
char * get_range_partbound_string(List *bound_datums)
@ ForwardScanDirection
Definition sdir.h:28
Snapshot GetLatestSnapshot(void)
Definition snapmgr.c:354
void UnregisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:866
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:824
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:48
uint16 StrategyNumber
Definition stratnum.h:22
#define BTGreaterStrategyNumber
Definition stratnum.h:33
#define BTLessStrategyNumber
Definition stratnum.h:29
#define BTEqualStrategyNumber
Definition stratnum.h:31
#define BTLessEqualStrategyNumber
Definition stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition stratnum.h:32
MemoryContext es_query_cxt
Definition execnodes.h:746
List * es_tupleTable
Definition execnodes.h:748
TupleTableSlot * ecxt_scantuple
Definition execnodes.h:287
Definition pg_list.h:54
Definition nodes.h:135
PartitionRangeDatumKind ** kind
Definition partbounds.h:84
PartitionStrategy strategy
Definition partbounds.h:81
Bitmapset * interleaved_parts
Definition partbounds.h:87
PartitionBoundInfo boundinfo
Definition partdesc.h:38
bool did_remapping
Definition partbounds.c:83
bool * merged
Definition partbounds.c:81
int * old_indexes
Definition partbounds.c:84
int * merged_indexes
Definition partbounds.c:80
PartitionRangeDatumKind * kind
Definition partbounds.c:69
PartitionRangeDatumKind kind
Definition parsenodes.h:978
Definition type.h:96
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:626
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition tableam.c:92
@ SO_NONE
Definition tableam.h:49
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key, uint32 flags)
Definition tableam.h:943
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition tableam.h:1096
bool PartConstraintImpliedByRelConstraint(Relation scanrel, List *partConstraint)