PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgpa_planner.c File Reference
#include "postgres.h"
#include "pg_plan_advice.h"
#include "pgpa_identifier.h"
#include "pgpa_output.h"
#include "pgpa_planner.h"
#include "pgpa_trove.h"
#include "pgpa_walker.h"
#include "commands/defrem.h"
#include "common/hashfn_unstable.h"
#include "nodes/makefuncs.h"
#include "optimizer/extendplan.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/planner.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
Include dependency graph for pgpa_planner.c:

Go to the source code of this file.

Data Structures

struct  pgpa_planner_state
 
struct  pgpa_join_state
 

Typedefs

typedef enum pgpa_jo_outcome pgpa_jo_outcome
 
typedef struct pgpa_planner_state pgpa_planner_state
 
typedef struct pgpa_join_state pgpa_join_state
 

Enumerations

enum  pgpa_jo_outcome { PGPA_JO_PERMITTED , PGPA_JO_DENIED , PGPA_JO_INDIFFERENT }
 

Functions

static void pgpa_planner_setup (PlannerGlobal *glob, Query *parse, const char *query_string, int cursorOptions, double *tuple_fraction, ExplainState *es)
 
static void pgpa_planner_shutdown (PlannerGlobal *glob, Query *parse, const char *query_string, PlannedStmt *pstmt)
 
static void pgpa_build_simple_rel (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static void pgpa_joinrel_setup (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static void pgpa_join_path_setup (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
 
static pgpa_join_statepgpa_get_join_state (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel)
 
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)
 
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 uint64 pgpa_join_strategy_mask_from_advice_tag (pgpa_advice_tag_type tag)
 
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 bool pgpa_opaque_join_permits_join (int outer_count, int inner_count, pgpa_identifier *rids, pgpa_trove_entry *entry, bool *restrict_method)
 
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 Listpgpa_planner_append_feedback (List *list, pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_identifier *rt_identifiers, pgpa_plan_walker_context *walker)
 
static pgpa_planner_infopgpa_planner_get_proot (pgpa_planner_state *pps, PlannerInfo *root)
 
static void pgpa_compute_rt_identifier (pgpa_planner_info *proot, PlannerInfo *root, RelOptInfo *rel)
 
static void pgpa_compute_rt_offsets (pgpa_planner_state *pps, PlannedStmt *pstmt)
 
static void pgpa_validate_rt_identifiers (pgpa_planner_state *pps, PlannedStmt *pstmt)
 
static charpgpa_bms_to_cstring (Bitmapset *bms)
 
static const charpgpa_jointype_to_cstring (JoinType jointype)
 
void pgpa_planner_install_hooks (void)
 
void pgpa_planner_feedback_warning (List *feedback)
 

Variables

static build_simple_rel_hook_type prev_build_simple_rel = NULL
 
static join_path_setup_hook_type prev_join_path_setup = NULL
 
static joinrel_setup_hook_type prev_joinrel_setup = NULL
 
static planner_setup_hook_type prev_planner_setup = NULL
 
static planner_shutdown_hook_type prev_planner_shutdown = NULL
 
int pgpa_planner_generate_advice = 0
 
static int planner_extension_id = -1
 

Typedef Documentation

◆ pgpa_jo_outcome

◆ pgpa_join_state

◆ pgpa_planner_state

Enumeration Type Documentation

◆ pgpa_jo_outcome

Enumerator
PGPA_JO_PERMITTED 
PGPA_JO_DENIED 
PGPA_JO_INDIFFERENT 

Definition at line 37 of file pgpa_planner.c.

38{
39 PGPA_JO_PERMITTED, /* permit this join order */
40 PGPA_JO_DENIED, /* deny this join order */
41 PGPA_JO_INDIFFERENT /* do neither */
pgpa_jo_outcome
@ PGPA_JO_PERMITTED
@ PGPA_JO_DENIED
@ PGPA_JO_INDIFFERENT

Function Documentation

◆ pgpa_bms_to_cstring()

static char * pgpa_bms_to_cstring ( Bitmapset bms)
static

Definition at line 2169 of file pgpa_planner.c.

2170{
2172 int x = -1;
2173
2174 if (bms_is_empty(bms))
2175 return "none";
2176
2178 while ((x = bms_next_member(bms, x)) >= 0)
2179 {
2180 if (buf.len > 0)
2181 appendStringInfo(&buf, ", %d", x);
2182 else
2183 appendStringInfo(&buf, "%d", x);
2184 }
2185
2186 return buf.data;
2187}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
#define bms_is_empty(a)
Definition bitmapset.h:118
int x
Definition isn.c:75
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void initStringInfo(StringInfo str)
Definition stringinfo.c:97

References appendStringInfo(), bms_is_empty, bms_next_member(), buf, fb(), initStringInfo(), and x.

Referenced by pgpa_join_path_setup(), and pgpa_joinrel_setup().

◆ pgpa_build_simple_rel()

static void pgpa_build_simple_rel ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 404 of file pgpa_planner.c.

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}
uint64_t uint64
Definition c.h:625
#define WARNING
Definition elog.h:37
#define ereport(elevel,...)
Definition elog.h:152
static void * GetPlannerGlobalExtensionState(PlannerGlobal *glob, int extension_id)
Definition extendplan.h:25
static char * errmsg
bool pg_plan_advice_trace_mask
static pgpa_planner_info * pgpa_planner_get_proot(pgpa_planner_state *pps, PlannerInfo *root)
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 build_simple_rel_hook_type prev_build_simple_rel
static void pgpa_compute_rt_identifier(pgpa_planner_info *proot, PlannerInfo *root, RelOptInfo *rel)
static int planner_extension_id
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_SCAN
Definition pgpa_trove.h:55
@ PGPA_TROVE_LOOKUP_REL
Definition pgpa_trove.h:54
tree ctl root
Definition radixtree.h:1857
Index relid
Definition pathnodes.h:1069
uint64 pgs_mask
Definition pathnodes.h:1039

References ereport, errmsg, fb(), GetPlannerGlobalExtensionState(), pg_plan_advice_trace_mask, pgpa_compute_rt_identifier(), pgpa_planner_apply_scan_advice(), pgpa_planner_get_proot(), pgpa_trove_lookup(), PGPA_TROVE_LOOKUP_REL, PGPA_TROVE_LOOKUP_SCAN, RelOptInfo::pgs_mask, planner_extension_id, prev_build_simple_rel, RelOptInfo::relid, root, and WARNING.

Referenced by pgpa_planner_install_hooks().

◆ pgpa_compute_rt_identifier()

static void pgpa_compute_rt_identifier ( pgpa_planner_info proot,
PlannerInfo root,
RelOptInfo rel 
)
inlinestatic

Definition at line 2039 of file pgpa_planner.c.

2041{
2042 pgpa_identifier *rid;
2043
2044 /* Allocate or extend the proot's rid_array as necessary. */
2045 if (proot->rid_array_size < rel->relid)
2046 {
2047 int new_size = pg_nextpower2_32(Max(rel->relid, 8));
2048
2049 if (proot->rid_array_size == 0)
2051 else
2052 proot->rid_array = repalloc0_array(proot->rid_array,
2054 proot->rid_array_size,
2055 new_size);
2056 proot->rid_array_size = new_size;
2057 }
2058
2059 /* Save relation identifier details for this RTI if not already done. */
2060 rid = &proot->rid_array[rel->relid - 1];
2061 if (rid->alias_name == NULL)
2063}
#define Max(x, y)
Definition c.h:1085
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define repalloc0_array(pointer, type, oldcount, count)
Definition palloc.h:109
static uint32 pg_nextpower2_32(uint32 num)
void pgpa_compute_identifier_by_rti(PlannerInfo *root, Index rti, pgpa_identifier *rid)
const char * alias_name

References pgpa_identifier::alias_name, fb(), Max, palloc0_array, pg_nextpower2_32(), pgpa_compute_identifier_by_rti(), RelOptInfo::relid, repalloc0_array, and root.

Referenced by pgpa_build_simple_rel().

◆ pgpa_compute_rt_offsets()

static void pgpa_compute_rt_offsets ( pgpa_planner_state pps,
PlannedStmt pstmt 
)
static

Definition at line 2073 of file pgpa_planner.c.

2074{
2076 {
2077 /* For the top query level, we've previously set rtoffset 0. */
2078 if (proot->plan_name == NULL)
2079 {
2080 Assert(proot->has_rtoffset);
2081 continue;
2082 }
2083
2084 /*
2085 * It's not guaranteed that every plan name we saw during planning has
2086 * a SubPlanInfo, but any that do not certainly don't appear in the
2087 * final range table.
2088 */
2090 {
2091 if (strcmp(proot->plan_name, rtinfo->plan_name) == 0)
2092 {
2093 /*
2094 * If rtinfo->dummy is set, then the subquery's range table
2095 * will only have been partially copied to the final range
2096 * table. Specifically, only RTE_RELATION entries and
2097 * RTE_SUBQUERY entries that were once RTE_RELATION entries
2098 * will be copied, as per add_rtes_to_flat_rtable. Therefore,
2099 * there's no fixed rtoffset that we can apply to the RTIs
2100 * used during planning to locate the corresponding relations.
2101 */
2102 if (!rtinfo->dummy)
2103 {
2104 Assert(!proot->has_rtoffset);
2105 proot->has_rtoffset = true;
2106 proot->rtoffset = rtinfo->rtoffset;
2107 }
2108 break;
2109 }
2110 }
2111
2112 /*
2113 * If we didn't end up setting has_rtoffset, then it will not be
2114 * possible to make any effective use of sj_unique_rels, and it also
2115 * won't be important to do so. So just throw the list away to avoid
2116 * confusing pgpa_plan_walker.
2117 */
2118 if (!proot->has_rtoffset)
2119 proot->sj_unique_rels = NIL;
2120 }
2121}
#define Assert(condition)
Definition c.h:943
#define NIL
Definition pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
#define foreach_node(type, var, lst)
Definition pg_list.h:528
List * subrtinfos
Definition plannodes.h:132

References Assert, fb(), foreach_node, foreach_ptr, NIL, and PlannedStmt::subrtinfos.

Referenced by pgpa_planner_shutdown().

◆ pgpa_get_join_state()

static pgpa_join_state * pgpa_get_join_state ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel 
)
static

Definition at line 619 of file pgpa_planner.c.

621{
624 bool new_pjs = false;
625
626 /* Fetch our private state, set up by pgpa_planner_setup(). */
628 if (pps == NULL || pps->trove == NULL)
629 {
630 /* No advice applies to this query, hence none to this joinrel. */
631 return NULL;
632 }
633
634 /*
635 * See whether we've previously associated a pgpa_join_state with this
636 * joinrel. If we have not, we need to try to construct one. If we have,
637 * then there are two cases: (a) if innerrel and outerrel are unchanged,
638 * we can simply use it, and (b) if they have changed, we need to rejigger
639 * the array of identifiers but can still skip the trove lookup.
640 */
642 if (pjs != NULL)
643 {
644 if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
645 {
646 /*
647 * If there's no potentially relevant advice, then the presence of
648 * this pgpa_join_state acts like a negative cache entry: it tells
649 * us not to bother searching the trove for advice, because we
650 * will not find any.
651 */
652 return NULL;
653 }
654
655 if (pjs->outerrel == outerrel && pjs->innerrel == innerrel)
656 {
657 /* No updates required, so just return. */
658 /* XXX. Does this need to do something different under GEQO? */
659 return pjs;
660 }
661 }
662
663 /*
664 * If there's no pgpa_join_state yet, we need to allocate one. Trove keys
665 * will not get built for RTE_JOIN RTEs, so the array may end up being
666 * larger than needed. It's not worth trying to compute a perfectly
667 * accurate count here.
668 */
669 if (pjs == NULL)
670 {
672
675 new_pjs = true;
676 }
677
678 /*
679 * Either we just allocated a new pgpa_join_state, or the existing one
680 * needs reconfiguring for a new innerrel and outerrel. The required array
681 * size can't change, so we can overwrite the existing one.
682 */
683 pjs->outerrel = outerrel;
684 pjs->innerrel = innerrel;
685 pjs->outer_count =
687 pjs->inner_count =
689 pjs->rids + pjs->outer_count);
690
691 /*
692 * If we allocated a new pgpa_join_state, search our trove of advice for
693 * relevant entries. The trove lookup will return the same results for
694 * every outerrel/innerrel combination, so we don't need to repeat that
695 * work every time.
696 */
697 if (new_pjs)
698 {
699 pgpa_trove_result tresult;
700
701 /* Find join entries. */
703 pjs->outer_count + pjs->inner_count,
704 pjs->rids, &tresult);
705 pjs->join_entries = tresult.entries;
706 pjs->join_indexes = tresult.indexes;
707
708 /* Find rel entries. */
710 pjs->outer_count + pjs->inner_count,
711 pjs->rids, &tresult);
712 pjs->rel_entries = tresult.entries;
713 pjs->rel_indexes = tresult.indexes;
714
715 /* Now that the new pgpa_join_state is fully valid, save a pointer. */
717
718 /*
719 * If there was no relevant advice found, just return NULL. This
720 * pgpa_join_state will stick around as a sort of negative cache
721 * entry, so that future calls for this same joinrel quickly return
722 * NULL.
723 */
724 if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
725 return NULL;
726 }
727
728 return pjs;
729}
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
void SetRelOptInfoExtensionState(RelOptInfo *rel, int extension_id, void *opaque)
Definition extendplan.c:147
static void * GetRelOptInfoExtensionState(RelOptInfo *rel, int extension_id)
Definition extendplan.h:53
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_object(type)
Definition fe_memutils.h:75
int pgpa_compute_identifiers_by_relids(PlannerInfo *root, Bitmapset *relids, pgpa_identifier *rids)
@ PGPA_TROVE_LOOKUP_JOIN
Definition pgpa_trove.h:53
Relids relids
Definition pathnodes.h:1021
pgpa_trove_entry * entries
Definition pgpa_trove.h:65
Bitmapset * indexes
Definition pgpa_trove.h:66

