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 void pgpa_planner_feedback_warning (List *feedback)
 
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)
 

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 2166 of file pgpa_planner.c.

2167{
2169 int x = -1;
2170
2171 if (bms_is_empty(bms))
2172 return "none";
2173
2175 while ((x = bms_next_member(bms, x)) >= 0)
2176 {
2177 if (buf.len > 0)
2178 appendStringInfo(&buf, ", %d", x);
2179 else
2180 appendStringInfo(&buf, "%d", x);
2181 }
2182
2183 return buf.data;
2184}
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 405 of file pgpa_planner.c.

406{
409
410 /* Fetch our private state, set up by pgpa_planner_setup(). */
412
413 /*
414 * Look up the pgpa_planner_info for this subquery, and make sure we've
415 * saved a range table identifier.
416 */
417 if (pps != NULL)
418 {
421 }
422
423 /* If query advice was provided, search for relevant entries. */
424 if (pps != NULL && pps->trove != NULL)
425 {
426 pgpa_identifier *rid;
429
430 /* Search for scan advice and general rel advice. */
431 rid = &proot->rid_array[rel->relid - 1];
433 &tresult_scan);
435 &tresult_rel);
436
437 /* If relevant entries were found, apply them. */
438 if (tresult_scan.indexes != NULL || tresult_rel.indexes != NULL)
439 {
441
443 tresult_scan.entries,
444 tresult_scan.indexes,
445 tresult_rel.entries,
446 tresult_rel.indexes);
447
448 /* Emit debugging message, if enabled. */
450 {
451 if (root->plan_name != NULL)
453 (errmsg("strategy mask for RTI %u in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
454 rel->relid, root->plan_name,
455 original_mask, rel->pgs_mask)));
456 else
458 (errmsg("strategy mask for RTI %u changed from 0x%" PRIx64 " to 0x%" PRIx64,
459 rel->relid, original_mask,
460 rel->pgs_mask)));
461 }
462 }
463 }
464
465 /* Pass call to previous hook. */
467 (*prev_build_simple_rel) (root, rel, rte);
468}
uint64_t uint64
Definition c.h:625
#define WARNING
Definition elog.h:36
#define ereport(elevel,...)
Definition elog.h:151
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:234
@ PGPA_TROVE_LOOKUP_SCAN
Definition pgpa_trove.h:85
@ PGPA_TROVE_LOOKUP_REL
Definition pgpa_trove.h:84
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 2040 of file pgpa_planner.c.

2042{
2043 pgpa_identifier *rid;
2044
2045 /* Allocate or extend the proot's rid_array as necessary. */
2046 if (proot->rid_array_size < rel->relid)
2047 {
2048 int new_size = pg_nextpower2_32(Max(rel->relid, 8));
2049
2050 if (proot->rid_array_size == 0)
2052 else
2053 proot->rid_array = repalloc0_array(proot->rid_array,
2055 proot->rid_array_size,
2056 new_size);
2057 proot->rid_array_size = new_size;
2058 }
2059
2060 /* Save relation identifier details for this RTI if not already done. */
2061 rid = &proot->rid_array[rel->relid - 1];
2062 if (rid->alias_name == NULL)
2064}
#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 2071 of file pgpa_planner.c.

