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