References bms_num_members(), pgpa_trove_result::entries, fb(), GetPlannerGlobalExtensionState(), GetRelOptInfoExtensionState(), pgpa_trove_result::indexes, palloc0_object, palloc_array, pgpa_compute_identifiers_by_relids(), pgpa_trove_lookup(), PGPA_TROVE_LOOKUP_JOIN, PGPA_TROVE_LOOKUP_REL, planner_extension_id, RelOptInfo::relids, root, and SetRelOptInfoExtensionState().

Referenced by pgpa_join_path_setup(), and pgpa_joinrel_setup().

◆ pgpa_join_method_permits_join()

static bool pgpa_join_method_permits_join ( int  outer_count,
int  inner_count,
pgpa_identifier rids,
pgpa_trove_entry entry,
bool restrict_method 
)
static

Definition at line 1356 of file pgpa_planner.c.

1360{
1361 pgpa_advice_target *target = entry->target;
1365
1366 /* We definitely have at least a partial match for this trove entry. */
1367 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1368
1369 *restrict_method = false;
1370
1371 /*
1372 * If our inner rel mentions exactly the same relations as the advice
1373 * target, allow the join and enforce the join method restriction.
1374 *
1375 * If our inner rel mentions a superset of the target relations, allow the
1376 * join. The join we care about has already taken place, and this advice
1377 * imposes no further restrictions.
1378 */
1380 rids + outer_count,
1381 target);
1383 {
1384 entry->flags |= PGPA_FB_MATCH_FULL;
1385 *restrict_method = true;
1386 return true;
1387 }
1389 return true;
1390
1391 /*
1392 * If our outer rel mentions a superset of the relations in the advice
1393 * target, no restrictions apply, because the join we care about has
1394 * already taken place.
1395 *
1396 * On the other hand, if our outer rel mentions exactly the relations
1397 * mentioned in the advice target, the planner is trying to reverse the
1398 * sides of the join as compared with our desired outcome. Reject that.
1399 */
1401 rids, target);
1403 return true;
1404 else if (outer_itm == PGPA_ITM_EQUAL)
1405 return false;
1406
1407 /*
1408 * If the advice target mentions only a single relation, the test below
1409 * cannot ever pass, so save some work by exiting now.
1410 */
1411 if (target->ttype == PGPA_TARGET_IDENTIFIER)
1412 return false;
1413
1414 /*
1415 * If everything in the joinrel appears in the advice target, we're below
1416 * the level of the join we want to control.
1417 *
1418 * For example, HASH_JOIN((x y)) doesn't restrict how x and y can be
1419 * joined.
1420 *
1421 * This lookup shouldn't return PGPA_ITM_DISJOINT, because any such advice
1422 * should not have been returned from the trove in the first place.
1423 */
1424 join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1425 rids, target);
1429 return true;
1430
1431 /*
1432 * We've already permitted all allowable cases, so reject this.
1433 *
1434 * If we reach this point, then the advice overlaps with this join but
1435 * isn't entirely contained within either side, and there's also at least
1436 * one relation present in the join that isn't mentioned by the advice.
1437 *
1438 * For instance, in the HASH_JOIN((x y)) example, we would reach here if x
1439 * were on one side of the join, y on the other, and at least one of the
1440 * two sides also included some other relation, say t. In that case,
1441 * accepting this join would allow the (x y t) joinrel to contain
1442 * non-disabled paths that do not put (x y) on the inner side of a hash
1443 * join; we could instead end up with something like (x JOIN t) JOIN y.
1444 */
1445 return false;
1446}
#define PGPA_FB_MATCH_FULL
#define PGPA_FB_MATCH_PARTIAL
pgpa_itm_type pgpa_identifiers_match_target(int nrids, pgpa_identifier *rids, pgpa_advice_target *target)
Definition pgpa_ast.c:283
@ PGPA_TARGET_IDENTIFIER
Definition pgpa_ast.h:27
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
pgpa_target_type ttype
Definition pgpa_ast.h:49
pgpa_advice_target * target
Definition pgpa_trove.h:29
int flags
Definition pgpa_trove.h:30

References Assert, fb(), pgpa_trove_entry::flags, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_identifiers_match_target(), PGPA_ITM_DISJOINT, PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_TARGET_IDENTIFIER, pgpa_trove_entry::target, and pgpa_advice_target::ttype.

Referenced by pgpa_planner_apply_join_path_advice().

◆ pgpa_join_order_permits_join()

static pgpa_jo_outcome pgpa_join_order_permits_join ( int  outer_count,
int  inner_count,
pgpa_identifier rids,
pgpa_trove_entry entry 
)
static

