Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pgpa_planner.c
4 : : * planner hooks
5 : : *
6 : : * Copyright (c) 2016-2024, PostgreSQL Global Development Group
7 : : *
8 : : * contrib/pg_plan_advice/pgpa_planner.c
9 : : *
10 : : *-------------------------------------------------------------------------
11 : : */
12 : : #include "postgres.h"
13 : :
14 : : #include "pg_plan_advice.h"
15 : : #include "pgpa_collector.h"
16 : : #include "pgpa_identifier.h"
17 : : #include "pgpa_output.h"
18 : : #include "pgpa_planner.h"
19 : : #include "pgpa_trove.h"
20 : : #include "pgpa_walker.h"
21 : :
22 : : #include "common/hashfn_unstable.h"
23 : : #include "nodes/makefuncs.h"
24 : : #include "optimizer/extendplan.h"
25 : : #include "optimizer/pathnode.h"
26 : : #include "optimizer/paths.h"
27 : : #include "optimizer/plancat.h"
28 : : #include "optimizer/planner.h"
29 : : #include "parser/parsetree.h"
30 : : #include "utils/lsyscache.h"
31 : :
32 : : #ifdef USE_ASSERT_CHECKING
33 : :
34 : : /*
35 : : * When assertions are enabled, we try generating relation identifiers during
36 : : * planning, saving them in a hash table, and then cross-checking them against
37 : : * the ones generated after planning is complete.
38 : : */
39 : : typedef struct pgpa_ri_checker_key
40 : : {
41 : : char *plan_name;
42 : : Index rti;
43 : : } pgpa_ri_checker_key;
44 : :
45 : : typedef struct pgpa_ri_checker
46 : : {
47 : : pgpa_ri_checker_key key;
48 : : uint32 status;
49 : : const char *rid_string;
50 : : } pgpa_ri_checker;
51 : :
52 : : static uint32 pgpa_ri_checker_hash_key(pgpa_ri_checker_key key);
53 : :
54 : : static inline bool
55 : 0 : pgpa_ri_checker_compare_key(pgpa_ri_checker_key a, pgpa_ri_checker_key b)
56 : : {
57 [ # # ]: 0 : if (a.rti != b.rti)
58 : 0 : return false;
59 [ # # ]: 0 : if (a.plan_name == NULL)
60 : 0 : return (b.plan_name == NULL);
61 [ # # ]: 0 : if (b.plan_name == NULL)
62 : 0 : return false;
63 : 0 : return strcmp(a.plan_name, b.plan_name) == 0;
64 : 0 : }
65 : :
66 : : #define SH_PREFIX pgpa_ri_check
67 : : #define SH_ELEMENT_TYPE pgpa_ri_checker
68 : : #define SH_KEY_TYPE pgpa_ri_checker_key
69 : : #define SH_KEY key
70 : : #define SH_HASH_KEY(tb, key) pgpa_ri_checker_hash_key(key)
71 : : #define SH_EQUAL(tb, a, b) pgpa_ri_checker_compare_key(a, b)
72 : : #define SH_SCOPE static inline
73 : : #define SH_DECLARE
74 : : #define SH_DEFINE
75 : : #include "lib/simplehash.h"
76 : :
77 : : #endif
78 : :
79 : : typedef struct pgpa_planner_state
80 : : {
81 : : ExplainState *explain_state;
82 : : bool generate_advice_feedback;
83 : : bool generate_advice_string;
84 : : pgpa_trove *trove;
85 : : MemoryContext trove_cxt;
86 : : List *sj_unique_rels;
87 : :
88 : : #ifdef USE_ASSERT_CHECKING
89 : : pgpa_ri_check_hash *ri_check_hash;
90 : : #endif
91 : : } pgpa_planner_state;
92 : :
93 : : typedef struct pgpa_join_state
94 : : {
95 : : /* Most-recently-considered outer rel. */
96 : : RelOptInfo *outerrel;
97 : :
98 : : /* Most-recently-considered inner rel. */
99 : : RelOptInfo *innerrel;
100 : :
101 : : /*
102 : : * Array of relation identifiers for all members of this joinrel, with
103 : : * outerrel idenifiers before innerrel identifiers.
104 : : */
105 : : pgpa_identifier *rids;
106 : :
107 : : /* Number of outer rel identifiers. */
108 : : int outer_count;
109 : :
110 : : /* Number of inner rel identifiers. */
111 : : int inner_count;
112 : :
113 : : /*
114 : : * Trove lookup results.
115 : : *
116 : : * join_entries and rel_entries are arrays of entries, and join_indexes
117 : : * and rel_indexes are the integer offsets within those arrays of entries
118 : : * potentially relevant to us. The "join" fields correspond to a lookup
119 : : * using PGPA_TROVE_LOOKUP_JOIN and the "rel" fields to a lookup using
120 : : * PGPA_TROVE_LOOKUP_REL.
121 : : */
122 : : pgpa_trove_entry *join_entries;
123 : : Bitmapset *join_indexes;
124 : : pgpa_trove_entry *rel_entries;
125 : : Bitmapset *rel_indexes;
126 : : } pgpa_join_state;
127 : :
128 : : /* Saved hook values */
129 : : static get_relation_info_hook_type prev_get_relation_info = NULL;
130 : : static join_path_setup_hook_type prev_join_path_setup = NULL;
131 : : static joinrel_setup_hook_type prev_joinrel_setup = NULL;
132 : : static planner_setup_hook_type prev_planner_setup = NULL;
133 : : static planner_shutdown_hook_type prev_planner_shutdown = NULL;
134 : :
135 : : /* Other global variabes */
136 : : static int planner_extension_id = -1;
137 : :
138 : : /* Function prototypes. */
139 : : static void pgpa_get_relation_info(PlannerInfo *root,
140 : : Oid relationObjectId,
141 : : bool inhparent,
142 : : RelOptInfo *rel);
143 : : static void pgpa_joinrel_setup(PlannerInfo *root,
144 : : RelOptInfo *joinrel,
145 : : RelOptInfo *outerrel,
146 : : RelOptInfo *innerrel,
147 : : SpecialJoinInfo *sjinfo,
148 : : List *restrictlist);
149 : : static void pgpa_join_path_setup(PlannerInfo *root,
150 : : RelOptInfo *joinrel,
151 : : RelOptInfo *outerrel,
152 : : RelOptInfo *innerrel,
153 : : JoinType jointype,
154 : : JoinPathExtraData *extra);
155 : : static void pgpa_planner_setup(PlannerGlobal *glob, Query *parse,
156 : : const char *query_string,
157 : : double *tuple_fraction,
158 : : ExplainState *es);
159 : : static void pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse,
160 : : const char *query_string, PlannedStmt *pstmt);
161 : : static void pgpa_planner_apply_joinrel_advice(uint64 *pgs_mask_p,
162 : : char *plan_name,
163 : : pgpa_join_state *pjs);
164 : : static void pgpa_planner_apply_join_path_advice(JoinType jointype,
165 : : uint64 *pgs_mask_p,
166 : : char *plan_name,
167 : : pgpa_join_state *pjs);
168 : : static void pgpa_planner_apply_scan_advice(RelOptInfo *rel,
169 : : pgpa_trove_entry *scan_entries,
170 : : Bitmapset *scan_indexes,
171 : : pgpa_trove_entry *rel_entries,
172 : : Bitmapset *rel_indexes);
173 : : static uint64 pgpa_join_strategy_mask_from_advice_tag(pgpa_advice_tag_type tag);
174 : : static bool pgpa_join_order_permits_join(int outer_count, int inner_count,
175 : : pgpa_identifier *rids,
176 : : pgpa_trove_entry *entry);
177 : : static bool pgpa_join_method_permits_join(int outer_count, int inner_count,
178 : : pgpa_identifier *rids,
179 : : pgpa_trove_entry *entry,
180 : : bool *restrict_method);
181 : : static bool pgpa_opaque_join_permits_join(int outer_count, int inner_count,
182 : : pgpa_identifier *rids,
183 : : pgpa_trove_entry *entry,
184 : : bool *restrict_method);
185 : : static bool pgpa_semijoin_permits_join(int outer_count, int inner_count,
186 : : pgpa_identifier *rids,
187 : : pgpa_trove_entry *entry,
188 : : bool outer_side_nullable,
189 : : bool *restrict_method);
190 : :
191 : : static List *pgpa_planner_append_feedback(List *list, pgpa_trove *trove,
192 : : pgpa_trove_lookup_type type,
193 : : pgpa_identifier *rt_identifiers,
194 : : pgpa_plan_walker_context *walker);
195 : :
196 : : static inline void pgpa_ri_checker_save(pgpa_planner_state *pps,
197 : : PlannerInfo *root,
198 : : RelOptInfo *rel);
199 : : static void pgpa_ri_checker_validate(pgpa_planner_state *pps,
200 : : PlannedStmt *pstmt);
201 : :
202 : : static char *pgpa_bms_to_cstring(Bitmapset *bms);
203 : : static const char *pgpa_jointype_to_cstring(JoinType jointype);
204 : :
205 : : /*
206 : : * Install planner-related hooks.
207 : : */
208 : : void
209 : 795 : pgpa_planner_install_hooks(void)
210 : : {
211 : 795 : planner_extension_id = GetPlannerExtensionId("pg_plan_advice");
212 : 795 : prev_get_relation_info = get_relation_info_hook;
213 : 795 : get_relation_info_hook = pgpa_get_relation_info;
214 : 795 : prev_joinrel_setup = joinrel_setup_hook;
215 : 795 : joinrel_setup_hook = pgpa_joinrel_setup;
216 : 795 : prev_join_path_setup = join_path_setup_hook;
217 : 795 : join_path_setup_hook = pgpa_join_path_setup;
218 : 795 : prev_planner_setup = planner_setup_hook;
219 : 795 : planner_setup_hook = pgpa_planner_setup;
220 : 795 : prev_planner_shutdown = planner_shutdown_hook;
221 : 795 : planner_shutdown_hook = pgpa_planner_shutdown;
222 : 795 : }
223 : :
224 : : /*
225 : : * Hook function for get_relation_info().
226 : : *
227 : : * We can apply scan advice at this opint, and we also usee this as an
228 : : * opportunity to do range-table identifier cross-checking in assert-enabled
229 : : * builds.
230 : : */
231 : : static void
232 : 50880 : pgpa_get_relation_info(PlannerInfo *root, Oid relationObjectId,
233 : : bool inhparent, RelOptInfo *rel)
234 : : {
235 : 50880 : pgpa_planner_state *pps;
236 : :
237 : : /* Fetch our private state, set up by pgpa_planner_setup(). */
238 : 50880 : pps = GetPlannerGlobalExtensionState(root->glob, planner_extension_id);
239 : :
240 : : /* Save details needed for range table identifier cross-checking. */
241 [ + + ]: 50880 : if (pps != NULL)
242 : 47309 : pgpa_ri_checker_save(pps, root, rel);
243 : :
244 : : /* If query advice was provided, search for relevant entries. */
245 [ + + + + ]: 50880 : if (pps != NULL && pps->trove != NULL)
246 : : {
247 : 45244 : pgpa_identifier rid;
248 : 45244 : pgpa_trove_result tresult_scan;
249 : 45244 : pgpa_trove_result tresult_rel;
250 : :
251 : : /* Search for scan advice and general rel advice. */
252 : 45244 : pgpa_compute_identifier_by_rti(root, rel->relid, &rid);
253 : 45244 : pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_SCAN, 1, &rid,
254 : : &tresult_scan);
255 : 45244 : pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_REL, 1, &rid,
256 : : &tresult_rel);
257 : :
258 : : /* If relevant entries were found, apply them. */
259 [ + + + + ]: 45244 : if (tresult_scan.indexes != NULL || tresult_rel.indexes != NULL)
260 : : {
261 : 72 : uint64 original_mask = rel->pgs_mask;
262 : :
263 : 144 : pgpa_planner_apply_scan_advice(rel,
264 : 72 : tresult_scan.entries,
265 : 72 : tresult_scan.indexes,
266 : 72 : tresult_rel.entries,
267 : 72 : tresult_rel.indexes);
268 : :
269 : : /* Emit debugging message, if enabled. */
270 [ - + # # ]: 72 : if (pg_plan_advice_trace_mask && original_mask != rel->pgs_mask)
271 [ # # # # ]: 0 : ereport(WARNING,
272 : : (errmsg("strategy mask for RTI %u changed from 0x%" PRIx64 " to 0x%" PRIx64,
273 : : rel->relid, original_mask, rel->pgs_mask)));
274 : 72 : }
275 : 45244 : }
276 : :
277 : : /* Pass call to previous hook. */
278 [ + - ]: 50880 : if (prev_get_relation_info)
279 : 0 : (*prev_get_relation_info) (root, relationObjectId, inhparent, rel);
280 : 50880 : }
281 : :
282 : : /*
283 : : * Search for advice pertaining to a proposed join.
284 : : */
285 : : static pgpa_join_state *
286 : 88433 : pgpa_get_join_state(PlannerInfo *root, RelOptInfo *joinrel,
287 : : RelOptInfo *outerrel, RelOptInfo *innerrel)
288 : : {
289 : 88433 : pgpa_planner_state *pps;
290 : 88433 : pgpa_join_state *pjs;
291 : 88433 : bool new_pjs = false;
292 : :
293 : : /* Fetch our private state, set up by pgpa_planner_setup(). */
294 : 88433 : pps = GetPlannerGlobalExtensionState(root->glob, planner_extension_id);
295 [ + - + + ]: 88433 : if (pps == NULL || pps->trove == NULL)
296 : : {
297 : : /* No advice applies to this query, hence none to this joinrel. */
298 : 123 : return NULL;
299 : : }
300 : :
301 : : /*
302 : : * See whether we've previously associated a pgpa_join_state with this
303 : : * joinrel. If we have not, we need to try to construct one. If we have,
304 : : * then there are two cases: (a) if innerrel and outerrel are unchanged,
305 : : * we can simply use it, and (b) if they have changed, we need to rejigger
306 : : * the array of identifiers but can still skip the trove lookup.
307 : : */
308 : 88310 : pjs = GetRelOptInfoExtensionState(joinrel, planner_extension_id);
309 [ + + ]: 88310 : if (pjs != NULL)
310 : : {
311 [ + + + + ]: 66438 : if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
312 : : {
313 : : /*
314 : : * If there's no potentially relevant advice, then the presence of
315 : : * this pgpa_join_state acts like a negative cache entry: it tells
316 : : * us not to bother searching the trove for advice, because we
317 : : * will not find any.
318 : : */
319 : 66150 : return NULL;
320 : : }
321 : :
322 [ + + - + ]: 288 : if (pjs->outerrel == outerrel && pjs->innerrel == innerrel)
323 : : {
324 : : /* No updates required, so just return. */
325 : : /* XXX. Does this need to do something different under GEQO? */
326 : 105 : return pjs;
327 : : }
328 : 183 : }
329 : :
330 : : /*
331 : : * If there's no pgpa_join_state yet, we need to allocate one. Trove keys
332 : : * will not get built for RTE_JOIN RTEs, so the array may end up being
333 : : * larger than needed. It's not worth trying to compute a perfectly
334 : : * accurate count here.
335 : : */
336 [ + + ]: 22055 : if (pjs == NULL)
337 : : {
338 : 21872 : int pessimistic_count = bms_num_members(joinrel->relids);
339 : :
340 : 21872 : pjs = palloc0_object(pgpa_join_state);
341 : 21872 : pjs->rids = palloc_array(pgpa_identifier, pessimistic_count);
342 : 21872 : new_pjs = true;
343 : 21872 : }
344 : :
345 : : /*
346 : : * Either we just allocated a new pgpa_join_state, or the existing one
347 : : * needs reconfiguring for a new innerrel and outerrel. The required array
348 : : * size can't change, so we can overwrite the existing one.
349 : : */
350 : 22055 : pjs->outerrel = outerrel;
351 : 22055 : pjs->innerrel = innerrel;
352 : 22055 : pjs->outer_count =
353 : 22055 : pgpa_compute_identifiers_by_relids(root, outerrel->relids, pjs->rids);
354 : 22055 : pjs->inner_count =
355 : 44110 : pgpa_compute_identifiers_by_relids(root, innerrel->relids,
356 : 22055 : pjs->rids + pjs->outer_count);
357 : :
358 : : /*
359 : : * If we allocated a new pgpa_join_state, search our trove of advice for
360 : : * relevant entries. The trove lookup will return the same results for
361 : : * every outerrel/innerrel combination, so we don't need to repeat that
362 : : * work every time.
363 : : */
364 [ + + ]: 22055 : if (new_pjs)
365 : : {
366 : 21872 : pgpa_trove_result tresult;
367 : :
368 : : /* Find join entries. */
369 : 43744 : pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_JOIN,
370 : 21872 : pjs->outer_count + pjs->inner_count,
371 : 21872 : pjs->rids, &tresult);
372 : 21872 : pjs->join_entries = tresult.entries;
373 : 21872 : pjs->join_indexes = tresult.indexes;
374 : :
375 : : /* Find rel entries. */
376 : 43744 : pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_REL,
377 : 21872 : pjs->outer_count + pjs->inner_count,
378 : 21872 : pjs->rids, &tresult);
379 : 21872 : pjs->rel_entries = tresult.entries;
380 : 21872 : pjs->rel_indexes = tresult.indexes;
381 : :
382 : : /* Now that the new pgpa_join_state is fully valid, save a pointer. */
383 : 21872 : SetRelOptInfoExtensionState(joinrel, planner_extension_id, pjs);
384 : :
385 : : /*
386 : : * If there was no relevant advice found, just return NULL. This
387 : : * pgpa_join_state will stick around as a sort of negative cache
388 : : * entry, so that future calls for this same joinrel quickly return
389 : : * NULL.
390 : : */
391 [ + + + + ]: 21872 : if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
392 : 21767 : return NULL;
393 [ + + ]: 21872 : }
394 : :
395 : 288 : return pjs;
396 : 88433 : }
397 : :
398 : : /*
399 : : * Enforce any provided advice that is relevant to any method of implementing
400 : : * this join.
401 : : *
402 : : * Although we're passed the outerrel and innerrel here, those are just
403 : : * whatever values happened to prompt the creation of this joinrel; they
404 : : * shouldn't really influence our choice of what advice to apply.
405 : : */
406 : : static void
407 : 21903 : pgpa_joinrel_setup(PlannerInfo *root, RelOptInfo *joinrel,
408 : : RelOptInfo *outerrel, RelOptInfo *innerrel,
409 : : SpecialJoinInfo *sjinfo, List *restrictlist)
410 : : {
411 : 21903 : pgpa_join_state *pjs;
412 : :
413 [ + - ]: 21903 : Assert(bms_membership(joinrel->relids) == BMS_MULTIPLE);
414 : :
415 : : /* Get our private state information for this join. */
416 : 21903 : pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
417 : :
418 : : /* If there is relevant advice, call a helper function to apply it. */
419 [ + + ]: 21903 : if (pjs != NULL)
420 : : {
421 : 105 : uint64 original_mask = joinrel->pgs_mask;
422 : :
423 : 210 : pgpa_planner_apply_joinrel_advice(&joinrel->pgs_mask,
424 : 105 : root->plan_name,
425 : 105 : pjs);
426 : :
427 : : /* Emit debugging message, if enabled. */
428 [ - + # # ]: 105 : if (pg_plan_advice_trace_mask && original_mask != joinrel->pgs_mask)
429 [ # # # # ]: 0 : ereport(WARNING,
430 : : (errmsg("strategy mask for join on RTIs %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
431 : : pgpa_bms_to_cstring(joinrel->relids),
432 : : original_mask,
433 : : joinrel->pgs_mask)));
434 : 105 : }
435 : :
436 : : /* Pass call to previous hook. */
437 [ + - ]: 21903 : if (prev_joinrel_setup)
438 : 0 : (*prev_joinrel_setup) (root, joinrel, outerrel, innerrel,
439 : 0 : sjinfo, restrictlist);
440 : 21903 : }
441 : :
442 : : /*
443 : : * Enforce any provided advice that is relevant to this particular method of
444 : : * implementing this particular join.
445 : : */
446 : : static void
447 : 66530 : pgpa_join_path_setup(PlannerInfo *root, RelOptInfo *joinrel,
448 : : RelOptInfo *outerrel, RelOptInfo *innerrel,
449 : : JoinType jointype, JoinPathExtraData *extra)
450 : : {
451 : 66530 : pgpa_join_state *pjs;
452 : :
453 [ + - ]: 66530 : Assert(bms_membership(joinrel->relids) == BMS_MULTIPLE);
454 : :
455 : : /*
456 : : * If we're considering implementing a semijoin by making one side unique,
457 : : * make a note of it in the pgpa_planner_state. See comments for
458 : : * pgpa_sj_unique_rel for why we do this.
459 : : */
460 [ + + + + ]: 66530 : if (jointype == JOIN_UNIQUE_OUTER || jointype == JOIN_UNIQUE_INNER)
461 : : {
462 : 1602 : pgpa_planner_state *pps;
463 : 1602 : RelOptInfo *uniquerel;
464 : :
465 [ + + ]: 1602 : uniquerel = jointype == JOIN_UNIQUE_OUTER ? outerrel : innerrel;
466 : 1602 : pps = GetPlannerGlobalExtensionState(root->glob, planner_extension_id);
467 [ + - # # ]: 1602 : if (pps != NULL &&
468 [ - + ]: 1602 : (pps->generate_advice_string || pps->generate_advice_feedback))
469 : : {
470 : 1602 : bool found = false;
471 : :
472 : : /* Avoid adding duplicates. */
473 [ + + + + : 4582 : foreach_ptr(pgpa_sj_unique_rel, ur, pps->sj_unique_rels)
+ + + + ]
474 : : {
475 : : /*
476 : : * We should always use the same pointer for the same plan
477 : : * name, so we need not use strcmp() here.
478 : : */
479 [ + - + + ]: 1378 : if (root->plan_name == ur->plan_name &&
480 : 1378 : bms_equal(uniquerel->relids, ur->relids))
481 : : {
482 : 880 : found = true;
483 : 880 : break;
484 : : }
485 : 2100 : }
486 : :
487 : : /* If not a duplicate, append to the list. */
488 [ + + ]: 1602 : if (!found)
489 : : {
490 : 722 : pgpa_sj_unique_rel *ur = palloc_object(pgpa_sj_unique_rel);
491 : :
492 : 722 : ur->plan_name = root->plan_name;
493 : 722 : ur->relids = uniquerel->relids;
494 : 722 : pps->sj_unique_rels = lappend(pps->sj_unique_rels, ur);
495 : 722 : }
496 : 1602 : }
497 : 1602 : }
498 : :
499 : : /* Get our private state information for this join. */
500 : 66530 : pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
501 : :
502 : : /* If there is relevant advice, call a helper function to apply it. */
503 [ + + ]: 66530 : if (pjs != NULL)
504 : : {
505 : 288 : uint64 original_mask = extra->pgs_mask;
506 : :
507 : 576 : pgpa_planner_apply_join_path_advice(jointype,
508 : 288 : &extra->pgs_mask,
509 : 288 : root->plan_name,
510 : 288 : pjs);
511 : :
512 : : /* Emit debugging message, if enabled. */
513 [ - + # # ]: 288 : if (pg_plan_advice_trace_mask && original_mask != extra->pgs_mask)
514 [ # # # # ]: 0 : ereport(WARNING,
515 : : (errmsg("strategy mask for %s join on %s with outer %s and inner %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
516 : : pgpa_jointype_to_cstring(jointype),
517 : : pgpa_bms_to_cstring(joinrel->relids),
518 : : pgpa_bms_to_cstring(outerrel->relids),
519 : : pgpa_bms_to_cstring(innerrel->relids),
520 : : original_mask,
521 : : extra->pgs_mask)));
522 : 288 : }
523 : :
524 : : /* Pass call to previous hook. */
525 [ + - ]: 66530 : if (prev_join_path_setup)
526 : 0 : (*prev_join_path_setup) (root, joinrel, outerrel, innerrel,
527 : 0 : jointype, extra);
528 : 66530 : }
529 : :
530 : : /*
531 : : * Carry out whatever setup work we need to do before planning.
532 : : */
533 : : static void
534 : 44331 : pgpa_planner_setup(PlannerGlobal *glob, Query *parse, const char *query_string,
535 : : double *tuple_fraction, ExplainState *es)
536 : : {
537 : 44331 : pgpa_trove *trove = NULL;
538 : 44331 : pgpa_planner_state *pps;
539 : 44331 : bool generate_advice_feedback = false;
540 : 44331 : bool generate_advice_string = false;
541 : 44331 : bool needs_pps = false;
542 : :
543 : : /*
544 : : * Decide whether we need to generate an advice string. We must do this if
545 : : * the user has told us to do it categorically, or if at least one
546 : : * collector is enabled, or if the user has requested it using the
547 : : * EXPLAIN (PLAN_ADVICE) option.
548 : : */
549 [ + + ]: 88661 : generate_advice_string = (pg_plan_advice_always_store_advice_details ||
550 [ + + ]: 44330 : pg_plan_advice_local_collector ||
551 [ + + ]: 42321 : pg_plan_advice_shared_collector ||
552 : 174 : pg_plan_advice_should_explain(es));
553 [ + + ]: 44331 : if (generate_advice_string)
554 : 44276 : needs_pps = true;
555 : :
556 : : /*
557 : : * If any advice was provided, build a trove of advice for use during
558 : : * planning.
559 : : */
560 [ + + + + ]: 44331 : if (pg_plan_advice_advice != NULL && pg_plan_advice_advice[0] != '\0')
561 : : {
562 : 42262 : List *advice_items;
563 : 42262 : char *error;
564 : :
565 : : /*
566 : : * Parsing shouldn't fail here, because we must have previously parsed
567 : : * successfully in pg_plan_advice_advice_check_hook, but if it does,
568 : : * emit a warning.
569 : : */
570 : 42262 : advice_items = pgpa_parse(pg_plan_advice_advice, &error);
571 [ + - ]: 42262 : if (error)
572 [ # # # # ]: 0 : elog(WARNING, "could not parse advice: %s", error);
573 : :
574 : : /*
575 : : * It's possible that the advice string was non-empty but contained no
576 : : * actual advice, e.g. it was all whitespace.
577 : : */
578 [ + + ]: 42262 : if (advice_items != NIL)
579 : : {
580 : 42260 : trove = pgpa_build_trove(advice_items);
581 : 42260 : needs_pps = true;
582 : :
583 : : /*
584 : : * If we know that we're running under EXPLAIN, or if the user
585 : : * has told us to always do the work, generate advice feedback.
586 : : */
587 [ + + - + ]: 42260 : if (es != NULL || pg_plan_advice_always_store_advice_details)
588 : 3538 : generate_advice_feedback = true;
589 : 42260 : }
590 : 42262 : }
591 : :
592 : : #ifdef USE_ASSERT_CHECKING
593 : :
594 : : /*
595 : : * If asserts are enabled, always build a private state object for
596 : : * cross-checks.
597 : : */
598 : 44331 : needs_pps = true;
599 : : #endif
600 : :
601 : : /*
602 : : * We only create and initialize a private state object if it's needed for
603 : : * some purpose. That could be (1) recording that we will need to generate
604 : : * an advice string, (2) storing a trove of supplied advice, or (3)
605 : : * facilitating debugging cross-checks when asserts are enabled.
606 : : */
607 [ - + ]: 44331 : if (needs_pps)
608 : : {
609 : 44331 : pps = palloc0_object(pgpa_planner_state);
610 : 44331 : pps->explain_state = es;
611 : 44331 : pps->generate_advice_feedback = generate_advice_feedback;
612 : 44331 : pps->generate_advice_string = generate_advice_string;
613 : 44331 : pps->trove = trove;
614 : : #ifdef USE_ASSERT_CHECKING
615 : 44331 : pps->ri_check_hash =
616 : 44331 : pgpa_ri_check_create(CurrentMemoryContext, 1024, NULL);
617 : : #endif
618 : 44331 : SetPlannerGlobalExtensionState(glob, planner_extension_id, pps);
619 : 44331 : }
620 : 44331 : }
621 : :
622 : : /*
623 : : * Carry out whatever work we want to do after planning is complete.
624 : : */
625 : : static void
626 : 43570 : pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse,
627 : : const char *query_string, PlannedStmt *pstmt)
628 : : {
629 : 43570 : pgpa_planner_state *pps;
630 : 43570 : pgpa_trove *trove = NULL;
631 : 43570 : pgpa_plan_walker_context walker = {0}; /* placate compiler */
632 : 43570 : bool generate_advice_feedback = false;
633 : 43570 : bool generate_advice_string = false;
634 : 43570 : List *pgpa_items = NIL;
635 : 43570 : pgpa_identifier *rt_identifiers = NULL;
636 : :
637 : : /* Fetch our private state, set up by pgpa_planner_setup(). */
638 : 43570 : pps = GetPlannerGlobalExtensionState(glob, planner_extension_id);
639 [ - + ]: 43570 : if (pps != NULL)
640 : : {
641 : 43570 : trove = pps->trove;
642 : 43570 : generate_advice_feedback = pps->generate_advice_feedback;
643 : 43570 : generate_advice_string = pps->generate_advice_string;
644 : 43570 : }
645 : :
646 : : /*
647 : : * If we're trying to generate an advice string or if we're trying to
648 : : * provide advice feedback, then we will need to create range table
649 : : * identifiers.
650 : : */
651 [ + + + + ]: 43570 : if (generate_advice_string || generate_advice_feedback)
652 : : {
653 : 43527 : pgpa_plan_walker(&walker, pstmt, pps->sj_unique_rels);
654 : 43527 : rt_identifiers = pgpa_create_identifiers_for_planned_stmt(pstmt);
655 : 43527 : }
656 : :
657 : : /* Generate the advice string, if we need to do so. */
658 [ + + ]: 43570 : if (generate_advice_string)
659 : : {
660 : 43515 : char *advice_string;
661 : 43515 : StringInfoData buf;
662 : :
663 : : /* Generate a textual advice string. */
664 : 43515 : initStringInfo(&buf);
665 : 43515 : pgpa_output_advice(&buf, &walker, rt_identifiers);
666 : 43515 : advice_string = buf.data;
667 : :
668 : : /* If the advice string is empty, don't bother collecting it. */
669 [ + + ]: 43515 : if (advice_string[0] != '\0')
670 : 24430 : pgpa_collect_advice(pstmt->queryId, query_string, advice_string);
671 : :
672 : : /* Save the advice string in the final plan. */
673 : 87030 : pgpa_items = lappend(pgpa_items,
674 : 43515 : makeDefElem("advice_string",
675 : 43515 : (Node *) makeString(advice_string),
676 : : -1));
677 : 43515 : }
678 : :
679 : : /*
680 : : * If we're trying to provide advice feedback, then we will need to
681 : : * analyze how successful the advice was.
682 : : */
683 [ + + ]: 43570 : if (generate_advice_feedback)
684 : : {
685 : 3531 : List *feedback = NIL;
686 : :
687 : : /*
688 : : * Inject a Node-tree representation of all the trove-entry flags into
689 : : * the PlannedStmt.
690 : : */
691 : 7062 : feedback = pgpa_planner_append_feedback(feedback,
692 : 3531 : trove,
693 : : PGPA_TROVE_LOOKUP_SCAN,
694 : 3531 : rt_identifiers, &walker);
695 : 7062 : feedback = pgpa_planner_append_feedback(feedback,
696 : 3531 : trove,
697 : : PGPA_TROVE_LOOKUP_JOIN,
698 : 3531 : rt_identifiers, &walker);
699 : 7062 : feedback = pgpa_planner_append_feedback(feedback,
700 : 3531 : trove,
701 : : PGPA_TROVE_LOOKUP_REL,
702 : 3531 : rt_identifiers, &walker);
703 : :
704 : 7062 : pgpa_items = lappend(pgpa_items, makeDefElem("feedback",
705 : 3531 : (Node *) feedback,
706 : : -1));
707 : 3531 : }
708 : :
709 : : /* Push whatever data we're saving into the PlannedStmt. */
710 [ + + ]: 43570 : if (pgpa_items != NIL)
711 : 43527 : pstmt->extension_state =
712 : 87054 : lappend(pstmt->extension_state,
713 : 43527 : makeDefElem("pg_plan_advice", (Node *) pgpa_items, -1));
714 : :
715 : : /*
716 : : * If assertions are enabled, cross-check the generated range table
717 : : * identifiers.
718 : : */
719 [ - + ]: 43570 : if (pps != NULL)
720 : 43570 : pgpa_ri_checker_validate(pps, pstmt);
721 : 43570 : }
722 : :
723 : : /*
724 : : * Enforce overall restrictions on a join relation that apply uniformly
725 : : * regardless of the choice of inner and outer rel.
726 : : */
727 : : static void
728 : 105 : pgpa_planner_apply_joinrel_advice(uint64 *pgs_mask_p, char *plan_name,
729 : : pgpa_join_state *pjs)
730 : : {
731 : 105 : int i = -1;
732 : 105 : int flags;
733 : 105 : bool gather_conflict = false;
734 : 105 : uint64 gather_mask = 0;
735 : 105 : Bitmapset *gather_partial_match = NULL;
736 : 105 : Bitmapset *gather_full_match = NULL;
737 : 105 : bool partitionwise_conflict = false;
738 : 105 : int partitionwise_outcome = 0;
739 : 105 : Bitmapset *partitionwise_partial_match = NULL;
740 : 105 : Bitmapset *partitionwise_full_match = NULL;
741 : :
742 : : /* Iterate over all possibly-relevant advice. */
743 [ + + ]: 151 : while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
744 : : {
745 : 46 : pgpa_trove_entry *entry = &pjs->rel_entries[i];
746 : 46 : pgpa_itm_type itm;
747 : 46 : bool full_match = false;
748 : 46 : uint64 my_gather_mask = 0;
749 : 46 : int my_partitionwise_outcome = 0; /* >0 yes, <0 no */
750 : :
751 : : /*
752 : : * For GATHER and GATHER_MERGE, if the specified relations exactly
753 : : * match this joinrel, do whatever the advice says; otherwise, don't
754 : : * allow Gather or Gather Merge at this level. For NO_GATHER, there
755 : : * must be a single target relation which must be included in this
756 : : * joinrel, so just don't allow Gather or Gather Merge here, full
757 : : * stop.
758 : : */
759 [ + + ]: 46 : if (entry->tag == PGPA_TAG_NO_GATHER)
760 : : {
761 : 7 : my_gather_mask = PGS_CONSIDER_NONPARTIAL;
762 : 7 : full_match = true;
763 : 7 : }
764 : : else
765 : : {
766 : 39 : int total_count;
767 : :
768 : 39 : total_count = pjs->outer_count + pjs->inner_count;
769 : 78 : itm = pgpa_identifiers_match_target(total_count, pjs->rids,
770 : 39 : entry->target);
771 [ + - ]: 39 : Assert(itm != PGPA_ITM_DISJOINT);
772 : :
773 [ + + ]: 39 : if (itm == PGPA_ITM_EQUAL)
774 : : {
775 : 9 : full_match = true;
776 [ + + ]: 9 : if (entry->tag == PGPA_TAG_PARTITIONWISE)
777 : 4 : my_partitionwise_outcome = 1;
778 [ + + ]: 5 : else if (entry->tag == PGPA_TAG_GATHER)
779 : 3 : my_gather_mask = PGS_GATHER;
780 [ + - ]: 2 : else if (entry->tag == PGPA_TAG_GATHER_MERGE)
781 : 2 : my_gather_mask = PGS_GATHER_MERGE;
782 : : else
783 [ # # # # ]: 0 : elog(ERROR, "unexpected advice tag: %d",
784 : : (int) entry->tag);
785 : 9 : }
786 : : else
787 : : {
788 [ + + ]: 30 : if (entry->tag == PGPA_TAG_PARTITIONWISE)
789 : : {
790 : 21 : my_partitionwise_outcome = -1;
791 : 21 : my_gather_mask = PGS_CONSIDER_NONPARTIAL;
792 : 21 : }
793 [ + + + - ]: 9 : else if (entry->tag == PGPA_TAG_GATHER ||
794 : 4 : entry->tag == PGPA_TAG_GATHER_MERGE)
795 : : {
796 : 9 : my_partitionwise_outcome = -1;
797 : 9 : my_gather_mask = PGS_CONSIDER_NONPARTIAL;
798 : 9 : }
799 : : else
800 [ # # # # ]: 0 : elog(ERROR, "unexpected advice tag: %d",
801 : : (int) entry->tag);
802 : : }
803 : 39 : }
804 : :
805 : : /*
806 : : * If we set my_gather_mask up above, then we (1) make a note if the
807 : : * advice conflicted, (2) remember the mask value, and (3) remember
808 : : * whether this was a full or partial match.
809 : : */
810 [ + + ]: 46 : if (my_gather_mask != 0)
811 : : {
812 [ + + + + ]: 42 : if (gather_mask != 0 && gather_mask != my_gather_mask)
813 : 1 : gather_conflict = true;
814 : 42 : gather_mask = my_gather_mask;
815 [ + + ]: 42 : if (full_match)
816 : 12 : gather_full_match = bms_add_member(gather_full_match, i);
817 : : else
818 : 30 : gather_partial_match = bms_add_member(gather_partial_match, i);
819 : 42 : }
820 : :
821 : : /*
822 : : * Likewise, if we set my_partitionwise_outcome up above, then we (1)
823 : : * make a note if the advice conflicted, (2) remember what the desired
824 : : * outcome was, and (3) remember whether this was a full or partial
825 : : * match.
826 : : */
827 [ + + ]: 46 : if (my_partitionwise_outcome != 0)
828 : : {
829 [ + + + + ]: 34 : if (partitionwise_outcome != 0 &&
830 : 14 : partitionwise_outcome != my_partitionwise_outcome)
831 : 2 : partitionwise_conflict = true;
832 : 34 : partitionwise_outcome = my_partitionwise_outcome;
833 [ + + ]: 34 : if (full_match)
834 : 4 : partitionwise_full_match =
835 : 4 : bms_add_member(partitionwise_full_match, i);
836 : : else
837 : 30 : partitionwise_partial_match =
838 : 30 : bms_add_member(partitionwise_partial_match, i);
839 : 34 : }
840 : 46 : }
841 : :
842 : : /*
843 : : * Mark every Gather-related piece of advice as partially matched, and if
844 : : * the set of targets exactly matched this relation, fully matched. If
845 : : * there was a conflict, mark them all as conflicting.
846 : : */
847 : 105 : flags = PGPA_TE_MATCH_PARTIAL;
848 [ + + ]: 105 : if (gather_conflict)
849 : 1 : flags |= PGPA_TE_CONFLICTING;
850 : 105 : pgpa_trove_set_flags(pjs->rel_entries, gather_partial_match, flags);
851 : 105 : flags |= PGPA_TE_MATCH_FULL;
852 : 105 : pgpa_trove_set_flags(pjs->rel_entries, gather_full_match, flags);
853 : :
854 : : /* Likewise for partitionwise advice. */
855 : 105 : flags = PGPA_TE_MATCH_PARTIAL;
856 [ + + ]: 105 : if (partitionwise_conflict)
857 : 2 : flags |= PGPA_TE_CONFLICTING;
858 : 105 : pgpa_trove_set_flags(pjs->rel_entries, partitionwise_partial_match, flags);
859 : 105 : flags |= PGPA_TE_MATCH_FULL;
860 : 105 : pgpa_trove_set_flags(pjs->rel_entries, partitionwise_full_match, flags);
861 : :
862 : : /* If there is a non-conflicting gather specification, enforce it. */
863 [ + + + + ]: 105 : if (gather_mask != 0 && !gather_conflict)
864 : : {
865 : 23 : *pgs_mask_p &=
866 : : ~(PGS_GATHER | PGS_GATHER_MERGE | PGS_CONSIDER_NONPARTIAL);
867 : 23 : *pgs_mask_p |= gather_mask;
868 : 23 : }
869 : :
870 : : /*
871 : : * If there is a non-conflicting partitionwise specification, enforce.
872 : : *
873 : : * To force a partitionwise join, we disable all the ordinary means of
874 : : * performing a join, and instead only Append and MergeAppend paths here.
875 : : * To prevent one, we just disable Append and MergeAppend. Note that we
876 : : * must not unset PGS_CONSIDER_PARTITIONWISE even when we don't want a
877 : : * partitionwise join here, because we might want one at a higher level
878 : : * that is constructing using paths from this level.
879 : : */
880 [ + + + + ]: 105 : if (partitionwise_outcome != 0 && !partitionwise_conflict)
881 : : {
882 [ + + ]: 18 : if (partitionwise_outcome > 0)
883 : 4 : *pgs_mask_p = (*pgs_mask_p & ~PGS_JOIN_ANY) |
884 : 2 : PGS_APPEND | PGS_MERGE_APPEND | PGS_CONSIDER_PARTITIONWISE;
885 : : else
886 : 16 : *pgs_mask_p &= ~(PGS_APPEND | PGS_MERGE_APPEND);
887 : 18 : }
888 : 105 : }
889 : :
890 : : /*
891 : : * Enforce restrictions on the join order or join method.
892 : : */
893 : : static void
894 : 288 : pgpa_planner_apply_join_path_advice(JoinType jointype, uint64 *pgs_mask_p,
895 : : char *plan_name,
896 : : pgpa_join_state *pjs)
897 : : {
898 : 288 : int i = -1;
899 : 288 : Bitmapset *jo_permit_indexes = NULL;
900 : 288 : Bitmapset *jo_deny_indexes = NULL;
901 : 288 : Bitmapset *jo_deny_rel_indexes = NULL;
902 : 288 : Bitmapset *jm_indexes = NULL;
903 : 288 : bool jm_conflict = false;
904 : 288 : uint32 join_mask = 0;
905 : 288 : Bitmapset *sj_permit_indexes = NULL;
906 : 288 : Bitmapset *sj_deny_indexes = NULL;
907 : :
908 : : /*
909 : : * Reconsider PARTITIONWISE(...) advice.
910 : : *
911 : : * We already thought about this for the joinrel as a whole, but in some
912 : : * cases, partitionwise advice can also constrain the join order. For
913 : : * instance, if the advice says PARTITIONWISE((t1 t2)), we shouldn't build
914 : : * join paths for a any joinrel that includes t1 or t2 unless it also
915 : : * includes the other. In general, the paritionwise operation must have
916 : : * already been completed within one side of the current join or the
917 : : * other, else the join order is impermissible.
918 : : *
919 : : * NB: It might seem tempting to try to deal with PARTITIONWISE advise
920 : : * entirely in this function, but that doesn't work. Here, we can only
921 : : * affect the pgs_mask within a particular JoinPathExtraData, that is, for
922 : : * a particular choice of innerrel and outerrel. Partitionwise paths are
923 : : * not built that way, so we must set pgs_mask for the RelOptInfo, which
924 : : * is best done in pgpa_planner_apply_joinrel_advice.
925 : : */
926 [ + + ]: 408 : while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
927 : : {
928 : 120 : pgpa_trove_entry *entry = &pjs->rel_entries[i];
929 : 120 : pgpa_itm_type inner_itm;
930 : 120 : pgpa_itm_type outer_itm;
931 : :
932 [ + + ]: 120 : if (entry->tag != PGPA_TAG_PARTITIONWISE)
933 : 42 : continue;
934 : :
935 : 156 : outer_itm = pgpa_identifiers_match_target(pjs->outer_count,
936 : 78 : pjs->rids, entry->target);
937 [ + + + + ]: 78 : if (outer_itm == PGPA_ITM_EQUAL ||
938 : 63 : outer_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
939 : 23 : continue;
940 : :
941 : 110 : inner_itm = pgpa_identifiers_match_target(pjs->inner_count,
942 : 55 : pjs->rids + pjs->outer_count,
943 : 55 : entry->target);
944 [ + + + + ]: 55 : if (inner_itm == PGPA_ITM_EQUAL ||
945 : 40 : inner_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
946 : 23 : continue;
947 : :
948 : 32 : jo_deny_rel_indexes = bms_add_member(jo_deny_rel_indexes, i);
949 [ + + ]: 120 : }
950 : :
951 : : /* Iterate over advice that pertains to the join order and method. */
952 : 288 : i = -1;
953 [ + + ]: 532 : while ((i = bms_next_member(pjs->join_indexes, i)) >= 0)
954 : : {
955 : 244 : pgpa_trove_entry *entry = &pjs->join_entries[i];
956 : 244 : uint32 my_join_mask;
957 : :
958 : : /* Handle join order advice. */
959 [ + + ]: 244 : if (entry->tag == PGPA_TAG_JOIN_ORDER)
960 : : {
961 [ + + + + ]: 316 : if (pgpa_join_order_permits_join(pjs->outer_count,
962 : 158 : pjs->inner_count,
963 : 158 : pjs->rids,
964 : 158 : entry))
965 : 35 : jo_permit_indexes = bms_add_member(jo_permit_indexes, i);
966 : : else
967 : 123 : jo_deny_indexes = bms_add_member(jo_deny_indexes, i);
968 : 158 : continue;
969 : : }
970 : :
971 : : /* Handle join method advice. */
972 : 86 : my_join_mask = pgpa_join_strategy_mask_from_advice_tag(entry->tag);
973 [ + + ]: 86 : if (my_join_mask != 0)
974 : : {
975 : 36 : bool permit;
976 : 36 : bool restrict_method;
977 : :
978 [ + + ]: 36 : if (entry->tag == PGPA_TAG_FOREIGN_JOIN)
979 : 4 : permit = pgpa_opaque_join_permits_join(pjs->outer_count,
980 : 2 : pjs->inner_count,
981 : 2 : pjs->rids,
982 : 2 : entry,
983 : : &restrict_method);
984 : : else
985 : 68 : permit = pgpa_join_method_permits_join(pjs->outer_count,
986 : 34 : pjs->inner_count,
987 : 34 : pjs->rids,
988 : 34 : entry,
989 : : &restrict_method);
990 [ + + ]: 36 : if (!permit)
991 : 17 : jo_deny_indexes = bms_add_member(jo_deny_indexes, i);
992 [ - + ]: 19 : else if (restrict_method)
993 : : {
994 : 19 : jm_indexes = bms_add_member(jm_indexes, i);
995 [ + + - + ]: 19 : if (join_mask != 0 && join_mask != my_join_mask)
996 : 1 : jm_conflict = true;
997 : 19 : join_mask = my_join_mask;
998 : 19 : }
999 : : continue;
1000 : 36 : }
1001 : :
1002 : : /* Handle semijoin uniqueness advice. */
1003 [ + + + - ]: 50 : if (entry->tag == PGPA_TAG_SEMIJOIN_UNIQUE ||
1004 : 24 : entry->tag == PGPA_TAG_SEMIJOIN_NON_UNIQUE)
1005 : : {
1006 : 50 : bool outer_side_nullable;
1007 : 50 : bool restrict_method;
1008 : :
1009 : : /* Planner has nullable side of the semijoin on the outer side? */
1010 [ + + ]: 50 : outer_side_nullable = (jointype == JOIN_UNIQUE_OUTER ||
1011 : 38 : jointype == JOIN_RIGHT_SEMI);
1012 : :
1013 [ + + + + ]: 100 : if (!pgpa_semijoin_permits_join(pjs->outer_count,
1014 : 50 : pjs->inner_count,
1015 : 50 : pjs->rids,
1016 : 50 : entry,
1017 : 50 : outer_side_nullable,
1018 : : &restrict_method))
1019 : 9 : jo_deny_indexes = bms_add_member(jo_deny_indexes, i);
1020 [ - + ]: 41 : else if (restrict_method)
1021 : : {
1022 : 41 : bool advice_unique;
1023 : 41 : bool jt_unique;
1024 : 41 : bool jt_non_unique;
1025 : :
1026 : : /* Advice wants to unique-ify and use a regular join? */
1027 : 41 : advice_unique = (entry->tag == PGPA_TAG_SEMIJOIN_UNIQUE);
1028 : :
1029 : : /* Planner is trying to unique-ify and use a regular join? */
1030 [ + + ]: 41 : jt_unique = (jointype == JOIN_UNIQUE_INNER ||
1031 : 31 : jointype == JOIN_UNIQUE_OUTER);
1032 : :
1033 : : /* Planner is trying a semi-join, without unique-ifying? */
1034 [ + + ]: 41 : jt_non_unique = (jointype == JOIN_SEMI ||
1035 : 31 : jointype == JOIN_RIGHT_SEMI);
1036 : :
1037 [ + + + + ]: 41 : if (!jt_unique && !jt_non_unique)
1038 : : {
1039 : : /*
1040 : : * This doesn't seem to be a semijoin to which SJ_UNIQUE
1041 : : * or SJ_NON_UNIQUE can be applied.
1042 : : */
1043 : 1 : entry->flags |= PGPA_TE_INAPPLICABLE;
1044 : 1 : }
1045 [ + + ]: 40 : else if (advice_unique != jt_unique)
1046 : 20 : sj_deny_indexes = bms_add_member(sj_deny_indexes, i);
1047 : : else
1048 : 20 : sj_permit_indexes = bms_add_member(sj_permit_indexes, i);
1049 : 41 : }
1050 : : continue;
1051 : 50 : }
1052 [ + - ]: 244 : }
1053 : :
1054 : : /*
1055 : : * If the advice indicates both that this join order is permissible and
1056 : : * also that it isn't, then mark advice related to the join order as
1057 : : * conflicting.
1058 : : */
1059 [ + + - + ]: 320 : if (jo_permit_indexes != NULL &&
1060 [ + + ]: 35 : (jo_deny_indexes != NULL || jo_deny_rel_indexes != NULL))
1061 : : {
1062 : 3 : pgpa_trove_set_flags(pjs->join_entries, jo_permit_indexes,
1063 : : PGPA_TE_CONFLICTING);
1064 : 3 : pgpa_trove_set_flags(pjs->join_entries, jo_deny_indexes,
1065 : : PGPA_TE_CONFLICTING);
1066 : 3 : pgpa_trove_set_flags(pjs->rel_entries, jo_deny_rel_indexes,
1067 : : PGPA_TE_CONFLICTING);
1068 : 3 : }
1069 : :
1070 : : /*
1071 : : * If more than one join method specification is relevant here and they
1072 : : * differ, mark them all as conflicting.
1073 : : */
1074 [ + + ]: 288 : if (jm_conflict)
1075 : 1 : pgpa_trove_set_flags(pjs->join_entries, jm_indexes,
1076 : : PGPA_TE_CONFLICTING);
1077 : :
1078 : : /* If semijoin advice says both yes and no, mark it all as conflicting. */
1079 [ + + + + ]: 288 : if (sj_permit_indexes != NULL && sj_deny_indexes != NULL)
1080 : : {
1081 : 4 : pgpa_trove_set_flags(pjs->join_entries, sj_permit_indexes,
1082 : : PGPA_TE_CONFLICTING);
1083 : 4 : pgpa_trove_set_flags(pjs->join_entries, sj_deny_indexes,
1084 : : PGPA_TE_CONFLICTING);
1085 : 4 : }
1086 : :
1087 : : /*
1088 : : * If we were advised to deny this join order, then do so. However, if we
1089 : : * were also advised to permit it, then do nothing, since the advice
1090 : : * conflicts.
1091 : : */
1092 [ + + + + ]: 288 : if ((jo_deny_indexes != NULL || jo_deny_rel_indexes != NULL) &&
1093 : 288 : jo_permit_indexes == NULL)
1094 : 169 : *pgs_mask_p = (*pgs_mask_p & ~PGS_JOIN_ANY) | join_mask;
1095 : :
1096 : : /*
1097 : : * If we were advised to restrict the join method, then do so. However, if
1098 : : * we got conflicting join method advice, then instead do nothing.
1099 : : */
1100 [ + + + + ]: 288 : if (join_mask != 0 && !jm_conflict)
1101 : 17 : *pgs_mask_p = (*pgs_mask_p & ~PGS_JOIN_ANY) | join_mask;
1102 : :
1103 : : /*
1104 : : * If we were advised to deny this due to semijoin-related advice, then do
1105 : : * so. However, if we got conflicting semijoin advice, then instead do
1106 : : * nothing.
1107 : : */
1108 [ + + + + ]: 288 : if (sj_deny_indexes != NULL && sj_permit_indexes == NULL)
1109 : 16 : *pgs_mask_p = (*pgs_mask_p & ~PGS_JOIN_ANY) | join_mask;
1110 : 288 : }
1111 : :
1112 : : /*
1113 : : * Translate an advice tag into a path generation strategy mask.
1114 : : *
1115 : : * This function can be called with tag types that don't represent join
1116 : : * strategies. In such cases, we just return 0, which can't be confused with
1117 : : * a valid mask.
1118 : : */
1119 : : static uint64
1120 : 86 : pgpa_join_strategy_mask_from_advice_tag(pgpa_advice_tag_type tag)
1121 : : {
1122 [ + + + + : 86 : switch (tag)
+ + + + ]
1123 : : {
1124 : : case PGPA_TAG_FOREIGN_JOIN:
1125 : 2 : return PGS_FOREIGNJOIN;
1126 : : case PGPA_TAG_MERGE_JOIN_PLAIN:
1127 : 4 : return PGS_MERGEJOIN_PLAIN;
1128 : : case PGPA_TAG_MERGE_JOIN_MATERIALIZE:
1129 : 4 : return PGS_MERGEJOIN_MATERIALIZE;
1130 : : case PGPA_TAG_NESTED_LOOP_PLAIN:
1131 : 10 : return PGS_NESTLOOP_PLAIN;
1132 : : case PGPA_TAG_NESTED_LOOP_MATERIALIZE:
1133 : 6 : return PGS_NESTLOOP_MATERIALIZE;
1134 : : case PGPA_TAG_NESTED_LOOP_MEMOIZE:
1135 : 4 : return PGS_NESTLOOP_MEMOIZE;
1136 : : case PGPA_TAG_HASH_JOIN:
1137 : 6 : return PGS_HASHJOIN;
1138 : : default:
1139 : 50 : return 0;
1140 : : }
1141 : 86 : }
1142 : :
1143 : : /*
1144 : : * Does a certain item of join order advice permit a certain join?
1145 : : */
1146 : : static bool
1147 : 158 : pgpa_join_order_permits_join(int outer_count, int inner_count,
1148 : : pgpa_identifier *rids,
1149 : : pgpa_trove_entry *entry)
1150 : : {
1151 : 158 : bool loop = true;
1152 : 158 : bool sublist = false;
1153 : 158 : int length;
1154 : 158 : int outer_length;
1155 : 158 : pgpa_advice_target *target = entry->target;
1156 : 158 : pgpa_advice_target *prefix_target;
1157 : :
1158 : : /* We definitely have at least a partial match for this trove entry. */
1159 : 158 : entry->flags |= PGPA_TE_MATCH_PARTIAL;
1160 : :
1161 : : /*
1162 : : * Find the innermost sublist that contains all keys; if no sublist does,
1163 : : * then continue processing with the toplevel list.
1164 : : *
1165 : : * For example, if the advice says JOIN_ORDER(t1 t2 (t3 t4 t5)), then we
1166 : : * should evaluate joins that only involve t3, t4, and/or t5 against the
1167 : : * (t3 t4 t5) sublist, and others against the full list.
1168 : : *
1169 : : * Note that (1) outermost sublist is always ordered and (2) whenever we
1170 : : * zoom into an unordered sublist, we instantly accept the proposed join.
1171 : : * If the advice says JOIN_ORDER(t1 t2 {t3 t4 t5}), any approach to
1172 : : * joining t3, t4, and/or t5 is acceptable.
1173 : : */
1174 [ + + ]: 318 : while (loop)
1175 : : {
1176 [ + - ]: 162 : Assert(target->ttype == PGPA_TARGET_ORDERED_LIST);
1177 : :
1178 : 162 : loop = false;
1179 [ + + + - : 704 : foreach_ptr(pgpa_advice_target, child_target, target->children)
+ + + + +
+ + + ]
1180 : : {
1181 : 382 : pgpa_itm_type itm;
1182 : :
1183 [ + + ]: 382 : if (child_target->ttype == PGPA_TARGET_IDENTIFIER)
1184 : 346 : continue;
1185 : :
1186 : 72 : itm = pgpa_identifiers_match_target(outer_count + inner_count,
1187 : 36 : rids, child_target);
1188 [ + + - + ]: 36 : if (itm == PGPA_ITM_EQUAL || itm == PGPA_ITM_KEYS_ARE_SUBSET)
1189 : : {
1190 [ + + ]: 6 : if (child_target->ttype == PGPA_TARGET_ORDERED_LIST)
1191 : : {
1192 : 4 : target = child_target;
1193 : 4 : sublist = true;
1194 : 4 : loop = true;
1195 : 4 : break;
1196 : : }
1197 : : else
1198 : : {
1199 [ - + ]: 2 : Assert(child_target->ttype == PGPA_TARGET_UNORDERED_LIST);
1200 : 2 : return true;
1201 : : }
1202 : : }
1203 [ + + + ]: 542 : }
1204 : : }
1205 : :
1206 : : /*
1207 : : * Try to find a prefix of the selected join order list that is exactly
1208 : : * equal to the outer side of the proposed join.
1209 : : */
1210 : 156 : length = list_length(target->children);
1211 : 156 : prefix_target = palloc0_object(pgpa_advice_target);
1212 : 156 : prefix_target->ttype = PGPA_TARGET_ORDERED_LIST;
1213 [ + + ]: 178 : for (outer_length = 1; outer_length <= length; ++outer_length)
1214 : : {
1215 : 175 : pgpa_itm_type itm;
1216 : :
1217 : : /* Avoid leaking memory in every loop iteration. */
1218 [ + + ]: 175 : if (prefix_target->children != NULL)
1219 : 19 : list_free(prefix_target->children);
1220 : 350 : prefix_target->children = list_copy_head(target->children,
1221 : 175 : outer_length);
1222 : :
1223 : : /* Search, hoping to find an exact match. */
1224 : 175 : itm = pgpa_identifiers_match_target(outer_count, rids, prefix_target);
1225 [ + + ]: 175 : if (itm == PGPA_ITM_EQUAL)
1226 : 47 : break;
1227 : :
1228 : : /*
1229 : : * If the prefix of the join order list that we're considering
1230 : : * includes some but not all of the outer rels, we can make the prefix
1231 : : * longer to find an exact match. But the advice hasn't mentioned
1232 : : * everything that's part of our outer rel yet, but has mentioned
1233 : : * things that are not, then this join doesn't match the join order
1234 : : * list.
1235 : : */
1236 [ + + ]: 128 : if (itm != PGPA_ITM_TARGETS_ARE_SUBSET)
1237 : 106 : return false;
1238 [ + + + ]: 175 : }
1239 : :
1240 : : /*
1241 : : * If the previous looped stopped before the prefix_target included the
1242 : : * entire join order list, then the next member of the join order list
1243 : : * must exactly match the inner side of the join.
1244 : : *
1245 : : * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), if the outer side of the
1246 : : * current join includes only t1, then the inner side must be exactly t2;
1247 : : * if the outer side includes both t1 and t2, then the inner side must
1248 : : * include exactly t3, t4, and t5.
1249 : : */
1250 [ + + ]: 50 : if (outer_length < length)
1251 : : {
1252 : 36 : pgpa_advice_target *inner_target;
1253 : 36 : pgpa_itm_type itm;
1254 : :
1255 : 36 : inner_target = list_nth(target->children, outer_length);
1256 : :
1257 : 72 : itm = pgpa_identifiers_match_target(inner_count, rids + outer_count,
1258 : 36 : inner_target);
1259 : :
1260 : : /*
1261 : : * Before returning, consider whether we need to mark this entry as
1262 : : * fully matched. If we're considering the full list rather than a
1263 : : * sublist, and if we found every item but one on the outer side of
1264 : : * the join and the last item on the inner side of the join, then the
1265 : : * answer is yes.
1266 : : */
1267 [ + + + + : 36 : if (!sublist && outer_length + 1 == length && itm == PGPA_ITM_EQUAL)
+ + ]
1268 : 10 : entry->flags |= PGPA_TE_MATCH_FULL;
1269 : :
1270 : 36 : return (itm == PGPA_ITM_EQUAL);
1271 : 36 : }
1272 : :
1273 : : /*
1274 : : * If we get here, then the outer side of the join includes the entirety
1275 : : * of the join order list. In this case, we behave differently depending
1276 : : * on whether we're looking at the top-level join order list or sublist.
1277 : : * At the top-level, we treat the specified list as mandating that the
1278 : : * actual join order has the given list as a prefix, but a sublist
1279 : : * requires an exact match.
1280 : : *
1281 : : * Exmaple: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), we must start by joining
1282 : : * all five of those relations and in that sequence, but once that is
1283 : : * done, it's OK to join any other rels that are part of the join problem.
1284 : : * This allows a user to specify the driving table and perhaps the first
1285 : : * few things to which it should be joined while leaving the rest of the
1286 : : * join order up the optimizer. But it seems like it would be surprising,
1287 : : * given that specification, if the user could add t6 to the (t3 t4 t5)
1288 : : * sub-join, so we don't allow that. If we did want to allow it, the logic
1289 : : * earlier in this function would require substantial adjustment: we could
1290 : : * allow the t3-t4-t5-t6 join to be built here, but the next step of
1291 : : * joining t1-t2 to the result would still be rejected.
1292 : : */
1293 [ - + ]: 14 : if (!sublist)
1294 : 14 : entry->flags |= PGPA_TE_MATCH_FULL;
1295 : 14 : return !sublist;
1296 : 158 : }
1297 : :
1298 : : /*
1299 : : * Does a certain item of join method advice permit a certain join?
1300 : : *
1301 : : * Advice such as HASH_JOIN((x y)) means that there should be a hash join with
1302 : : * exactly x and y on the inner side. Obviously, this means that if we are
1303 : : * considering a join with exactly x and y on the inner side, we should enforce
1304 : : * the use of a hash join. However, it also means that we must reject some
1305 : : * incompatible join orders entirely. For example, a join with exactly x
1306 : : * and y on the outer side shouldn't be allowed, because such paths might win
1307 : : * over the advice-driven path on cost.
1308 : : *
1309 : : * To accommodate these requirements, this function returns true if the join
1310 : : * should be allowed and false if it should not. Furthermore, *restrict_method
1311 : : * is set to true if the join method should be enforced and false if not.
1312 : : */
1313 : : static bool
1314 : 34 : pgpa_join_method_permits_join(int outer_count, int inner_count,
1315 : : pgpa_identifier *rids,
1316 : : pgpa_trove_entry *entry,
1317 : : bool *restrict_method)
1318 : : {
1319 : 34 : pgpa_advice_target *target = entry->target;
1320 : 34 : pgpa_itm_type inner_itm;
1321 : 34 : pgpa_itm_type outer_itm;
1322 : 34 : pgpa_itm_type join_itm;
1323 : :
1324 : : /* We definitely have at least a partial match for this trove entry. */
1325 : 34 : entry->flags |= PGPA_TE_MATCH_PARTIAL;
1326 : :
1327 : 34 : *restrict_method = false;
1328 : :
1329 : : /*
1330 : : * If our inner rel mentions exactly the same relations as the advice
1331 : : * target, allow the join and enforce the join method restriction.
1332 : : *
1333 : : * If our inner rel mentions a superset of the target relations, allow the
1334 : : * join. The join we care about has already taken place, and this advice
1335 : : * imposes no further restrictions.
1336 : : */
1337 : 68 : inner_itm = pgpa_identifiers_match_target(inner_count,
1338 : 34 : rids + outer_count,
1339 : 34 : target);
1340 [ + + ]: 34 : if (inner_itm == PGPA_ITM_EQUAL)
1341 : : {
1342 : 17 : entry->flags |= PGPA_TE_MATCH_FULL;
1343 : 17 : *restrict_method = true;
1344 : 17 : return true;
1345 : : }
1346 [ - + ]: 17 : else if (inner_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
1347 : 0 : return true;
1348 : :
1349 : : /*
1350 : : * If our outer rel mentions a supserset of the relations in the advice
1351 : : * target, no restrictions apply. The join we care has already taken
1352 : : * place, and this advice imposes no further restrictions.
1353 : : *
1354 : : * On the other hand, if our outer rel mentions exactly the relations
1355 : : * mentioned in the advice target, the planner is trying to reverse the
1356 : : * sides of the join as compared with our desired outcome. Reject that.
1357 : : */
1358 : 34 : outer_itm = pgpa_identifiers_match_target(outer_count,
1359 : 17 : rids, target);
1360 [ - + ]: 17 : if (outer_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
1361 : 0 : return true;
1362 [ - + ]: 17 : else if (outer_itm == PGPA_ITM_EQUAL)
1363 : 17 : return false;
1364 : :
1365 : : /*
1366 : : * If the advice target mentions only a single relation, the test below
1367 : : * cannot ever pass, so save some work by exiting now.
1368 : : */
1369 [ # # ]: 0 : if (target->ttype == PGPA_TARGET_IDENTIFIER)
1370 : 0 : return false;
1371 : :
1372 : : /*
1373 : : * If everything in the joinrel appears in the advice target, we're below
1374 : : * the level of the join we want to control.
1375 : : *
1376 : : * For example, HASH_JOIN((x y)) doesn't restrict how x and y can be
1377 : : * joined.
1378 : : *
1379 : : * This lookup shouldn't return PGPA_ITM_DISJOINT, because any such advice
1380 : : * should not have been returned from the trove in the first place.
1381 : : */
1382 : 0 : join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1383 : 0 : rids, target);
1384 [ # # ]: 0 : Assert(join_itm != PGPA_ITM_DISJOINT);
1385 [ # # # # ]: 0 : if (join_itm == PGPA_ITM_KEYS_ARE_SUBSET ||
1386 : 0 : join_itm == PGPA_ITM_EQUAL)
1387 : 0 : return true;
1388 : :
1389 : : /*
1390 : : * We've already permitted all allowable cases, so reject this.
1391 : : *
1392 : : * If we reach this point, then the advice overlaps with this join but
1393 : : * isn't entirely contained within either side, and there's also at least
1394 : : * one relation present in the join that isn't mentioned by the advice.
1395 : : *
1396 : : * For instance, in the HASH_JOIN((x y)) example, we would reach here if x
1397 : : * were on one side of the join, y on the other, and at least one of the
1398 : : * two sides also included some other relation, say t. In that case,
1399 : : * accepting this join would allow the (x y t) joinrel to contain
1400 : : * non-disabled paths that do not put (x y) on the inner side of a hash
1401 : : * join; we could instead end up with something like (x JOIN t) JOIN y.
1402 : : */
1403 : 0 : return false;
1404 : 34 : }
1405 : :
1406 : : /*
1407 : : * Does advice concerning an opaque join permit a certain join?
1408 : : *
1409 : : * By an opaque join, we mean one where the exact mechanism by which the
1410 : : * join is performed is not visible to PostgreSQL. Currently this is the
1411 : : * case only for foreign joins: FOREIGN_JOIN((x y z)) means that x, y, and
1412 : : * z are joined on the remote side, but we know nothing about the join order
1413 : : * or join methods used over there.
1414 : : *
1415 : : * The logic here needs to differ from pgpa_join_method_permits_join because,
1416 : : * for other join types, the advice target is the set of inner rels; here, it
1417 : : * includes both inner and outer rels.
1418 : : */
1419 : : static bool
1420 : 2 : pgpa_opaque_join_permits_join(int outer_count, int inner_count,
1421 : : pgpa_identifier *rids,
1422 : : pgpa_trove_entry *entry,
1423 : : bool *restrict_method)
1424 : : {
1425 : 2 : pgpa_advice_target *target = entry->target;
1426 : 2 : pgpa_itm_type join_itm;
1427 : :
1428 : : /* We definitely have at least a partial match for this trove entry. */
1429 : 2 : entry->flags |= PGPA_TE_MATCH_PARTIAL;
1430 : :
1431 : 2 : *restrict_method = false;
1432 : :
1433 : 4 : join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1434 : 2 : rids, target);
1435 [ - + ]: 2 : if (join_itm == PGPA_ITM_EQUAL)
1436 : : {
1437 : : /*
1438 : : * We have an exact match, and should therefore allow the join and
1439 : : * enforce the use of the relevant opaque join method.
1440 : : */
1441 : 2 : entry->flags |= PGPA_TE_MATCH_FULL;
1442 : 2 : *restrict_method = true;
1443 : 2 : return true;
1444 : : }
1445 : :
1446 [ # # # # ]: 0 : if (join_itm == PGPA_ITM_KEYS_ARE_SUBSET ||
1447 : 0 : join_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
1448 : : {
1449 : : /*
1450 : : * If join_itm == PGPA_ITM_TARGETS_ARE_SUBSET, then the join we care
1451 : : * about has already taken place and no further restrictions apply.
1452 : : *
1453 : : * If join_itm == PGPA_ITM_KEYS_ARE_SUBSET, we're still building up to
1454 : : * the join we care about and have not introduced any extraneous
1455 : : * relations not named in the advice. Note that ForeignScan paths for
1456 : : * joins are built up from ForeignScan paths from underlying joins and
1457 : : * scans, so we must not disable this join when considering a subset
1458 : : * of the relations we ultimately want.
1459 : : */
1460 : 0 : return true;
1461 : : }
1462 : :
1463 : : /*
1464 : : * The advice overlaps the join, but at least one relation is present in
1465 : : * the join that isn't mentioned by the advice. We want to disable such
1466 : : * paths so that we actually push down the join as intended.
1467 : : */
1468 : 0 : return false;
1469 : 2 : }
1470 : :
1471 : : /*
1472 : : * Does advice concerning a semijoin permit a certain join?
1473 : : *
1474 : : * Unlike join method advice, which lists the rels on the inner side of the
1475 : : * join, semijoin uniqueness advice lists the rels on the nullable side of the
1476 : : * join. Those can be the same, if the join type is JOIN_UNIQUE_INNER or
1477 : : * JOIN_SEMI, or they can be different, in case of JOIN_UNIQUE_OUTER or
1478 : : * JOIN_RIGHT_SEMI.
1479 : : *
1480 : : * We don't know here whether the caller specified SEMIJOIN_UNIQUE or
1481 : : * SEMIJOIN_NON_UNIQUE. The caller should check the join type against the
1482 : : * advice type if and only if we set *restrict_method to true.
1483 : : */
1484 : : static bool
1485 : 50 : pgpa_semijoin_permits_join(int outer_count, int inner_count,
1486 : : pgpa_identifier *rids,
1487 : : pgpa_trove_entry *entry,
1488 : : bool outer_is_nullable,
1489 : : bool *restrict_method)
1490 : : {
1491 : 50 : pgpa_advice_target *target = entry->target;
1492 : 50 : pgpa_itm_type join_itm;
1493 : 50 : pgpa_itm_type inner_itm;
1494 : 50 : pgpa_itm_type outer_itm;
1495 : :
1496 : 50 : *restrict_method = false;
1497 : :
1498 : : /* We definitely have at least a partial match for this trove entry. */
1499 : 50 : entry->flags |= PGPA_TE_MATCH_PARTIAL;
1500 : :
1501 : : /*
1502 : : * If outer rel is the nullable side and contains exactly the same
1503 : : * relations as the advice target, then the join order is allowable, but
1504 : : * the caller must check whether the advice tag (either SEMIJOIN_UNIQUE or
1505 : : * SEMIJOIN_NON_UNIQUE) matches the join type.
1506 : : *
1507 : : * If the outer rel is a superset of the target relations, the join we
1508 : : * care about has already taken place, so we should impose no futher
1509 : : * restritions.
1510 : : */
1511 : 100 : outer_itm = pgpa_identifiers_match_target(outer_count,
1512 : 50 : rids, target);
1513 [ + + ]: 50 : if (outer_itm == PGPA_ITM_EQUAL)
1514 : : {
1515 : 25 : entry->flags |= PGPA_TE_MATCH_FULL;
1516 [ + + ]: 25 : if (outer_is_nullable)
1517 : : {
1518 : 20 : *restrict_method = true;
1519 : 20 : return true;
1520 : : }
1521 : 5 : }
1522 [ - + ]: 25 : else if (outer_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
1523 : 0 : return true;
1524 : :
1525 : : /* As above, but for the inner rel. */
1526 : 60 : inner_itm = pgpa_identifiers_match_target(inner_count,
1527 : 30 : rids + outer_count,
1528 : 30 : target);
1529 [ + + ]: 30 : if (inner_itm == PGPA_ITM_EQUAL)
1530 : : {
1531 : 25 : entry->flags |= PGPA_TE_MATCH_FULL;
1532 [ + + ]: 25 : if (!outer_is_nullable)
1533 : : {
1534 : 21 : *restrict_method = true;
1535 : 21 : return true;
1536 : : }
1537 : 4 : }
1538 [ - + ]: 5 : else if (inner_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
1539 : 0 : return true;
1540 : :
1541 : : /*
1542 : : * If everything in the joinrel appears in the advice target, we're below
1543 : : * the level of the join we want to control.
1544 : : */
1545 : 18 : join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1546 : 9 : rids, target);
1547 [ + - ]: 9 : Assert(join_itm != PGPA_ITM_DISJOINT);
1548 [ + - - + ]: 9 : if (join_itm == PGPA_ITM_KEYS_ARE_SUBSET ||
1549 : 9 : join_itm == PGPA_ITM_EQUAL)
1550 : 0 : return true;
1551 : :
1552 : : /*
1553 : : * We've tested for all allowable possibilities, and so must reject this
1554 : : * join order. This can happen in two ways.
1555 : : *
1556 : : * First, we migh be considering a semijoin that overlaps incompletely
1557 : : * with one or both sides of the join. For example, if the user has
1558 : : * specified SEMIJOIN_UNIQUE((t1 t2)) or SEMIJOIN_NON_UNIQUE((t1 t2)), we
1559 : : * should reject a proposed t2-t3 join, since that could not result in a
1560 : : * final plan compatible with the advice.
1561 : : *
1562 : : * Second, we might be considering a semijoin where the advice target
1563 : : * perfectly matches one side of the join, but it's the wrong one. For
1564 : : * example, in the example above, we might see a 3-way join between t1,
1565 : : * t2, and t3, with (t1 t2) on the non-nullable side. That, too, would be
1566 : : * incompatible with the advice.
1567 : : */
1568 : 9 : return false;
1569 : 50 : }
1570 : :
1571 : : /*
1572 : : * Apply scan advice to a RelOptInfo.
1573 : : *
1574 : : * XXX. For bitmap heap scans, we're just ignoring the index information from
1575 : : * the advice. That's not cool.
1576 : : */
1577 : : static void
1578 : 72 : pgpa_planner_apply_scan_advice(RelOptInfo *rel,
1579 : : pgpa_trove_entry *scan_entries,
1580 : : Bitmapset *scan_indexes,
1581 : : pgpa_trove_entry *rel_entries,
1582 : : Bitmapset *rel_indexes)
1583 : : {
1584 : 72 : bool gather_conflict = false;
1585 : 72 : Bitmapset *gather_partial_match = NULL;
1586 : 72 : Bitmapset *gather_full_match = NULL;
1587 : 72 : int i = -1;
1588 : 72 : pgpa_trove_entry *scan_entry = NULL;
1589 : 72 : int flags;
1590 : 72 : bool scan_type_conflict = false;
1591 : 72 : Bitmapset *scan_type_indexes = NULL;
1592 : 72 : Bitmapset *scan_type_rel_indexes = NULL;
1593 : 72 : uint64 gather_mask = 0;
1594 : 72 : uint64 scan_type = 0;
1595 : :
1596 : : /* Scrutinize available scan advice. */
1597 [ + + ]: 110 : while ((i = bms_next_member(scan_indexes, i)) >= 0)
1598 : : {
1599 : 38 : pgpa_trove_entry *my_entry = &scan_entries[i];
1600 : 38 : uint64 my_scan_type = 0;
1601 : :
1602 : : /* Translate our advice tags to a scan strategy advice value. */
1603 [ + + ]: 38 : if (my_entry->tag == PGPA_TAG_BITMAP_HEAP_SCAN)
1604 : 3 : my_scan_type = PGS_BITMAPSCAN;
1605 [ + + ]: 35 : else if (my_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN)
1606 : 5 : my_scan_type = PGS_INDEXONLYSCAN | PGS_CONSIDER_INDEXONLY;
1607 [ + + ]: 30 : else if (my_entry->tag == PGPA_TAG_INDEX_SCAN)
1608 : 14 : my_scan_type = PGS_INDEXSCAN;
1609 [ + + ]: 16 : else if (my_entry->tag == PGPA_TAG_SEQ_SCAN)
1610 : 12 : my_scan_type = PGS_SEQSCAN;
1611 [ - + ]: 4 : else if (my_entry->tag == PGPA_TAG_TID_SCAN)
1612 : 4 : my_scan_type = PGS_TIDSCAN;
1613 : :
1614 : : /*
1615 : : * If this is understandable scan advice, hang on to the entry, the
1616 : : * inferred scan type type, and the index at which we found it.
1617 : : *
1618 : : * Also make a note if we see conflicting scan type advice. Note that
1619 : : * we regard two index specifications as conflicting unless they match
1620 : : * exactly. In theory, perhaps we could regard INDEX_SCAN(a c) and
1621 : : * INDEX_SCAN(a b.c) as non-conflicting if it happens that the only
1622 : : * index named c is in schema b, but it doesn't seem worth the code.
1623 : : */
1624 [ + - ]: 38 : if (my_scan_type != 0)
1625 : : {
1626 [ + + + - ]: 38 : if (scan_type != 0 && scan_type != my_scan_type)
1627 : 0 : scan_type_conflict = true;
1628 [ + - + + ]: 38 : if (!scan_type_conflict && scan_entry != NULL &&
1629 [ + - ]: 2 : my_entry->target->itarget != NULL &&
1630 [ + - + + ]: 2 : scan_entry->target->itarget != NULL &&
1631 : 4 : !pgpa_index_targets_equal(scan_entry->target->itarget,
1632 : 2 : my_entry->target->itarget))
1633 : 1 : scan_type_conflict = true;
1634 : 38 : scan_entry = my_entry;
1635 : 38 : scan_type = my_scan_type;
1636 : 38 : scan_type_indexes = bms_add_member(scan_type_indexes, i);
1637 : 38 : }
1638 : 38 : }
1639 : :
1640 : : /* Scrutinize available gather-related and partitionwise advice. */
1641 : 72 : i = -1;
1642 [ + + ]: 110 : while ((i = bms_next_member(rel_indexes, i)) >= 0)
1643 : : {
1644 : 38 : pgpa_trove_entry *my_entry = &rel_entries[i];
1645 : 38 : uint64 my_gather_mask = 0;
1646 : 38 : bool just_one_rel;
1647 : :
1648 : 57 : just_one_rel = my_entry->target->ttype == PGPA_TARGET_IDENTIFIER
1649 [ + + ]: 38 : || list_length(my_entry->target->children) == 1;
1650 : :
1651 : : /*
1652 : : * PARTITIONWISE behaves like a scan type, except that if there's more
1653 : : * than one relation targeted, it has no effect at this level.
1654 : : */
1655 [ + + ]: 38 : if (my_entry->tag == PGPA_TAG_PARTITIONWISE)
1656 : : {
1657 [ + + ]: 12 : if (just_one_rel)
1658 : : {
1659 : 4 : const uint64 my_scan_type = PGS_APPEND | PGS_MERGE_APPEND;
1660 : :
1661 [ - + # # ]: 4 : if (scan_type != 0 && scan_type != my_scan_type)
1662 : 0 : scan_type_conflict = true;
1663 : 4 : scan_entry = my_entry;
1664 : 4 : scan_type = my_scan_type;
1665 : 4 : scan_type_rel_indexes =
1666 : 4 : bms_add_member(scan_type_rel_indexes, i);
1667 : 4 : }
1668 : 12 : continue;
1669 : : }
1670 : :
1671 : : /*
1672 : : * GATHER and GATHER_MERGE applied to a single rel mean that we should
1673 : : * use the correspondings strategy here, while applying either to more
1674 : : * than one rel means we should not use those strategies here, but
1675 : : * rather at the level of the joinrel that corresponds to what was
1676 : : * specified. NO_GATHER can only be applied to single rels.
1677 : : *
1678 : : * Note that setting PGS_CONSIDER_NONPARTIAL in my_gather_mask is
1679 : : * equivalent to allowing the non-use of either form of Gather here.
1680 : : */
1681 [ + + + + ]: 26 : if (my_entry->tag == PGPA_TAG_GATHER ||
1682 : 15 : my_entry->tag == PGPA_TAG_GATHER_MERGE)
1683 : : {
1684 [ + + ]: 19 : if (!just_one_rel)
1685 : 11 : my_gather_mask = PGS_CONSIDER_NONPARTIAL;
1686 [ + + ]: 8 : else if (my_entry->tag == PGPA_TAG_GATHER)
1687 : 4 : my_gather_mask = PGS_GATHER;
1688 : : else
1689 : 4 : my_gather_mask = PGS_GATHER_MERGE;
1690 : 19 : }
1691 [ - + ]: 7 : else if (my_entry->tag == PGPA_TAG_NO_GATHER)
1692 : : {
1693 [ - + ]: 7 : Assert(just_one_rel);
1694 : 7 : my_gather_mask = PGS_CONSIDER_NONPARTIAL;
1695 : 7 : }
1696 : :
1697 : : /*
1698 : : * If we set my_gather_mask up above, then we (1) make a note if the
1699 : : * advice conflicted, (2) remember the mask value, and (3) remember
1700 : : * whether this was a full or partial match.
1701 : : */
1702 [ - + ]: 26 : if (my_gather_mask != 0)
1703 : : {
1704 [ + + + - ]: 26 : if (gather_mask != 0 && gather_mask != my_gather_mask)
1705 : 0 : gather_conflict = true;
1706 : 26 : gather_mask = my_gather_mask;
1707 [ + + ]: 26 : if (just_one_rel)
1708 : 15 : gather_full_match = bms_add_member(gather_full_match, i);
1709 : : else
1710 : 11 : gather_partial_match = bms_add_member(gather_partial_match, i);
1711 : 26 : }
1712 [ - + + ]: 38 : }
1713 : :
1714 : : /* Enforce choice of index. */
1715 [ + + + + : 100 : if (scan_entry != NULL && !scan_type_conflict &&
+ + ]
1716 [ + + ]: 39 : (scan_entry->tag == PGPA_TAG_INDEX_SCAN ||
1717 : 28 : scan_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN))
1718 : : {
1719 : 16 : pgpa_index_target *itarget = scan_entry->target->itarget;
1720 : 16 : IndexOptInfo *matched_index = NULL;
1721 : :
1722 [ + - ]: 16 : Assert(itarget->itype == PGPA_INDEX_NAME);
1723 : :
1724 [ + + + - : 62 : foreach_node(IndexOptInfo, index, rel->indexlist)
+ + + + ]
1725 : : {
1726 : 30 : char *relname = get_rel_name(index->indexoid);
1727 : 30 : Oid nspoid = get_rel_namespace(index->indexoid);
1728 : 30 : char *relnamespace = get_namespace_name(nspoid);
1729 : :
1730 [ + + + + ]: 33 : if (strcmp(itarget->indname, relname) == 0 &&
1731 [ + + ]: 14 : (itarget->indnamespace == NULL ||
1732 : 3 : strcmp(itarget->indnamespace, relnamespace) == 0))
1733 : : {
1734 : 13 : matched_index = index;
1735 : 13 : break;
1736 : : }
1737 [ + + ]: 46 : }
1738 : :
1739 [ + + ]: 16 : if (matched_index == NULL)
1740 : : {
1741 : : /* Don't force the scan type if the index doesn't exist. */
1742 : 3 : scan_type = 0;
1743 : :
1744 : : /* Mark advice as inapplicable. */
1745 : 3 : pgpa_trove_set_flags(scan_entries, scan_type_indexes,
1746 : : PGPA_TE_INAPPLICABLE);
1747 : 3 : }
1748 : : else
1749 : : {
1750 : : /* Retain this index and discard the rest. */
1751 : 13 : rel->indexlist = list_make1(matched_index);
1752 : : }
1753 : 16 : }
1754 : :
1755 : : /*
1756 : : * Mark all the scan method entries as fully matched; and if they specify
1757 : : * different things, mark them all as conflicting.
1758 : : */
1759 : 72 : flags = PGPA_TE_MATCH_PARTIAL | PGPA_TE_MATCH_FULL;
1760 [ + + ]: 72 : if (scan_type_conflict)
1761 : 1 : flags |= PGPA_TE_CONFLICTING;
1762 : 72 : pgpa_trove_set_flags(scan_entries, scan_type_indexes, flags);
1763 : 72 : pgpa_trove_set_flags(rel_entries, scan_type_rel_indexes, flags);
1764 : :
1765 : : /*
1766 : : * Mark every Gather-related piece of advice as partially matched. Mark
1767 : : * the ones that included this relation as a target by itself as fully
1768 : : * matched. If there was a conflict, mark them all as conflicting.
1769 : : */
1770 : 72 : flags = PGPA_TE_MATCH_PARTIAL;
1771 [ + - ]: 72 : if (gather_conflict)
1772 : 0 : flags |= PGPA_TE_CONFLICTING;
1773 : 72 : pgpa_trove_set_flags(rel_entries, gather_partial_match, flags);
1774 : 72 : flags |= PGPA_TE_MATCH_FULL;
1775 : 72 : pgpa_trove_set_flags(rel_entries, gather_full_match, flags);
1776 : :
1777 : : /* If there is a non-conflicting scan specification, enforce it. */
1778 [ + + + + ]: 72 : if (scan_type != 0 && !scan_type_conflict)
1779 : : {
1780 : 36 : rel->pgs_mask &=
1781 : : ~(PGS_SCAN_ANY | PGS_APPEND | PGS_MERGE_APPEND |
1782 : : PGS_CONSIDER_INDEXONLY);
1783 : 36 : rel->pgs_mask |= scan_type;
1784 : 36 : }
1785 : :
1786 : : /* If there is a non-conflicting gather specification, enforce it. */
1787 [ + + - + ]: 72 : if (gather_mask != 0 && !gather_conflict)
1788 : : {
1789 : 25 : rel->pgs_mask &=
1790 : : ~(PGS_GATHER | PGS_GATHER_MERGE | PGS_CONSIDER_NONPARTIAL);
1791 : 25 : rel->pgs_mask |= gather_mask;
1792 : 25 : }
1793 : 72 : }
1794 : :
1795 : : /*
1796 : : * Add feedback entries to for one trove slice to the provided list and
1797 : : * return the resulting list.
1798 : : *
1799 : : * Feedback entries are generated from the trove entry's flags. It's assumed
1800 : : * that the caller has already set all relevant flags with the exception of
1801 : : * PGPA_TE_FAILED. We set that flag here if appropriate.
1802 : : */
1803 : : static List *
1804 : 10593 : pgpa_planner_append_feedback(List *list, pgpa_trove *trove,
1805 : : pgpa_trove_lookup_type type,
1806 : : pgpa_identifier *rt_identifiers,
1807 : : pgpa_plan_walker_context *walker)
1808 : : {
1809 : 10593 : pgpa_trove_entry *entries;
1810 : 10593 : int nentries;
1811 : 10593 : StringInfoData buf;
1812 : :
1813 : 10593 : initStringInfo(&buf);
1814 : 10593 : pgpa_trove_lookup_all(trove, type, &entries, &nentries);
1815 [ + + ]: 20980 : for (int i = 0; i < nentries; ++i)
1816 : : {
1817 : 10387 : pgpa_trove_entry *entry = &entries[i];
1818 : 10387 : DefElem *item;
1819 : :
1820 : : /*
1821 : : * If this entry was fully matched, check whether generating advice
1822 : : * from this plan would produce such an entry. If not, label the entry
1823 : : * as failed.
1824 : : */
1825 [ + + + + ]: 10387 : if ((entry->flags & PGPA_TE_MATCH_FULL) != 0 &&
1826 : 226 : !pgpa_walker_would_advise(walker, rt_identifiers,
1827 : 113 : entry->tag, entry->target))
1828 : 30 : entry->flags |= PGPA_TE_FAILED;
1829 : :
1830 : 20774 : item = makeDefElem(pgpa_cstring_trove_entry(entry),
1831 : 10387 : (Node *) makeInteger(entry->flags), -1);
1832 : 10387 : list = lappend(list, item);
1833 : 10387 : }
1834 : :
1835 : 21186 : return list;
1836 : 10593 : }
1837 : :
1838 : : #ifdef USE_ASSERT_CHECKING
1839 : :
1840 : : /*
1841 : : * Fast hash function for a key consisting of an RTI and plan name.
1842 : : */
1843 : : static uint32
1844 : 47309 : pgpa_ri_checker_hash_key(pgpa_ri_checker_key key)
1845 : : {
1846 : 47309 : fasthash_state hs;
1847 : 47309 : int sp_len;
1848 : :
1849 : 47309 : fasthash_init(&hs, 0);
1850 : :
1851 : 47309 : hs.accum = key.rti;
1852 : 47309 : fasthash_combine(&hs);
1853 : :
1854 : : /* plan_name can be NULL */
1855 [ + + ]: 47309 : if (key.plan_name == NULL)
1856 : 37436 : sp_len = 0;
1857 : : else
1858 : 9873 : sp_len = fasthash_accum_cstring(&hs, key.plan_name);
1859 : :
1860 : : /* hashfn_unstable.h recommends using string length as tweak */
1861 : 94618 : return fasthash_final32(&hs, sp_len);
1862 : 47309 : }
1863 : :
1864 : : #endif
1865 : :
1866 : : /*
1867 : : * Save the range table identifier for one relation for future cross-checking.
1868 : : */
1869 : : static void
1870 : 47309 : pgpa_ri_checker_save(pgpa_planner_state *pps, PlannerInfo *root,
1871 : : RelOptInfo *rel)
1872 : : {
1873 : : #ifdef USE_ASSERT_CHECKING
1874 : 47309 : pgpa_ri_checker_key key;
1875 : 47309 : pgpa_ri_checker *check;
1876 : 47309 : pgpa_identifier rid;
1877 : 47309 : const char *rid_string;
1878 : 47309 : bool found;
1879 : :
1880 : 47309 : key.rti = bms_singleton_member(rel->relids);
1881 : 47309 : key.plan_name = root->plan_name;
1882 : 47309 : pgpa_compute_identifier_by_rti(root, key.rti, &rid);
1883 : 47309 : rid_string = pgpa_identifier_string(&rid);
1884 : 47309 : check = pgpa_ri_check_insert(pps->ri_check_hash, key, &found);
1885 [ - + # # ]: 47309 : Assert(!found || strcmp(check->rid_string, rid_string) == 0);
1886 : 47309 : check->rid_string = rid_string;
1887 : : #endif
1888 : 47309 : }
1889 : :
1890 : : /*
1891 : : * Validate that the range table identifiers we were able to generate during
1892 : : * planning match the ones we generated from the final plan.
1893 : : */
1894 : : static void
1895 : 43570 : pgpa_ri_checker_validate(pgpa_planner_state *pps, PlannedStmt *pstmt)
1896 : : {
1897 : : #ifdef USE_ASSERT_CHECKING
1898 : 43570 : pgpa_identifier *rt_identifiers;
1899 : 43570 : pgpa_ri_check_iterator it;
1900 : 43570 : pgpa_ri_checker *check;
1901 : :
1902 : : /* Create identifiers from the planned statement. */
1903 : 43570 : rt_identifiers = pgpa_create_identifiers_for_planned_stmt(pstmt);
1904 : :
1905 : : /* Iterate over identifiers created during planning, so we can compare. */
1906 : 43570 : pgpa_ri_check_start_iterate(pps->ri_check_hash, &it);
1907 [ + + ]: 90873 : while ((check = pgpa_ri_check_iterate(pps->ri_check_hash, &it)) != NULL)
1908 : : {
1909 : 47303 : int rtoffset = 0;
1910 : 47303 : const char *rid_string;
1911 : 47303 : Index flat_rti;
1912 : :
1913 : : /*
1914 : : * If there's no plan name associated with this entry, then the
1915 : : * rtoffset is 0. Otherwise, we can search the SubPlanRTInfo list to
1916 : : * find the rtoffset.
1917 : : */
1918 [ + + ]: 47303 : if (check->key.plan_name != NULL)
1919 : : {
1920 [ + + + + : 36895 : foreach_node(SubPlanRTInfo, rtinfo, pstmt->subrtinfos)
+ + + + ]
1921 : : {
1922 : : /*
1923 : : * If rtinfo->dummy is set, then the subquery's range table
1924 : : * will only have been partially copied to the final range
1925 : : * table. Specifically, only RTE_RELATION entries and
1926 : : * RTE_SUBQUERY entries that were once RTE_RELATION entries
1927 : : * will be copied, as per add_rtes_to_flat_rtable. Therefore,
1928 : : * there's no fixed rtoffset that we can apply to the RTIs
1929 : : * used during planning to locate the corresponding relations
1930 : : * in the final rtable.
1931 : : *
1932 : : * With more complex logic, we could work around that problem
1933 : : * by remembering the whole contents of the subquery's rtable
1934 : : * during planning, determining which of those would have been
1935 : : * copied to the final rtable, and matching them up. But it
1936 : : * doesn't seem like a worthwhile endeavor for right now,
1937 : : * because RTIs from such subqueries won't appear in the plan
1938 : : * tree itself, just in the range table. Hence, we can neither
1939 : : * generate nor accept advice for them.
1940 : : */
1941 : 17149 : if (strcmp(check->key.plan_name, rtinfo->plan_name) == 0
1942 [ + + + + ]: 17149 : && !rtinfo->dummy)
1943 : : {
1944 : 9668 : rtoffset = rtinfo->rtoffset;
1945 [ - + ]: 9668 : Assert(rtoffset > 0);
1946 : 9668 : break;
1947 : : }
1948 : 17354 : }
1949 : :
1950 : : /*
1951 : : * It's not an error if we don't find the plan name: that just
1952 : : * means that we planned a subplan by this name but it ended up
1953 : : * being a dummy subplan and so wasn't included in the final plan
1954 : : * tree.
1955 : : */
1956 [ + + ]: 9873 : if (rtoffset == 0)
1957 : 205 : continue;
1958 : 9668 : }
1959 : :
1960 : : /*
1961 : : * check->key.rti is the RTI that we saw prior to range-table
1962 : : * flattening, so we must add the appropriate RT offset to get the
1963 : : * final RTI.
1964 : : */
1965 : 47098 : flat_rti = check->key.rti + rtoffset;
1966 [ - + ]: 47098 : Assert(flat_rti <= list_length(pstmt->rtable));
1967 : :
1968 : : /* Assert that the string we compute now matches the previous one. */
1969 : 47098 : rid_string = pgpa_identifier_string(&rt_identifiers[flat_rti - 1]);
1970 [ - + ]: 47098 : Assert(strcmp(rid_string, check->rid_string) == 0);
1971 [ - + + ]: 47303 : }
1972 : : #endif
1973 : 43570 : }
1974 : :
1975 : : /*
1976 : : * Convert a bitmapset to a C string of comma-separated integers.
1977 : : */
1978 : : static char *
1979 : 0 : pgpa_bms_to_cstring(Bitmapset *bms)
1980 : : {
1981 : 0 : StringInfoData buf;
1982 : 0 : int x = -1;
1983 : :
1984 [ # # ]: 0 : if (bms_is_empty(bms))
1985 : 0 : return "none";
1986 : :
1987 : 0 : initStringInfo(&buf);
1988 [ # # ]: 0 : while ((x = bms_next_member(bms, x)) >= 0)
1989 : : {
1990 [ # # ]: 0 : if (buf.len > 0)
1991 : 0 : appendStringInfo(&buf, ", %d", x);
1992 : : else
1993 : 0 : appendStringInfo(&buf, "%d", x);
1994 : : }
1995 : :
1996 : 0 : return buf.data;
1997 : 0 : }
1998 : :
1999 : : /*
2000 : : * Convert a JoinType to a C string.
2001 : : */
2002 : : static const char *
2003 : 0 : pgpa_jointype_to_cstring(JoinType jointype)
2004 : : {
2005 [ # # # # : 0 : switch (jointype)
# # # # #
# # ]
2006 : : {
2007 : : case JOIN_INNER:
2008 : 0 : return "inner";
2009 : : case JOIN_LEFT:
2010 : 0 : return "left";
2011 : : case JOIN_FULL:
2012 : 0 : return "full";
2013 : : case JOIN_RIGHT:
2014 : 0 : return "right";
2015 : : case JOIN_SEMI:
2016 : 0 : return "semi";
2017 : : case JOIN_ANTI:
2018 : 0 : return "anti";
2019 : : case JOIN_RIGHT_SEMI:
2020 : 0 : return "right semi";
2021 : : case JOIN_RIGHT_ANTI:
2022 : 0 : return "right anti";
2023 : : case JOIN_UNIQUE_OUTER:
2024 : 0 : return "unique outer";
2025 : : case JOIN_UNIQUE_INNER:
2026 : 0 : return "unique inner";
2027 : : }
2028 : 0 : return "???";
2029 : 0 : }
|