Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * partprune.c
4 : : * Support for partition pruning during query planning and execution
5 : : *
6 : : * This module implements partition pruning using the information contained in
7 : : * a table's partition descriptor, query clauses, and run-time parameters.
8 : : *
9 : : * During planning, clauses that can be matched to the table's partition key
10 : : * are turned into a set of "pruning steps", which are then executed to
11 : : * identify a set of partitions (as indexes in the RelOptInfo->part_rels
12 : : * array) that satisfy the constraints in the step. Partitions not in the set
13 : : * are said to have been pruned.
14 : : *
15 : : * A base pruning step may involve expressions whose values are only known
16 : : * during execution, such as Params, in which case pruning cannot occur
17 : : * entirely during planning. In that case, such steps are included alongside
18 : : * the plan, so that they can be used by the executor for further pruning.
19 : : *
20 : : * There are two kinds of pruning steps. A "base" pruning step represents
21 : : * tests on partition key column(s), typically comparisons to expressions.
22 : : * A "combine" pruning step represents a Boolean connector (AND/OR), and
23 : : * combines the outputs of some previous steps using the appropriate
24 : : * combination method.
25 : : *
26 : : * See gen_partprune_steps_internal() for more details on step generation.
27 : : *
28 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
29 : : * Portions Copyright (c) 1994, Regents of the University of California
30 : : *
31 : : * IDENTIFICATION
32 : : * src/backend/partitioning/partprune.c
33 : : *
34 : : *-------------------------------------------------------------------------
35 : : */
36 : : #include "postgres.h"
37 : :
38 : : #include "access/hash.h"
39 : : #include "access/nbtree.h"
40 : : #include "catalog/pg_operator.h"
41 : : #include "catalog/pg_opfamily.h"
42 : : #include "catalog/pg_proc.h"
43 : : #include "catalog/pg_type.h"
44 : : #include "executor/executor.h"
45 : : #include "miscadmin.h"
46 : : #include "nodes/makefuncs.h"
47 : : #include "nodes/nodeFuncs.h"
48 : : #include "optimizer/appendinfo.h"
49 : : #include "optimizer/clauses.h"
50 : : #include "optimizer/cost.h"
51 : : #include "optimizer/optimizer.h"
52 : : #include "optimizer/pathnode.h"
53 : : #include "optimizer/placeholder.h"
54 : : #include "parser/parsetree.h"
55 : : #include "partitioning/partbounds.h"
56 : : #include "partitioning/partprune.h"
57 : : #include "utils/array.h"
58 : : #include "utils/lsyscache.h"
59 : :
60 : :
61 : : /*
62 : : * Information about a clause matched with a partition key.
63 : : */
64 : : typedef struct PartClauseInfo
65 : : {
66 : : int keyno; /* Partition key number (0 to partnatts - 1) */
67 : : Oid opno; /* operator used to compare partkey to expr */
68 : : bool op_is_ne; /* is clause's original operator <> ? */
69 : : Expr *expr; /* expr the partition key is compared to */
70 : : Oid cmpfn; /* Oid of function to compare 'expr' to the
71 : : * partition key */
72 : : int op_strategy; /* btree strategy identifying the operator */
73 : : } PartClauseInfo;
74 : :
75 : : /*
76 : : * PartClauseMatchStatus
77 : : * Describes the result of match_clause_to_partition_key()
78 : : */
79 : : typedef enum PartClauseMatchStatus
80 : : {
81 : : PARTCLAUSE_NOMATCH,
82 : : PARTCLAUSE_MATCH_CLAUSE,
83 : : PARTCLAUSE_MATCH_NULLNESS,
84 : : PARTCLAUSE_MATCH_STEPS,
85 : : PARTCLAUSE_MATCH_CONTRADICT,
86 : : PARTCLAUSE_UNSUPPORTED,
87 : : } PartClauseMatchStatus;
88 : :
89 : : /*
90 : : * PartClauseTarget
91 : : * Identifies which qual clauses we can use for generating pruning steps
92 : : */
93 : : typedef enum PartClauseTarget
94 : : {
95 : : PARTTARGET_PLANNER, /* want to prune during planning */
96 : : PARTTARGET_INITIAL, /* want to prune during executor startup */
97 : : PARTTARGET_EXEC, /* want to prune during each plan node scan */
98 : : } PartClauseTarget;
99 : :
100 : : /*
101 : : * GeneratePruningStepsContext
102 : : * Information about the current state of generation of "pruning steps"
103 : : * for a given set of clauses
104 : : *
105 : : * gen_partprune_steps() initializes and returns an instance of this struct.
106 : : *
107 : : * Note that has_mutable_op, has_mutable_arg, and has_exec_param are set if
108 : : * we found any potentially-useful-for-pruning clause having those properties,
109 : : * whether or not we actually used the clause in the steps list. This
110 : : * definition allows us to skip the PARTTARGET_EXEC pass in some cases.
111 : : */
112 : : typedef struct GeneratePruningStepsContext
113 : : {
114 : : /* Copies of input arguments for gen_partprune_steps: */
115 : : RelOptInfo *rel; /* the partitioned relation */
116 : : PartClauseTarget target; /* use-case we're generating steps for */
117 : : /* Result data: */
118 : : List *steps; /* list of PartitionPruneSteps */
119 : : bool has_mutable_op; /* clauses include any stable operators */
120 : : bool has_mutable_arg; /* clauses include any mutable comparison
121 : : * values, *other than* exec params */
122 : : bool has_exec_param; /* clauses include any PARAM_EXEC params */
123 : : bool contradictory; /* clauses were proven self-contradictory */
124 : : /* Working state: */
125 : : int next_step_id;
126 : : } GeneratePruningStepsContext;
127 : :
128 : : /* The result of performing one PartitionPruneStep */
129 : : typedef struct PruneStepResult
130 : : {
131 : : /*
132 : : * The offsets of bounds (in a table's boundinfo) whose partition is
133 : : * selected by the pruning step.
134 : : */
135 : : Bitmapset *bound_offsets;
136 : :
137 : : bool scan_default; /* Scan the default partition? */
138 : : bool scan_null; /* Scan the partition for NULL values? */
139 : : } PruneStepResult;
140 : :
141 : :
142 : : static List *add_part_relids(List *allpartrelids, Bitmapset *partrelids);
143 : : static List *make_partitionedrel_pruneinfo(PlannerInfo *root,
144 : : RelOptInfo *parentrel,
145 : : List *prunequal,
146 : : Bitmapset *partrelids,
147 : : int *relid_subplan_map,
148 : : Bitmapset **matchedsubplans);
149 : : static void gen_partprune_steps(RelOptInfo *rel, List *clauses,
150 : : PartClauseTarget target,
151 : : GeneratePruningStepsContext *context);
152 : : static List *gen_partprune_steps_internal(GeneratePruningStepsContext *context,
153 : : List *clauses);
154 : : static PartitionPruneStep *gen_prune_step_op(GeneratePruningStepsContext *context,
155 : : StrategyNumber opstrategy, bool op_is_ne,
156 : : List *exprs, List *cmpfns, Bitmapset *nullkeys);
157 : : static PartitionPruneStep *gen_prune_step_combine(GeneratePruningStepsContext *context,
158 : : List *source_stepids,
159 : : PartitionPruneCombineOp combineOp);
160 : : static List *gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
161 : : List **keyclauses, Bitmapset *nullkeys);
162 : : static PartClauseMatchStatus match_clause_to_partition_key(GeneratePruningStepsContext *context,
163 : : Expr *clause, const Expr *partkey, int partkeyidx,
164 : : bool *clause_is_not_null,
165 : : PartClauseInfo **pc, List **clause_steps);
166 : : static List *get_steps_using_prefix(GeneratePruningStepsContext *context,
167 : : StrategyNumber step_opstrategy,
168 : : bool step_op_is_ne,
169 : : Expr *step_lastexpr,
170 : : Oid step_lastcmpfn,
171 : : Bitmapset *step_nullkeys,
172 : : List *prefix);
173 : : static List *get_steps_using_prefix_recurse(GeneratePruningStepsContext *context,
174 : : StrategyNumber step_opstrategy,
175 : : bool step_op_is_ne,
176 : : Expr *step_lastexpr,
177 : : Oid step_lastcmpfn,
178 : : Bitmapset *step_nullkeys,
179 : : List *prefix,
180 : : ListCell *start,
181 : : List *step_exprs,
182 : : List *step_cmpfns);
183 : : static PruneStepResult *get_matching_hash_bounds(PartitionPruneContext *context,
184 : : StrategyNumber opstrategy, const Datum *values, int nvalues,
185 : : FmgrInfo *partsupfunc, Bitmapset *nullkeys);
186 : : static PruneStepResult *get_matching_list_bounds(PartitionPruneContext *context,
187 : : StrategyNumber opstrategy, Datum value, int nvalues,
188 : : FmgrInfo *partsupfunc, Bitmapset *nullkeys);
189 : : static PruneStepResult *get_matching_range_bounds(PartitionPruneContext *context,
190 : : StrategyNumber opstrategy, const Datum *values, int nvalues,
191 : : FmgrInfo *partsupfunc, Bitmapset *nullkeys);
192 : : static Bitmapset *pull_exec_paramids(Expr *expr);
193 : : static bool pull_exec_paramids_walker(Node *node, Bitmapset **context);
194 : : static Bitmapset *get_partkey_exec_paramids(List *steps);
195 : : static PruneStepResult *perform_pruning_base_step(PartitionPruneContext *context,
196 : : PartitionPruneStepOp *opstep);
197 : : static PruneStepResult *perform_pruning_combine_step(PartitionPruneContext *context,
198 : : PartitionPruneStepCombine *cstep,
199 : : PruneStepResult **step_results);
200 : : static PartClauseMatchStatus match_boolean_partition_clause(Oid partopfamily,
201 : : Expr *clause,
202 : : const Expr *partkey,
203 : : Expr **outconst,
204 : : bool *notclause);
205 : : static void partkey_datum_from_expr(PartitionPruneContext *context,
206 : : Expr *expr, int stateidx,
207 : : Datum *value, bool *isnull);
208 : :
209 : :
210 : : /*
211 : : * make_partition_pruneinfo
212 : : * Checks if the given set of quals can be used to build pruning steps
213 : : * that the executor can use to prune away unneeded partitions. If
214 : : * suitable quals are found then a PartitionPruneInfo is built and tagged
215 : : * onto the PlannerInfo's partPruneInfos list.
216 : : *
217 : : * The return value is the 0-based index of the item added to the
218 : : * partPruneInfos list or -1 if nothing was added.
219 : : *
220 : : * 'parentrel' is the RelOptInfo for an appendrel, and 'subpaths' is the list
221 : : * of scan paths for its child rels.
222 : : * 'prunequal' is a list of potential pruning quals (i.e., restriction
223 : : * clauses that are applicable to the appendrel).
224 : : */
225 : : int
226 : 6917 : make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
227 : : List *subpaths,
228 : : List *prunequal)
229 : : {
230 : : PartitionPruneInfo *pruneinfo;
231 : 6917 : Bitmapset *allmatchedsubplans = NULL;
232 : : List *allpartrelids;
233 : : List *prunerelinfos;
234 : : int *relid_subplan_map;
235 : : ListCell *lc;
236 : : int i;
237 : :
238 : : /*
239 : : * Scan the subpaths to see which ones are scans of partition child
240 : : * relations, and identify their parent partitioned rels. (Note: we must
241 : : * restrict the parent partitioned rels to be parentrel or children of
242 : : * parentrel, otherwise we couldn't translate prunequal to match.)
243 : : *
244 : : * Also construct a temporary array to map from partition-child-relation
245 : : * relid to the index in 'subpaths' of the scan plan for that partition.
246 : : * (Use of "subplan" rather than "subpath" is a bit of a misnomer, but
247 : : * we'll let it stand.) For convenience, we use 1-based indexes here, so
248 : : * that zero can represent an un-filled array entry.
249 : : */
250 : 6917 : allpartrelids = NIL;
251 : 6917 : relid_subplan_map = palloc0_array(int, root->simple_rel_array_size);
252 : :
253 : 6917 : i = 1;
254 [ + - + + : 20512 : foreach(lc, subpaths)
+ + ]
255 : : {
256 : 13595 : Path *path = (Path *) lfirst(lc);
257 : 13595 : RelOptInfo *pathrel = path->parent;
258 : :
259 : : /* We don't consider partitioned joins here */
260 [ + - ]: 13595 : if (pathrel->reloptkind == RELOPT_OTHER_MEMBER_REL)
261 : : {
262 : 13595 : RelOptInfo *prel = pathrel;
263 : 13595 : Bitmapset *partrelids = NULL;
264 : :
265 : : /*
266 : : * Traverse up to the pathrel's topmost partitioned parent,
267 : : * collecting parent relids as we go; but stop if we reach
268 : : * parentrel. (Normally, a pathrel's topmost partitioned parent
269 : : * is either parentrel or a UNION ALL appendrel child of
270 : : * parentrel. But when handling partitionwise joins of
271 : : * multi-level partitioning trees, we can see an append path whose
272 : : * parentrel is an intermediate partitioned table.)
273 : : */
274 : : do
275 : : {
276 : : AppendRelInfo *appinfo;
277 : :
278 : : Assert(prel->relid < root->simple_rel_array_size);
279 : 16323 : appinfo = root->append_rel_array[prel->relid];
280 : 16323 : prel = find_base_rel(root, appinfo->parent_relid);
281 [ + + + - : 16323 : if (!IS_PARTITIONED_REL(prel))
+ - + - +
- ]
282 : : break; /* reached a non-partitioned parent */
283 : : /* accept this level as an interesting parent */
284 : 13281 : partrelids = bms_add_member(partrelids, prel->relid);
285 [ + + ]: 13281 : if (prel == parentrel)
286 : 10553 : break; /* don't traverse above parentrel */
287 [ + - ]: 2728 : } while (prel->reloptkind == RELOPT_OTHER_MEMBER_REL);
288 : :
289 [ + + ]: 13595 : if (partrelids)
290 : : {
291 : : /*
292 : : * Found some relevant parent partitions, which may or may not
293 : : * overlap with partition trees we already found. Add new
294 : : * information to the allpartrelids list.
295 : : */
296 : 10768 : allpartrelids = add_part_relids(allpartrelids, partrelids);
297 : : /* Also record the subplan in relid_subplan_map[] */
298 : : /* No duplicates please */
299 : : Assert(relid_subplan_map[pathrel->relid] == 0);
300 : 10768 : relid_subplan_map[pathrel->relid] = i;
301 : : }
302 : : }
303 : 13595 : i++;
304 : : }
305 : :
306 : : /*
307 : : * We now build a PartitionedRelPruneInfo for each topmost partitioned rel
308 : : * (omitting any that turn out not to have useful pruning quals).
309 : : */
310 : 6917 : prunerelinfos = NIL;
311 [ + + + + : 12719 : foreach(lc, allpartrelids)
+ + ]
312 : : {
313 : 5802 : Bitmapset *partrelids = (Bitmapset *) lfirst(lc);
314 : : List *pinfolist;
315 : 5802 : Bitmapset *matchedsubplans = NULL;
316 : :
317 : 5802 : pinfolist = make_partitionedrel_pruneinfo(root, parentrel,
318 : : prunequal,
319 : : partrelids,
320 : : relid_subplan_map,
321 : : &matchedsubplans);
322 : :
323 : : /* When pruning is possible, record the matched subplans */
324 [ + + ]: 5802 : if (pinfolist != NIL)
325 : : {
326 : 497 : prunerelinfos = lappend(prunerelinfos, pinfolist);
327 : 497 : allmatchedsubplans = bms_join(matchedsubplans,
328 : : allmatchedsubplans);
329 : : }
330 : : }
331 : :
332 : 6917 : pfree(relid_subplan_map);
333 : :
334 : : /*
335 : : * If none of the partition hierarchies had any useful run-time pruning
336 : : * quals, then we can just not bother with run-time pruning.
337 : : */
338 [ + + ]: 6917 : if (prunerelinfos == NIL)
339 : 6430 : return -1;
340 : :
341 : : /* Else build the result data structure */
342 : 487 : pruneinfo = makeNode(PartitionPruneInfo);
343 : 487 : pruneinfo->relids = bms_copy(parentrel->relids);
344 : 487 : pruneinfo->prune_infos = prunerelinfos;
345 : :
346 : : /*
347 : : * Some subplans may not belong to any of the identified partitioned rels.
348 : : * This can happen for UNION ALL queries which include a non-partitioned
349 : : * table, or when some of the hierarchies aren't run-time prunable. Build
350 : : * a bitmapset of the indexes of all such subplans, so that the executor
351 : : * can identify which subplans should never be pruned.
352 : : */
353 [ + + ]: 487 : if (bms_num_members(allmatchedsubplans) < list_length(subpaths))
354 : : {
355 : : Bitmapset *other_subplans;
356 : :
357 : : /* Create the complement of allmatchedsubplans */
358 : 30 : other_subplans = bms_add_range(NULL, 0, list_length(subpaths) - 1);
359 : 30 : other_subplans = bms_del_members(other_subplans, allmatchedsubplans);
360 : :
361 : 30 : pruneinfo->other_subplans = other_subplans;
362 : : }
363 : : else
364 : 457 : pruneinfo->other_subplans = NULL;
365 : :
366 : 487 : root->partPruneInfos = lappend(root->partPruneInfos, pruneinfo);
367 : :
368 : 487 : return list_length(root->partPruneInfos) - 1;
369 : : }
370 : :
371 : : /*
372 : : * add_part_relids
373 : : * Add new info to a list of Bitmapsets of partitioned relids.
374 : : *
375 : : * Within 'allpartrelids', there is one Bitmapset for each topmost parent
376 : : * partitioned rel. Each Bitmapset contains the RT indexes of the topmost
377 : : * parent as well as its relevant non-leaf child partitions. Since (by
378 : : * construction of the rangetable list) parent partitions must have lower
379 : : * RT indexes than their children, we can distinguish the topmost parent
380 : : * as being the lowest set bit in the Bitmapset.
381 : : *
382 : : * 'partrelids' contains the RT indexes of a parent partitioned rel, and
383 : : * possibly some non-leaf children, that are newly identified as parents of
384 : : * some subpath rel passed to make_partition_pruneinfo(). These are added
385 : : * to an appropriate member of 'allpartrelids'.
386 : : *
387 : : * Note that the list contains only RT indexes of partitioned tables that
388 : : * are parents of some scan-level relation appearing in the 'subpaths' that
389 : : * make_partition_pruneinfo() is dealing with. Also, "topmost" parents are
390 : : * not allowed to be higher than the 'parentrel' associated with the append
391 : : * path. In this way, we avoid expending cycles on partitioned rels that
392 : : * can't contribute useful pruning information for the problem at hand.
393 : : * (It is possible for 'parentrel' to be a child partitioned table, and it
394 : : * is also possible for scan-level relations to be child partitioned tables
395 : : * rather than leaf partitions. Hence we must construct this relation set
396 : : * with reference to the particular append path we're dealing with, rather
397 : : * than looking at the full partitioning structure represented in the
398 : : * RelOptInfos.)
399 : : */
400 : : static List *
401 : 10768 : add_part_relids(List *allpartrelids, Bitmapset *partrelids)
402 : : {
403 : : Index targetpart;
404 : : ListCell *lc;
405 : :
406 : : /* We can easily get the lowest set bit this way: */
407 : 10768 : targetpart = bms_next_member(partrelids, -1);
408 : : Assert(targetpart > 0);
409 : :
410 : : /* Look for a matching topmost parent */
411 [ + + + + : 10828 : foreach(lc, allpartrelids)
+ + ]
412 : : {
413 : 5026 : Bitmapset *currpartrelids = (Bitmapset *) lfirst(lc);
414 : 5026 : Index currtarget = bms_next_member(currpartrelids, -1);
415 : :
416 [ + + ]: 5026 : if (targetpart == currtarget)
417 : : {
418 : : /* Found a match, so add any new RT indexes to this hierarchy */
419 : 4966 : currpartrelids = bms_add_members(currpartrelids, partrelids);
420 : 4966 : lfirst(lc) = currpartrelids;
421 : 4966 : return allpartrelids;
422 : : }
423 : : }
424 : : /* No match, so add the new partition hierarchy to the list */
425 : 5802 : return lappend(allpartrelids, partrelids);
426 : : }
427 : :
428 : : /*
429 : : * make_partitionedrel_pruneinfo
430 : : * Build a List of PartitionedRelPruneInfos, one for each interesting
431 : : * partitioned rel in a partitioning hierarchy. These can be used in the
432 : : * executor to allow additional partition pruning to take place.
433 : : *
434 : : * parentrel: rel associated with the appendpath being considered
435 : : * prunequal: potential pruning quals, represented for parentrel
436 : : * partrelids: Set of RT indexes identifying relevant partitioned tables
437 : : * within a single partitioning hierarchy
438 : : * relid_subplan_map[]: maps child relation relids to subplan indexes
439 : : * matchedsubplans: on success, receives the set of subplan indexes which
440 : : * were matched to this partition hierarchy
441 : : *
442 : : * If we cannot find any useful run-time pruning steps, return NIL.
443 : : * However, on success, each rel identified in partrelids will have
444 : : * an element in the result list, even if some of them are useless.
445 : : */
446 : : static List *
447 : 5802 : make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
448 : : List *prunequal,
449 : : Bitmapset *partrelids,
450 : : int *relid_subplan_map,
451 : : Bitmapset **matchedsubplans)
452 : : {
453 : 5802 : RelOptInfo *targetpart = NULL;
454 : 5802 : List *pinfolist = NIL;
455 : 5802 : bool doruntimeprune = false;
456 : : int *relid_subpart_map;
457 : 5802 : Bitmapset *subplansfound = NULL;
458 : : ListCell *lc;
459 : : int rti;
460 : : int i;
461 : :
462 : : /*
463 : : * Examine each partitioned rel, constructing a temporary array to map
464 : : * from planner relids to index of the partitioned rel, and building a
465 : : * PartitionedRelPruneInfo for each partitioned rel.
466 : : *
467 : : * In this phase we discover whether runtime pruning is needed at all; if
468 : : * not, we can avoid doing further work.
469 : : */
470 : 5802 : relid_subpart_map = palloc0_array(int, root->simple_rel_array_size);
471 : :
472 : 5802 : i = 1;
473 : 5802 : rti = -1;
474 [ + + ]: 12912 : while ((rti = bms_next_member(partrelids, rti)) > 0)
475 : : {
476 : 7115 : RelOptInfo *subpart = find_base_rel(root, rti);
477 : : PartitionedRelPruneInfo *pinfo;
478 : : List *partprunequal;
479 : : List *initial_pruning_steps;
480 : : List *exec_pruning_steps;
481 : : Bitmapset *execparamids;
482 : : GeneratePruningStepsContext context;
483 : :
484 : : /*
485 : : * Fill the mapping array.
486 : : *
487 : : * relid_subpart_map maps relid of a non-leaf partition to the index
488 : : * in the returned PartitionedRelPruneInfo list of the info for that
489 : : * partition. We use 1-based indexes here, so that zero can represent
490 : : * an un-filled array entry.
491 : : */
492 : : Assert(rti < root->simple_rel_array_size);
493 : 7115 : relid_subpart_map[rti] = i++;
494 : :
495 : : /*
496 : : * Translate pruning qual, if necessary, for this partition.
497 : : *
498 : : * The first item in the list is the target partitioned relation.
499 : : */
500 [ + + ]: 7115 : if (!targetpart)
501 : : {
502 : 5802 : targetpart = subpart;
503 : :
504 : : /*
505 : : * The prunequal is presented to us as a qual for 'parentrel'.
506 : : * Frequently this rel is the same as targetpart, so we can skip
507 : : * an adjust_appendrel_attrs step. But it might not be, and then
508 : : * we have to translate. We update the prunequal parameter here,
509 : : * because in later iterations of the loop for child partitions,
510 : : * we want to translate from parent to child variables.
511 : : */
512 [ + + ]: 5802 : if (!bms_equal(parentrel->relids, subpart->relids))
513 : : {
514 : : int nappinfos;
515 : 50 : AppendRelInfo **appinfos = find_appinfos_by_relids(root,
516 : : subpart->relids,
517 : : &nappinfos);
518 : :
519 : 50 : prunequal = (List *) adjust_appendrel_attrs(root, (Node *)
520 : : prunequal,
521 : : nappinfos,
522 : : appinfos);
523 : :
524 : 50 : pfree(appinfos);
525 : : }
526 : :
527 : 5802 : partprunequal = prunequal;
528 : : }
529 : : else
530 : : {
531 : : /*
532 : : * For sub-partitioned tables the columns may not be in the same
533 : : * order as the parent, so we must translate the prunequal to make
534 : : * it compatible with this relation.
535 : : */
536 : : partprunequal = (List *)
537 : 1313 : adjust_appendrel_attrs_multilevel(root,
538 : : (Node *) prunequal,
539 : : subpart,
540 : : targetpart);
541 : : }
542 : :
543 : : /*
544 : : * Convert pruning qual to pruning steps. We may need to do this
545 : : * twice, once to obtain executor startup pruning steps, and once for
546 : : * executor per-scan pruning steps. This first pass creates startup
547 : : * pruning steps and detects whether there's any possibly-useful quals
548 : : * that would require per-scan pruning.
549 : : */
550 : 7115 : gen_partprune_steps(subpart, partprunequal, PARTTARGET_INITIAL,
551 : : &context);
552 : :
553 [ + + ]: 7115 : if (context.contradictory)
554 : : {
555 : : /*
556 : : * This shouldn't happen as the planner should have detected this
557 : : * earlier. However, we do use additional quals from parameterized
558 : : * paths here. These do only compare Params to the partition key,
559 : : * so this shouldn't cause the discovery of any new qual
560 : : * contradictions that were not previously discovered as the Param
561 : : * values are unknown during planning. Anyway, we'd better do
562 : : * something sane here, so let's just disable run-time pruning.
563 : : */
564 : 5 : return NIL;
565 : : }
566 : :
567 : : /*
568 : : * If no mutable operators or expressions appear in usable pruning
569 : : * clauses, then there's no point in running startup pruning, because
570 : : * plan-time pruning should have pruned everything prunable.
571 : : */
572 [ + + + + ]: 7110 : if (context.has_mutable_op || context.has_mutable_arg)
573 : 305 : initial_pruning_steps = context.steps;
574 : : else
575 : 6805 : initial_pruning_steps = NIL;
576 : :
577 : : /*
578 : : * If no exec Params appear in potentially-usable pruning clauses,
579 : : * then there's no point in even thinking about per-scan pruning.
580 : : */
581 [ + + ]: 7110 : if (context.has_exec_param)
582 : : {
583 : : /* ... OK, we'd better think about it */
584 : 347 : gen_partprune_steps(subpart, partprunequal, PARTTARGET_EXEC,
585 : : &context);
586 : :
587 [ - + ]: 347 : if (context.contradictory)
588 : : {
589 : : /* As above, skip run-time pruning if anything fishy happens */
590 : 0 : return NIL;
591 : : }
592 : :
593 : 347 : exec_pruning_steps = context.steps;
594 : :
595 : : /*
596 : : * Detect which exec Params actually got used; the fact that some
597 : : * were in available clauses doesn't mean we actually used them.
598 : : * Skip per-scan pruning if there are none.
599 : : */
600 : 347 : execparamids = get_partkey_exec_paramids(exec_pruning_steps);
601 : :
602 [ - + ]: 347 : if (bms_is_empty(execparamids))
603 : 0 : exec_pruning_steps = NIL;
604 : : }
605 : : else
606 : : {
607 : : /* No exec Params anywhere, so forget about scan-time pruning */
608 : 6763 : exec_pruning_steps = NIL;
609 : 6763 : execparamids = NULL;
610 : : }
611 : :
612 [ + + + + ]: 7110 : if (initial_pruning_steps || exec_pruning_steps)
613 : 637 : doruntimeprune = true;
614 : :
615 : : /* Begin constructing the PartitionedRelPruneInfo for this rel */
616 : 7110 : pinfo = makeNode(PartitionedRelPruneInfo);
617 : 7110 : pinfo->rtindex = rti;
618 : 7110 : pinfo->initial_pruning_steps = initial_pruning_steps;
619 : 7110 : pinfo->exec_pruning_steps = exec_pruning_steps;
620 : 7110 : pinfo->execparamids = execparamids;
621 : : /* Remaining fields will be filled in the next loop */
622 : :
623 : 7110 : pinfolist = lappend(pinfolist, pinfo);
624 : : }
625 : :
626 [ + + ]: 5797 : if (!doruntimeprune)
627 : : {
628 : : /* No run-time pruning required. */
629 : 5300 : pfree(relid_subpart_map);
630 : 5300 : return NIL;
631 : : }
632 : :
633 : : /*
634 : : * Run-time pruning will be required, so initialize other information.
635 : : * That includes two maps -- one needed to convert partition indexes of
636 : : * leaf partitions to the indexes of their subplans in the subplan list,
637 : : * another needed to convert partition indexes of sub-partitioned
638 : : * partitions to the indexes of their PartitionedRelPruneInfo in the
639 : : * PartitionedRelPruneInfo list.
640 : : */
641 [ + - + + : 1343 : foreach(lc, pinfolist)
+ + ]
642 : : {
643 : 846 : PartitionedRelPruneInfo *pinfo = lfirst(lc);
644 : 846 : RelOptInfo *subpart = find_base_rel(root, pinfo->rtindex);
645 : : Bitmapset *present_parts;
646 : 846 : int nparts = subpart->nparts;
647 : : int *subplan_map;
648 : : int *subpart_map;
649 : : Oid *relid_map;
650 : : int *leafpart_rti_map;
651 : :
652 : : /*
653 : : * Construct the subplan and subpart maps for this partitioning level.
654 : : * Here we convert to zero-based indexes, with -1 for empty entries.
655 : : * Also construct a Bitmapset of all partitions that are present (that
656 : : * is, not pruned already).
657 : : */
658 : 846 : subplan_map = palloc_array(int, nparts);
659 : 846 : memset(subplan_map, -1, nparts * sizeof(int));
660 : 846 : subpart_map = palloc_array(int, nparts);
661 : 846 : memset(subpart_map, -1, nparts * sizeof(int));
662 : 846 : relid_map = palloc0_array(Oid, nparts);
663 : 846 : leafpart_rti_map = palloc0_array(int, nparts);
664 : 846 : present_parts = NULL;
665 : :
666 : 846 : i = -1;
667 [ + + ]: 3177 : while ((i = bms_next_member(subpart->live_parts, i)) >= 0)
668 : : {
669 : 2331 : RelOptInfo *partrel = subpart->part_rels[i];
670 : : int subplanidx;
671 : : int subpartidx;
672 : :
673 : : Assert(partrel != NULL);
674 : :
675 : 2331 : subplan_map[i] = subplanidx = relid_subplan_map[partrel->relid] - 1;
676 : 2331 : subpart_map[i] = subpartidx = relid_subpart_map[partrel->relid] - 1;
677 [ + - ]: 2331 : relid_map[i] = planner_rt_fetch(partrel->relid, root)->relid;
678 : :
679 : : /*
680 : : * Track the RT indexes of "leaf" partitions so they can be
681 : : * included in the PlannerGlobal.prunableRelids set, indicating
682 : : * relations that may be pruned during executor startup.
683 : : *
684 : : * Only leaf partitions with a valid subplan that are prunable
685 : : * using initial pruning are added to prunableRelids. So
686 : : * partitions without a subplan due to constraint exclusion will
687 : : * remain in PlannedStmt.unprunableRelids.
688 : : */
689 [ + + ]: 2331 : if (subplanidx >= 0)
690 : : {
691 : 1977 : present_parts = bms_add_member(present_parts, i);
692 : :
693 : : /*
694 : : * Non-leaf partitions may appear here when they use an
695 : : * unflattened Append or MergeAppend. These should not be
696 : : * included in prunableRelids.
697 : : */
698 [ + + ]: 1977 : if (partrel->nparts == -1)
699 : 1952 : leafpart_rti_map[i] = (int) partrel->relid;
700 : :
701 : : /* Record finding this subplan */
702 : 1977 : subplansfound = bms_add_member(subplansfound, subplanidx);
703 : : }
704 [ + + ]: 354 : else if (subpartidx >= 0)
705 : 349 : present_parts = bms_add_member(present_parts, i);
706 : : }
707 : :
708 : : /*
709 : : * Ensure there were no stray PartitionedRelPruneInfo generated for
710 : : * partitioned tables that we have no sub-paths or
711 : : * sub-PartitionedRelPruneInfo for.
712 : : */
713 : : Assert(!bms_is_empty(present_parts));
714 : :
715 : : /* Record the maps and other information. */
716 : 846 : pinfo->present_parts = present_parts;
717 : 846 : pinfo->nparts = nparts;
718 : 846 : pinfo->subplan_map = subplan_map;
719 : 846 : pinfo->subpart_map = subpart_map;
720 : 846 : pinfo->relid_map = relid_map;
721 : 846 : pinfo->leafpart_rti_map = leafpart_rti_map;
722 : : }
723 : :
724 : 497 : pfree(relid_subpart_map);
725 : :
726 : 497 : *matchedsubplans = subplansfound;
727 : :
728 : 497 : return pinfolist;
729 : : }
730 : :
731 : : /*
732 : : * gen_partprune_steps
733 : : * Process 'clauses' (typically a rel's baserestrictinfo list of clauses)
734 : : * and create a list of "partition pruning steps".
735 : : *
736 : : * 'target' tells whether to generate pruning steps for planning (use
737 : : * immutable clauses only), or for executor startup (use any allowable
738 : : * clause except ones containing PARAM_EXEC Params), or for executor
739 : : * per-scan pruning (use any allowable clause).
740 : : *
741 : : * 'context' is an output argument that receives the steps list as well as
742 : : * some subsidiary flags; see the GeneratePruningStepsContext typedef.
743 : : */
744 : : static void
745 : 15730 : gen_partprune_steps(RelOptInfo *rel, List *clauses, PartClauseTarget target,
746 : : GeneratePruningStepsContext *context)
747 : : {
748 : : /* Initialize all output values to zero/false/NULL */
749 : 15730 : memset(context, 0, sizeof(GeneratePruningStepsContext));
750 : 15730 : context->rel = rel;
751 : 15730 : context->target = target;
752 : :
753 : : /*
754 : : * If this partitioned table is in turn a partition, and it shares any
755 : : * partition keys with its parent, then it's possible that the hierarchy
756 : : * allows the parent a narrower range of values than some of its
757 : : * partitions (particularly the default one). This is normally not
758 : : * useful, but it can be to prune the default partition.
759 : : */
760 [ + + + + ]: 15730 : if (partition_bound_has_default(rel->boundinfo) && rel->partition_qual)
761 : : {
762 : : /* Make a copy to avoid modifying the passed-in List */
763 : 615 : clauses = list_concat_copy(clauses, rel->partition_qual);
764 : : }
765 : :
766 : : /* Down into the rabbit-hole. */
767 : 15730 : (void) gen_partprune_steps_internal(context, clauses);
768 : 15730 : }
769 : :
770 : : /*
771 : : * prune_append_rel_partitions
772 : : * Process rel's baserestrictinfo and make use of quals which can be
773 : : * evaluated during query planning in order to determine the minimum set
774 : : * of partitions which must be scanned to satisfy these quals. Returns
775 : : * the matching partitions in the form of a Bitmapset containing the
776 : : * partitions' indexes in the rel's part_rels array.
777 : : *
778 : : * Callers must ensure that 'rel' is a partitioned table.
779 : : */
780 : : Bitmapset *
781 : 13784 : prune_append_rel_partitions(RelOptInfo *rel)
782 : : {
783 : 13784 : List *clauses = rel->baserestrictinfo;
784 : : List *pruning_steps;
785 : : GeneratePruningStepsContext gcontext;
786 : : PartitionPruneContext context;
787 : :
788 : : Assert(rel->part_scheme != NULL);
789 : :
790 : : /* If there are no partitions, return the empty set */
791 [ - + ]: 13784 : if (rel->nparts == 0)
792 : 0 : return NULL;
793 : :
794 : : /*
795 : : * If pruning is disabled or if there are no clauses to prune with, return
796 : : * all partitions.
797 : : */
798 [ + + + + ]: 13784 : if (!enable_partition_pruning || clauses == NIL)
799 : 5516 : return bms_add_range(NULL, 0, rel->nparts - 1);
800 : :
801 : : /*
802 : : * Process clauses to extract pruning steps that are usable at plan time.
803 : : * If the clauses are found to be contradictory, we can return the empty
804 : : * set.
805 : : */
806 : 8268 : gen_partprune_steps(rel, clauses, PARTTARGET_PLANNER,
807 : : &gcontext);
808 [ + + ]: 8268 : if (gcontext.contradictory)
809 : 108 : return NULL;
810 : 8160 : pruning_steps = gcontext.steps;
811 : :
812 : : /* If there's nothing usable, return all partitions */
813 [ + + ]: 8160 : if (pruning_steps == NIL)
814 : 2485 : return bms_add_range(NULL, 0, rel->nparts - 1);
815 : :
816 : : /* Set up PartitionPruneContext */
817 : 5675 : context.strategy = rel->part_scheme->strategy;
818 : 5675 : context.partnatts = rel->part_scheme->partnatts;
819 : 5675 : context.nparts = rel->nparts;
820 : 5675 : context.boundinfo = rel->boundinfo;
821 : 5675 : context.partcollation = rel->part_scheme->partcollation;
822 : 5675 : context.partsupfunc = rel->part_scheme->partsupfunc;
823 : 5675 : context.stepcmpfuncs = palloc0_array(FmgrInfo,
824 : : context.partnatts * list_length(pruning_steps));
825 : 5675 : context.ppccontext = CurrentMemoryContext;
826 : :
827 : : /* These are not valid when being called from the planner */
828 : 5675 : context.planstate = NULL;
829 : 5675 : context.exprcontext = NULL;
830 : 5675 : context.exprstates = NULL;
831 : :
832 : : /* Actual pruning happens here. */
833 : 5675 : return get_matching_partitions(&context, pruning_steps);
834 : : }
835 : :
836 : : /*
837 : : * get_matching_partitions
838 : : * Determine partitions that survive partition pruning
839 : : *
840 : : * Note: context->exprcontext must be valid when the pruning_steps were
841 : : * generated with a target other than PARTTARGET_PLANNER.
842 : : *
843 : : * Returns a Bitmapset of the RelOptInfo->part_rels indexes of the surviving
844 : : * partitions.
845 : : */
846 : : Bitmapset *
847 : 8356 : get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
848 : : {
849 : : Bitmapset *result;
850 : 8356 : int num_steps = list_length(pruning_steps),
851 : : i;
852 : : PruneStepResult **results,
853 : : *final_result;
854 : : ListCell *lc;
855 : : bool scan_default;
856 : :
857 : : /* If there are no pruning steps then all partitions match. */
858 [ - + ]: 8356 : if (num_steps == 0)
859 : : {
860 : : Assert(context->nparts > 0);
861 : 0 : return bms_add_range(NULL, 0, context->nparts - 1);
862 : : }
863 : :
864 : : /*
865 : : * Allocate space for individual pruning steps to store its result. Each
866 : : * slot will hold a PruneStepResult after performing a given pruning step.
867 : : * Later steps may use the result of one or more earlier steps. The
868 : : * result of applying all pruning steps is the value contained in the slot
869 : : * of the last pruning step.
870 : : */
871 : 8356 : results = palloc0_array(PruneStepResult *, num_steps);
872 [ + - + + : 20937 : foreach(lc, pruning_steps)
+ + ]
873 : : {
874 : 12581 : PartitionPruneStep *step = lfirst(lc);
875 : :
876 [ + + - ]: 12581 : switch (nodeTag(step))
877 : : {
878 : 10401 : case T_PartitionPruneStepOp:
879 : 20802 : results[step->step_id] =
880 : 10401 : perform_pruning_base_step(context,
881 : : (PartitionPruneStepOp *) step);
882 : 10401 : break;
883 : :
884 : 2180 : case T_PartitionPruneStepCombine:
885 : 4360 : results[step->step_id] =
886 : 2180 : perform_pruning_combine_step(context,
887 : : (PartitionPruneStepCombine *) step,
888 : : results);
889 : 2180 : break;
890 : :
891 : 0 : default:
892 [ # # ]: 0 : elog(ERROR, "invalid pruning step type: %d",
893 : : (int) nodeTag(step));
894 : : }
895 : : }
896 : :
897 : : /*
898 : : * At this point we know the offsets of all the datums whose corresponding
899 : : * partitions need to be in the result, including special null-accepting
900 : : * and default partitions. Collect the actual partition indexes now.
901 : : */
902 : 8356 : final_result = results[num_steps - 1];
903 : : Assert(final_result != NULL);
904 : 8356 : i = -1;
905 : 8356 : result = NULL;
906 : 8356 : scan_default = final_result->scan_default;
907 [ + + ]: 17320 : while ((i = bms_next_member(final_result->bound_offsets, i)) >= 0)
908 : : {
909 : : int partindex;
910 : :
911 : : Assert(i < context->boundinfo->nindexes);
912 : 8964 : partindex = context->boundinfo->indexes[i];
913 : :
914 [ + + ]: 8964 : if (partindex < 0)
915 : : {
916 : : /*
917 : : * In range partitioning cases, if a partition index is -1 it
918 : : * means that the bound at the offset is the upper bound for a
919 : : * range not covered by any partition (other than a possible
920 : : * default partition). In hash partitioning, the same means no
921 : : * partition has been defined for the corresponding remainder
922 : : * value.
923 : : *
924 : : * In either case, the value is still part of the queried range of
925 : : * values, so mark to scan the default partition if one exists.
926 : : */
927 : 1191 : scan_default |= partition_bound_has_default(context->boundinfo);
928 : 1191 : continue;
929 : : }
930 : :
931 : 7773 : result = bms_add_member(result, partindex);
932 : : }
933 : :
934 : : /* Add the null and/or default partition if needed and present. */
935 [ + + ]: 8356 : if (final_result->scan_null)
936 : : {
937 : : Assert(context->strategy == PARTITION_STRATEGY_LIST);
938 : : Assert(partition_bound_accepts_nulls(context->boundinfo));
939 : 140 : result = bms_add_member(result, context->boundinfo->null_index);
940 : : }
941 [ + + ]: 8356 : if (scan_default)
942 : : {
943 : : Assert(context->strategy == PARTITION_STRATEGY_LIST ||
944 : : context->strategy == PARTITION_STRATEGY_RANGE);
945 : : Assert(partition_bound_has_default(context->boundinfo));
946 : 666 : result = bms_add_member(result, context->boundinfo->default_index);
947 : : }
948 : :
949 : 8356 : return result;
950 : : }
951 : :
952 : : /*
953 : : * gen_partprune_steps_internal
954 : : * Processes 'clauses' to generate a List of partition pruning steps. We
955 : : * return NIL when no steps were generated.
956 : : *
957 : : * These partition pruning steps come in 2 forms; operator steps and combine
958 : : * steps.
959 : : *
960 : : * Operator steps (PartitionPruneStepOp) contain details of clauses that we
961 : : * determined that we can use for partition pruning. These contain details of
962 : : * the expression which is being compared to the partition key and the
963 : : * comparison function.
964 : : *
965 : : * Combine steps (PartitionPruneStepCombine) instruct the partition pruning
966 : : * code how it should produce a single set of partitions from multiple input
967 : : * operator and other combine steps. A PARTPRUNE_COMBINE_INTERSECT type
968 : : * combine step will merge its input steps to produce a result which only
969 : : * contains the partitions which are present in all of the input operator
970 : : * steps. A PARTPRUNE_COMBINE_UNION combine step will produce a result that
971 : : * has all of the partitions from each of the input operator steps.
972 : : *
973 : : * For BoolExpr clauses, each argument is processed recursively. Steps
974 : : * generated from processing an OR BoolExpr will be combined using
975 : : * PARTPRUNE_COMBINE_UNION. AND BoolExprs get combined using
976 : : * PARTPRUNE_COMBINE_INTERSECT.
977 : : *
978 : : * Otherwise, the list of clauses we receive we assume to be mutually ANDed.
979 : : * We generate all of the pruning steps we can based on these clauses and then
980 : : * at the end, if we have more than 1 step, we combine each step with a
981 : : * PARTPRUNE_COMBINE_INTERSECT combine step. Single steps are returned as-is.
982 : : *
983 : : * If we find clauses that are mutually contradictory, or contradictory with
984 : : * the partitioning constraint, or a pseudoconstant clause that contains
985 : : * false, we set context->contradictory to true and return NIL (that is, no
986 : : * pruning steps). Caller should consider all partitions as pruned in that
987 : : * case.
988 : : */
989 : : static List *
990 : 20488 : gen_partprune_steps_internal(GeneratePruningStepsContext *context,
991 : : List *clauses)
992 : : {
993 : 20488 : PartitionScheme part_scheme = context->rel->part_scheme;
994 : : List *keyclauses[PARTITION_MAX_KEYS];
995 : 20488 : Bitmapset *nullkeys = NULL,
996 : 20488 : *notnullkeys = NULL;
997 : 20488 : bool generate_opsteps = false;
998 : 20488 : List *result = NIL;
999 : : ListCell *lc;
1000 : :
1001 : : /*
1002 : : * If this partitioned relation has a default partition and is itself a
1003 : : * partition (as evidenced by partition_qual being not NIL), we first
1004 : : * check if the clauses contradict the partition constraint. If they do,
1005 : : * there's no need to generate any steps as it'd already be proven that no
1006 : : * partitions need to be scanned.
1007 : : *
1008 : : * This is a measure of last resort only to be used because the default
1009 : : * partition cannot be pruned using the steps generated from clauses that
1010 : : * contradict the parent's partition constraint; regular pruning, which is
1011 : : * cheaper, is sufficient when no default partition exists.
1012 : : */
1013 [ + + + + ]: 26586 : if (partition_bound_has_default(context->rel->boundinfo) &&
1014 : 6098 : predicate_refuted_by(context->rel->partition_qual, clauses, false))
1015 : : {
1016 : 235 : context->contradictory = true;
1017 : 235 : return NIL;
1018 : : }
1019 : :
1020 : 20253 : memset(keyclauses, 0, sizeof(keyclauses));
1021 [ + - + + : 48437 : foreach(lc, clauses)
+ + ]
1022 : : {
1023 : 28292 : Expr *clause = (Expr *) lfirst(lc);
1024 : : int i;
1025 : :
1026 : : /* Look through RestrictInfo, if any */
1027 [ + + ]: 28292 : if (IsA(clause, RestrictInfo))
1028 : 11026 : clause = ((RestrictInfo *) clause)->clause;
1029 : :
1030 : : /* Constant-false-or-null is contradictory */
1031 [ + + ]: 28292 : if (IsA(clause, Const) &&
1032 [ + - ]: 58 : (((Const *) clause)->constisnull ||
1033 [ + - ]: 58 : !DatumGetBool(((Const *) clause)->constvalue)))
1034 : : {
1035 : 58 : context->contradictory = true;
1036 : 108 : return NIL;
1037 : : }
1038 : :
1039 : : /* Get the BoolExpr's out of the way. */
1040 [ + + ]: 28234 : if (IsA(clause, BoolExpr))
1041 : : {
1042 : : /*
1043 : : * Generate steps for arguments.
1044 : : *
1045 : : * While steps generated for the arguments themselves will be
1046 : : * added to context->steps during recursion and will be evaluated
1047 : : * independently, collect their step IDs to be stored in the
1048 : : * combine step we'll be creating.
1049 : : */
1050 [ + + ]: 2537 : if (is_orclause(clause))
1051 : 1661 : {
1052 : 1661 : List *arg_stepids = NIL;
1053 : 1661 : bool all_args_contradictory = true;
1054 : : ListCell *lc1;
1055 : :
1056 : : /*
1057 : : * We can share the outer context area with the recursive
1058 : : * call, but contradictory had better not be true yet.
1059 : : */
1060 : : Assert(!context->contradictory);
1061 : :
1062 : : /*
1063 : : * Get pruning step for each arg. If we get contradictory for
1064 : : * all args, it means the OR expression is false as a whole.
1065 : : */
1066 [ + - + + : 5243 : foreach(lc1, ((BoolExpr *) clause)->args)
+ + ]
1067 : : {
1068 : 3582 : Expr *arg = lfirst(lc1);
1069 : : bool arg_contradictory;
1070 : : List *argsteps;
1071 : :
1072 : 3582 : argsteps = gen_partprune_steps_internal(context,
1073 : : list_make1(arg));
1074 : 3582 : arg_contradictory = context->contradictory;
1075 : : /* Keep context->contradictory clear till we're done */
1076 : 3582 : context->contradictory = false;
1077 : :
1078 [ + + ]: 3582 : if (arg_contradictory)
1079 : : {
1080 : : /* Just ignore self-contradictory arguments. */
1081 : 230 : continue;
1082 : : }
1083 : : else
1084 : 3352 : all_args_contradictory = false;
1085 : :
1086 [ + + ]: 3352 : if (argsteps != NIL)
1087 : : {
1088 : : /*
1089 : : * gen_partprune_steps_internal() always adds a single
1090 : : * combine step when it generates multiple steps, so
1091 : : * here we can just pay attention to the last one in
1092 : : * the list. If it just generated one, then the last
1093 : : * one in the list is still the one we want.
1094 : : */
1095 : 2884 : PartitionPruneStep *last = llast(argsteps);
1096 : :
1097 : 2884 : arg_stepids = lappend_int(arg_stepids, last->step_id);
1098 : : }
1099 : : else
1100 : : {
1101 : : PartitionPruneStep *orstep;
1102 : :
1103 : : /*
1104 : : * The arg didn't contain a clause matching this
1105 : : * partition key. We cannot prune using such an arg.
1106 : : * To indicate that to the pruning code, we must
1107 : : * construct a dummy PartitionPruneStepCombine whose
1108 : : * source_stepids is set to an empty List.
1109 : : */
1110 : 468 : orstep = gen_prune_step_combine(context, NIL,
1111 : : PARTPRUNE_COMBINE_UNION);
1112 : 468 : arg_stepids = lappend_int(arg_stepids, orstep->step_id);
1113 : : }
1114 : : }
1115 : :
1116 : : /* If all the OR arms are contradictory, we can stop */
1117 [ - + ]: 1661 : if (all_args_contradictory)
1118 : : {
1119 : 0 : context->contradictory = true;
1120 : 0 : return NIL;
1121 : : }
1122 : :
1123 [ + - ]: 1661 : if (arg_stepids != NIL)
1124 : : {
1125 : : PartitionPruneStep *step;
1126 : :
1127 : 1661 : step = gen_prune_step_combine(context, arg_stepids,
1128 : : PARTPRUNE_COMBINE_UNION);
1129 : 1661 : result = lappend(result, step);
1130 : : }
1131 : 1661 : continue;
1132 : : }
1133 [ + + ]: 876 : else if (is_andclause(clause))
1134 : 656 : {
1135 : 656 : List *args = ((BoolExpr *) clause)->args;
1136 : : List *argsteps;
1137 : :
1138 : : /*
1139 : : * args may itself contain clauses of arbitrary type, so just
1140 : : * recurse and later combine the component partitions sets
1141 : : * using a combine step.
1142 : : */
1143 : 656 : argsteps = gen_partprune_steps_internal(context, args);
1144 : :
1145 : : /* If any AND arm is contradictory, we can stop immediately */
1146 [ - + ]: 656 : if (context->contradictory)
1147 : 0 : return NIL;
1148 : :
1149 : : /*
1150 : : * gen_partprune_steps_internal() always adds a single combine
1151 : : * step when it generates multiple steps, so here we can just
1152 : : * pay attention to the last one in the list. If it just
1153 : : * generated one, then the last one in the list is still the
1154 : : * one we want.
1155 : : */
1156 [ + + ]: 656 : if (argsteps != NIL)
1157 : 486 : result = lappend(result, llast(argsteps));
1158 : :
1159 : 656 : continue;
1160 : : }
1161 : :
1162 : : /*
1163 : : * Fall-through for a NOT clause, which if it's a Boolean clause,
1164 : : * will be handled in match_clause_to_partition_key(). We
1165 : : * currently don't perform any pruning for more complex NOT
1166 : : * clauses.
1167 : : */
1168 : : }
1169 : :
1170 : : /*
1171 : : * See if we can match this clause to any of the partition keys.
1172 : : */
1173 [ + + ]: 36106 : for (i = 0; i < part_scheme->partnatts; i++)
1174 : : {
1175 : 29427 : Expr *partkey = linitial(context->rel->partexprs[i]);
1176 : 29427 : bool clause_is_not_null = false;
1177 : 29427 : PartClauseInfo *pc = NULL;
1178 : 29427 : List *clause_steps = NIL;
1179 : :
1180 [ + + + + : 29427 : switch (match_clause_to_partition_key(context,
+ + - ]
1181 : : clause, partkey, i,
1182 : : &clause_is_not_null,
1183 : : &pc, &clause_steps))
1184 : : {
1185 : 15217 : case PARTCLAUSE_MATCH_CLAUSE:
1186 : : Assert(pc != NULL);
1187 : :
1188 : : /*
1189 : : * Since we only allow strict operators, check for any
1190 : : * contradicting IS NULL.
1191 : : */
1192 [ + + ]: 15217 : if (bms_is_member(i, nullkeys))
1193 : : {
1194 : 5 : context->contradictory = true;
1195 : 50 : return NIL;
1196 : : }
1197 : 15212 : generate_opsteps = true;
1198 : 15212 : keyclauses[i] = lappend(keyclauses[i], pc);
1199 : 15212 : break;
1200 : :
1201 : 1890 : case PARTCLAUSE_MATCH_NULLNESS:
1202 [ + + ]: 1890 : if (!clause_is_not_null)
1203 : : {
1204 : : /*
1205 : : * check for conflicting IS NOT NULL as well as
1206 : : * contradicting strict clauses
1207 : : */
1208 [ + + ]: 1385 : if (bms_is_member(i, notnullkeys) ||
1209 [ + + ]: 1380 : keyclauses[i] != NIL)
1210 : : {
1211 : 25 : context->contradictory = true;
1212 : 25 : return NIL;
1213 : : }
1214 : 1360 : nullkeys = bms_add_member(nullkeys, i);
1215 : : }
1216 : : else
1217 : : {
1218 : : /* check for conflicting IS NULL */
1219 [ - + ]: 505 : if (bms_is_member(i, nullkeys))
1220 : : {
1221 : 0 : context->contradictory = true;
1222 : 0 : return NIL;
1223 : : }
1224 : 505 : notnullkeys = bms_add_member(notnullkeys, i);
1225 : : }
1226 : 1865 : break;
1227 : :
1228 : 520 : case PARTCLAUSE_MATCH_STEPS:
1229 : : Assert(clause_steps != NIL);
1230 : 520 : result = list_concat(result, clause_steps);
1231 : 520 : break;
1232 : :
1233 : 20 : case PARTCLAUSE_MATCH_CONTRADICT:
1234 : : /* We've nothing more to do if a contradiction was found. */
1235 : 20 : context->contradictory = true;
1236 : 20 : return NIL;
1237 : :
1238 : 10189 : case PARTCLAUSE_NOMATCH:
1239 : :
1240 : : /*
1241 : : * Clause didn't match this key, but it might match the
1242 : : * next one.
1243 : : */
1244 : 10189 : continue;
1245 : :
1246 : 1591 : case PARTCLAUSE_UNSUPPORTED:
1247 : : /* This clause cannot be used for pruning. */
1248 : 1591 : break;
1249 : : }
1250 : :
1251 : : /* done; go check the next clause. */
1252 : 19188 : break;
1253 : : }
1254 : : }
1255 : :
1256 : : /*-----------
1257 : : * Now generate some (more) pruning steps. We have three strategies:
1258 : : *
1259 : : * 1) Generate pruning steps based on IS NULL clauses:
1260 : : * a) For list partitioning, null partition keys can only be found in
1261 : : * the designated null-accepting partition, so if there are IS NULL
1262 : : * clauses containing partition keys we should generate a pruning
1263 : : * step that gets rid of all partitions but that one. We can
1264 : : * disregard any OpExpr we may have found.
1265 : : * b) For range partitioning, only the default partition can contain
1266 : : * NULL values, so the same rationale applies.
1267 : : * c) For hash partitioning, we only apply this strategy if we have
1268 : : * IS NULL clauses for all the keys. Strategy 2 below will take
1269 : : * care of the case where some keys have OpExprs and others have
1270 : : * IS NULL clauses.
1271 : : *
1272 : : * 2) If not, generate steps based on OpExprs we have (if any).
1273 : : *
1274 : : * 3) If this doesn't work either, we may be able to generate steps to
1275 : : * prune just the null-accepting partition (if one exists), if we have
1276 : : * IS NOT NULL clauses for all partition keys.
1277 : : */
1278 [ + + ]: 20145 : if (!bms_is_empty(nullkeys) &&
1279 [ + + ]: 985 : (part_scheme->strategy == PARTITION_STRATEGY_LIST ||
1280 [ + + ]: 475 : part_scheme->strategy == PARTITION_STRATEGY_RANGE ||
1281 [ + - ]: 370 : (part_scheme->strategy == PARTITION_STRATEGY_HASH &&
1282 [ + + ]: 370 : bms_num_members(nullkeys) == part_scheme->partnatts)))
1283 : 655 : {
1284 : : PartitionPruneStep *step;
1285 : :
1286 : : /* Strategy 1 */
1287 : 655 : step = gen_prune_step_op(context, InvalidStrategy,
1288 : : false, NIL, NIL, nullkeys);
1289 : 655 : result = lappend(result, step);
1290 : : }
1291 [ + + ]: 19490 : else if (generate_opsteps)
1292 : : {
1293 : : List *opsteps;
1294 : :
1295 : : /* Strategy 2 */
1296 : 12987 : opsteps = gen_prune_steps_from_opexps(context, keyclauses, nullkeys);
1297 : 12987 : result = list_concat(result, opsteps);
1298 : : }
1299 [ + + ]: 6503 : else if (bms_num_members(notnullkeys) == part_scheme->partnatts)
1300 : : {
1301 : : PartitionPruneStep *step;
1302 : :
1303 : : /* Strategy 3 */
1304 : 170 : step = gen_prune_step_op(context, InvalidStrategy,
1305 : : false, NIL, NIL, NULL);
1306 : 170 : result = lappend(result, step);
1307 : : }
1308 : :
1309 : : /*
1310 : : * Finally, if there are multiple steps, since the 'clauses' are mutually
1311 : : * ANDed, add an INTERSECT step to combine the partition sets resulting
1312 : : * from them and append it to the result list.
1313 : : */
1314 [ + + ]: 20145 : if (list_length(result) > 1)
1315 : : {
1316 : 1570 : List *step_ids = NIL;
1317 : : PartitionPruneStep *final;
1318 : :
1319 [ + - + + : 5625 : foreach(lc, result)
+ + ]
1320 : : {
1321 : 4055 : PartitionPruneStep *step = lfirst(lc);
1322 : :
1323 : 4055 : step_ids = lappend_int(step_ids, step->step_id);
1324 : : }
1325 : :
1326 : 1570 : final = gen_prune_step_combine(context, step_ids,
1327 : : PARTPRUNE_COMBINE_INTERSECT);
1328 : 1570 : result = lappend(result, final);
1329 : : }
1330 : :
1331 : 20145 : return result;
1332 : : }
1333 : :
1334 : : /*
1335 : : * gen_prune_step_op
1336 : : * Generate a pruning step for a specific operator
1337 : : *
1338 : : * The step is assigned a unique step identifier and added to context's 'steps'
1339 : : * list.
1340 : : */
1341 : : static PartitionPruneStep *
1342 : 15292 : gen_prune_step_op(GeneratePruningStepsContext *context,
1343 : : StrategyNumber opstrategy, bool op_is_ne,
1344 : : List *exprs, List *cmpfns,
1345 : : Bitmapset *nullkeys)
1346 : : {
1347 : 15292 : PartitionPruneStepOp *opstep = makeNode(PartitionPruneStepOp);
1348 : :
1349 : 15292 : opstep->step.step_id = context->next_step_id++;
1350 : :
1351 : : /*
1352 : : * For clauses that contain an <> operator, set opstrategy to
1353 : : * InvalidStrategy to signal get_matching_list_bounds to do the right
1354 : : * thing.
1355 : : */
1356 [ + + ]: 15292 : opstep->opstrategy = op_is_ne ? InvalidStrategy : opstrategy;
1357 : : Assert(list_length(exprs) == list_length(cmpfns));
1358 : 15292 : opstep->exprs = exprs;
1359 : 15292 : opstep->cmpfns = cmpfns;
1360 : 15292 : opstep->nullkeys = nullkeys;
1361 : :
1362 : 15292 : context->steps = lappend(context->steps, opstep);
1363 : :
1364 : 15292 : return (PartitionPruneStep *) opstep;
1365 : : }
1366 : :
1367 : : /*
1368 : : * gen_prune_step_combine
1369 : : * Generate a pruning step for a combination of several other steps
1370 : : *
1371 : : * The step is assigned a unique step identifier and added to context's
1372 : : * 'steps' list.
1373 : : */
1374 : : static PartitionPruneStep *
1375 : 3699 : gen_prune_step_combine(GeneratePruningStepsContext *context,
1376 : : List *source_stepids,
1377 : : PartitionPruneCombineOp combineOp)
1378 : : {
1379 : 3699 : PartitionPruneStepCombine *cstep = makeNode(PartitionPruneStepCombine);
1380 : :
1381 : 3699 : cstep->step.step_id = context->next_step_id++;
1382 : 3699 : cstep->combineOp = combineOp;
1383 : 3699 : cstep->source_stepids = source_stepids;
1384 : :
1385 : 3699 : context->steps = lappend(context->steps, cstep);
1386 : :
1387 : 3699 : return (PartitionPruneStep *) cstep;
1388 : : }
1389 : :
1390 : : /*
1391 : : * gen_prune_steps_from_opexps
1392 : : * Generate and return a list of PartitionPruneStepOp that are based on
1393 : : * OpExpr and BooleanTest clauses that have been matched to the partition
1394 : : * key.
1395 : : *
1396 : : * 'keyclauses' is an array of List pointers, indexed by the partition key's
1397 : : * index. Each List element in the array can contain clauses that match to
1398 : : * the corresponding partition key column. Partition key columns without any
1399 : : * matched clauses will have an empty List.
1400 : : *
1401 : : * Some partitioning strategies allow pruning to still occur when we only have
1402 : : * clauses for a prefix of the partition key columns, for example, RANGE
1403 : : * partitioning. Other strategies, such as HASH partitioning, require clauses
1404 : : * for all partition key columns.
1405 : : *
1406 : : * When we return multiple pruning steps here, it's up to the caller to add a
1407 : : * relevant "combine" step to combine the returned steps. This is not done
1408 : : * here as callers may wish to include additional pruning steps before
1409 : : * combining them all.
1410 : : */
1411 : : static List *
1412 : 12987 : gen_prune_steps_from_opexps(GeneratePruningStepsContext *context,
1413 : : List **keyclauses, Bitmapset *nullkeys)
1414 : : {
1415 : 12987 : PartitionScheme part_scheme = context->rel->part_scheme;
1416 : 12987 : List *opsteps = NIL;
1417 : : List *btree_clauses[BTMaxStrategyNumber + 1],
1418 : : *hash_clauses[HTMaxStrategyNumber + 1];
1419 : : int i;
1420 : : ListCell *lc;
1421 : :
1422 : 12987 : memset(btree_clauses, 0, sizeof(btree_clauses));
1423 : 12987 : memset(hash_clauses, 0, sizeof(hash_clauses));
1424 [ + + ]: 25521 : for (i = 0; i < part_scheme->partnatts; i++)
1425 : : {
1426 : 15077 : List *clauselist = keyclauses[i];
1427 : 15077 : bool consider_next_key = true;
1428 : :
1429 : : /*
1430 : : * For range partitioning, if we have no clauses for the current key,
1431 : : * we can't consider any later keys either, so we can stop here.
1432 : : */
1433 [ + + + + ]: 15077 : if (part_scheme->strategy == PARTITION_STRATEGY_RANGE &&
1434 : : clauselist == NIL)
1435 : 540 : break;
1436 : :
1437 : : /*
1438 : : * For hash partitioning, if a column doesn't have the necessary
1439 : : * equality clause, there should be an IS NULL clause, otherwise
1440 : : * pruning is not possible.
1441 : : */
1442 [ + + + + ]: 14537 : if (part_scheme->strategy == PARTITION_STRATEGY_HASH &&
1443 [ + + ]: 645 : clauselist == NIL && !bms_is_member(i, nullkeys))
1444 : 60 : return NIL;
1445 : :
1446 [ + + + + : 29389 : foreach(lc, clauselist)
+ + ]
1447 : : {
1448 : 14912 : PartClauseInfo *pc = (PartClauseInfo *) lfirst(lc);
1449 : : Oid lefttype,
1450 : : righttype;
1451 : :
1452 : : /* Look up the operator's btree/hash strategy number. */
1453 [ + + ]: 14912 : if (pc->op_strategy == InvalidStrategy)
1454 : 525 : get_op_opfamily_properties(pc->opno,
1455 : 525 : part_scheme->partopfamily[i],
1456 : : false,
1457 : : &pc->op_strategy,
1458 : : &lefttype,
1459 : : &righttype);
1460 : :
1461 [ + + - ]: 14912 : switch (part_scheme->strategy)
1462 : : {
1463 : 14013 : case PARTITION_STRATEGY_LIST:
1464 : : case PARTITION_STRATEGY_RANGE:
1465 : 28026 : btree_clauses[pc->op_strategy] =
1466 : 14013 : lappend(btree_clauses[pc->op_strategy], pc);
1467 : :
1468 : : /*
1469 : : * We can't consider subsequent partition keys if the
1470 : : * clause for the current key contains a non-inclusive
1471 : : * operator.
1472 : : */
1473 [ + + ]: 14013 : if (pc->op_strategy == BTLessStrategyNumber ||
1474 [ + + ]: 12529 : pc->op_strategy == BTGreaterStrategyNumber)
1475 : 2078 : consider_next_key = false;
1476 : 14013 : break;
1477 : :
1478 : 899 : case PARTITION_STRATEGY_HASH:
1479 [ - + ]: 899 : if (pc->op_strategy != HTEqualStrategyNumber)
1480 [ # # ]: 0 : elog(ERROR, "invalid clause for hash partitioning");
1481 : 1798 : hash_clauses[pc->op_strategy] =
1482 : 899 : lappend(hash_clauses[pc->op_strategy], pc);
1483 : 899 : break;
1484 : :
1485 : 0 : default:
1486 [ # # ]: 0 : elog(ERROR, "invalid partition strategy: %c",
1487 : : part_scheme->strategy);
1488 : : break;
1489 : : }
1490 : : }
1491 : :
1492 : : /*
1493 : : * If we've decided that clauses for subsequent partition keys
1494 : : * wouldn't be useful for pruning, don't search any further.
1495 : : */
1496 [ + + ]: 14477 : if (!consider_next_key)
1497 : 1943 : break;
1498 : : }
1499 : :
1500 : : /*
1501 : : * Now, we have divided clauses according to their operator strategies.
1502 : : * Check for each strategy if we can generate pruning step(s) by
1503 : : * collecting a list of expressions whose values will constitute a vector
1504 : : * that can be used as a lookup key by a partition bound searching
1505 : : * function.
1506 : : */
1507 [ + + - ]: 12927 : switch (part_scheme->strategy)
1508 : : {
1509 : 12453 : case PARTITION_STRATEGY_LIST:
1510 : : case PARTITION_STRATEGY_RANGE:
1511 : : {
1512 : 12453 : List *eq_clauses = btree_clauses[BTEqualStrategyNumber];
1513 : 12453 : List *le_clauses = btree_clauses[BTLessEqualStrategyNumber];
1514 : 12453 : List *ge_clauses = btree_clauses[BTGreaterEqualStrategyNumber];
1515 : : int strat;
1516 : :
1517 : : /*
1518 : : * For each clause under consideration for a given strategy,
1519 : : * we collect expressions from clauses for earlier keys, whose
1520 : : * operator strategy is inclusive, into a list called
1521 : : * 'prefix'. By appending the clause's own expression to the
1522 : : * 'prefix', we'll generate one step using the so generated
1523 : : * vector and assign the current strategy to it. Actually,
1524 : : * 'prefix' might contain multiple clauses for the same key,
1525 : : * in which case, we must generate steps for various
1526 : : * combinations of expressions of different keys, which
1527 : : * get_steps_using_prefix takes care of for us.
1528 : : */
1529 [ + + ]: 74718 : for (strat = 1; strat <= BTMaxStrategyNumber; strat++)
1530 : : {
1531 [ + + + + : 76228 : foreach(lc, btree_clauses[strat])
+ + ]
1532 : : {
1533 : 14003 : PartClauseInfo *pc = lfirst(lc);
1534 : : ListCell *eq_start;
1535 : : ListCell *le_start;
1536 : : ListCell *ge_start;
1537 : : ListCell *lc1;
1538 : 14003 : List *prefix = NIL;
1539 : : List *pc_steps;
1540 : 14003 : bool prefix_valid = true;
1541 : : bool pk_has_clauses;
1542 : : int keyno;
1543 : :
1544 : : /*
1545 : : * If this is a clause for the first partition key,
1546 : : * there are no preceding expressions; generate a
1547 : : * pruning step without a prefix.
1548 : : *
1549 : : * Note that we pass NULL for step_nullkeys, because
1550 : : * we don't search list/range partition bounds where
1551 : : * some keys are NULL.
1552 : : */
1553 [ + + ]: 14003 : if (pc->keyno == 0)
1554 : : {
1555 : : Assert(pc->op_strategy == strat);
1556 : 13323 : pc_steps = get_steps_using_prefix(context, strat,
1557 : 13323 : pc->op_is_ne,
1558 : : pc->expr,
1559 : : pc->cmpfn,
1560 : : NULL,
1561 : : NIL);
1562 : 13323 : opsteps = list_concat(opsteps, pc_steps);
1563 : 13323 : continue;
1564 : : }
1565 : :
1566 : 680 : eq_start = list_head(eq_clauses);
1567 : 680 : le_start = list_head(le_clauses);
1568 : 680 : ge_start = list_head(ge_clauses);
1569 : :
1570 : : /*
1571 : : * We arrange clauses into prefix in ascending order
1572 : : * of their partition key numbers.
1573 : : */
1574 [ + + ]: 1490 : for (keyno = 0; keyno < pc->keyno; keyno++)
1575 : : {
1576 : 850 : pk_has_clauses = false;
1577 : :
1578 : : /*
1579 : : * Expressions from = clauses can always be in the
1580 : : * prefix, provided they're from an earlier key.
1581 : : */
1582 [ + + + + : 1525 : for_each_cell(lc1, eq_clauses, eq_start)
+ + ]
1583 : : {
1584 : 1205 : PartClauseInfo *eqpc = lfirst(lc1);
1585 : :
1586 [ + + ]: 1205 : if (eqpc->keyno == keyno)
1587 : : {
1588 : 675 : prefix = lappend(prefix, eqpc);
1589 : 675 : pk_has_clauses = true;
1590 : : }
1591 : : else
1592 : : {
1593 : : Assert(eqpc->keyno > keyno);
1594 : 530 : break;
1595 : : }
1596 : : }
1597 : 850 : eq_start = lc1;
1598 : :
1599 : : /*
1600 : : * If we're generating steps for </<= strategy, we
1601 : : * can add other <= clauses to the prefix,
1602 : : * provided they're from an earlier key.
1603 : : */
1604 [ + + + + ]: 850 : if (strat == BTLessStrategyNumber ||
1605 : : strat == BTLessEqualStrategyNumber)
1606 : : {
1607 [ + + + + : 145 : for_each_cell(lc1, le_clauses, le_start)
+ + ]
1608 : : {
1609 : 75 : PartClauseInfo *lepc = lfirst(lc1);
1610 : :
1611 [ + + ]: 75 : if (lepc->keyno == keyno)
1612 : : {
1613 : 25 : prefix = lappend(prefix, lepc);
1614 : 25 : pk_has_clauses = true;
1615 : : }
1616 : : else
1617 : : {
1618 : : Assert(lepc->keyno > keyno);
1619 : 50 : break;
1620 : : }
1621 : : }
1622 : 120 : le_start = lc1;
1623 : : }
1624 : :
1625 : : /*
1626 : : * If we're generating steps for >/>= strategy, we
1627 : : * can add other >= clauses to the prefix,
1628 : : * provided they're from an earlier key.
1629 : : */
1630 [ + + + + ]: 850 : if (strat == BTGreaterStrategyNumber ||
1631 : : strat == BTGreaterEqualStrategyNumber)
1632 : : {
1633 [ + + + - : 400 : for_each_cell(lc1, ge_clauses, ge_start)
+ + ]
1634 : : {
1635 : 320 : PartClauseInfo *gepc = lfirst(lc1);
1636 : :
1637 [ + + ]: 320 : if (gepc->keyno == keyno)
1638 : : {
1639 : 140 : prefix = lappend(prefix, gepc);
1640 : 140 : pk_has_clauses = true;
1641 : : }
1642 : : else
1643 : : {
1644 : : Assert(gepc->keyno > keyno);
1645 : 180 : break;
1646 : : }
1647 : : }
1648 : 260 : ge_start = lc1;
1649 : : }
1650 : :
1651 : : /*
1652 : : * If this key has no clauses, prefix is not valid
1653 : : * anymore.
1654 : : */
1655 [ + + ]: 850 : if (!pk_has_clauses)
1656 : : {
1657 : 40 : prefix_valid = false;
1658 : 40 : break;
1659 : : }
1660 : : }
1661 : :
1662 : : /*
1663 : : * If prefix_valid, generate PartitionPruneStepOps.
1664 : : * Otherwise, we would not find clauses for a valid
1665 : : * subset of the partition keys anymore for the
1666 : : * strategy; give up on generating partition pruning
1667 : : * steps further for the strategy.
1668 : : *
1669 : : * As mentioned above, if 'prefix' contains multiple
1670 : : * expressions for the same key, the following will
1671 : : * generate multiple steps, one for each combination
1672 : : * of the expressions for different keys.
1673 : : *
1674 : : * Note that we pass NULL for step_nullkeys, because
1675 : : * we don't search list/range partition bounds where
1676 : : * some keys are NULL.
1677 : : */
1678 [ + + ]: 680 : if (prefix_valid)
1679 : : {
1680 : : Assert(pc->op_strategy == strat);
1681 : 640 : pc_steps = get_steps_using_prefix(context, strat,
1682 : 640 : pc->op_is_ne,
1683 : : pc->expr,
1684 : : pc->cmpfn,
1685 : : NULL,
1686 : : prefix);
1687 : 640 : opsteps = list_concat(opsteps, pc_steps);
1688 : : }
1689 : : else
1690 : 40 : break;
1691 : : }
1692 : : }
1693 : 12453 : break;
1694 : : }
1695 : :
1696 : 474 : case PARTITION_STRATEGY_HASH:
1697 : : {
1698 : 474 : List *eq_clauses = hash_clauses[HTEqualStrategyNumber];
1699 : :
1700 : : /* For hash partitioning, we have just the = strategy. */
1701 [ + - ]: 474 : if (eq_clauses != NIL)
1702 : : {
1703 : : PartClauseInfo *pc;
1704 : : List *pc_steps;
1705 : 474 : List *prefix = NIL;
1706 : : int last_keyno;
1707 : : ListCell *lc1;
1708 : :
1709 : : /*
1710 : : * Locate the clause for the greatest column. This may
1711 : : * not belong to the last partition key, but it is the
1712 : : * clause belonging to the last partition key we found a
1713 : : * clause for above.
1714 : : */
1715 : 474 : pc = llast(eq_clauses);
1716 : :
1717 : : /*
1718 : : * There might be multiple clauses which matched to that
1719 : : * partition key; find the first such clause. While at
1720 : : * it, add all the clauses before that one to 'prefix'.
1721 : : */
1722 : 474 : last_keyno = pc->keyno;
1723 [ + - + - : 889 : foreach(lc, eq_clauses)
+ - ]
1724 : : {
1725 : 889 : pc = lfirst(lc);
1726 [ + + ]: 889 : if (pc->keyno == last_keyno)
1727 : 474 : break;
1728 : 415 : prefix = lappend(prefix, pc);
1729 : : }
1730 : :
1731 : : /*
1732 : : * For each clause for the "last" column, after appending
1733 : : * the clause's own expression to the 'prefix', we'll
1734 : : * generate one step using the so generated vector and
1735 : : * assign = as its strategy. Actually, 'prefix' might
1736 : : * contain multiple clauses for the same key, in which
1737 : : * case, we must generate steps for various combinations
1738 : : * of expressions of different keys, which
1739 : : * get_steps_using_prefix will take care of for us.
1740 : : */
1741 [ + - + + : 948 : for_each_cell(lc1, eq_clauses, lc)
+ + ]
1742 : : {
1743 : 474 : pc = lfirst(lc1);
1744 : :
1745 : : /*
1746 : : * Note that we pass nullkeys for step_nullkeys,
1747 : : * because we need to tell hash partition bound search
1748 : : * function which of the keys we found IS NULL clauses
1749 : : * for.
1750 : : */
1751 : : Assert(pc->op_strategy == HTEqualStrategyNumber);
1752 : : pc_steps =
1753 : 474 : get_steps_using_prefix(context,
1754 : : HTEqualStrategyNumber,
1755 : : false,
1756 : : pc->expr,
1757 : : pc->cmpfn,
1758 : : nullkeys,
1759 : : prefix);
1760 : 474 : opsteps = list_concat(opsteps, pc_steps);
1761 : : }
1762 : : }
1763 : 474 : break;
1764 : : }
1765 : :
1766 : 0 : default:
1767 [ # # ]: 0 : elog(ERROR, "invalid partition strategy: %c",
1768 : : part_scheme->strategy);
1769 : : break;
1770 : : }
1771 : :
1772 : 12927 : return opsteps;
1773 : : }
1774 : :
1775 : : /*
1776 : : * If the partition key has a collation, then the clause must have the same
1777 : : * input collation. If the partition key is non-collatable, we assume the
1778 : : * collation doesn't matter, because while collation wasn't considered when
1779 : : * performing partitioning, the clause still may have a collation assigned
1780 : : * due to the other input being of a collatable type.
1781 : : *
1782 : : * See also IndexCollMatchesExprColl.
1783 : : */
1784 : : #define PartCollMatchesExprColl(partcoll, exprcoll) \
1785 : : ((partcoll) == InvalidOid || (partcoll) == (exprcoll))
1786 : :
1787 : : /*
1788 : : * match_clause_to_partition_key
1789 : : * Attempt to match the given 'clause' with the specified partition key.
1790 : : *
1791 : : * Return value is:
1792 : : * * PARTCLAUSE_NOMATCH if the clause doesn't match this partition key (but
1793 : : * caller should keep trying, because it might match a subsequent key).
1794 : : * Output arguments: none set.
1795 : : *
1796 : : * * PARTCLAUSE_MATCH_CLAUSE if there is a match.
1797 : : * Output arguments: *pc is set to a PartClauseInfo constructed for the
1798 : : * matched clause.
1799 : : *
1800 : : * * PARTCLAUSE_MATCH_NULLNESS if there is a match, and the matched clause was
1801 : : * either a "a IS NULL" or "a IS NOT NULL" clause.
1802 : : * Output arguments: *clause_is_not_null is set to false in the former case
1803 : : * true otherwise.
1804 : : *
1805 : : * * PARTCLAUSE_MATCH_STEPS if there is a match.
1806 : : * Output arguments: *clause_steps is set to the list of recursively
1807 : : * generated steps for the clause.
1808 : : *
1809 : : * * PARTCLAUSE_MATCH_CONTRADICT if the clause is self-contradictory, ie
1810 : : * it provably returns FALSE or NULL.
1811 : : * Output arguments: none set.
1812 : : *
1813 : : * * PARTCLAUSE_UNSUPPORTED if the clause doesn't match this partition key
1814 : : * and couldn't possibly match any other one either, due to its form or
1815 : : * properties (such as containing a volatile function).
1816 : : * Output arguments: none set.
1817 : : *
1818 : : * Note that when pulling up a subquery, the clause operands may get wrapped
1819 : : * in PlaceHolderVars to enforce separate identity or as a result of outer
1820 : : * joins. We must strip such no-op PlaceHolderVars before comparing operands
1821 : : * to the partition key, otherwise the equal() checks will fail to recognize
1822 : : * valid matches. This is safe because the clauses here are always
1823 : : * relation-scan-level expressions, where a PlaceHolderVar with empty
1824 : : * phnullingrels is effectively a no-op. Stripping may also bring separate
1825 : : * RelabelType nodes into adjacency, so we must loop when peeling those.
1826 : : */
1827 : : static PartClauseMatchStatus
1828 : 29427 : match_clause_to_partition_key(GeneratePruningStepsContext *context,
1829 : : Expr *clause, const Expr *partkey, int partkeyidx,
1830 : : bool *clause_is_not_null, PartClauseInfo **pc,
1831 : : List **clause_steps)
1832 : : {
1833 : : PartClauseMatchStatus boolmatchstatus;
1834 : 29427 : PartitionScheme part_scheme = context->rel->part_scheme;
1835 : 29427 : Oid partopfamily = part_scheme->partopfamily[partkeyidx],
1836 : 29427 : partcoll = part_scheme->partcollation[partkeyidx];
1837 : : Expr *expr;
1838 : : bool notclause;
1839 : :
1840 : : /*
1841 : : * Recognize specially shaped clauses that match a Boolean partition key.
1842 : : */
1843 : 29427 : boolmatchstatus = match_boolean_partition_clause(partopfamily, clause,
1844 : : partkey, &expr,
1845 : : ¬clause);
1846 : :
1847 [ + + ]: 29427 : if (boolmatchstatus == PARTCLAUSE_MATCH_CLAUSE)
1848 : : {
1849 : : PartClauseInfo *partclause;
1850 : :
1851 : : /*
1852 : : * For bool tests in the form of partkey IS NOT true and IS NOT false,
1853 : : * we invert these clauses. Effectively, "partkey IS NOT true"
1854 : : * becomes "partkey IS false OR partkey IS NULL". We do this by
1855 : : * building an OR BoolExpr and forming a clause just like that and
1856 : : * punt it off to gen_partprune_steps_internal() to generate pruning
1857 : : * steps.
1858 : : */
1859 [ + + ]: 570 : if (notclause)
1860 : : {
1861 : : List *new_clauses;
1862 : : List *or_clause;
1863 : 180 : BooleanTest *new_booltest = (BooleanTest *) copyObject(clause);
1864 : : NullTest *nulltest;
1865 : :
1866 : : /* We expect 'notclause' to only be set to true for BooleanTests */
1867 : : Assert(IsA(clause, BooleanTest));
1868 : :
1869 : : /* reverse the bool test */
1870 [ + + ]: 180 : if (new_booltest->booltesttype == IS_NOT_TRUE)
1871 : 110 : new_booltest->booltesttype = IS_FALSE;
1872 [ + - ]: 70 : else if (new_booltest->booltesttype == IS_NOT_FALSE)
1873 : 70 : new_booltest->booltesttype = IS_TRUE;
1874 : : else
1875 : : {
1876 : : /*
1877 : : * We only expect match_boolean_partition_clause to return
1878 : : * PARTCLAUSE_MATCH_CLAUSE for IS_NOT_TRUE and IS_NOT_FALSE.
1879 : : */
1880 : : Assert(false);
1881 : : }
1882 : :
1883 : 180 : nulltest = makeNode(NullTest);
1884 : 180 : nulltest->arg = copyObject(partkey);
1885 : 180 : nulltest->nulltesttype = IS_NULL;
1886 : 180 : nulltest->argisrow = false;
1887 : 180 : nulltest->location = -1;
1888 : :
1889 : 180 : new_clauses = list_make2(new_booltest, nulltest);
1890 : 180 : or_clause = list_make1(makeBoolExpr(OR_EXPR, new_clauses, -1));
1891 : :
1892 : : /* Finally, generate steps */
1893 : 180 : *clause_steps = gen_partprune_steps_internal(context, or_clause);
1894 : :
1895 [ - + ]: 180 : if (context->contradictory)
1896 : 0 : return PARTCLAUSE_MATCH_CONTRADICT; /* shouldn't happen */
1897 [ - + ]: 180 : else if (*clause_steps == NIL)
1898 : 0 : return PARTCLAUSE_UNSUPPORTED; /* step generation failed */
1899 : 180 : return PARTCLAUSE_MATCH_STEPS;
1900 : : }
1901 : :
1902 : 390 : partclause = palloc_object(PartClauseInfo);
1903 : 390 : partclause->keyno = partkeyidx;
1904 : : /* Do pruning with the Boolean equality operator. */
1905 : 390 : partclause->opno = BooleanEqualOperator;
1906 : 390 : partclause->op_is_ne = false;
1907 : 390 : partclause->expr = expr;
1908 : : /* We know that expr is of Boolean type. */
1909 : 390 : partclause->cmpfn = part_scheme->partsupfunc[partkeyidx].fn_oid;
1910 : 390 : partclause->op_strategy = InvalidStrategy;
1911 : :
1912 : 390 : *pc = partclause;
1913 : :
1914 : 390 : return PARTCLAUSE_MATCH_CLAUSE;
1915 : : }
1916 [ + + ]: 28857 : else if (boolmatchstatus == PARTCLAUSE_MATCH_NULLNESS)
1917 : : {
1918 : : /*
1919 : : * Handle IS UNKNOWN and IS NOT UNKNOWN. These just logically
1920 : : * translate to IS NULL and IS NOT NULL.
1921 : : */
1922 : 80 : *clause_is_not_null = notclause;
1923 : 80 : return PARTCLAUSE_MATCH_NULLNESS;
1924 : : }
1925 [ + + + - ]: 53066 : else if (IsA(clause, OpExpr) &&
1926 : 24289 : list_length(((OpExpr *) clause)->args) == 2)
1927 : : {
1928 : 24289 : OpExpr *opclause = (OpExpr *) clause;
1929 : : Expr *leftop,
1930 : : *rightop;
1931 : : Oid opno,
1932 : : op_lefttype,
1933 : : op_righttype,
1934 : 24289 : negator = InvalidOid;
1935 : : Oid cmpfn;
1936 : : int op_strategy;
1937 : 24289 : bool is_opne_listp = false;
1938 : : PartClauseInfo *partclause;
1939 : :
1940 : 24289 : leftop = (Expr *) get_leftop(clause);
1941 : 24289 : leftop = (Expr *) strip_noop_phvs((Node *) leftop);
1942 [ + + ]: 24629 : while (IsA(leftop, RelabelType))
1943 : 340 : leftop = ((RelabelType *) leftop)->arg;
1944 : 24289 : rightop = (Expr *) get_rightop(clause);
1945 : 24289 : rightop = (Expr *) strip_noop_phvs((Node *) rightop);
1946 [ - + ]: 24289 : while (IsA(rightop, RelabelType))
1947 : 0 : rightop = ((RelabelType *) rightop)->arg;
1948 : 24289 : opno = opclause->opno;
1949 : :
1950 : : /* check if the clause matches this partition key */
1951 [ + + ]: 24289 : if (equal(leftop, partkey))
1952 : 14937 : expr = rightop;
1953 [ + + ]: 9352 : else if (equal(rightop, partkey))
1954 : : {
1955 : : /*
1956 : : * It's only useful if we can commute the operator to put the
1957 : : * partkey on the left. If we can't, the clause can be deemed
1958 : : * UNSUPPORTED. Even if its leftop matches some later partkey, we
1959 : : * now know it has Vars on the right, so it's no use.
1960 : : */
1961 : 1073 : opno = get_commutator(opno);
1962 [ - + ]: 1073 : if (!OidIsValid(opno))
1963 : 0 : return PARTCLAUSE_UNSUPPORTED;
1964 : 1073 : expr = leftop;
1965 : : }
1966 : : else
1967 : : /* clause does not match this partition key, but perhaps next. */
1968 : 8279 : return PARTCLAUSE_NOMATCH;
1969 : :
1970 : : /*
1971 : : * Partition key match also requires collation match. There may be
1972 : : * multiple partkeys with the same expression but different
1973 : : * collations, so failure is NOMATCH.
1974 : : */
1975 [ + + + + ]: 16010 : if (!PartCollMatchesExprColl(partcoll, opclause->inputcollid))
1976 : 50 : return PARTCLAUSE_NOMATCH;
1977 : :
1978 : : /*
1979 : : * See if the operator is relevant to the partitioning opfamily.
1980 : : *
1981 : : * Normally we only care about operators that are listed as being part
1982 : : * of the partitioning operator family. But there is one exception:
1983 : : * the not-equals operators are not listed in any operator family
1984 : : * whatsoever, but their negators (equality) are. We can use one of
1985 : : * those if we find it, but only for list partitioning.
1986 : : *
1987 : : * Note: we report NOMATCH on failure if the negator isn't the
1988 : : * equality operator for the partkey's opfamily as other partkeys may
1989 : : * have the same expression but different opfamily. That's unlikely,
1990 : : * but not much more so than duplicate expressions with different
1991 : : * collations.
1992 : : */
1993 [ + + ]: 15960 : if (op_in_opfamily(opno, partopfamily))
1994 : : {
1995 : 15670 : get_op_opfamily_properties(opno, partopfamily, false,
1996 : : &op_strategy, &op_lefttype,
1997 : : &op_righttype);
1998 : : }
1999 : : else
2000 : : {
2001 : : /* not supported for anything apart from LIST partitioned tables */
2002 [ + + ]: 290 : if (part_scheme->strategy != PARTITION_STRATEGY_LIST)
2003 : 80 : return PARTCLAUSE_UNSUPPORTED;
2004 : :
2005 : : /* See if the negator is equality */
2006 : 210 : negator = get_negator(opno);
2007 [ + - + + ]: 210 : if (OidIsValid(negator) && op_in_opfamily(negator, partopfamily))
2008 : : {
2009 : 200 : get_op_opfamily_properties(negator, partopfamily, false,
2010 : : &op_strategy, &op_lefttype,
2011 : : &op_righttype);
2012 [ + - ]: 200 : if (op_strategy == BTEqualStrategyNumber)
2013 : 200 : is_opne_listp = true; /* bingo */
2014 : : }
2015 : :
2016 : : /* Nope, it's not <> either. */
2017 [ + + ]: 210 : if (!is_opne_listp)
2018 : 10 : return PARTCLAUSE_NOMATCH;
2019 : : }
2020 : :
2021 : : /*
2022 : : * Only allow strict operators. This will guarantee nulls are
2023 : : * filtered. (This test is likely useless, since btree and hash
2024 : : * comparison operators are generally strict.)
2025 : : */
2026 [ + + ]: 15870 : if (!op_strict(opno))
2027 : 10 : return PARTCLAUSE_UNSUPPORTED;
2028 : :
2029 : : /*
2030 : : * OK, we have a match to the partition key and a suitable operator.
2031 : : * Examine the other argument to see if it's usable for pruning.
2032 : : *
2033 : : * In most of these cases, we can return UNSUPPORTED because the same
2034 : : * failure would occur no matter which partkey it's matched to. (In
2035 : : * particular, now that we've successfully matched one side of the
2036 : : * opclause to a partkey, there is no chance that matching the other
2037 : : * side to another partkey will produce a usable result, since that'd
2038 : : * mean there are Vars on both sides.)
2039 : : *
2040 : : * Also, if we reject an argument for a target-dependent reason, set
2041 : : * appropriate fields of *context to report that. We postpone these
2042 : : * tests until after matching the partkey and the operator, so as to
2043 : : * reduce the odds of setting the context fields for clauses that do
2044 : : * not end up contributing to pruning steps.
2045 : : *
2046 : : * First, check for non-Const argument. (We assume that any immutable
2047 : : * subexpression will have been folded to a Const already.)
2048 : : */
2049 [ + + ]: 15860 : if (!IsA(expr, Const))
2050 : : {
2051 : : Bitmapset *paramids;
2052 : :
2053 : : /*
2054 : : * When pruning in the planner, we only support pruning using
2055 : : * comparisons to constants. We cannot prune on the basis of
2056 : : * anything that's not immutable. (Note that has_mutable_arg and
2057 : : * has_exec_param do not get set for this target value.)
2058 : : */
2059 [ + + ]: 1776 : if (context->target == PARTTARGET_PLANNER)
2060 : 637 : return PARTCLAUSE_UNSUPPORTED;
2061 : :
2062 : : /*
2063 : : * We can never prune using an expression that contains Vars.
2064 : : */
2065 [ + + ]: 1139 : if (contain_var_clause((Node *) expr))
2066 : 34 : return PARTCLAUSE_UNSUPPORTED;
2067 : :
2068 : : /*
2069 : : * And we must reject anything containing a volatile function.
2070 : : * Stable functions are OK though.
2071 : : */
2072 [ - + ]: 1105 : if (contain_volatile_functions((Node *) expr))
2073 : 0 : return PARTCLAUSE_UNSUPPORTED;
2074 : :
2075 : : /*
2076 : : * See if there are any exec Params. If so, we can only use this
2077 : : * expression during per-scan pruning. The same is true if it
2078 : : * contains a SubPlan, as evaluating that requires the parent
2079 : : * node's PlanState, which startup-time pruning hasn't got. (A
2080 : : * SubPlan's testexpr normally contains exec Params, so we'd have
2081 : : * caught it above, but the testexpr can be folded to a Const.)
2082 : : */
2083 : 1105 : paramids = pull_exec_paramids(expr);
2084 [ + + ]: 1105 : if (!bms_is_empty(paramids))
2085 : : {
2086 : 714 : context->has_exec_param = true;
2087 [ + + ]: 714 : if (context->target != PARTTARGET_EXEC)
2088 : 352 : return PARTCLAUSE_UNSUPPORTED;
2089 : : }
2090 [ + + ]: 391 : else if (contain_subplans((Node *) expr))
2091 : : {
2092 [ + - ]: 5 : if (context->target != PARTTARGET_EXEC)
2093 : 5 : return PARTCLAUSE_UNSUPPORTED;
2094 : : }
2095 : : else
2096 : : {
2097 : : /* It's potentially usable, but mutable */
2098 : 386 : context->has_mutable_arg = true;
2099 : : }
2100 : : }
2101 : :
2102 : : /*
2103 : : * Check whether the comparison operator itself is immutable. (We
2104 : : * assume anything that's in a btree or hash opclass is at least
2105 : : * stable, but we need to check for immutability.)
2106 : : */
2107 [ + + ]: 14832 : if (op_volatile(opno) != PROVOLATILE_IMMUTABLE)
2108 : : {
2109 : 30 : context->has_mutable_op = true;
2110 : :
2111 : : /*
2112 : : * When pruning in the planner, we cannot prune with mutable
2113 : : * operators.
2114 : : */
2115 [ + + ]: 30 : if (context->target == PARTTARGET_PLANNER)
2116 : 5 : return PARTCLAUSE_UNSUPPORTED;
2117 : : }
2118 : :
2119 : : /*
2120 : : * Now find the procedure to use, based on the types. If the clause's
2121 : : * other argument is of the same type as the partitioning opclass's
2122 : : * declared input type, we can use the procedure cached in
2123 : : * PartitionKey. If not, search for a cross-type one in the same
2124 : : * opfamily; if one doesn't exist, report no match.
2125 : : */
2126 [ + + ]: 14827 : if (op_righttype == part_scheme->partopcintype[partkeyidx])
2127 : 14632 : cmpfn = part_scheme->partsupfunc[partkeyidx].fn_oid;
2128 : : else
2129 : : {
2130 [ + - - ]: 195 : switch (part_scheme->strategy)
2131 : : {
2132 : : /*
2133 : : * For range and list partitioning, we need the ordering
2134 : : * procedure with lefttype being the partition key's type,
2135 : : * and righttype the clause's operator's right type.
2136 : : */
2137 : 195 : case PARTITION_STRATEGY_LIST:
2138 : : case PARTITION_STRATEGY_RANGE:
2139 : : cmpfn =
2140 : 195 : get_opfamily_proc(part_scheme->partopfamily[partkeyidx],
2141 : 195 : part_scheme->partopcintype[partkeyidx],
2142 : : op_righttype, BTORDER_PROC);
2143 : 195 : break;
2144 : :
2145 : : /*
2146 : : * For hash partitioning, we need the hashing procedure
2147 : : * for the clause's type.
2148 : : */
2149 : 0 : case PARTITION_STRATEGY_HASH:
2150 : : cmpfn =
2151 : 0 : get_opfamily_proc(part_scheme->partopfamily[partkeyidx],
2152 : : op_righttype, op_righttype,
2153 : : HASHEXTENDED_PROC);
2154 : 0 : break;
2155 : :
2156 : 0 : default:
2157 [ # # ]: 0 : elog(ERROR, "invalid partition strategy: %c",
2158 : : part_scheme->strategy);
2159 : : cmpfn = InvalidOid; /* keep compiler quiet */
2160 : : break;
2161 : : }
2162 : :
2163 [ - + ]: 195 : if (!OidIsValid(cmpfn))
2164 : 0 : return PARTCLAUSE_NOMATCH;
2165 : : }
2166 : :
2167 : : /*
2168 : : * Build the clause, passing the negator if applicable.
2169 : : */
2170 : 14827 : partclause = palloc_object(PartClauseInfo);
2171 : 14827 : partclause->keyno = partkeyidx;
2172 [ + + ]: 14827 : if (is_opne_listp)
2173 : : {
2174 : : Assert(OidIsValid(negator));
2175 : 170 : partclause->opno = negator;
2176 : 170 : partclause->op_is_ne = true;
2177 : 170 : partclause->op_strategy = InvalidStrategy;
2178 : : }
2179 : : else
2180 : : {
2181 : 14657 : partclause->opno = opno;
2182 : 14657 : partclause->op_is_ne = false;
2183 : 14657 : partclause->op_strategy = op_strategy;
2184 : : }
2185 : 14827 : partclause->expr = expr;
2186 : 14827 : partclause->cmpfn = cmpfn;
2187 : :
2188 : 14827 : *pc = partclause;
2189 : :
2190 : 14827 : return PARTCLAUSE_MATCH_CLAUSE;
2191 : : }
2192 [ + + ]: 4488 : else if (IsA(clause, ScalarArrayOpExpr))
2193 : : {
2194 : 636 : ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2195 : 636 : Oid saop_op = saop->opno;
2196 : 636 : Oid saop_coll = saop->inputcollid;
2197 : 636 : Expr *leftop = (Expr *) linitial(saop->args),
2198 : 636 : *rightop = (Expr *) lsecond(saop->args);
2199 : : List *elem_exprs,
2200 : : *elem_clauses;
2201 : : ListCell *lc1;
2202 : :
2203 : 636 : leftop = (Expr *) strip_noop_phvs((Node *) leftop);
2204 [ + + ]: 776 : while (IsA(leftop, RelabelType))
2205 : 140 : leftop = ((RelabelType *) leftop)->arg;
2206 : :
2207 : : /* check if the LHS matches this partition key */
2208 [ + + + + ]: 636 : if (!equal(leftop, partkey) ||
2209 [ - + ]: 180 : !PartCollMatchesExprColl(partcoll, saop->inputcollid))
2210 : 158 : return PARTCLAUSE_NOMATCH;
2211 : :
2212 : : /*
2213 : : * See if the operator is relevant to the partitioning opfamily.
2214 : : *
2215 : : * In case of NOT IN (..), we get a '<>', which we handle if list
2216 : : * partitioning is in use and we're able to confirm that it's negator
2217 : : * is a btree equality operator belonging to the partitioning operator
2218 : : * family. As above, report NOMATCH for non-matching operator.
2219 : : */
2220 [ + + ]: 478 : if (!op_in_opfamily(saop_op, partopfamily))
2221 : : {
2222 : : Oid negator;
2223 : :
2224 [ + + ]: 60 : if (part_scheme->strategy != PARTITION_STRATEGY_LIST)
2225 : 10 : return PARTCLAUSE_NOMATCH;
2226 : :
2227 : 50 : negator = get_negator(saop_op);
2228 [ + - + + ]: 50 : if (OidIsValid(negator) && op_in_opfamily(negator, partopfamily))
2229 : 10 : {
2230 : : int strategy;
2231 : : Oid lefttype,
2232 : : righttype;
2233 : :
2234 : 10 : get_op_opfamily_properties(negator, partopfamily,
2235 : : false, &strategy,
2236 : : &lefttype, &righttype);
2237 [ - + ]: 10 : if (strategy != BTEqualStrategyNumber)
2238 : 0 : return PARTCLAUSE_NOMATCH;
2239 : : }
2240 : : else
2241 : 40 : return PARTCLAUSE_NOMATCH; /* no useful negator */
2242 : : }
2243 : :
2244 : : /*
2245 : : * Only allow strict operators. This will guarantee nulls are
2246 : : * filtered. (This test is likely useless, since btree and hash
2247 : : * comparison operators are generally strict.)
2248 : : */
2249 [ - + ]: 428 : if (!op_strict(saop_op))
2250 : 0 : return PARTCLAUSE_UNSUPPORTED;
2251 : :
2252 : : /*
2253 : : * OK, we have a match to the partition key and a suitable operator.
2254 : : * Examine the array argument to see if it's usable for pruning. This
2255 : : * is identical to the logic for a plain OpExpr.
2256 : : */
2257 [ + + ]: 428 : if (!IsA(rightop, Const))
2258 : : {
2259 : : Bitmapset *paramids;
2260 : :
2261 : : /*
2262 : : * When pruning in the planner, we only support pruning using
2263 : : * comparisons to constants. We cannot prune on the basis of
2264 : : * anything that's not immutable. (Note that has_mutable_arg and
2265 : : * has_exec_param do not get set for this target value.)
2266 : : */
2267 [ + + ]: 91 : if (context->target == PARTTARGET_PLANNER)
2268 : 43 : return PARTCLAUSE_UNSUPPORTED;
2269 : :
2270 : : /*
2271 : : * We can never prune using an expression that contains Vars.
2272 : : */
2273 [ - + ]: 48 : if (contain_var_clause((Node *) rightop))
2274 : 0 : return PARTCLAUSE_UNSUPPORTED;
2275 : :
2276 : : /*
2277 : : * And we must reject anything containing a volatile function.
2278 : : * Stable functions are OK though.
2279 : : */
2280 [ - + ]: 48 : if (contain_volatile_functions((Node *) rightop))
2281 : 0 : return PARTCLAUSE_UNSUPPORTED;
2282 : :
2283 : : /*
2284 : : * See if there are any exec Params. If so, we can only use this
2285 : : * expression during per-scan pruning.
2286 : : */
2287 : 48 : paramids = pull_exec_paramids(rightop);
2288 [ + + ]: 48 : if (!bms_is_empty(paramids))
2289 : : {
2290 : 10 : context->has_exec_param = true;
2291 [ + + ]: 10 : if (context->target != PARTTARGET_EXEC)
2292 : 5 : return PARTCLAUSE_UNSUPPORTED;
2293 : : }
2294 : : else
2295 : : {
2296 : : /* It's potentially usable, but mutable */
2297 : 38 : context->has_mutable_arg = true;
2298 : : }
2299 : : }
2300 : :
2301 : : /*
2302 : : * Check whether the comparison operator itself is immutable. (We
2303 : : * assume anything that's in a btree or hash opclass is at least
2304 : : * stable, but we need to check for immutability.)
2305 : : */
2306 [ + + ]: 380 : if (op_volatile(saop_op) != PROVOLATILE_IMMUTABLE)
2307 : : {
2308 : 30 : context->has_mutable_op = true;
2309 : :
2310 : : /*
2311 : : * When pruning in the planner, we cannot prune with mutable
2312 : : * operators.
2313 : : */
2314 [ + + ]: 30 : if (context->target == PARTTARGET_PLANNER)
2315 : 15 : return PARTCLAUSE_UNSUPPORTED;
2316 : : }
2317 : :
2318 : : /*
2319 : : * Examine the contents of the array argument.
2320 : : */
2321 : 365 : elem_exprs = NIL;
2322 [ + + ]: 365 : if (IsA(rightop, Const))
2323 : : {
2324 : : /*
2325 : : * For a constant array, convert the elements to a list of Const
2326 : : * nodes, one for each array element (excepting nulls).
2327 : : */
2328 : 322 : Const *arr = (Const *) rightop;
2329 : : ArrayType *arrval;
2330 : : int16 elemlen;
2331 : : bool elembyval;
2332 : : char elemalign;
2333 : : Datum *elem_values;
2334 : : bool *elem_nulls;
2335 : : int num_elems,
2336 : : i;
2337 : :
2338 : : /* If the array itself is null, the saop returns null */
2339 [ + + ]: 322 : if (arr->constisnull)
2340 : 20 : return PARTCLAUSE_MATCH_CONTRADICT;
2341 : :
2342 : 307 : arrval = DatumGetArrayTypeP(arr->constvalue);
2343 : 307 : get_typlenbyvalalign(ARR_ELEMTYPE(arrval),
2344 : : &elemlen, &elembyval, &elemalign);
2345 : 307 : deconstruct_array(arrval,
2346 : : ARR_ELEMTYPE(arrval),
2347 : : elemlen, elembyval, elemalign,
2348 : : &elem_values, &elem_nulls,
2349 : : &num_elems);
2350 [ + + ]: 1078 : for (i = 0; i < num_elems; i++)
2351 : : {
2352 : : Const *elem_expr;
2353 : :
2354 : : /*
2355 : : * A null array element must lead to a null comparison result,
2356 : : * since saop_op is known strict. We can ignore it in the
2357 : : * useOr case, but otherwise it implies self-contradiction.
2358 : : */
2359 [ + + ]: 776 : if (elem_nulls[i])
2360 : : {
2361 [ + + ]: 45 : if (saop->useOr)
2362 : 40 : continue;
2363 : 5 : return PARTCLAUSE_MATCH_CONTRADICT;
2364 : : }
2365 : :
2366 : 731 : elem_expr = makeConst(ARR_ELEMTYPE(arrval), -1,
2367 : : arr->constcollid, elemlen,
2368 : 731 : elem_values[i], false, elembyval);
2369 : 731 : elem_exprs = lappend(elem_exprs, elem_expr);
2370 : : }
2371 : : }
2372 [ + + ]: 43 : else if (IsA(rightop, ArrayExpr))
2373 : : {
2374 : 38 : ArrayExpr *arrexpr = castNode(ArrayExpr, rightop);
2375 : :
2376 : : /*
2377 : : * For a nested ArrayExpr, we don't know how to get the actual
2378 : : * scalar values out into a flat list, so we give up doing
2379 : : * anything with this ScalarArrayOpExpr.
2380 : : */
2381 [ - + ]: 38 : if (arrexpr->multidims)
2382 : 0 : return PARTCLAUSE_UNSUPPORTED;
2383 : :
2384 : : /*
2385 : : * Otherwise, we can just use the list of element values.
2386 : : */
2387 : 38 : elem_exprs = arrexpr->elements;
2388 : : }
2389 : : else
2390 : : {
2391 : : /* Give up on any other clause types. */
2392 : 5 : return PARTCLAUSE_UNSUPPORTED;
2393 : : }
2394 : :
2395 : : /*
2396 : : * Now generate a list of clauses, one for each array element, of the
2397 : : * form leftop saop_op elem_expr
2398 : : */
2399 : 340 : elem_clauses = NIL;
2400 [ + - + + : 1150 : foreach(lc1, elem_exprs)
+ + ]
2401 : : {
2402 : : Expr *elem_clause;
2403 : :
2404 : 810 : elem_clause = make_opclause(saop_op, BOOLOID, false,
2405 : 810 : leftop, lfirst(lc1),
2406 : : InvalidOid, saop_coll);
2407 : 810 : elem_clauses = lappend(elem_clauses, elem_clause);
2408 : : }
2409 : :
2410 : : /*
2411 : : * If we have an ANY clause and multiple elements, now turn the list
2412 : : * of clauses into an OR expression.
2413 : : */
2414 [ + + + + ]: 340 : if (saop->useOr && list_length(elem_clauses) > 1)
2415 : 275 : elem_clauses = list_make1(makeBoolExpr(OR_EXPR, elem_clauses, -1));
2416 : :
2417 : : /* Finally, generate steps */
2418 : 340 : *clause_steps = gen_partprune_steps_internal(context, elem_clauses);
2419 [ - + ]: 340 : if (context->contradictory)
2420 : 0 : return PARTCLAUSE_MATCH_CONTRADICT;
2421 [ - + ]: 340 : else if (*clause_steps == NIL)
2422 : 0 : return PARTCLAUSE_UNSUPPORTED; /* step generation failed */
2423 : 340 : return PARTCLAUSE_MATCH_STEPS;
2424 : : }
2425 [ + + ]: 3852 : else if (IsA(clause, NullTest))
2426 : : {
2427 : 3242 : NullTest *nulltest = (NullTest *) clause;
2428 : 3242 : Expr *arg = nulltest->arg;
2429 : :
2430 : 3242 : arg = (Expr *) strip_noop_phvs((Node *) arg);
2431 [ - + ]: 3242 : while (IsA(arg, RelabelType))
2432 : 0 : arg = ((RelabelType *) arg)->arg;
2433 : :
2434 : : /* Does arg match with this partition key column? */
2435 [ + + ]: 3242 : if (!equal(arg, partkey))
2436 : 1432 : return PARTCLAUSE_NOMATCH;
2437 : :
2438 : 1810 : *clause_is_not_null = (nulltest->nulltesttype == IS_NOT_NULL);
2439 : :
2440 : 1810 : return PARTCLAUSE_MATCH_NULLNESS;
2441 : : }
2442 : :
2443 : : /*
2444 : : * If we get here then the return value depends on the result of the
2445 : : * match_boolean_partition_clause call above. If the call returned
2446 : : * PARTCLAUSE_UNSUPPORTED then we're either not dealing with a bool qual
2447 : : * or the bool qual is not suitable for pruning. Since the qual didn't
2448 : : * match up to any of the other qual types supported here, then trying to
2449 : : * match it against any other partition key is a waste of time, so just
2450 : : * return PARTCLAUSE_UNSUPPORTED. If the qual just couldn't be matched to
2451 : : * this partition key, then it may match another, so return
2452 : : * PARTCLAUSE_NOMATCH. The only other value that
2453 : : * match_boolean_partition_clause can return is PARTCLAUSE_MATCH_CLAUSE,
2454 : : * and since that value was already dealt with above, then we can just
2455 : : * return boolmatchstatus.
2456 : : */
2457 : 610 : return boolmatchstatus;
2458 : : }
2459 : :
2460 : : /*
2461 : : * get_steps_using_prefix
2462 : : * Generate a list of PartitionPruneStepOps based on the given input.
2463 : : *
2464 : : * 'step_lastexpr' and 'step_lastcmpfn' are the Expr and comparison function
2465 : : * belonging to the final partition key that we have a clause for. 'prefix'
2466 : : * is a list of PartClauseInfos for partition key numbers prior to the given
2467 : : * 'step_lastexpr' and 'step_lastcmpfn'. 'prefix' may contain multiple
2468 : : * PartClauseInfos belonging to a single partition key. We will generate a
2469 : : * PartitionPruneStepOp for each combination of the given PartClauseInfos
2470 : : * using, at most, one PartClauseInfo per partition key.
2471 : : *
2472 : : * For LIST and RANGE partitioned tables, callers must ensure that
2473 : : * step_nullkeys is NULL, and that prefix contains at least one clause for
2474 : : * each of the partition keys prior to the key that 'step_lastexpr' and
2475 : : * 'step_lastcmpfn' belong to.
2476 : : *
2477 : : * For HASH partitioned tables, callers must ensure that 'prefix' contains at
2478 : : * least one clause for each of the partition keys apart from the final key
2479 : : * (the expr and comparison function for the final key are in 'step_lastexpr'
2480 : : * and 'step_lastcmpfn'). A bit set in step_nullkeys can substitute clauses
2481 : : * in the 'prefix' list for any given key. If a bit is set in 'step_nullkeys'
2482 : : * for a given key, then there must be no PartClauseInfo for that key in the
2483 : : * 'prefix' list.
2484 : : *
2485 : : * For each of the above cases, callers must ensure that PartClauseInfos in
2486 : : * 'prefix' are sorted in ascending order of keyno.
2487 : : */
2488 : : static List *
2489 : 14437 : get_steps_using_prefix(GeneratePruningStepsContext *context,
2490 : : StrategyNumber step_opstrategy,
2491 : : bool step_op_is_ne,
2492 : : Expr *step_lastexpr,
2493 : : Oid step_lastcmpfn,
2494 : : Bitmapset *step_nullkeys,
2495 : : List *prefix)
2496 : : {
2497 : : /* step_nullkeys must be empty for RANGE and LIST partitioned tables */
2498 : : Assert(step_nullkeys == NULL ||
2499 : : context->rel->part_scheme->strategy == PARTITION_STRATEGY_HASH);
2500 : :
2501 : : /*
2502 : : * No recursive processing is required when 'prefix' is an empty list.
2503 : : * This occurs when there is only 1 partition key column.
2504 : : */
2505 [ + + ]: 14437 : if (prefix == NIL)
2506 : : {
2507 : : PartitionPruneStep *step;
2508 : :
2509 : 13502 : step = gen_prune_step_op(context,
2510 : : step_opstrategy,
2511 : : step_op_is_ne,
2512 : : list_make1(step_lastexpr),
2513 : : list_make1_oid(step_lastcmpfn),
2514 : : step_nullkeys);
2515 : 13502 : return list_make1(step);
2516 : : }
2517 : :
2518 : : /* Recurse to generate steps for every combination of clauses. */
2519 : 935 : return get_steps_using_prefix_recurse(context,
2520 : : step_opstrategy,
2521 : : step_op_is_ne,
2522 : : step_lastexpr,
2523 : : step_lastcmpfn,
2524 : : step_nullkeys,
2525 : : prefix,
2526 : : list_head(prefix),
2527 : : NIL, NIL);
2528 : : }
2529 : :
2530 : : /*
2531 : : * get_steps_using_prefix_recurse
2532 : : * Generate and return a list of PartitionPruneStepOps using the 'prefix'
2533 : : * list of PartClauseInfos starting at the 'start' cell.
2534 : : *
2535 : : * When 'prefix' contains multiple PartClauseInfos for a single partition key
2536 : : * we create a PartitionPruneStepOp for each combination of duplicated
2537 : : * PartClauseInfos. The returned list will contain a PartitionPruneStepOp
2538 : : * for each unique combination of input PartClauseInfos containing at most one
2539 : : * PartClauseInfo per partition key.
2540 : : *
2541 : : * 'prefix' is the input list of PartClauseInfos sorted by keyno.
2542 : : * 'start' marks the cell that searching the 'prefix' list should start from.
2543 : : * 'step_exprs' and 'step_cmpfns' each contains the expressions and cmpfns
2544 : : * we've generated so far from the clauses for the previous part keys.
2545 : : */
2546 : : static List *
2547 : 1245 : get_steps_using_prefix_recurse(GeneratePruningStepsContext *context,
2548 : : StrategyNumber step_opstrategy,
2549 : : bool step_op_is_ne,
2550 : : Expr *step_lastexpr,
2551 : : Oid step_lastcmpfn,
2552 : : Bitmapset *step_nullkeys,
2553 : : List *prefix,
2554 : : ListCell *start,
2555 : : List *step_exprs,
2556 : : List *step_cmpfns)
2557 : : {
2558 : 1245 : List *result = NIL;
2559 : : ListCell *lc;
2560 : : int cur_keyno;
2561 : : int final_keyno;
2562 : :
2563 : : /* Actually, recursion would be limited by PARTITION_MAX_KEYS. */
2564 : 1245 : check_stack_depth();
2565 : :
2566 [ - + ]: 1245 : CHECK_FOR_INTERRUPTS();
2567 : :
2568 : : Assert(start != NULL);
2569 : 1245 : cur_keyno = ((PartClauseInfo *) lfirst(start))->keyno;
2570 : 1245 : final_keyno = ((PartClauseInfo *) llast(prefix))->keyno;
2571 : :
2572 : : /* Check if we need to recurse. */
2573 [ + + ]: 1245 : if (cur_keyno < final_keyno)
2574 : : {
2575 : : PartClauseInfo *pc;
2576 : : ListCell *next_start;
2577 : :
2578 : : /*
2579 : : * Find the first PartClauseInfo belonging to the next partition key,
2580 : : * the next recursive call must start iteration of the prefix list
2581 : : * from that point.
2582 : : */
2583 [ + - + - : 600 : for_each_cell(lc, prefix, start)
+ - ]
2584 : : {
2585 : 600 : pc = lfirst(lc);
2586 : :
2587 [ + + ]: 600 : if (pc->keyno > cur_keyno)
2588 : 290 : break;
2589 : : }
2590 : :
2591 : : /* record where to start iterating in the next recursive call */
2592 : 290 : next_start = lc;
2593 : :
2594 : : /*
2595 : : * For each PartClauseInfo with keyno set to cur_keyno, add its expr
2596 : : * and cmpfn to step_exprs and step_cmpfns, respectively, and recurse
2597 : : * using 'next_start' as the starting point in the 'prefix' list.
2598 : : */
2599 [ + - + - : 600 : for_each_cell(lc, prefix, start)
+ - ]
2600 : : {
2601 : : List *moresteps;
2602 : : List *step_exprs1,
2603 : : *step_cmpfns1;
2604 : :
2605 : 600 : pc = lfirst(lc);
2606 [ + + ]: 600 : if (pc->keyno == cur_keyno)
2607 : : {
2608 : : /* Leave the original step_exprs unmodified. */
2609 : 310 : step_exprs1 = list_copy(step_exprs);
2610 : 310 : step_exprs1 = lappend(step_exprs1, pc->expr);
2611 : :
2612 : : /* Leave the original step_cmpfns unmodified. */
2613 : 310 : step_cmpfns1 = list_copy(step_cmpfns);
2614 : 310 : step_cmpfns1 = lappend_oid(step_cmpfns1, pc->cmpfn);
2615 : : }
2616 : : else
2617 : : {
2618 : : /* check the 'prefix' list is sorted correctly */
2619 : : Assert(pc->keyno > cur_keyno);
2620 : 290 : break;
2621 : : }
2622 : :
2623 : 310 : moresteps = get_steps_using_prefix_recurse(context,
2624 : : step_opstrategy,
2625 : : step_op_is_ne,
2626 : : step_lastexpr,
2627 : : step_lastcmpfn,
2628 : : step_nullkeys,
2629 : : prefix,
2630 : : next_start,
2631 : : step_exprs1,
2632 : : step_cmpfns1);
2633 : 310 : result = list_concat(result, moresteps);
2634 : :
2635 : 310 : list_free(step_exprs1);
2636 : 310 : list_free(step_cmpfns1);
2637 : : }
2638 : : }
2639 : : else
2640 : : {
2641 : : /*
2642 : : * End the current recursion cycle and start generating steps, one for
2643 : : * each clause with cur_keyno, which is all clauses from here onward
2644 : : * till the end of the list. Note that for hash partitioning,
2645 : : * step_nullkeys is allowed to be non-empty, in which case step_exprs
2646 : : * would only contain expressions for the partition keys that are not
2647 : : * specified in step_nullkeys.
2648 : : */
2649 : : Assert(list_length(step_exprs) == cur_keyno ||
2650 : : !bms_is_empty(step_nullkeys));
2651 : :
2652 : : /*
2653 : : * Note also that for hash partitioning, each partition key should
2654 : : * have either equality clauses or an IS NULL clause, so if a
2655 : : * partition key doesn't have an expression, it would be specified in
2656 : : * step_nullkeys.
2657 : : */
2658 : : Assert(context->rel->part_scheme->strategy
2659 : : != PARTITION_STRATEGY_HASH ||
2660 : : list_length(step_exprs) + 2 + bms_num_members(step_nullkeys) ==
2661 : : context->rel->part_scheme->partnatts);
2662 [ + - + + : 1920 : for_each_cell(lc, prefix, start)
+ + ]
2663 : : {
2664 : 965 : PartClauseInfo *pc = lfirst(lc);
2665 : : PartitionPruneStep *step;
2666 : : List *step_exprs1,
2667 : : *step_cmpfns1;
2668 : :
2669 : : Assert(pc->keyno == cur_keyno);
2670 : :
2671 : : /* Leave the original step_exprs unmodified. */
2672 : 965 : step_exprs1 = list_copy(step_exprs);
2673 : 965 : step_exprs1 = lappend(step_exprs1, pc->expr);
2674 : 965 : step_exprs1 = lappend(step_exprs1, step_lastexpr);
2675 : :
2676 : : /* Leave the original step_cmpfns unmodified. */
2677 : 965 : step_cmpfns1 = list_copy(step_cmpfns);
2678 : 965 : step_cmpfns1 = lappend_oid(step_cmpfns1, pc->cmpfn);
2679 : 965 : step_cmpfns1 = lappend_oid(step_cmpfns1, step_lastcmpfn);
2680 : :
2681 : 965 : step = gen_prune_step_op(context,
2682 : : step_opstrategy, step_op_is_ne,
2683 : : step_exprs1, step_cmpfns1,
2684 : : step_nullkeys);
2685 : 965 : result = lappend(result, step);
2686 : : }
2687 : : }
2688 : :
2689 : 1245 : return result;
2690 : : }
2691 : :
2692 : : /*
2693 : : * get_matching_hash_bounds
2694 : : * Determine offset of the hash bound matching the specified values,
2695 : : * considering that all the non-null values come from clauses containing
2696 : : * a compatible hash equality operator and any keys that are null come
2697 : : * from an IS NULL clause.
2698 : : *
2699 : : * Generally this function will return a single matching bound offset,
2700 : : * although if a partition has not been setup for a given modulus then we may
2701 : : * return no matches. If the number of clauses found don't cover the entire
2702 : : * partition key, then we'll need to return all offsets.
2703 : : *
2704 : : * 'opstrategy' if non-zero must be HTEqualStrategyNumber.
2705 : : *
2706 : : * 'values' contains Datums indexed by the partition key to use for pruning.
2707 : : *
2708 : : * 'nvalues', the number of Datums in the 'values' array.
2709 : : *
2710 : : * 'partsupfunc' contains partition hashing functions that can produce correct
2711 : : * hash for the type of the values contained in 'values'.
2712 : : *
2713 : : * 'nullkeys' is the set of partition keys that are null.
2714 : : */
2715 : : static PruneStepResult *
2716 : 261 : get_matching_hash_bounds(PartitionPruneContext *context,
2717 : : StrategyNumber opstrategy, const Datum *values, int nvalues,
2718 : : FmgrInfo *partsupfunc, Bitmapset *nullkeys)
2719 : : {
2720 : 261 : PruneStepResult *result = palloc0_object(PruneStepResult);
2721 : 261 : PartitionBoundInfo boundinfo = context->boundinfo;
2722 : 261 : int *partindices = boundinfo->indexes;
2723 : 261 : int partnatts = context->partnatts;
2724 : : bool isnull[PARTITION_MAX_KEYS];
2725 : : int i;
2726 : : uint64 rowHash;
2727 : : int greatest_modulus;
2728 : 261 : Oid *partcollation = context->partcollation;
2729 : :
2730 : : Assert(context->strategy == PARTITION_STRATEGY_HASH);
2731 : :
2732 : : /*
2733 : : * For hash partitioning we can only perform pruning based on equality
2734 : : * clauses to the partition key or IS NULL clauses. We also can only
2735 : : * prune if we got values for all keys.
2736 : : */
2737 [ + - ]: 261 : if (nvalues + bms_num_members(nullkeys) == partnatts)
2738 : : {
2739 : : /*
2740 : : * If there are any values, they must have come from clauses
2741 : : * containing an equality operator compatible with hash partitioning.
2742 : : */
2743 : : Assert(opstrategy == HTEqualStrategyNumber || nvalues == 0);
2744 : :
2745 [ + + ]: 1066 : for (i = 0; i < partnatts; i++)
2746 : 805 : isnull[i] = bms_is_member(i, nullkeys);
2747 : :
2748 : 261 : rowHash = compute_partition_hash_value(partnatts, partsupfunc, partcollation,
2749 : : values, isnull);
2750 : :
2751 : 261 : greatest_modulus = boundinfo->nindexes;
2752 [ + + ]: 261 : if (partindices[rowHash % greatest_modulus] >= 0)
2753 : 256 : result->bound_offsets =
2754 : 256 : bms_make_singleton(rowHash % greatest_modulus);
2755 : : }
2756 : : else
2757 : : {
2758 : : /* Report all valid offsets into the boundinfo->indexes array. */
2759 : 0 : result->bound_offsets = bms_add_range(NULL, 0,
2760 : 0 : boundinfo->nindexes - 1);
2761 : : }
2762 : :
2763 : : /*
2764 : : * There is neither a special hash null partition or the default hash
2765 : : * partition.
2766 : : */
2767 : 261 : result->scan_null = result->scan_default = false;
2768 : :
2769 : 261 : return result;
2770 : : }
2771 : :
2772 : : /*
2773 : : * get_matching_list_bounds
2774 : : * Determine the offsets of list bounds matching the specified value,
2775 : : * according to the semantics of the given operator strategy
2776 : : *
2777 : : * scan_default will be set in the returned struct, if the default partition
2778 : : * needs to be scanned, provided one exists at all. scan_null will be set if
2779 : : * the special null-accepting partition needs to be scanned.
2780 : : *
2781 : : * 'opstrategy' if non-zero must be a btree strategy number.
2782 : : *
2783 : : * 'value' contains the value to use for pruning.
2784 : : *
2785 : : * 'nvalues', if non-zero, should be exactly 1, because of list partitioning.
2786 : : *
2787 : : * 'partsupfunc' contains the list partitioning comparison function to be used
2788 : : * to perform partition_list_bsearch
2789 : : *
2790 : : * 'nullkeys' is the set of partition keys that are null.
2791 : : */
2792 : : static PruneStepResult *
2793 : 5312 : get_matching_list_bounds(PartitionPruneContext *context,
2794 : : StrategyNumber opstrategy, Datum value, int nvalues,
2795 : : FmgrInfo *partsupfunc, Bitmapset *nullkeys)
2796 : : {
2797 : 5312 : PruneStepResult *result = palloc0_object(PruneStepResult);
2798 : 5312 : PartitionBoundInfo boundinfo = context->boundinfo;
2799 : : int off,
2800 : : minoff,
2801 : : maxoff;
2802 : : bool is_equal;
2803 : 5312 : bool inclusive = false;
2804 : 5312 : Oid *partcollation = context->partcollation;
2805 : :
2806 : : Assert(context->strategy == PARTITION_STRATEGY_LIST);
2807 : : Assert(context->partnatts == 1);
2808 : :
2809 : 5312 : result->scan_null = result->scan_default = false;
2810 : :
2811 [ + + ]: 5312 : if (!bms_is_empty(nullkeys))
2812 : : {
2813 : : /*
2814 : : * Nulls may exist in only one partition - the partition whose
2815 : : * accepted set of values includes null or the default partition if
2816 : : * the former doesn't exist.
2817 : : */
2818 [ + + ]: 275 : if (partition_bound_accepts_nulls(boundinfo))
2819 : 195 : result->scan_null = true;
2820 : : else
2821 : 80 : result->scan_default = partition_bound_has_default(boundinfo);
2822 : 275 : return result;
2823 : : }
2824 : :
2825 : : /*
2826 : : * If there are no datums to compare keys with, but there are partitions,
2827 : : * just return the default partition if one exists.
2828 : : */
2829 [ + + ]: 5037 : if (boundinfo->ndatums == 0)
2830 : : {
2831 : 5 : result->scan_default = partition_bound_has_default(boundinfo);
2832 : 5 : return result;
2833 : : }
2834 : :
2835 : 5032 : minoff = 0;
2836 : 5032 : maxoff = boundinfo->ndatums - 1;
2837 : :
2838 : : /*
2839 : : * If there are no values to compare with the datums in boundinfo, it
2840 : : * means the caller asked for partitions for all non-null datums. Add
2841 : : * indexes of *all* partitions, including the default if any.
2842 : : */
2843 [ + + ]: 5032 : if (nvalues == 0)
2844 : : {
2845 : : Assert(boundinfo->ndatums > 0);
2846 : 100 : result->bound_offsets = bms_add_range(NULL, 0,
2847 : 50 : boundinfo->ndatums - 1);
2848 : 50 : result->scan_default = partition_bound_has_default(boundinfo);
2849 : 50 : return result;
2850 : : }
2851 : :
2852 : : /* Special case handling of values coming from a <> operator clause. */
2853 [ + + ]: 4982 : if (opstrategy == InvalidStrategy)
2854 : : {
2855 : : /*
2856 : : * First match to all bounds. We'll remove any matching datums below.
2857 : : */
2858 : : Assert(boundinfo->ndatums > 0);
2859 : 224 : result->bound_offsets = bms_add_range(NULL, 0,
2860 : 112 : boundinfo->ndatums - 1);
2861 : :
2862 : 112 : off = partition_list_bsearch(partsupfunc, partcollation, boundinfo,
2863 : : value, &is_equal);
2864 [ + + + + ]: 112 : if (off >= 0 && is_equal)
2865 : : {
2866 : :
2867 : : /* We have a match. Remove from the result. */
2868 : : Assert(boundinfo->indexes[off] >= 0);
2869 : 87 : result->bound_offsets = bms_del_member(result->bound_offsets,
2870 : : off);
2871 : : }
2872 : :
2873 : : /* Always include the default partition if any. */
2874 : 112 : result->scan_default = partition_bound_has_default(boundinfo);
2875 : :
2876 : 112 : return result;
2877 : : }
2878 : :
2879 : : /*
2880 : : * With range queries, always include the default list partition, because
2881 : : * list partitions divide the key space in a discontinuous manner, not all
2882 : : * values in the given range will have a partition assigned. This may not
2883 : : * technically be true for some data types (e.g. integer types), however,
2884 : : * we currently lack any sort of infrastructure to provide us with proofs
2885 : : * that would allow us to do anything smarter here.
2886 : : */
2887 [ + + ]: 4870 : if (opstrategy != BTEqualStrategyNumber)
2888 : 786 : result->scan_default = partition_bound_has_default(boundinfo);
2889 : :
2890 [ + + + + : 4870 : switch (opstrategy)
+ - ]
2891 : : {
2892 : 4084 : case BTEqualStrategyNumber:
2893 : 4084 : off = partition_list_bsearch(partsupfunc,
2894 : : partcollation,
2895 : : boundinfo, value,
2896 : : &is_equal);
2897 [ + + + + ]: 4084 : if (off >= 0 && is_equal)
2898 : : {
2899 : : Assert(boundinfo->indexes[off] >= 0);
2900 : 1850 : result->bound_offsets = bms_make_singleton(off);
2901 : : }
2902 : : else
2903 : 2234 : result->scan_default = partition_bound_has_default(boundinfo);
2904 : 4084 : return result;
2905 : :
2906 : 361 : case BTGreaterEqualStrategyNumber:
2907 : 361 : inclusive = true;
2908 : : pg_fallthrough;
2909 : 400 : case BTGreaterStrategyNumber:
2910 : 400 : off = partition_list_bsearch(partsupfunc,
2911 : : partcollation,
2912 : : boundinfo, value,
2913 : : &is_equal);
2914 [ + + ]: 400 : if (off >= 0)
2915 : : {
2916 : : /* We don't want the matched datum to be in the result. */
2917 [ + + + + ]: 315 : if (!is_equal || !inclusive)
2918 : 114 : off++;
2919 : : }
2920 : : else
2921 : : {
2922 : : /*
2923 : : * This case means all partition bounds are greater, which in
2924 : : * turn means that all partitions satisfy this key.
2925 : : */
2926 : 85 : off = 0;
2927 : : }
2928 : :
2929 : : /*
2930 : : * off is greater than the numbers of datums we have partitions
2931 : : * for. The only possible partition that could contain a match is
2932 : : * the default partition, but we must've set context->scan_default
2933 : : * above anyway if one exists.
2934 : : */
2935 [ + + ]: 400 : if (off > boundinfo->ndatums - 1)
2936 : 5 : return result;
2937 : :
2938 : 395 : minoff = off;
2939 : 395 : break;
2940 : :
2941 : 80 : case BTLessEqualStrategyNumber:
2942 : 80 : inclusive = true;
2943 : : pg_fallthrough;
2944 : 386 : case BTLessStrategyNumber:
2945 : 386 : off = partition_list_bsearch(partsupfunc,
2946 : : partcollation,
2947 : : boundinfo, value,
2948 : : &is_equal);
2949 [ + - + + : 386 : if (off >= 0 && is_equal && !inclusive)
+ + ]
2950 : 37 : off--;
2951 : :
2952 : : /*
2953 : : * off is smaller than the datums of all non-default partitions.
2954 : : * The only possible partition that could contain a match is the
2955 : : * default partition, but we must've set context->scan_default
2956 : : * above anyway if one exists.
2957 : : */
2958 [ + + ]: 386 : if (off < 0)
2959 : 5 : return result;
2960 : :
2961 : 381 : maxoff = off;
2962 : 381 : break;
2963 : :
2964 : 0 : default:
2965 [ # # ]: 0 : elog(ERROR, "invalid strategy number %d", opstrategy);
2966 : : break;
2967 : : }
2968 : :
2969 : : Assert(minoff >= 0 && maxoff >= 0);
2970 : 776 : result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
2971 : 776 : return result;
2972 : : }
2973 : :
2974 : :
2975 : : /*
2976 : : * get_matching_range_bounds
2977 : : * Determine the offsets of range bounds matching the specified values,
2978 : : * according to the semantics of the given operator strategy
2979 : : *
2980 : : * Each datum whose offset is in result is to be treated as the upper bound of
2981 : : * the partition that will contain the desired values.
2982 : : *
2983 : : * scan_default is set in the returned struct if a default partition exists
2984 : : * and we're absolutely certain that it needs to be scanned. We do *not* set
2985 : : * it just because values match portions of the key space uncovered by
2986 : : * partitions other than default (space which we normally assume to belong to
2987 : : * the default partition): the final set of bounds obtained after combining
2988 : : * multiple pruning steps might exclude it, so we infer its inclusion
2989 : : * elsewhere.
2990 : : *
2991 : : * 'opstrategy' must be a btree strategy number.
2992 : : *
2993 : : * 'values' contains Datums indexed by the partition key to use for pruning.
2994 : : *
2995 : : * 'nvalues', number of Datums in 'values' array. Must be <= context->partnatts.
2996 : : *
2997 : : * 'partsupfunc' contains the range partitioning comparison functions to be
2998 : : * used to perform partition_range_datum_bsearch or partition_rbound_datum_cmp
2999 : : * using.
3000 : : *
3001 : : * 'nullkeys' is the set of partition keys that are null.
3002 : : */
3003 : : static PruneStepResult *
3004 : 4824 : get_matching_range_bounds(PartitionPruneContext *context,
3005 : : StrategyNumber opstrategy, const Datum *values, int nvalues,
3006 : : FmgrInfo *partsupfunc, Bitmapset *nullkeys)
3007 : : {
3008 : 4824 : PruneStepResult *result = palloc0_object(PruneStepResult);
3009 : 4824 : PartitionBoundInfo boundinfo = context->boundinfo;
3010 : 4824 : Oid *partcollation = context->partcollation;
3011 : 4824 : int partnatts = context->partnatts;
3012 : 4824 : int *partindices = boundinfo->indexes;
3013 : : int off,
3014 : : minoff,
3015 : : maxoff;
3016 : : bool is_equal;
3017 : 4824 : bool inclusive = false;
3018 : :
3019 : : Assert(context->strategy == PARTITION_STRATEGY_RANGE);
3020 : : Assert(nvalues <= partnatts);
3021 : :
3022 : 4824 : result->scan_null = result->scan_default = false;
3023 : :
3024 : : /*
3025 : : * If there are no datums to compare keys with, or if we got an IS NULL
3026 : : * clause just return the default partition, if it exists.
3027 : : */
3028 [ + + + + ]: 4824 : if (boundinfo->ndatums == 0 || !bms_is_empty(nullkeys))
3029 : : {
3030 : 55 : result->scan_default = partition_bound_has_default(boundinfo);
3031 : 55 : return result;
3032 : : }
3033 : :
3034 : 4769 : minoff = 0;
3035 : 4769 : maxoff = boundinfo->ndatums;
3036 : :
3037 : : /*
3038 : : * If there are no values to compare with the datums in boundinfo, it
3039 : : * means the caller asked for partitions for all non-null datums. Add
3040 : : * indexes of *all* partitions, including the default partition if one
3041 : : * exists.
3042 : : */
3043 [ + + ]: 4769 : if (nvalues == 0)
3044 : : {
3045 : 40 : result->scan_default = partition_bound_has_default(boundinfo);
3046 : : Assert(partindices[minoff] >= -1 &&
3047 : : partindices[maxoff] >= -1);
3048 : 40 : result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
3049 : :
3050 : 40 : return result;
3051 : : }
3052 : :
3053 : : /*
3054 : : * If the query does not constrain all key columns, we'll need to scan the
3055 : : * default partition, if any.
3056 : : */
3057 [ + + ]: 4729 : if (nvalues < partnatts)
3058 : 654 : result->scan_default = partition_bound_has_default(boundinfo);
3059 : :
3060 [ + + + + : 4729 : switch (opstrategy)
+ - ]
3061 : : {
3062 : 3369 : case BTEqualStrategyNumber:
3063 : : /* Look for the smallest bound that is = lookup value. */
3064 : 3369 : off = partition_range_datum_bsearch(partsupfunc,
3065 : : partcollation,
3066 : : boundinfo,
3067 : : nvalues, values,
3068 : : &is_equal);
3069 : :
3070 [ + + + + ]: 3369 : if (off >= 0 && is_equal)
3071 : : {
3072 [ + + ]: 912 : if (nvalues == partnatts)
3073 : : {
3074 : : /* There can only be zero or one matching partition. */
3075 : 539 : result->bound_offsets = bms_make_singleton(off + 1);
3076 : 539 : return result;
3077 : : }
3078 : : else
3079 : : {
3080 : 373 : int saved_off = off;
3081 : :
3082 : : /*
3083 : : * Since the lookup value contains only a prefix of keys,
3084 : : * we must find other bounds that may also match the
3085 : : * prefix. partition_range_datum_bsearch() returns the
3086 : : * offset of one of them, find others by checking adjacent
3087 : : * bounds.
3088 : : */
3089 : :
3090 : : /*
3091 : : * First find greatest bound that's smaller than the
3092 : : * lookup value.
3093 : : */
3094 [ + + ]: 578 : while (off >= 1)
3095 : : {
3096 : : int32 cmpval;
3097 : :
3098 : : cmpval =
3099 : 498 : partition_rbound_datum_cmp(partsupfunc,
3100 : : partcollation,
3101 : 498 : boundinfo->datums[off - 1],
3102 : 498 : boundinfo->kind[off - 1],
3103 : : values, nvalues);
3104 [ + + ]: 498 : if (cmpval != 0)
3105 : 293 : break;
3106 : 205 : off--;
3107 : : }
3108 : :
3109 : : Assert(0 ==
3110 : : partition_rbound_datum_cmp(partsupfunc,
3111 : : partcollation,
3112 : : boundinfo->datums[off],
3113 : : boundinfo->kind[off],
3114 : : values, nvalues));
3115 : :
3116 : : /*
3117 : : * We can treat 'off' as the offset of the smallest bound
3118 : : * to be included in the result, if we know it is the
3119 : : * upper bound of the partition in which the lookup value
3120 : : * could possibly exist. One case it couldn't is if the
3121 : : * bound, or precisely the matched portion of its prefix,
3122 : : * is not inclusive.
3123 : : */
3124 [ + + ]: 373 : if (boundinfo->kind[off][nvalues] ==
3125 : : PARTITION_RANGE_DATUM_MINVALUE)
3126 : 30 : off++;
3127 : :
3128 : 373 : minoff = off;
3129 : :
3130 : : /*
3131 : : * Now find smallest bound that's greater than the lookup
3132 : : * value.
3133 : : */
3134 : 373 : off = saved_off;
3135 [ + + ]: 608 : while (off < boundinfo->ndatums - 1)
3136 : : {
3137 : : int32 cmpval;
3138 : :
3139 : 558 : cmpval = partition_rbound_datum_cmp(partsupfunc,
3140 : : partcollation,
3141 : 558 : boundinfo->datums[off + 1],
3142 : 558 : boundinfo->kind[off + 1],
3143 : : values, nvalues);
3144 [ + + ]: 558 : if (cmpval != 0)
3145 : 323 : break;
3146 : 235 : off++;
3147 : : }
3148 : :
3149 : : Assert(0 ==
3150 : : partition_rbound_datum_cmp(partsupfunc,
3151 : : partcollation,
3152 : : boundinfo->datums[off],
3153 : : boundinfo->kind[off],
3154 : : values, nvalues));
3155 : :
3156 : : /*
3157 : : * off + 1, then would be the offset of the greatest bound
3158 : : * to be included in the result. The exception is the
3159 : : * mirror image of the MINVALUE case above: if the matched
3160 : : * bound is exactly (prefix, MAXVALUE), no row carrying
3161 : : * this prefix can sort above it, so the key space beyond
3162 : : * it need not be considered.
3163 : : */
3164 [ + + ]: 373 : if (boundinfo->kind[off][nvalues] ==
3165 : : PARTITION_RANGE_DATUM_MAXVALUE)
3166 : 40 : maxoff = off;
3167 : : else
3168 : 333 : maxoff = off + 1;
3169 : : }
3170 : :
3171 : : Assert(minoff >= 0 && maxoff >= 0);
3172 : 373 : result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
3173 : : }
3174 : : else
3175 : : {
3176 : : /*
3177 : : * The lookup value falls in the range between some bounds in
3178 : : * boundinfo. 'off' would be the offset of the greatest bound
3179 : : * that is <= lookup value, so add off + 1 to the result
3180 : : * instead as the offset of the upper bound of the only
3181 : : * partition that may contain the lookup value. If 'off' is
3182 : : * -1 indicating that all bounds are greater, then we simply
3183 : : * end up adding the first bound's offset, that is, 0.
3184 : : */
3185 : 2457 : result->bound_offsets = bms_make_singleton(off + 1);
3186 : : }
3187 : :
3188 : 2830 : return result;
3189 : :
3190 : 456 : case BTGreaterEqualStrategyNumber:
3191 : 456 : inclusive = true;
3192 : : pg_fallthrough;
3193 : 721 : case BTGreaterStrategyNumber:
3194 : :
3195 : : /*
3196 : : * Look for the smallest bound that is > or >= lookup value and
3197 : : * set minoff to its offset.
3198 : : */
3199 : 721 : off = partition_range_datum_bsearch(partsupfunc,
3200 : : partcollation,
3201 : : boundinfo,
3202 : : nvalues, values,
3203 : : &is_equal);
3204 [ + + ]: 721 : if (off < 0)
3205 : : {
3206 : : /*
3207 : : * All bounds are greater than the lookup value, so include
3208 : : * all of them in the result.
3209 : : */
3210 : 50 : minoff = 0;
3211 : : }
3212 : : else
3213 : : {
3214 [ + + + + ]: 671 : if (is_equal && nvalues < partnatts)
3215 : : {
3216 : : /*
3217 : : * Since the lookup value contains only a prefix of keys,
3218 : : * we must find other bounds that may also match the
3219 : : * prefix. partition_range_datum_bsearch() returns the
3220 : : * offset of one of them, find others by checking adjacent
3221 : : * bounds.
3222 : : *
3223 : : * Based on whether the lookup values are inclusive or
3224 : : * not, we must either include the indexes of all such
3225 : : * bounds in the result (that is, set minoff to the index
3226 : : * of smallest such bound) or find the smallest one that's
3227 : : * greater than the lookup values and set minoff to that.
3228 : : */
3229 : : while (true)
3230 : 50 : {
3231 : : int32 cmpval;
3232 : : int nextoff;
3233 : :
3234 [ + + ]: 125 : nextoff = inclusive ? off - 1 : off + 1;
3235 : :
3236 [ + + + + ]: 125 : if (nextoff < 0 || nextoff >= boundinfo->ndatums)
3237 : : break;
3238 : : cmpval =
3239 : 100 : partition_rbound_datum_cmp(partsupfunc,
3240 : : partcollation,
3241 : 100 : boundinfo->datums[nextoff],
3242 : 100 : boundinfo->kind[nextoff],
3243 : : values, nvalues);
3244 [ + + ]: 100 : if (cmpval != 0)
3245 : 50 : break;
3246 : :
3247 : 50 : off = nextoff;
3248 : : }
3249 : :
3250 : : Assert(0 ==
3251 : : partition_rbound_datum_cmp(partsupfunc,
3252 : : partcollation,
3253 : : boundinfo->datums[off],
3254 : : boundinfo->kind[off],
3255 : : values, nvalues));
3256 : :
3257 [ + + ]: 75 : minoff = inclusive ? off : off + 1;
3258 : : }
3259 : : else
3260 : : {
3261 : :
3262 : : /*
3263 : : * lookup value falls in the range between some bounds in
3264 : : * boundinfo. off would be the offset of the greatest
3265 : : * bound that is <= lookup value, so add off + 1 to the
3266 : : * result instead as the offset of the upper bound of the
3267 : : * smallest partition that may contain the lookup value.
3268 : : */
3269 : 596 : minoff = off + 1;
3270 : : }
3271 : : }
3272 : 721 : break;
3273 : :
3274 : 97 : case BTLessEqualStrategyNumber:
3275 : 97 : inclusive = true;
3276 : : pg_fallthrough;
3277 : 639 : case BTLessStrategyNumber:
3278 : :
3279 : : /*
3280 : : * Look for the greatest bound that is < or <= lookup value and
3281 : : * set maxoff to its offset.
3282 : : */
3283 : 639 : off = partition_range_datum_bsearch(partsupfunc,
3284 : : partcollation,
3285 : : boundinfo,
3286 : : nvalues, values,
3287 : : &is_equal);
3288 [ + + ]: 639 : if (off >= 0)
3289 : : {
3290 : : /*
3291 : : * As above, check adjacent bounds to see if the bound is
3292 : : * equal to the lookup value.
3293 : : */
3294 [ + + + + ]: 634 : if (is_equal && nvalues < partnatts)
3295 : : {
3296 : : while (true)
3297 : 25 : {
3298 : : int32 cmpval;
3299 : : int nextoff;
3300 : :
3301 [ + + ]: 123 : nextoff = inclusive ? off + 1 : off - 1;
3302 : :
3303 [ + - + - ]: 123 : if (nextoff < 0 || nextoff >= boundinfo->ndatums)
3304 : : break;
3305 : :
3306 : 123 : cmpval = partition_rbound_datum_cmp(partsupfunc,
3307 : : partcollation,
3308 : 123 : boundinfo->datums[nextoff],
3309 : 123 : boundinfo->kind[nextoff],
3310 : : values, nvalues);
3311 [ + + ]: 123 : if (cmpval != 0)
3312 : 98 : break;
3313 : :
3314 : 25 : off = nextoff;
3315 : : }
3316 : :
3317 : : Assert(0 ==
3318 : : partition_rbound_datum_cmp(partsupfunc,
3319 : : partcollation,
3320 : : boundinfo->datums[off],
3321 : : boundinfo->kind[off],
3322 : : values, nvalues));
3323 : :
3324 : 98 : maxoff = inclusive ? off + 1 : off;
3325 : : }
3326 : :
3327 : : /*
3328 : : * The lookup value falls in the range between some bounds in
3329 : : * boundinfo. 'off' would be the offset of the greatest bound
3330 : : * that is <= lookup value, so add off + 1 to the result
3331 : : * instead as the offset of the upper bound of the greatest
3332 : : * partition that may contain lookup value. If the lookup
3333 : : * value had exactly matched the bound, but it isn't
3334 : : * inclusive, no need add the adjacent partition.
3335 : : */
3336 [ + + + + ]: 536 : else if (!is_equal || inclusive)
3337 : 391 : maxoff = off + 1;
3338 : : else
3339 : 145 : maxoff = off;
3340 : : }
3341 : : else
3342 : : {
3343 : : /*
3344 : : * 'off' is -1 indicating that all bounds are greater, so just
3345 : : * set the first bound's offset as maxoff.
3346 : : */
3347 : 5 : maxoff = off + 1;
3348 : : }
3349 : 639 : break;
3350 : :
3351 : 0 : default:
3352 [ # # ]: 0 : elog(ERROR, "invalid strategy number %d", opstrategy);
3353 : : break;
3354 : : }
3355 : :
3356 : : Assert(minoff >= 0 && minoff <= boundinfo->ndatums);
3357 : : Assert(maxoff >= 0 && maxoff <= boundinfo->ndatums);
3358 : :
3359 : :
3360 : : /*
3361 : : * Check for cases that we're scanning the DEFAULT partition when no rows
3362 : : * can exist there for the given value. This can happen when the first or
3363 : : * final bound for partitions we're scanning have a MINVALUE or MAXVALUE
3364 : : * clause respectively. We needn't scan the DEFAULT partition for values
3365 : : * beyond the bound since the MINVALUE / MAXVALUE bound handles up to
3366 : : * negative or positive infinity. The BTEqualStrategyNumber code above
3367 : : * handled doing this for subsequent partition keys, so all that's left to
3368 : : * handle here is the same for the first partition key. We needn't
3369 : : * perform this for all steps as the intersected results from the
3370 : : * recursive processing of pruning steps for leading keys means we only
3371 : : * scan the partitions common to all steps in the intersected set of
3372 : : * steps.
3373 : : */
3374 [ + + ]: 1360 : if (nvalues == 1)
3375 : : {
3376 : : /*
3377 : : * Check if we're scanning the default and if a MINVALUE bound covers
3378 : : * the key space for the lower end.
3379 : : */
3380 [ + + + + ]: 1192 : if (minoff < boundinfo->ndatums && partindices[minoff] < 0 &&
3381 [ + + ]: 661 : boundinfo->kind[minoff][0] == PARTITION_RANGE_DATUM_MINVALUE)
3382 : : {
3383 : 105 : minoff++;
3384 : : Assert(boundinfo->indexes[minoff] >= 0);
3385 : : }
3386 : :
3387 : : /*
3388 : : * Likewise, do the same check for the upper range partition if the
3389 : : * prior bound is a MAXVALUE kind.
3390 : : */
3391 [ + - + + ]: 1192 : if (maxoff >= 1 && partindices[maxoff] < 0 &&
3392 [ + + ]: 775 : boundinfo->kind[maxoff - 1][0] == PARTITION_RANGE_DATUM_MAXVALUE)
3393 : : {
3394 : 114 : maxoff--;
3395 : : Assert(boundinfo->indexes[maxoff] >= 0);
3396 : : }
3397 : : }
3398 : : Assert(minoff >= 0 && maxoff >= 0);
3399 [ + - ]: 1360 : if (minoff <= maxoff)
3400 : 1360 : result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
3401 : :
3402 : 1360 : return result;
3403 : : }
3404 : :
3405 : : /*
3406 : : * pull_exec_paramids
3407 : : * Returns a Bitmapset containing the paramids of all Params with
3408 : : * paramkind = PARAM_EXEC in 'expr'.
3409 : : */
3410 : : static Bitmapset *
3411 : 1545 : pull_exec_paramids(Expr *expr)
3412 : : {
3413 : 1545 : Bitmapset *result = NULL;
3414 : :
3415 : 1545 : (void) pull_exec_paramids_walker((Node *) expr, &result);
3416 : :
3417 : 1545 : return result;
3418 : : }
3419 : :
3420 : : static bool
3421 : 1972 : pull_exec_paramids_walker(Node *node, Bitmapset **context)
3422 : : {
3423 [ + + ]: 1972 : if (node == NULL)
3424 : 15 : return false;
3425 [ + + ]: 1957 : if (IsA(node, Param))
3426 : : {
3427 : 1531 : Param *param = (Param *) node;
3428 : :
3429 [ + + ]: 1531 : if (param->paramkind == PARAM_EXEC)
3430 : 1136 : *context = bms_add_member(*context, param->paramid);
3431 : 1531 : return false;
3432 : : }
3433 : 426 : return expression_tree_walker(node, pull_exec_paramids_walker, context);
3434 : : }
3435 : :
3436 : : /*
3437 : : * get_partkey_exec_paramids
3438 : : * Loop through given pruning steps and find out which exec Params
3439 : : * are used.
3440 : : *
3441 : : * Returns a Bitmapset of Param IDs.
3442 : : */
3443 : : static Bitmapset *
3444 : 347 : get_partkey_exec_paramids(List *steps)
3445 : : {
3446 : 347 : Bitmapset *execparamids = NULL;
3447 : : ListCell *lc;
3448 : :
3449 [ + - + + : 792 : foreach(lc, steps)
+ + ]
3450 : : {
3451 : 445 : PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc);
3452 : : ListCell *lc2;
3453 : :
3454 [ + + ]: 445 : if (!IsA(step, PartitionPruneStepOp))
3455 : 44 : continue;
3456 : :
3457 [ + - + + : 842 : foreach(lc2, step->exprs)
+ + ]
3458 : : {
3459 : 441 : Expr *expr = lfirst(lc2);
3460 : :
3461 : : /* We can be quick for plain Consts */
3462 [ + + ]: 441 : if (!IsA(expr, Const))
3463 : 392 : execparamids = bms_join(execparamids,
3464 : : pull_exec_paramids(expr));
3465 : : }
3466 : : }
3467 : :
3468 : 347 : return execparamids;
3469 : : }
3470 : :
3471 : : /*
3472 : : * perform_pruning_base_step
3473 : : * Determines the indexes of datums that satisfy conditions specified in
3474 : : * 'opstep'.
3475 : : *
3476 : : * Result also contains whether special null-accepting and/or default
3477 : : * partition need to be scanned.
3478 : : */
3479 : : static PruneStepResult *
3480 : 10401 : perform_pruning_base_step(PartitionPruneContext *context,
3481 : : PartitionPruneStepOp *opstep)
3482 : : {
3483 : : ListCell *lc1,
3484 : : *lc2;
3485 : : int keyno,
3486 : : nvalues;
3487 : : Datum values[PARTITION_MAX_KEYS];
3488 : : FmgrInfo *partsupfunc;
3489 : : int stateidx;
3490 : :
3491 : : /*
3492 : : * There better be the same number of expressions and compare functions.
3493 : : */
3494 : : Assert(list_length(opstep->exprs) == list_length(opstep->cmpfns));
3495 : :
3496 : 10401 : nvalues = 0;
3497 : 10401 : lc1 = list_head(opstep->exprs);
3498 : 10401 : lc2 = list_head(opstep->cmpfns);
3499 : :
3500 : : /*
3501 : : * Generate the partition lookup key that will be used by one of the
3502 : : * get_matching_*_bounds functions called below.
3503 : : */
3504 [ + + ]: 22444 : for (keyno = 0; keyno < context->partnatts; keyno++)
3505 : : {
3506 : : /*
3507 : : * For hash partitioning, it is possible that values of some keys are
3508 : : * not provided in operator clauses, but instead the planner found
3509 : : * that they appeared in a IS NULL clause.
3510 : : */
3511 [ + + ]: 12353 : if (bms_is_member(keyno, opstep->nullkeys))
3512 : 689 : continue;
3513 : :
3514 : : /*
3515 : : * For range partitioning, we must only perform pruning with values
3516 : : * for either all partition keys or a prefix thereof.
3517 : : */
3518 [ + + + + ]: 11664 : if (keyno > nvalues && context->strategy == PARTITION_STRATEGY_RANGE)
3519 : 306 : break;
3520 : :
3521 [ + + ]: 11358 : if (lc1 != NULL)
3522 : : {
3523 : : Expr *expr;
3524 : : Datum datum;
3525 : : bool isnull;
3526 : : Oid cmpfn;
3527 : :
3528 : 10604 : expr = lfirst(lc1);
3529 : 10604 : stateidx = PruneCxtStateIdx(context->partnatts,
3530 : : opstep->step.step_id, keyno);
3531 : 10604 : partkey_datum_from_expr(context, expr, stateidx,
3532 : : &datum, &isnull);
3533 : :
3534 : : /*
3535 : : * Since we only allow strict operators in pruning steps, any
3536 : : * null-valued comparison value must cause the comparison to fail,
3537 : : * so that no partitions could match.
3538 : : */
3539 [ + + ]: 10604 : if (isnull)
3540 : : {
3541 : : PruneStepResult *result;
3542 : :
3543 : 4 : result = palloc_object(PruneStepResult);
3544 : 4 : result->bound_offsets = NULL;
3545 : 4 : result->scan_default = false;
3546 : 4 : result->scan_null = false;
3547 : :
3548 : 4 : return result;
3549 : : }
3550 : :
3551 : : /* Set up the stepcmpfuncs entry, unless we already did */
3552 : 10600 : cmpfn = lfirst_oid(lc2);
3553 : : Assert(OidIsValid(cmpfn));
3554 [ + + ]: 10600 : if (cmpfn != context->stepcmpfuncs[stateidx].fn_oid)
3555 : : {
3556 : : /*
3557 : : * If the needed support function is the same one cached in
3558 : : * the relation's partition key, copy the cached FmgrInfo.
3559 : : * Otherwise (i.e., when we have a cross-type comparison), an
3560 : : * actual lookup is required.
3561 : : */
3562 [ + + ]: 8491 : if (cmpfn == context->partsupfunc[keyno].fn_oid)
3563 : 8406 : fmgr_info_copy(&context->stepcmpfuncs[stateidx],
3564 : 8406 : &context->partsupfunc[keyno],
3565 : : context->ppccontext);
3566 : : else
3567 : 85 : fmgr_info_cxt(cmpfn, &context->stepcmpfuncs[stateidx],
3568 : : context->ppccontext);
3569 : : }
3570 : :
3571 : 10600 : values[keyno] = datum;
3572 : 10600 : nvalues++;
3573 : :
3574 : 10600 : lc1 = lnext(opstep->exprs, lc1);
3575 : 10600 : lc2 = lnext(opstep->cmpfns, lc2);
3576 : : }
3577 : : }
3578 : :
3579 : : /*
3580 : : * Point partsupfunc to the entry for the 0th key of this step; the
3581 : : * additional support functions, if any, follow consecutively.
3582 : : */
3583 : 10397 : stateidx = PruneCxtStateIdx(context->partnatts, opstep->step.step_id, 0);
3584 : 10397 : partsupfunc = &context->stepcmpfuncs[stateidx];
3585 : :
3586 [ + + + - ]: 10397 : switch (context->strategy)
3587 : : {
3588 : 261 : case PARTITION_STRATEGY_HASH:
3589 : 261 : return get_matching_hash_bounds(context,
3590 : 261 : opstep->opstrategy,
3591 : : values, nvalues,
3592 : : partsupfunc,
3593 : : opstep->nullkeys);
3594 : :
3595 : 5312 : case PARTITION_STRATEGY_LIST:
3596 : 5312 : return get_matching_list_bounds(context,
3597 : 5312 : opstep->opstrategy,
3598 : : values[0], nvalues,
3599 : : &partsupfunc[0],
3600 : : opstep->nullkeys);
3601 : :
3602 : 4824 : case PARTITION_STRATEGY_RANGE:
3603 : 4824 : return get_matching_range_bounds(context,
3604 : 4824 : opstep->opstrategy,
3605 : : values, nvalues,
3606 : : partsupfunc,
3607 : : opstep->nullkeys);
3608 : :
3609 : 0 : default:
3610 [ # # ]: 0 : elog(ERROR, "unexpected partition strategy: %d",
3611 : : (int) context->strategy);
3612 : : break;
3613 : : }
3614 : :
3615 : : return NULL;
3616 : : }
3617 : :
3618 : : /*
3619 : : * perform_pruning_combine_step
3620 : : * Determines the indexes of datums obtained by combining those given
3621 : : * by the steps identified by cstep->source_stepids using the specified
3622 : : * combination method
3623 : : *
3624 : : * Since cstep may refer to the result of earlier steps, we also receive
3625 : : * step_results here.
3626 : : */
3627 : : static PruneStepResult *
3628 : 2180 : perform_pruning_combine_step(PartitionPruneContext *context,
3629 : : PartitionPruneStepCombine *cstep,
3630 : : PruneStepResult **step_results)
3631 : : {
3632 : 2180 : PruneStepResult *result = palloc0_object(PruneStepResult);
3633 : : bool firststep;
3634 : : ListCell *lc1;
3635 : :
3636 : : /*
3637 : : * A combine step without any source steps is an indication to not perform
3638 : : * any partition pruning. Return all datum indexes in that case.
3639 : : */
3640 [ + + ]: 2180 : if (cstep->source_stepids == NIL)
3641 : : {
3642 : 319 : PartitionBoundInfo boundinfo = context->boundinfo;
3643 : :
3644 : 319 : result->bound_offsets =
3645 : 319 : bms_add_range(NULL, 0, boundinfo->nindexes - 1);
3646 : 319 : result->scan_default = partition_bound_has_default(boundinfo);
3647 : 319 : result->scan_null = partition_bound_accepts_nulls(boundinfo);
3648 : 319 : return result;
3649 : : }
3650 : :
3651 [ + + - ]: 1861 : switch (cstep->combineOp)
3652 : : {
3653 : 990 : case PARTPRUNE_COMBINE_UNION:
3654 [ + - + + : 3027 : foreach(lc1, cstep->source_stepids)
+ + ]
3655 : : {
3656 : 2037 : int step_id = lfirst_int(lc1);
3657 : : PruneStepResult *step_result;
3658 : :
3659 : : /*
3660 : : * step_results[step_id] must contain a valid result, which is
3661 : : * confirmed by the fact that cstep's step_id is greater than
3662 : : * step_id and the fact that results of the individual steps
3663 : : * are evaluated in sequence of their step_ids.
3664 : : */
3665 [ - + ]: 2037 : if (step_id >= cstep->step.step_id)
3666 [ # # ]: 0 : elog(ERROR, "invalid pruning combine step argument");
3667 : 2037 : step_result = step_results[step_id];
3668 : : Assert(step_result != NULL);
3669 : :
3670 : : /* Record any additional datum indexes from this step */
3671 : 4074 : result->bound_offsets = bms_add_members(result->bound_offsets,
3672 : 2037 : step_result->bound_offsets);
3673 : :
3674 : : /* Update whether to scan null and default partitions. */
3675 [ + + ]: 2037 : if (!result->scan_null)
3676 : 1952 : result->scan_null = step_result->scan_null;
3677 [ + + ]: 2037 : if (!result->scan_default)
3678 : 1802 : result->scan_default = step_result->scan_default;
3679 : : }
3680 : 990 : break;
3681 : :
3682 : 871 : case PARTPRUNE_COMBINE_INTERSECT:
3683 : 871 : firststep = true;
3684 [ + - + + : 3079 : foreach(lc1, cstep->source_stepids)
+ + ]
3685 : : {
3686 : 2208 : int step_id = lfirst_int(lc1);
3687 : : PruneStepResult *step_result;
3688 : :
3689 [ - + ]: 2208 : if (step_id >= cstep->step.step_id)
3690 [ # # ]: 0 : elog(ERROR, "invalid pruning combine step argument");
3691 : 2208 : step_result = step_results[step_id];
3692 : : Assert(step_result != NULL);
3693 : :
3694 [ + + ]: 2208 : if (firststep)
3695 : : {
3696 : : /* Copy step's result the first time. */
3697 : 871 : result->bound_offsets =
3698 : 871 : bms_copy(step_result->bound_offsets);
3699 : 871 : result->scan_null = step_result->scan_null;
3700 : 871 : result->scan_default = step_result->scan_default;
3701 : 871 : firststep = false;
3702 : : }
3703 : : else
3704 : : {
3705 : : /* Record datum indexes common to both steps */
3706 : 1337 : result->bound_offsets =
3707 : 1337 : bms_int_members(result->bound_offsets,
3708 : 1337 : step_result->bound_offsets);
3709 : :
3710 : : /* Update whether to scan null and default partitions. */
3711 [ + + ]: 1337 : if (result->scan_null)
3712 : 80 : result->scan_null = step_result->scan_null;
3713 [ + + ]: 1337 : if (result->scan_default)
3714 : 660 : result->scan_default = step_result->scan_default;
3715 : : }
3716 : : }
3717 : 871 : break;
3718 : : }
3719 : :
3720 : 1861 : return result;
3721 : : }
3722 : :
3723 : : /*
3724 : : * match_boolean_partition_clause
3725 : : *
3726 : : * If we're able to match the clause to the partition key as specially-shaped
3727 : : * boolean clause, set *outconst to a Const containing a true, false or NULL
3728 : : * value, set *notclause according to if the clause was in the "not" form,
3729 : : * i.e. "IS NOT TRUE", "IS NOT FALSE" or "IS NOT UNKNOWN" and return
3730 : : * PARTCLAUSE_MATCH_CLAUSE for "IS [NOT] (TRUE|FALSE)" clauses and
3731 : : * PARTCLAUSE_MATCH_NULLNESS for "IS [NOT] UNKNOWN" clauses. Otherwise,
3732 : : * return PARTCLAUSE_UNSUPPORTED if the clause cannot be used for partition
3733 : : * pruning, and PARTCLAUSE_NOMATCH for supported clauses that do not match this
3734 : : * 'partkey'.
3735 : : */
3736 : : static PartClauseMatchStatus
3737 : 29427 : match_boolean_partition_clause(Oid partopfamily, Expr *clause, const Expr *partkey,
3738 : : Expr **outconst, bool *notclause)
3739 : : {
3740 : : Expr *leftop;
3741 : :
3742 : 29427 : *outconst = NULL;
3743 : 29427 : *notclause = false;
3744 : :
3745 : : /*
3746 : : * Partitioning currently can only use built-in AMs, so checking for
3747 : : * built-in boolean opfamilies is good enough.
3748 : : */
3749 [ + + + - ]: 29427 : if (!IsBuiltinBooleanOpfamily(partopfamily))
3750 : 28197 : return PARTCLAUSE_UNSUPPORTED;
3751 : :
3752 [ + + ]: 1230 : if (IsA(clause, BooleanTest))
3753 : : {
3754 : 670 : BooleanTest *btest = (BooleanTest *) clause;
3755 : :
3756 : 670 : leftop = btest->arg;
3757 : 670 : leftop = (Expr *) strip_noop_phvs((Node *) leftop);
3758 [ - + ]: 670 : while (IsA(leftop, RelabelType))
3759 : 0 : leftop = ((RelabelType *) leftop)->arg;
3760 : :
3761 [ + + ]: 670 : if (equal(leftop, partkey))
3762 : : {
3763 [ + + + + : 490 : switch (btest->booltesttype)
+ + - ]
3764 : : {
3765 : 110 : case IS_NOT_TRUE:
3766 : 110 : *notclause = true;
3767 : : pg_fallthrough;
3768 : 225 : case IS_TRUE:
3769 : 225 : *outconst = (Expr *) makeBoolConst(true, false);
3770 : 225 : return PARTCLAUSE_MATCH_CLAUSE;
3771 : 70 : case IS_NOT_FALSE:
3772 : 70 : *notclause = true;
3773 : : pg_fallthrough;
3774 : 185 : case IS_FALSE:
3775 : 185 : *outconst = (Expr *) makeBoolConst(false, false);
3776 : 185 : return PARTCLAUSE_MATCH_CLAUSE;
3777 : 45 : case IS_NOT_UNKNOWN:
3778 : 45 : *notclause = true;
3779 : : pg_fallthrough;
3780 : 80 : case IS_UNKNOWN:
3781 : 80 : return PARTCLAUSE_MATCH_NULLNESS;
3782 : 0 : default:
3783 : 0 : return PARTCLAUSE_UNSUPPORTED;
3784 : : }
3785 : : }
3786 : : /* does not match partition key */
3787 : 180 : return PARTCLAUSE_NOMATCH;
3788 : : }
3789 : : else
3790 : : {
3791 : 560 : bool is_not_clause = is_notclause(clause);
3792 : :
3793 [ + + ]: 560 : leftop = is_not_clause ? get_notclausearg(clause) : clause;
3794 : :
3795 : 560 : leftop = (Expr *) strip_noop_phvs((Node *) leftop);
3796 [ - + ]: 560 : while (IsA(leftop, RelabelType))
3797 : 0 : leftop = ((RelabelType *) leftop)->arg;
3798 : :
3799 : : /* Compare to the partition key, and make up a clause ... */
3800 [ + + ]: 560 : if (equal(leftop, partkey))
3801 : 120 : *outconst = (Expr *) makeBoolConst(!is_not_clause, false);
3802 [ + + ]: 440 : else if (equal(negate_clause((Node *) leftop), partkey))
3803 : 40 : *outconst = (Expr *) makeBoolConst(is_not_clause, false);
3804 : : else
3805 : 400 : return PARTCLAUSE_NOMATCH;
3806 : :
3807 : 160 : return PARTCLAUSE_MATCH_CLAUSE;
3808 : : }
3809 : : }
3810 : :
3811 : : /*
3812 : : * partkey_datum_from_expr
3813 : : * Evaluate expression for potential partition pruning
3814 : : *
3815 : : * Evaluate 'expr'; set *value and *isnull to the resulting Datum and nullflag.
3816 : : *
3817 : : * If expr isn't a Const, its ExprState is in stateidx of the context
3818 : : * exprstate array.
3819 : : *
3820 : : * Note that the evaluated result may be in the per-tuple memory context of
3821 : : * context->exprcontext, and we may have leaked other memory there too.
3822 : : * This memory must be recovered by resetting that ExprContext after
3823 : : * we're done with the pruning operation (see execPartition.c).
3824 : : */
3825 : : static void
3826 : 10604 : partkey_datum_from_expr(PartitionPruneContext *context,
3827 : : Expr *expr, int stateidx,
3828 : : Datum *value, bool *isnull)
3829 : : {
3830 [ + + ]: 10604 : if (IsA(expr, Const))
3831 : : {
3832 : : /* We can always determine the value of a constant */
3833 : 7712 : Const *con = (Const *) expr;
3834 : :
3835 : 7712 : *value = con->constvalue;
3836 : 7712 : *isnull = con->constisnull;
3837 : : }
3838 : : else
3839 : : {
3840 : : ExprState *exprstate;
3841 : : ExprContext *ectx;
3842 : :
3843 : : /*
3844 : : * We should never see a non-Const in a step unless the caller has
3845 : : * passed a valid ExprContext.
3846 : : */
3847 : : Assert(context->exprcontext != NULL);
3848 : :
3849 : 2892 : exprstate = context->exprstates[stateidx];
3850 : 2892 : ectx = context->exprcontext;
3851 : 2892 : *value = ExecEvalExprSwitchContext(exprstate, ectx, isnull);
3852 : : }
3853 : 10604 : }
|