Definition at line 1190 of file pgpa_planner.c.

1193{
1194 bool loop = true;
1195 bool sublist = false;
1196 int length;
1197 int outer_length;
1198 pgpa_advice_target *target = entry->target;
1200
1201 /* We definitely have at least a partial match for this trove entry. */
1202 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1203
1204 /*
1205 * Find the innermost sublist that contains all keys; if no sublist does,
1206 * then continue processing with the toplevel list.
1207 *
1208 * For example, if the advice says JOIN_ORDER(t1 t2 (t3 t4 t5)), then we
1209 * should evaluate joins that only involve t3, t4, and/or t5 against the
1210 * (t3 t4 t5) sublist, and others against the full list.
1211 *
1212 * Note that (1) outermost sublist is always ordered and (2) whenever we
1213 * zoom into an unordered sublist, we instantly return
1214 * PGPA_JO_INDIFFERENT.
1215 */
1216 while (loop)
1217 {
1219
1220 loop = false;
1222 {
1224
1226 continue;
1227
1228 itm = pgpa_identifiers_match_target(outer_count + inner_count,
1229 rids, child_target);
1231 {
1233 {
1234 target = child_target;
1235 sublist = true;
1236 loop = true;
1237 break;
1238 }
1239 else
1240 {
1242 return PGPA_JO_INDIFFERENT;
1243 }
1244 }
1245 }
1246 }
1247
1248 /*
1249 * Try to find a prefix of the selected join order list that is exactly
1250 * equal to the outer side of the proposed join.
1251 */
1252 length = list_length(target->children);
1255 for (outer_length = 1; outer_length <= length; ++outer_length)
1256 {
1258
1259 /* Avoid leaking memory in every loop iteration. */
1260 if (prefix_target->children != NULL)
1261 list_free(prefix_target->children);
1262 prefix_target->children = list_copy_head(target->children,
1263 outer_length);
1264
1265 /* Search, hoping to find an exact match. */
1266 itm = pgpa_identifiers_match_target(outer_count, rids, prefix_target);
1267 if (itm == PGPA_ITM_EQUAL)
1268 break;
1269
1270 /*
1271 * If the prefix of the join order list that we're considering
1272 * includes some but not all of the outer rels, we can make the prefix
1273 * longer to find an exact match. But if the advice hasn't mentioned
1274 * everything that's part of our outer rel yet, but has mentioned
1275 * things that are not, then this join doesn't match the join order
1276 * list.
1277 */
1279 return PGPA_JO_DENIED;
1280 }
1281
1282 /*
1283 * If the previous loop stopped before the prefix_target included the
1284 * entire join order list, then the next member of the join order list
1285 * must exactly match the inner side of the join.
1286 *
1287 * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), if the outer side of the
1288 * current join includes only t1, then the inner side must be exactly t2;
1289 * if the outer side includes both t1 and t2, then the inner side must
1290 * include exactly t3, t4, and t5.
1291 */
1292 if (outer_length < length)
1293 {
1296
1298
1299 itm = pgpa_identifiers_match_target(inner_count, rids + outer_count,
1300 inner_target);
1301
1302 /*
1303 * Before returning, consider whether we need to mark this entry as
1304 * fully matched. If we're considering the full list rather than a
1305 * sublist, and if we found every item but one on the outer side of
1306 * the join and the last item on the inner side of the join, then the
1307 * answer is yes.
1308 */
1309 if (!sublist && outer_length + 1 == length && itm == PGPA_ITM_EQUAL)
1310 entry->flags |= PGPA_FB_MATCH_FULL;
1311
1313 }
1314
1315 /*
1316 * If we get here, then the outer side of the join includes the entirety
1317 * of the join order list. In this case, we behave differently depending
1318 * on whether we're looking at the top-level join order list or sublist.
1319 * At the top-level, we treat the specified list as mandating that the
1320 * actual join order has the given list as a prefix, but a sublist
1321 * requires an exact match.
1322 *
1323 * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), we must start by joining
1324 * all five of those relations and in that sequence, but once that is
1325 * done, it's OK to join any other rels that are part of the join problem.
1326 * This allows a user to specify the driving table and perhaps the first
1327 * few things to which it should be joined while leaving the rest of the
1328 * join order up the optimizer. But it seems like it would be surprising,
1329 * given that specification, if the user could add t6 to the (t3 t4 t5)
1330 * sub-join, so we don't allow that. If we did want to allow it, the logic
1331 * earlier in this function would require substantial adjustment: we could
1332 * allow the t3-t4-t5-t6 join to be built here, but the next step of
1333 * joining t1-t2 to the result would still be rejected.
1334 */
1335 if (!sublist)
1336 entry->flags |= PGPA_FB_MATCH_FULL;
1338}
void list_free(List *list)
Definition list.c:1546
List * list_copy_head(const List *oldlist, int len)
Definition list.c:1593
static int list_length(const List *l)
Definition pg_list.h:152
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
@ PGPA_TARGET_UNORDERED_LIST
Definition pgpa_ast.h:29
@ PGPA_TARGET_ORDERED_LIST
Definition pgpa_ast.h:28

References Assert, pgpa_advice_target::children, fb(), pgpa_trove_entry::flags, foreach_ptr, list_copy_head(), list_free(), list_length(), list_nth(), palloc0_object, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_identifiers_match_target(), PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_JO_DENIED, PGPA_JO_INDIFFERENT, PGPA_JO_PERMITTED, PGPA_TARGET_IDENTIFIER, PGPA_TARGET_ORDERED_LIST, PGPA_TARGET_UNORDERED_LIST, pgpa_trove_entry::target, and pgpa_advice_target::ttype.

Referenced by pgpa_planner_apply_join_path_advice().

◆ pgpa_join_path_setup()

static void pgpa_join_path_setup ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 528 of file pgpa_planner.c.

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
553 /*
554 * Get or create a pgpa_planner_info object, and then add the
555 * relids from the unique side to proot->sj_unique_rels.
556 *
557 * We must be careful here to use a sufficiently long-lived
558 * context, since we might have been called by GEQO. We want all
559 * the data we store here (including the proot, if we create it)
560 * to last for as long as the pgpa_planner_state.
561 */
562 oldcontext = MemoryContextSwitchTo(pps->mcxt);
564 if (!list_member(proot->sj_unique_rels, uniquerel->relids))
565 proot->sj_unique_rels = lappend(proot->sj_unique_rels,
566 bms_copy(uniquerel->relids));
567 MemoryContextSwitchTo(oldcontext);
568 }
569 }
570
571 /* Get our private state information for this join. */
572 pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
573
574 /* If there is relevant advice, call a helper function to apply it. */
575 if (pjs != NULL)
576 {
578
580 &extra->pgs_mask,
581 root->plan_name,
582 pjs);
583
584 /* Emit debugging message, if enabled. */
586 {
587 if (root->plan_name != NULL)
589 (errmsg("strategy mask for %s join on %s with outer %s and inner %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
590 pgpa_jointype_to_cstring(jointype),
591 pgpa_bms_to_cstring(joinrel->relids),
592 pgpa_bms_to_cstring(outerrel->relids),
593 pgpa_bms_to_cstring(innerrel->relids),
594 root->plan_name,
596 extra->pgs_mask)));
597 else
599 (errmsg("strategy mask for %s join on %s with outer %s and inner %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
600 pgpa_jointype_to_cstring(jointype),
601 pgpa_bms_to_cstring(joinrel->relids),
602 pgpa_bms_to_cstring(outerrel->relids),
603 pgpa_bms_to_cstring(innerrel->relids),
605 extra->pgs_mask)));
606 }
607 }
608
609 /* Pass call to previous hook. */
611 (*prev_join_path_setup) (root, joinrel, outerrel, innerrel,
612 jointype, extra);
613}
BMS_Membership bms_membership(const Bitmapset *a)
Definition bitmapset.c:765
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
@ BMS_MULTIPLE
Definition bitmapset.h:73
List * lappend(List *list, void *datum)
Definition list.c:339
bool list_member(const List *list, const void *datum)
Definition list.c:661
@ JOIN_UNIQUE_OUTER
Definition nodes.h:326
@ JOIN_UNIQUE_INNER
Definition nodes.h:327
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static pgpa_join_state * pgpa_get_join_state(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel)
static char * pgpa_bms_to_cstring(Bitmapset *bms)
static const char * pgpa_jointype_to_cstring(JoinType jointype)
static void pgpa_planner_apply_join_path_advice(JoinType jointype, uint64 *pgs_mask_p, char *plan_name, pgpa_join_state *pjs)
static join_path_setup_hook_type prev_join_path_setup

References Assert, bms_copy(), bms_membership(), BMS_MULTIPLE, ereport, errmsg, fb(), GetPlannerGlobalExtensionState(), JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, lappend(), list_member(), MemoryContextSwitchTo(), pg_plan_advice_trace_mask, pgpa_bms_to_cstring(), pgpa_get_join_state(), pgpa_jointype_to_cstring(), pgpa_planner_apply_join_path_advice(), pgpa_planner_get_proot(), JoinPathExtraData::pgs_mask, planner_extension_id, prev_join_path_setup, RelOptInfo::relids, root, and WARNING.

Referenced by pgpa_planner_install_hooks().

◆ pgpa_join_strategy_mask_from_advice_tag()

static uint64 pgpa_join_strategy_mask_from_advice_tag ( pgpa_advice_tag_type  tag)
static

