PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgpa_planner.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pgpa_planner.c
4 * Use planner hooks to observe and modify planner behavior
5 *
6 * All interaction with the core planner happens here. Much of it has to
7 * do with enforcing supplied advice, but we also need these hooks to
8 * generate advice strings (though the heavy lifting in that case is
9 * mostly done by pgpa_walker.c).
10 *
11 * Copyright (c) 2016-2026, PostgreSQL Global Development Group
12 *
13 * contrib/pg_plan_advice/pgpa_planner.c
14 *
15 *-------------------------------------------------------------------------
16 */
17#include "postgres.h"
18
19#include "pg_plan_advice.h"
20#include "pgpa_identifier.h"
21#include "pgpa_output.h"
22#include "pgpa_planner.h"
23#include "pgpa_trove.h"
24#include "pgpa_walker.h"
25
26#include "commands/defrem.h"
28#include "nodes/makefuncs.h"
30#include "optimizer/pathnode.h"
31#include "optimizer/paths.h"
32#include "optimizer/plancat.h"
33#include "optimizer/planner.h"
34#include "parser/parsetree.h"
35#include "utils/lsyscache.h"
36
37typedef enum pgpa_jo_outcome
38{
39 PGPA_JO_PERMITTED, /* permit this join order */
40 PGPA_JO_DENIED, /* deny this join order */
41 PGPA_JO_INDIFFERENT /* do neither */
43
53
54typedef struct pgpa_join_state
55{
56 /* Most-recently-considered outer rel. */
58
59 /* Most-recently-considered inner rel. */
61
62 /*
63 * Array of relation identifiers for all members of this joinrel, with
64 * outerrel identifiers before innerrel identifiers.
65 */
67
68 /* Number of outer rel identifiers. */
70
71 /* Number of inner rel identifiers. */
73
74 /*
75 * Trove lookup results.
76 *
77 * join_entries and rel_entries are arrays of entries, and join_indexes
78 * and rel_indexes are the integer offsets within those arrays of entries
79 * potentially relevant to us. The "join" fields correspond to a lookup
80 * using PGPA_TROVE_LOOKUP_JOIN and the "rel" fields to a lookup using
81 * PGPA_TROVE_LOOKUP_REL.
82 */
88
89/* Saved hook values */
95
96/* Other global variables */
98static int planner_extension_id = -1;
99
100/* Function prototypes. */
101static void pgpa_planner_setup(PlannerGlobal *glob, Query *parse,
102 const char *query_string,
103 int cursorOptions,
104 double *tuple_fraction,
105 ExplainState *es);
106static void pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse,
107 const char *query_string, PlannedStmt *pstmt);
109 RelOptInfo *rel,
112 RelOptInfo *joinrel,
113 RelOptInfo *outerrel,
114 RelOptInfo *innerrel,
115 SpecialJoinInfo *sjinfo,
116 List *restrictlist);
118 RelOptInfo *joinrel,
119 RelOptInfo *outerrel,
120 RelOptInfo *innerrel,
121 JoinType jointype,
122 JoinPathExtraData *extra);
124 RelOptInfo *joinrel,
125 RelOptInfo *outerrel,
126 RelOptInfo *innerrel);
128 char *plan_name,
132 char *plan_name,
137 pgpa_trove_entry *rel_entries,
138 Bitmapset *rel_indexes);
140static pgpa_jo_outcome pgpa_join_order_permits_join(int outer_count,
141 int inner_count,
142 pgpa_identifier *rids,
143 pgpa_trove_entry *entry);
144static bool pgpa_join_method_permits_join(int outer_count, int inner_count,
145 pgpa_identifier *rids,
146 pgpa_trove_entry *entry,
147 bool *restrict_method);
148static bool pgpa_opaque_join_permits_join(int outer_count, int inner_count,
149 pgpa_identifier *rids,
150 pgpa_trove_entry *entry,
151 bool *restrict_method);
152static bool pgpa_semijoin_permits_join(int outer_count, int inner_count,
153 pgpa_identifier *rids,
154 pgpa_trove_entry *entry,
156 bool *restrict_method);
157
162
165
168 RelOptInfo *rel);
170 PlannedStmt *pstmt);
172 PlannedStmt *pstmt);
173
174static char *pgpa_bms_to_cstring(Bitmapset *bms);
175static const char *pgpa_jointype_to_cstring(JoinType jointype);
176
177/*
178 * Install planner-related hooks.
179 */
180void
195
196/*
197 * Carry out whatever setup work we need to do before planning.
198 */
199static void
200pgpa_planner_setup(PlannerGlobal *glob, Query *parse, const char *query_string,
201 int cursorOptions, double *tuple_fraction,
202 ExplainState *es)
203{
204 pgpa_trove *trove = NULL;
206 char *supplied_advice;
207 bool generate_advice_feedback = false;
208 bool generate_advice_string = false;
209 bool needs_pps = false;
210
211 /*
212 * Decide whether we need to generate an advice string. We must do this if
213 * the user has told us to do it categorically, or if another loadable
214 * module has requested it, or if the user has requested it using the
215 * EXPLAIN (PLAN_ADVICE) option.
216 */
217 generate_advice_string = (pg_plan_advice_always_store_advice_details ||
220 if (generate_advice_string)
221 needs_pps = true;
222
223 /*
224 * If any advice was provided, build a trove of advice for use during
225 * planning.
226 */
228 query_string,
229 cursorOptions,
230 es);
231 if (supplied_advice != NULL && supplied_advice[0] != '\0')
232 {
234 char *error;
235
236 /*
237 * If the supplied advice string comes from pg_plan_advice.advice,
238 * parsing shouldn't fail here, because we must have previously parsed
239 * successfully in pg_plan_advice_advice_check_hook. However, it might
240 * also come from a hook registered via pg_plan_advice_add_advisor,
241 * and we can't be sure whether that's valid. (Plus, having an error
242 * check here seems like a good idea anyway, just for safety.)
243 */
245 if (error)
247 errmsg("could not parse supplied advice: %s", error));
248
249 /*
250 * It's possible that the advice string was non-empty but contained no
251 * actual advice, e.g. it was all whitespace.
252 */
253 if (advice_items != NIL)
254 {
256 needs_pps = true;
257
258 /*
259 * If we know that we're running under EXPLAIN, or if the user has
260 * told us to always do the work, generate advice feedback.
261 */
264 generate_advice_feedback = true;
265 }
266 }
267
268 /*
269 * We only create and initialize a private state object if it's needed for
270 * some purpose. That could be (1) recording that we will need to generate
271 * an advice string or (2) storing a trove of supplied advice.
272 *
273 * Currently, the active memory context should be one that will last for
274 * the entire duration of query planning, but if GEQO is in use, it's
275 * possible that some of our callbacks may be invoked later with
276 * CurrentMemoryContext set to some shorter-lived context. So, record the
277 * context that should be used for allocations that need to live as long
278 * as the pgpa_planner_state itself.
279 */
280 if (needs_pps)
281 {
284 pps->generate_advice_feedback = generate_advice_feedback;
285 pps->generate_advice_string = generate_advice_string;
286 pps->trove = trove;
288 }
289
290 /* Pass call to previous hook. */
292 (*prev_planner_setup) (glob, parse, query_string, cursorOptions,
293 tuple_fraction, es);
294}
295
296/*
297 * Carry out whatever work we want to do after planning is complete.
298 */
299static void
301 const char *query_string, PlannedStmt *pstmt)
302{
304 pgpa_trove *trove = NULL;
305 pgpa_plan_walker_context walker = {0}; /* placate compiler */
306 bool generate_advice_feedback = false;
307 bool generate_advice_string = false;
310
311 /* Fetch our private state, set up by pgpa_planner_setup(). */
313 if (pps != NULL)
314 {
315 /* Set up some local variables. */
316 trove = pps->trove;
317 generate_advice_feedback = pps->generate_advice_feedback;
318 generate_advice_string = pps->generate_advice_string;
319
320 /* Compute range table offsets. */
322
323 /* Cross-check range table identifiers. */
325 }
326
327 /*
328 * If we're trying to generate an advice string or if we're trying to
329 * provide advice feedback, then we will need to create range table
330 * identifiers.
331 */
332 if (generate_advice_string || generate_advice_feedback)
333 {
334 pgpa_plan_walker(&walker, pstmt, pps->proots);
336 }
337
338 /* Generate the advice string, if we need to do so. */
339 if (generate_advice_string)
340 {
341 char *advice_string;
343
344 /* Generate a textual advice string. */
347 advice_string = buf.data;
348
349 /* Save the advice string in the final plan. */
351 makeDefElem("advice_string",
352 (Node *) makeString(advice_string),
353 -1));
354 }
355
356 /*
357 * If we're trying to provide advice feedback, then we will need to
358 * analyze how successful the advice was.
359 */
360 if (generate_advice_feedback)
361 {
362 List *feedback = NIL;
363
364 /*
365 * Inject a Node-tree representation of all the trove-entry flags into
366 * the PlannedStmt.
367 */
369 trove,
373 trove,
377 trove,
380
382 (Node *) feedback, -1));
383
384 /* If we were asked to generate feedback warnings, do so. */
387 }
388
389 /* Push whatever data we're saving into the PlannedStmt. */
390 if (pgpa_items != NIL)
391 pstmt->extension_state =
393 makeDefElem("pg_plan_advice", (Node *) pgpa_items, -1));
394
395 /* Pass call to previous hook. */
397 (*prev_planner_shutdown) (glob, parse, query_string, pstmt);
398}
399
400/*
401 * Hook function for build_simple_rel().
402 */
403static void
405{
408
409 /* Fetch our private state, set up by pgpa_planner_setup(). */
411
412 /*
413 * Look up the pgpa_planner_info for this subquery, and make sure we've
414 * saved a range table identifier.
415 */
416 if (pps != NULL)
417 {
420 }
421
422 /* If query advice was provided, search for relevant entries. */
423 if (pps != NULL && pps->trove != NULL)
424 {
425 pgpa_identifier *rid;
428
429 /* Search for scan advice and general rel advice. */
430 rid = &proot->rid_array[rel->relid - 1];
432 &tresult_scan);
434 &tresult_rel);
435
436 /* If relevant entries were found, apply them. */
437 if (tresult_scan.indexes != NULL || tresult_rel.indexes != NULL)
438 {
440
442 tresult_scan.entries,
443 tresult_scan.indexes,
444 tresult_rel.entries,
445 tresult_rel.indexes);
446
447 /* Emit debugging message, if enabled. */
449 {
450 if (root->plan_name != NULL)
452 (errmsg("strategy mask for RTI %u in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
453 rel->relid, root->plan_name,
454 original_mask, rel->pgs_mask)));
455 else
457 (errmsg("strategy mask for RTI %u changed from 0x%" PRIx64 " to 0x%" PRIx64,
458 rel->relid, original_mask,
459 rel->pgs_mask)));
460 }
461 }
462 }
463
464 /* Pass call to previous hook. */
466 (*prev_build_simple_rel) (root, rel, rte);
467}
468
469/*
470 * Enforce any provided advice that is relevant to any method of implementing
471 * this join.
472 *
473 * Although we're passed the outerrel and innerrel here, those are just
474 * whatever values happened to prompt the creation of this joinrel; they
475 * shouldn't really influence our choice of what advice to apply.
476 */
477static void
479 RelOptInfo *outerrel, RelOptInfo *innerrel,
480 SpecialJoinInfo *sjinfo, List *restrictlist)
481{
483
485
486 /* Get our private state information for this join. */
487 pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
488
489 /* If there is relevant advice, call a helper function to apply it. */
490 if (pjs != NULL)
491 {
492 uint64 original_mask = joinrel->pgs_mask;
493
495 root->plan_name,
496 pjs);
497
498 /* Emit debugging message, if enabled. */
500 {
501 if (root->plan_name != NULL)
503 (errmsg("strategy mask for join on RTIs %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
504 pgpa_bms_to_cstring(joinrel->relids),
505 root->plan_name,
507 joinrel->pgs_mask)));
508 else
510 (errmsg("strategy mask for join on RTIs %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
511 pgpa_bms_to_cstring(joinrel->relids),
513 joinrel->pgs_mask)));
514 }
515 }
516
517 /* Pass call to previous hook. */
519 (*prev_joinrel_setup) (root, joinrel, outerrel, innerrel,
520 sjinfo, restrictlist);
521}
522
523/*
524 * Enforce any provided advice that is relevant to this particular method of
525 * implementing this particular join.
526 */
527static void
529 RelOptInfo *outerrel, RelOptInfo *innerrel,
530 JoinType jointype, JoinPathExtraData *extra)
531{
533
535
536 /*
537 * If we're considering implementing a semijoin by making one side unique,
538 * make a note of it in the pgpa_planner_state.
539 */
540 if (jointype == JOIN_UNIQUE_OUTER || jointype == JOIN_UNIQUE_INNER)
541 {
544
545 uniquerel = jointype == JOIN_UNIQUE_OUTER ? outerrel : innerrel;
547 if (pps != NULL &&
548 (pps->generate_advice_string || pps->generate_advice_feedback))
549 {
551 MemoryContext oldcontext;
552 Bitmapset *relids;
553
554 /*
555 * Get or create a pgpa_planner_info object, and then add the
556 * relids from the unique side to proot->sj_unique_rels.
557 *
558 * We must be careful here to use a sufficiently long-lived
559 * context, since we might have been called by GEQO. We want all
560 * the data we store here (including the proot, if we create it)
561 * to last for as long as the pgpa_planner_state.
562 *
563 * pgpa_filter_out_join_relids copies the input Bitmapset whether
564 * or not it is changed, so 'relids' is part of the long-lived
565 * context.
566 */
567 oldcontext = MemoryContextSwitchTo(pps->mcxt);
569 relids = pgpa_filter_out_join_relids(uniquerel->relids,
570 root->parse->rtable);
571 if (!list_member(proot->sj_unique_rels, relids))
572 proot->sj_unique_rels = lappend(proot->sj_unique_rels,
573 relids);
574 else
575 bms_free(relids);
576 MemoryContextSwitchTo(oldcontext);
577 }
578 }
579
580 /* Get our private state information for this join. */
581 pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
582
583 /* If there is relevant advice, call a helper function to apply it. */
584 if (pjs != NULL)
585 {
587
589 &extra->pgs_mask,
590 root->plan_name,
591 pjs);
592
593 /* Emit debugging message, if enabled. */
595 {
596 if (root->plan_name != NULL)
598 (errmsg("strategy mask for %s join on %s with outer %s and inner %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
599 pgpa_jointype_to_cstring(jointype),
600 pgpa_bms_to_cstring(joinrel->relids),
601 pgpa_bms_to_cstring(outerrel->relids),
602 pgpa_bms_to_cstring(innerrel->relids),
603 root->plan_name,
605 extra->pgs_mask)));
606 else
608 (errmsg("strategy mask for %s join on %s with outer %s and inner %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
609 pgpa_jointype_to_cstring(jointype),
610 pgpa_bms_to_cstring(joinrel->relids),
611 pgpa_bms_to_cstring(outerrel->relids),
612 pgpa_bms_to_cstring(innerrel->relids),
614 extra->pgs_mask)));
615 }
616 }
617
618 /* Pass call to previous hook. */
620 (*prev_join_path_setup) (root, joinrel, outerrel, innerrel,
621 jointype, extra);
622}
623
624/*
625 * Search for advice pertaining to a proposed join.
626 */
627static pgpa_join_state *
629 RelOptInfo *outerrel, RelOptInfo *innerrel)
630{
633 bool new_pjs = false;
634
635 /* Fetch our private state, set up by pgpa_planner_setup(). */
637 if (pps == NULL || pps->trove == NULL)
638 {
639 /* No advice applies to this query, hence none to this joinrel. */
640 return NULL;
641 }
642
643 /*
644 * See whether we've previously associated a pgpa_join_state with this
645 * joinrel. If we have not, we need to try to construct one. If we have,
646 * then there are two cases: (a) if innerrel and outerrel are unchanged,
647 * we can simply use it, and (b) if they have changed, we need to rejigger
648 * the array of identifiers but can still skip the trove lookup.
649 */
651 if (pjs != NULL)
652 {
653 if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
654 {
655 /*
656 * If there's no potentially relevant advice, then the presence of
657 * this pgpa_join_state acts like a negative cache entry: it tells
658 * us not to bother searching the trove for advice, because we
659 * will not find any.
660 */
661 return NULL;
662 }
663
664 if (pjs->outerrel == outerrel && pjs->innerrel == innerrel)
665 {
666 /* No updates required, so just return. */
667 /* XXX. Does this need to do something different under GEQO? */
668 return pjs;
669 }
670 }
671
672 /*
673 * If there's no pgpa_join_state yet, we need to allocate one. Trove keys
674 * will not get built for RTE_JOIN RTEs, so the array may end up being
675 * larger than needed. It's not worth trying to compute a perfectly
676 * accurate count here.
677 */
678 if (pjs == NULL)
679 {
681
684 new_pjs = true;
685 }
686
687 /*
688 * Either we just allocated a new pgpa_join_state, or the existing one
689 * needs reconfiguring for a new innerrel and outerrel. The required array
690 * size can't change, so we can overwrite the existing one.
691 */
692 pjs->outerrel = outerrel;
693 pjs->innerrel = innerrel;
694 pjs->outer_count =
696 pjs->inner_count =
698 pjs->rids + pjs->outer_count);
699
700 /*
701 * If we allocated a new pgpa_join_state, search our trove of advice for
702 * relevant entries. The trove lookup will return the same results for
703 * every outerrel/innerrel combination, so we don't need to repeat that
704 * work every time.
705 */
706 if (new_pjs)
707 {
708 pgpa_trove_result tresult;
709
710 /* Find join entries. */
712 pjs->outer_count + pjs->inner_count,
713 pjs->rids, &tresult);
714 pjs->join_entries = tresult.entries;
715 pjs->join_indexes = tresult.indexes;
716
717 /* Find rel entries. */
719 pjs->outer_count + pjs->inner_count,
720 pjs->rids, &tresult);
721 pjs->rel_entries = tresult.entries;
722 pjs->rel_indexes = tresult.indexes;
723
724 /* Now that the new pgpa_join_state is fully valid, save a pointer. */
726
727 /*
728 * If there was no relevant advice found, just return NULL. This
729 * pgpa_join_state will stick around as a sort of negative cache
730 * entry, so that future calls for this same joinrel quickly return
731 * NULL.
732 */
733 if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
734 return NULL;
735 }
736
737 return pjs;
738}
739
740/*
741 * Enforce overall restrictions on a join relation that apply uniformly
742 * regardless of the choice of inner and outer rel.
743 */
744static void
747{
748 int i = -1;
749 int flags;
750 bool gather_conflict = false;
754 bool partitionwise_conflict = false;
755 int partitionwise_outcome = 0;
758
759 /* Iterate over all possibly-relevant advice. */
760 while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
761 {
762 pgpa_trove_entry *entry = &pjs->rel_entries[i];
764 bool full_match = false;
766 int my_partitionwise_outcome = 0; /* >0 yes, <0 no */
767
768 /*
769 * For GATHER and GATHER_MERGE, if the specified relations exactly
770 * match this joinrel, do whatever the advice says; otherwise, don't
771 * allow Gather or Gather Merge at this level. For NO_GATHER, there
772 * must be a single target relation which must be included in this
773 * joinrel, so just don't allow Gather or Gather Merge here, full
774 * stop.
775 */
776 if (entry->tag == PGPA_TAG_NO_GATHER)
777 {
779 full_match = true;
780 }
781 else
782 {
783 int total_count;
784
785 total_count = pjs->outer_count + pjs->inner_count;
787 entry->target);
789
790 if (itm == PGPA_ITM_EQUAL)
791 {
792 full_match = true;
793 if (entry->tag == PGPA_TAG_PARTITIONWISE)
795 else if (entry->tag == PGPA_TAG_GATHER)
797 else if (entry->tag == PGPA_TAG_GATHER_MERGE)
799 else
800 elog(ERROR, "unexpected advice tag: %d",
801 (int) entry->tag);
802 }
803 else
804 {
805 /*
806 * If specified relations don't exactly match this joinrel,
807 * then we should do the opposite of whatever the advice says.
808 * For instance, if we have PARTITIONWISE((a b c)) or
809 * GATHER((a b c)) and this joinrel covers {a, b} or {a, b, c,
810 * d} or {a, d}, we shouldn't plan it partitionwise or put a
811 * Gather or Gather Merge on it here.
812 *
813 * Also, we can't put a Gather or Gather Merge at this level
814 * if there is PARTITIONWISE advice that overlaps with it,
815 * unless the PARTITIONWISE advice covers a subset of the
816 * relations in the joinrel. To continue the previous example,
817 * PARTITIONWISE((a b c)) is logically incompatible with
818 * GATHER((a b)) or GATHER((a d)), but not with GATHER((a b c
819 * d)).
820 *
821 * Conversely, we can't proceed partitionwise at this level if
822 * there is overlapping GATHER or GATHER_MERGE advice, unless
823 * that advice covers a superset of the relations in this
824 * joinrel. This is just the flip side of the preceding point.
825 */
826 if (entry->tag == PGPA_TAG_PARTITIONWISE)
827 {
831 }
832 else if (entry->tag == PGPA_TAG_GATHER ||
833 entry->tag == PGPA_TAG_GATHER_MERGE)
834 {
838 }
839 else
840 elog(ERROR, "unexpected advice tag: %d",
841 (int) entry->tag);
842 }
843 }
844
845 /*
846 * If we set my_gather_mask up above, then we (1) make a note if the
847 * advice conflicted, (2) remember the mask value, and (3) remember
848 * whether this was a full or partial match.
849 */
850 if (my_gather_mask != 0)
851 {
853 gather_conflict = true;
855 if (full_match)
857 else
859 }
860
861 /*
862 * Likewise, if we set my_partitionwise_outcome up above, then we (1)
863 * make a note if the advice conflicted, (2) remember what the desired
864 * outcome was, and (3) remember whether this was a full or partial
865 * match.
866 */
868 {
869 if (partitionwise_outcome != 0 &&
873 if (full_match)
876 else
879 }
880 }
881
882 /*
883 * Mark every Gather-related piece of advice as partially matched, and if
884 * the set of targets exactly matched this relation, fully matched. If
885 * there was a conflict, mark them all as conflicting.
886 */
887 flags = PGPA_FB_MATCH_PARTIAL;
888 if (gather_conflict)
889 flags |= PGPA_FB_CONFLICTING;
890 pgpa_trove_set_flags(pjs->rel_entries, gather_partial_match, flags);
891 flags |= PGPA_FB_MATCH_FULL;
892 pgpa_trove_set_flags(pjs->rel_entries, gather_full_match, flags);
893
894 /* Likewise for partitionwise advice. */
895 flags = PGPA_FB_MATCH_PARTIAL;
897 flags |= PGPA_FB_CONFLICTING;
899 flags |= PGPA_FB_MATCH_FULL;
901
902 /*
903 * Enforce restrictions on the Gather/Gather Merge. Only clear bits here,
904 * so that we still respect the enable_* GUCs. Do nothing if the advice
905 * conflicts.
906 */
907 if (gather_mask != 0 && !gather_conflict)
908 {
910
914 }
915
916 /*
917 * As above, but for partitionwise advice.
918 *
919 * To induce a partitionwise join, we disable all the ordinary means of
920 * performing a join, so that an Append or MergeAppend path will hopefully
921 * be chosen.
922 *
923 * To prevent one, we just disable Append and MergeAppend. Note that we
924 * must not unset PGS_CONSIDER_PARTITIONWISE even when we don't want a
925 * partitionwise join here, because we might want one at a higher level
926 * that will construct its own paths using the ones from this level.
927 */
929 {
930 if (partitionwise_outcome > 0)
932 else
934 }
935}
936
937/*
938 * Enforce restrictions on the join order or join method.
939 */
940static void
942 char *plan_name,
944{
945 int i = -1;
950 bool jm_conflict = false;
951 uint64 join_mask = 0;
954
955 /*
956 * Reconsider PARTITIONWISE(...) advice.
957 *
958 * We already thought about this for the joinrel as a whole, but in some
959 * cases, partitionwise advice can also constrain the join order. For
960 * instance, if the advice says PARTITIONWISE((t1 t2)), we shouldn't build
961 * join paths for any joinrel that includes t1 or t2 unless it also
962 * includes the other. In general, the partitionwise operation must have
963 * already been completed within one side of the current join or the
964 * other, else the join order is impermissible.
965 *
966 * NB: It might seem tempting to try to deal with PARTITIONWISE advice
967 * entirely in this function, but that doesn't work. Here, we can only
968 * affect the pgs_mask within a particular JoinPathExtraData, that is, for
969 * a particular choice of innerrel and outerrel. Partitionwise paths are
970 * not built that way, so we must set pgs_mask for the RelOptInfo, which
971 * is best done in pgpa_planner_apply_joinrel_advice.
972 */
973 while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
974 {
975 pgpa_trove_entry *entry = &pjs->rel_entries[i];
978
979 if (entry->tag != PGPA_TAG_PARTITIONWISE)
980 continue;
981
983 pjs->rids, entry->target);
984 if (outer_itm == PGPA_ITM_EQUAL ||
986 continue;
987
989 pjs->rids + pjs->outer_count,
990 entry->target);
991 if (inner_itm == PGPA_ITM_EQUAL ||
993 continue;
994
996 }
997
998 /* Iterate over advice that pertains to the join order and method. */
999 i = -1;
1000 while ((i = bms_next_member(pjs->join_indexes, i)) >= 0)
1001 {
1002 pgpa_trove_entry *entry = &pjs->join_entries[i];
1004
1005 /* Handle join order advice. */
1006 if (entry->tag == PGPA_TAG_JOIN_ORDER)
1007 {
1009
1011 pjs->inner_count,
1012 pjs->rids,
1013 entry);
1016 else if (jo_outcome == PGPA_JO_DENIED)
1018 continue;
1019 }
1020
1021 /* Handle join method advice. */
1023 if (my_join_mask != 0)
1024 {
1025 bool permit;
1026 bool restrict_method;
1027
1028 if (entry->tag == PGPA_TAG_FOREIGN_JOIN)
1030 pjs->inner_count,
1031 pjs->rids,
1032 entry,
1034 else
1036 pjs->inner_count,
1037 pjs->rids,
1038 entry,
1040 if (!permit)
1042 else if (restrict_method)
1043 {
1045 if (join_mask != 0 && join_mask != my_join_mask)
1046 jm_conflict = true;
1048 }
1049 continue;
1050 }
1051
1052 /* Handle semijoin uniqueness advice. */
1053 if (entry->tag == PGPA_TAG_SEMIJOIN_UNIQUE ||
1055 {
1057 bool restrict_method;
1058
1059 /* Planner has nullable side of the semijoin on the outer side? */
1060 outer_side_nullable = (jointype == JOIN_UNIQUE_OUTER ||
1061 jointype == JOIN_RIGHT_SEMI);
1062
1063 if (!pgpa_semijoin_permits_join(pjs->outer_count,
1064 pjs->inner_count,
1065 pjs->rids,
1066 entry,
1070 else if (restrict_method)
1071 {
1072 bool advice_unique;
1073 bool jt_unique;
1074 bool jt_non_unique;
1075
1076 /* Advice wants to unique-ify and use a regular join? */
1078
1079 /* Planner is trying to unique-ify and use a regular join? */
1080 jt_unique = (jointype == JOIN_UNIQUE_INNER ||
1081 jointype == JOIN_UNIQUE_OUTER);
1082
1083 /* Planner is trying a semi-join, without unique-ifying? */
1084 jt_non_unique = (jointype == JOIN_SEMI ||
1085 jointype == JOIN_RIGHT_SEMI);
1086
1087 if (!jt_unique && !jt_non_unique)
1088 {
1089 /*
1090 * This doesn't seem to be a semijoin to which SJ_UNIQUE
1091 * or SJ_NON_UNIQUE can be applied.
1092 */
1093 entry->flags |= PGPA_FB_INAPPLICABLE;
1094 }
1095 else if (advice_unique != jt_unique)
1097 else
1099 }
1100 continue;
1101 }
1102 }
1103
1104 /*
1105 * If the advice indicates both that this join order is permissible and
1106 * also that it isn't, then mark advice related to the join order as
1107 * conflicting.
1108 */
1109 if (jo_permit_indexes != NULL &&
1111 {
1118 }
1119
1120 /*
1121 * If more than one join method specification is relevant here and they
1122 * differ, mark them all as conflicting.
1123 */
1124 if (jm_conflict)
1125 pgpa_trove_set_flags(pjs->join_entries, jm_indexes,
1127
1128 /* If semijoin advice says both yes and no, mark it all as conflicting. */
1130 {
1135 }
1136
1137 /*
1138 * Enforce restrictions on the join order and join method, and any
1139 * semijoin-related restrictions. Only clear bits here, so that we still
1140 * respect the enable_* GUCs. Do nothing in cases where the advice on a
1141 * single topic conflicts.
1142 */
1146 if (join_mask != 0 && !jm_conflict)
1150}
1151
1152/*
1153 * Translate an advice tag into a path generation strategy mask.
1154 *
1155 * This function can be called with tag types that don't represent join
1156 * strategies. In such cases, we just return 0, which can't be confused with
1157 * a valid mask.
1158 */
1159static uint64
1161{
1162 switch (tag)
1163 {
1165 return PGS_FOREIGNJOIN;
1167 return PGS_MERGEJOIN_PLAIN;
1171 return PGS_NESTLOOP_PLAIN;
1175 return PGS_NESTLOOP_MEMOIZE;
1176 case PGPA_TAG_HASH_JOIN:
1177 return PGS_HASHJOIN;
1178 default:
1179 return 0;
1180 }
1181}
1182
1183/*
1184 * Does a certain item of join order advice permit a certain join?
1185 *
1186 * Returns PGPA_JO_DENIED if the advice is incompatible with the proposed
1187 * join order.
1188 *
1189 * Returns PGPA_JO_PERMITTED if the advice specifies exactly the proposed
1190 * join order. This implies that a partitionwise join should not be
1191 * performed at this level; rather, one of the traditional join methods
1192 * should be used.
1193 *
1194 * Returns PGPA_JO_INDIFFERENT if the advice does not care what happens.
1195 * We use this for unordered JOIN_ORDER sublists, which are compatible with
1196 * partitionwise join but do not mandate it.
1197 */
1198static pgpa_jo_outcome
1199pgpa_join_order_permits_join(int outer_count, int inner_count,
1200 pgpa_identifier *rids,
1201 pgpa_trove_entry *entry)
1202{
1203 bool loop = true;
1204 bool sublist = false;
1205 int length;
1206 int outer_length;
1207 pgpa_advice_target *target = entry->target;
1209
1210 /* We definitely have at least a partial match for this trove entry. */
1211 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1212
1213 /*
1214 * Find the innermost sublist that contains all keys; if no sublist does,
1215 * then continue processing with the toplevel list.
1216 *
1217 * For example, if the advice says JOIN_ORDER(t1 t2 (t3 t4 t5)), then we
1218 * should evaluate joins that only involve t3, t4, and/or t5 against the
1219 * (t3 t4 t5) sublist, and others against the full list.
1220 *
1221 * Note that (1) outermost sublist is always ordered and (2) whenever we
1222 * zoom into an unordered sublist, we instantly return
1223 * PGPA_JO_INDIFFERENT.
1224 */
1225 while (loop)
1226 {
1228
1229 loop = false;
1231 {
1233
1235 continue;
1236
1237 itm = pgpa_identifiers_match_target(outer_count + inner_count,
1238 rids, child_target);
1240 {
1242 {
1243 target = child_target;
1244 sublist = true;
1245 loop = true;
1246 break;
1247 }
1248 else
1249 {
1251 return PGPA_JO_INDIFFERENT;
1252 }
1253 }
1254 }
1255 }
1256
1257 /*
1258 * Try to find a prefix of the selected join order list that is exactly
1259 * equal to the outer side of the proposed join.
1260 */
1261 length = list_length(target->children);
1264 for (outer_length = 1; outer_length <= length; ++outer_length)
1265 {
1267
1268 /* Avoid leaking memory in every loop iteration. */
1269 if (prefix_target->children != NULL)
1270 list_free(prefix_target->children);
1271 prefix_target->children = list_copy_head(target->children,
1272 outer_length);
1273
1274 /* Search, hoping to find an exact match. */
1275 itm = pgpa_identifiers_match_target(outer_count, rids, prefix_target);
1276 if (itm == PGPA_ITM_EQUAL)
1277 break;
1278
1279 /*
1280 * If the prefix of the join order list that we're considering
1281 * includes some but not all of the outer rels, we can make the prefix
1282 * longer to find an exact match. But if the advice hasn't mentioned
1283 * everything that's part of our outer rel yet, but has mentioned
1284 * things that are not, then this join doesn't match the join order
1285 * list.
1286 */
1288 return PGPA_JO_DENIED;
1289 }
1290
1291 /*
1292 * If the previous loop stopped before the prefix_target included the
1293 * entire join order list, then the next member of the join order list
1294 * must exactly match the inner side of the join.
1295 *
1296 * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), if the outer side of the
1297 * current join includes only t1, then the inner side must be exactly t2;
1298 * if the outer side includes both t1 and t2, then the inner side must
1299 * include exactly t3, t4, and t5.
1300 */
1301 if (outer_length < length)
1302 {
1305
1307
1308 itm = pgpa_identifiers_match_target(inner_count, rids + outer_count,
1309 inner_target);
1310
1311 /*
1312 * Before returning, consider whether we need to mark this entry as
1313 * fully matched. If we're considering the full list rather than a
1314 * sublist, and if we found every item but one on the outer side of
1315 * the join and the last item on the inner side of the join, then the
1316 * answer is yes.
1317 */
1318 if (!sublist && outer_length + 1 == length && itm == PGPA_ITM_EQUAL)
1319 entry->flags |= PGPA_FB_MATCH_FULL;
1320
1322 }
1323
1324 /*
1325 * If we get here, then the outer side of the join includes the entirety
1326 * of the join order list. In this case, we behave differently depending
1327 * on whether we're looking at the top-level join order list or sublist.
1328 * At the top-level, we treat the specified list as mandating that the
1329 * actual join order has the given list as a prefix, but a sublist
1330 * requires an exact match.
1331 *
1332 * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), we must start by joining
1333 * all five of those relations and in that sequence, but once that is
1334 * done, it's OK to join any other rels that are part of the join problem.
1335 * This allows a user to specify the driving table and perhaps the first
1336 * few things to which it should be joined while leaving the rest of the
1337 * join order up the optimizer. But it seems like it would be surprising,
1338 * given that specification, if the user could add t6 to the (t3 t4 t5)
1339 * sub-join, so we don't allow that. If we did want to allow it, the logic
1340 * earlier in this function would require substantial adjustment: we could
1341 * allow the t3-t4-t5-t6 join to be built here, but the next step of
1342 * joining t1-t2 to the result would still be rejected.
1343 */
1344 if (!sublist)
1345 entry->flags |= PGPA_FB_MATCH_FULL;
1347}
1348
1349/*
1350 * Does a certain item of join method advice permit a certain join?
1351 *
1352 * Advice such as HASH_JOIN((x y)) means that there should be a hash join with
1353 * exactly x and y on the inner side. Obviously, this means that if we are
1354 * considering a join with exactly x and y on the inner side, we should enforce
1355 * the use of a hash join. However, it also means that we must reject some
1356 * incompatible join orders entirely. For example, a join with exactly x
1357 * and y on the outer side shouldn't be allowed, because such paths might win
1358 * over the advice-driven path on cost.
1359 *
1360 * To accommodate these requirements, this function returns true if the join
1361 * should be allowed and false if it should not. Furthermore, *restrict_method
1362 * is set to true if the join method should be enforced and false if not.
1363 */
1364static bool
1365pgpa_join_method_permits_join(int outer_count, int inner_count,
1366 pgpa_identifier *rids,
1367 pgpa_trove_entry *entry,
1368 bool *restrict_method)
1369{
1370 pgpa_advice_target *target = entry->target;
1374
1375 /* We definitely have at least a partial match for this trove entry. */
1376 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1377
1378 *restrict_method = false;
1379
1380 /*
1381 * If our inner rel mentions exactly the same relations as the advice
1382 * target, allow the join and enforce the join method restriction.
1383 *
1384 * If our inner rel mentions a superset of the target relations, allow the
1385 * join. The join we care about has already taken place, and this advice
1386 * imposes no further restrictions.
1387 */
1389 rids + outer_count,
1390 target);
1392 {
1393 entry->flags |= PGPA_FB_MATCH_FULL;
1394 *restrict_method = true;
1395 return true;
1396 }
1398 return true;
1399
1400 /*
1401 * If our outer rel mentions a superset of the relations in the advice
1402 * target, no restrictions apply, because the join we care about has
1403 * already taken place.
1404 *
1405 * On the other hand, if our outer rel mentions exactly the relations
1406 * mentioned in the advice target, the planner is trying to reverse the
1407 * sides of the join as compared with our desired outcome. Reject that.
1408 */
1410 rids, target);
1412 return true;
1413 else if (outer_itm == PGPA_ITM_EQUAL)
1414 return false;
1415
1416 /*
1417 * If the advice target mentions only a single relation, the test below
1418 * cannot ever pass, so save some work by exiting now.
1419 */
1420 if (target->ttype == PGPA_TARGET_IDENTIFIER)
1421 return false;
1422
1423 /*
1424 * If everything in the joinrel appears in the advice target, we're below
1425 * the level of the join we want to control.
1426 *
1427 * For example, HASH_JOIN((x y)) doesn't restrict how x and y can be
1428 * joined.
1429 *
1430 * This lookup shouldn't return PGPA_ITM_DISJOINT, because any such advice
1431 * should not have been returned from the trove in the first place.
1432 */
1433 join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1434 rids, target);
1438 return true;
1439
1440 /*
1441 * We've already permitted all allowable cases, so reject this.
1442 *
1443 * If we reach this point, then the advice overlaps with this join but
1444 * isn't entirely contained within either side, and there's also at least
1445 * one relation present in the join that isn't mentioned by the advice.
1446 *
1447 * For instance, in the HASH_JOIN((x y)) example, we would reach here if x
1448 * were on one side of the join, y on the other, and at least one of the
1449 * two sides also included some other relation, say t. In that case,
1450 * accepting this join would allow the (x y t) joinrel to contain
1451 * non-disabled paths that do not put (x y) on the inner side of a hash
1452 * join; we could instead end up with something like (x JOIN t) JOIN y.
1453 */
1454 return false;
1455}
1456
1457/*
1458 * Does advice concerning an opaque join permit a certain join?
1459 *
1460 * By an opaque join, we mean one where the exact mechanism by which the
1461 * join is performed is not visible to PostgreSQL. Currently this is the
1462 * case only for foreign joins: FOREIGN_JOIN((x y z)) means that x, y, and
1463 * z are joined on the remote side, but we know nothing about the join order
1464 * or join methods used over there.
1465 *
1466 * The logic here needs to differ from pgpa_join_method_permits_join because,
1467 * for other join types, the advice target is the set of inner rels; here, it
1468 * includes both inner and outer rels.
1469 */
1470static bool
1471pgpa_opaque_join_permits_join(int outer_count, int inner_count,
1472 pgpa_identifier *rids,
1473 pgpa_trove_entry *entry,
1474 bool *restrict_method)
1475{
1476 pgpa_advice_target *target = entry->target;
1478
1479 /* We definitely have at least a partial match for this trove entry. */
1480 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1481
1482 *restrict_method = false;
1483
1484 join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1485 rids, target);
1486 if (join_itm == PGPA_ITM_EQUAL)
1487 {
1488 /*
1489 * We have an exact match, and should therefore allow the join and
1490 * enforce the use of the relevant opaque join method.
1491 */
1492 entry->flags |= PGPA_FB_MATCH_FULL;
1493 *restrict_method = true;
1494 return true;
1495 }
1496
1499 {
1500 /*
1501 * If join_itm == PGPA_ITM_TARGETS_ARE_SUBSET, then the join we care
1502 * about has already taken place and no further restrictions apply.
1503 *
1504 * If join_itm == PGPA_ITM_KEYS_ARE_SUBSET, we're still building up to
1505 * the join we care about and have not introduced any extraneous
1506 * relations not named in the advice. Note that ForeignScan paths for
1507 * joins are built up from ForeignScan paths from underlying joins and
1508 * scans, so we must not disable this join when considering a subset
1509 * of the relations we ultimately want.
1510 */
1511 return true;
1512 }
1513
1514 /*
1515 * The advice overlaps the join, but at least one relation is present in
1516 * the join that isn't mentioned by the advice. We want to disable such
1517 * paths so that we actually push down the join as intended.
1518 */
1519 return false;
1520}
1521
1522/*
1523 * Does advice concerning a semijoin permit a certain join?
1524 *
1525 * Unlike join method advice, which lists the rels on the inner side of the
1526 * join, semijoin uniqueness advice lists the rels on the nullable side of the
1527 * join. Those can be the same, if the join type is JOIN_UNIQUE_INNER or
1528 * JOIN_SEMI, or they can be different, in case of JOIN_UNIQUE_OUTER or
1529 * JOIN_RIGHT_SEMI.
1530 *
1531 * We don't know here whether the caller specified SEMIJOIN_UNIQUE or
1532 * SEMIJOIN_NON_UNIQUE. The caller should check the join type against the
1533 * advice type if and only if we set *restrict_method to true.
1534 */
1535static bool
1536pgpa_semijoin_permits_join(int outer_count, int inner_count,
1537 pgpa_identifier *rids,
1538 pgpa_trove_entry *entry,
1539 bool outer_is_nullable,
1540 bool *restrict_method)
1541{
1542 pgpa_advice_target *target = entry->target;
1546
1547 *restrict_method = false;
1548
1549 /* We definitely have at least a partial match for this trove entry. */
1550 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1551
1552 /*
1553 * If outer rel is the nullable side and contains exactly the same
1554 * relations as the advice target, then the join order is allowable, but
1555 * the caller must check whether the advice tag (either SEMIJOIN_UNIQUE or
1556 * SEMIJOIN_NON_UNIQUE) matches the join type.
1557 *
1558 * If the outer rel is a superset of the target relations, the join we
1559 * care about has already taken place, so we should impose no further
1560 * restrictions.
1561 */
1563 rids, target);
1565 {
1566 entry->flags |= PGPA_FB_MATCH_FULL;
1568 {
1569 *restrict_method = true;
1570 return true;
1571 }
1572 }
1574 return true;
1575
1576 /* As above, but for the inner rel. */
1578 rids + outer_count,
1579 target);
1581 {
1582 entry->flags |= PGPA_FB_MATCH_FULL;
1583 if (!outer_is_nullable)
1584 {
1585 *restrict_method = true;
1586 return true;
1587 }
1588 }
1590 return true;
1591
1592 /*
1593 * If everything in the joinrel appears in the advice target, we're below
1594 * the level of the join we want to control.
1595 */
1596 join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1597 rids, target);
1601 return true;
1602
1603 /*
1604 * We've tested for all allowable possibilities, and so must reject this
1605 * join order. This can happen in two ways.
1606 *
1607 * First, we might be considering a semijoin that overlaps incompletely
1608 * with one or both sides of the join. For example, if the user has
1609 * specified SEMIJOIN_UNIQUE((t1 t2)) or SEMIJOIN_NON_UNIQUE((t1 t2)), we
1610 * should reject a proposed t2-t3 join, since that could not result in a
1611 * final plan compatible with the advice.
1612 *
1613 * Second, we might be considering a semijoin where the advice target
1614 * perfectly matches one side of the join, but it's the wrong one. For
1615 * example, in the example above, we might see a 3-way join between t1,
1616 * t2, and t3, with (t1 t2) on the non-nullable side. That, too, would be
1617 * incompatible with the advice.
1618 */
1619 return false;
1620}
1621
1622/*
1623 * Apply scan advice to a RelOptInfo.
1624 */
1625static void
1629 pgpa_trove_entry *rel_entries,
1630 Bitmapset *rel_indexes)
1631{
1634 bool gather_conflict = false;
1637 int i = -1;
1639 int flags;
1640 bool scan_type_conflict = false;
1643 uint64 gather_mask = 0;
1644 uint64 scan_type = all_scan_mask; /* sentinel: no advice yet */
1645
1646 /* Scrutinize available scan advice. */
1647 while ((i = bms_next_member(scan_indexes, i)) >= 0)
1648 {
1651
1652 /* Translate our advice tags to a scan strategy advice value. */
1653 if (my_entry->tag == PGPA_TAG_DO_NOT_SCAN)
1654 my_scan_type = 0;
1655 else if (my_entry->tag == PGPA_TAG_BITMAP_HEAP_SCAN)
1656 {
1657 /*
1658 * Currently, PGS_CONSIDER_INDEXONLY can suppress Bitmap Heap
1659 * Scans, so don't clear it when such a scan is requested. This
1660 * happens because build_index_scankeys() thinks that the
1661 * possibility of an index-only scan is a sufficient reason to
1662 * consider using an otherwise-useless index, and
1663 * get_index_paths() thinks that the same paths that are useful
1664 * for index or index-only scans should also be considered for
1665 * bitmap scans. Perhaps that logic should be tightened up, but
1666 * until then we need to include PGS_CONSIDER_INDEXONLY in
1667 * my_scan_type here.
1668 */
1670 }
1671 else if (my_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN)
1673 else if (my_entry->tag == PGPA_TAG_INDEX_SCAN)
1675 else if (my_entry->tag == PGPA_TAG_SEQ_SCAN)
1677 else if (my_entry->tag == PGPA_TAG_TID_SCAN)
1679
1680 /*
1681 * If this is understandable scan advice, hang on to the entry, the
1682 * inferred scan type, and the index at which we found it.
1683 *
1684 * Also make a note if we see conflicting scan type advice. Note that
1685 * we regard two index specifications as conflicting unless they match
1686 * exactly. In theory, perhaps we could regard INDEX_SCAN(a c) and
1687 * INDEX_SCAN(a b.c) as non-conflicting if it happens that the only
1688 * index named c is in schema b, but it doesn't seem worth the code.
1689 */
1691 {
1693 scan_type_conflict = true;
1694 if (!scan_type_conflict && scan_entry != NULL &&
1695 my_entry->target->itarget != NULL &&
1696 scan_entry->target->itarget != NULL &&
1697 !pgpa_index_targets_equal(scan_entry->target->itarget,
1698 my_entry->target->itarget))
1699 scan_type_conflict = true;
1703 }
1704 }
1705
1706 /* Scrutinize available gather-related and partitionwise advice. */
1707 i = -1;
1708 while ((i = bms_next_member(rel_indexes, i)) >= 0)
1709 {
1710 pgpa_trove_entry *my_entry = &rel_entries[i];
1712 bool just_one_rel;
1713
1715 || list_length(my_entry->target->children) == 1;
1716
1717 /*
1718 * PARTITIONWISE behaves like a scan type, except that if there's more
1719 * than one relation targeted, it has no effect at this level.
1720 */
1721 if (my_entry->tag == PGPA_TAG_PARTITIONWISE)
1722 {
1723 if (just_one_rel)
1724 {
1726
1728 scan_type_conflict = true;
1733 }
1734 continue;
1735 }
1736
1737 /*
1738 * GATHER and GATHER_MERGE applied to a single rel mean that we should
1739 * use the corresponding strategy here, while applying either to more
1740 * than one rel means we should not use those strategies here, but
1741 * rather at the level of the joinrel that corresponds to what was
1742 * specified. NO_GATHER can only be applied to single rels.
1743 *
1744 * Note that setting PGS_CONSIDER_NONPARTIAL in my_gather_mask is
1745 * equivalent to allowing the non-use of either form of Gather here.
1746 */
1747 if (my_entry->tag == PGPA_TAG_GATHER ||
1749 {
1750 if (!just_one_rel)
1752 else if (my_entry->tag == PGPA_TAG_GATHER)
1754 else
1756 }
1757 else if (my_entry->tag == PGPA_TAG_NO_GATHER)
1758 {
1761 }
1762
1763 /*
1764 * If we set my_gather_mask up above, then we (1) make a note if the
1765 * advice conflicted, (2) remember the mask value, and (3) remember
1766 * whether this was a full or partial match.
1767 */
1768 if (my_gather_mask != 0)
1769 {
1770 if (gather_mask != 0 && gather_mask != my_gather_mask)
1771 gather_conflict = true;
1773 if (just_one_rel)
1775 else
1777 }
1778 }
1779
1780 /* Enforce choice of index. */
1781 if (scan_entry != NULL && !scan_type_conflict &&
1784 {
1785 pgpa_index_target *itarget = scan_entry->target->itarget;
1787
1789 {
1790 char *relname = get_rel_name(index->indexoid);
1791 Oid nspoid = get_rel_namespace(index->indexoid);
1792 char *relnamespace = get_namespace_name_or_temp(nspoid);
1793
1794 if (strcmp(itarget->indname, relname) == 0 &&
1795 (itarget->indnamespace == NULL ||
1796 strcmp(itarget->indnamespace, relnamespace) == 0))
1797 {
1799 break;
1800 }
1801 }
1802
1803 if (matched_index == NULL)
1804 {
1805 /* Don't force the scan type if the index doesn't exist. */
1807
1808 /* Mark advice as inapplicable. */
1811 }
1812 else
1813 {
1814 /* Disable every other index. */
1816 {
1817 if (index != matched_index)
1818 index->disabled = true;
1819 }
1820 }
1821 }
1822
1823 /*
1824 * Mark all the scan method entries as fully matched; and if they specify
1825 * different things, mark them all as conflicting.
1826 */
1829 flags |= PGPA_FB_CONFLICTING;
1831 pgpa_trove_set_flags(rel_entries, scan_type_rel_indexes, flags);
1832
1833 /*
1834 * Mark every Gather-related piece of advice as partially matched. Mark
1835 * the ones that included this relation as a target by itself as fully
1836 * matched. If there was a conflict, mark them all as conflicting.
1837 */
1838 flags = PGPA_FB_MATCH_PARTIAL;
1839 if (gather_conflict)
1840 flags |= PGPA_FB_CONFLICTING;
1841 pgpa_trove_set_flags(rel_entries, gather_partial_match, flags);
1842 flags |= PGPA_FB_MATCH_FULL;
1843 pgpa_trove_set_flags(rel_entries, gather_full_match, flags);
1844
1845 /*
1846 * Enforce restrictions on the scan type and use of Gather/Gather Merge.
1847 * Only clear bits here, so that we still respect the enable_* GUCs. Do
1848 * nothing in cases where the advice on a single topic conflicts.
1849 */
1851 rel->pgs_mask &= ~(all_scan_mask & ~scan_type);
1852 if (gather_mask != 0 && !gather_conflict)
1853 {
1855
1859 }
1860}
1861
1862/*
1863 * Add feedback entries for one trove slice to the provided list and
1864 * return the resulting list.
1865 *
1866 * Feedback entries are generated from the trove entry's flags. It's assumed
1867 * that the caller has already set all relevant flags with the exception of
1868 * PGPA_FB_FAILED. We set that flag here if appropriate.
1869 */
1870static List *
1875{
1876 pgpa_trove_entry *entries;
1877 int nentries;
1878
1879 pgpa_trove_lookup_all(trove, type, &entries, &nentries);
1880 for (int i = 0; i < nentries; ++i)
1881 {
1882 pgpa_trove_entry *entry = &entries[i];
1883 DefElem *item;
1884
1885 /*
1886 * If this entry was fully matched, check whether generating advice
1887 * from this plan would produce such an entry. If not, label the entry
1888 * as failed.
1889 */
1890 if ((entry->flags & PGPA_FB_MATCH_FULL) != 0 &&
1892 entry->tag, entry->target))
1893 entry->flags |= PGPA_FB_FAILED;
1894
1896 (Node *) makeInteger(entry->flags), -1);
1897 list = lappend(list, item);
1898 }
1899
1900 return list;
1901}
1902
1903/*
1904 * Emit a WARNING to tell the user about a problem with the supplied plan
1905 * advice.
1906 */
1907void
1909{
1912
1913 /* Quick exit if there's no feedback. */
1914 if (feedback == NIL)
1915 return;
1916
1917 /* Initialize buffers. */
1920
1921 /* Main loop. */
1923 {
1924 int flags = defGetInt32(item);
1925
1926 /*
1927 * Don't emit anything if it was fully matched with no problems found.
1928 *
1929 * NB: Feedback should never be marked fully matched without also
1930 * being marked partially matched.
1931 */
1933 continue;
1934
1935 /*
1936 * Terminate each detail line except the last with a newline. This is
1937 * also a convenient place to reset flagbuf.
1938 */
1939 if (detailbuf.len > 0)
1940 {
1943 }
1944
1945 /* Generate output. */
1947 appendStringInfo(&detailbuf, "advice %s feedback is \"%s\"",
1948 item->defname, flagbuf.data);
1949 }
1950
1951 /* Emit the warning, if any problems were found. */
1952 if (detailbuf.len > 0)
1954 errmsg("supplied plan advice was not enforced"),
1955 errdetail("%s", detailbuf.data));
1956}
1957
1958/*
1959 * Get or create the pgpa_planner_info for the given PlannerInfo.
1960 */
1961static pgpa_planner_info *
1963{
1965
1966 /*
1967 * If pps->last_proot isn't populated, there are no pgpa_planner_info
1968 * objects yet, so we can drop through and create a new one. Otherwise,
1969 * search for an object with a matching name, and drop through only if
1970 * none is found.
1971 */
1972 if (pps->last_proot != NULL)
1973 {
1974 if (root->plan_name == NULL)
1975 {
1976 if (pps->last_proot->plan_name == NULL)
1977 return pps->last_proot;
1978
1980 {
1981 if (proot->plan_name == NULL)
1982 {
1983 pps->last_proot = proot;
1984 return proot;
1985 }
1986 }
1987 }
1988 else
1989 {
1990 if (pps->last_proot->plan_name != NULL &&
1991 strcmp(pps->last_proot->plan_name, root->plan_name) == 0)
1992 return pps->last_proot;
1993
1995 {
1996 if (proot->plan_name != NULL &&
1997 strcmp(proot->plan_name, root->plan_name) == 0)
1998 {
1999 pps->last_proot = proot;
2000 return proot;
2001 }
2002 }
2003 }
2004 }
2005
2006 /* Create new object. */
2008
2009 /* Set plan name and alternative plan name. */
2010 new_proot->plan_name = root->plan_name;
2011 new_proot->alternative_plan_name = root->alternative_plan_name;
2012
2013 /*
2014 * If the newly-created proot shares an alternative_plan_name with one or
2015 * more others, all should have the is_alternative_plan flag set.
2016 */
2018 {
2019 if (strings_equal_or_both_null(new_proot->alternative_plan_name,
2020 other_proot->alternative_plan_name))
2021 {
2022 new_proot->is_alternative_plan = true;
2023 other_proot->is_alternative_plan = true;
2024 }
2025 }
2026
2027 /*
2028 * Outermost query level always has rtoffset 0; other rtoffset values are
2029 * computed later.
2030 */
2031 if (root->plan_name == NULL)
2032 {
2033 new_proot->has_rtoffset = true;
2034 new_proot->rtoffset = 0;
2035 }
2036
2037 /* Add to list and make it most recently used. */
2038 pps->proots = lappend(pps->proots, new_proot);
2039 pps->last_proot = new_proot;
2040
2041 return new_proot;
2042}
2043
2044/*
2045 * Compute the range table identifier for one relation and save it for future
2046 * use.
2047 */
2048static void
2050 RelOptInfo *rel)
2051{
2052 pgpa_identifier *rid;
2053
2054 /* Allocate or extend the proot's rid_array as necessary. */
2055 if (proot->rid_array_size < rel->relid)
2056 {
2057 int new_size = pg_nextpower2_32(Max(rel->relid, 8));
2058
2059 if (proot->rid_array_size == 0)
2061 else
2062 proot->rid_array = repalloc0_array(proot->rid_array,
2064 proot->rid_array_size,
2065 new_size);
2066 proot->rid_array_size = new_size;
2067 }
2068
2069 /* Save relation identifier details for this RTI if not already done. */
2070 rid = &proot->rid_array[rel->relid - 1];
2071 if (rid->alias_name == NULL)
2073}
2074
2075/*
2076 * Compute the range table offset for each pgpa_planner_info for which it
2077 * is possible to meaningfully do so.
2078 *
2079 * For pgpa_planner_info objects for which no RT offset can be computed,
2080 * clear sj_unique_rels, which is meaningless in such cases.
2081 */
2082static void
2084{
2086 {
2087 /* For the top query level, we've previously set rtoffset 0. */
2088 if (proot->plan_name == NULL)
2089 {
2090 Assert(proot->has_rtoffset);
2091 continue;
2092 }
2093
2094 /*
2095 * It's not guaranteed that every plan name we saw during planning has
2096 * a SubPlanRTInfo, but any that do not certainly don't appear in the
2097 * final range table.
2098 */
2100 {
2101 if (strcmp(proot->plan_name, rtinfo->plan_name) == 0)
2102 {
2103 /*
2104 * If rtinfo->dummy is set, then the subquery's range table
2105 * will only have been partially copied to the final range
2106 * table. Specifically, only RTE_RELATION entries and
2107 * RTE_SUBQUERY entries that were once RTE_RELATION entries
2108 * will be copied, as per add_rtes_to_flat_rtable. Therefore,
2109 * there's no fixed rtoffset that we can apply to the RTIs
2110 * used during planning to locate the corresponding relations.
2111 */
2112 if (!rtinfo->dummy)
2113 {
2114 Assert(!proot->has_rtoffset);
2115 proot->has_rtoffset = true;
2116 proot->rtoffset = rtinfo->rtoffset;
2117 }
2118 break;
2119 }
2120 }
2121
2122 /*
2123 * If we didn't end up setting has_rtoffset, then it will not be
2124 * possible to make any effective use of sj_unique_rels, and it also
2125 * won't be important to do so. So just throw the list away to avoid
2126 * confusing pgpa_plan_walker.
2127 */
2128 if (!proot->has_rtoffset)
2129 proot->sj_unique_rels = NIL;
2130 }
2131}
2132
2133/*
2134 * Validate that the range table identifiers we were able to generate during
2135 * planning match the ones we generated from the final plan.
2136 */
2137static void
2139{
2140#ifdef USE_ASSERT_CHECKING
2143
2144 /* Create identifiers from the planned statement. */
2146
2147 /* Iterate over identifiers created during planning, so we can compare. */
2149 {
2150 if (!proot->has_rtoffset)
2151 continue;
2152
2153 for (int rti = 1; rti <= proot->rid_array_size; ++rti)
2154 {
2155 Index flat_rti = proot->rtoffset + rti;
2156 pgpa_identifier *rid1 = &proot->rid_array[rti - 1];
2158
2159 if (rid1->alias_name == NULL)
2160 continue;
2161
2164 Assert(strcmp(rid1->alias_name, rid2->alias_name) == 0);
2165 Assert(rid1->occurrence == rid2->occurrence);
2166 Assert(strings_equal_or_both_null(rid1->partnsp, rid2->partnsp));
2167 Assert(strings_equal_or_both_null(rid1->partrel, rid2->partrel));
2169 rid2->plan_name));
2170 }
2171 }
2172#endif
2173}
2174
2175/*
2176 * Convert a bitmapset to a C string of comma-separated integers.
2177 */
2178static char *
2180{
2182 int x = -1;
2183
2184 if (bms_is_empty(bms))
2185 return "none";
2186
2188 while ((x = bms_next_member(bms, x)) >= 0)
2189 {
2190 if (buf.len > 0)
2191 appendStringInfo(&buf, ", %d", x);
2192 else
2193 appendStringInfo(&buf, "%d", x);
2194 }
2195
2196 return buf.data;
2197}
2198
2199/*
2200 * Convert a JoinType to a C string.
2201 */
2202static const char *
2204{
2205 switch (jointype)
2206 {
2207 case JOIN_INNER:
2208 return "inner";
2209 case JOIN_LEFT:
2210 return "left";
2211 case JOIN_FULL:
2212 return "full";
2213 case JOIN_RIGHT:
2214 return "right";
2215 case JOIN_SEMI:
2216 return "semi";
2217 case JOIN_ANTI:
2218 return "anti";
2219 case JOIN_RIGHT_SEMI:
2220 return "right semi";
2221 case JOIN_RIGHT_ANTI:
2222 return "right anti";
2223 case JOIN_UNIQUE_OUTER:
2224 return "unique outer";
2225 case JOIN_UNIQUE_INNER:
2226 return "unique inner";
2227 }
2228 return "???";
2229}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
void bms_free(Bitmapset *a)
Definition bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
#define bms_is_empty(a)
Definition bitmapset.h:118
@ BMS_MULTIPLE
Definition bitmapset.h:73
#define Max(x, y)
Definition c.h:1085
#define Assert(condition)
Definition c.h:943
uint64_t uint64
Definition c.h:625
unsigned int Index
Definition c.h:698
int32 defGetInt32(DefElem *def)
Definition define.c:148
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
void SetRelOptInfoExtensionState(RelOptInfo *rel, int extension_id, void *opaque)
Definition extendplan.c:147
int GetPlannerExtensionId(const char *extension_name)
Definition extendplan.c:41
void SetPlannerGlobalExtensionState(PlannerGlobal *glob, int extension_id, void *opaque)
Definition extendplan.c:77
static void * GetPlannerGlobalExtensionState(PlannerGlobal *glob, int extension_id)
Definition extendplan.h:25
static void * GetRelOptInfoExtensionState(RelOptInfo *rel, int extension_id)
Definition extendplan.h:53
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
void parse(int)
Definition parse.c:49
int x
Definition isn.c:75
int i
Definition isn.c:77
join_path_setup_hook_type join_path_setup_hook
Definition joinpath.c:32
List * lappend(List *list, void *datum)
Definition list.c:339
void list_free(List *list)
Definition list.c:1546
bool list_member(const List *list, const void *datum)
Definition list.c:661
List * list_copy_head(const List *oldlist, int len)
Definition list.c:1593
char * get_rel_name(Oid relid)
Definition lsyscache.c:2121
Oid get_rel_namespace(Oid relid)
Definition lsyscache.c:2145
char * get_namespace_name_or_temp(Oid nspid)
Definition lsyscache.c:3585
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition makefuncs.c:637
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
JoinType
Definition nodes.h:298
@ JOIN_SEMI
Definition nodes.h:317
@ JOIN_FULL
Definition nodes.h:305
@ JOIN_INNER
Definition nodes.h:303
@ JOIN_RIGHT
Definition nodes.h:306
@ JOIN_RIGHT_SEMI
Definition nodes.h:319
@ JOIN_LEFT
Definition nodes.h:304
@ JOIN_UNIQUE_OUTER
Definition nodes.h:326
@ JOIN_RIGHT_ANTI
Definition nodes.h:320
@ JOIN_UNIQUE_INNER
Definition nodes.h:327
@ JOIN_ANTI
Definition nodes.h:318
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define repalloc0_array(pointer, type, oldcount, count)
Definition palloc.h:109
void(* joinrel_setup_hook_type)(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition pathnode.h:42
void(* build_simple_rel_hook_type)(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition pathnode.h:21
#define PGS_NESTLOOP_MEMOIZE
Definition pathnodes.h:76
#define PGS_TIDSCAN
Definition pathnodes.h:70
#define PGS_FOREIGNJOIN
Definition pathnodes.h:71
#define PGS_APPEND
Definition pathnodes.h:78
#define PGS_MERGE_APPEND
Definition pathnodes.h:79
#define PGS_SEQSCAN
Definition pathnodes.h:66
#define PGS_CONSIDER_INDEXONLY
Definition pathnodes.h:82
#define PGS_NESTLOOP_MATERIALIZE
Definition pathnodes.h:75
#define PGS_MERGEJOIN_PLAIN
Definition pathnodes.h:72
#define PGS_MERGEJOIN_MATERIALIZE
Definition pathnodes.h:73
#define PGS_HASHJOIN
Definition pathnodes.h:77
#define PGS_CONSIDER_NONPARTIAL
Definition pathnodes.h:84
#define PGS_BITMAPSCAN
Definition pathnodes.h:69
#define PGS_GATHER
Definition pathnodes.h:80
#define PGS_SCAN_ANY
Definition pathnodes.h:89
#define PGS_GATHER_MERGE
Definition pathnodes.h:81
#define PGS_INDEXONLYSCAN
Definition pathnodes.h:68
#define PGS_INDEXSCAN
Definition pathnodes.h:67
#define PGS_JOIN_ANY
Definition pathnodes.h:96
#define PGS_NESTLOOP_PLAIN
Definition pathnodes.h:74
void(* join_path_setup_hook_type)(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
Definition paths.h:32
static uint32 pg_nextpower2_32(uint32 num)
NameData relname
Definition pg_class.h:40
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
#define foreach_node(type, var, lst)
Definition pg_list.h:528
bool pg_plan_advice_trace_mask
bool pg_plan_advice_always_store_advice_details
bool pg_plan_advice_should_explain(ExplainState *es)
char * pg_plan_advice_get_supplied_query_advice(PlannerGlobal *glob, Query *parse, const char *query_string, int cursorOptions, ExplainState *es)
bool pg_plan_advice_feedback_warnings
#define PGPA_FB_INAPPLICABLE
#define PGPA_FB_CONFLICTING
#define PGPA_FB_MATCH_FULL
#define PGPA_FB_FAILED
#define PGPA_FB_MATCH_PARTIAL
static char buf[DEFAULT_XLOG_SEG_SIZE]
bool pgpa_index_targets_equal(pgpa_index_target *i1, pgpa_index_target *i2)
Definition pgpa_ast.c:218
pgpa_itm_type pgpa_identifiers_match_target(int nrids, pgpa_identifier *rids, pgpa_advice_target *target)
Definition pgpa_ast.c:283
pgpa_advice_tag_type
Definition pgpa_ast.h:81
@ PGPA_TAG_INDEX_SCAN
Definition pgpa_ast.h:89
@ PGPA_TAG_NESTED_LOOP_MATERIALIZE
Definition pgpa_ast.h:93
@ PGPA_TAG_MERGE_JOIN_PLAIN
Definition pgpa_ast.h:92
@ PGPA_TAG_GATHER_MERGE
Definition pgpa_ast.h:86
@ PGPA_TAG_GATHER
Definition pgpa_ast.h:85
@ PGPA_TAG_NESTED_LOOP_MEMOIZE
Definition pgpa_ast.h:94
@ PGPA_TAG_SEMIJOIN_NON_UNIQUE
Definition pgpa_ast.h:98
@ PGPA_TAG_BITMAP_HEAP_SCAN
Definition pgpa_ast.h:82
@ PGPA_TAG_PARTITIONWISE
Definition pgpa_ast.h:97
@ PGPA_TAG_NO_GATHER
Definition pgpa_ast.h:96
@ PGPA_TAG_INDEX_ONLY_SCAN
Definition pgpa_ast.h:88
@ PGPA_TAG_SEQ_SCAN
Definition pgpa_ast.h:100
@ PGPA_TAG_HASH_JOIN
Definition pgpa_ast.h:87
@ PGPA_TAG_SEMIJOIN_UNIQUE
Definition pgpa_ast.h:99
@ PGPA_TAG_DO_NOT_SCAN
Definition pgpa_ast.h:83
@ PGPA_TAG_JOIN_ORDER
Definition pgpa_ast.h:90
@ PGPA_TAG_TID_SCAN
Definition pgpa_ast.h:101
@ PGPA_TAG_FOREIGN_JOIN
Definition pgpa_ast.h:84
@ PGPA_TAG_NESTED_LOOP_PLAIN
Definition pgpa_ast.h:95
@ PGPA_TAG_MERGE_JOIN_MATERIALIZE
Definition pgpa_ast.h:91
List * pgpa_parse(const char *advice_string, char **error_p)
@ PGPA_TARGET_UNORDERED_LIST
Definition pgpa_ast.h:29
@ PGPA_TARGET_IDENTIFIER
Definition pgpa_ast.h:27
@ PGPA_TARGET_ORDERED_LIST
Definition pgpa_ast.h:28
pgpa_itm_type
Definition pgpa_ast.h:142
@ PGPA_ITM_EQUAL
Definition pgpa_ast.h:143
@ PGPA_ITM_DISJOINT
Definition pgpa_ast.h:147
@ PGPA_ITM_KEYS_ARE_SUBSET
Definition pgpa_ast.h:144
@ PGPA_ITM_TARGETS_ARE_SUBSET
Definition pgpa_ast.h:145
void pgpa_compute_identifier_by_rti(PlannerInfo *root, Index rti, pgpa_identifier *rid)
pgpa_identifier * pgpa_create_identifiers_for_planned_stmt(PlannedStmt *pstmt)
int pgpa_compute_identifiers_by_relids(PlannerInfo *root, Bitmapset *relids, pgpa_identifier *rids)
static bool strings_equal_or_both_null(const char *a, const char *b)
void pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker, pgpa_identifier *rt_identifiers)
Definition pgpa_output.c:80
static void pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse, const char *query_string, PlannedStmt *pstmt)
static void pgpa_compute_rt_offsets(pgpa_planner_state *pps, PlannedStmt *pstmt)
static pgpa_planner_info * pgpa_planner_get_proot(pgpa_planner_state *pps, PlannerInfo *root)
void pgpa_planner_feedback_warning(List *feedback)
static planner_setup_hook_type prev_planner_setup
void pgpa_planner_install_hooks(void)
static void pgpa_planner_apply_scan_advice(RelOptInfo *rel, pgpa_trove_entry *scan_entries, Bitmapset *scan_indexes, pgpa_trove_entry *rel_entries, Bitmapset *rel_indexes)
static pgpa_join_state * pgpa_get_join_state(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel)
static joinrel_setup_hook_type prev_joinrel_setup
static build_simple_rel_hook_type prev_build_simple_rel
static void pgpa_build_simple_rel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
static bool pgpa_opaque_join_permits_join(int outer_count, int inner_count, pgpa_identifier *rids, pgpa_trove_entry *entry, bool *restrict_method)
static void pgpa_validate_rt_identifiers(pgpa_planner_state *pps, PlannedStmt *pstmt)
static void pgpa_compute_rt_identifier(pgpa_planner_info *proot, PlannerInfo *root, RelOptInfo *rel)
static void pgpa_planner_setup(PlannerGlobal *glob, Query *parse, const char *query_string, int cursorOptions, double *tuple_fraction, ExplainState *es)
int pgpa_planner_generate_advice
static planner_shutdown_hook_type prev_planner_shutdown
static char * pgpa_bms_to_cstring(Bitmapset *bms)
static int planner_extension_id
static pgpa_jo_outcome pgpa_join_order_permits_join(int outer_count, int inner_count, pgpa_identifier *rids, pgpa_trove_entry *entry)
static bool pgpa_join_method_permits_join(int outer_count, int inner_count, pgpa_identifier *rids, pgpa_trove_entry *entry, bool *restrict_method)
static uint64 pgpa_join_strategy_mask_from_advice_tag(pgpa_advice_tag_type tag)
static const char * pgpa_jointype_to_cstring(JoinType jointype)
static List * pgpa_planner_append_feedback(List *list, pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_identifier *rt_identifiers, pgpa_plan_walker_context *walker)
static void pgpa_join_path_setup(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
static bool pgpa_semijoin_permits_join(int outer_count, int inner_count, pgpa_identifier *rids, pgpa_trove_entry *entry, bool outer_is_nullable, bool *restrict_method)
static void pgpa_joinrel_setup(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, SpecialJoinInfo *sjinfo, List *restrictlist)
static void pgpa_planner_apply_joinrel_advice(uint64 *pgs_mask_p, char *plan_name, pgpa_join_state *pjs)
static void pgpa_planner_apply_join_path_advice(JoinType jointype, uint64 *pgs_mask_p, char *plan_name, pgpa_join_state *pjs)
pgpa_jo_outcome
@ PGPA_JO_PERMITTED
@ PGPA_JO_DENIED
@ PGPA_JO_INDIFFERENT
static join_path_setup_hook_type prev_join_path_setup
void pgpa_trove_set_flags(pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
Definition pgpa_trove.c:328
pgpa_trove * pgpa_build_trove(List *advice_items)
Definition pgpa_trove.c:133
void pgpa_trove_append_flags(StringInfo buf, int flags)
Definition pgpa_trove.c:345
void pgpa_trove_lookup_all(pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_trove_entry **entries, int *nentries)
Definition pgpa_trove.c:277
char * pgpa_cstring_trove_entry(pgpa_trove_entry *entry)
Definition pgpa_trove.c:297
void pgpa_trove_lookup(pgpa_trove *trove, pgpa_trove_lookup_type type, int nrids, pgpa_identifier *rids, pgpa_trove_result *result)
Definition pgpa_trove.c:235
pgpa_trove_lookup_type
Definition pgpa_trove.h:52
@ PGPA_TROVE_LOOKUP_SCAN
Definition pgpa_trove.h:55
@ PGPA_TROVE_LOOKUP_JOIN
Definition pgpa_trove.h:53
@ PGPA_TROVE_LOOKUP_REL
Definition pgpa_trove.h:54
Bitmapset * pgpa_filter_out_join_relids(Bitmapset *relids, List *rtable)
void pgpa_plan_walker(pgpa_plan_walker_context *walker, PlannedStmt *pstmt, List *proots)
Definition pgpa_walker.c:77
bool pgpa_walker_would_advise(pgpa_plan_walker_context *walker, pgpa_identifier *rt_identifiers, pgpa_advice_tag_type tag, pgpa_advice_target *target)
planner_shutdown_hook_type planner_shutdown_hook
Definition planner.c:80
planner_setup_hook_type planner_setup_hook
Definition planner.c:77
void(* planner_setup_hook_type)(PlannerGlobal *glob, Query *parse, const char *query_string, int cursorOptions, double *tuple_fraction, ExplainState *es)
Definition planner.h:36
void(* planner_shutdown_hook_type)(PlannerGlobal *glob, Query *parse, const char *query_string, PlannedStmt *pstmt)
Definition planner.h:44
unsigned int Oid
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
joinrel_setup_hook_type joinrel_setup_hook
Definition relnode.c:54
build_simple_rel_hook_type build_simple_rel_hook
Definition relnode.c:51
static void error(void)
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Definition pg_list.h:54
Definition nodes.h:135
List * extension_state
Definition plannodes.h:165
List * subrtinfos
Definition plannodes.h:132
List * rtable
Definition plannodes.h:107
Relids relids
Definition pathnodes.h:1021
Index relid
Definition pathnodes.h:1069
uint64 pgs_mask
Definition pathnodes.h:1039
List * indexlist
Definition pathnodes.h:1091
Definition type.h:96
pgpa_target_type ttype
Definition pgpa_ast.h:49
const char * alias_name
char * indnamespace
Definition pgpa_ast.h:38
RelOptInfo * outerrel
RelOptInfo * innerrel
pgpa_trove_entry * join_entries
pgpa_trove_entry * rel_entries
Bitmapset * rel_indexes
pgpa_identifier * rids
Bitmapset * join_indexes
pgpa_planner_info * last_proot
MemoryContext mcxt
pgpa_trove * trove
Definition pgpa_trove.h:27
pgpa_advice_target * target
Definition pgpa_trove.h:29
pgpa_advice_tag_type tag
Definition pgpa_trove.h:28
int flags
Definition pgpa_trove.h:30
pgpa_trove_entry * entries
Definition pgpa_trove.h:65
Bitmapset * indexes
Definition pgpa_trove.h:66
Integer * makeInteger(int i)
Definition value.c:23
String * makeString(char *str)
Definition value.c:63
const char * type