2072{
2074 {
2075 /* For the top query level, we've previously set rtoffset 0. */
2076 if (proot->plan_name == NULL)
2077 {
2078 Assert(proot->has_rtoffset);
2079 continue;
2080 }
2081
2082 /*
2083 * It's not guaranteed that every plan name we saw during planning has
2084 * a SubPlanInfo, but any that do not certainly don't appear in the
2085 * final range table.
2086 */
2088 {
2089 if (strcmp(proot->plan_name, rtinfo->plan_name) == 0)
2090 {
2091 /*
2092 * If rtinfo->dummy is set, then the subquery's range table
2093 * will only have been partially copied to the final range
2094 * table. Specifically, only RTE_RELATION entries and
2095 * RTE_SUBQUERY entries that were once RTE_RELATION entries
2096 * will be copied, as per add_rtes_to_flat_rtable. Therefore,
2097 * there's no fixed rtoffset that we can apply to the RTIs
2098 * used during planning to locate the corresponding relations.
2099 */
2100 if (rtinfo->dummy)
2101 {
2102 /*
2103 * It will not be possible to make any effective use of
2104 * the sj_unique_rels list in this case, and it also won't
2105 * be important to do so. So just throw the list away to
2106 * avoid confusing pgpa_plan_walker.
2107 */
2108 proot->sj_unique_rels = NIL;
2109 break;
2110 }
2111 Assert(!proot->has_rtoffset);
2112 proot->has_rtoffset = true;
2113 proot->rtoffset = rtinfo->rtoffset;
2114 break;
2115 }
2116 }
2117 }
2118}
#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 620 of file pgpa_planner.c.

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

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 1357 of file pgpa_planner.c.

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

References Assert, fb(), pgpa_trove_entry::flags, pgpa_identifiers_match_target(), PGPA_ITM_DISJOINT, PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_TARGET_IDENTIFIER, PGPA_TE_MATCH_FULL, PGPA_TE_MATCH_PARTIAL, 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 1191 of file pgpa_planner.c.

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

532{
534
536
537 /*
538 * If we're considering implementing a semijoin by making one side unique,
539 * make a note of it in the pgpa_planner_state.
540 */
541 if (jointype == JOIN_UNIQUE_OUTER || jointype == JOIN_UNIQUE_INNER)
542 {
545
546 uniquerel = jointype == JOIN_UNIQUE_OUTER ? outerrel : innerrel;
548 if (pps != NULL &&
549 (pps->generate_advice_string || pps->generate_advice_feedback))
550 {
552 MemoryContext oldcontext;
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 oldcontext = MemoryContextSwitchTo(pps->mcxt);
565 if (!list_member(proot->sj_unique_rels, uniquerel->relids))
566 proot->sj_unique_rels = lappend(proot->sj_unique_rels,
567 bms_copy(uniquerel->relids));
568 MemoryContextSwitchTo(oldcontext);
569 }
570 }
571
572 /* Get our private state information for this join. */
573 pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
574
575 /* If there is relevant advice, call a helper function to apply it. */
576 if (pjs != NULL)
577 {
579
581 &extra->pgs_mask,
582 root->plan_name,
583 pjs);
584
585 /* Emit debugging message, if enabled. */
587 {
588 if (root->plan_name != NULL)
590 (errmsg("strategy mask for %s join on %s with outer %s and inner %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
591 pgpa_jointype_to_cstring(jointype),
592 pgpa_bms_to_cstring(joinrel->relids),
593 pgpa_bms_to_cstring(outerrel->relids),
594 pgpa_bms_to_cstring(innerrel->relids),
595 root->plan_name,
597 extra->pgs_mask)));
598 else
600 (errmsg("strategy mask for %s join on %s with outer %s and inner %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
601 pgpa_jointype_to_cstring(jointype),
602 pgpa_bms_to_cstring(joinrel->relids),
603 pgpa_bms_to_cstring(outerrel->relids),
604 pgpa_bms_to_cstring(innerrel->relids),
606 extra->pgs_mask)));
607 }
608 }
609
610 /* Pass call to previous hook. */
612 (*prev_join_path_setup) (root, joinrel, outerrel, innerrel,
613 jointype, extra);
614}
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 1152 of file pgpa_planner.c.

1153{
1154 switch (tag)
1155 {
1157 return PGS_FOREIGNJOIN;
1159 return PGS_MERGEJOIN_PLAIN;
1163 return PGS_NESTLOOP_PLAIN;
1167 return PGS_NESTLOOP_MEMOIZE;
1168 case PGPA_TAG_HASH_JOIN:
1169 return PGS_HASHJOIN;
1170 default:
1171 return 0;
1172 }
1173}
#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 479 of file pgpa_planner.c.