Definition at line 1151 of file pgpa_planner.c.

1152{
1153 switch (tag)
1154 {
1156 return PGS_FOREIGNJOIN;
1158 return PGS_MERGEJOIN_PLAIN;
1162 return PGS_NESTLOOP_PLAIN;
1166 return PGS_NESTLOOP_MEMOIZE;
1167 case PGPA_TAG_HASH_JOIN:
1168 return PGS_HASHJOIN;
1169 default:
1170 return 0;
1171 }
1172}
#define PGS_NESTLOOP_MEMOIZE
Definition pathnodes.h:76
#define PGS_FOREIGNJOIN
Definition pathnodes.h:71
#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_NESTLOOP_PLAIN
Definition pathnodes.h:74
@ PGPA_TAG_NESTED_LOOP_MATERIALIZE
Definition pgpa_ast.h:93
@ PGPA_TAG_MERGE_JOIN_PLAIN
Definition pgpa_ast.h:92
@ PGPA_TAG_NESTED_LOOP_MEMOIZE
Definition pgpa_ast.h:94
@ PGPA_TAG_HASH_JOIN
Definition pgpa_ast.h:87
@ 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

References PGPA_TAG_FOREIGN_JOIN, PGPA_TAG_HASH_JOIN, PGPA_TAG_MERGE_JOIN_MATERIALIZE, PGPA_TAG_MERGE_JOIN_PLAIN, PGPA_TAG_NESTED_LOOP_MATERIALIZE, PGPA_TAG_NESTED_LOOP_MEMOIZE, PGPA_TAG_NESTED_LOOP_PLAIN, PGS_FOREIGNJOIN, PGS_HASHJOIN, PGS_MERGEJOIN_MATERIALIZE, PGS_MERGEJOIN_PLAIN, PGS_NESTLOOP_MATERIALIZE, PGS_NESTLOOP_MEMOIZE, and PGS_NESTLOOP_PLAIN.

Referenced by pgpa_planner_apply_join_path_advice().

◆ pgpa_joinrel_setup()

static void pgpa_joinrel_setup ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
SpecialJoinInfo sjinfo,
List restrictlist 
)
static

Definition at line 478 of file pgpa_planner.c.

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}
static joinrel_setup_hook_type prev_joinrel_setup
static void pgpa_planner_apply_joinrel_advice(uint64 *pgs_mask_p, char *plan_name, pgpa_join_state *pjs)

References Assert, bms_membership(), BMS_MULTIPLE, ereport, errmsg, fb(), pg_plan_advice_trace_mask, pgpa_bms_to_cstring(), pgpa_get_join_state(), pgpa_planner_apply_joinrel_advice(), RelOptInfo::pgs_mask, prev_joinrel_setup, RelOptInfo::relids, root, and WARNING.

Referenced by pgpa_planner_install_hooks().

◆ pgpa_jointype_to_cstring()

static const char * pgpa_jointype_to_cstring ( JoinType  jointype)
static

Definition at line 2193 of file pgpa_planner.c.

2194{
2195 switch (jointype)
2196 {
2197 case JOIN_INNER:
2198 return "inner";
2199 case JOIN_LEFT:
2200 return "left";
2201 case JOIN_FULL:
2202 return "full";
2203 case JOIN_RIGHT:
2204 return "right";
2205 case JOIN_SEMI:
2206 return "semi";
2207 case JOIN_ANTI:
2208 return "anti";
2209 case JOIN_RIGHT_SEMI:
2210 return "right semi";
2211 case JOIN_RIGHT_ANTI:
2212 return "right anti";
2213 case JOIN_UNIQUE_OUTER:
2214 return "unique outer";
2215 case JOIN_UNIQUE_INNER:
2216 return "unique inner";
2217 }
2218 return "???";
2219}
@ 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_RIGHT_ANTI
Definition nodes.h:320
@ JOIN_ANTI
Definition nodes.h:318

References JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_RIGHT_ANTI, JOIN_RIGHT_SEMI, JOIN_SEMI, JOIN_UNIQUE_INNER, and JOIN_UNIQUE_OUTER.

Referenced by pgpa_join_path_setup().

◆ pgpa_opaque_join_permits_join()

static bool pgpa_opaque_join_permits_join ( int  outer_count,
int  inner_count,
pgpa_identifier rids,
pgpa_trove_entry entry,
bool restrict_method 
)
static

Definition at line 1462 of file pgpa_planner.c.

1466{
1467 pgpa_advice_target *target = entry->target;
1469
1470 /* We definitely have at least a partial match for this trove entry. */
1471 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1472
1473 *restrict_method = false;
1474
1475 join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1476 rids, target);
1477 if (join_itm == PGPA_ITM_EQUAL)
1478 {
1479 /*
1480 * We have an exact match, and should therefore allow the join and
1481 * enforce the use of the relevant opaque join method.
1482 */
1483 entry->flags |= PGPA_FB_MATCH_FULL;
1484 *restrict_method = true;
1485 return true;
1486 }
1487
1490 {
1491 /*
1492 * If join_itm == PGPA_ITM_TARGETS_ARE_SUBSET, then the join we care
1493 * about has already taken place and no further restrictions apply.
1494 *
1495 * If join_itm == PGPA_ITM_KEYS_ARE_SUBSET, we're still building up to
1496 * the join we care about and have not introduced any extraneous
1497 * relations not named in the advice. Note that ForeignScan paths for
1498 * joins are built up from ForeignScan paths from underlying joins and
1499 * scans, so we must not disable this join when considering a subset
1500 * of the relations we ultimately want.
1501 */
1502 return true;
1503 }
1504
1505 /*
1506 * The advice overlaps the join, but at least one relation is present in
1507 * the join that isn't mentioned by the advice. We want to disable such
1508 * paths so that we actually push down the join as intended.
1509 */
1510 return false;
1511}

References fb(), pgpa_trove_entry::flags, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_identifiers_match_target(), PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, and pgpa_trove_entry::target.

Referenced by pgpa_planner_apply_join_path_advice().

◆ pgpa_planner_append_feedback()

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

Definition at line 1861 of file pgpa_planner.c.

1865{
1866 pgpa_trove_entry *entries;
1867 int nentries;
1868
1869 pgpa_trove_lookup_all(trove, type, &entries, &nentries);
1870 for (int i = 0; i < nentries; ++i)
1871 {
1872 pgpa_trove_entry *entry = &entries[i];
1873 DefElem *item;
1874
1875 /*
1876 * If this entry was fully matched, check whether generating advice
1877 * from this plan would produce such an entry. If not, label the entry
1878 * as failed.
1879 */
1880 if ((entry->flags & PGPA_FB_MATCH_FULL) != 0 &&
1882 entry->tag, entry->target))
1883 entry->flags |= PGPA_FB_FAILED;
1884
1886 (Node *) makeInteger(entry->flags), -1);
1887 list = lappend(list, item);
1888 }
1889
1890 return list;
1891}
int i
Definition isn.c:77
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition makefuncs.c:637
#define PGPA_FB_FAILED
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
bool pgpa_walker_would_advise(pgpa_plan_walker_context *walker, pgpa_identifier *rt_identifiers, pgpa_advice_tag_type tag, pgpa_advice_target *target)
Definition nodes.h:135
Definition pgpa_trove.h:27
pgpa_advice_tag_type tag
Definition pgpa_trove.h:28
Integer * makeInteger(int i)
Definition value.c:23
const char * type

References fb(), pgpa_trove_entry::flags, i, lappend(), makeDefElem(), makeInteger(), pgpa_cstring_trove_entry(), PGPA_FB_FAILED, PGPA_FB_MATCH_FULL, pgpa_trove_lookup_all(), pgpa_walker_would_advise(), pgpa_trove_entry::tag, pgpa_trove_entry::target, and type.

Referenced by pgpa_planner_shutdown().

◆ pgpa_planner_apply_join_path_advice()

static void pgpa_planner_apply_join_path_advice ( JoinType  jointype,
uint64 pgs_mask_p,
char plan_name,
pgpa_join_state pjs 
)
static

Definition at line 932 of file pgpa_planner.c.

