LCOV - code coverage report
Current view: top level - src/backend/partitioning - partbounds.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 1355 1416 95.7 %
Date: 2023-12-11 16:10:55 Functions: 51 52 98.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * partbounds.c
       4             :  *      Support routines for manipulating partition bounds
       5             :  *
       6             :  * Portions Copyright (c) 1996-2023, 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/partition.h"
      21             : #include "catalog/pg_inherits.h"
      22             : #include "catalog/pg_type.h"
      23             : #include "commands/tablecmds.h"
      24             : #include "common/hashfn.h"
      25             : #include "executor/executor.h"
      26             : #include "miscadmin.h"
      27             : #include "nodes/makefuncs.h"
      28             : #include "nodes/nodeFuncs.h"
      29             : #include "nodes/pathnodes.h"
      30             : #include "parser/parse_coerce.h"
      31             : #include "partitioning/partbounds.h"
      32             : #include "partitioning/partdesc.h"
      33             : #include "partitioning/partprune.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 */
      50             : typedef struct PartitionHashBound
      51             : {
      52             :     int         modulus;
      53             :     int         remainder;
      54             :     int         index;
      55             : } PartitionHashBound;
      56             : 
      57             : /* One value coming from some (index'th) list partition */
      58             : typedef struct PartitionListValue
      59             : {
      60             :     int         index;
      61             :     Datum       value;
      62             : } PartitionListValue;
      63             : 
      64             : /* One bound of a range partition */
      65             : typedef 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 */
      71             : } PartitionRangeBound;
      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             :  */
      77             : typedef 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 */
      86             : } PartitionMap;
      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             : 
      95             : static int32 qsort_partition_hbound_cmp(const void *a, const void *b);
      96             : static int32 qsort_partition_list_value_cmp(const void *a, const void *b,
      97             :                                             void *arg);
      98             : static int32 qsort_partition_rbound_cmp(const void *a, const void *b,
      99             :                                         void *arg);
     100             : static PartitionBoundInfo create_hash_bounds(PartitionBoundSpec **boundspecs,
     101             :                                              int nparts, PartitionKey key, int **mapping);
     102             : static PartitionBoundInfo create_list_bounds(PartitionBoundSpec **boundspecs,
     103             :                                              int nparts, PartitionKey key, int **mapping);
     104             : static PartitionBoundInfo create_range_bounds(PartitionBoundSpec **boundspecs,
     105             :                                               int nparts, PartitionKey key, int **mapping);
     106             : static PartitionBoundInfo merge_list_bounds(FmgrInfo *partsupfunc,
     107             :                                             Oid *partcollation,
     108             :                                             RelOptInfo *outer_rel,
     109             :                                             RelOptInfo *inner_rel,
     110             :                                             JoinType jointype,
     111             :                                             List **outer_parts,
     112             :                                             List **inner_parts);
     113             : static PartitionBoundInfo merge_range_bounds(int partnatts,
     114             :                                              FmgrInfo *partsupfuncs,
     115             :                                              Oid *partcollations,
     116             :                                              RelOptInfo *outer_rel,
     117             :                                              RelOptInfo *inner_rel,
     118             :                                              JoinType jointype,
     119             :                                              List **outer_parts,
     120             :                                              List **inner_parts);
     121             : static void init_partition_map(RelOptInfo *rel, PartitionMap *map);
     122             : static void free_partition_map(PartitionMap *map);
     123             : static bool is_dummy_partition(RelOptInfo *rel, int part_index);
     124             : static int  merge_matching_partitions(PartitionMap *outer_map,
     125             :                                       PartitionMap *inner_map,
     126             :                                       int outer_index,
     127             :                                       int inner_index,
     128             :                                       int *next_index);
     129             : static int  process_outer_partition(PartitionMap *outer_map,
     130             :                                     PartitionMap *inner_map,
     131             :                                     bool outer_has_default,
     132             :                                     bool inner_has_default,
     133             :                                     int outer_index,
     134             :                                     int inner_default,
     135             :                                     JoinType jointype,
     136             :                                     int *next_index,
     137             :                                     int *default_index);
     138             : static int  process_inner_partition(PartitionMap *outer_map,
     139             :                                     PartitionMap *inner_map,
     140             :                                     bool outer_has_default,
     141             :                                     bool inner_has_default,
     142             :                                     int inner_index,
     143             :                                     int outer_default,
     144             :                                     JoinType jointype,
     145             :                                     int *next_index,
     146             :                                     int *default_index);
     147             : static void merge_null_partitions(PartitionMap *outer_map,
     148             :                                   PartitionMap *inner_map,
     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);
     156             : static void merge_default_partitions(PartitionMap *outer_map,
     157             :                                      PartitionMap *inner_map,
     158             :                                      bool outer_has_default,
     159             :                                      bool inner_has_default,
     160             :                                      int outer_default,
     161             :                                      int inner_default,
     162             :                                      JoinType jointype,
     163             :                                      int *next_index,
     164             :                                      int *default_index);
     165             : static int  merge_partition_with_dummy(PartitionMap *map, int index,
     166             :                                        int *next_index);
     167             : static void fix_merged_indexes(PartitionMap *outer_map,
     168             :                                PartitionMap *inner_map,
     169             :                                int nmerged, List *merged_indexes);
     170             : static void generate_matching_part_pairs(RelOptInfo *outer_rel,
     171             :                                          RelOptInfo *inner_rel,
     172             :                                          PartitionMap *outer_map,
     173             :                                          PartitionMap *inner_map,
     174             :                                          int nmerged,
     175             :                                          List **outer_parts,
     176             :                                          List **inner_parts);
     177             : static PartitionBoundInfo build_merged_partition_bounds(char strategy,
     178             :                                                         List *merged_datums,
     179             :                                                         List *merged_kinds,
     180             :                                                         List *merged_indexes,
     181             :                                                         int null_index,
     182             :                                                         int default_index);
     183             : static int  get_range_partition(RelOptInfo *rel,
     184             :                                 PartitionBoundInfo bi,
     185             :                                 int *lb_pos,
     186             :                                 PartitionRangeBound *lb,
     187             :                                 PartitionRangeBound *ub);
     188             : static int  get_range_partition_internal(PartitionBoundInfo bi,
     189             :                                          int *lb_pos,
     190             :                                          PartitionRangeBound *lb,
     191             :                                          PartitionRangeBound *ub);
     192             : static bool compare_range_partitions(int partnatts, FmgrInfo *partsupfuncs,
     193             :                                      Oid *partcollations,
     194             :                                      PartitionRangeBound *outer_lb,
     195             :                                      PartitionRangeBound *outer_ub,
     196             :                                      PartitionRangeBound *inner_lb,
     197             :                                      PartitionRangeBound *inner_ub,
     198             :                                      int *lb_cmpval, int *ub_cmpval);
     199             : static void get_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
     200             :                                     Oid *partcollations, JoinType jointype,
     201             :                                     PartitionRangeBound *outer_lb,
     202             :                                     PartitionRangeBound *outer_ub,
     203             :                                     PartitionRangeBound *inner_lb,
     204             :                                     PartitionRangeBound *inner_ub,
     205             :                                     int lb_cmpval, int ub_cmpval,
     206             :                                     PartitionRangeBound *merged_lb,
     207             :                                     PartitionRangeBound *merged_ub);
     208             : static void add_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
     209             :                                     Oid *partcollations,
     210             :                                     PartitionRangeBound *merged_lb,
     211             :                                     PartitionRangeBound *merged_ub,
     212             :                                     int merged_index,
     213             :                                     List **merged_datums,
     214             :                                     List **merged_kinds,
     215             :                                     List **merged_indexes);
     216             : static PartitionRangeBound *make_one_partition_rbound(PartitionKey key, int index,
     217             :                                                       List *datums, bool lower);
     218             : static int32 partition_hbound_cmp(int modulus1, int remainder1, int modulus2,
     219             :                                   int remainder2);
     220             : static int32 partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc,
     221             :                                   Oid *partcollation, Datum *datums1,
     222             :                                   PartitionRangeDatumKind *kind1, bool lower1,
     223             :                                   PartitionRangeBound *b2);
     224             : static int  partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
     225             :                                     Oid *partcollation,
     226             :                                     PartitionBoundInfo boundinfo,
     227             :                                     PartitionRangeBound *probe, int32 *cmpval);
     228             : static Expr *make_partition_op_expr(PartitionKey key, int keynum,
     229             :                                     uint16 strategy, Expr *arg1, Expr *arg2);
     230             : static Oid  get_partition_operator(PartitionKey key, int col,
     231             :                                    StrategyNumber strategy, bool *need_relabel);
     232             : static List *get_qual_for_hash(Relation parent, PartitionBoundSpec *spec);
     233             : static List *get_qual_for_list(Relation parent, PartitionBoundSpec *spec);
     234             : static List *get_qual_for_range(Relation parent, PartitionBoundSpec *spec,
     235             :                                 bool for_default);
     236             : static void get_range_key_properties(PartitionKey key, int keynum,
     237             :                                      PartitionRangeDatum *ldatum,
     238             :                                      PartitionRangeDatum *udatum,
     239             :                                      ListCell **partexprs_item,
     240             :                                      Expr **keyCol,
     241             :                                      Const **lower_val, Const **upper_val);
     242             : static List *get_range_nulltest(PartitionKey key);
     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             :  */
     249             : List *
     250        5044 : get_qual_from_partbound(Relation parent, PartitionBoundSpec *spec)
     251             : {
     252        5044 :     PartitionKey key = RelationGetPartitionKey(parent);
     253        5044 :     List       *my_qual = NIL;
     254             : 
     255             :     Assert(key != NULL);
     256             : 
     257        5044 :     switch (key->strategy)
     258             :     {
     259         152 :         case PARTITION_STRATEGY_HASH:
     260             :             Assert(spec->strategy == PARTITION_STRATEGY_HASH);
     261         152 :             my_qual = get_qual_for_hash(parent, spec);
     262         152 :             break;
     263             : 
     264        2272 :         case PARTITION_STRATEGY_LIST:
     265             :             Assert(spec->strategy == PARTITION_STRATEGY_LIST);
     266        2272 :             my_qual = get_qual_for_list(parent, spec);
     267        2272 :             break;
     268             : 
     269        2620 :         case PARTITION_STRATEGY_RANGE:
     270             :             Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
     271        2620 :             my_qual = get_qual_for_range(parent, spec, false);
     272        2620 :             break;
     273             :     }
     274             : 
     275        5044 :     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             :  */
     299             : PartitionBoundInfo
     300       14766 : partition_bounds_create(PartitionBoundSpec **boundspecs, int nparts,
     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       14766 :     *mapping = (int *) palloc(sizeof(int) * nparts);
     324       44434 :     for (i = 0; i < nparts; i++)
     325       29668 :         (*mapping)[i] = -1;
     326             : 
     327       14766 :     switch (key->strategy)
     328             :     {
     329         708 :         case PARTITION_STRATEGY_HASH:
     330         708 :             return create_hash_bounds(boundspecs, nparts, key, mapping);
     331             : 
     332        7444 :         case PARTITION_STRATEGY_LIST:
     333        7444 :             return create_list_bounds(boundspecs, nparts, key, mapping);
     334             : 
     335        6614 :         case PARTITION_STRATEGY_RANGE:
     336        6614 :             return create_range_bounds(boundspecs, nparts, key, mapping);
     337             :     }
     338             : 
     339             :     Assert(false);
     340           0 :     return NULL;                /* keep compiler quiet */
     341             : }
     342             : 
     343             : /*
     344             :  * create_hash_bounds
     345             :  *      Create a PartitionBoundInfo for a hash partitioned table
     346             :  */
     347             : static PartitionBoundInfo
     348         708 : create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts,
     349             :                    PartitionKey key, int **mapping)
     350             : {
     351             :     PartitionBoundInfo boundinfo;
     352             :     PartitionHashBound *hbounds;
     353             :     int         i;
     354             :     int         greatest_modulus;
     355             :     Datum      *boundDatums;
     356             : 
     357             :     boundinfo = (PartitionBoundInfoData *)
     358         708 :         palloc0(sizeof(PartitionBoundInfoData));
     359         708 :     boundinfo->strategy = key->strategy;
     360             :     /* No special hash partitions. */
     361         708 :     boundinfo->null_index = -1;
     362         708 :     boundinfo->default_index = -1;
     363             : 
     364             :     hbounds = (PartitionHashBound *)
     365         708 :         palloc(nparts * sizeof(PartitionHashBound));
     366             : 
     367             :     /* Convert from node to the internal representation */
     368        2260 :     for (i = 0; i < nparts; i++)
     369             :     {
     370        1552 :         PartitionBoundSpec *spec = boundspecs[i];
     371             : 
     372        1552 :         if (spec->strategy != PARTITION_STRATEGY_HASH)
     373           0 :             elog(ERROR, "invalid strategy in partition bound spec");
     374             : 
     375        1552 :         hbounds[i].modulus = spec->modulus;
     376        1552 :         hbounds[i].remainder = spec->remainder;
     377        1552 :         hbounds[i].index = i;
     378             :     }
     379             : 
     380             :     /* Sort all the bounds in ascending order */
     381         708 :     qsort(hbounds, nparts, sizeof(PartitionHashBound),
     382             :           qsort_partition_hbound_cmp);
     383             : 
     384             :     /* After sorting, moduli are now stored in ascending order. */
     385         708 :     greatest_modulus = hbounds[nparts - 1].modulus;
     386             : 
     387         708 :     boundinfo->ndatums = nparts;
     388         708 :     boundinfo->datums = (Datum **) palloc0(nparts * sizeof(Datum *));
     389         708 :     boundinfo->kind = NULL;
     390         708 :     boundinfo->interleaved_parts = NULL;
     391         708 :     boundinfo->nindexes = greatest_modulus;
     392         708 :     boundinfo->indexes = (int *) palloc(greatest_modulus * sizeof(int));
     393        5838 :     for (i = 0; i < greatest_modulus; i++)
     394        5130 :         boundinfo->indexes[i] = -1;
     395             : 
     396             :     /*
     397             :      * In the loop below, to save from allocating a series of small datum
     398             :      * arrays, here we just allocate a single array and below we'll just
     399             :      * assign a portion of this array per partition.
     400             :      */
     401         708 :     boundDatums = (Datum *) palloc(nparts * 2 * sizeof(Datum));
     402             : 
     403             :     /*
     404             :      * For hash partitioning, there are as many datums (modulus and remainder
     405             :      * pairs) as there are partitions.  Indexes are simply values ranging from
     406             :      * 0 to (nparts - 1).
     407             :      */
     408        2260 :     for (i = 0; i < nparts; i++)
     409             :     {
     410        1552 :         int         modulus = hbounds[i].modulus;
     411        1552 :         int         remainder = hbounds[i].remainder;
     412             : 
     413        1552 :         boundinfo->datums[i] = &boundDatums[i * 2];
     414        1552 :         boundinfo->datums[i][0] = Int32GetDatum(modulus);
     415        1552 :         boundinfo->datums[i][1] = Int32GetDatum(remainder);
     416             : 
     417        3578 :         while (remainder < greatest_modulus)
     418             :         {
     419             :             /* overlap? */
     420             :             Assert(boundinfo->indexes[remainder] == -1);
     421        2026 :             boundinfo->indexes[remainder] = i;
     422        2026 :             remainder += modulus;
     423             :         }
     424             : 
     425        1552 :         (*mapping)[hbounds[i].index] = i;
     426             :     }
     427         708 :     pfree(hbounds);
     428             : 
     429         708 :     return boundinfo;
     430             : }
     431             : 
     432             : /*
     433             :  * get_non_null_list_datum_count
     434             :  *      Counts the number of non-null Datums in each partition.
     435             :  */
     436             : static int
     437        7444 : get_non_null_list_datum_count(PartitionBoundSpec **boundspecs, int nparts)
     438             : {
     439             :     int         i;
     440        7444 :     int         count = 0;
     441             : 
     442       21990 :     for (i = 0; i < nparts; i++)
     443             :     {
     444             :         ListCell   *lc;
     445             : 
     446       35224 :         foreach(lc, boundspecs[i]->listdatums)
     447             :         {
     448       20678 :             Const      *val = lfirst_node(Const, lc);
     449             : 
     450       20678 :             if (!val->constisnull)
     451       20144 :                 count++;
     452             :         }
     453             :     }
     454             : 
     455        7444 :     return count;
     456             : }
     457             : 
     458             : /*
     459             :  * create_list_bounds
     460             :  *      Create a PartitionBoundInfo for a list partitioned table
     461             :  */
     462             : static PartitionBoundInfo
     463        7444 : create_list_bounds(PartitionBoundSpec **boundspecs, int nparts,
     464             :                    PartitionKey key, int **mapping)
     465             : {
     466             :     PartitionBoundInfo boundinfo;
     467             :     PartitionListValue *all_values;
     468             :     int         i;
     469             :     int         j;
     470             :     int         ndatums;
     471        7444 :     int         next_index = 0;
     472        7444 :     int         default_index = -1;
     473        7444 :     int         null_index = -1;
     474             :     Datum      *boundDatums;
     475             : 
     476             :     boundinfo = (PartitionBoundInfoData *)
     477        7444 :         palloc0(sizeof(PartitionBoundInfoData));
     478        7444 :     boundinfo->strategy = key->strategy;
     479             :     /* Will be set correctly below. */
     480        7444 :     boundinfo->null_index = -1;
     481        7444 :     boundinfo->default_index = -1;
     482             : 
     483        7444 :     ndatums = get_non_null_list_datum_count(boundspecs, nparts);
     484             :     all_values = (PartitionListValue *)
     485        7444 :         palloc(ndatums * sizeof(PartitionListValue));
     486             : 
     487             :     /* Create a unified list of non-null values across all partitions. */
     488       21990 :     for (j = 0, i = 0; i < nparts; i++)
     489             :     {
     490       14546 :         PartitionBoundSpec *spec = boundspecs[i];
     491             :         ListCell   *c;
     492             : 
     493       14546 :         if (spec->strategy != PARTITION_STRATEGY_LIST)
     494           0 :             elog(ERROR, "invalid strategy in partition bound spec");
     495             : 
     496             :         /*
     497             :          * Note the index of the partition bound spec for the default
     498             :          * partition.  There's no datum to add to the list on non-null datums
     499             :          * for this partition.
     500             :          */
     501       14546 :         if (spec->is_default)
     502             :         {
     503         858 :             default_index = i;
     504         858 :             continue;
     505             :         }
     506             : 
     507       34366 :         foreach(c, spec->listdatums)
     508             :         {
     509       20678 :             Const      *val = lfirst_node(Const, c);
     510             : 
     511       20678 :             if (!val->constisnull)
     512             :             {
     513       20144 :                 all_values[j].index = i;
     514       20144 :                 all_values[j].value = val->constvalue;
     515       20144 :                 j++;
     516             :             }
     517             :             else
     518             :             {
     519             :                 /*
     520             :                  * Never put a null into the values array; save the index of
     521             :                  * the partition that stores nulls, instead.
     522             :                  */
     523         534 :                 if (null_index != -1)
     524           0 :                     elog(ERROR, "found null more than once");
     525         534 :                 null_index = i;
     526             :             }
     527             :         }
     528             :     }
     529             : 
     530             :     /* ensure we found a Datum for every slot in the all_values array */
     531             :     Assert(j == ndatums);
     532             : 
     533        7444 :     qsort_arg(all_values, ndatums, sizeof(PartitionListValue),
     534             :               qsort_partition_list_value_cmp, key);
     535             : 
     536        7444 :     boundinfo->ndatums = ndatums;
     537        7444 :     boundinfo->datums = (Datum **) palloc0(ndatums * sizeof(Datum *));
     538        7444 :     boundinfo->kind = NULL;
     539        7444 :     boundinfo->interleaved_parts = NULL;
     540        7444 :     boundinfo->nindexes = ndatums;
     541        7444 :     boundinfo->indexes = (int *) palloc(ndatums * sizeof(int));
     542             : 
     543             :     /*
     544             :      * In the loop below, to save from allocating a series of small datum
     545             :      * arrays, here we just allocate a single array and below we'll just
     546             :      * assign a portion of this array per datum.
     547             :      */
     548        7444 :     boundDatums = (Datum *) palloc(ndatums * sizeof(Datum));
     549             : 
     550             :     /*
     551             :      * Copy values.  Canonical indexes are values ranging from 0 to (nparts -
     552             :      * 1) assigned to each partition such that all datums of a given partition
     553             :      * receive the same value. The value for a given partition is the index of
     554             :      * that partition's smallest datum in the all_values[] array.
     555             :      */
     556       27588 :     for (i = 0; i < ndatums; i++)
     557             :     {
     558       20144 :         int         orig_index = all_values[i].index;
     559             : 
     560       20144 :         boundinfo->datums[i] = &boundDatums[i];
     561       40288 :         boundinfo->datums[i][0] = datumCopy(all_values[i].value,
     562       20144 :                                             key->parttypbyval[0],
     563       20144 :                                             key->parttyplen[0]);
     564             : 
     565             :         /* If the old index has no mapping, assign one */
     566       20144 :         if ((*mapping)[orig_index] == -1)
     567       13496 :             (*mapping)[orig_index] = next_index++;
     568             : 
     569       20144 :         boundinfo->indexes[i] = (*mapping)[orig_index];
     570             :     }
     571             : 
     572        7444 :     pfree(all_values);
     573             : 
     574             :     /*
     575             :      * Set the canonical value for null_index, if any.
     576             :      *
     577             :      * It is possible that the null-accepting partition has not been assigned
     578             :      * an index yet, which could happen if such partition accepts only null
     579             :      * and hence not handled in the above loop which only looked at non-null
     580             :      * values.
     581             :      */
     582        7444 :     if (null_index != -1)
     583             :     {
     584             :         Assert(null_index >= 0);
     585         534 :         if ((*mapping)[null_index] == -1)
     586         192 :             (*mapping)[null_index] = next_index++;
     587         534 :         boundinfo->null_index = (*mapping)[null_index];
     588             :     }
     589             : 
     590             :     /* Set the canonical value for default_index, if any. */
     591        7444 :     if (default_index != -1)
     592             :     {
     593             :         /*
     594             :          * The default partition accepts any value not specified in the lists
     595             :          * of other partitions, hence it should not get mapped index while
     596             :          * assigning those for non-null datums.
     597             :          */
     598             :         Assert(default_index >= 0);
     599             :         Assert((*mapping)[default_index] == -1);
     600         858 :         (*mapping)[default_index] = next_index++;
     601         858 :         boundinfo->default_index = (*mapping)[default_index];
     602             :     }
     603             : 
     604             :     /*
     605             :      * Calculate interleaved partitions.  Here we look for partitions which
     606             :      * might be interleaved with other partitions and set a bit in
     607             :      * interleaved_parts for any partitions which may be interleaved with
     608             :      * another partition.
     609             :      */
     610             : 
     611             :     /*
     612             :      * There must be multiple partitions to have any interleaved partitions,
     613             :      * otherwise there's nothing to interleave with.
     614             :      */
     615        7444 :     if (nparts > 1)
     616             :     {
     617             :         /*
     618             :          * Short-circuit check to see if only 1 Datum is allowed per
     619             :          * partition.  When this is true there's no need to do the more
     620             :          * expensive checks to look for interleaved values.
     621             :          */
     622        4454 :         if (boundinfo->ndatums +
     623        4454 :             partition_bound_accepts_nulls(boundinfo) +
     624        4454 :             partition_bound_has_default(boundinfo) != nparts)
     625             :         {
     626        1704 :             int         last_index = -1;
     627             : 
     628             :             /*
     629             :              * Since the indexes array is sorted in Datum order, if any
     630             :              * partitions are interleaved then it will show up by the
     631             :              * partition indexes not being in ascending order.  Here we check
     632             :              * for that and record all partitions that are out of order.
     633             :              */
     634       11958 :             for (i = 0; i < boundinfo->nindexes; i++)
     635             :             {
     636       10254 :                 int         index = boundinfo->indexes[i];
     637             : 
     638       10254 :                 if (index < last_index)
     639         616 :                     boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
     640             :                                                                   index);
     641             : 
     642             :                 /*
     643             :                  * Otherwise, if the null_index exists in the indexes array,
     644             :                  * then the NULL partition must also allow some other Datum,
     645             :                  * therefore it's "interleaved".
     646             :                  */
     647        9638 :                 else if (partition_bound_accepts_nulls(boundinfo) &&
     648        2838 :                          index == boundinfo->null_index)
     649         756 :                     boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
     650             :                                                                   index);
     651             : 
     652       10254 :                 last_index = index;
     653             :             }
     654             :         }
     655             : 
     656             :         /*
     657             :          * The DEFAULT partition is the "catch-all" partition that can contain
     658             :          * anything that does not belong to any other partition.  If there are
     659             :          * any other partitions then the DEFAULT partition must be marked as
     660             :          * interleaved.
     661             :          */
     662        4454 :         if (partition_bound_has_default(boundinfo))
     663         736 :             boundinfo->interleaved_parts = bms_add_member(boundinfo->interleaved_parts,
     664             :                                                           boundinfo->default_index);
     665             :     }
     666             : 
     667             : 
     668             :     /* All partitions must now have been assigned canonical indexes. */
     669             :     Assert(next_index == nparts);
     670        7444 :     return boundinfo;
     671             : }
     672             : 
     673             : /*
     674             :  * create_range_bounds
     675             :  *      Create a PartitionBoundInfo for a range partitioned table
     676             :  */
     677             : static PartitionBoundInfo
     678        6614 : create_range_bounds(PartitionBoundSpec **boundspecs, int nparts,
     679             :                     PartitionKey key, int **mapping)
     680             : {
     681             :     PartitionBoundInfo boundinfo;
     682        6614 :     PartitionRangeBound **rbounds = NULL;
     683             :     PartitionRangeBound **all_bounds,
     684             :                *prev;
     685             :     int         i,
     686             :                 k,
     687             :                 partnatts;
     688        6614 :     int         ndatums = 0;
     689        6614 :     int         default_index = -1;
     690        6614 :     int         next_index = 0;
     691             :     Datum      *boundDatums;
     692             :     PartitionRangeDatumKind *boundKinds;
     693             : 
     694             :     boundinfo = (PartitionBoundInfoData *)
     695        6614 :         palloc0(sizeof(PartitionBoundInfoData));
     696        6614 :     boundinfo->strategy = key->strategy;
     697             :     /* There is no special null-accepting range partition. */
     698        6614 :     boundinfo->null_index = -1;
     699             :     /* Will be set correctly below. */
     700        6614 :     boundinfo->default_index = -1;
     701             : 
     702             :     all_bounds = (PartitionRangeBound **)
     703        6614 :         palloc0(2 * nparts * sizeof(PartitionRangeBound *));
     704             : 
     705             :     /* Create a unified list of range bounds across all the partitions. */
     706        6614 :     ndatums = 0;
     707       20184 :     for (i = 0; i < nparts; i++)
     708             :     {
     709       13570 :         PartitionBoundSpec *spec = boundspecs[i];
     710             :         PartitionRangeBound *lower,
     711             :                    *upper;
     712             : 
     713       13570 :         if (spec->strategy != PARTITION_STRATEGY_RANGE)
     714           0 :             elog(ERROR, "invalid strategy in partition bound spec");
     715             : 
     716             :         /*
     717             :          * Note the index of the partition bound spec for the default
     718             :          * partition.  There's no datum to add to the all_bounds array for
     719             :          * this partition.
     720             :          */
     721       13570 :         if (spec->is_default)
     722             :         {
     723         760 :             default_index = i;
     724         760 :             continue;
     725             :         }
     726             : 
     727       12810 :         lower = make_one_partition_rbound(key, i, spec->lowerdatums, true);
     728       12810 :         upper = make_one_partition_rbound(key, i, spec->upperdatums, false);
     729       12810 :         all_bounds[ndatums++] = lower;
     730       12810 :         all_bounds[ndatums++] = upper;
     731             :     }
     732             : 
     733             :     Assert(ndatums == nparts * 2 ||
     734             :            (default_index != -1 && ndatums == (nparts - 1) * 2));
     735             : 
     736             :     /* Sort all the bounds in ascending order */
     737        6614 :     qsort_arg(all_bounds, ndatums,
     738             :               sizeof(PartitionRangeBound *),
     739             :               qsort_partition_rbound_cmp,
     740             :               key);
     741             : 
     742             :     /* Save distinct bounds from all_bounds into rbounds. */
     743             :     rbounds = (PartitionRangeBound **)
     744        6614 :         palloc(ndatums * sizeof(PartitionRangeBound *));
     745        6614 :     k = 0;
     746        6614 :     prev = NULL;
     747       32234 :     for (i = 0; i < ndatums; i++)
     748             :     {
     749       25620 :         PartitionRangeBound *cur = all_bounds[i];
     750       25620 :         bool        is_distinct = false;
     751             :         int         j;
     752             : 
     753             :         /* Is the current bound distinct from the previous one? */
     754       34736 :         for (j = 0; j < key->partnatts; j++)
     755             :         {
     756             :             Datum       cmpval;
     757             : 
     758       29368 :             if (prev == NULL || cur->kind[j] != prev->kind[j])
     759             :             {
     760        7908 :                 is_distinct = true;
     761        7908 :                 break;
     762             :             }
     763             : 
     764             :             /*
     765             :              * If the bounds are both MINVALUE or MAXVALUE, stop now and treat
     766             :              * them as equal, since any values after this point must be
     767             :              * ignored.
     768             :              */
     769       21460 :             if (cur->kind[j] != PARTITION_RANGE_DATUM_VALUE)
     770         186 :                 break;
     771             : 
     772       21274 :             cmpval = FunctionCall2Coll(&key->partsupfunc[j],
     773       21274 :                                        key->partcollation[j],
     774       21274 :                                        cur->datums[j],
     775       21274 :                                        prev->datums[j]);
     776       21274 :             if (DatumGetInt32(cmpval) != 0)
     777             :             {
     778       12158 :                 is_distinct = true;
     779       12158 :                 break;
     780             :             }
     781             :         }
     782             : 
     783             :         /*
     784             :          * Only if the bound is distinct save it into a temporary array, i.e,
     785             :          * rbounds which is later copied into boundinfo datums array.
     786             :          */
     787       25620 :         if (is_distinct)
     788       20066 :             rbounds[k++] = all_bounds[i];
     789             : 
     790       25620 :         prev = cur;
     791             :     }
     792             : 
     793        6614 :     pfree(all_bounds);
     794             : 
     795             :     /* Update ndatums to hold the count of distinct datums. */
     796        6614 :     ndatums = k;
     797             : 
     798             :     /*
     799             :      * Add datums to boundinfo.  Canonical indexes are values ranging from 0
     800             :      * to nparts - 1, assigned in that order to each partition's upper bound.
     801             :      * For 'datums' elements that are lower bounds, there is -1 in the
     802             :      * 'indexes' array to signify that no partition exists for the values less
     803             :      * than such a bound and greater than or equal to the previous upper
     804             :      * bound.
     805             :      */
     806        6614 :     boundinfo->ndatums = ndatums;
     807        6614 :     boundinfo->datums = (Datum **) palloc0(ndatums * sizeof(Datum *));
     808        6614 :     boundinfo->kind = (PartitionRangeDatumKind **)
     809        6614 :         palloc(ndatums *
     810             :                sizeof(PartitionRangeDatumKind *));
     811        6614 :     boundinfo->interleaved_parts = NULL;
     812             : 
     813             :     /*
     814             :      * For range partitioning, an additional value of -1 is stored as the last
     815             :      * element of the indexes[] array.
     816             :      */
     817        6614 :     boundinfo->nindexes = ndatums + 1;
     818        6614 :     boundinfo->indexes = (int *) palloc((ndatums + 1) * sizeof(int));
     819             : 
     820             :     /*
     821             :      * In the loop below, to save from allocating a series of small arrays,
     822             :      * here we just allocate a single array for Datums and another for
     823             :      * PartitionRangeDatumKinds, below we'll just assign a portion of these
     824             :      * arrays in each loop.
     825             :      */
     826        6614 :     partnatts = key->partnatts;
     827        6614 :     boundDatums = (Datum *) palloc(ndatums * partnatts * sizeof(Datum));
     828        6614 :     boundKinds = (PartitionRangeDatumKind *) palloc(ndatums * partnatts *
     829             :                                                     sizeof(PartitionRangeDatumKind));
     830             : 
     831       26680 :     for (i = 0; i < ndatums; i++)
     832             :     {
     833             :         int         j;
     834             : 
     835       20066 :         boundinfo->datums[i] = &boundDatums[i * partnatts];
     836       20066 :         boundinfo->kind[i] = &boundKinds[i * partnatts];
     837       45660 :         for (j = 0; j < partnatts; j++)
     838             :         {
     839       25594 :             if (rbounds[i]->kind[j] == PARTITION_RANGE_DATUM_VALUE)
     840       23108 :                 boundinfo->datums[i][j] =
     841       23108 :                     datumCopy(rbounds[i]->datums[j],
     842       23108 :                               key->parttypbyval[j],
     843       23108 :                               key->parttyplen[j]);
     844       25594 :             boundinfo->kind[i][j] = rbounds[i]->kind[j];
     845             :         }
     846             : 
     847             :         /*
     848             :          * There is no mapping for invalid indexes.
     849             :          *
     850             :          * Any lower bounds in the rbounds array have invalid indexes
     851             :          * assigned, because the values between the previous bound (if there
     852             :          * is one) and this (lower) bound are not part of the range of any
     853             :          * existing partition.
     854             :          */
     855       20066 :         if (rbounds[i]->lower)
     856        7256 :             boundinfo->indexes[i] = -1;
     857             :         else
     858             :         {
     859       12810 :             int         orig_index = rbounds[i]->index;
     860             : 
     861             :             /* If the old index has no mapping, assign one */
     862       12810 :             if ((*mapping)[orig_index] == -1)
     863       12810 :                 (*mapping)[orig_index] = next_index++;
     864             : 
     865       12810 :             boundinfo->indexes[i] = (*mapping)[orig_index];
     866             :         }
     867             :     }
     868             : 
     869        6614 :     pfree(rbounds);
     870             : 
     871             :     /* Set the canonical value for default_index, if any. */
     872        6614 :     if (default_index != -1)
     873             :     {
     874             :         Assert(default_index >= 0 && (*mapping)[default_index] == -1);
     875         760 :         (*mapping)[default_index] = next_index++;
     876         760 :         boundinfo->default_index = (*mapping)[default_index];
     877             :     }
     878             : 
     879             :     /* The extra -1 element. */
     880             :     Assert(i == ndatums);
     881        6614 :     boundinfo->indexes[i] = -1;
     882             : 
     883             :     /* All partitions must now have been assigned canonical indexes. */
     884             :     Assert(next_index == nparts);
     885        6614 :     return boundinfo;
     886             : }
     887             : 
     888             : /*
     889             :  * Are two partition bound collections logically equal?
     890             :  *
     891             :  * Used in the keep logic of relcache.c (ie, in RelationClearRelation()).
     892             :  * This is also useful when b1 and b2 are bound collections of two separate
     893             :  * relations, respectively, because PartitionBoundInfo is a canonical
     894             :  * representation of partition bounds.
     895             :  */
     896             : bool
     897        1488 : partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
     898             :                        PartitionBoundInfo b1, PartitionBoundInfo b2)
     899             : {
     900             :     int         i;
     901             : 
     902        1488 :     if (b1->strategy != b2->strategy)
     903           0 :         return false;
     904             : 
     905        1488 :     if (b1->ndatums != b2->ndatums)
     906         222 :         return false;
     907             : 
     908        1266 :     if (b1->nindexes != b2->nindexes)
     909           0 :         return false;
     910             : 
     911        1266 :     if (b1->null_index != b2->null_index)
     912          72 :         return false;
     913             : 
     914        1194 :     if (b1->default_index != b2->default_index)
     915           0 :         return false;
     916             : 
     917             :     /* For all partition strategies, the indexes[] arrays have to match */
     918        7190 :     for (i = 0; i < b1->nindexes; i++)
     919             :     {
     920        6044 :         if (b1->indexes[i] != b2->indexes[i])
     921          48 :             return false;
     922             :     }
     923             : 
     924             :     /* Finally, compare the datums[] arrays */
     925        1146 :     if (b1->strategy == PARTITION_STRATEGY_HASH)
     926             :     {
     927             :         /*
     928             :          * We arrange the partitions in the ascending order of their moduli
     929             :          * and remainders.  Also every modulus is factor of next larger
     930             :          * modulus.  Therefore we can safely store index of a given partition
     931             :          * in indexes array at remainder of that partition.  Also entries at
     932             :          * (remainder + N * modulus) positions in indexes array are all same
     933             :          * for (modulus, remainder) specification for any partition.  Thus the
     934             :          * datums arrays from the given bounds are the same, if and only if
     935             :          * their indexes arrays are the same.  So, it suffices to compare the
     936             :          * indexes arrays.
     937             :          *
     938             :          * Nonetheless make sure that the bounds are indeed the same when the
     939             :          * indexes match.  Hash partition bound stores modulus and remainder
     940             :          * at b1->datums[i][0] and b1->datums[i][1] position respectively.
     941             :          */
     942             : #ifdef USE_ASSERT_CHECKING
     943             :         for (i = 0; i < b1->ndatums; i++)
     944             :             Assert((b1->datums[i][0] == b2->datums[i][0] &&
     945             :                     b1->datums[i][1] == b2->datums[i][1]));
     946             : #endif
     947             :     }
     948             :     else
     949             :     {
     950        5144 :         for (i = 0; i < b1->ndatums; i++)
     951             :         {
     952             :             int         j;
     953             : 
     954        8326 :             for (j = 0; j < partnatts; j++)
     955             :             {
     956             :                 /* For range partitions, the bounds might not be finite. */
     957        4280 :                 if (b1->kind != NULL)
     958             :                 {
     959             :                     /* The different kinds of bound all differ from each other */
     960        3206 :                     if (b1->kind[i][j] != b2->kind[i][j])
     961           0 :                         return false;
     962             : 
     963             :                     /*
     964             :                      * Non-finite bounds are equal without further
     965             :                      * examination.
     966             :                      */
     967        3206 :                     if (b1->kind[i][j] != PARTITION_RANGE_DATUM_VALUE)
     968           0 :                         continue;
     969             :                 }
     970             : 
     971             :                 /*
     972             :                  * Compare the actual values. Note that it would be both
     973             :                  * incorrect and unsafe to invoke the comparison operator
     974             :                  * derived from the partitioning specification here.  It would
     975             :                  * be incorrect because we want the relcache entry to be
     976             :                  * updated for ANY change to the partition bounds, not just
     977             :                  * those that the partitioning operator thinks are
     978             :                  * significant.  It would be unsafe because we might reach
     979             :                  * this code in the context of an aborted transaction, and an
     980             :                  * arbitrary partitioning operator might not be safe in that
     981             :                  * context.  datumIsEqual() should be simple enough to be
     982             :                  * safe.
     983             :                  */
     984        4280 :                 if (!datumIsEqual(b1->datums[i][j], b2->datums[i][j],
     985        4280 :                                   parttypbyval[j], parttyplen[j]))
     986         186 :                     return false;
     987             :             }
     988             :         }
     989             :     }
     990         960 :     return true;
     991             : }
     992             : 
     993             : /*
     994             :  * Return a copy of given PartitionBoundInfo structure. The data types of bounds
     995             :  * are described by given partition key specification.
     996             :  *
     997             :  * Note: it's important that this function and its callees not do any catalog
     998             :  * access, nor anything else that would result in allocating memory other than
     999             :  * the returned data structure.  Since this is called in a long-lived context,
    1000             :  * that would result in unwanted memory leaks.
    1001             :  */
    1002             : PartitionBoundInfo
    1003       14766 : partition_bounds_copy(PartitionBoundInfo src,
    1004             :                       PartitionKey key)
    1005             : {
    1006             :     PartitionBoundInfo dest;
    1007             :     int         i;
    1008             :     int         ndatums;
    1009             :     int         nindexes;
    1010             :     int         partnatts;
    1011             :     bool        hash_part;
    1012             :     int         natts;
    1013             :     Datum      *boundDatums;
    1014             : 
    1015       14766 :     dest = (PartitionBoundInfo) palloc(sizeof(PartitionBoundInfoData));
    1016             : 
    1017       14766 :     dest->strategy = src->strategy;
    1018       14766 :     ndatums = dest->ndatums = src->ndatums;
    1019       14766 :     nindexes = dest->nindexes = src->nindexes;
    1020       14766 :     partnatts = key->partnatts;
    1021             : 
    1022             :     /* List partitioned tables have only a single partition key. */
    1023             :     Assert(key->strategy != PARTITION_STRATEGY_LIST || partnatts == 1);
    1024             : 
    1025       14766 :     dest->datums = (Datum **) palloc(sizeof(Datum *) * ndatums);
    1026             : 
    1027       14766 :     if (src->kind != NULL)
    1028             :     {
    1029             :         PartitionRangeDatumKind *boundKinds;
    1030             : 
    1031             :         /* only RANGE partition should have a non-NULL kind */
    1032             :         Assert(key->strategy == PARTITION_STRATEGY_RANGE);
    1033             : 
    1034        6614 :         dest->kind = (PartitionRangeDatumKind **) palloc(ndatums *
    1035             :                                                          sizeof(PartitionRangeDatumKind *));
    1036             : 
    1037             :         /*
    1038             :          * In the loop below, to save from allocating a series of small arrays
    1039             :          * for storing the PartitionRangeDatumKind, we allocate a single chunk
    1040             :          * here and use a smaller portion of it for each datum.
    1041             :          */
    1042        6614 :         boundKinds = (PartitionRangeDatumKind *) palloc(ndatums * partnatts *
    1043             :                                                         sizeof(PartitionRangeDatumKind));
    1044             : 
    1045       26680 :         for (i = 0; i < ndatums; i++)
    1046             :         {
    1047       20066 :             dest->kind[i] = &boundKinds[i * partnatts];
    1048       20066 :             memcpy(dest->kind[i], src->kind[i],
    1049             :                    sizeof(PartitionRangeDatumKind) * partnatts);
    1050             :         }
    1051             :     }
    1052             :     else
    1053        8152 :         dest->kind = NULL;
    1054             : 
    1055             :     /* copy interleaved partitions for LIST partitioned tables */
    1056       14766 :     dest->interleaved_parts = bms_copy(src->interleaved_parts);
    1057             : 
    1058             :     /*
    1059             :      * For hash partitioning, datums array will have two elements - modulus
    1060             :      * and remainder.
    1061             :      */
    1062       14766 :     hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
    1063       14766 :     natts = hash_part ? 2 : partnatts;
    1064       14766 :     boundDatums = palloc(ndatums * natts * sizeof(Datum));
    1065             : 
    1066       56528 :     for (i = 0; i < ndatums; i++)
    1067             :     {
    1068             :         int         j;
    1069             : 
    1070       41762 :         dest->datums[i] = &boundDatums[i * natts];
    1071             : 
    1072       90604 :         for (j = 0; j < natts; j++)
    1073             :         {
    1074             :             bool        byval;
    1075             :             int         typlen;
    1076             : 
    1077       48842 :             if (hash_part)
    1078             :             {
    1079        3104 :                 typlen = sizeof(int32); /* Always int4 */
    1080        3104 :                 byval = true;   /* int4 is pass-by-value */
    1081             :             }
    1082             :             else
    1083             :             {
    1084       45738 :                 byval = key->parttypbyval[j];
    1085       45738 :                 typlen = key->parttyplen[j];
    1086             :             }
    1087             : 
    1088       48842 :             if (dest->kind == NULL ||
    1089       25594 :                 dest->kind[i][j] == PARTITION_RANGE_DATUM_VALUE)
    1090       46356 :                 dest->datums[i][j] = datumCopy(src->datums[i][j],
    1091             :                                                byval, typlen);
    1092             :         }
    1093             :     }
    1094             : 
    1095       14766 :     dest->indexes = (int *) palloc(sizeof(int) * nindexes);
    1096       14766 :     memcpy(dest->indexes, src->indexes, sizeof(int) * nindexes);
    1097             : 
    1098       14766 :     dest->null_index = src->null_index;
    1099       14766 :     dest->default_index = src->default_index;
    1100             : 
    1101       14766 :     return dest;
    1102             : }
    1103             : 
    1104             : /*
    1105             :  * partition_bounds_merge
    1106             :  *      Check to see whether every partition of 'outer_rel' matches/overlaps
    1107             :  *      one partition of 'inner_rel' at most, and vice versa; and if so, build
    1108             :  *      and return the partition bounds for a join relation between the rels,
    1109             :  *      generating two lists of the matching/overlapping partitions, which are
    1110             :  *      returned to *outer_parts and *inner_parts respectively.
    1111             :  *
    1112             :  * The lists contain the same number of partitions, and the partitions at the
    1113             :  * same positions in the lists indicate join pairs used for partitioned join.
    1114             :  * If a partition on one side matches/overlaps multiple partitions on the other
    1115             :  * side, this function returns NULL, setting *outer_parts and *inner_parts to
    1116             :  * NIL.
    1117             :  */
    1118             : PartitionBoundInfo
    1119         846 : partition_bounds_merge(int partnatts,
    1120             :                        FmgrInfo *partsupfunc, Oid *partcollation,
    1121             :                        RelOptInfo *outer_rel, RelOptInfo *inner_rel,
    1122             :                        JoinType jointype,
    1123             :                        List **outer_parts, List **inner_parts)
    1124             : {
    1125             :     /*
    1126             :      * Currently, this function is called only from try_partitionwise_join(),
    1127             :      * so the join type should be INNER, LEFT, FULL, SEMI, or ANTI.
    1128             :      */
    1129             :     Assert(jointype == JOIN_INNER || jointype == JOIN_LEFT ||
    1130             :            jointype == JOIN_FULL || jointype == JOIN_SEMI ||
    1131             :            jointype == JOIN_ANTI);
    1132             : 
    1133             :     /* The partitioning strategies should be the same. */
    1134             :     Assert(outer_rel->boundinfo->strategy == inner_rel->boundinfo->strategy);
    1135             : 
    1136         846 :     *outer_parts = *inner_parts = NIL;
    1137         846 :     switch (outer_rel->boundinfo->strategy)
    1138             :     {
    1139           0 :         case PARTITION_STRATEGY_HASH:
    1140             : 
    1141             :             /*
    1142             :              * For hash partitioned tables, we currently support partitioned
    1143             :              * join only when they have exactly the same partition bounds.
    1144             :              *
    1145             :              * XXX: it might be possible to relax the restriction to support
    1146             :              * cases where hash partitioned tables have missing partitions
    1147             :              * and/or different moduli, but it's not clear if it would be
    1148             :              * useful to support the former case since it's unusual to have
    1149             :              * missing partitions.  On the other hand, it would be useful to
    1150             :              * support the latter case, but in that case, there is a high
    1151             :              * probability that a partition on one side will match multiple
    1152             :              * partitions on the other side, which is the scenario the current
    1153             :              * implementation of partitioned join can't handle.
    1154             :              */
    1155           0 :             return NULL;
    1156             : 
    1157         486 :         case PARTITION_STRATEGY_LIST:
    1158         486 :             return merge_list_bounds(partsupfunc,
    1159             :                                      partcollation,
    1160             :                                      outer_rel,
    1161             :                                      inner_rel,
    1162             :                                      jointype,
    1163             :                                      outer_parts,
    1164             :                                      inner_parts);
    1165             : 
    1166         360 :         case PARTITION_STRATEGY_RANGE:
    1167         360 :             return merge_range_bounds(partnatts,
    1168             :                                       partsupfunc,
    1169             :                                       partcollation,
    1170             :                                       outer_rel,
    1171             :                                       inner_rel,
    1172             :                                       jointype,
    1173             :                                       outer_parts,
    1174             :                                       inner_parts);
    1175             :     }
    1176             : 
    1177           0 :     return NULL;
    1178             : }
    1179             : 
    1180             : /*
    1181             :  * merge_list_bounds
    1182             :  *      Create the partition bounds for a join relation between list
    1183             :  *      partitioned tables, if possible
    1184             :  *
    1185             :  * In this function we try to find sets of matching partitions from both sides
    1186             :  * by comparing list values stored in their partition bounds.  Since the list
    1187             :  * values appear in the ascending order, an algorithm similar to merge join is
    1188             :  * used for that.  If a partition on one side doesn't have a matching
    1189             :  * partition on the other side, the algorithm tries to match it with the
    1190             :  * default partition on the other side if any; if not, the algorithm tries to
    1191             :  * match it with a dummy partition on the other side if it's on the
    1192             :  * non-nullable side of an outer join.  Also, if both sides have the default
    1193             :  * partitions, the algorithm tries to match them with each other.  We give up
    1194             :  * if the algorithm finds a partition matching multiple partitions on the
    1195             :  * other side, which is the scenario the current implementation of partitioned
    1196             :  * join can't handle.
    1197             :  */
    1198             : static PartitionBoundInfo
    1199         486 : merge_list_bounds(FmgrInfo *partsupfunc, Oid *partcollation,
    1200             :                   RelOptInfo *outer_rel, RelOptInfo *inner_rel,
    1201             :                   JoinType jointype,
    1202             :                   List **outer_parts, List **inner_parts)
    1203             : {
    1204         486 :     PartitionBoundInfo merged_bounds = NULL;
    1205         486 :     PartitionBoundInfo outer_bi = outer_rel->boundinfo;
    1206         486 :     PartitionBoundInfo inner_bi = inner_rel->boundinfo;
    1207         486 :     bool        outer_has_default = partition_bound_has_default(outer_bi);
    1208         486 :     bool        inner_has_default = partition_bound_has_default(inner_bi);
    1209         486 :     int         outer_default = outer_bi->default_index;
    1210         486 :     int         inner_default = inner_bi->default_index;
    1211         486 :     bool        outer_has_null = partition_bound_accepts_nulls(outer_bi);
    1212         486 :     bool        inner_has_null = partition_bound_accepts_nulls(inner_bi);
    1213             :     PartitionMap outer_map;
    1214             :     PartitionMap inner_map;
    1215             :     int         outer_pos;
    1216             :     int         inner_pos;
    1217         486 :     int         next_index = 0;
    1218         486 :     int         null_index = -1;
    1219         486 :     int         default_index = -1;
    1220         486 :     List       *merged_datums = NIL;
    1221         486 :     List       *merged_indexes = NIL;
    1222             : 
    1223             :     Assert(*outer_parts == NIL);
    1224             :     Assert(*inner_parts == NIL);
    1225             :     Assert(outer_bi->strategy == inner_bi->strategy &&
    1226             :            outer_bi->strategy == PARTITION_STRATEGY_LIST);
    1227             :     /* List partitioning doesn't require kinds. */
    1228             :     Assert(!outer_bi->kind && !inner_bi->kind);
    1229             : 
    1230         486 :     init_partition_map(outer_rel, &outer_map);
    1231         486 :     init_partition_map(inner_rel, &inner_map);
    1232             : 
    1233             :     /*
    1234             :      * If the default partitions (if any) have been proven empty, deem them
    1235             :      * non-existent.
    1236             :      */
    1237         486 :     if (outer_has_default && is_dummy_partition(outer_rel, outer_default))
    1238          24 :         outer_has_default = false;
    1239         486 :     if (inner_has_default && is_dummy_partition(inner_rel, inner_default))
    1240           0 :         inner_has_default = false;
    1241             : 
    1242             :     /*
    1243             :      * Merge partitions from both sides.  In each iteration we compare a pair
    1244             :      * of list values, one from each side, and decide whether the
    1245             :      * corresponding partitions match or not.  If the two values match
    1246             :      * exactly, move to the next pair of list values, otherwise move to the
    1247             :      * next list value on the side with a smaller list value.
    1248             :      */
    1249         486 :     outer_pos = inner_pos = 0;
    1250        3822 :     while (outer_pos < outer_bi->ndatums || inner_pos < inner_bi->ndatums)
    1251             :     {
    1252        3384 :         int         outer_index = -1;
    1253        3384 :         int         inner_index = -1;
    1254             :         Datum      *outer_datums;
    1255             :         Datum      *inner_datums;
    1256             :         int         cmpval;
    1257        3384 :         Datum      *merged_datum = NULL;
    1258        3384 :         int         merged_index = -1;
    1259             : 
    1260        3384 :         if (outer_pos < outer_bi->ndatums)
    1261             :         {
    1262             :             /*
    1263             :              * If the partition on the outer side has been proven empty,
    1264             :              * ignore it and move to the next datum on the outer side.
    1265             :              */
    1266        3336 :             outer_index = outer_bi->indexes[outer_pos];
    1267        3336 :             if (is_dummy_partition(outer_rel, outer_index))
    1268             :             {
    1269         168 :                 outer_pos++;
    1270         168 :                 continue;
    1271             :             }
    1272             :         }
    1273        3216 :         if (inner_pos < inner_bi->ndatums)
    1274             :         {
    1275             :             /*
    1276             :              * If the partition on the inner side has been proven empty,
    1277             :              * ignore it and move to the next datum on the inner side.
    1278             :              */
    1279        3216 :             inner_index = inner_bi->indexes[inner_pos];
    1280        3216 :             if (is_dummy_partition(inner_rel, inner_index))
    1281             :             {
    1282           0 :                 inner_pos++;
    1283           0 :                 continue;
    1284             :             }
    1285             :         }
    1286             : 
    1287             :         /* Get the list values. */
    1288        6432 :         outer_datums = outer_pos < outer_bi->ndatums ?
    1289        3216 :             outer_bi->datums[outer_pos] : NULL;
    1290        6432 :         inner_datums = inner_pos < inner_bi->ndatums ?
    1291        3216 :             inner_bi->datums[inner_pos] : NULL;
    1292             : 
    1293             :         /*
    1294             :          * We run this loop till both sides finish.  This allows us to avoid
    1295             :          * duplicating code to handle the remaining values on the side which
    1296             :          * finishes later.  For that we set the comparison parameter cmpval in
    1297             :          * such a way that it appears as if the side which finishes earlier
    1298             :          * has an extra value higher than any other value on the unfinished
    1299             :          * side. That way we advance the values on the unfinished side till
    1300             :          * all of its values are exhausted.
    1301             :          */
    1302        3216 :         if (outer_pos >= outer_bi->ndatums)
    1303          48 :             cmpval = 1;
    1304        3168 :         else if (inner_pos >= inner_bi->ndatums)
    1305           0 :             cmpval = -1;
    1306             :         else
    1307             :         {
    1308             :             Assert(outer_datums != NULL && inner_datums != NULL);
    1309        3168 :             cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
    1310             :                                                      partcollation[0],
    1311             :                                                      outer_datums[0],
    1312             :                                                      inner_datums[0]));
    1313             :         }
    1314             : 
    1315        3216 :         if (cmpval == 0)
    1316             :         {
    1317             :             /* Two list values match exactly. */
    1318             :             Assert(outer_pos < outer_bi->ndatums);
    1319             :             Assert(inner_pos < inner_bi->ndatums);
    1320             :             Assert(outer_index >= 0);
    1321             :             Assert(inner_index >= 0);
    1322             : 
    1323             :             /*
    1324             :              * Try merging both partitions.  If successful, add the list value
    1325             :              * and index of the merged partition below.
    1326             :              */
    1327        1644 :             merged_index = merge_matching_partitions(&outer_map, &inner_map,
    1328             :                                                      outer_index, inner_index,
    1329             :                                                      &next_index);
    1330        1644 :             if (merged_index == -1)
    1331          30 :                 goto cleanup;
    1332             : 
    1333        1614 :             merged_datum = outer_datums;
    1334             : 
    1335             :             /* Move to the next pair of list values. */
    1336        1614 :             outer_pos++;
    1337        1614 :             inner_pos++;
    1338             :         }
    1339        1572 :         else if (cmpval < 0)
    1340             :         {
    1341             :             /* A list value missing from the inner side. */
    1342             :             Assert(outer_pos < outer_bi->ndatums);
    1343             : 
    1344             :             /*
    1345             :              * If the inner side has the default partition, or this is an
    1346             :              * outer join, try to assign a merged partition to the outer
    1347             :              * partition (see process_outer_partition()).  Otherwise, the
    1348             :              * outer partition will not contribute to the result.
    1349             :              */
    1350         636 :             if (inner_has_default || IS_OUTER_JOIN(jointype))
    1351             :             {
    1352             :                 /* Get the outer partition. */
    1353         408 :                 outer_index = outer_bi->indexes[outer_pos];
    1354             :                 Assert(outer_index >= 0);
    1355         408 :                 merged_index = process_outer_partition(&outer_map,
    1356             :                                                        &inner_map,
    1357             :                                                        outer_has_default,
    1358             :                                                        inner_has_default,
    1359             :                                                        outer_index,
    1360             :                                                        inner_default,
    1361             :                                                        jointype,
    1362             :                                                        &next_index,
    1363             :                                                        &default_index);
    1364         408 :                 if (merged_index == -1)
    1365           6 :                     goto cleanup;
    1366         402 :                 merged_datum = outer_datums;
    1367             :             }
    1368             : 
    1369             :             /* Move to the next list value on the outer side. */
    1370         630 :             outer_pos++;
    1371             :         }
    1372             :         else
    1373             :         {
    1374             :             /* A list value missing from the outer side. */
    1375             :             Assert(cmpval > 0);
    1376             :             Assert(inner_pos < inner_bi->ndatums);
    1377             : 
    1378             :             /*
    1379             :              * If the outer side has the default partition, or this is a FULL
    1380             :              * join, try to assign a merged partition to the inner partition
    1381             :              * (see process_inner_partition()).  Otherwise, the inner
    1382             :              * partition will not contribute to the result.
    1383             :              */
    1384         936 :             if (outer_has_default || jointype == JOIN_FULL)
    1385             :             {
    1386             :                 /* Get the inner partition. */
    1387         252 :                 inner_index = inner_bi->indexes[inner_pos];
    1388             :                 Assert(inner_index >= 0);
    1389         252 :                 merged_index = process_inner_partition(&outer_map,
    1390             :                                                        &inner_map,
    1391             :                                                        outer_has_default,
    1392             :                                                        inner_has_default,
    1393             :                                                        inner_index,
    1394             :                                                        outer_default,
    1395             :                                                        jointype,
    1396             :                                                        &next_index,
    1397             :                                                        &default_index);
    1398         252 :                 if (merged_index == -1)
    1399          12 :                     goto cleanup;
    1400         240 :                 merged_datum = inner_datums;
    1401             :             }
    1402             : 
    1403             :             /* Move to the next list value on the inner side. */
    1404         924 :             inner_pos++;
    1405             :         }
    1406             : 
    1407             :         /*
    1408             :          * If we assigned a merged partition, add the list value and index of
    1409             :          * the merged partition if appropriate.
    1410             :          */
    1411        3168 :         if (merged_index >= 0 && merged_index != default_index)
    1412             :         {
    1413        2184 :             merged_datums = lappend(merged_datums, merged_datum);
    1414        2184 :             merged_indexes = lappend_int(merged_indexes, merged_index);
    1415             :         }
    1416             :     }
    1417             : 
    1418             :     /*
    1419             :      * If the NULL partitions (if any) have been proven empty, deem them
    1420             :      * non-existent.
    1421             :      */
    1422         630 :     if (outer_has_null &&
    1423         192 :         is_dummy_partition(outer_rel, outer_bi->null_index))
    1424           0 :         outer_has_null = false;
    1425         630 :     if (inner_has_null &&
    1426         192 :         is_dummy_partition(inner_rel, inner_bi->null_index))
    1427           0 :         inner_has_null = false;
    1428             : 
    1429             :     /* Merge the NULL partitions if any. */
    1430         438 :     if (outer_has_null || inner_has_null)
    1431         216 :         merge_null_partitions(&outer_map, &inner_map,
    1432             :                               outer_has_null, inner_has_null,
    1433             :                               outer_bi->null_index, inner_bi->null_index,
    1434             :                               jointype, &next_index, &null_index);
    1435             :     else
    1436             :         Assert(null_index == -1);
    1437             : 
    1438             :     /* Merge the default partitions if any. */
    1439         438 :     if (outer_has_default || inner_has_default)
    1440          96 :         merge_default_partitions(&outer_map, &inner_map,
    1441             :                                  outer_has_default, inner_has_default,
    1442             :                                  outer_default, inner_default,
    1443             :                                  jointype, &next_index, &default_index);
    1444             :     else
    1445             :         Assert(default_index == -1);
    1446             : 
    1447             :     /* If we have merged partitions, create the partition bounds. */
    1448         438 :     if (next_index > 0)
    1449             :     {
    1450             :         /* Fix the merged_indexes list if necessary. */
    1451         438 :         if (outer_map.did_remapping || inner_map.did_remapping)
    1452             :         {
    1453             :             Assert(jointype == JOIN_FULL);
    1454          48 :             fix_merged_indexes(&outer_map, &inner_map,
    1455             :                                next_index, merged_indexes);
    1456             :         }
    1457             : 
    1458             :         /* Use maps to match partitions from inputs. */
    1459         438 :         generate_matching_part_pairs(outer_rel, inner_rel,
    1460             :                                      &outer_map, &inner_map,
    1461             :                                      next_index,
    1462             :                                      outer_parts, inner_parts);
    1463             :         Assert(*outer_parts != NIL);
    1464             :         Assert(*inner_parts != NIL);
    1465             :         Assert(list_length(*outer_parts) == list_length(*inner_parts));
    1466             :         Assert(list_length(*outer_parts) <= next_index);
    1467             : 
    1468             :         /* Make a PartitionBoundInfo struct to return. */
    1469         438 :         merged_bounds = build_merged_partition_bounds(outer_bi->strategy,
    1470             :                                                       merged_datums,
    1471             :                                                       NIL,
    1472             :                                                       merged_indexes,
    1473             :                                                       null_index,
    1474             :                                                       default_index);
    1475             :         Assert(merged_bounds);
    1476             :     }
    1477             : 
    1478           0 : cleanup:
    1479             :     /* Free local memory before returning. */
    1480         486 :     list_free(merged_datums);
    1481         486 :     list_free(merged_indexes);
    1482         486 :     free_partition_map(&outer_map);
    1483         486 :     free_partition_map(&inner_map);
    1484             : 
    1485         486 :     return merged_bounds;
    1486             : }
    1487             : 
    1488             : /*
    1489             :  * merge_range_bounds
    1490             :  *      Create the partition bounds for a join relation between range
    1491             :  *      partitioned tables, if possible
    1492             :  *
    1493             :  * In this function we try to find sets of overlapping partitions from both
    1494             :  * sides by comparing ranges stored in their partition bounds.  Since the
    1495             :  * ranges appear in the ascending order, an algorithm similar to merge join is
    1496             :  * used for that.  If a partition on one side doesn't have an overlapping
    1497             :  * partition on the other side, the algorithm tries to match it with the
    1498             :  * default partition on the other side if any; if not, the algorithm tries to
    1499             :  * match it with a dummy partition on the other side if it's on the
    1500             :  * non-nullable side of an outer join.  Also, if both sides have the default
    1501             :  * partitions, the algorithm tries to match them with each other.  We give up
    1502             :  * if the algorithm finds a partition overlapping multiple partitions on the
    1503             :  * other side, which is the scenario the current implementation of partitioned
    1504             :  * join can't handle.
    1505             :  */
    1506             : static PartitionBoundInfo
    1507         360 : merge_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
    1508             :                    Oid *partcollations,
    1509             :                    RelOptInfo *outer_rel, RelOptInfo *inner_rel,
    1510             :                    JoinType jointype,
    1511             :                    List **outer_parts, List **inner_parts)
    1512             : {
    1513         360 :     PartitionBoundInfo merged_bounds = NULL;
    1514         360 :     PartitionBoundInfo outer_bi = outer_rel->boundinfo;
    1515         360 :     PartitionBoundInfo inner_bi = inner_rel->boundinfo;
    1516         360 :     bool        outer_has_default = partition_bound_has_default(outer_bi);
    1517         360 :     bool        inner_has_default = partition_bound_has_default(inner_bi);
    1518         360 :     int         outer_default = outer_bi->default_index;
    1519         360 :     int         inner_default = inner_bi->default_index;
    1520             :     PartitionMap outer_map;
    1521             :     PartitionMap inner_map;
    1522             :     int         outer_index;
    1523             :     int         inner_index;
    1524             :     int         outer_lb_pos;
    1525             :     int         inner_lb_pos;
    1526             :     PartitionRangeBound outer_lb;
    1527             :     PartitionRangeBound outer_ub;
    1528             :     PartitionRangeBound inner_lb;
    1529             :     PartitionRangeBound inner_ub;
    1530         360 :     int         next_index = 0;
    1531         360 :     int         default_index = -1;
    1532         360 :     List       *merged_datums = NIL;
    1533         360 :     List       *merged_kinds = NIL;
    1534         360 :     List       *merged_indexes = NIL;
    1535             : 
    1536             :     Assert(*outer_parts == NIL);
    1537             :     Assert(*inner_parts == NIL);
    1538             :     Assert(outer_bi->strategy == inner_bi->strategy &&
    1539             :            outer_bi->strategy == PARTITION_STRATEGY_RANGE);
    1540             : 
    1541         360 :     init_partition_map(outer_rel, &outer_map);
    1542         360 :     init_partition_map(inner_rel, &inner_map);
    1543             : 
    1544             :     /*
    1545             :      * If the default partitions (if any) have been proven empty, deem them
    1546             :      * non-existent.
    1547             :      */
    1548         360 :     if (outer_has_default && is_dummy_partition(outer_rel, outer_default))
    1549          12 :         outer_has_default = false;
    1550         360 :     if (inner_has_default && is_dummy_partition(inner_rel, inner_default))
    1551           0 :         inner_has_default = false;
    1552             : 
    1553             :     /*
    1554             :      * Merge partitions from both sides.  In each iteration we compare a pair
    1555             :      * of ranges, one from each side, and decide whether the corresponding
    1556             :      * partitions match or not.  If the two ranges overlap, move to the next
    1557             :      * pair of ranges, otherwise move to the next range on the side with a
    1558             :      * lower range.  outer_lb_pos/inner_lb_pos keep track of the positions of
    1559             :      * lower bounds in the datums arrays in the outer/inner
    1560             :      * PartitionBoundInfos respectively.
    1561             :      */
    1562         360 :     outer_lb_pos = inner_lb_pos = 0;
    1563         360 :     outer_index = get_range_partition(outer_rel, outer_bi, &outer_lb_pos,
    1564             :                                       &outer_lb, &outer_ub);
    1565         360 :     inner_index = get_range_partition(inner_rel, inner_bi, &inner_lb_pos,
    1566             :                                       &inner_lb, &inner_ub);
    1567        1284 :     while (outer_index >= 0 || inner_index >= 0)
    1568             :     {
    1569             :         bool        overlap;
    1570             :         int         ub_cmpval;
    1571             :         int         lb_cmpval;
    1572         990 :         PartitionRangeBound merged_lb = {-1, NULL, NULL, true};
    1573         990 :         PartitionRangeBound merged_ub = {-1, NULL, NULL, false};
    1574         990 :         int         merged_index = -1;
    1575             : 
    1576             :         /*
    1577             :          * We run this loop till both sides finish.  This allows us to avoid
    1578             :          * duplicating code to handle the remaining ranges on the side which
    1579             :          * finishes later.  For that we set the comparison parameter cmpval in
    1580             :          * such a way that it appears as if the side which finishes earlier
    1581             :          * has an extra range higher than any other range on the unfinished
    1582             :          * side. That way we advance the ranges on the unfinished side till
    1583             :          * all of its ranges are exhausted.
    1584             :          */
    1585         990 :         if (outer_index == -1)
    1586             :         {
    1587          90 :             overlap = false;
    1588          90 :             lb_cmpval = 1;
    1589          90 :             ub_cmpval = 1;
    1590             :         }
    1591         900 :         else if (inner_index == -1)
    1592             :         {
    1593          36 :             overlap = false;
    1594          36 :             lb_cmpval = -1;
    1595          36 :             ub_cmpval = -1;
    1596             :         }
    1597             :         else
    1598         864 :             overlap = compare_range_partitions(partnatts, partsupfuncs,
    1599             :                                                partcollations,
    1600             :                                                &outer_lb, &outer_ub,
    1601             :                                                &inner_lb, &inner_ub,
    1602             :                                                &lb_cmpval, &ub_cmpval);
    1603             : 
    1604         990 :         if (overlap)
    1605             :         {
    1606             :             /* Two ranges overlap; form a join pair. */
    1607             : 
    1608             :             PartitionRangeBound save_outer_ub;
    1609             :             PartitionRangeBound save_inner_ub;
    1610             : 
    1611             :             /* Both partitions should not have been merged yet. */
    1612             :             Assert(outer_index >= 0);
    1613             :             Assert(outer_map.merged_indexes[outer_index] == -1 &&
    1614             :                    outer_map.merged[outer_index] == false);
    1615             :             Assert(inner_index >= 0);
    1616             :             Assert(inner_map.merged_indexes[inner_index] == -1 &&
    1617             :                    inner_map.merged[inner_index] == false);
    1618             : 
    1619             :             /*
    1620             :              * Get the index of the merged partition.  Both partitions aren't
    1621             :              * merged yet, so the partitions should be merged successfully.
    1622             :              */
    1623         828 :             merged_index = merge_matching_partitions(&outer_map, &inner_map,
    1624             :                                                      outer_index, inner_index,
    1625             :                                                      &next_index);
    1626             :             Assert(merged_index >= 0);
    1627             : 
    1628             :             /* Get the range bounds of the merged partition. */
    1629         828 :             get_merged_range_bounds(partnatts, partsupfuncs,
    1630             :                                     partcollations, jointype,
    1631             :                                     &outer_lb, &outer_ub,
    1632             :                                     &inner_lb, &inner_ub,
    1633             :                                     lb_cmpval, ub_cmpval,
    1634             :                                     &merged_lb, &merged_ub);
    1635             : 
    1636             :             /* Save the upper bounds of both partitions for use below. */
    1637         828 :             save_outer_ub = outer_ub;
    1638         828 :             save_inner_ub = inner_ub;
    1639             : 
    1640             :             /* Move to the next pair of ranges. */
    1641         828 :             outer_index = get_range_partition(outer_rel, outer_bi, &outer_lb_pos,
    1642             :                                               &outer_lb, &outer_ub);
    1643         828 :             inner_index = get_range_partition(inner_rel, inner_bi, &inner_lb_pos,
    1644             :                                               &inner_lb, &inner_ub);
    1645             : 
    1646             :             /*
    1647             :              * If the range of a partition on one side overlaps the range of
    1648             :              * the next partition on the other side, that will cause the
    1649             :              * partition on one side to match at least two partitions on the
    1650             :              * other side, which is the case that we currently don't support
    1651             :              * partitioned join for; give up.
    1652             :              */
    1653        1032 :             if (ub_cmpval > 0 && inner_index >= 0 &&
    1654         204 :                 compare_range_bounds(partnatts, partsupfuncs, partcollations,
    1655             :                                      &save_outer_ub, &inner_lb) > 0)
    1656          60 :                 goto cleanup;
    1657         858 :             if (ub_cmpval < 0 && outer_index >= 0 &&
    1658          66 :                 compare_range_bounds(partnatts, partsupfuncs, partcollations,
    1659             :                                      &outer_lb, &save_inner_ub) < 0)
    1660          18 :                 goto cleanup;
    1661             : 
    1662             :             /*
    1663             :              * A row from a non-overlapping portion (if any) of a partition on
    1664             :              * one side might find its join partner in the default partition
    1665             :              * (if any) on the other side, causing the same situation as
    1666             :              * above; give up in that case.
    1667             :              */
    1668         774 :             if ((outer_has_default && (lb_cmpval > 0 || ub_cmpval < 0)) ||
    1669          24 :                 (inner_has_default && (lb_cmpval < 0 || ub_cmpval > 0)))
    1670           6 :                 goto cleanup;
    1671             :         }
    1672         162 :         else if (ub_cmpval < 0)
    1673             :         {
    1674             :             /* A non-overlapping outer range. */
    1675             : 
    1676             :             /* The outer partition should not have been merged yet. */
    1677             :             Assert(outer_index >= 0);
    1678             :             Assert(outer_map.merged_indexes[outer_index] == -1 &&
    1679             :                    outer_map.merged[outer_index] == false);
    1680             : 
    1681             :             /*
    1682             :              * If the inner side has the default partition, or this is an
    1683             :              * outer join, try to assign a merged partition to the outer
    1684             :              * partition (see process_outer_partition()).  Otherwise, the
    1685             :              * outer partition will not contribute to the result.
    1686             :              */
    1687          36 :             if (inner_has_default || IS_OUTER_JOIN(jointype))
    1688             :             {
    1689          24 :                 merged_index = process_outer_partition(&outer_map,
    1690             :                                                        &inner_map,
    1691             :                                                        outer_has_default,
    1692             :                                                        inner_has_default,
    1693             :                                                        outer_index,
    1694             :                                                        inner_default,
    1695             :                                                        jointype,
    1696             :                                                        &next_index,
    1697             :                                                        &default_index);
    1698          24 :                 if (merged_index == -1)
    1699           0 :                     goto cleanup;
    1700          24 :                 merged_lb = outer_lb;
    1701          24 :                 merged_ub = outer_ub;
    1702             :             }
    1703             : 
    1704             :             /* Move to the next range on the outer side. */
    1705          36 :             outer_index = get_range_partition(outer_rel, outer_bi, &outer_lb_pos,
    1706             :                                               &outer_lb, &outer_ub);
    1707             :         }
    1708             :         else
    1709             :         {
    1710             :             /* A non-overlapping inner range. */
    1711             :             Assert(ub_cmpval > 0);
    1712             : 
    1713             :             /* The inner partition should not have been merged yet. */
    1714             :             Assert(inner_index >= 0);
    1715             :             Assert(inner_map.merged_indexes[inner_index] == -1 &&
    1716             :                    inner_map.merged[inner_index] == false);
    1717             : 
    1718             :             /*
    1719             :              * If the outer side has the default partition, or this is a FULL
    1720             :              * join, try to assign a merged partition to the inner partition
    1721             :              * (see process_inner_partition()).  Otherwise, the inner
    1722             :              * partition will not contribute to the result.
    1723             :              */
    1724         126 :             if (outer_has_default || jointype == JOIN_FULL)
    1725             :             {
    1726          66 :                 merged_index = process_inner_partition(&outer_map,
    1727             :                                                        &inner_map,
    1728             :                                                        outer_has_default,
    1729             :                                                        inner_has_default,
    1730             :                                                        inner_index,
    1731             :                                                        outer_default,
    1732             :                                                        jointype,
    1733             :                                                        &next_index,
    1734             :                                                        &default_index);
    1735          66 :                 if (merged_index == -1)
    1736           6 :                     goto cleanup;
    1737          60 :                 merged_lb = inner_lb;
    1738          60 :                 merged_ub = inner_ub;
    1739             :             }
    1740             : 
    1741             :             /* Move to the next range on the inner side. */
    1742         120 :             inner_index = get_range_partition(inner_rel, inner_bi, &inner_lb_pos,
    1743             :                                               &inner_lb, &inner_ub);
    1744             :         }
    1745             : 
    1746             :         /*
    1747             :          * If we assigned a merged partition, add the range bounds and index
    1748             :          * of the merged partition if appropriate.
    1749             :          */
    1750         924 :         if (merged_index >= 0 && merged_index != default_index)
    1751         816 :             add_merged_range_bounds(partnatts, partsupfuncs, partcollations,
    1752             :                                     &merged_lb, &merged_ub, merged_index,
    1753             :                                     &merged_datums, &merged_kinds,
    1754             :                                     &merged_indexes);
    1755             :     }
    1756             : 
    1757             :     /* Merge the default partitions if any. */
    1758         294 :     if (outer_has_default || inner_has_default)
    1759          60 :         merge_default_partitions(&outer_map, &inner_map,
    1760             :                                  outer_has_default, inner_has_default,
    1761             :                                  outer_default, inner_default,
    1762             :                                  jointype, &next_index, &default_index);
    1763             :     else
    1764             :         Assert(default_index == -1);
    1765             : 
    1766             :     /* If we have merged partitions, create the partition bounds. */
    1767         294 :     if (next_index > 0)
    1768             :     {
    1769             :         /*
    1770             :          * Unlike the case of list partitioning, we wouldn't have re-merged
    1771             :          * partitions, so did_remapping should be left alone.
    1772             :          */
    1773             :         Assert(!outer_map.did_remapping);
    1774             :         Assert(!inner_map.did_remapping);
    1775             : 
    1776             :         /* Use maps to match partitions from inputs. */
    1777         294 :         generate_matching_part_pairs(outer_rel, inner_rel,
    1778             :                                      &outer_map, &inner_map,
    1779             :                                      next_index,
    1780             :                                      outer_parts, inner_parts);
    1781             :         Assert(*outer_parts != NIL);
    1782             :         Assert(*inner_parts != NIL);
    1783             :         Assert(list_length(*outer_parts) == list_length(*inner_parts));
    1784             :         Assert(list_length(*outer_parts) == next_index);
    1785             : 
    1786             :         /* Make a PartitionBoundInfo struct to return. */
    1787         294 :         merged_bounds = build_merged_partition_bounds(outer_bi->strategy,
    1788             :                                                       merged_datums,
    1789             :                                                       merged_kinds,
    1790             :                                                       merged_indexes,
    1791             :                                                       -1,
    1792             :                                                       default_index);
    1793             :         Assert(merged_bounds);
    1794             :     }
    1795             : 
    1796           0 : cleanup:
    1797             :     /* Free local memory before returning. */
    1798         360 :     list_free(merged_datums);
    1799         360 :     list_free(merged_kinds);
    1800         360 :     list_free(merged_indexes);
    1801         360 :     free_partition_map(&outer_map);
    1802         360 :     free_partition_map(&inner_map);
    1803             : 
    1804         360 :     return merged_bounds;
    1805             : }
    1806             : 
    1807             : /*
    1808             :  * init_partition_map
    1809             :  *      Initialize a PartitionMap struct for given relation
    1810             :  */
    1811             : static void
    1812        1692 : init_partition_map(RelOptInfo *rel, PartitionMap *map)
    1813             : {
    1814        1692 :     int         nparts = rel->nparts;
    1815             :     int         i;
    1816             : 
    1817        1692 :     map->nparts = nparts;
    1818        1692 :     map->merged_indexes = (int *) palloc(sizeof(int) * nparts);
    1819        1692 :     map->merged = (bool *) palloc(sizeof(bool) * nparts);
    1820        1692 :     map->did_remapping = false;
    1821        1692 :     map->old_indexes = (int *) palloc(sizeof(int) * nparts);
    1822        6870 :     for (i = 0; i < nparts; i++)
    1823             :     {
    1824        5178 :         map->merged_indexes[i] = map->old_indexes[i] = -1;
    1825        5178 :         map->merged[i] = false;
    1826             :     }
    1827        1692 : }
    1828             : 
    1829             : /*
    1830             :  * free_partition_map
    1831             :  */
    1832             : static void
    1833        1692 : free_partition_map(PartitionMap *map)
    1834             : {
    1835        1692 :     pfree(map->merged_indexes);
    1836        1692 :     pfree(map->merged);
    1837        1692 :     pfree(map->old_indexes);
    1838        1692 : }
    1839             : 
    1840             : /*
    1841             :  * is_dummy_partition --- has partition been proven empty?
    1842             :  */
    1843             : static bool
    1844        9144 : is_dummy_partition(RelOptInfo *rel, int part_index)
    1845             : {
    1846             :     RelOptInfo *part_rel;
    1847             : 
    1848             :     Assert(part_index >= 0);
    1849        9144 :     part_rel = rel->part_rels[part_index];
    1850        9144 :     if (part_rel == NULL || IS_DUMMY_REL(part_rel))
    1851         252 :         return true;
    1852        8892 :     return false;
    1853             : }
    1854             : 
    1855             : /*
    1856             :  * merge_matching_partitions
    1857             :  *      Try to merge given outer/inner partitions, and return the index of a
    1858             :  *      merged partition produced from them if successful, -1 otherwise
    1859             :  *
    1860             :  * If the merged partition is newly created, *next_index is incremented.
    1861             :  */
    1862             : static int
    1863        2718 : merge_matching_partitions(PartitionMap *outer_map, PartitionMap *inner_map,
    1864             :                           int outer_index, int inner_index, int *next_index)
    1865             : {
    1866             :     int         outer_merged_index;
    1867             :     int         inner_merged_index;
    1868             :     bool        outer_merged;
    1869             :     bool        inner_merged;
    1870             : 
    1871             :     Assert(outer_index >= 0 && outer_index < outer_map->nparts);
    1872        2718 :     outer_merged_index = outer_map->merged_indexes[outer_index];
    1873        2718 :     outer_merged = outer_map->merged[outer_index];
    1874             :     Assert(inner_index >= 0 && inner_index < inner_map->nparts);
    1875        2718 :     inner_merged_index = inner_map->merged_indexes[inner_index];
    1876        2718 :     inner_merged = inner_map->merged[inner_index];
    1877             : 
    1878             :     /*
    1879             :      * Handle cases where we have already assigned a merged partition to each
    1880             :      * of the given partitions.
    1881             :      */
    1882        2718 :     if (outer_merged_index >= 0 && inner_merged_index >= 0)
    1883             :     {
    1884             :         /*
    1885             :          * If the merged partitions are the same, no need to do anything;
    1886             :          * return the index of the merged partitions.  Otherwise, if each of
    1887             :          * the given partitions has been merged with a dummy partition on the
    1888             :          * other side, re-map them to either of the two merged partitions.
    1889             :          * Otherwise, they can't be merged, so return -1.
    1890             :          */
    1891         660 :         if (outer_merged_index == inner_merged_index)
    1892             :         {
    1893             :             Assert(outer_merged);
    1894             :             Assert(inner_merged);
    1895         552 :             return outer_merged_index;
    1896             :         }
    1897         108 :         if (!outer_merged && !inner_merged)
    1898             :         {
    1899             :             /*
    1900             :              * This can only happen for a list-partitioning case.  We re-map
    1901             :              * them to the merged partition with the smaller of the two merged
    1902             :              * indexes to preserve the property that the canonical order of
    1903             :              * list partitions is determined by the indexes assigned to the
    1904             :              * smallest list value of each partition.
    1905             :              */
    1906         102 :             if (outer_merged_index < inner_merged_index)
    1907             :             {
    1908          54 :                 outer_map->merged[outer_index] = true;
    1909          54 :                 inner_map->merged_indexes[inner_index] = outer_merged_index;
    1910          54 :                 inner_map->merged[inner_index] = true;
    1911          54 :                 inner_map->did_remapping = true;
    1912          54 :                 inner_map->old_indexes[inner_index] = inner_merged_index;
    1913          54 :                 return outer_merged_index;
    1914             :             }
    1915             :             else
    1916             :             {
    1917          48 :                 inner_map->merged[inner_index] = true;
    1918          48 :                 outer_map->merged_indexes[outer_index] = inner_merged_index;
    1919          48 :                 outer_map->merged[outer_index] = true;
    1920          48 :                 outer_map->did_remapping = true;
    1921          48 :                 outer_map->old_indexes[outer_index] = outer_merged_index;
    1922          48 :                 return inner_merged_index;
    1923             :             }
    1924             :         }
    1925           6 :         return -1;
    1926             :     }
    1927             : 
    1928             :     /* At least one of the given partitions should not have yet been merged. */
    1929             :     Assert(outer_merged_index == -1 || inner_merged_index == -1);
    1930             : 
    1931             :     /*
    1932             :      * If neither of them has been merged, merge them.  Otherwise, if one has
    1933             :      * been merged with a dummy partition on the other side (and the other
    1934             :      * hasn't yet been merged with anything), re-merge them.  Otherwise, they
    1935             :      * can't be merged, so return -1.
    1936             :      */
    1937        2058 :     if (outer_merged_index == -1 && inner_merged_index == -1)
    1938             :     {
    1939        1758 :         int         merged_index = *next_index;
    1940             : 
    1941             :         Assert(!outer_merged);
    1942             :         Assert(!inner_merged);
    1943        1758 :         outer_map->merged_indexes[outer_index] = merged_index;
    1944        1758 :         outer_map->merged[outer_index] = true;
    1945        1758 :         inner_map->merged_indexes[inner_index] = merged_index;
    1946        1758 :         inner_map->merged[inner_index] = true;
    1947        1758 :         *next_index = *next_index + 1;
    1948        1758 :         return merged_index;
    1949             :     }
    1950         300 :     if (outer_merged_index >= 0 && !outer_map->merged[outer_index])
    1951             :     {
    1952             :         Assert(inner_merged_index == -1);
    1953             :         Assert(!inner_merged);
    1954         264 :         inner_map->merged_indexes[inner_index] = outer_merged_index;
    1955         264 :         inner_map->merged[inner_index] = true;
    1956         264 :         outer_map->merged[outer_index] = true;
    1957         264 :         return outer_merged_index;
    1958             :     }
    1959          36 :     if (inner_merged_index >= 0 && !inner_map->merged[inner_index])
    1960             :     {
    1961             :         Assert(outer_merged_index == -1);
    1962             :         Assert(!outer_merged);
    1963           0 :         outer_map->merged_indexes[outer_index] = inner_merged_index;
    1964           0 :         outer_map->merged[outer_index] = true;
    1965           0 :         inner_map->merged[inner_index] = true;
    1966           0 :         return inner_merged_index;
    1967             :     }
    1968          36 :     return -1;
    1969             : }
    1970             : 
    1971             : /*
    1972             :  * process_outer_partition
    1973             :  *      Try to assign given outer partition a merged partition, and return the
    1974             :  *      index of the merged partition if successful, -1 otherwise
    1975             :  *
    1976             :  * If the partition is newly created, *next_index is incremented.  Also, if it
    1977             :  * is the default partition of the join relation, *default_index is set to the
    1978             :  * index if not already done.
    1979             :  */
    1980             : static int
    1981         432 : process_outer_partition(PartitionMap *outer_map,
    1982             :                         PartitionMap *inner_map,
    1983             :                         bool outer_has_default,
    1984             :                         bool inner_has_default,
    1985             :                         int outer_index,
    1986             :                         int inner_default,
    1987             :                         JoinType jointype,
    1988             :                         int *next_index,
    1989             :                         int *default_index)
    1990             : {
    1991         432 :     int         merged_index = -1;
    1992             : 
    1993             :     Assert(outer_index >= 0);
    1994             : 
    1995             :     /*
    1996             :      * If the inner side has the default partition, a row from the outer
    1997             :      * partition might find its join partner in the default partition; try
    1998             :      * merging the outer partition with the default partition.  Otherwise,
    1999             :      * this should be an outer join, in which case the outer partition has to
    2000             :      * be scanned all the way anyway; merge the outer partition with a dummy
    2001             :      * partition on the other side.
    2002             :      */
    2003         432 :     if (inner_has_default)
    2004             :     {
    2005             :         Assert(inner_default >= 0);
    2006             : 
    2007             :         /*
    2008             :          * If the outer side has the default partition as well, the default
    2009             :          * partition on the inner side will have two matching partitions on
    2010             :          * the other side: the outer partition and the default partition on
    2011             :          * the outer side.  Partitionwise join doesn't handle this scenario
    2012             :          * yet.
    2013             :          */
    2014           6 :         if (outer_has_default)
    2015           0 :             return -1;
    2016             : 
    2017           6 :         merged_index = merge_matching_partitions(outer_map, inner_map,
    2018             :                                                  outer_index, inner_default,
    2019             :                                                  next_index);
    2020           6 :         if (merged_index == -1)
    2021           6 :             return -1;
    2022             : 
    2023             :         /*
    2024             :          * If this is a FULL join, the default partition on the inner side has
    2025             :          * to be scanned all the way anyway, so the resulting partition will
    2026             :          * contain all key values from the default partition, which any other
    2027             :          * partition of the join relation will not contain.  Thus the
    2028             :          * resulting partition will act as the default partition of the join
    2029             :          * relation; record the index in *default_index if not already done.
    2030             :          */
    2031           0 :         if (jointype == JOIN_FULL)
    2032             :         {
    2033           0 :             if (*default_index == -1)
    2034           0 :                 *default_index = merged_index;
    2035             :             else
    2036             :                 Assert(*default_index == merged_index);
    2037             :         }
    2038             :     }
    2039             :     else
    2040             :     {
    2041             :         Assert(IS_OUTER_JOIN(jointype));
    2042             :         Assert(jointype != JOIN_RIGHT);
    2043             : 
    2044             :         /* If we have already assigned a partition, no need to do anything. */
    2045         426 :         merged_index = outer_map->merged_indexes[outer_index];
    2046         426 :         if (merged_index == -1)
    2047         402 :             merged_index = merge_partition_with_dummy(outer_map, outer_index,
    2048             :                                                       next_index);
    2049             :     }
    2050         426 :     return merged_index;
    2051             : }
    2052             : 
    2053             : /*
    2054             :  * process_inner_partition
    2055             :  *      Try to assign given inner partition a merged partition, and return the
    2056             :  *      index of the merged partition if successful, -1 otherwise
    2057             :  *
    2058             :  * If the partition is newly created, *next_index is incremented.  Also, if it
    2059             :  * is the default partition of the join relation, *default_index is set to the
    2060             :  * index if not already done.
    2061             :  */
    2062             : static int
    2063         318 : process_inner_partition(PartitionMap *outer_map,
    2064             :                         PartitionMap *inner_map,
    2065             :                         bool outer_has_default,
    2066             :                         bool inner_has_default,
    2067             :                         int inner_index,
    2068             :                         int outer_default,
    2069             :                         JoinType jointype,
    2070             :                         int *next_index,
    2071             :                         int *default_index)
    2072             : {
    2073         318 :     int         merged_index = -1;
    2074             : 
    2075             :     Assert(inner_index >= 0);
    2076             : 
    2077             :     /*
    2078             :      * If the outer side has the default partition, a row from the inner
    2079             :      * partition might find its join partner in the default partition; try
    2080             :      * merging the inner partition with the default partition.  Otherwise,
    2081             :      * this should be a FULL join, in which case the inner partition has to be
    2082             :      * scanned all the way anyway; merge the inner partition with a dummy
    2083             :      * partition on the other side.
    2084             :      */
    2085         318 :     if (outer_has_default)
    2086             :     {
    2087             :         Assert(outer_default >= 0);
    2088             : 
    2089             :         /*
    2090             :          * If the inner side has the default partition as well, the default
    2091             :          * partition on the outer side will have two matching partitions on
    2092             :          * the other side: the inner partition and the default partition on
    2093             :          * the inner side.  Partitionwise join doesn't handle this scenario
    2094             :          * yet.
    2095             :          */
    2096         204 :         if (inner_has_default)
    2097          12 :             return -1;
    2098             : 
    2099         192 :         merged_index = merge_matching_partitions(outer_map, inner_map,
    2100             :                                                  outer_default, inner_index,
    2101             :                                                  next_index);
    2102         192 :         if (merged_index == -1)
    2103           6 :             return -1;
    2104             : 
    2105             :         /*
    2106             :          * If this is an outer join, the default partition on the outer side
    2107             :          * has to be scanned all the way anyway, so the resulting partition
    2108             :          * will contain all key values from the default partition, which any
    2109             :          * other partition of the join relation will not contain.  Thus the
    2110             :          * resulting partition will act as the default partition of the join
    2111             :          * relation; record the index in *default_index if not already done.
    2112             :          */
    2113         186 :         if (IS_OUTER_JOIN(jointype))
    2114             :         {
    2115             :             Assert(jointype != JOIN_RIGHT);
    2116         108 :             if (*default_index == -1)
    2117          72 :                 *default_index = merged_index;
    2118             :             else
    2119             :                 Assert(*default_index == merged_index);
    2120             :         }
    2121             :     }
    2122             :     else
    2123             :     {
    2124             :         Assert(jointype == JOIN_FULL);
    2125             : 
    2126             :         /* If we have already assigned a partition, no need to do anything. */
    2127         114 :         merged_index = inner_map->merged_indexes[inner_index];
    2128         114 :         if (merged_index == -1)
    2129         114 :             merged_index = merge_partition_with_dummy(inner_map, inner_index,
    2130             :                                                       next_index);
    2131             :     }
    2132         300 :     return merged_index;
    2133             : }
    2134             : 
    2135             : /*
    2136             :  * merge_null_partitions
    2137             :  *      Merge the NULL partitions from a join's outer and inner sides.
    2138             :  *
    2139             :  * If the merged partition produced from them is the NULL partition of the join
    2140             :  * relation, *null_index is set to the index of the merged partition.
    2141             :  *
    2142             :  * Note: We assume here that the join clause for a partitioned join is strict
    2143             :  * because have_partkey_equi_join() requires that the corresponding operator
    2144             :  * be mergejoinable, and we currently assume that mergejoinable operators are
    2145             :  * strict (see MJEvalOuterValues()/MJEvalInnerValues()).
    2146             :  */
    2147             : static void
    2148         216 : merge_null_partitions(PartitionMap *outer_map,
    2149             :                       PartitionMap *inner_map,
    2150             :                       bool outer_has_null,
    2151             :                       bool inner_has_null,
    2152             :                       int outer_null,
    2153             :                       int inner_null,
    2154             :                       JoinType jointype,
    2155             :                       int *next_index,
    2156             :                       int *null_index)
    2157             : {
    2158         216 :     bool        consider_outer_null = false;
    2159         216 :     bool        consider_inner_null = false;
    2160             : 
    2161             :     Assert(outer_has_null || inner_has_null);
    2162             :     Assert(*null_index == -1);
    2163             : 
    2164             :     /*
    2165             :      * Check whether the NULL partitions have already been merged and if so,
    2166             :      * set the consider_outer_null/consider_inner_null flags.
    2167             :      */
    2168         216 :     if (outer_has_null)
    2169             :     {
    2170             :         Assert(outer_null >= 0 && outer_null < outer_map->nparts);
    2171         192 :         if (outer_map->merged_indexes[outer_null] == -1)
    2172          84 :             consider_outer_null = true;
    2173             :     }
    2174         216 :     if (inner_has_null)
    2175             :     {
    2176             :         Assert(inner_null >= 0 && inner_null < inner_map->nparts);
    2177         192 :         if (inner_map->merged_indexes[inner_null] == -1)
    2178         120 :             consider_inner_null = true;
    2179             :     }
    2180             : 
    2181             :     /* If both flags are set false, we don't need to do anything. */
    2182         216 :     if (!consider_outer_null && !consider_inner_null)
    2183          72 :         return;
    2184             : 
    2185         144 :     if (consider_outer_null && !consider_inner_null)
    2186             :     {
    2187             :         Assert(outer_has_null);
    2188             : 
    2189             :         /*
    2190             :          * If this is an outer join, the NULL partition on the outer side has
    2191             :          * to be scanned all the way anyway; merge the NULL partition with a
    2192             :          * dummy partition on the other side.  In that case
    2193             :          * consider_outer_null means that the NULL partition only contains
    2194             :          * NULL values as the key values, so the merged partition will do so;
    2195             :          * treat it as the NULL partition of the join relation.
    2196             :          */
    2197          24 :         if (IS_OUTER_JOIN(jointype))
    2198             :         {
    2199             :             Assert(jointype != JOIN_RIGHT);
    2200          12 :             *null_index = merge_partition_with_dummy(outer_map, outer_null,
    2201             :                                                      next_index);
    2202             :         }
    2203             :     }
    2204         120 :     else if (!consider_outer_null && consider_inner_null)
    2205             :     {
    2206             :         Assert(inner_has_null);
    2207             : 
    2208             :         /*
    2209             :          * If this is a FULL join, the NULL partition on the inner side has to
    2210             :          * be scanned all the way anyway; merge the NULL partition with a
    2211             :          * dummy partition on the other side.  In that case
    2212             :          * consider_inner_null means that the NULL partition only contains
    2213             :          * NULL values as the key values, so the merged partition will do so;
    2214             :          * treat it as the NULL partition of the join relation.
    2215             :          */
    2216          60 :         if (jointype == JOIN_FULL)
    2217           0 :             *null_index = merge_partition_with_dummy(inner_map, inner_null,
    2218             :                                                      next_index);
    2219             :     }
    2220             :     else
    2221             :     {
    2222             :         Assert(consider_outer_null && consider_inner_null);
    2223             :         Assert(outer_has_null);
    2224             :         Assert(inner_has_null);
    2225             : 
    2226             :         /*
    2227             :          * If this is an outer join, the NULL partition on the outer side (and
    2228             :          * that on the inner side if this is a FULL join) have to be scanned
    2229             :          * all the way anyway, so merge them.  Note that each of the NULL
    2230             :          * partitions isn't merged yet, so they should be merged successfully.
    2231             :          * Like the above, each of the NULL partitions only contains NULL
    2232             :          * values as the key values, so the merged partition will do so; treat
    2233             :          * it as the NULL partition of the join relation.
    2234             :          *
    2235             :          * Note: if this an INNER/SEMI join, the join clause will never be
    2236             :          * satisfied by two NULL values (see comments above), so both the NULL
    2237             :          * partitions can be eliminated.
    2238             :          */
    2239          60 :         if (IS_OUTER_JOIN(jointype))
    2240             :         {
    2241             :             Assert(jointype != JOIN_RIGHT);
    2242          48 :             *null_index = merge_matching_partitions(outer_map, inner_map,
    2243             :                                                     outer_null, inner_null,
    2244             :                                                     next_index);
    2245             :             Assert(*null_index >= 0);
    2246             :         }
    2247             :     }
    2248             : }
    2249             : 
    2250             : /*
    2251             :  * merge_default_partitions
    2252             :  *      Merge the default partitions from a join's outer and inner sides.
    2253             :  *
    2254             :  * If the merged partition produced from them is the default partition of the
    2255             :  * join relation, *default_index is set to the index of the merged partition.
    2256             :  */
    2257             : static void
    2258         156 : merge_default_partitions(PartitionMap *outer_map,
    2259             :                          PartitionMap *inner_map,
    2260             :                          bool outer_has_default,
    2261             :                          bool inner_has_default,
    2262             :                          int outer_default,
    2263             :                          int inner_default,
    2264             :                          JoinType jointype,
    2265             :                          int *next_index,
    2266             :                          int *default_index)
    2267             : {
    2268         156 :     int         outer_merged_index = -1;
    2269         156 :     int         inner_merged_index = -1;
    2270             : 
    2271             :     Assert(outer_has_default || inner_has_default);
    2272             : 
    2273             :     /* Get the merged partition indexes for the default partitions. */
    2274         156 :     if (outer_has_default)
    2275             :     {
    2276             :         Assert(outer_default >= 0 && outer_default < outer_map->nparts);
    2277         120 :         outer_merged_index = outer_map->merged_indexes[outer_default];
    2278             :     }
    2279         156 :     if (inner_has_default)
    2280             :     {
    2281             :         Assert(inner_default >= 0 && inner_default < inner_map->nparts);
    2282          36 :         inner_merged_index = inner_map->merged_indexes[inner_default];
    2283             :     }
    2284             : 
    2285         156 :     if (outer_has_default && !inner_has_default)
    2286             :     {
    2287             :         /*
    2288             :          * If this is an outer join, the default partition on the outer side
    2289             :          * has to be scanned all the way anyway; if we have not yet assigned a
    2290             :          * partition, merge the default partition with a dummy partition on
    2291             :          * the other side.  The merged partition will act as the default
    2292             :          * partition of the join relation (see comments in
    2293             :          * process_inner_partition()).
    2294             :          */
    2295         120 :         if (IS_OUTER_JOIN(jointype))
    2296             :         {
    2297             :             Assert(jointype != JOIN_RIGHT);
    2298          72 :             if (outer_merged_index == -1)
    2299             :             {
    2300             :                 Assert(*default_index == -1);
    2301           0 :                 *default_index = merge_partition_with_dummy(outer_map,
    2302             :                                                             outer_default,
    2303             :                                                             next_index);
    2304             :             }
    2305             :             else
    2306             :                 Assert(*default_index == outer_merged_index);
    2307             :         }
    2308             :         else
    2309             :             Assert(*default_index == -1);
    2310             :     }
    2311          36 :     else if (!outer_has_default && inner_has_default)
    2312             :     {
    2313             :         /*
    2314             :          * If this is a FULL join, the default partition on the inner side has
    2315             :          * to be scanned all the way anyway; if we have not yet assigned a
    2316             :          * partition, merge the default partition with a dummy partition on
    2317             :          * the other side.  The merged partition will act as the default
    2318             :          * partition of the join relation (see comments in
    2319             :          * process_outer_partition()).
    2320             :          */
    2321          36 :         if (jointype == JOIN_FULL)
    2322             :         {
    2323           0 :             if (inner_merged_index == -1)
    2324             :             {
    2325             :                 Assert(*default_index == -1);
    2326           0 :                 *default_index = merge_partition_with_dummy(inner_map,
    2327             :                                                             inner_default,
    2328             :                                                             next_index);
    2329             :             }
    2330             :             else
    2331             :                 Assert(*default_index == inner_merged_index);
    2332             :         }
    2333             :         else
    2334             :             Assert(*default_index == -1);
    2335             :     }
    2336             :     else
    2337             :     {
    2338             :         Assert(outer_has_default && inner_has_default);
    2339             : 
    2340             :         /*
    2341             :          * The default partitions have to be joined with each other, so merge
    2342             :          * them.  Note that each of the default partitions isn't merged yet
    2343             :          * (see, process_outer_partition()/process_inner_partition()), so they
    2344             :          * should be merged successfully.  The merged partition will act as
    2345             :          * the default partition of the join relation.
    2346             :          */
    2347             :         Assert(outer_merged_index == -1);
    2348             :         Assert(inner_merged_index == -1);
    2349             :         Assert(*default_index == -1);
    2350           0 :         *default_index = merge_matching_partitions(outer_map,
    2351             :                                                    inner_map,
    2352             :                                                    outer_default,
    2353             :                                                    inner_default,
    2354             :                                                    next_index);
    2355             :         Assert(*default_index >= 0);
    2356             :     }
    2357         156 : }
    2358             : 
    2359             : /*
    2360             :  * merge_partition_with_dummy
    2361             :  *      Assign given partition a new partition of a join relation
    2362             :  *
    2363             :  * Note: The caller assumes that the given partition doesn't have a non-dummy
    2364             :  * matching partition on the other side, but if the given partition finds the
    2365             :  * matching partition later, we will adjust the assignment.
    2366             :  */
    2367             : static int
    2368         528 : merge_partition_with_dummy(PartitionMap *map, int index, int *next_index)
    2369             : {
    2370         528 :     int         merged_index = *next_index;
    2371             : 
    2372             :     Assert(index >= 0 && index < map->nparts);
    2373             :     Assert(map->merged_indexes[index] == -1);
    2374             :     Assert(!map->merged[index]);
    2375         528 :     map->merged_indexes[index] = merged_index;
    2376             :     /* Leave the merged flag alone! */
    2377         528 :     *next_index = *next_index + 1;
    2378         528 :     return merged_index;
    2379             : }
    2380             : 
    2381             : /*
    2382             :  * fix_merged_indexes
    2383             :  *      Adjust merged indexes of re-merged partitions
    2384             :  */
    2385             : static void
    2386          48 : fix_merged_indexes(PartitionMap *outer_map, PartitionMap *inner_map,
    2387             :                    int nmerged, List *merged_indexes)
    2388             : {
    2389             :     int        *new_indexes;
    2390             :     int         merged_index;
    2391             :     int         i;
    2392             :     ListCell   *lc;
    2393             : 
    2394             :     Assert(nmerged > 0);
    2395             : 
    2396          48 :     new_indexes = (int *) palloc(sizeof(int) * nmerged);
    2397         312 :     for (i = 0; i < nmerged; i++)
    2398         264 :         new_indexes[i] = -1;
    2399             : 
    2400             :     /* Build the mapping of old merged indexes to new merged indexes. */
    2401          48 :     if (outer_map->did_remapping)
    2402             :     {
    2403         210 :         for (i = 0; i < outer_map->nparts; i++)
    2404             :         {
    2405         162 :             merged_index = outer_map->old_indexes[i];
    2406         162 :             if (merged_index >= 0)
    2407          48 :                 new_indexes[merged_index] = outer_map->merged_indexes[i];
    2408             :         }
    2409             :     }
    2410          48 :     if (inner_map->did_remapping)
    2411             :     {
    2412         210 :         for (i = 0; i < inner_map->nparts; i++)
    2413             :         {
    2414         162 :             merged_index = inner_map->old_indexes[i];
    2415         162 :             if (merged_index >= 0)
    2416          48 :                 new_indexes[merged_index] = inner_map->merged_indexes[i];
    2417             :         }
    2418             :     }
    2419             : 
    2420             :     /* Fix the merged_indexes list using the mapping. */
    2421         438 :     foreach(lc, merged_indexes)
    2422             :     {
    2423         390 :         merged_index = lfirst_int(lc);
    2424             :         Assert(merged_index >= 0);
    2425         390 :         if (new_indexes[merged_index] >= 0)
    2426          96 :             lfirst_int(lc) = new_indexes[merged_index];
    2427             :     }
    2428             : 
    2429          48 :     pfree(new_indexes);
    2430          48 : }
    2431             : 
    2432             : /*
    2433             :  * generate_matching_part_pairs
    2434             :  *      Generate a pair of lists of partitions that produce merged partitions
    2435             :  *
    2436             :  * The lists of partitions are built in the order of merged partition indexes,
    2437             :  * and returned in *outer_parts and *inner_parts.
    2438             :  */
    2439             : static void
    2440         732 : generate_matching_part_pairs(RelOptInfo *outer_rel, RelOptInfo *inner_rel,
    2441             :                              PartitionMap *outer_map, PartitionMap *inner_map,
    2442             :                              int nmerged,
    2443             :                              List **outer_parts, List **inner_parts)
    2444             : {
    2445         732 :     int         outer_nparts = outer_map->nparts;
    2446         732 :     int         inner_nparts = inner_map->nparts;
    2447             :     int        *outer_indexes;
    2448             :     int        *inner_indexes;
    2449             :     int         max_nparts;
    2450             :     int         i;
    2451             : 
    2452             :     Assert(nmerged > 0);
    2453             :     Assert(*outer_parts == NIL);
    2454             :     Assert(*inner_parts == NIL);
    2455             : 
    2456         732 :     outer_indexes = (int *) palloc(sizeof(int) * nmerged);
    2457         732 :     inner_indexes = (int *) palloc(sizeof(int) * nmerged);
    2458        2796 :     for (i = 0; i < nmerged; i++)
    2459        2064 :         outer_indexes[i] = inner_indexes[i] = -1;
    2460             : 
    2461             :     /* Set pairs of matching partitions. */
    2462             :     Assert(outer_nparts == outer_rel->nparts);
    2463             :     Assert(inner_nparts == inner_rel->nparts);
    2464         732 :     max_nparts = Max(outer_nparts, inner_nparts);
    2465        3060 :     for (i = 0; i < max_nparts; i++)
    2466             :     {
    2467        2328 :         if (i < outer_nparts)
    2468             :         {
    2469        2220 :             int         merged_index = outer_map->merged_indexes[i];
    2470             : 
    2471        2220 :             if (merged_index >= 0)
    2472             :             {
    2473             :                 Assert(merged_index < nmerged);
    2474        1956 :                 outer_indexes[merged_index] = i;
    2475             :             }
    2476             :         }
    2477        2328 :         if (i < inner_nparts)
    2478             :         {
    2479        2244 :             int         merged_index = inner_map->merged_indexes[i];
    2480             : 
    2481        2244 :             if (merged_index >= 0)
    2482             :             {
    2483             :                 Assert(merged_index < nmerged);
    2484        1920 :                 inner_indexes[merged_index] = i;
    2485             :             }
    2486             :         }
    2487             :     }
    2488             : 
    2489             :     /* Build the list pairs. */
    2490        2796 :     for (i = 0; i < nmerged; i++)
    2491             :     {
    2492        2064 :         int         outer_index = outer_indexes[i];
    2493        2064 :         int         inner_index = inner_indexes[i];
    2494             : 
    2495             :         /*
    2496             :          * If both partitions are dummy, it means the merged partition that
    2497             :          * had been assigned to the outer/inner partition was removed when
    2498             :          * re-merging the outer/inner partition in
    2499             :          * merge_matching_partitions(); ignore the merged partition.
    2500             :          */
    2501        2064 :         if (outer_index == -1 && inner_index == -1)
    2502          96 :             continue;
    2503             : 
    2504        3924 :         *outer_parts = lappend(*outer_parts, outer_index >= 0 ?
    2505        1956 :                                outer_rel->part_rels[outer_index] : NULL);
    2506        3888 :         *inner_parts = lappend(*inner_parts, inner_index >= 0 ?
    2507        1920 :                                inner_rel->part_rels[inner_index] : NULL);
    2508             :     }
    2509             : 
    2510         732 :     pfree(outer_indexes);
    2511         732 :     pfree(inner_indexes);
    2512         732 : }
    2513             : 
    2514             : /*
    2515             :  * build_merged_partition_bounds
    2516             :  *      Create a PartitionBoundInfo struct from merged partition bounds
    2517             :  */
    2518             : static PartitionBoundInfo
    2519         732 : build_merged_partition_bounds(char strategy, List *merged_datums,
    2520             :                               List *merged_kinds, List *merged_indexes,
    2521             :                               int null_index, int default_index)
    2522             : {
    2523             :     PartitionBoundInfo merged_bounds;
    2524         732 :     int         ndatums = list_length(merged_datums);
    2525             :     int         pos;
    2526             :     ListCell   *lc;
    2527             : 
    2528         732 :     merged_bounds = (PartitionBoundInfo) palloc(sizeof(PartitionBoundInfoData));
    2529         732 :     merged_bounds->strategy = strategy;
    2530         732 :     merged_bounds->ndatums = ndatums;
    2531             : 
    2532         732 :     merged_bounds->datums = (Datum **) palloc(sizeof(Datum *) * ndatums);
    2533         732 :     pos = 0;
    2534        4044 :     foreach(lc, merged_datums)
    2535        3312 :         merged_bounds->datums[pos++] = (Datum *) lfirst(lc);
    2536             : 
    2537         732 :     if (strategy == PARTITION_STRATEGY_RANGE)
    2538             :     {
    2539             :         Assert(list_length(merged_kinds) == ndatums);
    2540         294 :         merged_bounds->kind = (PartitionRangeDatumKind **)
    2541         294 :             palloc(sizeof(PartitionRangeDatumKind *) * ndatums);
    2542         294 :         pos = 0;
    2543        1560 :         foreach(lc, merged_kinds)
    2544        1266 :             merged_bounds->kind[pos++] = (PartitionRangeDatumKind *) lfirst(lc);
    2545             : 
    2546             :         /* There are ndatums+1 indexes in the case of range partitioning. */
    2547         294 :         merged_indexes = lappend_int(merged_indexes, -1);
    2548         294 :         ndatums++;
    2549             :     }
    2550             :     else
    2551             :     {
    2552             :         Assert(strategy == PARTITION_STRATEGY_LIST);
    2553             :         Assert(merged_kinds == NIL);
    2554         438 :         merged_bounds->kind = NULL;
    2555             :     }
    2556             : 
    2557             :     /* interleaved_parts is always NULL for join relations. */
    2558         732 :     merged_bounds->interleaved_parts = NULL;
    2559             : 
    2560             :     Assert(list_length(merged_indexes) == ndatums);
    2561         732 :     merged_bounds->nindexes = ndatums;
    2562         732 :     merged_bounds->indexes = (int *) palloc(sizeof(int) * ndatums);
    2563         732 :     pos = 0;
    2564        4338 :     foreach(lc, merged_indexes)
    2565        3606 :         merged_bounds->indexes[pos++] = lfirst_int(lc);
    2566             : 
    2567         732 :     merged_bounds->null_index = null_index;
    2568         732 :     merged_bounds->default_index = default_index;
    2569             : 
    2570         732 :     return merged_bounds;
    2571             : }
    2572             : 
    2573             : /*
    2574             :  * get_range_partition
    2575             :  *      Get the next non-dummy partition of a range-partitioned relation,
    2576             :  *      returning the index of that partition
    2577             :  *
    2578             :  * *lb and *ub are set to the lower and upper bounds of that partition
    2579             :  * respectively, and *lb_pos is advanced to the next lower bound, if any.
    2580             :  */
    2581             : static int
    2582        2580 : get_range_partition(RelOptInfo *rel,
    2583             :                     PartitionBoundInfo bi,
    2584             :                     int *lb_pos,
    2585             :                     PartitionRangeBound *lb,
    2586             :                     PartitionRangeBound *ub)
    2587             : {
    2588             :     int         part_index;
    2589             : 
    2590             :     Assert(bi->strategy == PARTITION_STRATEGY_RANGE);
    2591             : 
    2592             :     do
    2593             :     {
    2594        2580 :         part_index = get_range_partition_internal(bi, lb_pos, lb, ub);
    2595        2580 :         if (part_index == -1)
    2596         630 :             return -1;
    2597        1950 :     } while (is_dummy_partition(rel, part_index));
    2598             : 
    2599        1902 :     return part_index;
    2600             : }
    2601             : 
    2602             : static int
    2603        2580 : get_range_partition_internal(PartitionBoundInfo bi,
    2604             :                              int *lb_pos,
    2605             :                              PartitionRangeBound *lb,
    2606             :                              PartitionRangeBound *ub)
    2607             : {
    2608             :     /* Return the index as -1 if we've exhausted all lower bounds. */
    2609        2580 :     if (*lb_pos >= bi->ndatums)
    2610         630 :         return -1;
    2611             : 
    2612             :     /* A lower bound should have at least one more bound after it. */
    2613             :     Assert(*lb_pos + 1 < bi->ndatums);
    2614             : 
    2615             :     /* Set the lower bound. */
    2616        1950 :     lb->index = bi->indexes[*lb_pos];
    2617        1950 :     lb->datums = bi->datums[*lb_pos];
    2618        1950 :     lb->kind = bi->kind[*lb_pos];
    2619        1950 :     lb->lower = true;
    2620             :     /* Set the upper bound. */
    2621        1950 :     ub->index = bi->indexes[*lb_pos + 1];
    2622        1950 :     ub->datums = bi->datums[*lb_pos + 1];
    2623        1950 :     ub->kind = bi->kind[*lb_pos + 1];
    2624        1950 :     ub->lower = false;
    2625             : 
    2626             :     /* The index assigned to an upper bound should be valid. */
    2627             :     Assert(ub->index >= 0);
    2628             : 
    2629             :     /*
    2630             :      * Advance the position to the next lower bound.  If there are no bounds
    2631             :      * left beyond the upper bound, we have reached the last lower bound.
    2632             :      */
    2633        1950 :     if (*lb_pos + 2 >= bi->ndatums)
    2634         684 :         *lb_pos = bi->ndatums;
    2635             :     else
    2636             :     {
    2637             :         /*
    2638             :          * If the index assigned to the bound next to the upper bound isn't
    2639             :          * valid, that is the next lower bound; else, the upper bound is also
    2640             :          * the lower bound of the next range partition.
    2641             :          */
    2642        1266 :         if (bi->indexes[*lb_pos + 2] < 0)
    2643         474 :             *lb_pos = *lb_pos + 2;
    2644             :         else
    2645         792 :             *lb_pos = *lb_pos + 1;
    2646             :     }
    2647             : 
    2648        1950 :     return ub->index;
    2649             : }
    2650             : 
    2651             : /*
    2652             :  * compare_range_partitions
    2653             :  *      Compare the bounds of two range partitions, and return true if the
    2654             :  *      two partitions overlap, false otherwise
    2655             :  *
    2656             :  * *lb_cmpval is set to -1, 0, or 1 if the outer partition's lower bound is
    2657             :  * lower than, equal to, or higher than the inner partition's lower bound
    2658             :  * respectively.  Likewise, *ub_cmpval is set to -1, 0, or 1 if the outer
    2659             :  * partition's upper bound is lower than, equal to, or higher than the inner
    2660             :  * partition's upper bound respectively.
    2661             :  */
    2662             : static bool
    2663         864 : compare_range_partitions(int partnatts, FmgrInfo *partsupfuncs,
    2664             :                          Oid *partcollations,
    2665             :                          PartitionRangeBound *outer_lb,
    2666             :                          PartitionRangeBound *outer_ub,
    2667             :                          PartitionRangeBound *inner_lb,
    2668             :                          PartitionRangeBound *inner_ub,
    2669             :                          int *lb_cmpval, int *ub_cmpval)
    2670             : {
    2671             :     /*
    2672             :      * Check if the outer partition's upper bound is lower than the inner
    2673             :      * partition's lower bound; if so the partitions aren't overlapping.
    2674             :      */
    2675         864 :     if (compare_range_bounds(partnatts, partsupfuncs, partcollations,
    2676             :                              outer_ub, inner_lb) < 0)
    2677             :     {
    2678           0 :         *lb_cmpval = -1;
    2679           0 :         *ub_cmpval = -1;
    2680           0 :         return false;
    2681             :     }
    2682             : 
    2683             :     /*
    2684             :      * Check if the outer partition's lower bound is higher than the inner
    2685             :      * partition's upper bound; if so the partitions aren't overlapping.
    2686             :      */
    2687         864 :     if (compare_range_bounds(partnatts, partsupfuncs, partcollations,
    2688             :                              outer_lb, inner_ub) > 0)
    2689             :     {
    2690          36 :         *lb_cmpval = 1;
    2691          36 :         *ub_cmpval = 1;
    2692          36 :         return false;
    2693             :     }
    2694             : 
    2695             :     /* All other cases indicate overlapping partitions. */
    2696         828 :     *lb_cmpval = compare_range_bounds(partnatts, partsupfuncs, partcollations,
    2697             :                                       outer_lb, inner_lb);
    2698         828 :     *ub_cmpval = compare_range_bounds(partnatts, partsupfuncs, partcollations,
    2699             :                                       outer_ub, inner_ub);
    2700         828 :     return true;
    2701             : }
    2702             : 
    2703             : /*
    2704             :  * get_merged_range_bounds
    2705             :  *      Given the bounds of range partitions to be joined, determine the bounds
    2706             :  *      of a merged partition produced from the range partitions
    2707             :  *
    2708             :  * *merged_lb and *merged_ub are set to the lower and upper bounds of the
    2709             :  * merged partition.
    2710             :  */
    2711             : static void
    2712         828 : get_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
    2713             :                         Oid *partcollations, JoinType jointype,
    2714             :                         PartitionRangeBound *outer_lb,
    2715             :                         PartitionRangeBound *outer_ub,
    2716             :                         PartitionRangeBound *inner_lb,
    2717             :                         PartitionRangeBound *inner_ub,
    2718             :                         int lb_cmpval, int ub_cmpval,
    2719             :                         PartitionRangeBound *merged_lb,
    2720             :                         PartitionRangeBound *merged_ub)
    2721             : {
    2722             :     Assert(compare_range_bounds(partnatts, partsupfuncs, partcollations,
    2723             :                                 outer_lb, inner_lb) == lb_cmpval);
    2724             :     Assert(compare_range_bounds(partnatts, partsupfuncs, partcollations,
    2725             :                                 outer_ub, inner_ub) == ub_cmpval);
    2726             : 
    2727         828 :     switch (jointype)
    2728             :     {
    2729         432 :         case JOIN_INNER:
    2730             :         case JOIN_SEMI:
    2731             : 
    2732             :             /*
    2733             :              * An INNER/SEMI join will have the rows that fit both sides, so
    2734             :              * the lower bound of the merged partition will be the higher of
    2735             :              * the two lower bounds, and the upper bound of the merged
    2736             :              * partition will be the lower of the two upper bounds.
    2737             :              */
    2738         432 :             *merged_lb = (lb_cmpval > 0) ? *outer_lb : *inner_lb;
    2739         432 :             *merged_ub = (ub_cmpval < 0) ? *outer_ub : *inner_ub;
    2740         432 :             break;
    2741             : 
    2742         324 :         case JOIN_LEFT:
    2743             :         case JOIN_ANTI:
    2744             : 
    2745             :             /*
    2746             :              * A LEFT/ANTI join will have all the rows from the outer side, so
    2747             :              * the bounds of the merged partition will be the same as the
    2748             :              * outer bounds.
    2749             :              */
    2750         324 :             *merged_lb = *outer_lb;
    2751         324 :             *merged_ub = *outer_ub;
    2752         324 :             break;
    2753             : 
    2754          72 :         case JOIN_FULL:
    2755             : 
    2756             :             /*
    2757             :              * A FULL join will have all the rows from both sides, so the
    2758             :              * lower bound of the merged partition will be the lower of the
    2759             :              * two lower bounds, and the upper bound of the merged partition
    2760             :              * will be the higher of the two upper bounds.
    2761             :              */
    2762          72 :             *merged_lb = (lb_cmpval < 0) ? *outer_lb : *inner_lb;
    2763          72 :             *merged_ub = (ub_cmpval > 0) ? *outer_ub : *inner_ub;
    2764          72 :             break;
    2765             : 
    2766           0 :         default:
    2767           0 :             elog(ERROR, "unrecognized join type: %d", (int) jointype);
    2768             :     }
    2769         828 : }
    2770             : 
    2771             : /*
    2772             :  * add_merged_range_bounds
    2773             :  *      Add the bounds of a merged partition to the lists of range bounds
    2774             :  */
    2775             : static void
    2776         816 : add_merged_range_bounds(int partnatts, FmgrInfo *partsupfuncs,
    2777             :                         Oid *partcollations,
    2778             :                         PartitionRangeBound *merged_lb,
    2779             :                         PartitionRangeBound *merged_ub,
    2780             :                         int merged_index,
    2781             :                         List **merged_datums,
    2782             :                         List **merged_kinds,
    2783             :                         List **merged_indexes)
    2784             : {
    2785             :     int         cmpval;
    2786             : 
    2787         816 :     if (!*merged_datums)
    2788             :     {
    2789             :         /* First merged partition */
    2790             :         Assert(!*merged_kinds);
    2791             :         Assert(!*merged_indexes);
    2792         330 :         cmpval = 1;
    2793             :     }
    2794             :     else
    2795             :     {
    2796             :         PartitionRangeBound prev_ub;
    2797             : 
    2798             :         Assert(*merged_datums);
    2799             :         Assert(*merged_kinds);
    2800             :         Assert(*merged_indexes);
    2801             : 
    2802             :         /* Get the last upper bound. */
    2803         486 :         prev_ub.index = llast_int(*merged_indexes);
    2804         486 :         prev_ub.datums = (Datum *) llast(*merged_datums);
    2805         486 :         prev_ub.kind = (PartitionRangeDatumKind *) llast(*merged_kinds);
    2806         486 :         prev_ub.lower = false;
    2807             : 
    2808             :         /*
    2809             :          * We pass lower1 = false to partition_rbound_cmp() to prevent it from
    2810             :          * considering the last upper bound to be smaller than the lower bound
    2811             :          * of the merged partition when the values of the two range bounds
    2812             :          * compare equal.
    2813             :          */
    2814         486 :         cmpval = partition_rbound_cmp(partnatts, partsupfuncs, partcollations,
    2815             :                                       merged_lb->datums, merged_lb->kind,
    2816             :                                       false, &prev_ub);
    2817             :         Assert(cmpval >= 0);
    2818             :     }
    2819             : 
    2820             :     /*
    2821             :      * If the lower bound is higher than the last upper bound, add the lower
    2822             :      * bound with the index as -1 indicating that that is a lower bound; else,
    2823             :      * the last upper bound will be reused as the lower bound of the merged
    2824             :      * partition, so skip this.
    2825             :      */
    2826         816 :     if (cmpval > 0)
    2827             :     {
    2828         576 :         *merged_datums = lappend(*merged_datums, merged_lb->datums);
    2829         576 :         *merged_kinds = lappend(*merged_kinds, merged_lb->kind);
    2830         576 :         *merged_indexes = lappend_int(*merged_indexes, -1);
    2831             :     }
    2832             : 
    2833             :     /* Add the upper bound and index of the merged partition. */
    2834         816 :     *merged_datums = lappend(*merged_datums, merged_ub->datums);
    2835         816 :     *merged_kinds = lappend(*merged_kinds, merged_ub->kind);
    2836         816 :     *merged_indexes = lappend_int(*merged_indexes, merged_index);
    2837         816 : }
    2838             : 
    2839             : /*
    2840             :  * partitions_are_ordered
    2841             :  *      Determine whether the partitions described by 'boundinfo' are ordered,
    2842             :  *      that is partitions appearing earlier in the PartitionDesc sequence
    2843             :  *      contain partition keys strictly less than those appearing later.
    2844             :  *      Also, if NULL values are possible, they must come in the last
    2845             :  *      partition defined in the PartitionDesc.  'live_parts' marks which
    2846             :  *      partitions we should include when checking the ordering.  Partitions
    2847             :  *      that do not appear in 'live_parts' are ignored.
    2848             :  *
    2849             :  * If out of order, or there is insufficient info to know the order,
    2850             :  * then we return false.
    2851             :  */
    2852             : bool
    2853       25198 : partitions_are_ordered(PartitionBoundInfo boundinfo, Bitmapset *live_parts)
    2854             : {
    2855             :     Assert(boundinfo != NULL);
    2856             : 
    2857       25198 :     switch (boundinfo->strategy)
    2858             :     {
    2859       15648 :         case PARTITION_STRATEGY_RANGE:
    2860             : 
    2861             :             /*
    2862             :              * RANGE-type partitioning guarantees that the partitions can be
    2863             :              * scanned in the order that they're defined in the PartitionDesc
    2864             :              * to provide sequential, non-overlapping ranges of tuples.
    2865             :              * However, if a DEFAULT partition exists and it's contained
    2866             :              * within live_parts, then the partitions are not ordered.
    2867             :              */
    2868       15648 :             if (!partition_bound_has_default(boundinfo) ||
    2869        1816 :                 !bms_is_member(boundinfo->default_index, live_parts))
    2870       14396 :                 return true;
    2871        1252 :             break;
    2872             : 
    2873        8640 :         case PARTITION_STRATEGY_LIST:
    2874             : 
    2875             :             /*
    2876             :              * LIST partitioned are ordered providing none of live_parts
    2877             :              * overlap with the partitioned table's interleaved partitions.
    2878             :              */
    2879        8640 :             if (!bms_overlap(live_parts, boundinfo->interleaved_parts))
    2880        6834 :                 return true;
    2881             : 
    2882        1806 :             break;
    2883         910 :         case PARTITION_STRATEGY_HASH:
    2884         910 :             break;
    2885             :     }
    2886             : 
    2887        3968 :     return false;
    2888             : }
    2889             : 
    2890             : /*
    2891             :  * check_new_partition_bound
    2892             :  *
    2893             :  * Checks if the new partition's bound overlaps any of the existing partitions
    2894             :  * of parent.  Also performs additional checks as necessary per strategy.
    2895             :  */
    2896             : void
    2897        9066 : check_new_partition_bound(char *relname, Relation parent,
    2898             :                           PartitionBoundSpec *spec, ParseState *pstate)
    2899             : {
    2900        9066 :     PartitionKey key = RelationGetPartitionKey(parent);
    2901        9066 :     PartitionDesc partdesc = RelationGetPartitionDesc(parent, false);
    2902        9066 :     PartitionBoundInfo boundinfo = partdesc->boundinfo;
    2903        9066 :     int         with = -1;
    2904        9066 :     bool        overlap = false;
    2905        9066 :     int         overlap_location = -1;
    2906             : 
    2907        9066 :     if (spec->is_default)
    2908             :     {
    2909             :         /*
    2910             :          * The default partition bound never conflicts with any other
    2911             :          * partition's; if that's what we're attaching, the only possible
    2912             :          * problem is that one already exists, so check for that and we're
    2913             :          * done.
    2914             :          */
    2915         532 :         if (boundinfo == NULL || !partition_bound_has_default(boundinfo))
    2916         508 :             return;
    2917             : 
    2918             :         /* Default partition already exists, error out. */
    2919          24 :         ereport(ERROR,
    2920             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    2921             :                  errmsg("partition \"%s\" conflicts with existing default partition \"%s\"",
    2922             :                         relname, get_rel_name(partdesc->oids[boundinfo->default_index])),
    2923             :                  parser_errposition(pstate, spec->location)));
    2924             :     }
    2925             : 
    2926        8534 :     switch (key->strategy)
    2927             :     {
    2928         554 :         case PARTITION_STRATEGY_HASH:
    2929             :             {
    2930             :                 Assert(spec->strategy == PARTITION_STRATEGY_HASH);
    2931             :                 Assert(spec->remainder >= 0 && spec->remainder < spec->modulus);
    2932             : 
    2933         554 :                 if (partdesc->nparts > 0)
    2934             :                 {
    2935             :                     int         greatest_modulus;
    2936             :                     int         remainder;
    2937             :                     int         offset;
    2938             : 
    2939             :                     /*
    2940             :                      * Check rule that every modulus must be a factor of the
    2941             :                      * next larger modulus.  (For example, if you have a bunch
    2942             :                      * of partitions that all have modulus 5, you can add a
    2943             :                      * new partition with modulus 10 or a new partition with
    2944             :                      * modulus 15, but you cannot add both a partition with
    2945             :                      * modulus 10 and a partition with modulus 15, because 10
    2946             :                      * is not a factor of 15.)  We need only check the next
    2947             :                      * smaller and next larger existing moduli, relying on
    2948             :                      * previous enforcement of this rule to be sure that the
    2949             :                      * rest are in line.
    2950             :                      */
    2951             : 
    2952             :                     /*
    2953             :                      * Get the greatest (modulus, remainder) pair contained in
    2954             :                      * boundinfo->datums that is less than or equal to the
    2955             :                      * (spec->modulus, spec->remainder) pair.
    2956             :                      */
    2957         380 :                     offset = partition_hash_bsearch(boundinfo,
    2958             :                                                     spec->modulus,
    2959             :                                                     spec->remainder);
    2960         380 :                     if (offset < 0)
    2961             :                     {
    2962             :                         int         next_modulus;
    2963             : 
    2964             :                         /*
    2965             :                          * All existing moduli are greater or equal, so the
    2966             :                          * new one must be a factor of the smallest one, which
    2967             :                          * is first in the boundinfo.
    2968             :                          */
    2969          14 :                         next_modulus = DatumGetInt32(boundinfo->datums[0][0]);
    2970          14 :                         if (next_modulus % spec->modulus != 0)
    2971           6 :                             ereport(ERROR,
    2972             :                                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    2973             :                                      errmsg("every hash partition modulus must be a factor of the next larger modulus"),
    2974             :                                      errdetail("The new modulus %d is not a factor of %d, the modulus of existing partition \"%s\".",
    2975             :                                                spec->modulus, next_modulus,
    2976             :                                                get_rel_name(partdesc->oids[0]))));
    2977             :                     }
    2978             :                     else
    2979             :                     {
    2980             :                         int         prev_modulus;
    2981             : 
    2982             :                         /*
    2983             :                          * We found the largest (modulus, remainder) pair less
    2984             :                          * than or equal to the new one.  That modulus must be
    2985             :                          * a divisor of, or equal to, the new modulus.
    2986             :                          */
    2987         366 :                         prev_modulus = DatumGetInt32(boundinfo->datums[offset][0]);
    2988             : 
    2989         366 :                         if (spec->modulus % prev_modulus != 0)
    2990           6 :                             ereport(ERROR,
    2991             :                                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    2992             :                                      errmsg("every hash partition modulus must be a factor of the next larger modulus"),
    2993             :                                      errdetail("The new modulus %d is not divisible by %d, the modulus of existing partition \"%s\".",
    2994             :                                                spec->modulus,
    2995             :                                                prev_modulus,
    2996             :                                                get_rel_name(partdesc->oids[offset]))));
    2997             : 
    2998         360 :                         if (offset + 1 < boundinfo->ndatums)
    2999             :                         {
    3000             :                             int         next_modulus;
    3001             : 
    3002             :                             /*
    3003             :                              * Look at the next higher (modulus, remainder)
    3004             :                              * pair.  That could have the same modulus and a
    3005             :                              * larger remainder than the new pair, in which
    3006             :                              * case we're good.  If it has a larger modulus,
    3007             :                              * the new modulus must divide that one.
    3008             :                              */
    3009          30 :                             next_modulus = DatumGetInt32(boundinfo->datums[offset + 1][0]);
    3010             : 
    3011          30 :                             if (next_modulus % spec->modulus != 0)
    3012           6 :                                 ereport(ERROR,
    3013             :                                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    3014             :                                          errmsg("every hash partition modulus must be a factor of the next larger modulus"),
    3015             :                                          errdetail("The new modulus %d is not a factor of %d, the modulus of existing partition \"%s\".",
    3016             :                                                    spec->modulus, next_modulus,
    3017             :                                                    get_rel_name(partdesc->oids[offset + 1]))));
    3018             :                         }
    3019             :                     }
    3020             : 
    3021         362 :                     greatest_modulus = boundinfo->nindexes;
    3022         362 :                     remainder = spec->remainder;
    3023             : 
    3024             :                     /*
    3025             :                      * Normally, the lowest remainder that could conflict with
    3026             :                      * the new partition is equal to the remainder specified
    3027             :                      * for the new partition, but when the new partition has a
    3028             :                      * modulus higher than any used so far, we need to adjust.
    3029             :                      */
    3030         362 :                     if (remainder >= greatest_modulus)
    3031          12 :                         remainder = remainder % greatest_modulus;
    3032             : 
    3033             :                     /* Check every potentially-conflicting remainder. */
    3034             :                     do
    3035             :                     {
    3036         488 :                         if (boundinfo->indexes[remainder] != -1)
    3037             :                         {
    3038          24 :                             overlap = true;
    3039          24 :                             overlap_location = spec->location;
    3040          24 :                             with = boundinfo->indexes[remainder];
    3041          24 :                             break;
    3042             :                         }
    3043         464 :                         remainder += spec->modulus;
    3044         464 :                     } while (remainder < greatest_modulus);
    3045             :                 }
    3046             : 
    3047         536 :                 break;
    3048             :             }
    3049             : 
    3050        4258 :         case PARTITION_STRATEGY_LIST:
    3051             :             {
    3052             :                 Assert(spec->strategy == PARTITION_STRATEGY_LIST);
    3053             : 
    3054        4258 :                 if (partdesc->nparts > 0)
    3055             :                 {
    3056             :                     ListCell   *cell;
    3057             : 
    3058             :                     Assert(boundinfo &&
    3059             :                            boundinfo->strategy == PARTITION_STRATEGY_LIST &&
    3060             :                            (boundinfo->ndatums > 0 ||
    3061             :                             partition_bound_accepts_nulls(boundinfo) ||
    3062             :                             partition_bound_has_default(boundinfo)));
    3063             : 
    3064        5438 :                     foreach(cell, spec->listdatums)
    3065             :                     {
    3066        3240 :                         Const      *val = lfirst_node(Const, cell);
    3067             : 
    3068        3240 :                         overlap_location = val->location;
    3069        3240 :                         if (!val->constisnull)
    3070             :                         {
    3071             :                             int         offset;
    3072             :                             bool        equal;
    3073             : 
    3074        3126 :                             offset = partition_list_bsearch(&key->partsupfunc[0],
    3075             :                                                             key->partcollation,
    3076             :                                                             boundinfo,
    3077             :                                                             val->constvalue,
    3078             :                                                             &equal);
    3079        3126 :                             if (offset >= 0 && equal)
    3080             :                             {
    3081          18 :                                 overlap = true;
    3082          18 :                                 with = boundinfo->indexes[offset];
    3083          18 :                                 break;
    3084             :                             }
    3085             :                         }
    3086         114 :                         else if (partition_bound_accepts_nulls(boundinfo))
    3087             :                         {
    3088           6 :                             overlap = true;
    3089           6 :                             with = boundinfo->null_index;
    3090           6 :                             break;
    3091             :                         }
    3092             :                     }
    3093             :                 }
    3094             : 
    3095        4258 :                 break;
    3096             :             }
    3097             : 
    3098        3722 :         case PARTITION_STRATEGY_RANGE:
    3099             :             {
    3100             :                 PartitionRangeBound *lower,
    3101             :                            *upper;
    3102             :                 int         cmpval;
    3103             : 
    3104             :                 Assert(spec->strategy == PARTITION_STRATEGY_RANGE);
    3105        3722 :                 lower = make_one_partition_rbound(key, -1, spec->lowerdatums, true);
    3106        3722 :                 upper = make_one_partition_rbound(key, -1, spec->upperdatums, false);
    3107             : 
    3108             :                 /*
    3109             :                  * First check if the resulting range would be empty with
    3110             :                  * specified lower and upper bounds.  partition_rbound_cmp
    3111             :                  * cannot return zero here, since the lower-bound flags are
    3112             :                  * different.
    3113             :                  */
    3114        3722 :                 cmpval = partition_rbound_cmp(key->partnatts,
    3115             :                                               key->partsupfunc,
    3116             :                                               key->partcollation,
    3117             :                                               lower->datums, lower->kind,
    3118             :                                               true, upper);
    3119             :                 Assert(cmpval != 0);
    3120        3722 :                 if (cmpval > 0)
    3121             :                 {
    3122             :                     /* Point to problematic key in the lower datums list. */
    3123          12 :                     PartitionRangeDatum *datum = list_nth(spec->lowerdatums,
    3124             :                                                           cmpval - 1);
    3125             : 
    3126          12 :                     ereport(ERROR,
    3127             :                             (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    3128             :                              errmsg("empty range bound specified for partition \"%s\"",
    3129             :                                     relname),
    3130             :                              errdetail("Specified lower bound %s is greater than or equal to upper bound %s.",
    3131             :                                        get_range_partbound_string(spec->lowerdatums),
    3132             :                                        get_range_partbound_string(spec->upperdatums)),
    3133             :                              parser_errposition(pstate, datum->location)));
    3134             :                 }
    3135             : 
    3136        3710 :                 if (partdesc->nparts > 0)
    3137             :                 {
    3138             :                     int         offset;
    3139             : 
    3140             :                     Assert(boundinfo &&
    3141             :                            boundinfo->strategy == PARTITION_STRATEGY_RANGE &&
    3142             :                            (boundinfo->ndatums > 0 ||
    3143             :                             partition_bound_has_default(boundinfo)));
    3144             : 
    3145             :                     /*
    3146             :                      * Test whether the new lower bound (which is treated
    3147             :                      * inclusively as part of the new partition) lies inside
    3148             :                      * an existing partition, or in a gap.
    3149             :                      *
    3150             :                      * If it's inside an existing partition, the bound at
    3151             :                      * offset + 1 will be the upper bound of that partition,
    3152             :                      * and its index will be >= 0.
    3153             :                      *
    3154             :                      * If it's in a gap, the bound at offset + 1 will be the
    3155             :                      * lower bound of the next partition, and its index will
    3156             :                      * be -1. This is also true if there is no next partition,
    3157             :                      * since the index array is initialised with an extra -1
    3158             :                      * at the end.
    3159             :                      */
    3160        2050 :                     offset = partition_range_bsearch(key->partnatts,
    3161             :                                                      key->partsupfunc,
    3162             :                                                      key->partcollation,
    3163             :                                                      boundinfo, lower,
    3164             :                                                      &cmpval);
    3165             : 
    3166        2050 :                     if (boundinfo->indexes[offset + 1] < 0)
    3167             :                     {
    3168             :                         /*
    3169             :                          * Check that the new partition will fit in the gap.
    3170             :                          * For it to fit, the new upper bound must be less
    3171             :                          * than or equal to the lower bound of the next
    3172             :                          * partition, if there is one.
    3173             :                          */
    3174        2014 :                         if (offset + 1 < boundinfo->ndatums)
    3175             :                         {
    3176             :                             Datum      *datums;
    3177             :                             PartitionRangeDatumKind *kind;
    3178             :                             bool        is_lower;
    3179             : 
    3180          90 :                             datums = boundinfo->datums[offset + 1];
    3181          90 :                             kind = boundinfo->kind[offset + 1];
    3182          90 :                             is_lower = (boundinfo->indexes[offset + 1] == -1);
    3183             : 
    3184          90 :                             cmpval = partition_rbound_cmp(key->partnatts,
    3185             :                                                           key->partsupfunc,
    3186             :                                                           key->partcollation,
    3187             :                                                           datums, kind,
    3188             :                                                           is_lower, upper);
    3189          90 :                             if (cmpval < 0)
    3190             :                             {
    3191             :                                 /*
    3192             :                                  * Point to problematic key in the upper
    3193             :                                  * datums list.
    3194             :                                  */
    3195             :                                 PartitionRangeDatum *datum =
    3196          12 :                                     list_nth(spec->upperdatums, abs(cmpval) - 1);
    3197             : 
    3198             :                                 /*
    3199             :                                  * The new partition overlaps with the
    3200             :                                  * existing partition between offset + 1 and
    3201             :                                  * offset + 2.
    3202             :                                  */
    3203          12 :                                 overlap = true;
    3204          12 :                                 overlap_location = datum->location;
    3205          12 :                                 with = boundinfo->indexes[offset + 2];
    3206             :                             }
    3207             :                         }
    3208             :                     }
    3209             :                     else
    3210             :                     {
    3211             :                         /*
    3212             :                          * The new partition overlaps with the existing
    3213             :                          * partition between offset and offset + 1.
    3214             :                          */
    3215             :                         PartitionRangeDatum *datum;
    3216             : 
    3217             :                         /*
    3218             :                          * Point to problematic key in the lower datums list;
    3219             :                          * if we have equality, point to the first one.
    3220             :                          */
    3221          36 :                         datum = cmpval == 0 ? linitial(spec->lowerdatums) :
    3222          18 :                             list_nth(spec->lowerdatums, abs(cmpval) - 1);
    3223          36 :                         overlap = true;
    3224          36 :                         overlap_location = datum->location;
    3225          36 :                         with = boundinfo->indexes[offset + 1];
    3226             :                     }
    3227             :                 }
    3228             : 
    3229        3710 :                 break;
    3230             :             }
    3231             :     }
    3232             : 
    3233        8504 :     if (overlap)
    3234             :     {
    3235             :         Assert(with >= 0);
    3236          96 :         ereport(ERROR,
    3237             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    3238             :                  errmsg("partition \"%s\" would overlap partition \"%s\"",
    3239             :                         relname, get_rel_name(partdesc->oids[with])),
    3240             :                  parser_errposition(pstate, overlap_location)));
    3241             :     }
    3242             : }
    3243             : 
    3244             : /*
    3245             :  * check_default_partition_contents
    3246             :  *
    3247             :  * This function checks if there exists a row in the default partition that
    3248             :  * would properly belong to the new partition being added.  If it finds one,
    3249             :  * it throws an error.
    3250             :  */
    3251             : void
    3252         336 : check_default_partition_contents(Relation parent, Relation default_rel,
    3253             :                                  PartitionBoundSpec *new_spec)
    3254             : {
    3255             :     List       *new_part_constraints;
    3256             :     List       *def_part_constraints;
    3257             :     List       *all_parts;
    3258             :     ListCell   *lc;
    3259             : 
    3260         672 :     new_part_constraints = (new_spec->strategy == PARTITION_STRATEGY_LIST)
    3261         150 :         ? get_qual_for_list(parent, new_spec)
    3262         336 :         : get_qual_for_range(parent, new_spec, false);
    3263             :     def_part_constraints =
    3264         336 :         get_proposed_default_constraint(new_part_constraints);
    3265             : 
    3266             :     /*
    3267             :      * Map the Vars in the constraint expression from parent's attnos to
    3268             :      * default_rel's.
    3269             :      */
    3270             :     def_part_constraints =
    3271         336 :         map_partition_varattnos(def_part_constraints, 1, default_rel,
    3272             :                                 parent);
    3273             : 
    3274             :     /*
    3275             :      * If the existing constraints on the default partition imply that it will
    3276             :      * not contain any row that would belong to the new partition, we can
    3277             :      * avoid scanning the default partition.
    3278             :      */
    3279         336 :     if (PartConstraintImpliedByRelConstraint(default_rel, def_part_constraints))
    3280             :     {
    3281          14 :         ereport(DEBUG1,
    3282             :                 (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
    3283             :                                  RelationGetRelationName(default_rel))));
    3284          14 :         return;
    3285             :     }
    3286             : 
    3287             :     /*
    3288             :      * Scan the default partition and its subpartitions, and check for rows
    3289             :      * that do not satisfy the revised partition constraints.
    3290             :      */
    3291         322 :     if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
    3292          52 :         all_parts = find_all_inheritors(RelationGetRelid(default_rel),
    3293             :                                         AccessExclusiveLock, NULL);
    3294             :     else
    3295         270 :         all_parts = list_make1_oid(RelationGetRelid(default_rel));
    3296             : 
    3297         768 :     foreach(lc, all_parts)
    3298             :     {
    3299         464 :         Oid         part_relid = lfirst_oid(lc);
    3300             :         Relation    part_rel;
    3301             :         Expr       *partition_constraint;
    3302             :         EState     *estate;
    3303         464 :         ExprState  *partqualstate = NULL;
    3304             :         Snapshot    snapshot;
    3305             :         ExprContext *econtext;
    3306             :         TableScanDesc scan;
    3307             :         MemoryContext oldCxt;
    3308             :         TupleTableSlot *tupslot;
    3309             : 
    3310             :         /* Lock already taken above. */
    3311         464 :         if (part_relid != RelationGetRelid(default_rel))
    3312             :         {
    3313         142 :             part_rel = table_open(part_relid, NoLock);
    3314             : 
    3315             :             /*
    3316             :              * Map the Vars in the constraint expression from default_rel's
    3317             :              * the sub-partition's.
    3318             :              */
    3319         142 :             partition_constraint = make_ands_explicit(def_part_constraints);
    3320             :             partition_constraint = (Expr *)
    3321         142 :                 map_partition_varattnos((List *) partition_constraint, 1,
    3322             :                                         part_rel, default_rel);
    3323             : 
    3324             :             /*
    3325             :              * If the partition constraints on default partition child imply
    3326             :              * that it will not contain any row that would belong to the new
    3327             :              * partition, we can avoid scanning the child table.
    3328             :              */
    3329         142 :             if (PartConstraintImpliedByRelConstraint(part_rel,
    3330             :                                                      def_part_constraints))
    3331             :             {
    3332           8 :                 ereport(DEBUG1,
    3333             :                         (errmsg_internal("updated partition constraint for default partition \"%s\" is implied by existing constraints",
    3334             :                                          RelationGetRelationName(part_rel))));
    3335             : 
    3336           8 :                 table_close(part_rel, NoLock);
    3337           8 :                 continue;
    3338             :             }
    3339             :         }
    3340             :         else
    3341             :         {
    3342         322 :             part_rel = default_rel;
    3343         322 :             partition_constraint = make_ands_explicit(def_part_constraints);
    3344             :         }
    3345             : 
    3346             :         /*
    3347             :          * Only RELKIND_RELATION relations (i.e. leaf partitions) need to be
    3348             :          * scanned.
    3349             :          */
    3350         456 :         if (part_rel->rd_rel->relkind != RELKIND_RELATION)
    3351             :         {
    3352          52 :             if (part_rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
    3353           0 :                 ereport(WARNING,
    3354             :                         (errcode(ERRCODE_CHECK_VIOLATION),
    3355             :                          errmsg("skipped scanning foreign table \"%s\" which is a partition of default partition \"%s\"",
    3356             :                                 RelationGetRelationName(part_rel),
    3357             :                                 RelationGetRelationName(default_rel))));
    3358             : 
    3359          52 :             if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
    3360           0 :                 table_close(part_rel, NoLock);
    3361             : 
    3362          52 :             continue;
    3363             :         }
    3364             : 
    3365         404 :         estate = CreateExecutorState();
    3366             : 
    3367             :         /* Build expression execution states for partition check quals */
    3368         404 :         partqualstate = ExecPrepareExpr(partition_constraint, estate);
    3369             : 
    3370         404 :         econtext = GetPerTupleExprContext(estate);
    3371         404 :         snapshot = RegisterSnapshot(GetLatestSnapshot());
    3372         404 :         tupslot = table_slot_create(part_rel, &estate->es_tupleTable);
    3373         404 :         scan = table_beginscan(part_rel, snapshot, 0, NULL);
    3374             : 
    3375             :         /*
    3376             :          * Switch to per-tuple memory context and reset it for each tuple
    3377             :          * produced, so we don't leak memory.
    3378             :          */
    3379         404 :         oldCxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
    3380             : 
    3381         452 :         while (table_scan_getnextslot(scan, ForwardScanDirection, tupslot))
    3382             :         {
    3383          66 :             econtext->ecxt_scantuple = tupslot;
    3384             : 
    3385          66 :             if (!ExecCheck(partqualstate, econtext))
    3386          18 :                 ereport(ERROR,
    3387             :                         (errcode(ERRCODE_CHECK_VIOLATION),
    3388             :                          errmsg("updated partition constraint for default partition \"%s\" would be violated by some row",
    3389             :                                 RelationGetRelationName(default_rel)),
    3390             :                          errtable(default_rel)));
    3391             : 
    3392          48 :             ResetExprContext(econtext);
    3393          48 :             CHECK_FOR_INTERRUPTS();
    3394             :         }
    3395             : 
    3396         386 :         MemoryContextSwitchTo(oldCxt);
    3397         386 :         table_endscan(scan);
    3398         386 :         UnregisterSnapshot(snapshot);
    3399         386 :         ExecDropSingleTupleTableSlot(tupslot);
    3400         386 :         FreeExecutorState(estate);
    3401             : 
    3402         386 :         if (RelationGetRelid(default_rel) != RelationGetRelid(part_rel))
    3403         134 :             table_close(part_rel, NoLock);  /* keep the lock until commit */
    3404             :     }
    3405             : }
    3406             : 
    3407             : /*
    3408             :  * get_hash_partition_greatest_modulus
    3409             :  *
    3410             :  * Returns the greatest modulus of the hash partition bound.
    3411             :  * This is no longer used in the core code, but we keep it around
    3412             :  * in case external modules are using it.
    3413             :  */
    3414             : int
    3415           0 : get_hash_partition_greatest_modulus(PartitionBoundInfo bound)
    3416             : {
    3417             :     Assert(bound && bound->strategy == PARTITION_STRATEGY_HASH);
    3418           0 :     return bound->nindexes;
    3419             : }
    3420             : 
    3421             : /*
    3422             :  * make_one_partition_rbound
    3423             :  *
    3424             :  * Return a PartitionRangeBound given a list of PartitionRangeDatum elements
    3425             :  * and a flag telling whether the bound is lower or not.  Made into a function
    3426             :  * because there are multiple sites that want to use this facility.
    3427             :  */
    3428             : static PartitionRangeBound *
    3429       33064 : make_one_partition_rbound(PartitionKey key, int index, List *datums, bool lower)
    3430             : {
    3431             :     PartitionRangeBound *bound;
    3432             :     ListCell   *lc;
    3433             :     int         i;
    3434             : 
    3435             :     Assert(datums != NIL);
    3436             : 
    3437       33064 :     bound = (PartitionRangeBound *) palloc0(sizeof(PartitionRangeBound));
    3438       33064 :     bound->index = index;
    3439       33064 :     bound->datums = (Datum *) palloc0(key->partnatts * sizeof(Datum));
    3440       33064 :     bound->kind = (PartitionRangeDatumKind *) palloc0(key->partnatts *
    3441             :                                                       sizeof(PartitionRangeDatumKind));
    3442       33064 :     bound->lower = lower;
    3443             : 
    3444       33064 :     i = 0;
    3445       75372 :     foreach(lc, datums)
    3446             :     {
    3447       42308 :         PartitionRangeDatum *datum = lfirst_node(PartitionRangeDatum, lc);
    3448             : 
    3449             :         /* What's contained in this range datum? */
    3450       42308 :         bound->kind[i] = datum->kind;
    3451             : 
    3452       42308 :         if (datum->kind == PARTITION_RANGE_DATUM_VALUE)
    3453             :         {
    3454       38940 :             Const      *val = castNode(Const, datum->value);
    3455             : 
    3456       38940 :             if (val->constisnull)
    3457           0 :                 elog(ERROR, "invalid range bound datum");
    3458       38940 :             bound->datums[i] = val->constvalue;
    3459             :         }
    3460             : 
    3461       42308 :         i++;
    3462             :     }
    3463             : 
    3464       33064 :     return bound;
    3465             : }
    3466             : 
    3467             : /*
    3468             :  * partition_rbound_cmp
    3469             :  *
    3470             :  * For two range bounds this decides whether the 1st one (specified by
    3471             :  * datums1, kind1, and lower1) is <, =, or > the bound specified in *b2.
    3472             :  *
    3473             :  * 0 is returned if they are equal, otherwise a non-zero integer whose sign
    3474             :  * indicates the ordering, and whose absolute value gives the 1-based
    3475             :  * partition key number of the first mismatching column.
    3476             :  *
    3477             :  * partnatts, partsupfunc and partcollation give the number of attributes in the
    3478             :  * bounds to be compared, comparison function to be used and the collations of
    3479             :  * attributes, respectively.
    3480             :  *
    3481             :  * Note that if the values of the two range bounds compare equal, then we take
    3482             :  * into account whether they are upper or lower bounds, and an upper bound is
    3483             :  * considered to be smaller than a lower bound. This is important to the way
    3484             :  * that RelationBuildPartitionDesc() builds the PartitionBoundInfoData
    3485             :  * structure, which only stores the upper bound of a common boundary between
    3486             :  * two contiguous partitions.
    3487             :  */
    3488             : static int32
    3489       33032 : partition_rbound_cmp(int partnatts, FmgrInfo *partsupfunc,
    3490             :                      Oid *partcollation,
    3491             :                      Datum *datums1, PartitionRangeDatumKind *kind1,
    3492             :                      bool lower1, PartitionRangeBound *b2)
    3493             : {
    3494       33032 :     int32       colnum = 0;
    3495       33032 :     int32       cmpval = 0;     /* placate compiler */
    3496             :     int         i;
    3497       33032 :     Datum      *datums2 = b2->datums;
    3498       33032 :     PartitionRangeDatumKind *kind2 = b2->kind;
    3499       33032 :     bool        lower2 = b2->lower;
    3500             : 
    3501       47226 :     for (i = 0; i < partnatts; i++)
    3502             :     {
    3503             :         /* Track column number in case we need it for result */
    3504       38798 :         colnum++;
    3505             : 
    3506             :         /*
    3507             :          * First, handle cases where the column is unbounded, which should not
    3508             :          * invoke the comparison procedure, and should not consider any later
    3509             :          * columns. Note that the PartitionRangeDatumKind enum elements
    3510             :          * compare the same way as the values they represent.
    3511             :          */
    3512       38798 :         if (kind1[i] < kind2[i])
    3513        1940 :             return -colnum;
    3514       36858 :         else if (kind1[i] > kind2[i])
    3515           6 :             return colnum;
    3516       36852 :         else if (kind1[i] != PARTITION_RANGE_DATUM_VALUE)
    3517             :         {
    3518             :             /*
    3519             :              * The column bounds are both MINVALUE or both MAXVALUE. No later
    3520             :              * columns should be considered, but we still need to compare
    3521             :              * whether they are upper or lower bounds.
    3522             :              */
    3523         258 :             break;
    3524             :         }
    3525             : 
    3526       36594 :         cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
    3527       36594 :                                                  partcollation[i],
    3528       36594 :                                                  datums1[i],
    3529       36594 :                                                  datums2[i]));
    3530       36594 :         if (cmpval != 0)
    3531       22400 :             break;
    3532             :     }
    3533             : 
    3534             :     /*
    3535             :      * If the comparison is anything other than equal, we're done. If they
    3536             :      * compare equal though, we still have to consider whether the boundaries
    3537             :      * are inclusive or exclusive.  Exclusive one is considered smaller of the
    3538             :      * two.
    3539             :      */
    3540       31086 :     if (cmpval == 0 && lower1 != lower2)
    3541        7492 :         cmpval = lower1 ? 1 : -1;
    3542             : 
    3543       31086 :     return cmpval == 0 ? 0 : (cmpval < 0 ? -colnum : colnum);
    3544             : }
    3545             : 
    3546             : /*
    3547             :  * partition_rbound_datum_cmp
    3548             :  *
    3549             :  * Return whether range bound (specified in rb_datums and rb_kind)
    3550             :  * is <, =, or > partition key of tuple (tuple_datums)
    3551             :  *
    3552             :  * n_tuple_datums, partsupfunc and partcollation give number of attributes in
    3553             :  * the bounds to be compared, comparison function to be used and the collations
    3554             :  * of attributes resp.
    3555             :  */
    3556             : int32
    3557     1554912 : partition_rbound_datum_cmp(FmgrInfo *partsupfunc, Oid *partcollation,
    3558             :                            Datum *rb_datums, PartitionRangeDatumKind *rb_kind,
    3559             :                            Datum *tuple_datums, int n_tuple_datums)
    3560             : {
    3561             :     int         i;
    3562     1554912 :     int32       cmpval = -1;
    3563             : 
    3564     1622954 :     for (i = 0; i < n_tuple_datums; i++)
    3565             :     {
    3566     1560396 :         if (rb_kind[i] == PARTITION_RANGE_DATUM_MINVALUE)
    3567       68368 :             return -1;
    3568     1492028 :         else if (rb_kind[i] == PARTITION_RANGE_DATUM_MAXVALUE)
    3569       68744 :             return 1;
    3570             : 
    3571     1423284 :         cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[i],
    3572     1423284 :                                                  partcollation[i],
    3573     1423284 :                                                  rb_datums[i],
    3574     1423284 :                                                  tuple_datums[i]));
    3575     1423284 :         if (cmpval != 0)
    3576     1355242 :             break;
    3577             :     }
    3578             : 
    3579     1417800 :     return cmpval;
    3580             : }
    3581             : 
    3582             : /*
    3583             :  * partition_hbound_cmp
    3584             :  *
    3585             :  * Compares modulus first, then remainder if modulus is equal.
    3586             :  */
    3587             : static int32
    3588        1478 : partition_hbound_cmp(int modulus1, int remainder1, int modulus2, int remainder2)
    3589             : {
    3590        1478 :     if (modulus1 < modulus2)
    3591         174 :         return -1;
    3592        1304 :     if (modulus1 > modulus2)
    3593          60 :         return 1;
    3594        1244 :     if (modulus1 == modulus2 && remainder1 != remainder2)
    3595        1244 :         return (remainder1 > remainder2) ? 1 : -1;
    3596           0 :     return 0;
    3597             : }
    3598             : 
    3599             : /*
    3600             :  * partition_list_bsearch
    3601             :  *      Returns the index of the greatest bound datum that is less than equal
    3602             :  *      to the given value or -1 if all of the bound datums are greater
    3603             :  *
    3604             :  * *is_equal is set to true if the bound datum at the returned index is equal
    3605             :  * to the input value.
    3606             :  */
    3607             : int
    3608      155806 : partition_list_bsearch(FmgrInfo *partsupfunc, Oid *partcollation,
    3609             :                        PartitionBoundInfo boundinfo,
    3610             :                        Datum value, bool *is_equal)
    3611             : {
    3612             :     int         lo,
    3613             :                 hi,
    3614             :                 mid;
    3615             : 
    3616      155806 :     lo = -1;
    3617      155806 :     hi = boundinfo->ndatums - 1;
    3618      313982 :     while (lo < hi)
    3619             :     {
    3620             :         int32       cmpval;
    3621             : 
    3622      306676 :         mid = (lo + hi + 1) / 2;
    3623      306676 :         cmpval = DatumGetInt32(FunctionCall2Coll(&partsupfunc[0],
    3624             :                                                  partcollation[0],
    3625      306676 :                                                  boundinfo->datums[mid][0],
    3626             :                                                  value));
    3627      306676 :         if (cmpval <= 0)
    3628             :         {
    3629      261532 :             lo = mid;
    3630      261532 :             *is_equal = (cmpval == 0);
    3631      261532 :             if (*is_equal)
    3632      148500 :                 break;
    3633             :         }
    3634             :         else
    3635       45144 :             hi = mid - 1;
    3636             :     }
    3637             : 
    3638      155806 :     return lo;
    3639             : }
    3640             : 
    3641             : /*
    3642             :  * partition_range_bsearch
    3643             :  *      Returns the index of the greatest range bound that is less than or
    3644             :  *      equal to the given range bound or -1 if all of the range bounds are
    3645             :  *      greater
    3646             :  *
    3647             :  * Upon return from this function, *cmpval is set to 0 if the bound at the
    3648             :  * returned index matches the input range bound exactly, otherwise a
    3649             :  * non-zero integer whose sign indicates the ordering, and whose absolute
    3650             :  * value gives the 1-based partition key number of the first mismatching
    3651             :  * column.
    3652             :  */
    3653             : static int
    3654        2050 : partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
    3655             :                         Oid *partcollation,
    3656             :                         PartitionBoundInfo boundinfo,
    3657             :                         PartitionRangeBound *probe, int32 *cmpval)
    3658             : {
    3659             :     int         lo,
    3660             :                 hi,
    3661             :                 mid;
    3662             : 
    3663        2050 :     lo = -1;
    3664        2050 :     hi = boundinfo->ndatums - 1;
    3665        6422 :     while (lo < hi)
    3666             :     {
    3667        4390 :         mid = (lo + hi + 1) / 2;
    3668        8780 :         *cmpval = partition_rbound_cmp(partnatts, partsupfunc,
    3669             :                                        partcollation,
    3670        4390 :                                        boundinfo->datums[mid],
    3671        4390 :                                        boundinfo->kind[mid],
    3672        4390 :                                        (boundinfo->indexes[mid] == -1),
    3673             :                                        probe);
    3674        4390 :         if (*cmpval <= 0)
    3675             :         {
    3676        4252 :             lo = mid;
    3677        4252 :             if (*cmpval == 0)
    3678          18 :                 break;
    3679             :         }
    3680             :         else
    3681         138 :             hi = mid - 1;
    3682             :     }
    3683             : 
    3684        2050 :     return lo;
    3685             : }
    3686             : 
    3687             : /*
    3688             :  * partition_range_datum_bsearch
    3689             :  *      Returns the index of the greatest range bound that is less than or
    3690             :  *      equal to the given tuple or -1 if all of the range bounds are greater
    3691             :  *
    3692             :  * *is_equal is set to true if the range bound at the returned index is equal
    3693             :  * to the input tuple.
    3694             :  */
    3695             : int
    3696      493034 : partition_range_datum_bsearch(FmgrInfo *partsupfunc, Oid *partcollation,
    3697             :                               PartitionBoundInfo boundinfo,
    3698             :                               int nvalues, Datum *values, bool *is_equal)
    3699             : {
    3700             :     int         lo,
    3701             :                 hi,
    3702             :                 mid;
    3703             : 
    3704      493034 :     lo = -1;
    3705      493034 :     hi = boundinfo->ndatums - 1;
    3706     1518502 :     while (lo < hi)
    3707             :     {
    3708             :         int32       cmpval;
    3709             : 
    3710     1081434 :         mid = (lo + hi + 1) / 2;
    3711     1081434 :         cmpval = partition_rbound_datum_cmp(partsupfunc,
    3712             :                                             partcollation,
    3713     1081434 :                                             boundinfo->datums[mid],
    3714     1081434 :                                             boundinfo->kind[mid],
    3715             :                                             values,
    3716             :                                             nvalues);
    3717     1081434 :         if (cmpval <= 0)
    3718             :         {
    3719      622696 :             lo = mid;
    3720      622696 :             *is_equal = (cmpval == 0);
    3721             : 
    3722      622696 :             if (*is_equal)
    3723       55966 :                 break;
    3724             :         }
    3725             :         else
    3726      458738 :             hi = mid - 1;
    3727             :     }
    3728             : 
    3729      493034 :     return lo;
    3730             : }
    3731             : 
    3732             : /*
    3733             :  * partition_hash_bsearch
    3734             :  *      Returns the index of the greatest (modulus, remainder) pair that is
    3735             :  *      less than or equal to the given (modulus, remainder) pair or -1 if
    3736             :  *      all of them are greater
    3737             :  */
    3738             : int
    3739         380 : partition_hash_bsearch(PartitionBoundInfo boundinfo,
    3740             :                        int modulus, int remainder)
    3741             : {
    3742             :     int         lo,
    3743             :                 hi,
    3744             :                 mid;
    3745             : 
    3746         380 :     lo = -1;
    3747         380 :     hi = boundinfo->ndatums - 1;
    3748         984 :     while (lo < hi)
    3749             :     {
    3750             :         int32       cmpval,
    3751             :                     bound_modulus,
    3752             :                     bound_remainder;
    3753             : 
    3754         604 :         mid = (lo + hi + 1) / 2;
    3755         604 :         bound_modulus = DatumGetInt32(boundinfo->datums[mid][0]);
    3756         604 :         bound_remainder = DatumGetInt32(boundinfo->datums[mid][1]);
    3757         604 :         cmpval = partition_hbound_cmp(bound_modulus, bound_remainder,
    3758             :                                       modulus, remainder);
    3759         604 :         if (cmpval <= 0)
    3760             :         {
    3761         542 :             lo = mid;
    3762             : 
    3763         542 :             if (cmpval == 0)
    3764           0 :                 break;
    3765             :         }
    3766             :         else
    3767          62 :             hi = mid - 1;
    3768             :     }
    3769             : 
    3770         380 :     return lo;
    3771             : }
    3772             : 
    3773             : /*
    3774             :  * qsort_partition_hbound_cmp
    3775             :  *
    3776             :  * Hash bounds are sorted by modulus, then by remainder.
    3777             :  */
    3778             : static int32
    3779         874 : qsort_partition_hbound_cmp(const void *a, const void *b)
    3780             : {
    3781         874 :     const PartitionHashBound *h1 = (const PartitionHashBound *) a;
    3782         874 :     const PartitionHashBound *h2 = (const PartitionHashBound *) b;
    3783             : 
    3784         874 :     return partition_hbound_cmp(h1->modulus, h1->remainder,
    3785             :                                 h2->modulus, h2->remainder);
    3786             : }
    3787             : 
    3788             : /*
    3789             :  * qsort_partition_list_value_cmp
    3790             :  *
    3791             :  * Compare two list partition bound datums.
    3792             :  */
    3793             : static int32
    3794       20012 : qsort_partition_list_value_cmp(const void *a, const void *b, void *arg)
    3795             : {
    3796       20012 :     Datum       val1 = ((const PartitionListValue *) a)->value,
    3797       20012 :                 val2 = ((const PartitionListValue *) b)->value;
    3798       20012 :     PartitionKey key = (PartitionKey) arg;
    3799             : 
    3800       20012 :     return DatumGetInt32(FunctionCall2Coll(&key->partsupfunc[0],
    3801       20012 :                                            key->partcollation[0],
    3802             :                                            val1, val2));
    3803             : }
    3804             : 
    3805             : /*
    3806             :  * qsort_partition_rbound_cmp
    3807             :  *
    3808             :  * Used when sorting range bounds across all range partitions.
    3809             :  */
    3810             : static int32
    3811       20690 : qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
    3812             : {
    3813       20690 :     PartitionRangeBound *b1 = (*(PartitionRangeBound *const *) a);
    3814       20690 :     PartitionRangeBound *b2 = (*(PartitionRangeBound *const *) b);
    3815       20690 :     PartitionKey key = (PartitionKey) arg;
    3816             : 
    3817       20690 :     return compare_range_bounds(key->partnatts, key->partsupfunc,
    3818             :                                 key->partcollation,
    3819             :                                 b1, b2);
    3820             : }
    3821             : 
    3822             : /*
    3823             :  * get_partition_operator
    3824             :  *
    3825             :  * Return oid of the operator of the given strategy for the given partition
    3826             :  * key column.  It is assumed that the partitioning key is of the same type as
    3827             :  * the chosen partitioning opclass, or at least binary-compatible.  In the
    3828             :  * latter case, *need_relabel is set to true if the opclass is not of a
    3829             :  * polymorphic type (indicating a RelabelType node needed on top), otherwise
    3830             :  * false.
    3831             :  */
    3832             : static Oid
    3833       12686 : get_partition_operator(PartitionKey key, int col, StrategyNumber strategy,
    3834             :                        bool *need_relabel)
    3835             : {
    3836             :     Oid         operoid;
    3837             : 
    3838             :     /*
    3839             :      * Get the operator in the partitioning opfamily using the opclass'
    3840             :      * declared input type as both left- and righttype.
    3841             :      */
    3842       12686 :     operoid = get_opfamily_member(key->partopfamily[col],
    3843       12686 :                                   key->partopcintype[col],
    3844       12686 :                                   key->partopcintype[col],
    3845             :                                   strategy);
    3846       12686 :     if (!OidIsValid(operoid))
    3847           0 :         elog(ERROR, "missing operator %d(%u,%u) in partition opfamily %u",
    3848             :              strategy, key->partopcintype[col], key->partopcintype[col],
    3849             :              key->partopfamily[col]);
    3850             : 
    3851             :     /*
    3852             :      * If the partition key column is not of the same type as the operator
    3853             :      * class and not polymorphic, tell caller to wrap the non-Const expression
    3854             :      * in a RelabelType.  This matches what parse_coerce.c does.
    3855             :      */
    3856       25434 :     *need_relabel = (key->parttypid[col] != key->partopcintype[col] &&
    3857       12742 :                      key->partopcintype[col] != RECORDOID &&
    3858          56 :                      !IsPolymorphicType(key->partopcintype[col]));
    3859             : 
    3860       12686 :     return operoid;
    3861             : }
    3862             : 
    3863             : /*
    3864             :  * make_partition_op_expr
    3865             :  *      Returns an Expr for the given partition key column with arg1 and
    3866             :  *      arg2 as its leftop and rightop, respectively
    3867             :  */
    3868             : static Expr *
    3869       12686 : make_partition_op_expr(PartitionKey key, int keynum,
    3870             :                        uint16 strategy, Expr *arg1, Expr *arg2)
    3871             : {
    3872             :     Oid         operoid;
    3873       12686 :     bool        need_relabel = false;
    3874       12686 :     Expr       *result = NULL;
    3875             : 
    3876             :     /* Get the correct btree operator for this partitioning column */
    3877       12686 :     operoid = get_partition_operator(key, keynum, strategy, &need_relabel);
    3878             : 
    3879             :     /*
    3880             :      * Chosen operator may be such that the non-Const operand needs to be
    3881             :      * coerced, so apply the same; see the comment in
    3882             :      * get_partition_operator().
    3883             :      */
    3884       12686 :     if (!IsA(arg1, Const) &&
    3885        9536 :         (need_relabel ||
    3886        9490 :          key->partcollation[keynum] != key->parttypcoll[keynum]))
    3887          46 :         arg1 = (Expr *) makeRelabelType(arg1,
    3888          46 :                                         key->partopcintype[keynum],
    3889             :                                         -1,
    3890          46 :                                         key->partcollation[keynum],
    3891             :                                         COERCE_EXPLICIT_CAST);
    3892             : 
    3893             :     /* Generate the actual expression */
    3894       12686 :     switch (key->strategy)
    3895             :     {
    3896        2360 :         case PARTITION_STRATEGY_LIST:
    3897             :             {
    3898        2360 :                 List       *elems = (List *) arg2;
    3899        2360 :                 int         nelems = list_length(elems);
    3900             : 
    3901             :                 Assert(nelems >= 1);
    3902             :                 Assert(keynum == 0);
    3903             : 
    3904        3152 :                 if (nelems > 1 &&
    3905         792 :                     !type_is_array(key->parttypid[keynum]))
    3906         786 :                 {
    3907             :                     ArrayExpr  *arrexpr;
    3908             :                     ScalarArrayOpExpr *saopexpr;
    3909             : 
    3910             :                     /* Construct an ArrayExpr for the right-hand inputs */
    3911         786 :                     arrexpr = makeNode(ArrayExpr);
    3912         786 :                     arrexpr->array_typeid =
    3913         786 :                         get_array_type(key->parttypid[keynum]);
    3914         786 :                     arrexpr->array_collid = key->parttypcoll[keynum];
    3915         786 :                     arrexpr->element_typeid = key->parttypid[keynum];
    3916         786 :                     arrexpr->elements = elems;
    3917         786 :                     arrexpr->multidims = false;
    3918         786 :                     arrexpr->location = -1;
    3919             : 
    3920             :                     /* Build leftop = ANY (rightop) */
    3921         786 :                     saopexpr = makeNode(ScalarArrayOpExpr);
    3922         786 :                     saopexpr->opno = operoid;
    3923         786 :                     saopexpr->opfuncid = get_opcode(operoid);
    3924         786 :                     saopexpr->hashfuncid = InvalidOid;
    3925         786 :                     saopexpr->negfuncid = InvalidOid;
    3926         786 :                     saopexpr->useOr = true;
    3927         786 :                     saopexpr->inputcollid = key->partcollation[keynum];
    3928         786 :                     saopexpr->args = list_make2(arg1, arrexpr);
    3929         786 :                     saopexpr->location = -1;
    3930             : 
    3931         786 :                     result = (Expr *) saopexpr;
    3932             :                 }
    3933             :                 else
    3934             :                 {
    3935        1574 :                     List       *elemops = NIL;
    3936             :                     ListCell   *lc;
    3937             : 
    3938        3154 :                     foreach(lc, elems)
    3939             :                     {
    3940        1580 :                         Expr       *elem = lfirst(lc),
    3941             :                                    *elemop;
    3942             : 
    3943        1580 :                         elemop = make_opclause(operoid,
    3944             :                                                BOOLOID,
    3945             :                                                false,
    3946             :                                                arg1, elem,
    3947             :                                                InvalidOid,
    3948        1580 :                                                key->partcollation[keynum]);
    3949        1580 :                         elemops = lappend(elemops, elemop);
    3950             :                     }
    3951             : 
    3952        1574 :                     result = nelems > 1 ? makeBoolExpr(OR_EXPR, elemops, -1) : linitial(elemops);
    3953             :                 }
    3954        2360 :                 break;
    3955             :             }
    3956             : 
    3957       10326 :         case PARTITION_STRATEGY_RANGE:
    3958       10326 :             result = make_opclause(operoid,
    3959             :                                    BOOLOID,
    3960             :                                    false,
    3961             :                                    arg1, arg2,
    3962             :                                    InvalidOid,
    3963       10326 :                                    key->partcollation[keynum]);
    3964       10326 :             break;
    3965             : 
    3966           0 :         case PARTITION_STRATEGY_HASH:
    3967             :             Assert(false);
    3968           0 :             break;
    3969             :     }
    3970             : 
    3971       12686 :     return result;
    3972             : }
    3973             : 
    3974             : /*
    3975             :  * get_qual_for_hash
    3976             :  *
    3977             :  * Returns a CHECK constraint expression to use as a hash partition's
    3978             :  * constraint, given the parent relation and partition bound structure.
    3979             :  *
    3980             :  * The partition constraint for a hash partition is always a call to the
    3981             :  * built-in function satisfies_hash_partition().
    3982             :  */
    3983             : static List *
    3984         152 : get_qual_for_hash(Relation parent, PartitionBoundSpec *spec)
    3985             : {
    3986         152 :     PartitionKey key = RelationGetPartitionKey(parent);
    3987             :     FuncExpr   *fexpr;
    3988             :     Node       *relidConst;
    3989             :     Node       *modulusConst;
    3990             :     Node       *remainderConst;
    3991             :     List       *args;
    3992             :     ListCell   *partexprs_item;
    3993             :     int         i;
    3994             : 
    3995             :     /* Fixed arguments. */
    3996         152 :     relidConst = (Node *) makeConst(OIDOID,
    3997             :                                     -1,
    3998             :                                     InvalidOid,
    3999             :                                     sizeof(Oid),
    4000             :                                     ObjectIdGetDatum(RelationGetRelid(parent)),
    4001             :                                     false,
    4002             :                                     true);
    4003             : 
    4004         152 :     modulusConst = (Node *) makeConst(INT4OID,
    4005             :                                       -1,
    4006             :                                       InvalidOid,
    4007             :                                       sizeof(int32),
    4008             :                                       Int32GetDatum(spec->modulus),
    4009             :                                       false,
    4010             :                                       true);
    4011             : 
    4012         152 :     remainderConst = (Node *) makeConst(INT4OID,
    4013             :                                         -1,
    4014             :                                         InvalidOid,
    4015             :                                         sizeof(int32),
    4016             :                                         Int32GetDatum(spec->remainder),
    4017             :                                         false,
    4018             :                                         true);
    4019             : 
    4020         152 :     args = list_make3(relidConst, modulusConst, remainderConst);
    4021         152 :     partexprs_item = list_head(key->partexprs);
    4022             : 
    4023             :     /* Add an argument for each key column. */
    4024         322 :     for (i = 0; i < key->partnatts; i++)
    4025             :     {
    4026             :         Node       *keyCol;
    4027             : 
    4028             :         /* Left operand */
    4029         170 :         if (key->partattrs[i] != 0)
    4030             :         {
    4031         164 :             keyCol = (Node *) makeVar(1,
    4032         164 :                                       key->partattrs[i],
    4033         164 :                                       key->parttypid[i],
    4034         164 :                                       key->parttypmod[i],
    4035         164 :                                       key->parttypcoll[i],
    4036             :                                       0);
    4037             :         }
    4038             :         else
    4039             :         {
    4040           6 :             keyCol = (Node *) copyObject(lfirst(partexprs_item));
    4041           6 :             partexprs_item = lnext(key->partexprs, partexprs_item);
    4042             :         }
    4043             : 
    4044         170 :         args = lappend(args, keyCol);
    4045             :     }
    4046             : 
    4047         152 :     fexpr = makeFuncExpr(F_SATISFIES_HASH_PARTITION,
    4048             :                          BOOLOID,
    4049             :                          args,
    4050             :                          InvalidOid,
    4051             :                          InvalidOid,
    4052             :                          COERCE_EXPLICIT_CALL);
    4053             : 
    4054         152 :     return list_make1(fexpr);
    4055             : }
    4056             : 
    4057             : /*
    4058             :  * get_qual_for_list
    4059             :  *
    4060             :  * Returns an implicit-AND list of expressions to use as a list partition's
    4061             :  * constraint, given the parent relation and partition bound structure.
    4062             :  *
    4063             :  * The function returns NIL for a default partition when it's the only
    4064             :  * partition since in that case there is no constraint.
    4065             :  */
    4066             : static List *
    4067        2422 : get_qual_for_list(Relation parent, PartitionBoundSpec *spec)
    4068             : {
    4069        2422 :     PartitionKey key = RelationGetPartitionKey(parent);
    4070             :     List       *result;
    4071             :     Expr       *keyCol;
    4072             :     Expr       *opexpr;
    4073             :     NullTest   *nulltest;
    4074             :     ListCell   *cell;
    4075        2422 :     List       *elems = NIL;
    4076        2422 :     bool        list_has_null = false;
    4077             : 
    4078             :     /*
    4079             :      * Only single-column list partitioning is supported, so we are worried
    4080             :      * only about the partition key with index 0.
    4081             :      */
    4082             :     Assert(key->partnatts == 1);
    4083             : 
    4084             :     /* Construct Var or expression representing the partition column */
    4085        2422 :     if (key->partattrs[0] != 0)
    4086        2314 :         keyCol = (Expr *) makeVar(1,
    4087        2314 :                                   key->partattrs[0],
    4088        2314 :                                   key->parttypid[0],
    4089        2314 :                                   key->parttypmod[0],
    4090        2314 :                                   key->parttypcoll[0],
    4091             :                                   0);
    4092             :     else
    4093         108 :         keyCol = (Expr *) copyObject(linitial(key->partexprs));
    4094             : 
    4095             :     /*
    4096             :      * For default list partition, collect datums for all the partitions. The
    4097             :      * default partition constraint should check that the partition key is
    4098             :      * equal to none of those.
    4099             :      */
    4100        2422 :     if (spec->is_default)
    4101             :     {
    4102             :         int         i;
    4103         244 :         int         ndatums = 0;
    4104         244 :         PartitionDesc pdesc = RelationGetPartitionDesc(parent, false);
    4105         244 :         PartitionBoundInfo boundinfo = pdesc->boundinfo;
    4106             : 
    4107         244 :         if (boundinfo)
    4108             :         {
    4109         244 :             ndatums = boundinfo->ndatums;
    4110             : 
    4111         244 :             if (partition_bound_accepts_nulls(boundinfo))
    4112          48 :                 list_has_null = true;
    4113             :         }
    4114             : 
    4115             :         /*
    4116             :          * If default is the only partition, there need not be any partition
    4117             :          * constraint on it.
    4118             :          */
    4119         244 :         if (ndatums == 0 && !list_has_null)
    4120          38 :             return NIL;
    4121             : 
    4122        1026 :         for (i = 0; i < ndatums; i++)
    4123             :         {
    4124             :             Const      *val;
    4125             : 
    4126             :             /*
    4127             :              * Construct Const from known-not-null datum.  We must be careful
    4128             :              * to copy the value, because our result has to be able to outlive
    4129             :              * the relcache entry we're copying from.
    4130             :              */
    4131        1640 :             val = makeConst(key->parttypid[0],
    4132         820 :                             key->parttypmod[0],
    4133         820 :                             key->parttypcoll[0],
    4134         820 :                             key->parttyplen[0],
    4135         820 :                             datumCopy(*boundinfo->datums[i],
    4136         820 :                                       key->parttypbyval[0],
    4137         820 :                                       key->parttyplen[0]),
    4138             :                             false,  /* isnull */
    4139         820 :                             key->parttypbyval[0]);
    4140             : 
    4141         820 :             elems = lappend(elems, val);
    4142             :         }
    4143             :     }
    4144             :     else
    4145             :     {
    4146             :         /*
    4147             :          * Create list of Consts for the allowed values, excluding any nulls.
    4148             :          */
    4149        5464 :         foreach(cell, spec->listdatums)
    4150             :         {
    4151        3286 :             Const      *val = lfirst_node(Const, cell);
    4152             : 
    4153        3286 :             if (val->constisnull)
    4154          54 :                 list_has_null = true;
    4155             :             else
    4156        3232 :                 elems = lappend(elems, copyObject(val));
    4157             :         }
    4158             :     }
    4159             : 
    4160        2384 :     if (elems)
    4161             :     {
    4162             :         /*
    4163             :          * Generate the operator expression from the non-null partition
    4164             :          * values.
    4165             :          */
    4166        2360 :         opexpr = make_partition_op_expr(key, 0, BTEqualStrategyNumber,
    4167             :                                         keyCol, (Expr *) elems);
    4168             :     }
    4169             :     else
    4170             :     {
    4171             :         /*
    4172             :          * If there are no partition values, we don't need an operator
    4173             :          * expression.
    4174             :          */
    4175          24 :         opexpr = NULL;
    4176             :     }
    4177             : 
    4178        2384 :     if (!list_has_null)
    4179             :     {
    4180             :         /*
    4181             :          * Gin up a "col IS NOT NULL" test that will be ANDed with the main
    4182             :          * expression.  This might seem redundant, but the partition routing
    4183             :          * machinery needs it.
    4184             :          */
    4185        2282 :         nulltest = makeNode(NullTest);
    4186        2282 :         nulltest->arg = keyCol;
    4187        2282 :         nulltest->nulltesttype = IS_NOT_NULL;
    4188        2282 :         nulltest->argisrow = false;
    4189        2282 :         nulltest->location = -1;
    4190             : 
    4191        2282 :         result = opexpr ? list_make2(nulltest, opexpr) : list_make1(nulltest);
    4192             :     }
    4193             :     else
    4194             :     {
    4195             :         /*
    4196             :          * Gin up a "col IS NULL" test that will be OR'd with the main
    4197             :          * expression.
    4198             :          */
    4199         102 :         nulltest = makeNode(NullTest);
    4200         102 :         nulltest->arg = keyCol;
    4201         102 :         nulltest->nulltesttype = IS_NULL;
    4202         102 :         nulltest->argisrow = false;
    4203         102 :         nulltest->location = -1;
    4204             : 
    4205         102 :         if (opexpr)
    4206             :         {
    4207             :             Expr       *or;
    4208             : 
    4209          78 :             or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
    4210          78 :             result = list_make1(or);
    4211             :         }
    4212             :         else
    4213          24 :             result = list_make1(nulltest);
    4214             :     }
    4215             : 
    4216             :     /*
    4217             :      * Note that, in general, applying NOT to a constraint expression doesn't
    4218             :      * necessarily invert the set of rows it accepts, because NOT (NULL) is
    4219             :      * NULL.  However, the partition constraints we construct here never
    4220             :      * evaluate to NULL, so applying NOT works as intended.
    4221             :      */
    4222        2384 :     if (spec->is_default)
    4223             :     {
    4224         206 :         result = list_make1(make_ands_explicit(result));
    4225         206 :         result = list_make1(makeBoolExpr(NOT_EXPR, result, -1));
    4226             :     }
    4227             : 
    4228        2384 :     return result;
    4229             : }
    4230             : 
    4231             : /*
    4232             :  * get_qual_for_range
    4233             :  *
    4234             :  * Returns an implicit-AND list of expressions to use as a range partition's
    4235             :  * constraint, given the parent relation and partition bound structure.
    4236             :  *
    4237             :  * For a multi-column range partition key, say (a, b, c), with (al, bl, cl)
    4238             :  * as the lower bound tuple and (au, bu, cu) as the upper bound tuple, we
    4239             :  * generate an expression tree of the following form:
    4240             :  *
    4241             :  *  (a IS NOT NULL) and (b IS NOT NULL) and (c IS NOT NULL)
    4242             :  *      AND
    4243             :  *  (a > al OR (a = al AND b > bl) OR (a = al AND b = bl AND c >= cl))
    4244             :  *      AND
    4245             :  *  (a < au OR (a = au AND b < bu) OR (a = au AND b = bu AND c < cu))
    4246             :  *
    4247             :  * It is often the case that a prefix of lower and upper bound tuples contains
    4248             :  * the same values, for example, (al = au), in which case, we will emit an
    4249             :  * expression tree of the following form:
    4250             :  *
    4251             :  *  (a IS NOT NULL) and (b IS NOT NULL) and (c IS NOT NULL)
    4252             :  *      AND
    4253             :  *  (a = al)
    4254             :  *      AND
    4255             :  *  (b > bl OR (b = bl AND c >= cl))
    4256             :  *      AND
    4257             :  *  (b < bu OR (b = bu AND c < cu))
    4258             :  *
    4259             :  * If a bound datum is either MINVALUE or MAXVALUE, these expressions are
    4260             :  * simplified using the fact that any value is greater than MINVALUE and less
    4261             :  * than MAXVALUE. So, for example, if cu = MAXVALUE, c < cu is automatically
    4262             :  * true, and we need not emit any expression for it, and the last line becomes
    4263             :  *
    4264             :  *  (b < bu) OR (b = bu), which is simplified to (b <= bu)
    4265             :  *
    4266             :  * In most common cases with only one partition column, say a, the following
    4267             :  * expression tree will be generated: a IS NOT NULL AND a >= al AND a < au
    4268             :  *
    4269             :  * For default partition, it returns the negation of the constraints of all
    4270             :  * the other partitions.
    4271             :  *
    4272             :  * External callers should pass for_default as false; we set it to true only
    4273             :  * when recursing.
    4274             :  */
    4275             : static List *
    4276        3212 : get_qual_for_range(Relation parent, PartitionBoundSpec *spec,
    4277             :                    bool for_default)
    4278             : {
    4279        3212 :     List       *result = NIL;
    4280             :     ListCell   *cell1,
    4281             :                *cell2,
    4282             :                *partexprs_item,
    4283             :                *partexprs_item_saved;
    4284             :     int         i,
    4285             :                 j;
    4286             :     PartitionRangeDatum *ldatum,
    4287             :                *udatum;
    4288        3212 :     PartitionKey key = RelationGetPartitionKey(parent);
    4289             :     Expr       *keyCol;
    4290             :     Const      *lower_val,
    4291             :                *upper_val;
    4292             :     List       *lower_or_arms,
    4293             :                *upper_or_arms;
    4294             :     int         num_or_arms,
    4295             :                 current_or_arm;
    4296             :     ListCell   *lower_or_start_datum,
    4297             :                *upper_or_start_datum;
    4298             :     bool        need_next_lower_arm,
    4299             :                 need_next_upper_arm;
    4300             : 
    4301        3212 :     if (spec->is_default)
    4302             :     {
    4303         222 :         List       *or_expr_args = NIL;
    4304         222 :         PartitionDesc pdesc = RelationGetPartitionDesc(parent, false);
    4305         222 :         Oid        *inhoids = pdesc->oids;
    4306         222 :         int         nparts = pdesc->nparts,
    4307             :                     k;
    4308             : 
    4309         850 :         for (k = 0; k < nparts; k++)
    4310             :         {
    4311         628 :             Oid         inhrelid = inhoids[k];
    4312             :             HeapTuple   tuple;
    4313             :             Datum       datum;
    4314             :             PartitionBoundSpec *bspec;
    4315             : 
    4316         628 :             tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(inhrelid));
    4317         628 :             if (!HeapTupleIsValid(tuple))
    4318           0 :                 elog(ERROR, "cache lookup failed for relation %u", inhrelid);
    4319             : 
    4320         628 :             datum = SysCacheGetAttrNotNull(RELOID, tuple,
    4321             :                                            Anum_pg_class_relpartbound);
    4322             :             bspec = (PartitionBoundSpec *)
    4323         628 :                 stringToNode(TextDatumGetCString(datum));
    4324         628 :             if (!IsA(bspec, PartitionBoundSpec))
    4325           0 :                 elog(ERROR, "expected PartitionBoundSpec");
    4326             : 
    4327         628 :             if (!bspec->is_default)
    4328             :             {
    4329             :                 List       *part_qual;
    4330             : 
    4331         406 :                 part_qual = get_qual_for_range(parent, bspec, true);
    4332             : 
    4333             :                 /*
    4334             :                  * AND the constraints of the partition and add to
    4335             :                  * or_expr_args
    4336             :                  */
    4337         812 :                 or_expr_args = lappend(or_expr_args, list_length(part_qual) > 1
    4338         388 :                                        ? makeBoolExpr(AND_EXPR, part_qual, -1)
    4339          18 :                                        : linitial(part_qual));
    4340             :             }
    4341         628 :             ReleaseSysCache(tuple);
    4342             :         }
    4343             : 
    4344         222 :         if (or_expr_args != NIL)
    4345             :         {
    4346             :             Expr       *other_parts_constr;
    4347             : 
    4348             :             /*
    4349             :              * Combine the constraints obtained for non-default partitions
    4350             :              * using OR.  As requested, each of the OR's args doesn't include
    4351             :              * the NOT NULL test for partition keys (which is to avoid its
    4352             :              * useless repetition).  Add the same now.
    4353             :              */
    4354             :             other_parts_constr =
    4355         308 :                 makeBoolExpr(AND_EXPR,
    4356             :                              lappend(get_range_nulltest(key),
    4357         154 :                                      list_length(or_expr_args) > 1
    4358         116 :                                      ? makeBoolExpr(OR_EXPR, or_expr_args,
    4359             :                                                     -1)
    4360          38 :                                      : linitial(or_expr_args)),
    4361             :                              -1);
    4362             : 
    4363             :             /*
    4364             :              * Finally, the default partition contains everything *NOT*
    4365             :              * contained in the non-default partitions.
    4366             :              */
    4367         154 :             result = list_make1(makeBoolExpr(NOT_EXPR,
    4368             :                                              list_make1(other_parts_constr), -1));
    4369             :         }
    4370             : 
    4371         222 :         return result;
    4372             :     }
    4373             : 
    4374             :     /*
    4375             :      * If it is the recursive call for default, we skip the get_range_nulltest
    4376             :      * to avoid accumulating the NullTest on the same keys for each partition.
    4377             :      */
    4378        2990 :     if (!for_default)
    4379        2584 :         result = get_range_nulltest(key);
    4380             : 
    4381             :     /*
    4382             :      * Iterate over the key columns and check if the corresponding lower and
    4383             :      * upper datums are equal using the btree equality operator for the
    4384             :      * column's type.  If equal, we emit single keyCol = common_value
    4385             :      * expression.  Starting from the first column for which the corresponding
    4386             :      * lower and upper bound datums are not equal, we generate OR expressions
    4387             :      * as shown in the function's header comment.
    4388             :      */
    4389        2990 :     i = 0;
    4390        2990 :     partexprs_item = list_head(key->partexprs);
    4391        2990 :     partexprs_item_saved = partexprs_item;  /* placate compiler */
    4392        3530 :     forboth(cell1, spec->lowerdatums, cell2, spec->upperdatums)
    4393             :     {
    4394             :         EState     *estate;
    4395             :         MemoryContext oldcxt;
    4396             :         Expr       *test_expr;
    4397             :         ExprState  *test_exprstate;
    4398             :         Datum       test_result;
    4399             :         bool        isNull;
    4400             : 
    4401        3530 :         ldatum = lfirst_node(PartitionRangeDatum, cell1);
    4402        3530 :         udatum = lfirst_node(PartitionRangeDatum, cell2);
    4403             : 
    4404             :         /*
    4405             :          * Since get_range_key_properties() modifies partexprs_item, and we
    4406             :          * might need to start over from the previous expression in the later
    4407             :          * part of this function, save away the current value.
    4408             :          */
    4409        3530 :         partexprs_item_saved = partexprs_item;
    4410             : 
    4411        3530 :         get_range_key_properties(key, i, ldatum, udatum,
    4412             :                                  &partexprs_item,
    4413             :                                  &keyCol,
    4414             :                                  &lower_val, &upper_val);
    4415             : 
    4416             :         /*
    4417             :          * If either value is NULL, the corresponding partition bound is
    4418             :          * either MINVALUE or MAXVALUE, and we treat them as unequal, because
    4419             :          * even if they're the same, there is no common value to equate the
    4420             :          * key column with.
    4421             :          */
    4422        3530 :         if (!lower_val || !upper_val)
    4423             :             break;
    4424             : 
    4425             :         /* Create the test expression */
    4426        3150 :         estate = CreateExecutorState();
    4427        3150 :         oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
    4428        3150 :         test_expr = make_partition_op_expr(key, i, BTEqualStrategyNumber,
    4429             :                                            (Expr *) lower_val,
    4430             :                                            (Expr *) upper_val);
    4431        3150 :         fix_opfuncids((Node *) test_expr);
    4432        3150 :         test_exprstate = ExecInitExpr(test_expr, NULL);
    4433        3150 :         test_result = ExecEvalExprSwitchContext(test_exprstate,
    4434        3150 :                                                 GetPerTupleExprContext(estate),
    4435             :                                                 &isNull);
    4436        3150 :         MemoryContextSwitchTo(oldcxt);
    4437        3150 :         FreeExecutorState(estate);
    4438             : 
    4439             :         /* If not equal, go generate the OR expressions */
    4440        3150 :         if (!DatumGetBool(test_result))
    4441        2610 :             break;
    4442             : 
    4443             :         /*
    4444             :          * The bounds for the last key column can't be equal, because such a
    4445             :          * range partition would never be allowed to be defined (it would have
    4446             :          * an empty range otherwise).
    4447             :          */
    4448         540 :         if (i == key->partnatts - 1)
    4449           0 :             elog(ERROR, "invalid range bound specification");
    4450             : 
    4451             :         /* Equal, so generate keyCol = lower_val expression */
    4452         540 :         result = lappend(result,
    4453         540 :                          make_partition_op_expr(key, i, BTEqualStrategyNumber,
    4454             :                                                 keyCol, (Expr *) lower_val));
    4455             : 
    4456         540 :         i++;
    4457             :     }
    4458             : 
    4459             :     /* First pair of lower_val and upper_val that are not equal. */
    4460        2990 :     lower_or_start_datum = cell1;
    4461        2990 :     upper_or_start_datum = cell2;
    4462             : 
    4463             :     /* OR will have as many arms as there are key columns left. */
    4464        2990 :     num_or_arms = key->partnatts - i;
    4465        2990 :     current_or_arm = 0;
    4466        2990 :     lower_or_arms = upper_or_arms = NIL;
    4467        2990 :     need_next_lower_arm = need_next_upper_arm = true;
    4468        3282 :     while (current_or_arm < num_or_arms)
    4469             :     {
    4470        3282 :         List       *lower_or_arm_args = NIL,
    4471        3282 :                    *upper_or_arm_args = NIL;
    4472             : 
    4473             :         /* Restart scan of columns from the i'th one */
    4474        3282 :         j = i;
    4475        3282 :         partexprs_item = partexprs_item_saved;
    4476             : 
    4477        3658 :         for_both_cell(cell1, spec->lowerdatums, lower_or_start_datum,
    4478             :                       cell2, spec->upperdatums, upper_or_start_datum)
    4479             :         {
    4480        3658 :             PartitionRangeDatum *ldatum_next = NULL,
    4481        3658 :                        *udatum_next = NULL;
    4482             : 
    4483        3658 :             ldatum = lfirst_node(PartitionRangeDatum, cell1);
    4484        3658 :             if (lnext(spec->lowerdatums, cell1))
    4485         746 :                 ldatum_next = castNode(PartitionRangeDatum,
    4486             :                                        lfirst(lnext(spec->lowerdatums, cell1)));
    4487        3658 :             udatum = lfirst_node(PartitionRangeDatum, cell2);
    4488        3658 :             if (lnext(spec->upperdatums, cell2))
    4489         746 :                 udatum_next = castNode(PartitionRangeDatum,
    4490             :                                        lfirst(lnext(spec->upperdatums, cell2)));
    4491        3658 :             get_range_key_properties(key, j, ldatum, udatum,
    4492             :                                      &partexprs_item,
    4493             :                                      &keyCol,
    4494             :                                      &lower_val, &upper_val);
    4495             : 
    4496        3658 :             if (need_next_lower_arm && lower_val)
    4497             :             {
    4498             :                 uint16      strategy;
    4499             : 
    4500             :                 /*
    4501             :                  * For the non-last columns of this arm, use the EQ operator.
    4502             :                  * For the last column of this arm, use GT, unless this is the
    4503             :                  * last column of the whole bound check, or the next bound
    4504             :                  * datum is MINVALUE, in which case use GE.
    4505             :                  */
    4506        3382 :                 if (j - i < current_or_arm)
    4507         322 :                     strategy = BTEqualStrategyNumber;
    4508        3060 :                 else if (j == key->partnatts - 1 ||
    4509         310 :                          (ldatum_next &&
    4510         310 :                           ldatum_next->kind == PARTITION_RANGE_DATUM_MINVALUE))
    4511        2792 :                     strategy = BTGreaterEqualStrategyNumber;
    4512             :                 else
    4513         268 :                     strategy = BTGreaterStrategyNumber;
    4514             : 
    4515        3382 :                 lower_or_arm_args = lappend(lower_or_arm_args,
    4516        3382 :                                             make_partition_op_expr(key, j,
    4517             :                                                                    strategy,
    4518             :                                                                    keyCol,
    4519             :                                                                    (Expr *) lower_val));
    4520             :             }
    4521             : 
    4522        3658 :             if (need_next_upper_arm && upper_val)
    4523             :             {
    4524             :                 uint16      strategy;
    4525             : 
    4526             :                 /*
    4527             :                  * For the non-last columns of this arm, use the EQ operator.
    4528             :                  * For the last column of this arm, use LT, unless the next
    4529             :                  * bound datum is MAXVALUE, in which case use LE.
    4530             :                  */
    4531        3254 :                 if (j - i < current_or_arm)
    4532         256 :                     strategy = BTEqualStrategyNumber;
    4533        2998 :                 else if (udatum_next &&
    4534         274 :                          udatum_next->kind == PARTITION_RANGE_DATUM_MAXVALUE)
    4535          30 :                     strategy = BTLessEqualStrategyNumber;
    4536             :                 else
    4537        2968 :                     strategy = BTLessStrategyNumber;
    4538             : 
    4539        3254 :                 upper_or_arm_args = lappend(upper_or_arm_args,
    4540        3254 :                                             make_partition_op_expr(key, j,
    4541             :                                                                    strategy,
    4542             :                                                                    keyCol,
    4543             :                                                                    (Expr *) upper_val));
    4544             :             }
    4545             : 
    4546             :             /*
    4547             :              * Did we generate enough of OR's arguments?  First arm considers
    4548             :              * the first of the remaining columns, second arm considers first
    4549             :              * two of the remaining columns, and so on.
    4550             :              */
    4551        3658 :             ++j;
    4552        3658 :             if (j - i > current_or_arm)
    4553             :             {
    4554             :                 /*
    4555             :                  * We must not emit any more arms if the new column that will
    4556             :                  * be considered is unbounded, or this one was.
    4557             :                  */
    4558        3282 :                 if (!lower_val || !ldatum_next ||
    4559         310 :                     ldatum_next->kind != PARTITION_RANGE_DATUM_VALUE)
    4560        3026 :                     need_next_lower_arm = false;
    4561        3282 :                 if (!upper_val || !udatum_next ||
    4562         274 :                     udatum_next->kind != PARTITION_RANGE_DATUM_VALUE)
    4563        3074 :                     need_next_upper_arm = false;
    4564        3282 :                 break;
    4565             :             }
    4566             :         }
    4567             : 
    4568        3282 :         if (lower_or_arm_args != NIL)
    4569        6120 :             lower_or_arms = lappend(lower_or_arms,
    4570        3060 :                                     list_length(lower_or_arm_args) > 1
    4571         256 :                                     ? makeBoolExpr(AND_EXPR, lower_or_arm_args, -1)
    4572        2804 :                                     : linitial(lower_or_arm_args));
    4573             : 
    4574        3282 :         if (upper_or_arm_args != NIL)
    4575        5996 :             upper_or_arms = lappend(upper_or_arms,
    4576        2998 :                                     list_length(upper_or_arm_args) > 1
    4577         208 :                                     ? makeBoolExpr(AND_EXPR, upper_or_arm_args, -1)
    4578        2790 :                                     : linitial(upper_or_arm_args));
    4579             : 
    4580             :         /* If no work to do in the next iteration, break away. */
    4581        3282 :         if (!need_next_lower_arm && !need_next_upper_arm)
    4582        2990 :             break;
    4583             : 
    4584         292 :         ++current_or_arm;
    4585             :     }
    4586             : 
    4587             :     /*
    4588             :      * Generate the OR expressions for each of lower and upper bounds (if
    4589             :      * required), and append to the list of implicitly ANDed list of
    4590             :      * expressions.
    4591             :      */
    4592        2990 :     if (lower_or_arms != NIL)
    4593        5608 :         result = lappend(result,
    4594        2804 :                          list_length(lower_or_arms) > 1
    4595         190 :                          ? makeBoolExpr(OR_EXPR, lower_or_arms, -1)
    4596        2614 :                          : linitial(lower_or_arms));
    4597        2990 :     if (upper_or_arms != NIL)
    4598        5580 :         result = lappend(result,
    4599        2790 :                          list_length(upper_or_arms) > 1
    4600         160 :                          ? makeBoolExpr(OR_EXPR, upper_or_arms, -1)
    4601        2630 :                          : linitial(upper_or_arms));
    4602             : 
    4603             :     /*
    4604             :      * As noted above, for non-default, we return list with constant TRUE. If
    4605             :      * the result is NIL during the recursive call for default, it implies
    4606             :      * this is the only other partition which can hold every value of the key
    4607             :      * except NULL. Hence we return the NullTest result skipped earlier.
    4608             :      */
    4609        2990 :     if (result == NIL)
    4610           0 :         result = for_default
    4611           0 :             ? get_range_nulltest(key)
    4612           0 :             : list_make1(makeBoolConst(true, false));
    4613             : 
    4614        2990 :     return result;
    4615             : }
    4616             : 
    4617             : /*
    4618             :  * get_range_key_properties
    4619             :  *      Returns range partition key information for a given column
    4620             :  *
    4621             :  * This is a subroutine for get_qual_for_range, and its API is pretty
    4622             :  * specialized to that caller.
    4623             :  *
    4624             :  * Constructs an Expr for the key column (returned in *keyCol) and Consts
    4625             :  * for the lower and upper range limits (returned in *lower_val and
    4626             :  * *upper_val).  For MINVALUE/MAXVALUE limits, NULL is returned instead of
    4627             :  * a Const.  All of these structures are freshly palloc'd.
    4628             :  *
    4629             :  * *partexprs_item points to the cell containing the next expression in
    4630             :  * the key->partexprs list, or NULL.  It may be advanced upon return.
    4631             :  */
    4632             : static void
    4633        7188 : get_range_key_properties(PartitionKey key, int keynum,
    4634             :                          PartitionRangeDatum *ldatum,
    4635             :                          PartitionRangeDatum *udatum,
    4636             :                          ListCell **partexprs_item,
    4637             :                          Expr **keyCol,
    4638             :                          Const **lower_val, Const **upper_val)
    4639             : {
    4640             :     /* Get partition key expression for this column */
    4641        7188 :     if (key->partattrs[keynum] != 0)
    4642             :     {
    4643        6460 :         *keyCol = (Expr *) makeVar(1,
    4644        6460 :                                    key->partattrs[keynum],
    4645        6460 :                                    key->parttypid[keynum],
    4646        6460 :                                    key->parttypmod[keynum],
    4647        6460 :                                    key->parttypcoll[keynum],
    4648             :                                    0);
    4649             :     }
    4650             :     else
    4651             :     {
    4652         728 :         if (*partexprs_item == NULL)
    4653           0 :             elog(ERROR, "wrong number of partition key expressions");
    4654         728 :         *keyCol = copyObject(lfirst(*partexprs_item));
    4655         728 :         *partexprs_item = lnext(key->partexprs, *partexprs_item);
    4656             :     }
    4657             : 
    4658             :     /* Get appropriate Const nodes for the bounds */
    4659        7188 :     if (ldatum->kind == PARTITION_RANGE_DATUM_VALUE)
    4660        6744 :         *lower_val = castNode(Const, copyObject(ldatum->value));
    4661             :     else
    4662         444 :         *lower_val = NULL;
    4663             : 
    4664        7188 :     if (udatum->kind == PARTITION_RANGE_DATUM_VALUE)
    4665        6608 :         *upper_val = castNode(Const, copyObject(udatum->value));
    4666             :     else
    4667         580 :         *upper_val = NULL;
    4668        7188 : }
    4669             : 
    4670             : /*
    4671             :  * get_range_nulltest
    4672             :  *
    4673             :  * A non-default range partition table does not currently allow partition
    4674             :  * keys to be null, so emit an IS NOT NULL expression for each key column.
    4675             :  */
    4676             : static List *
    4677        2738 : get_range_nulltest(PartitionKey key)
    4678             : {
    4679        2738 :     List       *result = NIL;
    4680             :     NullTest   *nulltest;
    4681             :     ListCell   *partexprs_item;
    4682             :     int         i;
    4683             : 
    4684        2738 :     partexprs_item = list_head(key->partexprs);
    4685        6248 :     for (i = 0; i < key->partnatts; i++)
    4686             :     {
    4687             :         Expr       *keyCol;
    4688             : 
    4689        3510 :         if (key->partattrs[i] != 0)
    4690             :         {
    4691        3158 :             keyCol = (Expr *) makeVar(1,
    4692        3158 :                                       key->partattrs[i],
    4693        3158 :                                       key->parttypid[i],
    4694        3158 :                                       key->parttypmod[i],
    4695        3158 :                                       key->parttypcoll[i],
    4696             :                                       0);
    4697             :         }
    4698             :         else
    4699             :         {
    4700         352 :             if (partexprs_item == NULL)
    4701           0 :                 elog(ERROR, "wrong number of partition key expressions");
    4702         352 :             keyCol = copyObject(lfirst(partexprs_item));
    4703         352 :             partexprs_item = lnext(key->partexprs, partexprs_item);
    4704             :         }
    4705             : 
    4706        3510 :         nulltest = makeNode(NullTest);
    4707        3510 :         nulltest->arg = keyCol;
    4708        3510 :         nulltest->nulltesttype = IS_NOT_NULL;
    4709        3510 :         nulltest->argisrow = false;
    4710        3510 :         nulltest->location = -1;
    4711        3510 :         result = lappend(result, nulltest);
    4712             :     }
    4713             : 
    4714        2738 :     return result;
    4715             : }
    4716             : 
    4717             : /*
    4718             :  * compute_partition_hash_value
    4719             :  *
    4720             :  * Compute the hash value for given partition key values.
    4721             :  */
    4722             : uint64
    4723      212994 : compute_partition_hash_value(int partnatts, FmgrInfo *partsupfunc, const Oid *partcollation,
    4724             :                              const Datum *values, const bool *isnull)
    4725             : {
    4726             :     int         i;
    4727      212994 :     uint64      rowHash = 0;
    4728      212994 :     Datum       seed = UInt64GetDatum(HASH_PARTITION_SEED);
    4729             : 
    4730      426972 :     for (i = 0; i < partnatts; i++)
    4731             :     {
    4732             :         /* Nulls are just ignored */
    4733      213978 :         if (!isnull[i])
    4734             :         {
    4735             :             Datum       hash;
    4736             : 
    4737             :             Assert(OidIsValid(partsupfunc[i].fn_oid));
    4738             : 
    4739             :             /*
    4740             :              * Compute hash for each datum value by calling respective
    4741             :              * datatype-specific hash functions of each partition key
    4742             :              * attribute.
    4743             :              */
    4744      213336 :             hash = FunctionCall2Coll(&partsupfunc[i], partcollation[i],
    4745      213336 :                                      values[i], seed);
    4746             : 
    4747             :             /* Form a single 64-bit hash value */
    4748      213336 :             rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
    4749             :         }
    4750             :     }
    4751             : 
    4752      212994 :     return rowHash;
    4753             : }
    4754             : 
    4755             : /*
    4756             :  * satisfies_hash_partition
    4757             :  *
    4758             :  * This is an SQL-callable function for use in hash partition constraints.
    4759             :  * The first three arguments are the parent table OID, modulus, and remainder.
    4760             :  * The remaining arguments are the value of the partitioning columns (or
    4761             :  * expressions); these are hashed and the results are combined into a single
    4762             :  * hash value by calling hash_combine64.
    4763             :  *
    4764             :  * Returns true if remainder produced when this computed single hash value is
    4765             :  * divided by the given modulus is equal to given remainder, otherwise false.
    4766             :  * NB: it's important that this never return null, as the constraint machinery
    4767             :  * would consider that to be a "pass".
    4768             :  *
    4769             :  * See get_qual_for_hash() for usage.
    4770             :  */
    4771             : Datum
    4772        4228 : satisfies_hash_partition(PG_FUNCTION_ARGS)
    4773             : {
    4774             :     typedef struct ColumnsHashData
    4775             :     {
    4776             :         Oid         relid;
    4777             :         int         nkeys;
    4778             :         Oid         variadic_type;
    4779             :         int16       variadic_typlen;
    4780             :         bool        variadic_typbyval;
    4781             :         char        variadic_typalign;
    4782             :         Oid         partcollid[PARTITION_MAX_KEYS];
    4783             :         FmgrInfo    partsupfunc[FLEXIBLE_ARRAY_MEMBER];
    4784             :     } ColumnsHashData;
    4785             :     Oid         parentId;
    4786             :     int         modulus;
    4787             :     int         remainder;
    4788        4228 :     Datum       seed = UInt64GetDatum(HASH_PARTITION_SEED);
    4789             :     ColumnsHashData *my_extra;
    4790        4228 :     uint64      rowHash = 0;
    4791             : 
    4792             :     /* Return false if the parent OID, modulus, or remainder is NULL. */
    4793        4228 :     if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
    4794          12 :         PG_RETURN_BOOL(false);
    4795        4216 :     parentId = PG_GETARG_OID(0);
    4796        4216 :     modulus = PG_GETARG_INT32(1);
    4797        4216 :     remainder = PG_GETARG_INT32(2);
    4798             : 
    4799             :     /* Sanity check modulus and remainder. */
    4800        4216 :     if (modulus <= 0)
    4801           6 :         ereport(ERROR,
    4802             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4803             :                  errmsg("modulus for hash partition must be an integer value greater than zero")));
    4804        4210 :     if (remainder < 0)
    4805           6 :         ereport(ERROR,
    4806             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4807             :                  errmsg("remainder for hash partition must be an integer value greater than or equal to zero")));
    4808        4204 :     if (remainder >= modulus)
    4809           6 :         ereport(ERROR,
    4810             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4811             :                  errmsg("remainder for hash partition must be less than modulus")));
    4812             : 
    4813             :     /*
    4814             :      * Cache hash function information.
    4815             :      */
    4816        4198 :     my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
    4817        4198 :     if (my_extra == NULL || my_extra->relid != parentId)
    4818             :     {
    4819             :         Relation    parent;
    4820             :         PartitionKey key;
    4821             :         int         j;
    4822             : 
    4823             :         /* Open parent relation and fetch partition key info */
    4824        2196 :         parent = relation_open(parentId, AccessShareLock);
    4825        2190 :         key = RelationGetPartitionKey(parent);
    4826             : 
    4827             :         /* Reject parent table that is not hash-partitioned. */
    4828        2190 :         if (key == NULL || key->strategy != PARTITION_STRATEGY_HASH)
    4829          12 :             ereport(ERROR,
    4830             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4831             :                      errmsg("\"%s\" is not a hash partitioned table",
    4832             :                             get_rel_name(parentId))));
    4833             : 
    4834        2178 :         if (!get_fn_expr_variadic(fcinfo->flinfo))
    4835             :         {
    4836        2148 :             int         nargs = PG_NARGS() - 3;
    4837             : 
    4838             :             /* complain if wrong number of column values */
    4839        2148 :             if (key->partnatts != nargs)
    4840          12 :                 ereport(ERROR,
    4841             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4842             :                          errmsg("number of partitioning columns (%d) does not match number of partition keys provided (%d)",
    4843             :                                 key->partnatts, nargs)));
    4844             : 
    4845             :             /* allocate space for our cache */
    4846        4272 :             fcinfo->flinfo->fn_extra =
    4847        2136 :                 MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
    4848             :                                        offsetof(ColumnsHashData, partsupfunc) +
    4849        2136 :                                        sizeof(FmgrInfo) * nargs);
    4850        2136 :             my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
    4851        2136 :             my_extra->relid = parentId;
    4852        2136 :             my_extra->nkeys = key->partnatts;
    4853        2136 :             memcpy(my_extra->partcollid, key->partcollation,
    4854        2136 :                    key->partnatts * sizeof(Oid));
    4855             : 
    4856             :             /* check argument types and save fmgr_infos */
    4857        4314 :             for (j = 0; j < key->partnatts; ++j)
    4858             :             {
    4859        2184 :                 Oid         argtype = get_fn_expr_argtype(fcinfo->flinfo, j + 3);
    4860             : 
    4861        2184 :                 if (argtype != key->parttypid[j] && !IsBinaryCoercible(argtype, key->parttypid[j]))
    4862           6 :                     ereport(ERROR,
    4863             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4864             :                              errmsg("column %d of the partition key has type %s, but supplied value is of type %s",
    4865             :                                     j + 1, format_type_be(key->parttypid[j]), format_type_be(argtype))));
    4866             : 
    4867        2178 :                 fmgr_info_copy(&my_extra->partsupfunc[j],
    4868        2178 :                                &key->partsupfunc[j],
    4869        2178 :                                fcinfo->flinfo->fn_mcxt);
    4870             :             }
    4871             :         }
    4872             :         else
    4873             :         {
    4874          30 :             ArrayType  *variadic_array = PG_GETARG_ARRAYTYPE_P(3);
    4875             : 
    4876             :             /* allocate space for our cache -- just one FmgrInfo in this case */
    4877          60 :             fcinfo->flinfo->fn_extra =
    4878          30 :                 MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
    4879             :                                        offsetof(ColumnsHashData, partsupfunc) +
    4880             :                                        sizeof(FmgrInfo));
    4881          30 :             my_extra = (ColumnsHashData *) fcinfo->flinfo->fn_extra;
    4882          30 :             my_extra->relid = parentId;
    4883          30 :             my_extra->nkeys = key->partnatts;
    4884          30 :             my_extra->variadic_type = ARR_ELEMTYPE(variadic_array);
    4885          30 :             get_typlenbyvalalign(my_extra->variadic_type,
    4886             :                                  &my_extra->variadic_typlen,
    4887             :                                  &my_extra->variadic_typbyval,
    4888             :                                  &my_extra->variadic_typalign);
    4889          30 :             my_extra->partcollid[0] = key->partcollation[0];
    4890             : 
    4891             :             /* check argument types */
    4892          72 :             for (j = 0; j < key->partnatts; ++j)
    4893          54 :                 if (key->parttypid[j] != my_extra->variadic_type)
    4894          12 :                     ereport(ERROR,
    4895             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4896             :                              errmsg("column %d of the partition key has type \"%s\", but supplied value is of type \"%s\"",
    4897             :                                     j + 1,
    4898             :                                     format_type_be(key->parttypid[j]),
    4899             :                                     format_type_be(my_extra->variadic_type))));
    4900             : 
    4901          18 :             fmgr_info_copy(&my_extra->partsupfunc[0],
    4902             :                            &key->partsupfunc[0],
    4903          18 :                            fcinfo->flinfo->fn_mcxt);
    4904             :         }
    4905             : 
    4906             :         /* Hold lock until commit */
    4907        2148 :         relation_close(parent, NoLock);
    4908             :     }
    4909             : 
    4910        4150 :     if (!OidIsValid(my_extra->variadic_type))
    4911             :     {
    4912        4132 :         int         nkeys = my_extra->nkeys;
    4913             :         int         i;
    4914             : 
    4915             :         /*
    4916             :          * For a non-variadic call, neither the number of arguments nor their
    4917             :          * types can change across calls, so avoid the expense of rechecking
    4918             :          * here.
    4919             :          */
    4920             : 
    4921        8306 :         for (i = 0; i < nkeys; i++)
    4922             :         {
    4923             :             Datum       hash;
    4924             : 
    4925             :             /* keys start from fourth argument of function. */
    4926        4174 :             int         argno = i + 3;
    4927             : 
    4928        4174 :             if (PG_ARGISNULL(argno))
    4929           0 :                 continue;
    4930             : 
    4931        4174 :             hash = FunctionCall2Coll(&my_extra->partsupfunc[i],
    4932             :                                      my_extra->partcollid[i],
    4933             :                                      PG_GETARG_DATUM(argno),
    4934             :                                      seed);
    4935             : 
    4936             :             /* Form a single 64-bit hash value */
    4937        4174 :             rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
    4938             :         }
    4939             :     }
    4940             :     else
    4941             :     {
    4942          18 :         ArrayType  *variadic_array = PG_GETARG_ARRAYTYPE_P(3);
    4943             :         int         i;
    4944             :         int         nelems;
    4945             :         Datum      *datum;
    4946             :         bool       *isnull;
    4947             : 
    4948          18 :         deconstruct_array(variadic_array,
    4949             :                           my_extra->variadic_type,
    4950          18 :                           my_extra->variadic_typlen,
    4951          18 :                           my_extra->variadic_typbyval,
    4952          18 :                           my_extra->variadic_typalign,
    4953             :                           &datum, &isnull, &nelems);
    4954             : 
    4955             :         /* complain if wrong number of column values */
    4956          18 :         if (nelems != my_extra->nkeys)
    4957           6 :             ereport(ERROR,
    4958             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4959             :                      errmsg("number of partitioning columns (%d) does not match number of partition keys provided (%d)",
    4960             :                             my_extra->nkeys, nelems)));
    4961             : 
    4962          36 :         for (i = 0; i < nelems; i++)
    4963             :         {
    4964             :             Datum       hash;
    4965             : 
    4966          24 :             if (isnull[i])
    4967           0 :                 continue;
    4968             : 
    4969          24 :             hash = FunctionCall2Coll(&my_extra->partsupfunc[0],
    4970             :                                      my_extra->partcollid[0],
    4971          24 :                                      datum[i],
    4972             :                                      seed);
    4973             : 
    4974             :             /* Form a single 64-bit hash value */
    4975          24 :             rowHash = hash_combine64(rowHash, DatumGetUInt64(hash));
    4976             :         }
    4977             :     }
    4978             : 
    4979        4144 :     PG_RETURN_BOOL(rowHash % modulus == remainder);
    4980             : }

Generated by: LCOV version 1.14