482{
484
486
487 /* Get our private state information for this join. */
488 pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
489
490 /* If there is relevant advice, call a helper function to apply it. */
491 if (pjs != NULL)
492 {
493 uint64 original_mask = joinrel->pgs_mask;
494
496 root->plan_name,
497 pjs);
498
499 /* Emit debugging message, if enabled. */
501 {
502 if (root->plan_name != NULL)
504 (errmsg("strategy mask for join on RTIs %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
505 pgpa_bms_to_cstring(joinrel->relids),
506 root->plan_name,
508 joinrel->pgs_mask)));
509 else
511 (errmsg("strategy mask for join on RTIs %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
512 pgpa_bms_to_cstring(joinrel->relids),
514 joinrel->pgs_mask)));
515 }
516 }
517
518 /* Pass call to previous hook. */
520 (*prev_joinrel_setup) (root, joinrel, outerrel, innerrel,
521 sjinfo, restrictlist);
522}
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 2190 of file pgpa_planner.c.

2191{
2192 switch (jointype)
2193 {
2194 case JOIN_INNER:
2195 return "inner";
2196 case JOIN_LEFT:
2197 return "left";
2198 case JOIN_FULL:
2199 return "full";
2200 case JOIN_RIGHT:
2201 return "right";
2202 case JOIN_SEMI:
2203 return "semi";
2204 case JOIN_ANTI:
2205 return "anti";
2206 case JOIN_RIGHT_SEMI:
2207 return "right semi";
2208 case JOIN_RIGHT_ANTI:
2209 return "right anti";
2210 case JOIN_UNIQUE_OUTER:
2211 return "unique outer";
2212 case JOIN_UNIQUE_INNER:
2213 return "unique inner";
2214 }
2215 return "???";
2216}
@ 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 1463 of file pgpa_planner.c.

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

References fb(), pgpa_trove_entry::flags, pgpa_identifiers_match_target(), PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_TE_MATCH_FULL, PGPA_TE_MATCH_PARTIAL, 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 1862 of file pgpa_planner.c.

1866{
1867 pgpa_trove_entry *entries;
1868 int nentries;
1869
1870 pgpa_trove_lookup_all(trove, type, &entries, &nentries);
1871 for (int i = 0; i < nentries; ++i)
1872 {
1873 pgpa_trove_entry *entry = &entries[i];
1874 DefElem *item;
1875
1876 /*
1877 * If this entry was fully matched, check whether generating advice
1878 * from this plan would produce such an entry. If not, label the entry
1879 * as failed.
1880 */
1881 if ((entry->flags & PGPA_TE_MATCH_FULL) != 0 &&
1883 entry->tag, entry->target))
1884 entry->flags |= PGPA_TE_FAILED;
1885
1887 (Node *) makeInteger(entry->flags), -1);
1888 list = lappend(list, item);
1889 }
1890
1891 return list;
1892}
int i
Definition isn.c:77
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition makefuncs.c:637
void pgpa_trove_lookup_all(pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_trove_entry **entries, int *nentries)
Definition pgpa_trove.c:276
char * pgpa_cstring_trove_entry(pgpa_trove_entry *entry)
Definition pgpa_trove.c:296
#define PGPA_TE_FAILED
Definition pgpa_trove.h:50
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:57
pgpa_advice_tag_type tag
Definition pgpa_trove.h:58
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_TE_FAILED, PGPA_TE_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 933 of file pgpa_planner.c.

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

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_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_TE_CONFLICTING, PGPA_TE_INAPPLICABLE, 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 737 of file pgpa_planner.c.