935{
936 int i = -1;
941 bool jm_conflict = false;
942 uint64 join_mask = 0;
945
946 /*
947 * Reconsider PARTITIONWISE(...) advice.
948 *
949 * We already thought about this for the joinrel as a whole, but in some
950 * cases, partitionwise advice can also constrain the join order. For
951 * instance, if the advice says PARTITIONWISE((t1 t2)), we shouldn't build
952 * join paths for any joinrel that includes t1 or t2 unless it also
953 * includes the other. In general, the partitionwise operation must have
954 * already been completed within one side of the current join or the
955 * other, else the join order is impermissible.
956 *
957 * NB: It might seem tempting to try to deal with PARTITIONWISE advice
958 * entirely in this function, but that doesn't work. Here, we can only
959 * affect the pgs_mask within a particular JoinPathExtraData, that is, for
960 * a particular choice of innerrel and outerrel. Partitionwise paths are
961 * not built that way, so we must set pgs_mask for the RelOptInfo, which
962 * is best done in pgpa_planner_apply_joinrel_advice.
963 */
964 while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
965 {
966 pgpa_trove_entry *entry = &pjs->rel_entries[i];
969
970 if (entry->tag != PGPA_TAG_PARTITIONWISE)
971 continue;
972
974 pjs->rids, entry->target);
975 if (outer_itm == PGPA_ITM_EQUAL ||
977 continue;
978
980 pjs->rids + pjs->outer_count,
981 entry->target);
982 if (inner_itm == PGPA_ITM_EQUAL ||
984 continue;
985
987 }
988
989 /* Iterate over advice that pertains to the join order and method. */
990 i = -1;
991 while ((i = bms_next_member(pjs->join_indexes, i)) >= 0)
992 {
993 pgpa_trove_entry *entry = &pjs->join_entries[i];
995
996 /* Handle join order advice. */
997 if (entry->tag == PGPA_TAG_JOIN_ORDER)
998 {
1000
1002 pjs->inner_count,
1003 pjs->rids,
1004 entry);
1007 else if (jo_outcome == PGPA_JO_DENIED)
1009 continue;
1010 }
1011
1012 /* Handle join method advice. */
1014 if (my_join_mask != 0)
1015 {
1016 bool permit;
1017 bool restrict_method;
1018
1019 if (entry->tag == PGPA_TAG_FOREIGN_JOIN)
1021 pjs->inner_count,
1022 pjs->rids,
1023 entry,
1025 else
1027 pjs->inner_count,
1028 pjs->rids,
1029 entry,
1031 if (!permit)
1033 else if (restrict_method)
1034 {
1036 if (join_mask != 0 && join_mask != my_join_mask)
1037 jm_conflict = true;
1039 }
1040 continue;
1041 }
1042
1043 /* Handle semijoin uniqueness advice. */
1044 if (entry->tag == PGPA_TAG_SEMIJOIN_UNIQUE ||
1046 {
1048 bool restrict_method;
1049
1050 /* Planner has nullable side of the semijoin on the outer side? */
1051 outer_side_nullable = (jointype == JOIN_UNIQUE_OUTER ||
1052 jointype == JOIN_RIGHT_SEMI);
1053
1054 if (!pgpa_semijoin_permits_join(pjs->outer_count,
1055 pjs->inner_count,
1056 pjs->rids,
1057 entry,
1061 else if (restrict_method)
1062 {
1063 bool advice_unique;
1064 bool jt_unique;
1065 bool jt_non_unique;
1066
1067 /* Advice wants to unique-ify and use a regular join? */
1069
1070 /* Planner is trying to unique-ify and use a regular join? */
1071 jt_unique = (jointype == JOIN_UNIQUE_INNER ||
1072 jointype == JOIN_UNIQUE_OUTER);
1073
1074 /* Planner is trying a semi-join, without unique-ifying? */
1075 jt_non_unique = (jointype == JOIN_SEMI ||
1076 jointype == JOIN_RIGHT_SEMI);
1077
1078 if (!jt_unique && !jt_non_unique)
1079 {
1080 /*
1081 * This doesn't seem to be a semijoin to which SJ_UNIQUE
1082 * or SJ_NON_UNIQUE can be applied.
1083 */
1084 entry->flags |= PGPA_FB_INAPPLICABLE;
1085 }
1086 else if (advice_unique != jt_unique)
1088 else
1090 }
1091 continue;
1092 }
1093 }
1094
1095 /*
1096 * If the advice indicates both that this join order is permissible and
1097 * also that it isn't, then mark advice related to the join order as
1098 * conflicting.
1099 */
1100 if (jo_permit_indexes != NULL &&
1102 {
1109 }
1110
1111 /*
1112 * If more than one join method specification is relevant here and they
1113 * differ, mark them all as conflicting.
1114 */
1115 if (jm_conflict)
1116 pgpa_trove_set_flags(pjs->join_entries, jm_indexes,
1118
1119 /* If semijoin advice says both yes and no, mark it all as conflicting. */
1121 {
1126 }
1127
1128 /*
1129 * Enforce restrictions on the join order and join method, and any
1130 * semijoin-related restrictions. Only clear bits here, so that we still
1131 * respect the enable_* GUCs. Do nothing in cases where the advice on a
1132 * single topic conflicts.
1133 */
1137 if (join_mask != 0 && !jm_conflict)
1141}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define PGS_JOIN_ANY
Definition pathnodes.h:96
#define PGPA_FB_INAPPLICABLE
#define PGPA_FB_CONFLICTING
@ PGPA_TAG_SEMIJOIN_NON_UNIQUE
Definition pgpa_ast.h:98
@ PGPA_TAG_PARTITIONWISE
Definition pgpa_ast.h:97
@ PGPA_TAG_SEMIJOIN_UNIQUE
Definition pgpa_ast.h:99
@ PGPA_TAG_JOIN_ORDER
Definition pgpa_ast.h:90
static bool pgpa_opaque_join_permits_join(int outer_count, int inner_count, pgpa_identifier *rids, pgpa_trove_entry *entry, bool *restrict_method)
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 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)
void pgpa_trove_set_flags(pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
Definition pgpa_trove.c:328

References bms_add_member(), bms_next_member(), fb(), pgpa_trove_entry::flags, i, JOIN_RIGHT_SEMI, JOIN_SEMI, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, PGPA_FB_CONFLICTING, PGPA_FB_INAPPLICABLE, pgpa_identifiers_match_target(), PGPA_ITM_EQUAL, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_JO_DENIED, PGPA_JO_PERMITTED, pgpa_join_method_permits_join(), pgpa_join_order_permits_join(), pgpa_join_strategy_mask_from_advice_tag(), pgpa_opaque_join_permits_join(), pgpa_semijoin_permits_join(), PGPA_TAG_FOREIGN_JOIN, PGPA_TAG_JOIN_ORDER, PGPA_TAG_PARTITIONWISE, PGPA_TAG_SEMIJOIN_NON_UNIQUE, PGPA_TAG_SEMIJOIN_UNIQUE, pgpa_trove_set_flags(), PGS_JOIN_ANY, pgpa_trove_entry::tag, and pgpa_trove_entry::target.

Referenced by pgpa_join_path_setup().

◆ pgpa_planner_apply_joinrel_advice()

static void pgpa_planner_apply_joinrel_advice ( uint64 pgs_mask_p,
char plan_name,
pgpa_join_state pjs 
)
static

Definition at line 736 of file pgpa_planner.c.

738{
739 int i = -1;
740 int flags;
741 bool gather_conflict = false;
745 bool partitionwise_conflict = false;
746 int partitionwise_outcome = 0;
749
750 /* Iterate over all possibly-relevant advice. */
751 while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
752 {
753 pgpa_trove_entry *entry = &pjs->rel_entries[i];
755 bool full_match = false;
757 int my_partitionwise_outcome = 0; /* >0 yes, <0 no */
758
759 /*
760 * For GATHER and GATHER_MERGE, if the specified relations exactly
761 * match this joinrel, do whatever the advice says; otherwise, don't
762 * allow Gather or Gather Merge at this level. For NO_GATHER, there
763 * must be a single target relation which must be included in this
764 * joinrel, so just don't allow Gather or Gather Merge here, full
765 * stop.
766 */
767 if (entry->tag == PGPA_TAG_NO_GATHER)
768 {
770 full_match = true;
771 }
772 else
773 {
774 int total_count;
775
776 total_count = pjs->outer_count + pjs->inner_count;
778 entry->target);
780
781 if (itm == PGPA_ITM_EQUAL)
782 {
783 full_match = true;
784 if (entry->tag == PGPA_TAG_PARTITIONWISE)
786 else if (entry->tag == PGPA_TAG_GATHER)
788 else if (entry->tag == PGPA_TAG_GATHER_MERGE)
790 else
791 elog(ERROR, "unexpected advice tag: %d",
792 (int) entry->tag);
793 }
794 else
795 {
796 /*
797 * If specified relations don't exactly match this joinrel,
798 * then we should do the opposite of whatever the advice says.
799 * For instance, if we have PARTITIONWISE((a b c)) or
800 * GATHER((a b c)) and this joinrel covers {a, b} or {a, b, c,
801 * d} or {a, d}, we shouldn't plan it partitionwise or put a
802 * Gather or Gather Merge on it here.
803 *
804 * Also, we can't put a Gather or Gather Merge at this level
805 * if there is PARTITIONWISE advice that overlaps with it,
806 * unless the PARTITIONWISE advice covers a subset of the
807 * relations in the joinrel. To continue the previous example,
808 * PARTITIONWISE((a b c)) is logically incompatible with
809 * GATHER((a b)) or GATHER((a d)), but not with GATHER((a b c
810 * d)).
811 *
812 * Conversely, we can't proceed partitionwise at this level if
813 * there is overlapping GATHER or GATHER_MERGE advice, unless
814 * that advice covers a superset of the relations in this
815 * joinrel. This is just the flip side of the preceding point.
816 */
817 if (entry->tag == PGPA_TAG_PARTITIONWISE)
818 {
822 }
823 else if (entry->tag == PGPA_TAG_GATHER ||
824 entry->tag == PGPA_TAG_GATHER_MERGE)
825 {
829 }
830 else
831 elog(ERROR, "unexpected advice tag: %d",
832 (int) entry->tag);
833 }
834 }
835
836 /*
837 * If we set my_gather_mask up above, then we (1) make a note if the
838 * advice conflicted, (2) remember the mask value, and (3) remember
839 * whether this was a full or partial match.
840 */
841 if (my_gather_mask != 0)
842 {
844 gather_conflict = true;
846 if (full_match)
848 else
850 }
851
852 /*
853 * Likewise, if we set my_partitionwise_outcome up above, then we (1)
854 * make a note if the advice conflicted, (2) remember what the desired
855 * outcome was, and (3) remember whether this was a full or partial
856 * match.
857 */
859 {
860 if (partitionwise_outcome != 0 &&
864 if (full_match)
867 else
870 }
871 }
872
873 /*
874 * Mark every Gather-related piece of advice as partially matched, and if
875 * the set of targets exactly matched this relation, fully matched. If
876 * there was a conflict, mark them all as conflicting.
877 */
878 flags = PGPA_FB_MATCH_PARTIAL;
879 if (gather_conflict)
880 flags |= PGPA_FB_CONFLICTING;
881 pgpa_trove_set_flags(pjs->rel_entries, gather_partial_match, flags);
882 flags |= PGPA_FB_MATCH_FULL;
883 pgpa_trove_set_flags(pjs->rel_entries, gather_full_match, flags);
884
885 /* Likewise for partitionwise advice. */
886 flags = PGPA_FB_MATCH_PARTIAL;
888 flags |= PGPA_FB_CONFLICTING;
890 flags |= PGPA_FB_MATCH_FULL;
892
893 /*
894 * Enforce restrictions on the Gather/Gather Merge. Only clear bits here,
895 * so that we still respect the enable_* GUCs. Do nothing if the advice
896 * conflicts.
897 */
898 if (gather_mask != 0 && !gather_conflict)
899 {
901
905 }
906
907 /*
908 * As above, but for partitionwise advice.
909 *
910 * To induce a partitionwise join, we disable all the ordinary means of
911 * performing a join, so that an Append or MergeAppend path will hopefully
912 * be chosen.
913 *
914 * To prevent one, we just disable Append and MergeAppend. Note that we
915 * must not unset PGS_CONSIDER_PARTITIONWISE even when we don't want a
916 * partitionwise join here, because we might want one at a higher level
917 * that will construct its own paths using the ones from this level.
918 */
920 {
921 if (partitionwise_outcome > 0)
923 else
925 }
926}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define PGS_APPEND
Definition pathnodes.h:78
#define PGS_MERGE_APPEND
Definition pathnodes.h:79
#define PGS_CONSIDER_NONPARTIAL
Definition pathnodes.h:84
#define PGS_GATHER
Definition pathnodes.h:80
#define PGS_GATHER_MERGE
Definition pathnodes.h:81
@ PGPA_TAG_GATHER_MERGE
Definition pgpa_ast.h:86
@ PGPA_TAG_GATHER
Definition pgpa_ast.h:85
@ PGPA_TAG_NO_GATHER
Definition pgpa_ast.h:96

References Assert, bms_add_member(), bms_next_member(), elog, ERROR, fb(), i, PGPA_FB_CONFLICTING, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_identifiers_match_target(), PGPA_ITM_DISJOINT, PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_TAG_GATHER, PGPA_TAG_GATHER_MERGE, PGPA_TAG_NO_GATHER, PGPA_TAG_PARTITIONWISE, pgpa_trove_set_flags(), PGS_APPEND, PGS_CONSIDER_NONPARTIAL, PGS_GATHER, PGS_GATHER_MERGE, PGS_MERGE_APPEND, pgpa_trove_entry::tag, and pgpa_trove_entry::target.

Referenced by pgpa_joinrel_setup().

◆ pgpa_planner_apply_scan_advice()

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

Definition at line 1617 of file pgpa_planner.c.

1622{
1625 bool gather_conflict = false;
1628 int i = -1;
1630 int flags;
1631 bool scan_type_conflict = false;
1634 uint64 gather_mask = 0;
1635 uint64 scan_type = all_scan_mask; /* sentinel: no advice yet */
1636
1637 /* Scrutinize available scan advice. */
1638 while ((i = bms_next_member(scan_indexes, i)) >= 0)
1639 {
1642
1643 /* Translate our advice tags to a scan strategy advice value. */
1644 if (my_entry->tag == PGPA_TAG_DO_NOT_SCAN)
1645 my_scan_type = 0;
1646 else if (my_entry->tag == PGPA_TAG_BITMAP_HEAP_SCAN)
1647 {
1648 /*
1649 * Currently, PGS_CONSIDER_INDEXONLY can suppress Bitmap Heap
1650 * Scans, so don't clear it when such a scan is requested. This
1651 * happens because build_index_scan() thinks that the possibility
1652 * of an index-only scan is a sufficient reason to consider using
1653 * an otherwise-useless index, and get_index_paths() thinks that
1654 * the same paths that are useful for index or index-only scans
1655 * should also be considered for bitmap scans. Perhaps that logic
1656 * should be tightened up, but until then we need to include
1657 * PGS_CONSIDER_INDEXONLY in my_scan_type here.
1658 */
1660 }
1661 else if (my_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN)
1663 else if (my_entry->tag == PGPA_TAG_INDEX_SCAN)
1665 else if (my_entry->tag == PGPA_TAG_SEQ_SCAN)
1667 else if (my_entry->tag == PGPA_TAG_TID_SCAN)
1669
1670 /*
1671 * If this is understandable scan advice, hang on to the entry, the
1672 * inferred scan type, and the index at which we found it.
1673 *
1674 * Also make a note if we see conflicting scan type advice. Note that
1675 * we regard two index specifications as conflicting unless they match
1676 * exactly. In theory, perhaps we could regard INDEX_SCAN(a c) and
1677 * INDEX_SCAN(a b.c) as non-conflicting if it happens that the only
1678 * index named c is in schema b, but it doesn't seem worth the code.
1679 */
1681 {
1683 scan_type_conflict = true;
1684 if (!scan_type_conflict && scan_entry != NULL &&
1685 my_entry->target->itarget != NULL &&
1686 scan_entry->target->itarget != NULL &&
1687 !pgpa_index_targets_equal(scan_entry->target->itarget,
1688 my_entry->target->itarget))
1689 scan_type_conflict = true;
1693 }
1694 }
1695
1696 /* Scrutinize available gather-related and partitionwise advice. */
1697 i = -1;
1698 while ((i = bms_next_member(rel_indexes, i)) >= 0)
1699 {
1700 pgpa_trove_entry *my_entry = &rel_entries[i];
1702 bool just_one_rel;
1703
1705 || list_length(my_entry->target->children) == 1;
1706
1707 /*
1708 * PARTITIONWISE behaves like a scan type, except that if there's more
1709 * than one relation targeted, it has no effect at this level.
1710 */
1711 if (my_entry->tag == PGPA_TAG_PARTITIONWISE)
1712 {
1713 if (just_one_rel)
1714 {
1716
1718 scan_type_conflict = true;
1723 }
1724 continue;
1725 }
1726
1727 /*
1728 * GATHER and GATHER_MERGE applied to a single rel mean that we should
1729 * use the corresponding strategy here, while applying either to more
1730 * than one rel means we should not use those strategies here, but
1731 * rather at the level of the joinrel that corresponds to what was
1732 * specified. NO_GATHER can only be applied to single rels.
1733 *
1734 * Note that setting PGS_CONSIDER_NONPARTIAL in my_gather_mask is
1735 * equivalent to allowing the non-use of either form of Gather here.
1736 */
1737 if (my_entry->tag == PGPA_TAG_GATHER ||
1739 {
1740 if (!just_one_rel)
1742 else if (my_entry->tag == PGPA_TAG_GATHER)
1744 else
1746 }
1747 else if (my_entry->tag == PGPA_TAG_NO_GATHER)
1748 {
1751 }
1752
1753 /*
1754 * If we set my_gather_mask up above, then we (1) make a note if the
1755 * advice conflicted, (2) remember the mask value, and (3) remember
1756 * whether this was a full or partial match.
1757 */
1758 if (my_gather_mask != 0)
1759 {
1760 if (gather_mask != 0 && gather_mask != my_gather_mask)
1761 gather_conflict = true;
1763 if (just_one_rel)
1765 else
1767 }
1768 }
1769
1770 /* Enforce choice of index. */
1771 if (scan_entry != NULL && !scan_type_conflict &&
1774 {
1775 pgpa_index_target *itarget = scan_entry->target->itarget;
1777
1779 {
1780 char *relname = get_rel_name(index->indexoid);
1781 Oid nspoid = get_rel_namespace(index->indexoid);
1782 char *relnamespace = get_namespace_name_or_temp(nspoid);
1783
1784 if (strcmp(itarget->indname, relname) == 0 &&
1785 (itarget->indnamespace == NULL ||
1786 strcmp(itarget->indnamespace, relnamespace) == 0))
1787 {
1789 break;
1790 }
1791 }
1792
1793 if (matched_index == NULL)
1794 {
1795 /* Don't force the scan type if the index doesn't exist. */
1797
1798 /* Mark advice as inapplicable. */
1801 }
1802 else
1803 {
1804 /* Disable every other index. */
1806 {
1807 if (index != matched_index)
1808 index->disabled = true;
1809 }
1810 }
1811 }
1812
1813 /*
1814 * Mark all the scan method entries as fully matched; and if they specify
1815 * different things, mark them all as conflicting.
1816 */
1819 flags |= PGPA_FB_CONFLICTING;
1821 pgpa_trove_set_flags(rel_entries, scan_type_rel_indexes, flags);
1822
1823 /*
1824 * Mark every Gather-related piece of advice as partially matched. Mark
1825 * the ones that included this relation as a target by itself as fully
1826 * matched. If there was a conflict, mark them all as conflicting.
1827 */
1828 flags = PGPA_FB_MATCH_PARTIAL;
1829 if (gather_conflict)
1830 flags |= PGPA_FB_CONFLICTING;
1831 pgpa_trove_set_flags(rel_entries, gather_partial_match, flags);
1832 flags |= PGPA_FB_MATCH_FULL;
1833 pgpa_trove_set_flags(rel_entries, gather_full_match, flags);
1834
1835 /*
1836 * Enforce restrictions on the scan type and use of Gather/Gather Merge.
1837 * Only clear bits here, so that we still respect the enable_* GUCs. Do
1838 * nothing in cases where the advice on a single topic conflicts.
1839 */
1841 rel->pgs_mask &= ~(all_scan_mask & ~scan_type);
1842 if (gather_mask != 0 && !gather_conflict)
1843 {
1845
1849 }
1850}
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
#define PGS_TIDSCAN
Definition pathnodes.h:70
#define PGS_SEQSCAN
Definition pathnodes.h:66
#define PGS_CONSIDER_INDEXONLY
Definition pathnodes.h:82
#define PGS_BITMAPSCAN
Definition pathnodes.h:69
#define PGS_SCAN_ANY
Definition pathnodes.h:89
#define PGS_INDEXONLYSCAN
Definition pathnodes.h:68
#define PGS_INDEXSCAN
Definition pathnodes.h:67
NameData relname
Definition pg_class.h:40
bool pgpa_index_targets_equal(pgpa_index_target *i1, pgpa_index_target *i2)
Definition pgpa_ast.c:218
@ PGPA_TAG_INDEX_SCAN
Definition pgpa_ast.h:89
@ PGPA_TAG_BITMAP_HEAP_SCAN
Definition pgpa_ast.h:82
@ PGPA_TAG_INDEX_ONLY_SCAN
Definition pgpa_ast.h:88
@ PGPA_TAG_SEQ_SCAN
Definition pgpa_ast.h:100
@ PGPA_TAG_DO_NOT_SCAN
Definition pgpa_ast.h:83
@ PGPA_TAG_TID_SCAN
Definition pgpa_ast.h:101
unsigned int Oid
List * indexlist
Definition pathnodes.h:1091
Definition type.h:96
char * indnamespace
Definition pgpa_ast.h:38

References Assert, bms_add_member(), bms_next_member(), fb(), foreach_node, get_namespace_name_or_temp(), get_rel_name(), get_rel_namespace(), i, RelOptInfo::indexlist, pgpa_index_target::indname, pgpa_index_target::indnamespace, list_length(), PGPA_FB_CONFLICTING, PGPA_FB_INAPPLICABLE, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_index_targets_equal(), PGPA_TAG_BITMAP_HEAP_SCAN, PGPA_TAG_DO_NOT_SCAN, PGPA_TAG_GATHER, PGPA_TAG_GATHER_MERGE, PGPA_TAG_INDEX_ONLY_SCAN, PGPA_TAG_INDEX_SCAN, PGPA_TAG_NO_GATHER, PGPA_TAG_PARTITIONWISE, PGPA_TAG_SEQ_SCAN, PGPA_TAG_TID_SCAN, PGPA_TARGET_IDENTIFIER, pgpa_trove_set_flags(), PGS_APPEND, PGS_BITMAPSCAN, PGS_CONSIDER_INDEXONLY, PGS_CONSIDER_NONPARTIAL, PGS_GATHER, PGS_GATHER_MERGE, PGS_INDEXONLYSCAN, PGS_INDEXSCAN, RelOptInfo::pgs_mask, PGS_MERGE_APPEND, PGS_SCAN_ANY, PGS_SEQSCAN, PGS_TIDSCAN, relname, pgpa_trove_entry::target, and pgpa_advice_target::ttype.

Referenced by pgpa_build_simple_rel().

◆ pgpa_planner_feedback_warning()

void pgpa_planner_feedback_warning ( List feedback)

Definition at line 1898 of file pgpa_planner.c.

1899{
1902
1903 /* Quick exit if there's no feedback. */
1904 if (feedback == NIL)
1905 return;
1906
1907 /* Initialize buffers. */
1910
1911 /* Main loop. */
1913 {
1914 int flags = defGetInt32(item);
1915
1916 /*
1917 * Don't emit anything if it was fully matched with no problems found.
1918 *
1919 * NB: Feedback should never be marked fully matched without also
1920 * being marked partially matched.
1921 */
1923 continue;
1924
1925 /*
1926 * Terminate each detail line except the last with a newline. This is
1927 * also a convenient place to reset flagbuf.
1928 */
1929 if (detailbuf.len > 0)
1930 {
1933 }
1934
1935 /* Generate output. */
1937 appendStringInfo(&detailbuf, "advice %s feedback is \"%s\"",
1938 item->defname, flagbuf.data);
1939 }
1940
1941 /* Emit the warning, if any problems were found. */
1942 if (detailbuf.len > 0)
1944 errmsg("supplied plan advice was not enforced"),
1945 errdetail("%s", detailbuf.data));
1946}
int32 defGetInt32(DefElem *def)
Definition define.c:148
int errdetail(const char *fmt,...) pg_attribute_printf(1
void pgpa_trove_append_flags(StringInfo buf, int flags)
Definition pgpa_trove.c:345
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242

References appendStringInfo(), appendStringInfoChar(), defGetInt32(), ereport, errdetail(), errmsg, fb(), foreach_node, initStringInfo(), NIL, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_trove_append_flags(), resetStringInfo(), and WARNING.

Referenced by pgpa_planner_shutdown().

◆ pgpa_planner_get_proot()

static pgpa_planner_info * pgpa_planner_get_proot ( pgpa_planner_state pps,
PlannerInfo root 
)
static

Definition at line 1952 of file pgpa_planner.c.

1953{
1955
1956 /*
1957 * If pps->last_proot isn't populated, there are no pgpa_planner_info
1958 * objects yet, so we can drop through and create a new one. Otherwise,
1959 * search for an object with a matching name, and drop through only if
1960 * none is found.
1961 */
1962 if (pps->last_proot != NULL)
1963 {
1964 if (root->plan_name == NULL)
1965 {
1966 if (pps->last_proot->plan_name == NULL)
1967 return pps->last_proot;
1968
1970 {
1971 if (proot->plan_name == NULL)
1972 {
1973 pps->last_proot = proot;
1974 return proot;
1975 }
1976 }
1977 }
1978 else
1979 {
1980 if (pps->last_proot->plan_name != NULL &&
1981 strcmp(pps->last_proot->plan_name, root->plan_name) == 0)
1982 return pps->last_proot;
1983
1985 {
1986 if (proot->plan_name != NULL &&
1987 strcmp(proot->plan_name, root->plan_name) == 0)
1988 {
1989 pps->last_proot = proot;
1990 return proot;
1991 }
1992 }
1993 }
1994 }
1995
1996 /* Create new object. */
1998
1999 /* Set plan name and alternative plan name. */
2000 new_proot->plan_name = root->plan_name;
2001 new_proot->alternative_plan_name = root->alternative_plan_name;
2002
2003 /*
2004 * If the newly-created proot shares an alternative_plan_name with one or
2005 * more others, all should have the is_alternative_plan flag set.
2006 */
2008 {
2009 if (strings_equal_or_both_null(new_proot->alternative_plan_name,
2010 other_proot->alternative_plan_name))
2011 {
2012 new_proot->is_alternative_plan = true;
2013 other_proot->is_alternative_plan = true;
2014 }
2015 }
2016
2017 /*
2018 * Outermost query level always has rtoffset 0; other rtoffset values are
2019 * computed later.
2020 */
2021 if (root->plan_name == NULL)
2022 {
2023 new_proot->has_rtoffset = true;
2024 new_proot->rtoffset = 0;
2025 }
2026
2027 /* Add to list and make it most recently used. */
2028 pps->proots = lappend(pps->proots, new_proot);
2029 pps->last_proot = new_proot;
2030
2031 return new_proot;
2032}
static bool strings_equal_or_both_null(const char *a, const char *b)

References fb(), foreach_ptr, lappend(), palloc0_object, root, and strings_equal_or_both_null().

Referenced by pgpa_build_simple_rel(), and pgpa_join_path_setup().

◆ pgpa_planner_install_hooks()

void pgpa_planner_install_hooks ( void  )

Definition at line 181 of file pgpa_planner.c.

182{
194}
int GetPlannerExtensionId(const char *extension_name)
Definition extendplan.c:41
join_path_setup_hook_type join_path_setup_hook
Definition joinpath.c:32
static void pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse, const char *query_string, PlannedStmt *pstmt)
static planner_setup_hook_type prev_planner_setup
static void pgpa_build_simple_rel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
static void pgpa_planner_setup(PlannerGlobal *glob, Query *parse, const char *query_string, int cursorOptions, double *tuple_fraction, ExplainState *es)
static planner_shutdown_hook_type prev_planner_shutdown
static void pgpa_join_path_setup(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
static void pgpa_joinrel_setup(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, SpecialJoinInfo *sjinfo, List *restrictlist)
planner_shutdown_hook_type planner_shutdown_hook
Definition planner.c:80
planner_setup_hook_type planner_setup_hook
Definition planner.c:77
joinrel_setup_hook_type joinrel_setup_hook
Definition relnode.c:54
build_simple_rel_hook_type build_simple_rel_hook
Definition relnode.c:51

References build_simple_rel_hook, GetPlannerExtensionId(), join_path_setup_hook, joinrel_setup_hook, pgpa_build_simple_rel(), pgpa_join_path_setup(), pgpa_joinrel_setup(), pgpa_planner_setup(), pgpa_planner_shutdown(), planner_extension_id, planner_setup_hook, planner_shutdown_hook, prev_build_simple_rel, prev_join_path_setup, prev_joinrel_setup, prev_planner_setup, and prev_planner_shutdown.

Referenced by _PG_init().

◆ pgpa_planner_setup()

static void pgpa_planner_setup ( PlannerGlobal glob,
Query parse,
const char query_string,
int  cursorOptions,
double tuple_fraction,
ExplainState es 
)
static

Definition at line 200 of file pgpa_planner.c.

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}
void SetPlannerGlobalExtensionState(PlannerGlobal *glob, int extension_id, void *opaque)
Definition extendplan.c:77
void parse(int)
Definition parse.c:49
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
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
List * pgpa_parse(const char *advice_string, char **error_p)
int pgpa_planner_generate_advice
pgpa_trove * pgpa_build_trove(List *advice_items)
Definition pgpa_trove.c:133
static void error(void)
Definition pg_list.h:54

References CurrentMemoryContext, ereport, errmsg, error(), fb(), NIL, palloc0_object, parse(), pg_plan_advice_always_store_advice_details, pg_plan_advice_feedback_warnings, pg_plan_advice_get_supplied_query_advice(), pg_plan_advice_should_explain(), pgpa_build_trove(), pgpa_parse(), pgpa_planner_generate_advice, planner_extension_id, prev_planner_setup, SetPlannerGlobalExtensionState(), and WARNING.

Referenced by pgpa_planner_install_hooks().

◆ pgpa_planner_shutdown()

static void pgpa_planner_shutdown ( PlannerGlobal glob,
Query parse,
const char query_string,
PlannedStmt pstmt 
)
static

Definition at line 300 of file pgpa_planner.c.

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}
pgpa_identifier * pgpa_create_identifiers_for_planned_stmt(PlannedStmt *pstmt)
void pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker, pgpa_identifier *rt_identifiers)
Definition pgpa_output.c:80
static void pgpa_compute_rt_offsets(pgpa_planner_state *pps, PlannedStmt *pstmt)
void pgpa_planner_feedback_warning(List *feedback)
static void pgpa_validate_rt_identifiers(pgpa_planner_state *pps, PlannedStmt *pstmt)
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)
void pgpa_plan_walker(pgpa_plan_walker_context *walker, PlannedStmt *pstmt, List *proots)
Definition pgpa_walker.c:77
List * extension_state
Definition plannodes.h:165
String * makeString(char *str)
Definition value.c:63

References buf, PlannedStmt::extension_state, fb(), GetPlannerGlobalExtensionState(), initStringInfo(), lappend(), makeDefElem(), makeString(), NIL, parse(), pg_plan_advice_feedback_warnings, pgpa_compute_rt_offsets(), pgpa_create_identifiers_for_planned_stmt(), pgpa_output_advice(), pgpa_plan_walker(), pgpa_planner_append_feedback(), pgpa_planner_feedback_warning(), PGPA_TROVE_LOOKUP_JOIN, PGPA_TROVE_LOOKUP_REL, PGPA_TROVE_LOOKUP_SCAN, pgpa_validate_rt_identifiers(), planner_extension_id, and prev_planner_shutdown.

Referenced by pgpa_planner_install_hooks().

◆ pgpa_semijoin_permits_join()

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

Definition at line 1527 of file pgpa_planner.c.

1532{
1533 pgpa_advice_target *target = entry->target;
1537
1538 *restrict_method = false;
1539
1540 /* We definitely have at least a partial match for this trove entry. */
1541 entry->flags |= PGPA_FB_MATCH_PARTIAL;
1542
1543 /*
1544 * If outer rel is the nullable side and contains exactly the same
1545 * relations as the advice target, then the join order is allowable, but
1546 * the caller must check whether the advice tag (either SEMIJOIN_UNIQUE or
1547 * SEMIJOIN_NON_UNIQUE) matches the join type.
1548 *
1549 * If the outer rel is a superset of the target relations, the join we
1550 * care about has already taken place, so we should impose no further
1551 * restrictions.
1552 */
1554 rids, target);
1556 {
1557 entry->flags |= PGPA_FB_MATCH_FULL;
1559 {
1560 *restrict_method = true;
1561 return true;
1562 }
1563 }
1565 return true;
1566
1567 /* As above, but for the inner rel. */
1569 rids + outer_count,
1570 target);
1572 {
1573 entry->flags |= PGPA_FB_MATCH_FULL;
1574 if (!outer_is_nullable)
1575 {
1576 *restrict_method = true;
1577 return true;
1578 }
1579 }
1581 return true;
1582
1583 /*
1584 * If everything in the joinrel appears in the advice target, we're below
1585 * the level of the join we want to control.
1586 */
1587 join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
1588 rids, target);
1592 return true;
1593
1594 /*
1595 * We've tested for all allowable possibilities, and so must reject this
1596 * join order. This can happen in two ways.
1597 *
1598 * First, we might be considering a semijoin that overlaps incompletely
1599 * with one or both sides of the join. For example, if the user has
1600 * specified SEMIJOIN_UNIQUE((t1 t2)) or SEMIJOIN_NON_UNIQUE((t1 t2)), we
1601 * should reject a proposed t2-t3 join, since that could not result in a
1602 * final plan compatible with the advice.
1603 *
1604 * Second, we might be considering a semijoin where the advice target
1605 * perfectly matches one side of the join, but it's the wrong one. For
1606 * example, in the example above, we might see a 3-way join between t1,
1607 * t2, and t3, with (t1 t2) on the non-nullable side. That, too, would be
1608 * incompatible with the advice.
1609 */
1610 return false;
1611}

