1 /*-------------------------------------------------------------------------
4 * hinting on how to execute a query for PostgreSQL
6 * Copyright (c) 2012-2018, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
8 *-------------------------------------------------------------------------
13 #include "catalog/pg_collation.h"
14 #include "catalog/pg_index.h"
15 #include "commands/prepare.h"
16 #include "mb/pg_wchar.h"
17 #include "miscadmin.h"
18 #include "nodes/nodeFuncs.h"
19 #include "nodes/params.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/cost.h"
22 #include "optimizer/geqo.h"
23 #include "optimizer/joininfo.h"
24 #include "optimizer/pathnode.h"
25 #include "optimizer/paths.h"
26 #include "optimizer/plancat.h"
27 #include "optimizer/planner.h"
28 #include "optimizer/prep.h"
29 #include "optimizer/restrictinfo.h"
30 #include "parser/analyze.h"
31 #include "parser/scansup.h"
32 #include "tcop/utility.h"
33 #include "utils/builtins.h"
34 #include "utils/lsyscache.h"
35 #include "utils/memutils.h"
36 #include "utils/rel.h"
37 #include "utils/snapmgr.h"
38 #include "utils/syscache.h"
39 #include "utils/resowner.h"
41 #include "catalog/pg_class.h"
43 #include "executor/spi.h"
44 #include "catalog/pg_type.h"
48 /* partially copied from pg_stat_statements */
49 #include "normalize_query.h"
52 #include "access/htup_details.h"
54 #ifdef PG_MODULE_MAGIC
58 #define BLOCK_COMMENT_START "/*"
59 #define BLOCK_COMMENT_END "*/"
60 #define HINT_COMMENT_KEYWORD "+"
61 #define HINT_START BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
62 #define HINT_END BLOCK_COMMENT_END
65 #define HINT_SEQSCAN "SeqScan"
66 #define HINT_INDEXSCAN "IndexScan"
67 #define HINT_INDEXSCANREGEXP "IndexScanRegexp"
68 #define HINT_BITMAPSCAN "BitmapScan"
69 #define HINT_BITMAPSCANREGEXP "BitmapScanRegexp"
70 #define HINT_TIDSCAN "TidScan"
71 #define HINT_NOSEQSCAN "NoSeqScan"
72 #define HINT_NOINDEXSCAN "NoIndexScan"
73 #define HINT_NOBITMAPSCAN "NoBitmapScan"
74 #define HINT_NOTIDSCAN "NoTidScan"
75 #define HINT_INDEXONLYSCAN "IndexOnlyScan"
76 #define HINT_INDEXONLYSCANREGEXP "IndexOnlyScanRegexp"
77 #define HINT_NOINDEXONLYSCAN "NoIndexOnlyScan"
78 #define HINT_PARALLEL "Parallel"
80 #define HINT_NESTLOOP "NestLoop"
81 #define HINT_MERGEJOIN "MergeJoin"
82 #define HINT_HASHJOIN "HashJoin"
83 #define HINT_NONESTLOOP "NoNestLoop"
84 #define HINT_NOMERGEJOIN "NoMergeJoin"
85 #define HINT_NOHASHJOIN "NoHashJoin"
86 #define HINT_LEADING "Leading"
87 #define HINT_SET "Set"
88 #define HINT_ROWS "Rows"
90 #define HINT_ARRAY_DEFAULT_INITSIZE 8
92 #define hint_ereport(str, detail) hint_parse_ereport(str, detail)
93 #define hint_parse_ereport(str, detail) \
95 ereport(pg_hint_plan_parse_message_level, \
96 (errmsg("pg_hint_plan: hint syntax error at or near \"%s\"", (str)), \
100 #define skip_space(str) \
101 while (isspace(*str)) \
106 ENABLE_SEQSCAN = 0x01,
107 ENABLE_INDEXSCAN = 0x02,
108 ENABLE_BITMAPSCAN = 0x04,
109 ENABLE_TIDSCAN = 0x08,
110 ENABLE_INDEXONLYSCAN = 0x10
115 ENABLE_NESTLOOP = 0x01,
116 ENABLE_MERGEJOIN = 0x02,
117 ENABLE_HASHJOIN = 0x04
120 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
121 ENABLE_BITMAPSCAN | ENABLE_TIDSCAN | \
122 ENABLE_INDEXONLYSCAN)
123 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
124 #define DISABLE_ALL_SCAN 0
125 #define DISABLE_ALL_JOIN 0
127 /* hint keyword of enum type*/
128 typedef enum HintKeyword
130 HINT_KEYWORD_SEQSCAN,
131 HINT_KEYWORD_INDEXSCAN,
132 HINT_KEYWORD_INDEXSCANREGEXP,
133 HINT_KEYWORD_BITMAPSCAN,
134 HINT_KEYWORD_BITMAPSCANREGEXP,
135 HINT_KEYWORD_TIDSCAN,
136 HINT_KEYWORD_NOSEQSCAN,
137 HINT_KEYWORD_NOINDEXSCAN,
138 HINT_KEYWORD_NOBITMAPSCAN,
139 HINT_KEYWORD_NOTIDSCAN,
140 HINT_KEYWORD_INDEXONLYSCAN,
141 HINT_KEYWORD_INDEXONLYSCANREGEXP,
142 HINT_KEYWORD_NOINDEXONLYSCAN,
144 HINT_KEYWORD_NESTLOOP,
145 HINT_KEYWORD_MERGEJOIN,
146 HINT_KEYWORD_HASHJOIN,
147 HINT_KEYWORD_NONESTLOOP,
148 HINT_KEYWORD_NOMERGEJOIN,
149 HINT_KEYWORD_NOHASHJOIN,
151 HINT_KEYWORD_LEADING,
154 HINT_KEYWORD_PARALLEL,
156 HINT_KEYWORD_UNRECOGNIZED
159 #define SCAN_HINT_ACCEPTS_INDEX_NAMES(kw) \
160 (kw == HINT_KEYWORD_INDEXSCAN || \
161 kw == HINT_KEYWORD_INDEXSCANREGEXP || \
162 kw == HINT_KEYWORD_INDEXONLYSCAN || \
163 kw == HINT_KEYWORD_INDEXONLYSCANREGEXP || \
164 kw == HINT_KEYWORD_BITMAPSCAN || \
165 kw == HINT_KEYWORD_BITMAPSCANREGEXP)
168 typedef struct Hint Hint;
169 typedef struct HintState HintState;
171 typedef Hint *(*HintCreateFunction) (const char *hint_str,
173 HintKeyword hint_keyword);
174 typedef void (*HintDeleteFunction) (Hint *hint);
175 typedef void (*HintDescFunction) (Hint *hint, StringInfo buf, bool nolf);
176 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
177 typedef const char *(*HintParseFunction) (Hint *hint, HintState *hstate,
178 Query *parse, const char *str);
181 #define NUM_HINT_TYPE 6
182 typedef enum HintType
184 HINT_TYPE_SCAN_METHOD,
185 HINT_TYPE_JOIN_METHOD,
192 typedef enum HintTypeBitmap
194 HINT_BM_SCAN_METHOD = 1,
198 static const char *HintTypeName[] = {
208 typedef enum HintStatus
210 HINT_STATE_NOTUSED = 0, /* specified relation not used in query */
211 HINT_STATE_USED, /* hint is used */
212 HINT_STATE_DUPLICATION, /* specified hint duplication */
213 HINT_STATE_ERROR /* execute error (parse error does not include
217 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
218 (hint)->base.state == HINT_STATE_USED)
220 static unsigned int qno = 0;
221 static unsigned int msgqno = 0;
222 static char qnostr[32];
223 static const char *current_hint_str = NULL;
225 /* common data for all hints. */
228 const char *hint_str; /* must not do pfree */
229 const char *keyword; /* must not do pfree */
230 HintKeyword hint_keyword;
233 HintDeleteFunction delete_func;
234 HintDescFunction desc_func;
235 HintCmpFunction cmp_func;
236 HintParseFunction parse_func;
239 /* scan method hints */
240 typedef struct ScanMethodHint
246 unsigned char enforce_mask;
249 typedef struct ParentIndexInfo
254 char *expression_str;
261 /* join method hints */
262 typedef struct JoinMethodHint
268 unsigned char enforce_mask;
270 Relids inner_joinrelids;
273 /* join order hints */
274 typedef struct OuterInnerRels
277 List *outer_inner_pair;
280 typedef struct LeadingHint
283 List *relations; /* relation names specified in Leading hint */
284 OuterInnerRels *outer_inner;
287 /* change a run-time parameter hints */
288 typedef struct SetHint
291 char *name; /* name of variable */
297 typedef enum RowsValueType {
298 RVT_ABSOLUTE, /* Rows(... #1000) */
299 RVT_ADD, /* Rows(... +1000) */
300 RVT_SUB, /* Rows(... -1000) */
301 RVT_MULTI, /* Rows(... *1.2) */
303 typedef struct RowsHint
310 Relids inner_joinrelids;
312 RowsValueType value_type;
317 typedef struct ParallelHint
321 char *nworkers_str; /* original string of nworkers */
322 int nworkers; /* num of workers specified by Worker */
323 bool force_parallel; /* force parallel scan */
327 * Describes a context of hint processing.
331 char *hint_str; /* original hint string */
334 int nall_hints; /* # of valid all hints */
335 int max_all_hints; /* # of slots for all hints */
336 Hint **all_hints; /* parsed all hints */
338 /* # of each hints */
339 int num_hints[NUM_HINT_TYPE];
341 /* for scan method hints */
342 ScanMethodHint **scan_hints; /* parsed scan hints */
344 /* Initial values of parameters */
345 int init_scan_mask; /* enable_* mask */
346 int init_nworkers; /* max_parallel_workers_per_gather */
347 int init_min_para_size; /* min_parallel_relation_size*/
348 int init_paratup_cost; /* parallel_tuple_cost */
349 int init_parasetup_cost;/* parallel_setup_cost */
351 PlannerInfo *current_root; /* PlannerInfo for the followings */
352 Index parent_relid; /* inherit parent of table relid */
353 ScanMethodHint *parent_scan_hint; /* scan hint for the parent */
354 ParallelHint *parent_parallel_hint; /* parallel hint for the parent */
355 List *parent_index_infos; /* list of parent table's index */
357 JoinMethodHint **join_hints; /* parsed join hints */
358 int init_join_mask; /* initial value join parameter */
359 List **join_hint_level;
360 LeadingHint **leading_hint; /* parsed Leading hints */
361 SetHint **set_hints; /* parsed Set hints */
362 GucContext context; /* which GUC parameters can we set? */
363 RowsHint **rows_hints; /* parsed Rows hints */
364 ParallelHint **parallel_hints; /* parsed Parallel hints */
368 * Describes a hint parser module which is bound with particular hint keyword.
370 typedef struct HintParser
373 HintCreateFunction create_func;
374 HintKeyword hint_keyword;
377 /* Module callbacks */
381 static void push_hint(HintState *hstate);
382 static void pop_hint(void);
384 static void pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query);
385 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
386 ParamListInfo boundParams);
387 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
391 /* Scan method hint callbacks */
392 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword,
393 HintKeyword hint_keyword);
394 static void ScanMethodHintDelete(ScanMethodHint *hint);
395 static void ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf);
396 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
397 static const char *ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate,
398 Query *parse, const char *str);
400 /* Join method hint callbacks */
401 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword,
402 HintKeyword hint_keyword);
403 static void JoinMethodHintDelete(JoinMethodHint *hint);
404 static void JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf);
405 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
406 static const char *JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate,
407 Query *parse, const char *str);
409 /* Leading hint callbacks */
410 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword,
411 HintKeyword hint_keyword);
412 static void LeadingHintDelete(LeadingHint *hint);
413 static void LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf);
414 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
415 static const char *LeadingHintParse(LeadingHint *hint, HintState *hstate,
416 Query *parse, const char *str);
418 /* Set hint callbacks */
419 static Hint *SetHintCreate(const char *hint_str, const char *keyword,
420 HintKeyword hint_keyword);
421 static void SetHintDelete(SetHint *hint);
422 static void SetHintDesc(SetHint *hint, StringInfo buf, bool nolf);
423 static int SetHintCmp(const SetHint *a, const SetHint *b);
424 static const char *SetHintParse(SetHint *hint, HintState *hstate, Query *parse,
427 /* Rows hint callbacks */
428 static Hint *RowsHintCreate(const char *hint_str, const char *keyword,
429 HintKeyword hint_keyword);
430 static void RowsHintDelete(RowsHint *hint);
431 static void RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf);
432 static int RowsHintCmp(const RowsHint *a, const RowsHint *b);
433 static const char *RowsHintParse(RowsHint *hint, HintState *hstate,
434 Query *parse, const char *str);
436 /* Parallel hint callbacks */
437 static Hint *ParallelHintCreate(const char *hint_str, const char *keyword,
438 HintKeyword hint_keyword);
439 static void ParallelHintDelete(ParallelHint *hint);
440 static void ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf);
441 static int ParallelHintCmp(const ParallelHint *a, const ParallelHint *b);
442 static const char *ParallelHintParse(ParallelHint *hint, HintState *hstate,
443 Query *parse, const char *str);
445 static void quote_value(StringInfo buf, const char *value);
447 static const char *parse_quoted_value(const char *str, char **word,
450 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
453 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
454 void pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
455 Index rti, RangeTblEntry *rte);
456 static void create_plain_partial_paths(PlannerInfo *root,
459 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
460 ListCell *other_rels);
461 static void make_rels_by_clauseless_joins(PlannerInfo *root,
463 ListCell *other_rels);
464 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
465 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
467 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
468 Index rti, RangeTblEntry *rte);
469 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
470 List *live_childrels,
471 List *all_child_pathkeys);
472 static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
474 Relids required_outer);
475 static List *accumulate_append_subpath(List *subpaths, Path *path);
476 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
479 static void pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
481 static void pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate,
483 static void plpgsql_query_erase_callback(ResourceReleasePhase phase,
487 static int set_config_option_noerror(const char *name, const char *value,
488 GucContext context, GucSource source,
489 GucAction action, bool changeVal, int elevel);
490 static void setup_scan_method_enforcement(ScanMethodHint *scanhint,
492 static int set_config_int32_option(const char *name, int32 value,
496 static bool pg_hint_plan_enable_hint = true;
497 static int debug_level = 0;
498 static int pg_hint_plan_parse_message_level = INFO;
499 static int pg_hint_plan_debug_message_level = LOG;
500 /* Default is off, to keep backward compatibility. */
501 static bool pg_hint_plan_enable_hint_table = false;
503 static int plpgsql_recurse_level = 0; /* PLpgSQL recursion level */
504 static int hint_inhibit_level = 0; /* Inhibit hinting if this is above 0 */
505 /* (This could not be above 1) */
506 static int max_hint_nworkers = -1; /* Maximum nworkers of Workers hints */
508 static const struct config_enum_entry parse_messages_level_options[] = {
509 {"debug", DEBUG2, true},
510 {"debug5", DEBUG5, false},
511 {"debug4", DEBUG4, false},
512 {"debug3", DEBUG3, false},
513 {"debug2", DEBUG2, false},
514 {"debug1", DEBUG1, false},
516 {"info", INFO, false},
517 {"notice", NOTICE, false},
518 {"warning", WARNING, false},
519 {"error", ERROR, false},
521 * {"fatal", FATAL, true},
522 * {"panic", PANIC, true},
527 static const struct config_enum_entry parse_debug_level_options[] = {
530 {"detailed", 2, false},
531 {"verbose", 3, false},
543 /* Saved hook values in case of unload */
544 static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
545 static planner_hook_type prev_planner = NULL;
546 static join_search_hook_type prev_join_search = NULL;
547 static set_rel_pathlist_hook_type prev_set_rel_pathlist = NULL;
549 /* Hold reference to currently active hint */
550 static HintState *current_hint_state = NULL;
553 * List of hint contexts. We treat the head of the list as the Top of the
554 * context stack, so current_hint_state always points the first element of this
557 static List *HintStateStack = NIL;
560 * Holds statement name during executing EXECUTE command. NULL for other
563 static char *stmt_name = NULL;
565 static const HintParser parsers[] = {
566 {HINT_SEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_SEQSCAN},
567 {HINT_INDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCAN},
568 {HINT_INDEXSCANREGEXP, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCANREGEXP},
569 {HINT_BITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_BITMAPSCAN},
570 {HINT_BITMAPSCANREGEXP, ScanMethodHintCreate,
571 HINT_KEYWORD_BITMAPSCANREGEXP},
572 {HINT_TIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_TIDSCAN},
573 {HINT_NOSEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOSEQSCAN},
574 {HINT_NOINDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXSCAN},
575 {HINT_NOBITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOBITMAPSCAN},
576 {HINT_NOTIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOTIDSCAN},
577 {HINT_INDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXONLYSCAN},
578 {HINT_INDEXONLYSCANREGEXP, ScanMethodHintCreate,
579 HINT_KEYWORD_INDEXONLYSCANREGEXP},
580 {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXONLYSCAN},
582 {HINT_NESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NESTLOOP},
583 {HINT_MERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_MERGEJOIN},
584 {HINT_HASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_HASHJOIN},
585 {HINT_NONESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NONESTLOOP},
586 {HINT_NOMERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOMERGEJOIN},
587 {HINT_NOHASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOHASHJOIN},
589 {HINT_LEADING, LeadingHintCreate, HINT_KEYWORD_LEADING},
590 {HINT_SET, SetHintCreate, HINT_KEYWORD_SET},
591 {HINT_ROWS, RowsHintCreate, HINT_KEYWORD_ROWS},
592 {HINT_PARALLEL, ParallelHintCreate, HINT_KEYWORD_PARALLEL},
594 {NULL, NULL, HINT_KEYWORD_UNRECOGNIZED}
597 PLpgSQL_plugin plugin_funcs = {
601 pg_hint_plan_plpgsql_stmt_beg,
602 pg_hint_plan_plpgsql_stmt_end,
608 * Module load callbacks
613 PLpgSQL_plugin **var_ptr;
615 /* Define custom GUC variables. */
616 DefineCustomBoolVariable("pg_hint_plan.enable_hint",
617 "Force planner to use plans specified in the hint comment preceding to the query.",
619 &pg_hint_plan_enable_hint,
627 DefineCustomEnumVariable("pg_hint_plan.debug_print",
628 "Logs results of hint parsing.",
632 parse_debug_level_options,
639 DefineCustomEnumVariable("pg_hint_plan.parse_messages",
640 "Message level of parse errors.",
642 &pg_hint_plan_parse_message_level,
644 parse_messages_level_options,
651 DefineCustomEnumVariable("pg_hint_plan.message_level",
652 "Message level of debug messages.",
654 &pg_hint_plan_debug_message_level,
656 parse_messages_level_options,
663 DefineCustomBoolVariable("pg_hint_plan.enable_hint_table",
664 "Let pg_hint_plan look up the hint table.",
666 &pg_hint_plan_enable_hint_table,
675 prev_post_parse_analyze_hook = post_parse_analyze_hook;
676 post_parse_analyze_hook = pg_hint_plan_post_parse_analyze;
677 prev_planner = planner_hook;
678 planner_hook = pg_hint_plan_planner;
679 prev_join_search = join_search_hook;
680 join_search_hook = pg_hint_plan_join_search;
681 prev_set_rel_pathlist = set_rel_pathlist_hook;
682 set_rel_pathlist_hook = pg_hint_plan_set_rel_pathlist;
684 /* setup PL/pgSQL plugin hook */
685 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
686 *var_ptr = &plugin_funcs;
688 RegisterResourceReleaseCallback(plpgsql_query_erase_callback, NULL);
692 * Module unload callback
698 PLpgSQL_plugin **var_ptr;
700 /* Uninstall hooks. */
701 post_parse_analyze_hook = prev_post_parse_analyze_hook;
702 planner_hook = prev_planner;
703 join_search_hook = prev_join_search;
704 set_rel_pathlist_hook = prev_set_rel_pathlist;
706 /* uninstall PL/pgSQL plugin hook */
707 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
712 * create and delete functions the hint object
716 ScanMethodHintCreate(const char *hint_str, const char *keyword,
717 HintKeyword hint_keyword)
719 ScanMethodHint *hint;
721 hint = palloc(sizeof(ScanMethodHint));
722 hint->base.hint_str = hint_str;
723 hint->base.keyword = keyword;
724 hint->base.hint_keyword = hint_keyword;
725 hint->base.type = HINT_TYPE_SCAN_METHOD;
726 hint->base.state = HINT_STATE_NOTUSED;
727 hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
728 hint->base.desc_func = (HintDescFunction) ScanMethodHintDesc;
729 hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
730 hint->base.parse_func = (HintParseFunction) ScanMethodHintParse;
731 hint->relname = NULL;
732 hint->indexnames = NIL;
733 hint->regexp = false;
734 hint->enforce_mask = 0;
736 return (Hint *) hint;
740 ScanMethodHintDelete(ScanMethodHint *hint)
746 pfree(hint->relname);
747 list_free_deep(hint->indexnames);
752 JoinMethodHintCreate(const char *hint_str, const char *keyword,
753 HintKeyword hint_keyword)
755 JoinMethodHint *hint;
757 hint = palloc(sizeof(JoinMethodHint));
758 hint->base.hint_str = hint_str;
759 hint->base.keyword = keyword;
760 hint->base.hint_keyword = hint_keyword;
761 hint->base.type = HINT_TYPE_JOIN_METHOD;
762 hint->base.state = HINT_STATE_NOTUSED;
763 hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
764 hint->base.desc_func = (HintDescFunction) JoinMethodHintDesc;
765 hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
766 hint->base.parse_func = (HintParseFunction) JoinMethodHintParse;
768 hint->inner_nrels = 0;
769 hint->relnames = NULL;
770 hint->enforce_mask = 0;
771 hint->joinrelids = NULL;
772 hint->inner_joinrelids = NULL;
774 return (Hint *) hint;
778 JoinMethodHintDelete(JoinMethodHint *hint)
787 for (i = 0; i < hint->nrels; i++)
788 pfree(hint->relnames[i]);
789 pfree(hint->relnames);
792 bms_free(hint->joinrelids);
793 bms_free(hint->inner_joinrelids);
798 LeadingHintCreate(const char *hint_str, const char *keyword,
799 HintKeyword hint_keyword)
803 hint = palloc(sizeof(LeadingHint));
804 hint->base.hint_str = hint_str;
805 hint->base.keyword = keyword;
806 hint->base.hint_keyword = hint_keyword;
807 hint->base.type = HINT_TYPE_LEADING;
808 hint->base.state = HINT_STATE_NOTUSED;
809 hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
810 hint->base.desc_func = (HintDescFunction) LeadingHintDesc;
811 hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
812 hint->base.parse_func = (HintParseFunction) LeadingHintParse;
813 hint->relations = NIL;
814 hint->outer_inner = NULL;
816 return (Hint *) hint;
820 LeadingHintDelete(LeadingHint *hint)
825 list_free_deep(hint->relations);
826 if (hint->outer_inner)
827 pfree(hint->outer_inner);
832 SetHintCreate(const char *hint_str, const char *keyword,
833 HintKeyword hint_keyword)
837 hint = palloc(sizeof(SetHint));
838 hint->base.hint_str = hint_str;
839 hint->base.keyword = keyword;
840 hint->base.hint_keyword = hint_keyword;
841 hint->base.type = HINT_TYPE_SET;
842 hint->base.state = HINT_STATE_NOTUSED;
843 hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
844 hint->base.desc_func = (HintDescFunction) SetHintDesc;
845 hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
846 hint->base.parse_func = (HintParseFunction) SetHintParse;
851 return (Hint *) hint;
855 SetHintDelete(SetHint *hint)
865 list_free(hint->words);
870 RowsHintCreate(const char *hint_str, const char *keyword,
871 HintKeyword hint_keyword)
875 hint = palloc(sizeof(RowsHint));
876 hint->base.hint_str = hint_str;
877 hint->base.keyword = keyword;
878 hint->base.hint_keyword = hint_keyword;
879 hint->base.type = HINT_TYPE_ROWS;
880 hint->base.state = HINT_STATE_NOTUSED;
881 hint->base.delete_func = (HintDeleteFunction) RowsHintDelete;
882 hint->base.desc_func = (HintDescFunction) RowsHintDesc;
883 hint->base.cmp_func = (HintCmpFunction) RowsHintCmp;
884 hint->base.parse_func = (HintParseFunction) RowsHintParse;
886 hint->inner_nrels = 0;
887 hint->relnames = NULL;
888 hint->joinrelids = NULL;
889 hint->inner_joinrelids = NULL;
890 hint->rows_str = NULL;
891 hint->value_type = RVT_ABSOLUTE;
894 return (Hint *) hint;
898 RowsHintDelete(RowsHint *hint)
907 for (i = 0; i < hint->nrels; i++)
908 pfree(hint->relnames[i]);
909 pfree(hint->relnames);
912 bms_free(hint->joinrelids);
913 bms_free(hint->inner_joinrelids);
918 ParallelHintCreate(const char *hint_str, const char *keyword,
919 HintKeyword hint_keyword)
923 hint = palloc(sizeof(ScanMethodHint));
924 hint->base.hint_str = hint_str;
925 hint->base.keyword = keyword;
926 hint->base.hint_keyword = hint_keyword;
927 hint->base.type = HINT_TYPE_PARALLEL;
928 hint->base.state = HINT_STATE_NOTUSED;
929 hint->base.delete_func = (HintDeleteFunction) ParallelHintDelete;
930 hint->base.desc_func = (HintDescFunction) ParallelHintDesc;
931 hint->base.cmp_func = (HintCmpFunction) ParallelHintCmp;
932 hint->base.parse_func = (HintParseFunction) ParallelHintParse;
933 hint->relname = NULL;
935 hint->nworkers_str = "0";
937 return (Hint *) hint;
941 ParallelHintDelete(ParallelHint *hint)
947 pfree(hint->relname);
953 HintStateCreate(void)
957 hstate = palloc(sizeof(HintState));
958 hstate->hint_str = NULL;
959 hstate->nall_hints = 0;
960 hstate->max_all_hints = 0;
961 hstate->all_hints = NULL;
962 memset(hstate->num_hints, 0, sizeof(hstate->num_hints));
963 hstate->scan_hints = NULL;
964 hstate->init_scan_mask = 0;
965 hstate->init_nworkers = 0;
966 hstate->init_min_para_size = 0;
967 hstate->init_paratup_cost = 0;
968 hstate->init_parasetup_cost = 0;
969 hstate->current_root = NULL;
970 hstate->parent_relid = 0;
971 hstate->parent_scan_hint = NULL;
972 hstate->parent_parallel_hint = NULL;
973 hstate->parent_index_infos = NIL;
974 hstate->join_hints = NULL;
975 hstate->init_join_mask = 0;
976 hstate->join_hint_level = NULL;
977 hstate->leading_hint = NULL;
978 hstate->context = superuser() ? PGC_SUSET : PGC_USERSET;
979 hstate->set_hints = NULL;
980 hstate->rows_hints = NULL;
981 hstate->parallel_hints = NULL;
987 HintStateDelete(HintState *hstate)
994 if (hstate->hint_str)
995 pfree(hstate->hint_str);
997 for (i = 0; i < hstate->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
998 hstate->all_hints[i]->delete_func(hstate->all_hints[i]);
999 if (hstate->all_hints)
1000 pfree(hstate->all_hints);
1001 if (hstate->parent_index_infos)
1002 list_free(hstate->parent_index_infos);
1006 * Copy given value into buf, with quoting with '"' if necessary.
1009 quote_value(StringInfo buf, const char *value)
1011 bool need_quote = false;
1014 for (str = value; *str != '\0'; str++)
1016 if (isspace(*str) || *str == '(' || *str == ')' || *str == '"')
1019 appendStringInfoCharMacro(buf, '"');
1024 for (str = value; *str != '\0'; str++)
1027 appendStringInfoCharMacro(buf, '"');
1029 appendStringInfoCharMacro(buf, *str);
1033 appendStringInfoCharMacro(buf, '"');
1037 ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf)
1041 appendStringInfo(buf, "%s(", hint->base.keyword);
1042 if (hint->relname != NULL)
1044 quote_value(buf, hint->relname);
1045 foreach(l, hint->indexnames)
1047 appendStringInfoCharMacro(buf, ' ');
1048 quote_value(buf, (char *) lfirst(l));
1051 appendStringInfoString(buf, ")");
1053 appendStringInfoChar(buf, '\n');
1057 JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf)
1061 appendStringInfo(buf, "%s(", hint->base.keyword);
1062 if (hint->relnames != NULL)
1064 quote_value(buf, hint->relnames[0]);
1065 for (i = 1; i < hint->nrels; i++)
1067 appendStringInfoCharMacro(buf, ' ');
1068 quote_value(buf, hint->relnames[i]);
1071 appendStringInfoString(buf, ")");
1073 appendStringInfoChar(buf, '\n');
1077 OuterInnerDesc(OuterInnerRels *outer_inner, StringInfo buf)
1079 if (outer_inner->relation == NULL)
1086 appendStringInfoCharMacro(buf, '(');
1087 foreach(l, outer_inner->outer_inner_pair)
1092 appendStringInfoCharMacro(buf, ' ');
1094 OuterInnerDesc(lfirst(l), buf);
1097 appendStringInfoCharMacro(buf, ')');
1100 quote_value(buf, outer_inner->relation);
1104 LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf)
1106 appendStringInfo(buf, "%s(", HINT_LEADING);
1107 if (hint->outer_inner == NULL)
1114 foreach(l, hint->relations)
1119 appendStringInfoCharMacro(buf, ' ');
1121 quote_value(buf, (char *) lfirst(l));
1125 OuterInnerDesc(hint->outer_inner, buf);
1127 appendStringInfoString(buf, ")");
1129 appendStringInfoChar(buf, '\n');
1133 SetHintDesc(SetHint *hint, StringInfo buf, bool nolf)
1135 bool is_first = true;
1138 appendStringInfo(buf, "%s(", HINT_SET);
1139 foreach(l, hint->words)
1144 appendStringInfoCharMacro(buf, ' ');
1146 quote_value(buf, (char *) lfirst(l));
1148 appendStringInfo(buf, ")");
1150 appendStringInfoChar(buf, '\n');
1154 RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf)
1158 appendStringInfo(buf, "%s(", hint->base.keyword);
1159 if (hint->relnames != NULL)
1161 quote_value(buf, hint->relnames[0]);
1162 for (i = 1; i < hint->nrels; i++)
1164 appendStringInfoCharMacro(buf, ' ');
1165 quote_value(buf, hint->relnames[i]);
1168 appendStringInfo(buf, " %s", hint->rows_str);
1169 appendStringInfoString(buf, ")");
1171 appendStringInfoChar(buf, '\n');
1175 ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf)
1177 appendStringInfo(buf, "%s(", hint->base.keyword);
1178 if (hint->relname != NULL)
1180 quote_value(buf, hint->relname);
1182 /* number of workers */
1183 appendStringInfoCharMacro(buf, ' ');
1184 quote_value(buf, hint->nworkers_str);
1185 /* application mode of num of workers */
1186 appendStringInfoCharMacro(buf, ' ');
1187 appendStringInfoString(buf,
1188 (hint->force_parallel ? "hard" : "soft"));
1190 appendStringInfoString(buf, ")");
1192 appendStringInfoChar(buf, '\n');
1196 * Append string which represents all hints in a given state to buf, with
1197 * preceding title with them.
1200 desc_hint_in_state(HintState *hstate, StringInfo buf, const char *title,
1201 HintStatus state, bool nolf)
1205 appendStringInfo(buf, "%s:", title);
1207 appendStringInfoChar(buf, '\n');
1210 for (i = 0; i < hstate->nall_hints; i++)
1212 if (hstate->all_hints[i]->state != state)
1215 hstate->all_hints[i]->desc_func(hstate->all_hints[i], buf, nolf);
1219 if (nolf && nshown == 0)
1220 appendStringInfoString(buf, "(none)");
1224 * Dump contents of given hstate to server log with log level LOG.
1227 HintStateDump(HintState *hstate)
1233 elog(pg_hint_plan_debug_message_level, "pg_hint_plan:\nno hint");
1237 initStringInfo(&buf);
1239 appendStringInfoString(&buf, "pg_hint_plan:\n");
1240 desc_hint_in_state(hstate, &buf, "used hint", HINT_STATE_USED, false);
1241 desc_hint_in_state(hstate, &buf, "not used hint", HINT_STATE_NOTUSED, false);
1242 desc_hint_in_state(hstate, &buf, "duplication hint", HINT_STATE_DUPLICATION, false);
1243 desc_hint_in_state(hstate, &buf, "error hint", HINT_STATE_ERROR, false);
1245 ereport(pg_hint_plan_debug_message_level,
1246 (errmsg ("%s", buf.data)));
1252 HintStateDump2(HintState *hstate)
1258 elog(pg_hint_plan_debug_message_level,
1259 "pg_hint_plan%s: HintStateDump: no hint", qnostr);
1263 initStringInfo(&buf);
1264 appendStringInfo(&buf, "pg_hint_plan%s: HintStateDump: ", qnostr);
1265 desc_hint_in_state(hstate, &buf, "{used hints", HINT_STATE_USED, true);
1266 desc_hint_in_state(hstate, &buf, "}, {not used hints", HINT_STATE_NOTUSED, true);
1267 desc_hint_in_state(hstate, &buf, "}, {duplicate hints", HINT_STATE_DUPLICATION, true);
1268 desc_hint_in_state(hstate, &buf, "}, {error hints", HINT_STATE_ERROR, true);
1269 appendStringInfoChar(&buf, '}');
1271 ereport(pg_hint_plan_debug_message_level,
1272 (errmsg("%s", buf.data),
1274 errhidecontext(true)));
1284 RelnameCmp(const void *a, const void *b)
1286 const char *relnamea = *((const char **) a);
1287 const char *relnameb = *((const char **) b);
1289 return strcmp(relnamea, relnameb);
1293 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
1295 return RelnameCmp(&a->relname, &b->relname);
1299 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
1303 if (a->nrels != b->nrels)
1304 return a->nrels - b->nrels;
1306 for (i = 0; i < a->nrels; i++)
1309 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1317 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
1323 SetHintCmp(const SetHint *a, const SetHint *b)
1325 return strcmp(a->name, b->name);
1329 RowsHintCmp(const RowsHint *a, const RowsHint *b)
1333 if (a->nrels != b->nrels)
1334 return a->nrels - b->nrels;
1336 for (i = 0; i < a->nrels; i++)
1339 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1347 ParallelHintCmp(const ParallelHint *a, const ParallelHint *b)
1349 return RelnameCmp(&a->relname, &b->relname);
1353 HintCmp(const void *a, const void *b)
1355 const Hint *hinta = *((const Hint **) a);
1356 const Hint *hintb = *((const Hint **) b);
1358 if (hinta->type != hintb->type)
1359 return hinta->type - hintb->type;
1360 if (hinta->state == HINT_STATE_ERROR)
1362 if (hintb->state == HINT_STATE_ERROR)
1364 return hinta->cmp_func(hinta, hintb);
1368 * Returns byte offset of hint b from hint a. If hint a was specified before
1369 * b, positive value is returned.
1372 HintCmpWithPos(const void *a, const void *b)
1374 const Hint *hinta = *((const Hint **) a);
1375 const Hint *hintb = *((const Hint **) b);
1378 result = HintCmp(a, b);
1380 result = hinta->hint_str - hintb->hint_str;
1389 parse_keyword(const char *str, StringInfo buf)
1393 while (!isspace(*str) && *str != '(' && *str != '\0')
1394 appendStringInfoCharMacro(buf, *str++);
1400 skip_parenthesis(const char *str, char parenthesis)
1404 if (*str != parenthesis)
1406 if (parenthesis == '(')
1407 hint_ereport(str, ("Opening parenthesis is necessary."));
1408 else if (parenthesis == ')')
1409 hint_ereport(str, ("Closing parenthesis is necessary."));
1420 * Parse a token from str, and store malloc'd copy into word. A token can be
1421 * quoted with '"'. Return value is pointer to unparsed portion of original
1422 * string, or NULL if an error occurred.
1424 * Parsed token is truncated within NAMEDATALEN-1 bytes, when truncate is true.
1427 parse_quoted_value(const char *str, char **word, bool truncate)
1432 /* Skip leading spaces. */
1435 initStringInfo(&buf);
1448 /* Double quotation must be closed. */
1452 hint_ereport(str, ("Unterminated quoted string."));
1457 * Skip escaped double quotation.
1459 * We don't allow slash-asterisk and asterisk-slash (delimiters of
1460 * block comments) to be an object name, so users must specify
1461 * alias for such object names.
1463 * Those special names can be allowed if we care escaped slashes
1464 * and asterisks, but we don't.
1473 else if (isspace(*str) || *str == '(' || *str == ')' || *str == '"' ||
1477 appendStringInfoCharMacro(&buf, *str++);
1482 hint_ereport(str, ("Zero-length delimited string."));
1489 /* Truncate name if it's too long */
1491 truncate_identifier(buf.data, strlen(buf.data), true);
1498 static OuterInnerRels *
1499 OuterInnerRelsCreate(char *name, List *outer_inner_list)
1501 OuterInnerRels *outer_inner;
1503 outer_inner = palloc(sizeof(OuterInnerRels));
1504 outer_inner->relation = name;
1505 outer_inner->outer_inner_pair = outer_inner_list;
1511 parse_parentheses_Leading_in(const char *str, OuterInnerRels **outer_inner)
1513 List *outer_inner_pair = NIL;
1515 if ((str = skip_parenthesis(str, '(')) == NULL)
1520 /* Store words in parentheses into outer_inner_list. */
1521 while(*str != ')' && *str != '\0')
1523 OuterInnerRels *outer_inner_rels;
1527 str = parse_parentheses_Leading_in(str, &outer_inner_rels);
1535 if ((str = parse_quoted_value(str, &name, true)) == NULL)
1538 outer_inner_rels = OuterInnerRelsCreate(name, NIL);
1541 outer_inner_pair = lappend(outer_inner_pair, outer_inner_rels);
1546 (str = skip_parenthesis(str, ')')) == NULL)
1548 list_free(outer_inner_pair);
1552 *outer_inner = OuterInnerRelsCreate(NULL, outer_inner_pair);
1558 parse_parentheses_Leading(const char *str, List **name_list,
1559 OuterInnerRels **outer_inner)
1562 bool truncate = true;
1564 if ((str = skip_parenthesis(str, '(')) == NULL)
1570 if ((str = parse_parentheses_Leading_in(str, outer_inner)) == NULL)
1575 /* Store words in parentheses into name_list. */
1576 while(*str != ')' && *str != '\0')
1578 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1580 list_free(*name_list);
1584 *name_list = lappend(*name_list, name);
1589 if ((str = skip_parenthesis(str, ')')) == NULL)
1595 parse_parentheses(const char *str, List **name_list, HintKeyword keyword)
1598 bool truncate = true;
1600 if ((str = skip_parenthesis(str, '(')) == NULL)
1605 /* Store words in parentheses into name_list. */
1606 while(*str != ')' && *str != '\0')
1608 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1610 list_free(*name_list);
1614 *name_list = lappend(*name_list, name);
1617 if (keyword == HINT_KEYWORD_INDEXSCANREGEXP ||
1618 keyword == HINT_KEYWORD_INDEXONLYSCANREGEXP ||
1619 keyword == HINT_KEYWORD_BITMAPSCANREGEXP ||
1620 keyword == HINT_KEYWORD_SET)
1626 if ((str = skip_parenthesis(str, ')')) == NULL)
1632 parse_hints(HintState *hstate, Query *parse, const char *str)
1637 initStringInfo(&buf);
1638 while (*str != '\0')
1640 const HintParser *parser;
1642 /* in error message, we output the comment including the keyword. */
1643 head = (char *) str;
1645 /* parse only the keyword of the hint. */
1646 resetStringInfo(&buf);
1647 str = parse_keyword(str, &buf);
1649 for (parser = parsers; parser->keyword != NULL; parser++)
1651 char *keyword = parser->keyword;
1654 if (pg_strcasecmp(buf.data, keyword) != 0)
1657 hint = parser->create_func(head, keyword, parser->hint_keyword);
1659 /* parser of each hint does parse in a parenthesis. */
1660 if ((str = hint->parse_func(hint, hstate, parse, str)) == NULL)
1662 hint->delete_func(hint);
1668 * Add hint information into all_hints array. If we don't have
1669 * enough space, double the array.
1671 if (hstate->nall_hints == 0)
1673 hstate->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
1674 hstate->all_hints = (Hint **)
1675 palloc(sizeof(Hint *) * hstate->max_all_hints);
1677 else if (hstate->nall_hints == hstate->max_all_hints)
1679 hstate->max_all_hints *= 2;
1680 hstate->all_hints = (Hint **)
1681 repalloc(hstate->all_hints,
1682 sizeof(Hint *) * hstate->max_all_hints);
1685 hstate->all_hints[hstate->nall_hints] = hint;
1686 hstate->nall_hints++;
1693 if (parser->keyword == NULL)
1696 ("Unrecognized hint keyword \"%s\".", buf.data));
1707 * Get hints from table by client-supplied query string and application name.
1710 get_hints_from_table(const char *client_query, const char *client_application)
1712 const char *search_query =
1714 " FROM hint_plan.hints "
1715 " WHERE norm_query_string = $1 "
1716 " AND ( application_name = $2 "
1717 " OR application_name = '' ) "
1718 " ORDER BY application_name DESC";
1719 static SPIPlanPtr plan = NULL;
1721 Oid argtypes[2] = { TEXTOID, TEXTOID };
1723 bool nulls[2] = { false, false };
1729 bool snapshot_set = false;
1731 hint_inhibit_level++;
1733 if (!ActiveSnapshotSet())
1735 PushActiveSnapshot(GetTransactionSnapshot());
1736 snapshot_set = true;
1744 p = SPI_prepare(search_query, 2, argtypes);
1745 plan = SPI_saveplan(p);
1749 qry = cstring_to_text(client_query);
1750 app = cstring_to_text(client_application);
1751 values[0] = PointerGetDatum(qry);
1752 values[1] = PointerGetDatum(app);
1754 SPI_execute_plan(plan, values, nulls, true, 1);
1756 if (SPI_processed > 0)
1760 hints = SPI_getvalue(SPI_tuptable->vals[0],
1761 SPI_tuptable->tupdesc, 1);
1763 * Here we use SPI_palloc to ensure that hints string is valid even
1764 * after SPI_finish call. We can't use simple palloc because it
1765 * allocates memory in SPI's context and that context is deleted in
1768 buf = SPI_palloc(strlen(hints) + 1);
1776 PopActiveSnapshot();
1778 hint_inhibit_level--;
1782 hint_inhibit_level--;
1791 * Get client-supplied query string. Addtion to that the jumbled query is
1792 * supplied if the caller requested. From the restriction of JumbleQuery, some
1793 * kind of query needs special amendments. Reutrns NULL if this query doesn't
1794 * change the current hint. This function returns NULL also when something
1795 * wrong has happend and let the caller continue using the current hints.
1798 get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
1800 const char *p = debug_query_string;
1803 * If debug_query_string is set, it is the top level statement. But in some
1804 * cases we reach here with debug_query_string set NULL for example in the
1805 * case of DESCRIBE message handling. We may still see a candidate
1806 * top-level query in pstate in the case.
1810 /* We don't see a query string, return NULL */
1811 if (!pstate->p_sourcetext)
1814 p = pstate->p_sourcetext;
1817 if (jumblequery != NULL)
1818 *jumblequery = query;
1820 /* Query for DeclareCursorStmt is CMD_SELECT and has query->utilityStmt */
1821 if (query->commandType == CMD_UTILITY || query->utilityStmt)
1823 Query *target_query = query;
1826 * Some utility statements have a subquery that we can hint on. Since
1827 * EXPLAIN can be placed before other kind of utility statements and
1828 * EXECUTE can be contained other kind of utility statements, these
1829 * conditions are not mutually exclusive and should be considered in
1832 if (IsA(target_query->utilityStmt, ExplainStmt))
1834 ExplainStmt *stmt = (ExplainStmt *)target_query->utilityStmt;
1836 Assert(IsA(stmt->query, Query));
1837 target_query = (Query *)stmt->query;
1839 /* strip out the top-level query for further processing */
1840 if (target_query->commandType == CMD_UTILITY &&
1841 target_query->utilityStmt != NULL)
1842 target_query = (Query *)target_query->utilityStmt;
1846 * JumbleQuery does not accept a Query that has utilityStmt. On the
1847 * other hand DeclareCursorStmt is in a bit strange shape that is
1848 * flipped upside down.
1850 if (IsA(target_query, Query) &&
1851 target_query->utilityStmt &&
1852 IsA(target_query->utilityStmt, DeclareCursorStmt))
1855 * The given Query cannot be modified so copy it and modify so that
1856 * JumbleQuery can accept it.
1858 Assert(IsA(target_query, Query) &&
1859 target_query->commandType == CMD_SELECT);
1860 target_query = copyObject(target_query);
1861 target_query->utilityStmt = NULL;
1864 if (IsA(target_query, CreateTableAsStmt))
1866 CreateTableAsStmt *stmt = (CreateTableAsStmt *) target_query;
1868 Assert(IsA(stmt->query, Query));
1869 target_query = (Query *) stmt->query;
1871 /* strip out the top-level query for further processing */
1872 if (target_query->commandType == CMD_UTILITY &&
1873 target_query->utilityStmt != NULL)
1874 target_query = (Query *)target_query->utilityStmt;
1877 if (IsA(target_query, ExecuteStmt))
1880 * Use the prepared query for EXECUTE. The Query for jumble
1881 * also replaced with the corresponding one.
1883 ExecuteStmt *stmt = (ExecuteStmt *)target_query;
1884 PreparedStatement *entry;
1886 entry = FetchPreparedStatement(stmt->name, true);
1887 p = entry->plansource->query_string;
1888 target_query = (Query *) linitial (entry->plansource->query_list);
1891 /* JumbleQuery accespts only a non-utility Query */
1892 if (!IsA(target_query, Query) ||
1893 target_query->utilityStmt != NULL)
1894 target_query = NULL;
1897 *jumblequery = target_query;
1900 /* Return NULL if the pstate is not identical to the top-level query */
1901 else if (strcmp(pstate->p_sourcetext, p) != 0)
1908 * Get hints from the head block comment in client-supplied query string.
1911 get_hints_from_comment(const char *p)
1913 const char *hint_head;
1921 /* extract query head comment. */
1922 hint_head = strstr(p, HINT_START);
1923 if (hint_head == NULL)
1925 for (;p < hint_head; p++)
1928 * Allow these characters precedes hint comment:
1930 * - alphabets which are in ASCII range
1931 * - space, tabs and new-lines
1932 * - underscores, for identifier
1933 * - commas, for SELECT clause, EXPLAIN and PREPARE
1934 * - parentheses, for EXPLAIN and PREPARE
1936 * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1937 * avoid behavior which depends on locale setting.
1939 if (!(*p >= '0' && *p <= '9') &&
1940 !(*p >= 'A' && *p <= 'Z') &&
1941 !(*p >= 'a' && *p <= 'z') &&
1945 *p != '(' && *p != ')')
1949 len = strlen(HINT_START);
1954 /* find hint end keyword. */
1955 if ((tail = strstr(p, HINT_END)) == NULL)
1957 hint_ereport(head, ("Unterminated block comment."));
1961 /* We don't support nested block comments. */
1962 if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1964 hint_ereport(head, ("Nested block comments are not supported."));
1968 /* Make a copy of hint. */
1970 head = palloc(len + 1);
1971 memcpy(head, p, len);
1979 * Parse hints that got, create hint struct from parse tree and parse hints.
1982 create_hintstate(Query *parse, const char *hints)
1991 /* -1 means that no Parallel hint is specified. */
1992 max_hint_nworkers = -1;
1995 hstate = HintStateCreate();
1996 hstate->hint_str = (char *) hints;
1998 /* parse each hint. */
1999 parse_hints(hstate, parse, p);
2001 /* When nothing specified a hint, we free HintState and returns NULL. */
2002 if (hstate->nall_hints == 0)
2004 HintStateDelete(hstate);
2008 /* Sort hints in order of original position. */
2009 qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
2012 /* Count number of hints per hint-type. */
2013 for (i = 0; i < hstate->nall_hints; i++)
2015 Hint *cur_hint = hstate->all_hints[i];
2016 hstate->num_hints[cur_hint->type]++;
2020 * If an object (or a set of objects) has multiple hints of same hint-type,
2021 * only the last hint is valid and others are ignored in planning.
2022 * Hints except the last are marked as 'duplicated' to remember the order.
2024 for (i = 0; i < hstate->nall_hints - 1; i++)
2026 Hint *cur_hint = hstate->all_hints[i];
2027 Hint *next_hint = hstate->all_hints[i + 1];
2030 * Leading hint is marked as 'duplicated' in transform_join_hints.
2032 if (cur_hint->type == HINT_TYPE_LEADING &&
2033 next_hint->type == HINT_TYPE_LEADING)
2037 * Note that we need to pass addresses of hint pointers, because
2038 * HintCmp is designed to sort array of Hint* by qsort.
2040 if (HintCmp(&cur_hint, &next_hint) == 0)
2042 hint_ereport(cur_hint->hint_str,
2043 ("Conflict %s hint.", HintTypeName[cur_hint->type]));
2044 cur_hint->state = HINT_STATE_DUPLICATION;
2049 * Make sure that per-type array pointers point proper position in the
2050 * array which consists of all hints.
2052 hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
2053 hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
2054 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
2055 hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
2056 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
2057 hstate->set_hints = (SetHint **) (hstate->leading_hint +
2058 hstate->num_hints[HINT_TYPE_LEADING]);
2059 hstate->rows_hints = (RowsHint **) (hstate->set_hints +
2060 hstate->num_hints[HINT_TYPE_SET]);
2061 hstate->parallel_hints = (ParallelHint **) (hstate->set_hints +
2062 hstate->num_hints[HINT_TYPE_ROWS]);
2068 * Parse inside of parentheses of scan-method hints.
2071 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
2074 const char *keyword = hint->base.keyword;
2075 HintKeyword hint_keyword = hint->base.hint_keyword;
2076 List *name_list = NIL;
2079 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2082 /* Parse relation name and index name(s) if given hint accepts. */
2083 length = list_length(name_list);
2085 /* at least twp parameters required */
2089 ("%s hint requires a relation.", hint->base.keyword));
2090 hint->base.state = HINT_STATE_ERROR;
2094 hint->relname = linitial(name_list);
2095 hint->indexnames = list_delete_first(name_list);
2097 /* check whether the hint accepts index name(s) */
2098 if (length > 1 && !SCAN_HINT_ACCEPTS_INDEX_NAMES(hint_keyword))
2101 ("%s hint accepts only one relation.",
2102 hint->base.keyword));
2103 hint->base.state = HINT_STATE_ERROR;
2107 /* Set a bit for specified hint. */
2108 switch (hint_keyword)
2110 case HINT_KEYWORD_SEQSCAN:
2111 hint->enforce_mask = ENABLE_SEQSCAN;
2113 case HINT_KEYWORD_INDEXSCAN:
2114 hint->enforce_mask = ENABLE_INDEXSCAN;
2116 case HINT_KEYWORD_INDEXSCANREGEXP:
2117 hint->enforce_mask = ENABLE_INDEXSCAN;
2118 hint->regexp = true;
2120 case HINT_KEYWORD_BITMAPSCAN:
2121 hint->enforce_mask = ENABLE_BITMAPSCAN;
2123 case HINT_KEYWORD_BITMAPSCANREGEXP:
2124 hint->enforce_mask = ENABLE_BITMAPSCAN;
2125 hint->regexp = true;
2127 case HINT_KEYWORD_TIDSCAN:
2128 hint->enforce_mask = ENABLE_TIDSCAN;
2130 case HINT_KEYWORD_NOSEQSCAN:
2131 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
2133 case HINT_KEYWORD_NOINDEXSCAN:
2134 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
2136 case HINT_KEYWORD_NOBITMAPSCAN:
2137 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
2139 case HINT_KEYWORD_NOTIDSCAN:
2140 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
2142 case HINT_KEYWORD_INDEXONLYSCAN:
2143 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2145 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
2146 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2147 hint->regexp = true;
2149 case HINT_KEYWORD_NOINDEXONLYSCAN:
2150 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
2153 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2162 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
2165 const char *keyword = hint->base.keyword;
2166 HintKeyword hint_keyword = hint->base.hint_keyword;
2167 List *name_list = NIL;
2169 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2172 hint->nrels = list_length(name_list);
2174 if (hint->nrels > 0)
2180 * Transform relation names from list to array to sort them with qsort
2183 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2184 foreach (l, name_list)
2186 hint->relnames[i] = lfirst(l);
2191 list_free(name_list);
2193 /* A join hint requires at least two relations */
2194 if (hint->nrels < 2)
2197 ("%s hint requires at least two relations.",
2198 hint->base.keyword));
2199 hint->base.state = HINT_STATE_ERROR;
2203 /* Sort hints in alphabetical order of relation names. */
2204 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2206 switch (hint_keyword)
2208 case HINT_KEYWORD_NESTLOOP:
2209 hint->enforce_mask = ENABLE_NESTLOOP;
2211 case HINT_KEYWORD_MERGEJOIN:
2212 hint->enforce_mask = ENABLE_MERGEJOIN;
2214 case HINT_KEYWORD_HASHJOIN:
2215 hint->enforce_mask = ENABLE_HASHJOIN;
2217 case HINT_KEYWORD_NONESTLOOP:
2218 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
2220 case HINT_KEYWORD_NOMERGEJOIN:
2221 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
2223 case HINT_KEYWORD_NOHASHJOIN:
2224 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
2227 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2236 OuterInnerPairCheck(OuterInnerRels *outer_inner)
2239 if (outer_inner->outer_inner_pair == NIL)
2241 if (outer_inner->relation)
2247 if (list_length(outer_inner->outer_inner_pair) == 2)
2249 foreach(l, outer_inner->outer_inner_pair)
2251 if (!OuterInnerPairCheck(lfirst(l)))
2262 OuterInnerList(OuterInnerRels *outer_inner)
2264 List *outer_inner_list = NIL;
2266 OuterInnerRels *outer_inner_rels;
2268 foreach(l, outer_inner->outer_inner_pair)
2270 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
2272 if (outer_inner_rels->relation != NULL)
2273 outer_inner_list = lappend(outer_inner_list,
2274 outer_inner_rels->relation);
2276 outer_inner_list = list_concat(outer_inner_list,
2277 OuterInnerList(outer_inner_rels));
2279 return outer_inner_list;
2283 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
2286 List *name_list = NIL;
2287 OuterInnerRels *outer_inner = NULL;
2289 if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
2293 if (outer_inner != NULL)
2294 name_list = OuterInnerList(outer_inner);
2296 hint->relations = name_list;
2297 hint->outer_inner = outer_inner;
2299 /* A Leading hint requires at least two relations */
2300 if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
2302 hint_ereport(hint->base.hint_str,
2303 ("%s hint requires at least two relations.",
2305 hint->base.state = HINT_STATE_ERROR;
2307 else if (hint->outer_inner != NULL &&
2308 !OuterInnerPairCheck(hint->outer_inner))
2310 hint_ereport(hint->base.hint_str,
2311 ("%s hint requires two sets of relations when parentheses nests.",
2313 hint->base.state = HINT_STATE_ERROR;
2320 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
2322 List *name_list = NIL;
2324 if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
2328 hint->words = name_list;
2330 /* We need both name and value to set GUC parameter. */
2331 if (list_length(name_list) == 2)
2333 hint->name = linitial(name_list);
2334 hint->value = lsecond(name_list);
2338 hint_ereport(hint->base.hint_str,
2339 ("%s hint requires name and value of GUC parameter.",
2341 hint->base.state = HINT_STATE_ERROR;
2348 RowsHintParse(RowsHint *hint, HintState *hstate, Query *parse,
2351 HintKeyword hint_keyword = hint->base.hint_keyword;
2352 List *name_list = NIL;
2356 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2359 /* Last element must be rows specification */
2360 hint->nrels = list_length(name_list) - 1;
2362 if (hint->nrels > 0)
2368 * Transform relation names from list to array to sort them with qsort
2371 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2372 foreach (l, name_list)
2374 if (hint->nrels <= i)
2376 hint->relnames[i] = lfirst(l);
2381 /* Retieve rows estimation */
2382 rows_str = list_nth(name_list, hint->nrels);
2383 hint->rows_str = rows_str; /* store as-is for error logging */
2384 if (rows_str[0] == '#')
2386 hint->value_type = RVT_ABSOLUTE;
2389 else if (rows_str[0] == '+')
2391 hint->value_type = RVT_ADD;
2394 else if (rows_str[0] == '-')
2396 hint->value_type = RVT_SUB;
2399 else if (rows_str[0] == '*')
2401 hint->value_type = RVT_MULTI;
2406 hint_ereport(rows_str, ("Unrecognized rows value type notation."));
2407 hint->base.state = HINT_STATE_ERROR;
2410 hint->rows = strtod(rows_str, &end_ptr);
2413 hint_ereport(rows_str,
2414 ("%s hint requires valid number as rows estimation.",
2415 hint->base.keyword));
2416 hint->base.state = HINT_STATE_ERROR;
2420 /* A join hint requires at least two relations */
2421 if (hint->nrels < 2)
2424 ("%s hint requires at least two relations.",
2425 hint->base.keyword));
2426 hint->base.state = HINT_STATE_ERROR;
2430 list_free(name_list);
2432 /* Sort relnames in alphabetical order. */
2433 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2439 ParallelHintParse(ParallelHint *hint, HintState *hstate, Query *parse,
2442 HintKeyword hint_keyword = hint->base.hint_keyword;
2443 List *name_list = NIL;
2447 bool force_parallel = false;
2449 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2452 /* Parse relation name and index name(s) if given hint accepts. */
2453 length = list_length(name_list);
2455 if (length < 2 || length > 3)
2458 ("wrong number of arguments (%d): %s",
2459 length, hint->base.keyword));
2460 hint->base.state = HINT_STATE_ERROR;
2464 hint->relname = linitial(name_list);
2466 /* The second parameter is number of workers */
2467 hint->nworkers_str = list_nth(name_list, 1);
2468 nworkers = strtod(hint->nworkers_str, &end_ptr);
2469 if (*end_ptr || nworkers < 0 || nworkers > max_worker_processes)
2472 hint_ereport(hint->nworkers_str,
2473 ("number of workers must be a number: %s",
2474 hint->base.keyword));
2475 else if (nworkers < 0)
2476 hint_ereport(hint->nworkers_str,
2477 ("number of workers must be positive: %s",
2478 hint->base.keyword));
2479 else if (nworkers > max_worker_processes)
2480 hint_ereport(hint->nworkers_str,
2481 ("number of workers = %d is larger than max_worker_processes(%d): %s",
2482 nworkers, max_worker_processes, hint->base.keyword));
2484 hint->base.state = HINT_STATE_ERROR;
2487 hint->nworkers = nworkers;
2489 /* optional third parameter is specified */
2492 const char *modeparam = (const char *)list_nth(name_list, 2);
2493 if (pg_strcasecmp(modeparam, "hard") == 0)
2494 force_parallel = true;
2495 else if (pg_strcasecmp(modeparam, "soft") != 0)
2497 hint_ereport(modeparam,
2498 ("enforcement must be soft or hard: %s",
2499 hint->base.keyword));
2500 hint->base.state = HINT_STATE_ERROR;
2504 hint->force_parallel = force_parallel;
2506 if (hint->base.state != HINT_STATE_ERROR &&
2507 nworkers > max_hint_nworkers)
2508 max_hint_nworkers = nworkers;
2514 * set GUC parameter functions
2518 get_current_scan_mask()
2523 mask |= ENABLE_SEQSCAN;
2524 if (enable_indexscan)
2525 mask |= ENABLE_INDEXSCAN;
2526 if (enable_bitmapscan)
2527 mask |= ENABLE_BITMAPSCAN;
2529 mask |= ENABLE_TIDSCAN;
2530 if (enable_indexonlyscan)
2531 mask |= ENABLE_INDEXONLYSCAN;
2537 get_current_join_mask()
2541 if (enable_nestloop)
2542 mask |= ENABLE_NESTLOOP;
2543 if (enable_mergejoin)
2544 mask |= ENABLE_MERGEJOIN;
2545 if (enable_hashjoin)
2546 mask |= ENABLE_HASHJOIN;
2552 * Sets GUC prameters without throwing exception. Reutrns false if something
2556 set_config_option_noerror(const char *name, const char *value,
2557 GucContext context, GucSource source,
2558 GucAction action, bool changeVal, int elevel)
2561 MemoryContext ccxt = CurrentMemoryContext;
2565 result = set_config_option(name, value, context, source,
2566 action, changeVal, 0, false);
2572 /* Save error info */
2573 MemoryContextSwitchTo(ccxt);
2574 errdata = CopyErrorData();
2578 (errcode(errdata->sqlerrcode),
2579 errmsg("%s", errdata->message),
2580 errdata->detail ? errdetail("%s", errdata->detail) : 0,
2581 errdata->hint ? errhint("%s", errdata->hint) : 0));
2583 FreeErrorData(errdata);
2591 * Sets GUC parameter of int32 type without throwing exceptions. Returns false
2592 * if something wrong.
2595 set_config_int32_option(const char *name, int32 value, GucContext context)
2597 char buf[16]; /* enough for int32 */
2599 if (snprintf(buf, 16, "%d", value) < 0)
2601 ereport(pg_hint_plan_parse_message_level,
2602 (errmsg ("Failed to convert integer to string: %d", value)));
2607 set_config_option_noerror(name, buf, context,
2608 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2609 pg_hint_plan_parse_message_level);
2612 /* setup scan method enforcement according to given options */
2614 setup_guc_enforcement(SetHint **options, int noptions, GucContext context)
2618 for (i = 0; i < noptions; i++)
2620 SetHint *hint = options[i];
2623 if (!hint_state_enabled(hint))
2626 result = set_config_option_noerror(hint->name, hint->value, context,
2627 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2628 pg_hint_plan_parse_message_level);
2630 hint->base.state = HINT_STATE_USED;
2632 hint->base.state = HINT_STATE_ERROR;
2639 * Setup parallel execution environment.
2641 * If hint is not NULL, set up using it, elsewise reset to initial environment.
2644 setup_parallel_plan_enforcement(ParallelHint *hint, HintState *state)
2648 hint->base.state = HINT_STATE_USED;
2649 set_config_int32_option("max_parallel_workers_per_gather",
2650 hint->nworkers, state->context);
2653 set_config_int32_option("max_parallel_workers_per_gather",
2654 state->init_nworkers, state->context);
2656 /* force means that enforce parallel as far as possible */
2657 if (hint && hint->force_parallel)
2659 set_config_int32_option("parallel_tuple_cost", 0, state->context);
2660 set_config_int32_option("parallel_setup_cost", 0, state->context);
2661 set_config_int32_option("min_parallel_relation_size", 0,
2666 set_config_int32_option("parallel_tuple_cost",
2667 state->init_paratup_cost, state->context);
2668 set_config_int32_option("parallel_setup_cost",
2669 state->init_parasetup_cost, state->context);
2670 set_config_int32_option("min_parallel_relation_size",
2671 state->init_min_para_size, state->context);
2675 #define SET_CONFIG_OPTION(name, type_bits) \
2676 set_config_option_noerror((name), \
2677 (mask & (type_bits)) ? "true" : "false", \
2678 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
2682 * Setup GUC environment to enforce scan methods. If scanhint is NULL, reset
2683 * GUCs to the saved state in state.
2686 setup_scan_method_enforcement(ScanMethodHint *scanhint, HintState *state)
2688 unsigned char enforce_mask = state->init_scan_mask;
2689 GucContext context = state->context;
2694 enforce_mask = scanhint->enforce_mask;
2695 scanhint->base.state = HINT_STATE_USED;
2698 if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
2699 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
2700 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
2702 mask = enforce_mask;
2704 mask = enforce_mask & current_hint_state->init_scan_mask;
2706 SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
2707 SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
2708 SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
2709 SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
2710 SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
2714 set_join_config_options(unsigned char enforce_mask, GucContext context)
2718 if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
2719 enforce_mask == ENABLE_HASHJOIN)
2720 mask = enforce_mask;
2722 mask = enforce_mask & current_hint_state->init_join_mask;
2724 SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
2725 SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
2726 SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
2730 * Push a hint into hint stack which is implemented with List struct. Head of
2731 * list is top of stack.
2734 push_hint(HintState *hstate)
2736 /* Prepend new hint to the list means pushing to stack. */
2737 HintStateStack = lcons(hstate, HintStateStack);
2739 /* Pushed hint is the one which should be used hereafter. */
2740 current_hint_state = hstate;
2743 /* Pop a hint from hint stack. Popped hint is automatically discarded. */
2747 /* Hint stack must not be empty. */
2748 if(HintStateStack == NIL)
2749 elog(ERROR, "hint stack is empty");
2752 * Take a hint at the head from the list, and free it. Switch
2753 * current_hint_state to point new head (NULL if the list is empty).
2755 HintStateStack = list_delete_first(HintStateStack);
2756 HintStateDelete(current_hint_state);
2757 if(HintStateStack == NIL)
2758 current_hint_state = NULL;
2760 current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
2764 * Retrieve and store a hint string from given query or from the hint table.
2765 * If we are using the hint table, the query string is needed to be normalized.
2766 * However, ParseState, which is not available in planner_hook, is required to
2767 * check if the query tree (Query) is surely corresponding to the target query.
2770 pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
2772 const char *query_str;
2773 MemoryContext oldcontext;
2775 if (prev_post_parse_analyze_hook)
2776 prev_post_parse_analyze_hook(pstate, query);
2778 /* do nothing under hint table search */
2779 if (hint_inhibit_level > 0)
2782 if (!pg_hint_plan_enable_hint)
2784 if (current_hint_str)
2786 pfree((void *)current_hint_str);
2787 current_hint_str = NULL;
2792 /* increment the query number */
2794 if (debug_level > 1)
2795 snprintf(qnostr, sizeof(qnostr), "[qno=0x%x]", qno++);
2798 /* search the hint table for a hint if requested */
2799 if (pg_hint_plan_enable_hint_table)
2802 pgssJumbleState jstate;
2804 char *normalized_query = NULL;
2806 query_str = get_query_string(pstate, query, &jumblequery);
2808 /* If this query is not for hint, just return */
2812 /* clear the previous hint string */
2813 if (current_hint_str)
2815 pfree((void *)current_hint_str);
2816 current_hint_str = NULL;
2822 * XXX: normalizing code is copied from pg_stat_statements.c, so be
2823 * careful to PostgreSQL's version up.
2825 jstate.jumble = (unsigned char *) palloc(JUMBLE_SIZE);
2826 jstate.jumble_len = 0;
2827 jstate.clocations_buf_size = 32;
2828 jstate.clocations = (pgssLocationLen *)
2829 palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
2830 jstate.clocations_count = 0;
2832 JumbleQuery(&jstate, jumblequery);
2835 * Normalize the query string by replacing constants with '?'
2838 * Search hint string which is stored keyed by query string
2839 * and application name. The query string is normalized to allow
2842 * Adding 1 byte to query_len ensures that the returned string has
2843 * a terminating NULL.
2845 query_len = strlen(query_str) + 1;
2847 generate_normalized_query(&jstate, query_str,
2849 GetDatabaseEncoding());
2852 * find a hint for the normalized query. the result should be in
2855 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2857 get_hints_from_table(normalized_query, application_name);
2858 MemoryContextSwitchTo(oldcontext);
2860 if (debug_level > 1)
2862 if (current_hint_str)
2863 ereport(pg_hint_plan_debug_message_level,
2864 (errmsg("pg_hint_plan[qno=0x%x]: "
2865 "post_parse_analyze_hook: "
2866 "hints from table: \"%s\": "
2867 "normalized_query=\"%s\", "
2868 "application name =\"%s\"",
2869 qno, current_hint_str,
2870 normalized_query, application_name),
2871 errhidestmt(msgqno != qno),
2872 errhidecontext(msgqno != qno)));
2874 ereport(pg_hint_plan_debug_message_level,
2875 (errmsg("pg_hint_plan[qno=0x%x]: "
2876 "no match found in table: "
2877 "application name = \"%s\", "
2878 "normalized_query=\"%s\"",
2879 qno, application_name,
2881 errhidestmt(msgqno != qno),
2882 errhidecontext(msgqno != qno)));
2888 /* retrun if we have hint here */
2889 if (current_hint_str)
2893 query_str = get_query_string(pstate, query, NULL);
2898 * get hints from the comment. However we may have the same query
2899 * string with the previous call, but just retrieving hints is expected
2900 * to be faster than checking for identicalness before retrieval.
2902 if (current_hint_str)
2903 pfree((void *)current_hint_str);
2905 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2906 current_hint_str = get_hints_from_comment(query_str);
2907 MemoryContextSwitchTo(oldcontext);
2910 if (debug_level > 1)
2912 if (debug_level == 1 &&
2913 (stmt_name || strcmp(query_str, debug_query_string)))
2914 ereport(pg_hint_plan_debug_message_level,
2915 (errmsg("hints in comment=\"%s\"",
2916 current_hint_str ? current_hint_str : "(none)"),
2917 errhidestmt(msgqno != qno),
2918 errhidecontext(msgqno != qno)));
2920 ereport(pg_hint_plan_debug_message_level,
2921 (errmsg("hints in comment=\"%s\", stmt=\"%s\", query=\"%s\", debug_query_string=\"%s\"",
2922 current_hint_str ? current_hint_str : "(none)",
2923 stmt_name, query_str, debug_query_string),
2924 errhidestmt(msgqno != qno),
2925 errhidecontext(msgqno != qno)));
2931 * Read and set up hint information
2933 static PlannedStmt *
2934 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
2937 PlannedStmt *result;
2941 * Use standard planner if pg_hint_plan is disabled or current nesting
2942 * depth is nesting depth of SPI calls. Other hook functions try to change
2943 * plan with current_hint_state if any, so set it to NULL.
2945 if (!pg_hint_plan_enable_hint || hint_inhibit_level > 0)
2947 if (debug_level > 1)
2948 ereport(pg_hint_plan_debug_message_level,
2949 (errmsg ("pg_hint_plan%s: planner: enable_hint=%d,"
2950 " hint_inhibit_level=%d",
2951 qnostr, pg_hint_plan_enable_hint,
2952 hint_inhibit_level),
2953 errhidestmt(msgqno != qno)));
2956 goto standard_planner_proc;
2960 * Support for nested plpgsql functions. This is quite ugly but this is the
2961 * only point I could find where I can get the query string.
2963 if (plpgsql_recurse_level > 0)
2965 MemoryContext oldcontext;
2967 if (current_hint_str)
2968 pfree((void *)current_hint_str);
2970 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2972 get_hints_from_comment((char *)error_context_stack->arg);
2973 MemoryContextSwitchTo(oldcontext);
2976 if (!current_hint_str)
2977 goto standard_planner_proc;
2979 /* parse the hint into hint state struct */
2980 hstate = create_hintstate(parse, pstrdup(current_hint_str));
2982 /* run standard planner if the statement has not valid hint */
2984 goto standard_planner_proc;
2987 * Push new hint struct to the hint stack to disable previous hint context.
2991 /* Set scan enforcement here. */
2992 save_nestlevel = NewGUCNestLevel();
2994 /* Apply Set hints, then save it as the initial state */
2995 setup_guc_enforcement(current_hint_state->set_hints,
2996 current_hint_state->num_hints[HINT_TYPE_SET],
2997 current_hint_state->context);
2999 current_hint_state->init_scan_mask = get_current_scan_mask();
3000 current_hint_state->init_join_mask = get_current_join_mask();
3001 current_hint_state->init_min_para_size = min_parallel_relation_size;
3002 current_hint_state->init_paratup_cost = parallel_tuple_cost;
3003 current_hint_state->init_parasetup_cost = parallel_setup_cost;
3006 * max_parallel_workers_per_gather should be non-zero here if Workers hint
3009 if (max_hint_nworkers > 0 && max_parallel_workers_per_gather < 1)
3010 set_config_int32_option("max_parallel_workers_per_gather",
3011 1, current_hint_state->context);
3012 current_hint_state->init_nworkers = max_parallel_workers_per_gather;
3014 if (debug_level > 1)
3016 ereport(pg_hint_plan_debug_message_level,
3017 (errhidestmt(msgqno != qno),
3018 errmsg("pg_hint_plan%s: planner", qnostr)));
3023 * Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
3024 * the state when this planner started when error occurred in planner.
3029 result = (*prev_planner) (parse, cursorOptions, boundParams);
3031 result = standard_planner(parse, cursorOptions, boundParams);
3036 * Rollback changes of GUC parameters, and pop current hint context
3037 * from hint stack to rewind the state.
3039 AtEOXact_GUC(true, save_nestlevel);
3045 /* Print hint in debug mode. */
3046 if (debug_level == 1)
3047 HintStateDump(current_hint_state);
3048 else if (debug_level > 1)
3049 HintStateDump2(current_hint_state);
3052 * Rollback changes of GUC parameters, and pop current hint context from
3053 * hint stack to rewind the state.
3055 AtEOXact_GUC(true, save_nestlevel);
3060 standard_planner_proc:
3061 if (debug_level > 1)
3063 ereport(pg_hint_plan_debug_message_level,
3064 (errhidestmt(msgqno != qno),
3065 errmsg("pg_hint_plan%s: planner: no valid hint",
3069 current_hint_state = NULL;
3071 return (*prev_planner) (parse, cursorOptions, boundParams);
3073 return standard_planner(parse, cursorOptions, boundParams);
3077 * Find scan method hint to be applied to the given relation
3080 static ScanMethodHint *
3081 find_scan_hint(PlannerInfo *root, Index relid)
3085 ScanMethodHint *real_name_hint = NULL;
3086 ScanMethodHint *alias_hint = NULL;
3089 /* This should not be a join rel */
3091 rel = root->simple_rel_array[relid];
3094 * This function is called for any RelOptInfo or its inheritance parent if
3095 * any. If we are called from inheritance planner, the RelOptInfo for the
3096 * parent of target child relation is not set in the planner info.
3098 * Otherwise we should check that the reloptinfo is base relation or
3099 * inheritance children.
3102 rel->reloptkind != RELOPT_BASEREL &&
3103 rel->reloptkind != RELOPT_OTHER_MEMBER_REL)
3107 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3109 rte = root->simple_rte_array[relid];
3112 /* We don't hint on other than relation and foreign tables */
3113 if (rte->rtekind != RTE_RELATION ||
3114 rte->relkind == RELKIND_FOREIGN_TABLE)
3117 /* Find scan method hint, which matches given names, from the list. */
3118 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
3120 ScanMethodHint *hint = current_hint_state->scan_hints[i];
3122 /* We ignore disabled hints. */
3123 if (!hint_state_enabled(hint))
3127 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3130 /* check the real name for appendrel children */
3131 if (!real_name_hint &&
3132 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3134 char *realname = get_rel_name(rte->relid);
3136 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3137 real_name_hint = hint;
3140 /* No more match expected, break */
3141 if(alias_hint && real_name_hint)
3145 /* real name match precedes alias match */
3147 return real_name_hint;
3152 static ParallelHint *
3153 find_parallel_hint(PlannerInfo *root, Index relid)
3157 ParallelHint *real_name_hint = NULL;
3158 ParallelHint *alias_hint = NULL;
3161 /* This should not be a join rel */
3163 rel = root->simple_rel_array[relid];
3166 * Parallel planning is appliable only on base relation, which has
3173 * We have set root->glob->parallelModeOK if needed. What we should do here
3174 * is just following the decision of planner.
3176 if (!rel->consider_parallel)
3180 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3182 rte = root->simple_rte_array[relid];
3185 /* Find parallel method hint, which matches given names, from the list. */
3186 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_PARALLEL]; i++)
3188 ParallelHint *hint = current_hint_state->parallel_hints[i];
3190 /* We ignore disabled hints. */
3191 if (!hint_state_enabled(hint))
3195 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3198 /* check the real name for appendrel children */
3199 if (!real_name_hint &&
3200 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3202 char *realname = get_rel_name(rte->relid);
3204 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3205 real_name_hint = hint;
3208 /* No more match expected, break */
3209 if(alias_hint && real_name_hint)
3213 /* real name match precedes alias match */
3215 return real_name_hint;
3223 * Returns TRUE on match, FALSE on no match.
3225 * s1 --- the data to match against
3226 * s2 --- the pattern
3228 * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
3231 regexpeq(const char *s1, const char *s2)
3237 strcpy(name.data, s1);
3238 regexp = cstring_to_text(s2);
3240 result = DirectFunctionCall2Coll(nameregexeq,
3241 DEFAULT_COLLATION_OID,
3242 NameGetDatum(&name),
3243 PointerGetDatum(regexp));
3244 return DatumGetBool(result);
3248 /* Remove indexes instructed not to use by hint. */
3250 restrict_indexes(PlannerInfo *root, ScanMethodHint *hint, RelOptInfo *rel,
3251 bool using_parent_hint)
3257 RangeTblEntry *rte = root->simple_rte_array[rel->relid];
3258 Oid relationObjectId = rte->relid;
3261 * We delete all the IndexOptInfo list and prevent you from being usable by
3264 if (hint->enforce_mask == ENABLE_SEQSCAN ||
3265 hint->enforce_mask == ENABLE_TIDSCAN)
3267 list_free_deep(rel->indexlist);
3268 rel->indexlist = NIL;
3269 hint->base.state = HINT_STATE_USED;
3275 * When a list of indexes is not specified, we just use all indexes.
3277 if (hint->indexnames == NIL)
3281 * Leaving only an specified index, we delete it from a IndexOptInfo list
3285 if (debug_level > 0)
3286 initStringInfo(&buf);
3288 for (cell = list_head(rel->indexlist); cell; cell = next)
3290 IndexOptInfo *info = (IndexOptInfo *) lfirst(cell);
3291 char *indexname = get_rel_name(info->indexoid);
3293 bool use_index = false;
3297 foreach(l, hint->indexnames)
3299 char *hintname = (char *) lfirst(l);
3303 result = regexpeq(indexname, hintname);
3305 result = RelnameCmp(&indexname, &hintname) == 0;
3310 if (debug_level > 0)
3312 appendStringInfoCharMacro(&buf, ' ');
3313 quote_value(&buf, indexname);
3321 * Apply index restriction of parent hint to children. Since index
3322 * inheritance is not explicitly described we should search for an
3323 * children's index with the same definition to that of the parent.
3325 if (using_parent_hint && !use_index)
3327 foreach(l, current_hint_state->parent_index_infos)
3331 ParentIndexInfo *p_info = (ParentIndexInfo *)lfirst(l);
3334 * we check the 'same' index by comparing uniqueness, access
3335 * method and index key columns.
3337 if (p_info->indisunique != info->unique ||
3338 p_info->method != info->relam ||
3339 list_length(p_info->column_names) != info->ncolumns)
3342 /* Check if index key columns match */
3343 for (i = 0; i < info->ncolumns; i++)
3345 char *c_attname = NULL;
3346 char *p_attname = NULL;
3348 p_attname = list_nth(p_info->column_names, i);
3351 * if both of the key of the same position are expressions,
3352 * ignore them for now and check later.
3354 if (info->indexkeys[i] == 0 && !p_attname)
3357 /* deny if one is expression while another is not */
3358 if (info->indexkeys[i] == 0 || !p_attname)
3361 c_attname = get_attname(relationObjectId,
3362 info->indexkeys[i]);
3364 /* deny if any of column attributes don't match */
3365 if (strcmp(p_attname, c_attname) != 0 ||
3366 p_info->indcollation[i] != info->indexcollations[i] ||
3367 p_info->opclass[i] != info->opcintype[i]||
3368 ((p_info->indoption[i] & INDOPTION_DESC) != 0)
3369 != info->reverse_sort[i] ||
3370 ((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0)
3371 != info->nulls_first[i])
3375 /* deny this if any difference found */
3376 if (i != info->ncolumns)
3379 /* check on key expressions */
3380 if ((p_info->expression_str && (info->indexprs != NIL)) ||
3381 (p_info->indpred_str && (info->indpred != NIL)))
3383 /* fetch the index of this child */
3384 ht_idx = SearchSysCache1(INDEXRELID,
3385 ObjectIdGetDatum(info->indexoid));
3387 /* check expressions if both expressions are available */
3388 if (p_info->expression_str &&
3389 !heap_attisnull(ht_idx, Anum_pg_index_indexprs))
3396 * to change the expression's parameter of child's
3399 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3400 Anum_pg_index_indexprs,
3403 result = DirectFunctionCall2(pg_get_expr,
3408 /* deny if expressions don't match */
3409 if (strcmp(p_info->expression_str,
3410 text_to_cstring(DatumGetTextP(result))) != 0)
3413 ReleaseSysCache(ht_idx);
3418 /* compare index predicates */
3419 if (p_info->indpred_str &&
3420 !heap_attisnull(ht_idx, Anum_pg_index_indpred))
3426 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3427 Anum_pg_index_indpred,
3430 result = DirectFunctionCall2(pg_get_expr,
3435 if (strcmp(p_info->indpred_str,
3436 text_to_cstring(DatumGetTextP(result))) != 0)
3439 ReleaseSysCache(ht_idx);
3445 ReleaseSysCache(ht_idx);
3447 else if (p_info->expression_str || (info->indexprs != NIL))
3449 else if (p_info->indpred_str || (info->indpred != NIL))
3454 /* to log the candidate of index */
3455 if (debug_level > 0)
3457 appendStringInfoCharMacro(&buf, ' ');
3458 quote_value(&buf, indexname);
3466 rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
3473 if (debug_level == 1)
3475 StringInfoData rel_buf;
3476 char *disprelname = "";
3479 * If this hint targetted the parent, use the real name of this
3480 * child. Otherwise use hint specification.
3482 if (using_parent_hint)
3483 disprelname = get_rel_name(rte->relid);
3485 disprelname = hint->relname;
3488 initStringInfo(&rel_buf);
3489 quote_value(&rel_buf, disprelname);
3491 ereport(pg_hint_plan_debug_message_level,
3492 (errmsg("available indexes for %s(%s):%s",
3497 pfree(rel_buf.data);
3502 * Return information of index definition.
3504 static ParentIndexInfo *
3505 get_parent_index_info(Oid indexoid, Oid relid)
3507 ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
3508 Relation indexRelation;
3509 Form_pg_index index;
3513 indexRelation = index_open(indexoid, RowExclusiveLock);
3515 index = indexRelation->rd_index;
3517 p_info->indisunique = index->indisunique;
3518 p_info->method = indexRelation->rd_rel->relam;
3520 p_info->column_names = NIL;
3521 p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3522 p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3523 p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
3525 for (i = 0; i < index->indnatts; i++)
3527 attname = get_attname(relid, index->indkey.values[i]);
3528 p_info->column_names = lappend(p_info->column_names, attname);
3530 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
3531 p_info->opclass[i] = indexRelation->rd_opcintype[i];
3532 p_info->indoption[i] = indexRelation->rd_indoption[i];
3536 * to check to match the expression's parameter of index with child indexes
3538 p_info->expression_str = NULL;
3539 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs))
3545 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3546 Anum_pg_index_indexprs, &isnull);
3548 result = DirectFunctionCall2(pg_get_expr,
3550 ObjectIdGetDatum(relid));
3552 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
3556 * to check to match the predicate's parameter of index with child indexes
3558 p_info->indpred_str = NULL;
3559 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred))
3565 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3566 Anum_pg_index_indpred, &isnull);
3568 result = DirectFunctionCall2(pg_get_expr,
3570 ObjectIdGetDatum(relid));
3572 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
3575 index_close(indexRelation, NoLock);
3581 * cancel hint enforcement
3584 reset_hint_enforcement()
3586 setup_scan_method_enforcement(NULL, current_hint_state);
3587 setup_parallel_plan_enforcement(NULL, current_hint_state);
3591 * Set planner guc parameters according to corresponding scan hints. Returns
3592 * bitmap of HintTypeBitmap. If shint or phint is not NULL, set used hint
3593 * there respectively.
3596 setup_hint_enforcement(PlannerInfo *root, RelOptInfo *rel,
3597 ScanMethodHint **rshint, ParallelHint **rphint)
3599 Index new_parent_relid = 0;
3601 ScanMethodHint *shint = NULL;
3602 ParallelHint *phint = NULL;
3603 bool inhparent = root->simple_rte_array[rel->relid]->inh;
3604 Oid relationObjectId = root->simple_rte_array[rel->relid]->relid;
3607 /* reset returns if requested */
3608 if (rshint != NULL) *rshint = NULL;
3609 if (rphint != NULL) *rphint = NULL;
3612 * We could register the parent relation of the following children here
3613 * when inhparent == true but inheritnce planner doesn't call this function
3614 * for parents. Since we cannot distinguish who called this function we
3615 * cannot do other than always seeking the parent regardless of who called
3620 if (debug_level > 1)
3621 ereport(pg_hint_plan_debug_message_level,
3623 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3624 " skipping inh parent: relation=%u(%s), inhparent=%d,"
3625 " current_hint_state=%p, hint_inhibit_level=%d",
3626 qnostr, relationObjectId,
3627 get_rel_name(relationObjectId),
3628 inhparent, current_hint_state, hint_inhibit_level)));
3632 /* Forget about the parent of another subquery */
3633 if (root != current_hint_state->current_root)
3634 current_hint_state->parent_relid = 0;
3636 /* Find the parent for this relation other than the registered parent */
3637 foreach (l, root->append_rel_list)
3639 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3641 if (appinfo->child_relid == rel->relid)
3643 if (current_hint_state->parent_relid != appinfo->parent_relid)
3645 new_parent_relid = appinfo->parent_relid;
3646 current_hint_state->current_root = root;
3654 /* This relation doesn't have a parent. Cancel current_hint_state. */
3655 current_hint_state->parent_relid = 0;
3656 current_hint_state->parent_scan_hint = NULL;
3657 current_hint_state->parent_parallel_hint = NULL;
3660 if (new_parent_relid > 0)
3663 * Here we found a new parent for the current relation. Scan continues
3664 * hint to other childrens of this parent so remember it * to avoid
3665 * hinthintredundant setup cost.
3667 current_hint_state->parent_relid = new_parent_relid;
3669 /* Find hints for the parent */
3670 current_hint_state->parent_scan_hint =
3671 find_scan_hint(root, current_hint_state->parent_relid);
3673 current_hint_state->parent_parallel_hint =
3674 find_parallel_hint(root, current_hint_state->parent_relid);
3677 * If hint is found for the parent, apply it for this child instead
3680 if (current_hint_state->parent_scan_hint)
3682 ScanMethodHint * pshint = current_hint_state->parent_scan_hint;
3684 pshint->base.state = HINT_STATE_USED;
3686 /* Apply index mask in the same manner to the parent. */
3687 if (pshint->indexnames)
3690 Relation parent_rel;
3693 root->simple_rte_array[current_hint_state->parent_relid]->relid;
3694 parent_rel = heap_open(parentrel_oid, NoLock);
3696 /* Search the parent relation for indexes match the hint spec */
3697 foreach(l, RelationGetIndexList(parent_rel))
3699 Oid indexoid = lfirst_oid(l);
3700 char *indexname = get_rel_name(indexoid);
3702 ParentIndexInfo *parent_index_info;
3704 foreach(lc, pshint->indexnames)
3706 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
3713 get_parent_index_info(indexoid, parentrel_oid);
3714 current_hint_state->parent_index_infos =
3715 lappend(current_hint_state->parent_index_infos,
3718 heap_close(parent_rel, NoLock);
3723 shint = find_scan_hint(root, rel->relid);
3725 shint = current_hint_state->parent_scan_hint;
3729 bool using_parent_hint =
3730 (shint == current_hint_state->parent_scan_hint);
3732 ret |= HINT_BM_SCAN_METHOD;
3734 /* Setup scan enforcement environment */
3735 setup_scan_method_enforcement(shint, current_hint_state);
3737 /* restrict unwanted inexes */
3738 restrict_indexes(root, shint, rel, using_parent_hint);
3740 if (debug_level > 1)
3742 char *additional_message = "";
3744 if (shint == current_hint_state->parent_scan_hint)
3745 additional_message = " by parent hint";
3747 ereport(pg_hint_plan_debug_message_level,
3749 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3750 " index deletion%s:"
3751 " relation=%u(%s), inhparent=%d, "
3752 "current_hint_state=%p,"
3753 " hint_inhibit_level=%d, scanmask=0x%x",
3754 qnostr, additional_message,
3756 get_rel_name(relationObjectId),
3757 inhparent, current_hint_state,
3759 shint->enforce_mask)));
3763 /* Do the same for parallel plan enforcement */
3764 phint = find_parallel_hint(root, rel->relid);
3766 phint = current_hint_state->parent_parallel_hint;
3768 setup_parallel_plan_enforcement(phint, current_hint_state);
3771 ret |= HINT_BM_PARALLEL;
3773 /* Nothing to apply. Reset the scan mask to intial state */
3774 if (!shint && ! phint)
3776 if (debug_level > 1)
3777 ereport(pg_hint_plan_debug_message_level,
3778 (errhidestmt (true),
3779 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3781 " relation=%u(%s), inhparent=%d, current_hint=%p,"
3782 " hint_inhibit_level=%d, scanmask=0x%x",
3783 qnostr, relationObjectId,
3784 get_rel_name(relationObjectId),
3785 inhparent, current_hint_state, hint_inhibit_level,
3786 current_hint_state->init_scan_mask)));
3788 setup_scan_method_enforcement(NULL, current_hint_state);
3793 if (rshint != NULL) *rshint = shint;
3794 if (rphint != NULL) *rphint = phint;
3800 * Return index of relation which matches given aliasname, or 0 if not found.
3801 * If same aliasname was used multiple times in a query, return -1.
3804 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
3810 for (i = 1; i < root->simple_rel_array_size; i++)
3814 if (root->simple_rel_array[i] == NULL)
3817 Assert(i == root->simple_rel_array[i]->relid);
3819 if (RelnameCmp(&aliasname,
3820 &root->simple_rte_array[i]->eref->aliasname) != 0)
3823 foreach(l, initial_rels)
3825 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3827 if (rel->reloptkind == RELOPT_BASEREL)
3829 if (rel->relid != i)
3834 Assert(rel->reloptkind == RELOPT_JOINREL);
3836 if (!bms_is_member(i, rel->relids))
3843 ("Relation name \"%s\" is ambiguous.",
3858 * Return join hint which matches given joinrelids.
3860 static JoinMethodHint *
3861 find_join_hint(Relids joinrelids)
3866 join_hint = current_hint_state->join_hint_level[bms_num_members(joinrelids)];
3868 foreach(l, join_hint)
3870 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
3872 if (bms_equal(joinrelids, hint->joinrelids))
3880 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
3881 PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
3883 OuterInnerRels *outer_rels;
3884 OuterInnerRels *inner_rels;
3885 Relids outer_relids;
3886 Relids inner_relids;
3888 JoinMethodHint *hint;
3890 if (outer_inner->relation != NULL)
3892 return bms_make_singleton(
3893 find_relid_aliasname(root, outer_inner->relation,
3895 leading_hint->base.hint_str));
3898 outer_rels = lfirst(outer_inner->outer_inner_pair->head);
3899 inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
3901 outer_relids = OuterInnerJoinCreate(outer_rels,
3907 inner_relids = OuterInnerJoinCreate(inner_rels,
3914 join_relids = bms_add_members(outer_relids, inner_relids);
3916 if (bms_num_members(join_relids) > nbaserel)
3920 * If we don't have join method hint, create new one for the
3921 * join combination with all join methods are enabled.
3923 hint = find_join_hint(join_relids);
3927 * Here relnames is not set, since Relids bitmap is sufficient to
3928 * control paths of this query afterward.
3930 hint = (JoinMethodHint *) JoinMethodHintCreate(
3931 leading_hint->base.hint_str,
3933 HINT_KEYWORD_LEADING);
3934 hint->base.state = HINT_STATE_USED;
3935 hint->nrels = bms_num_members(join_relids);
3936 hint->enforce_mask = ENABLE_ALL_JOIN;
3937 hint->joinrelids = bms_copy(join_relids);
3938 hint->inner_nrels = bms_num_members(inner_relids);
3939 hint->inner_joinrelids = bms_copy(inner_relids);
3941 hstate->join_hint_level[hint->nrels] =
3942 lappend(hstate->join_hint_level[hint->nrels], hint);
3946 hint->inner_nrels = bms_num_members(inner_relids);
3947 hint->inner_joinrelids = bms_copy(inner_relids);
3954 create_bms_of_relids(Hint *base, PlannerInfo *root, List *initial_rels,
3955 int nrels, char **relnames)
3958 Relids relids = NULL;
3962 for (j = 0; j < nrels; j++)
3964 relname = relnames[j];
3966 relid = find_relid_aliasname(root, relname, initial_rels,
3970 base->state = HINT_STATE_ERROR;
3973 * the aliasname is not found(relid == 0) or same aliasname was used
3974 * multiple times in a query(relid == -1)
3981 if (bms_is_member(relid, relids))
3983 hint_ereport(base->hint_str,
3984 ("Relation name \"%s\" is duplicated.", relname));
3985 base->state = HINT_STATE_ERROR;
3989 relids = bms_add_member(relids, relid);
3994 * Transform join method hint into handy form.
3996 * - create bitmap of relids from alias names, to make it easier to check
3997 * whether a join path matches a join method hint.
3998 * - add join method hints which are necessary to enforce join order
3999 * specified by Leading hint
4002 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
4003 List *initial_rels, JoinMethodHint **join_method_hints)
4011 LeadingHint *lhint = NULL;
4014 * Create bitmap of relids from alias names for each join method hint.
4015 * Bitmaps are more handy than strings in join searching.
4017 for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
4019 JoinMethodHint *hint = hstate->join_hints[i];
4021 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4024 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4025 initial_rels, hint->nrels, hint->relnames);
4027 if (hint->joinrelids == NULL || hint->base.state == HINT_STATE_ERROR)
4030 hstate->join_hint_level[hint->nrels] =
4031 lappend(hstate->join_hint_level[hint->nrels], hint);
4035 * Create bitmap of relids from alias names for each rows hint.
4036 * Bitmaps are more handy than strings in join searching.
4038 for (i = 0; i < hstate->num_hints[HINT_TYPE_ROWS]; i++)
4040 RowsHint *hint = hstate->rows_hints[i];
4042 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4045 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4046 initial_rels, hint->nrels, hint->relnames);
4049 /* Do nothing if no Leading hint was supplied. */
4050 if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
4054 * Decide whether to use Leading hint
4056 for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
4058 LeadingHint *leading_hint = (LeadingHint *)hstate->leading_hint[i];
4061 if (leading_hint->base.state == HINT_STATE_ERROR)
4067 foreach(l, leading_hint->relations)
4069 relname = (char *)lfirst(l);;
4071 relid = find_relid_aliasname(root, relname, initial_rels,
4072 leading_hint->base.hint_str);
4074 leading_hint->base.state = HINT_STATE_ERROR;
4079 if (bms_is_member(relid, relids))
4081 hint_ereport(leading_hint->base.hint_str,
4082 ("Relation name \"%s\" is duplicated.", relname));
4083 leading_hint->base.state = HINT_STATE_ERROR;
4087 relids = bms_add_member(relids, relid);
4090 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
4095 hint_ereport(lhint->base.hint_str,
4096 ("Conflict %s hint.", HintTypeName[lhint->base.type]));
4097 lhint->base.state = HINT_STATE_DUPLICATION;
4099 leading_hint->base.state = HINT_STATE_USED;
4100 lhint = leading_hint;
4103 /* check to exist Leading hint marked with 'used'. */
4108 * We need join method hints which fit specified join order in every join
4109 * level. For example, Leading(A B C) virtually requires following join
4110 * method hints, if no join method hint supplied:
4112 * - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
4113 * - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
4115 * If we already have join method hint which fits specified join order in
4116 * that join level, we leave it as-is and don't add new hints.
4120 if (lhint->outer_inner == NULL)
4122 foreach(l, lhint->relations)
4124 JoinMethodHint *hint;
4126 relname = (char *)lfirst(l);
4129 * Find relid of the relation which has given name. If we have the
4130 * name given in Leading hint multiple times in the join, nothing to
4133 relid = find_relid_aliasname(root, relname, initial_rels,
4136 /* Create bitmap of relids for current join level. */
4137 joinrelids = bms_add_member(joinrelids, relid);
4140 /* We never have join method hint for single relation. */
4145 * If we don't have join method hint, create new one for the
4146 * join combination with all join methods are enabled.
4148 hint = find_join_hint(joinrelids);
4152 * Here relnames is not set, since Relids bitmap is sufficient
4153 * to control paths of this query afterward.
4155 hint = (JoinMethodHint *) JoinMethodHintCreate(
4156 lhint->base.hint_str,
4158 HINT_KEYWORD_LEADING);
4159 hint->base.state = HINT_STATE_USED;
4160 hint->nrels = njoinrels;
4161 hint->enforce_mask = ENABLE_ALL_JOIN;
4162 hint->joinrelids = bms_copy(joinrelids);
4165 join_method_hints[njoinrels] = hint;
4167 if (njoinrels >= nbaserel)
4170 bms_free(joinrelids);
4176 * Delete all join hints which have different combination from Leading
4179 for (i = 2; i <= njoinrels; i++)
4181 list_free(hstate->join_hint_level[i]);
4183 hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
4188 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
4195 njoinrels = bms_num_members(joinrelids);
4196 Assert(njoinrels >= 2);
4199 * Delete all join hints which have different combination from Leading
4202 for (i = 2;i <= njoinrels; i++)
4204 if (hstate->join_hint_level[i] != NIL)
4206 ListCell *prev = NULL;
4207 ListCell *next = NULL;
4208 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
4211 JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
4215 if (hint->inner_nrels == 0 &&
4216 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
4217 bms_equal(bms_union(hint->joinrelids, joinrelids),
4220 hstate->join_hint_level[i] =
4221 list_delete_cell(hstate->join_hint_level[i], l,
4230 bms_free(joinrelids);
4233 if (hint_state_enabled(lhint))
4235 set_join_config_options(DISABLE_ALL_JOIN, current_hint_state->context);
4242 * wrapper of make_join_rel()
4244 * call make_join_rel() after changing enable_* parameters according to given
4248 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
4251 JoinMethodHint *hint;
4255 joinrelids = bms_union(rel1->relids, rel2->relids);
4256 hint = find_join_hint(joinrelids);
4257 bms_free(joinrelids);
4260 return pg_hint_plan_make_join_rel(root, rel1, rel2);
4262 if (hint->inner_nrels == 0)
4264 save_nestlevel = NewGUCNestLevel();
4266 set_join_config_options(hint->enforce_mask,
4267 current_hint_state->context);
4269 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4270 hint->base.state = HINT_STATE_USED;
4273 * Restore the GUC variables we set above.
4275 AtEOXact_GUC(true, save_nestlevel);
4278 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4287 add_paths_to_joinrel_wrapper(PlannerInfo *root,
4288 RelOptInfo *joinrel,
4289 RelOptInfo *outerrel,
4290 RelOptInfo *innerrel,
4292 SpecialJoinInfo *sjinfo,
4296 JoinMethodHint *join_hint;
4299 joinrelids = bms_union(outerrel->relids, innerrel->relids);
4300 join_hint = find_join_hint(joinrelids);
4301 bms_free(joinrelids);
4303 if (join_hint && join_hint->inner_nrels != 0)
4305 save_nestlevel = NewGUCNestLevel();
4307 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
4310 set_join_config_options(join_hint->enforce_mask,
4311 current_hint_state->context);
4313 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4314 sjinfo, restrictlist);
4315 join_hint->base.state = HINT_STATE_USED;
4319 set_join_config_options(DISABLE_ALL_JOIN,
4320 current_hint_state->context);
4321 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4322 sjinfo, restrictlist);
4326 * Restore the GUC variables we set above.
4328 AtEOXact_GUC(true, save_nestlevel);
4331 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4332 sjinfo, restrictlist);
4336 get_num_baserels(List *initial_rels)
4341 foreach(l, initial_rels)
4343 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
4345 if (rel->reloptkind == RELOPT_BASEREL)
4347 else if (rel->reloptkind ==RELOPT_JOINREL)
4348 nbaserel+= bms_num_members(rel->relids);
4351 /* other values not expected here */
4352 elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
4360 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
4363 JoinMethodHint **join_method_hints;
4367 bool leading_hint_enable;
4370 * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
4371 * valid hint is supplied or current nesting depth is nesting depth of SPI
4374 if (!current_hint_state || hint_inhibit_level > 0)
4376 if (prev_join_search)
4377 return (*prev_join_search) (root, levels_needed, initial_rels);
4378 else if (enable_geqo && levels_needed >= geqo_threshold)
4379 return geqo(root, levels_needed, initial_rels);
4381 return standard_join_search(root, levels_needed, initial_rels);
4385 * In the case using GEQO, only scan method hints and Set hints have
4386 * effect. Join method and join order is not controllable by hints.
4388 if (enable_geqo && levels_needed >= geqo_threshold)
4389 return geqo(root, levels_needed, initial_rels);
4391 nbaserel = get_num_baserels(initial_rels);
4392 current_hint_state->join_hint_level =
4393 palloc0(sizeof(List *) * (nbaserel + 1));
4394 join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
4396 leading_hint_enable = transform_join_hints(current_hint_state,
4398 initial_rels, join_method_hints);
4400 rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
4402 for (i = 2; i <= nbaserel; i++)
4404 list_free(current_hint_state->join_hint_level[i]);
4406 /* free Leading hint only */
4407 if (join_method_hints[i] != NULL &&
4408 join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
4409 JoinMethodHintDelete(join_method_hints[i]);
4411 pfree(current_hint_state->join_hint_level);
4412 pfree(join_method_hints);
4414 if (leading_hint_enable)
4415 set_join_config_options(current_hint_state->init_join_mask,
4416 current_hint_state->context);
4422 * Force number of wokers if instructed by hint
4425 pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
4426 Index rti, RangeTblEntry *rte)
4428 ParallelHint *phint;
4432 /* call the previous hook */
4433 if (prev_set_rel_pathlist)
4434 prev_set_rel_pathlist(root, rel, rti, rte);
4436 /* Nothing to do if no hint available */
4437 if (current_hint_state == NULL)
4440 /* Don't touch dummy rels. */
4441 if (IS_DUMMY_REL(rel))
4445 * We can accept only plain relations, foreign tables and table saples are
4446 * also unacceptable. See set_rel_pathlist.
4448 if (rel->rtekind != RTE_RELATION ||
4449 rte->relkind == RELKIND_FOREIGN_TABLE ||
4450 rte->tablesample != NULL)
4453 /* We cannot handle if this requires an outer */
4454 if (rel->lateral_relids)
4457 /* Return if this relation gets no enfocement */
4458 if ((found_hints = setup_hint_enforcement(root, rel, NULL, &phint)) == 0)
4461 /* Here, we regenerate paths with the current hint restriction */
4463 if (found_hints & HINT_BM_SCAN_METHOD)
4466 * With scan hints, we regenerate paths for this relation from the
4467 * first under the restricion.
4469 list_free_deep(rel->pathlist);
4470 rel->pathlist = NIL;
4472 set_plain_rel_pathlist(root, rel, rte);
4475 if (found_hints & HINT_BM_PARALLEL)
4479 /* the partial_pathlist may be for different parameters, discard it */
4480 if (rel->partial_pathlist)
4482 list_free_deep(rel->partial_pathlist);
4483 rel->partial_pathlist = NIL;
4486 /* also remove gather path */
4489 ListCell *cell, *prev = NULL, *next;
4491 for (cell = list_head(rel->pathlist) ; cell; cell = next)
4493 Path *path = (Path *) lfirst(cell);
4496 if (IsA(path, GatherPath))
4497 rel->pathlist = list_delete_cell(rel->pathlist,
4504 /* then generate new paths if needed */
4505 if (phint->nworkers > 0)
4507 /* Lower the priorities of non-parallel paths */
4508 foreach (l, rel->pathlist)
4510 Path *path = (Path *) lfirst(l);
4512 if (path->startup_cost < disable_cost)
4514 path->startup_cost += disable_cost;
4515 path->total_cost += disable_cost;
4520 * generate partial paths with enforcement, this is affected by
4521 * scan method enforcement. Specifically, the cost of this partial
4522 * seqscan path will be disabled_cost if seqscan is inhibited by
4523 * hint or GUC parameters.
4525 Assert (rel->partial_pathlist == NIL);
4526 create_plain_partial_paths(root, rel);
4528 /* enforce number of workers if requested */
4529 if (phint->force_parallel)
4531 foreach (l, rel->partial_pathlist)
4533 Path *ppath = (Path *) lfirst(l);
4535 ppath->parallel_workers = phint->nworkers;
4539 /* Generate gather paths for base rels */
4540 if (rel->reloptkind == RELOPT_BASEREL)
4541 generate_gather_paths(root, rel);
4545 reset_hint_enforcement();
4550 * Build access paths for a base relation
4552 * This function was copied and edited from set_rel_pathlist() in
4553 * src/backend/optimizer/path/allpaths.c in order not to copy other static
4554 * functions not required here.
4557 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
4558 Index rti, RangeTblEntry *rte)
4560 if (IS_DUMMY_REL(rel))
4562 /* We already proved the relation empty, so nothing more to do */
4566 /* It's an "append relation", process accordingly */
4567 set_append_rel_pathlist(root, rel, rti, rte);
4571 if (rel->rtekind == RTE_RELATION)
4573 if (rte->relkind == RELKIND_RELATION)
4575 if(rte->tablesample != NULL)
4576 elog(ERROR, "sampled relation is not supported");
4578 /* Plain relation */
4579 set_plain_rel_pathlist(root, rel, rte);
4582 elog(ERROR, "unexpected relkind: %c", rte->relkind);
4585 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
4589 * Allow a plugin to editorialize on the set of Paths for this base
4590 * relation. It could add new paths (such as CustomPaths) by calling
4591 * add_path(), or delete or modify paths added by the core code.
4593 if (set_rel_pathlist_hook)
4594 (*set_rel_pathlist_hook) (root, rel, rti, rte);
4596 /* Now find the cheapest of the paths for this rel */
4601 * stmt_beg callback is called when each query in PL/pgSQL function is about
4602 * to be executed. At that timing, we save query string in the global variable
4603 * plpgsql_query_string to use it in planner hook. It's safe to use one global
4604 * variable for the purpose, because its content is only necessary until
4605 * planner hook is called for the query, so recursive PL/pgSQL function calls
4606 * don't harm this mechanism.
4609 pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4611 plpgsql_recurse_level++;
4615 * stmt_end callback is called then each query in PL/pgSQL function has
4616 * finished. At that timing, we clear plpgsql_query_string to tell planner
4617 * hook that next call is not for a query written in PL/pgSQL block.
4620 pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4622 plpgsql_recurse_level--;
4625 void plpgsql_query_erase_callback(ResourceReleasePhase phase,
4630 if (!isTopLevel || phase != RESOURCE_RELEASE_AFTER_LOCKS)
4632 /* Cancel plpgsql nest level*/
4633 plpgsql_recurse_level = 0;
4636 #define standard_join_search pg_hint_plan_standard_join_search
4637 #define join_search_one_level pg_hint_plan_join_search_one_level
4638 #define make_join_rel make_join_rel_wrapper
4641 #undef make_join_rel
4642 #define make_join_rel pg_hint_plan_make_join_rel
4643 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
4644 #include "make_join_rel.c"
4646 #include "pg_stat_statements.c"