739{
740 int i = -1;
741 int flags;
742 bool gather_conflict = false;
746 bool partitionwise_conflict = false;
747 int partitionwise_outcome = 0;
750
751 /* Iterate over all possibly-relevant advice. */
752 while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
753 {
754 pgpa_trove_entry *entry = &pjs->rel_entries[i];
756 bool full_match = false;
758 int my_partitionwise_outcome = 0; /* >0 yes, <0 no */
759
760 /*
761 * For GATHER and GATHER_MERGE, if the specified relations exactly
762 * match this joinrel, do whatever the advice says; otherwise, don't
763 * allow Gather or Gather Merge at this level. For NO_GATHER, there
764 * must be a single target relation which must be included in this
765 * joinrel, so just don't allow Gather or Gather Merge here, full
766 * stop.
767 */
768 if (entry->tag == PGPA_TAG_NO_GATHER)
769 {
771 full_match = true;
772 }
773 else
774 {
775 int total_count;
776
777 total_count = pjs->outer_count + pjs->inner_count;
779 entry->target);
781
782 if (itm == PGPA_ITM_EQUAL)
783 {
784 full_match = true;
785 if (entry->tag == PGPA_TAG_PARTITIONWISE)
787 else if (entry->tag == PGPA_TAG_GATHER)
789 else if (entry->tag == PGPA_TAG_GATHER_MERGE)
791 else
792 elog(ERROR, "unexpected advice tag: %d",
793 (int) entry->tag);
794 }
795 else
796 {
797 /*
798 * If specified relations don't exactly match this joinrel,
799 * then we should do the opposite of whatever the advice says.
800 * For instance, if we have PARTITIONWISE((a b c)) or
801 * GATHER((a b c)) and this joinrel covers {a, b} or {a, b, c,
802 * d} or {a, d}, we shouldn't plan it partitionwise or put a
803 * Gather or Gather Merge on it here.
804 *
805 * Also, we can't put a Gather or Gather Merge at this level
806 * if there is PARTITIONWISE advice that overlaps with it,
807 * unless the PARTITIONWISE advice covers a subset of the
808 * relations in the joinrel. To continue the previous example,
809 * PARTITIONWISE((a b c)) is logically incompatible with
810 * GATHER((a b)) or GATHER((a d)), but not with GATHER((a b c
811 * d)).
812 *
813 * Conversely, we can't proceed partitionwise at this level if
814 * there is overlapping GATHER or GATHER_MERGE advice, unless
815 * that advice covers a superset of the relations in this
816 * joinrel. This is just the flip side of the preceding point.
817 */
818 if (entry->tag == PGPA_TAG_PARTITIONWISE)
819 {
823 }
824 else if (entry->tag == PGPA_TAG_GATHER ||
825 entry->tag == PGPA_TAG_GATHER_MERGE)
826 {
830 }
831 else
832 elog(ERROR, "unexpected advice tag: %d",
833 (int) entry->tag);
834 }
835 }
836
837 /*
838 * If we set my_gather_mask up above, then we (1) make a note if the
839 * advice conflicted, (2) remember the mask value, and (3) remember
840 * whether this was a full or partial match.
841 */
842 if (my_gather_mask != 0)
843 {
845 gather_conflict = true;
847 if (full_match)
849 else
851 }
852
853 /*
854 * Likewise, if we set my_partitionwise_outcome up above, then we (1)
855 * make a note if the advice conflicted, (2) remember what the desired
856 * outcome was, and (3) remember whether this was a full or partial
857 * match.
858 */
860 {
861 if (partitionwise_outcome != 0 &&
865 if (full_match)
868 else
871 }
872 }
873
874 /*
875 * Mark every Gather-related piece of advice as partially matched, and if
876 * the set of targets exactly matched this relation, fully matched. If
877 * there was a conflict, mark them all as conflicting.
878 */
879 flags = PGPA_TE_MATCH_PARTIAL;
880 if (gather_conflict)
881 flags |= PGPA_TE_CONFLICTING;
882 pgpa_trove_set_flags(pjs->rel_entries, gather_partial_match, flags);
883 flags |= PGPA_TE_MATCH_FULL;
884 pgpa_trove_set_flags(pjs->rel_entries, gather_full_match, flags);
885
886 /* Likewise for partitionwise advice. */
887 flags = PGPA_TE_MATCH_PARTIAL;
889 flags |= PGPA_TE_CONFLICTING;
891 flags |= PGPA_TE_MATCH_FULL;
893
894 /*
895 * Enforce restrictions on the Gather/Gather Merge. Only clear bits here,
896 * so that we still respect the enable_* GUCs. Do nothing if the advice
897 * conflicts.
898 */
899 if (gather_mask != 0 && !gather_conflict)
900 {
902
906 }
907
908 /*
909 * As above, but for partitionwise advice.
910 *
911 * To induce a partitionwise join, we disable all the ordinary means of
912 * performing a join, so that an Append or MergeAppend path will hopefully
913 * be chosen.
914 *
915 * To prevent one, we just disable Append and MergeAppend. Note that we
916 * must not unset PGS_CONSIDER_PARTITIONWISE even when we don't want a
917 * partitionwise join here, because we might want one at a higher level
918 * that will construct its own paths using the ones from this level.
919 */
921 {
922 if (partitionwise_outcome > 0)
924 else
926 }
927}
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:227
#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_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_TE_CONFLICTING, PGPA_TE_MATCH_FULL, PGPA_TE_MATCH_PARTIAL, 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 1618 of file pgpa_planner.c.