References Assert, fb(), pgpa_trove_entry::flags, PGPA_FB_MATCH_FULL, PGPA_FB_MATCH_PARTIAL, pgpa_identifiers_match_target(), PGPA_ITM_DISJOINT, PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, and pgpa_trove_entry::target.

Referenced by pgpa_planner_apply_join_path_advice().

◆ pgpa_validate_rt_identifiers()

static void pgpa_validate_rt_identifiers ( pgpa_planner_state pps,
PlannedStmt pstmt 
)
static

Definition at line 2128 of file pgpa_planner.c.

2129{
2130#ifdef USE_ASSERT_CHECKING
2133
2134 /* Create identifiers from the planned statement. */
2136
2137 /* Iterate over identifiers created during planning, so we can compare. */
2139 {
2140 if (!proot->has_rtoffset)
2141 continue;
2142
2143 for (int rti = 1; rti <= proot->rid_array_size; ++rti)
2144 {
2145 Index flat_rti = proot->rtoffset + rti;
2146 pgpa_identifier *rid1 = &proot->rid_array[rti - 1];
2148
2149 if (rid1->alias_name == NULL)
2150 continue;
2151
2154 Assert(strcmp(rid1->alias_name, rid2->alias_name) == 0);
2155 Assert(rid1->occurrence == rid2->occurrence);
2156 Assert(strings_equal_or_both_null(rid1->partnsp, rid2->partnsp));
2157 Assert(strings_equal_or_both_null(rid1->partrel, rid2->partrel));
2159 rid2->plan_name));
2160 }
2161 }
2162#endif
2163}
unsigned int Index
Definition c.h:698
List * rtable
Definition plannodes.h:107

