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