1623{
1626 bool gather_conflict = false;
1629 int i = -1;
1631 int flags;
1632 bool scan_type_conflict = false;
1635 uint64 gather_mask = 0;
1636 uint64 scan_type = all_scan_mask; /* sentinel: no advice yet */
1637
1638 /* Scrutinize available scan advice. */
1639 while ((i = bms_next_member(scan_indexes, i)) >= 0)
1640 {
1643
1644 /* Translate our advice tags to a scan strategy advice value. */
1645 if (my_entry->tag == PGPA_TAG_DO_NOT_SCAN)
1646 my_scan_type = 0;
1647 else if (my_entry->tag == PGPA_TAG_BITMAP_HEAP_SCAN)
1648 {
1649 /*
1650 * Currently, PGS_CONSIDER_INDEXONLY can suppress Bitmap Heap
1651 * Scans, so don't clear it when such a scan is requested. This
1652 * happens because build_index_scan() thinks that the possibility
1653 * of an index-only scan is a sufficient reason to consider using
1654 * an otherwise-useless index, and get_index_paths() thinks that
1655 * the same paths that are useful for index or index-only scans
1656 * should also be considered for bitmap scans. Perhaps that logic
1657 * should be tightened up, but until then we need to include
1658 * PGS_CONSIDER_INDEXONLY in my_scan_type here.
1659 */
1661 }
1662 else if (my_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN)
1664 else if (my_entry->tag == PGPA_TAG_INDEX_SCAN)
1666 else if (my_entry->tag == PGPA_TAG_SEQ_SCAN)
1668 else if (my_entry->tag == PGPA_TAG_TID_SCAN)
1670
1671 /*
1672 * If this is understandable scan advice, hang on to the entry, the
1673 * inferred scan type, and the index at which we found it.
1674 *
1675 * Also make a note if we see conflicting scan type advice. Note that
1676 * we regard two index specifications as conflicting unless they match
1677 * exactly. In theory, perhaps we could regard INDEX_SCAN(a c) and
1678 * INDEX_SCAN(a b.c) as non-conflicting if it happens that the only
1679 * index named c is in schema b, but it doesn't seem worth the code.
1680 */
1682 {
1684 scan_type_conflict = true;
1685 if (!scan_type_conflict && scan_entry != NULL &&
1686 my_entry->target->itarget != NULL &&
1687 scan_entry->target->itarget != NULL &&
1688 !pgpa_index_targets_equal(scan_entry->target->itarget,
1689 my_entry->target->itarget))
1690 scan_type_conflict = true;
1694 }
1695 }
1696
1697 /* Scrutinize available gather-related and partitionwise advice. */
1698 i = -1;
1699 while ((i = bms_next_member(rel_indexes, i)) >= 0)
1700 {
1701 pgpa_trove_entry *my_entry = &rel_entries[i];
1703 bool just_one_rel;
1704
1706 || list_length(my_entry->target->children) == 1;
1707
1708 /*
1709 * PARTITIONWISE behaves like a scan type, except that if there's more
1710 * than one relation targeted, it has no effect at this level.
1711 */
1712 if (my_entry->tag == PGPA_TAG_PARTITIONWISE)
1713 {
1714 if (just_one_rel)
1715 {
1717
1719 scan_type_conflict = true;
1724 }
1725 continue;
1726 }
1727
1728 /*
1729 * GATHER and GATHER_MERGE applied to a single rel mean that we should
1730 * use the corresponding strategy here, while applying either to more
1731 * than one rel means we should not use those strategies here, but
1732 * rather at the level of the joinrel that corresponds to what was
1733 * specified. NO_GATHER can only be applied to single rels.
1734 *
1735 * Note that setting PGS_CONSIDER_NONPARTIAL in my_gather_mask is
1736 * equivalent to allowing the non-use of either form of Gather here.
1737 */
1738 if (my_entry->tag == PGPA_TAG_GATHER ||
1740 {
1741 if (!just_one_rel)
1743 else if (my_entry->tag == PGPA_TAG_GATHER)
1745 else
1747 }
1748 else if (my_entry->tag == PGPA_TAG_NO_GATHER)
1749 {
1752 }
1753
1754 /*
1755 * If we set my_gather_mask up above, then we (1) make a note if the
1756 * advice conflicted, (2) remember the mask value, and (3) remember
1757 * whether this was a full or partial match.
1758 */
1759 if (my_gather_mask != 0)
1760 {
1761 if (gather_mask != 0 && gather_mask != my_gather_mask)
1762 gather_conflict = true;
1764 if (just_one_rel)
1766 else
1768 }
1769 }
1770
1771 /* Enforce choice of index. */
1772 if (scan_entry != NULL && !scan_type_conflict &&
1775 {
1776 pgpa_index_target *itarget = scan_entry->target->itarget;
1778
1780 {
1781 char *relname = get_rel_name(index->indexoid);
1782 Oid nspoid = get_rel_namespace(index->indexoid);
1783 char *relnamespace = get_namespace_name_or_temp(nspoid);
1784
1785 if (strcmp(itarget->indname, relname) == 0 &&
1786 (itarget->indnamespace == NULL ||
1787 strcmp(itarget->indnamespace, relnamespace) == 0))
1788 {
1790 break;
1791 }
1792 }
1793
1794 if (matched_index == NULL)
1795 {
1796 /* Don't force the scan type if the index doesn't exist. */
1798
1799 /* Mark advice as inapplicable. */
1802 }
1803 else
1804 {
1805 /* Disable every other index. */
1807 {
1808 if (index != matched_index)
1809 index->disabled = true;
1810 }
1811 }
1812 }
1813
1814 /*
1815 * Mark all the scan method entries as fully matched; and if they specify
1816 * different things, mark them all as conflicting.
1817 */
1820 flags |= PGPA_TE_CONFLICTING;
1822 pgpa_trove_set_flags(rel_entries, scan_type_rel_indexes, flags);
1823
1824 /*
1825 * Mark every Gather-related piece of advice as partially matched. Mark
1826 * the ones that included this relation as a target by itself as fully
1827 * matched. If there was a conflict, mark them all as conflicting.
1828 */
1829 flags = PGPA_TE_MATCH_PARTIAL;
1830 if (gather_conflict)
1831 flags |= PGPA_TE_CONFLICTING;
1832 pgpa_trove_set_flags(rel_entries, gather_partial_match, flags);
1833 flags |= PGPA_TE_MATCH_FULL;
1834 pgpa_trove_set_flags(rel_entries, gather_full_match, flags);
1835
1836 /*
1837 * Enforce restrictions on the scan type and use of Gather/Gather Merge.
1838 * Only clear bits here, so that we still respect the enable_* GUCs. Do
1839 * nothing in cases where the advice on a single topic conflicts.
1840 */
1842 rel->pgs_mask &= ~(all_scan_mask & ~scan_type);
1843 if (gather_mask != 0 && !gather_conflict)
1844 {
1846
1850 }
1851}
char * get_rel_name(Oid relid)
Definition lsyscache.c:2148
Oid get_rel_namespace(Oid relid)
Definition lsyscache.c:2172
char * get_namespace_name_or_temp(Oid nspid)
Definition lsyscache.c:3612
#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_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_TE_CONFLICTING, PGPA_TE_INAPPLICABLE, PGPA_TE_MATCH_FULL, PGPA_TE_MATCH_PARTIAL, 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()

static void pgpa_planner_feedback_warning ( List feedback)
static

Definition at line 1899 of file pgpa_planner.c.

1900{
1903
1904 /* Quick exit if there's no feedback. */
1905 if (feedback == NIL)
1906 return;
1907
1908 /* Initialize buffers. */
1911
1912 /* Main loop. */
1914 {
1915 int flags = defGetInt32(item);
1916
1917 /*
1918 * Don't emit anything if it was fully matched with no problems found.
1919 *
1920 * NB: Feedback should never be marked fully matched without also
1921 * being marked partially matched.
1922 */
1924 continue;
1925
1926 /*
1927 * Terminate each detail line except the last with a newline. This is
1928 * also a convenient place to reset flagbuf.
1929 */
1930 if (detailbuf.len > 0)
1931 {
1934 }
1935
1936 /* Generate output. */
1938 appendStringInfo(&detailbuf, "advice %s feedback is \"%s\"",
1939 item->defname, flagbuf.data);
1940 }
1941
1942 /* Emit the warning, if any problems were found. */
1943 if (detailbuf.len > 0)
1945 errmsg("supplied plan advice was not enforced"),
1946 errdetail("%s", detailbuf.data));
1947}
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:344
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_TE_MATCH_FULL, PGPA_TE_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 1953 of file pgpa_planner.c.

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

183{
195}
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 201 of file pgpa_planner.c.

204{
205 pgpa_trove *trove = NULL;
207 char *supplied_advice;
208 bool generate_advice_feedback = false;
209 bool generate_advice_string = false;
210 bool needs_pps = false;
211
212 /*
213 * Decide whether we need to generate an advice string. We must do this if
214 * the user has told us to do it categorically, or if another loadable
215 * module has requested it, or if the user has requested it using the
216 * EXPLAIN (PLAN_ADVICE) option.
217 */
218 generate_advice_string = (pg_plan_advice_always_store_advice_details ||
221 if (generate_advice_string)
222 needs_pps = true;
223
224 /*
225 * If any advice was provided, build a trove of advice for use during
226 * planning.
227 */
229 query_string,
230 cursorOptions,
231 es);
232 if (supplied_advice != NULL && supplied_advice[0] != '\0')
233 {
235 char *error;
236
237 /*
238 * If the supplied advice string comes from pg_plan_advice.advice,
239 * parsing shouldn't fail here, because we must have previously parsed
240 * successfully in pg_plan_advice_advice_check_hook. However, it might
241 * also come from a hook registered via pg_plan_advice_add_advisor,
242 * and we can't be sure whether that's valid. (Plus, having an error
243 * check here seems like a good idea anyway, just for safety.)
244 */
246 if (error)
248 errmsg("could not parse supplied advice: %s", error));
249
250 /*
251 * It's possible that the advice string was non-empty but contained no
252 * actual advice, e.g. it was all whitespace.
253 */
254 if (advice_items != NIL)
255 {
257 needs_pps = true;
258
259 /*
260 * If we know that we're running under EXPLAIN, or if the user has
261 * told us to always do the work, generate advice feedback.
262 */
265 generate_advice_feedback = true;
266 }
267 }
268
269 /*
270 * We only create and initialize a private state object if it's needed for
271 * some purpose. That could be (1) recording that we will need to generate
272 * an advice string or (2) storing a trove of supplied advice.
273 *
274 * Currently, the active memory context should be one that will last for
275 * the entire duration of query planning, but if GEQO is in use, it's
276 * possible that some of our callbacks may be invoked later with
277 * CurrentMemoryContext set to some shorter-lived context. So, record the
278 * context that should be used for allocations that need to live as long
279 * as the pgpa_planner_state itself.
280 */
281 if (needs_pps)
282 {
285 pps->generate_advice_feedback = generate_advice_feedback;
286 pps->generate_advice_string = generate_advice_string;
287 pps->trove = trove;
289 }
290
291 /* Pass call to previous hook. */
293 (*prev_planner_setup) (glob, parse, query_string, cursorOptions,
294 tuple_fraction, es);
295}
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:132
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 301 of file pgpa_planner.c.