References Assert, fb(), foreach_ptr, list_length(), pgpa_create_identifiers_for_planned_stmt(), PlannedStmt::rtable, and strings_equal_or_both_null().

Referenced by pgpa_planner_shutdown().

Variable Documentation

◆ pgpa_planner_generate_advice

int pgpa_planner_generate_advice = 0

Definition at line 97 of file pgpa_planner.c.

Referenced by pg_plan_advice_request_advice_generation(), and pgpa_planner_setup().

◆ planner_extension_id

◆ prev_build_simple_rel

build_simple_rel_hook_type prev_build_simple_rel = NULL
static

Definition at line 90 of file pgpa_planner.c.

Referenced by pgpa_build_simple_rel(), and pgpa_planner_install_hooks().

◆ prev_join_path_setup

join_path_setup_hook_type prev_join_path_setup = NULL
static

Definition at line 91 of file pgpa_planner.c.

Referenced by pgpa_join_path_setup(), and pgpa_planner_install_hooks().

◆ prev_joinrel_setup

joinrel_setup_hook_type prev_joinrel_setup = NULL
static

Definition at line 92 of file pgpa_planner.c.

Referenced by pgpa_joinrel_setup(), and pgpa_planner_install_hooks().

◆ prev_planner_setup

planner_setup_hook_type prev_planner_setup = NULL
static

Definition at line 93 of file pgpa_planner.c.

Referenced by pgpa_planner_install_hooks(), and pgpa_planner_setup().

◆ prev_planner_shutdown

planner_shutdown_hook_type prev_planner_shutdown = NULL
static

Definition at line 94 of file pgpa_planner.c.

Referenced by pgpa_planner_install_hooks(), and pgpa_planner_shutdown().