303{
305 pgpa_trove *trove = NULL;
306 pgpa_plan_walker_context walker = {0}; /* placate compiler */
307 bool generate_advice_feedback = false;
308 bool generate_advice_string = false;
311
312 /* Fetch our private state, set up by pgpa_planner_setup(). */
314 if (pps != NULL)
315 {
316 /* Set up some local variables. */
317 trove = pps->trove;
318 generate_advice_feedback = pps->generate_advice_feedback;
319 generate_advice_string = pps->generate_advice_string;
320
321 /* Compute range table offsets. */
323
324 /* Cross-check range table identifiers. */
326 }
327
328 /*
329 * If we're trying to generate an advice string or if we're trying to
330 * provide advice feedback, then we will need to create range table
331 * identifiers.
332 */
333 if (generate_advice_string || generate_advice_feedback)
334 {
335 pgpa_plan_walker(&walker, pstmt, pps->proots);
337 }
338
339 /* Generate the advice string, if we need to do so. */
340 if (generate_advice_string)
341 {
342 char *advice_string;
344
345 /* Generate a textual advice string. */
348 advice_string = buf.data;
349
350 /* Save the advice string in the final plan. */
352 makeDefElem("advice_string",
354 -1));
355 }
356
357 /*
358 * If we're trying to provide advice feedback, then we will need to
359 * analyze how successful the advice was.
360 */
361 if (generate_advice_feedback)
362 {
363 List *feedback = NIL;
364
365 /*
366 * Inject a Node-tree representation of all the trove-entry flags into
367 * the PlannedStmt.
368 */
370 trove,
374 trove,
378 trove,
381
383 (Node *) feedback, -1));
384
385 /* If we were asked to generate feedback warnings, do so. */
388 }
389
390 /* Push whatever data we're saving into the PlannedStmt. */
391 if (pgpa_items != NIL)
392 pstmt->extension_state =
394 makeDefElem("pg_plan_advice", (Node *) pgpa_items, -1));
395
396 /* Pass call to previous hook. */
398 (*prev_planner_shutdown) (glob, parse, query_string, pstmt);
399}
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)
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)
static void pgpa_planner_feedback_warning(List *feedback)
void pgpa_plan_walker(pgpa_plan_walker_context *walker, PlannedStmt *pstmt, List *proots)
Definition pgpa_walker.c:76
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 1528 of file pgpa_planner.c.

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

References Assert, fb(), pgpa_trove_entry::flags, pgpa_identifiers_match_target(), PGPA_ITM_DISJOINT, PGPA_ITM_EQUAL, PGPA_ITM_KEYS_ARE_SUBSET, PGPA_ITM_TARGETS_ARE_SUBSET, PGPA_TE_MATCH_FULL, PGPA_TE_MATCH_PARTIAL, 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 2125 of file pgpa_planner.c.

2126{
2127#ifdef USE_ASSERT_CHECKING
2130
2131 /* Create identifiers from the planned statement. */
2133
2134 /* Iterate over identifiers created during planning, so we can compare. */
2136 {
2137 if (!proot->has_rtoffset)
2138 continue;
2139
2140 for (int rti = 1; rti <= proot->rid_array_size; ++rti)
2141 {
2142 Index flat_rti = proot->rtoffset + rti;
2143 pgpa_identifier *rid1 = &proot->rid_array[rti - 1];
2145
2146 if (rid1->alias_name == NULL)
2147 continue;
2148
2151 Assert(strcmp(rid1->alias_name, rid2->alias_name) == 0);
2152 Assert(rid1->occurrence == rid2->occurrence);
2153 Assert(strings_equal_or_both_null(rid1->partnsp, rid2->partnsp));
2154 Assert(strings_equal_or_both_null(rid1->partrel, rid2->partrel));
2156 rid2->plan_name));
2157 }
2158 }
2159#endif
2160}
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().