1 /*-------------------------------------------------------------------------
4 * hinting on how to execute a query for PostgreSQL
6 * Copyright (c) 2012-2017, 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 "nodes/relation.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/geqo.h"
24 #include "optimizer/joininfo.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/plancat.h"
28 #include "optimizer/planner.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/restrictinfo.h"
31 #include "parser/analyze.h"
32 #include "parser/parsetree.h"
33 #include "parser/scansup.h"
34 #include "tcop/utility.h"
35 #include "utils/builtins.h"
36 #include "utils/lsyscache.h"
37 #include "utils/memutils.h"
38 #include "utils/rel.h"
39 #include "utils/snapmgr.h"
40 #include "utils/syscache.h"
41 #include "utils/resowner.h"
43 #include "catalog/pg_class.h"
45 #include "executor/spi.h"
46 #include "catalog/pg_type.h"
50 /* partially copied from pg_stat_statements */
51 #include "normalize_query.h"
54 #include "access/htup_details.h"
56 #ifdef PG_MODULE_MAGIC
60 #define BLOCK_COMMENT_START "/*"
61 #define BLOCK_COMMENT_END "*/"
62 #define HINT_COMMENT_KEYWORD "+"
63 #define HINT_START BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
64 #define HINT_END BLOCK_COMMENT_END
67 #define HINT_SEQSCAN "SeqScan"
68 #define HINT_INDEXSCAN "IndexScan"
69 #define HINT_INDEXSCANREGEXP "IndexScanRegexp"
70 #define HINT_BITMAPSCAN "BitmapScan"
71 #define HINT_BITMAPSCANREGEXP "BitmapScanRegexp"
72 #define HINT_TIDSCAN "TidScan"
73 #define HINT_NOSEQSCAN "NoSeqScan"
74 #define HINT_NOINDEXSCAN "NoIndexScan"
75 #define HINT_NOBITMAPSCAN "NoBitmapScan"
76 #define HINT_NOTIDSCAN "NoTidScan"
77 #define HINT_INDEXONLYSCAN "IndexOnlyScan"
78 #define HINT_INDEXONLYSCANREGEXP "IndexOnlyScanRegexp"
79 #define HINT_NOINDEXONLYSCAN "NoIndexOnlyScan"
80 #define HINT_PARALLEL "Parallel"
82 #define HINT_NESTLOOP "NestLoop"
83 #define HINT_MERGEJOIN "MergeJoin"
84 #define HINT_HASHJOIN "HashJoin"
85 #define HINT_NONESTLOOP "NoNestLoop"
86 #define HINT_NOMERGEJOIN "NoMergeJoin"
87 #define HINT_NOHASHJOIN "NoHashJoin"
88 #define HINT_LEADING "Leading"
89 #define HINT_SET "Set"
90 #define HINT_ROWS "Rows"
92 #define HINT_ARRAY_DEFAULT_INITSIZE 8
94 #define hint_ereport(str, detail) hint_parse_ereport(str, detail)
95 #define hint_parse_ereport(str, detail) \
97 ereport(pg_hint_plan_parse_message_level, \
98 (errmsg("pg_hint_plan: hint syntax error at or near \"%s\"", (str)), \
102 #define skip_space(str) \
103 while (isspace(*str)) \
108 ENABLE_SEQSCAN = 0x01,
109 ENABLE_INDEXSCAN = 0x02,
110 ENABLE_BITMAPSCAN = 0x04,
111 ENABLE_TIDSCAN = 0x08,
112 ENABLE_INDEXONLYSCAN = 0x10
117 ENABLE_NESTLOOP = 0x01,
118 ENABLE_MERGEJOIN = 0x02,
119 ENABLE_HASHJOIN = 0x04
122 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
123 ENABLE_BITMAPSCAN | ENABLE_TIDSCAN | \
124 ENABLE_INDEXONLYSCAN)
125 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
126 #define DISABLE_ALL_SCAN 0
127 #define DISABLE_ALL_JOIN 0
129 /* hint keyword of enum type*/
130 typedef enum HintKeyword
132 HINT_KEYWORD_SEQSCAN,
133 HINT_KEYWORD_INDEXSCAN,
134 HINT_KEYWORD_INDEXSCANREGEXP,
135 HINT_KEYWORD_BITMAPSCAN,
136 HINT_KEYWORD_BITMAPSCANREGEXP,
137 HINT_KEYWORD_TIDSCAN,
138 HINT_KEYWORD_NOSEQSCAN,
139 HINT_KEYWORD_NOINDEXSCAN,
140 HINT_KEYWORD_NOBITMAPSCAN,
141 HINT_KEYWORD_NOTIDSCAN,
142 HINT_KEYWORD_INDEXONLYSCAN,
143 HINT_KEYWORD_INDEXONLYSCANREGEXP,
144 HINT_KEYWORD_NOINDEXONLYSCAN,
146 HINT_KEYWORD_NESTLOOP,
147 HINT_KEYWORD_MERGEJOIN,
148 HINT_KEYWORD_HASHJOIN,
149 HINT_KEYWORD_NONESTLOOP,
150 HINT_KEYWORD_NOMERGEJOIN,
151 HINT_KEYWORD_NOHASHJOIN,
153 HINT_KEYWORD_LEADING,
156 HINT_KEYWORD_PARALLEL,
158 HINT_KEYWORD_UNRECOGNIZED
161 #define SCAN_HINT_ACCEPTS_INDEX_NAMES(kw) \
162 (kw == HINT_KEYWORD_INDEXSCAN || \
163 kw == HINT_KEYWORD_INDEXSCANREGEXP || \
164 kw == HINT_KEYWORD_INDEXONLYSCAN || \
165 kw == HINT_KEYWORD_INDEXONLYSCANREGEXP || \
166 kw == HINT_KEYWORD_BITMAPSCAN || \
167 kw == HINT_KEYWORD_BITMAPSCANREGEXP)
170 typedef struct Hint Hint;
171 typedef struct HintState HintState;
173 typedef Hint *(*HintCreateFunction) (const char *hint_str,
175 HintKeyword hint_keyword);
176 typedef void (*HintDeleteFunction) (Hint *hint);
177 typedef void (*HintDescFunction) (Hint *hint, StringInfo buf, bool nolf);
178 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
179 typedef const char *(*HintParseFunction) (Hint *hint, HintState *hstate,
180 Query *parse, const char *str);
183 #define NUM_HINT_TYPE 6
184 typedef enum HintType
186 HINT_TYPE_SCAN_METHOD,
187 HINT_TYPE_JOIN_METHOD,
194 typedef enum HintTypeBitmap
196 HINT_BM_SCAN_METHOD = 1,
200 static const char *HintTypeName[] = {
210 typedef enum HintStatus
212 HINT_STATE_NOTUSED = 0, /* specified relation not used in query */
213 HINT_STATE_USED, /* hint is used */
214 HINT_STATE_DUPLICATION, /* specified hint duplication */
215 HINT_STATE_ERROR /* execute error (parse error does not include
219 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
220 (hint)->base.state == HINT_STATE_USED)
222 static unsigned int qno = 0;
223 static unsigned int msgqno = 0;
224 static char qnostr[32];
225 static const char *current_hint_str = NULL;
227 /* common data for all hints. */
230 const char *hint_str; /* must not do pfree */
231 const char *keyword; /* must not do pfree */
232 HintKeyword hint_keyword;
235 HintDeleteFunction delete_func;
236 HintDescFunction desc_func;
237 HintCmpFunction cmp_func;
238 HintParseFunction parse_func;
241 /* scan method hints */
242 typedef struct ScanMethodHint
248 unsigned char enforce_mask;
251 typedef struct ParentIndexInfo
256 char *expression_str;
263 /* join method hints */
264 typedef struct JoinMethodHint
270 unsigned char enforce_mask;
272 Relids inner_joinrelids;
275 /* join order hints */
276 typedef struct OuterInnerRels
279 List *outer_inner_pair;
282 typedef struct LeadingHint
285 List *relations; /* relation names specified in Leading hint */
286 OuterInnerRels *outer_inner;
289 /* change a run-time parameter hints */
290 typedef struct SetHint
293 char *name; /* name of variable */
299 typedef enum RowsValueType {
300 RVT_ABSOLUTE, /* Rows(... #1000) */
301 RVT_ADD, /* Rows(... +1000) */
302 RVT_SUB, /* Rows(... -1000) */
303 RVT_MULTI, /* Rows(... *1.2) */
305 typedef struct RowsHint
312 Relids inner_joinrelids;
314 RowsValueType value_type;
319 typedef struct ParallelHint
323 char *nworkers_str; /* original string of nworkers */
324 int nworkers; /* num of workers specified by Worker */
325 bool force_parallel; /* force parallel scan */
329 * Describes a context of hint processing.
333 char *hint_str; /* original hint string */
336 int nall_hints; /* # of valid all hints */
337 int max_all_hints; /* # of slots for all hints */
338 Hint **all_hints; /* parsed all hints */
340 /* # of each hints */
341 int num_hints[NUM_HINT_TYPE];
343 /* for scan method hints */
344 ScanMethodHint **scan_hints; /* parsed scan hints */
346 /* Initial values of parameters */
347 int init_scan_mask; /* enable_* mask */
348 int init_nworkers; /* max_parallel_workers_per_gather */
349 /* min_parallel_table_scan_size*/
350 int init_min_para_tablescan_size;
351 /* min_parallel_index_scan_size*/
352 int init_min_para_indexscan_size;
353 int init_paratup_cost; /* parallel_tuple_cost */
354 int init_parasetup_cost;/* parallel_setup_cost */
356 PlannerInfo *current_root; /* PlannerInfo for the followings */
357 Index parent_relid; /* inherit parent of table relid */
358 ScanMethodHint *parent_scan_hint; /* scan hint for the parent */
359 ParallelHint *parent_parallel_hint; /* parallel hint for the parent */
360 List *parent_index_infos; /* list of parent table's index */
362 JoinMethodHint **join_hints; /* parsed join hints */
363 int init_join_mask; /* initial value join parameter */
364 List **join_hint_level;
365 LeadingHint **leading_hint; /* parsed Leading hints */
366 SetHint **set_hints; /* parsed Set hints */
367 GucContext context; /* which GUC parameters can we set? */
368 RowsHint **rows_hints; /* parsed Rows hints */
369 ParallelHint **parallel_hints; /* parsed Parallel hints */
373 * Describes a hint parser module which is bound with particular hint keyword.
375 typedef struct HintParser
378 HintCreateFunction create_func;
379 HintKeyword hint_keyword;
382 /* Module callbacks */
386 static void push_hint(HintState *hstate);
387 static void pop_hint(void);
389 static void pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query);
390 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
391 ParamListInfo boundParams);
392 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
396 /* Scan method hint callbacks */
397 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword,
398 HintKeyword hint_keyword);
399 static void ScanMethodHintDelete(ScanMethodHint *hint);
400 static void ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf);
401 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
402 static const char *ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate,
403 Query *parse, const char *str);
405 /* Join method hint callbacks */
406 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword,
407 HintKeyword hint_keyword);
408 static void JoinMethodHintDelete(JoinMethodHint *hint);
409 static void JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf);
410 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
411 static const char *JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate,
412 Query *parse, const char *str);
414 /* Leading hint callbacks */
415 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword,
416 HintKeyword hint_keyword);
417 static void LeadingHintDelete(LeadingHint *hint);
418 static void LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf);
419 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
420 static const char *LeadingHintParse(LeadingHint *hint, HintState *hstate,
421 Query *parse, const char *str);
423 /* Set hint callbacks */
424 static Hint *SetHintCreate(const char *hint_str, const char *keyword,
425 HintKeyword hint_keyword);
426 static void SetHintDelete(SetHint *hint);
427 static void SetHintDesc(SetHint *hint, StringInfo buf, bool nolf);
428 static int SetHintCmp(const SetHint *a, const SetHint *b);
429 static const char *SetHintParse(SetHint *hint, HintState *hstate, Query *parse,
432 /* Rows hint callbacks */
433 static Hint *RowsHintCreate(const char *hint_str, const char *keyword,
434 HintKeyword hint_keyword);
435 static void RowsHintDelete(RowsHint *hint);
436 static void RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf);
437 static int RowsHintCmp(const RowsHint *a, const RowsHint *b);
438 static const char *RowsHintParse(RowsHint *hint, HintState *hstate,
439 Query *parse, const char *str);
441 /* Parallel hint callbacks */
442 static Hint *ParallelHintCreate(const char *hint_str, const char *keyword,
443 HintKeyword hint_keyword);
444 static void ParallelHintDelete(ParallelHint *hint);
445 static void ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf);
446 static int ParallelHintCmp(const ParallelHint *a, const ParallelHint *b);
447 static const char *ParallelHintParse(ParallelHint *hint, HintState *hstate,
448 Query *parse, const char *str);
450 static void quote_value(StringInfo buf, const char *value);
452 static const char *parse_quoted_value(const char *str, char **word,
455 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
458 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
459 void pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
460 Index rti, RangeTblEntry *rte);
461 static void create_plain_partial_paths(PlannerInfo *root,
463 static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
464 List *live_childrels);
465 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
466 ListCell *other_rels);
467 static void make_rels_by_clauseless_joins(PlannerInfo *root,
469 ListCell *other_rels);
470 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
471 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
473 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
474 Index rti, RangeTblEntry *rte);
475 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
476 List *live_childrels,
477 List *all_child_pathkeys,
478 List *partitioned_rels);
479 static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
481 Relids required_outer);
482 static List *accumulate_append_subpath(List *subpaths, Path *path);
483 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
486 static void pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
488 static void pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate,
490 static void plpgsql_query_erase_callback(ResourceReleasePhase phase,
494 static int set_config_option_noerror(const char *name, const char *value,
495 GucContext context, GucSource source,
496 GucAction action, bool changeVal, int elevel);
497 static void setup_scan_method_enforcement(ScanMethodHint *scanhint,
499 static int set_config_int32_option(const char *name, int32 value,
503 static bool pg_hint_plan_enable_hint = true;
504 static int debug_level = 0;
505 static int pg_hint_plan_parse_message_level = INFO;
506 static int pg_hint_plan_debug_message_level = LOG;
507 /* Default is off, to keep backward compatibility. */
508 static bool pg_hint_plan_enable_hint_table = false;
510 static int plpgsql_recurse_level = 0; /* PLpgSQL recursion level */
511 static int hint_inhibit_level = 0; /* Inhibit hinting if this is above 0 */
512 /* (This could not be above 1) */
513 static int max_hint_nworkers = -1; /* Maximum nworkers of Workers hints */
515 static const struct config_enum_entry parse_messages_level_options[] = {
516 {"debug", DEBUG2, true},
517 {"debug5", DEBUG5, false},
518 {"debug4", DEBUG4, false},
519 {"debug3", DEBUG3, false},
520 {"debug2", DEBUG2, false},
521 {"debug1", DEBUG1, false},
523 {"info", INFO, false},
524 {"notice", NOTICE, false},
525 {"warning", WARNING, false},
526 {"error", ERROR, false},
528 * {"fatal", FATAL, true},
529 * {"panic", PANIC, true},
534 static const struct config_enum_entry parse_debug_level_options[] = {
537 {"detailed", 2, false},
538 {"verbose", 3, false},
550 /* Saved hook values in case of unload */
551 static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
552 static planner_hook_type prev_planner = NULL;
553 static join_search_hook_type prev_join_search = NULL;
554 static set_rel_pathlist_hook_type prev_set_rel_pathlist = NULL;
556 /* Hold reference to currently active hint */
557 static HintState *current_hint_state = NULL;
560 * List of hint contexts. We treat the head of the list as the Top of the
561 * context stack, so current_hint_state always points the first element of this
564 static List *HintStateStack = NIL;
567 * Holds statement name during executing EXECUTE command. NULL for other
570 static char *stmt_name = NULL;
572 static const HintParser parsers[] = {
573 {HINT_SEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_SEQSCAN},
574 {HINT_INDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCAN},
575 {HINT_INDEXSCANREGEXP, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCANREGEXP},
576 {HINT_BITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_BITMAPSCAN},
577 {HINT_BITMAPSCANREGEXP, ScanMethodHintCreate,
578 HINT_KEYWORD_BITMAPSCANREGEXP},
579 {HINT_TIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_TIDSCAN},
580 {HINT_NOSEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOSEQSCAN},
581 {HINT_NOINDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXSCAN},
582 {HINT_NOBITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOBITMAPSCAN},
583 {HINT_NOTIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOTIDSCAN},
584 {HINT_INDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXONLYSCAN},
585 {HINT_INDEXONLYSCANREGEXP, ScanMethodHintCreate,
586 HINT_KEYWORD_INDEXONLYSCANREGEXP},
587 {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXONLYSCAN},
589 {HINT_NESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NESTLOOP},
590 {HINT_MERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_MERGEJOIN},
591 {HINT_HASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_HASHJOIN},
592 {HINT_NONESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NONESTLOOP},
593 {HINT_NOMERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOMERGEJOIN},
594 {HINT_NOHASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOHASHJOIN},
596 {HINT_LEADING, LeadingHintCreate, HINT_KEYWORD_LEADING},
597 {HINT_SET, SetHintCreate, HINT_KEYWORD_SET},
598 {HINT_ROWS, RowsHintCreate, HINT_KEYWORD_ROWS},
599 {HINT_PARALLEL, ParallelHintCreate, HINT_KEYWORD_PARALLEL},
601 {NULL, NULL, HINT_KEYWORD_UNRECOGNIZED}
604 PLpgSQL_plugin plugin_funcs = {
608 pg_hint_plan_plpgsql_stmt_beg,
609 pg_hint_plan_plpgsql_stmt_end,
615 * Module load callbacks
620 PLpgSQL_plugin **var_ptr;
622 /* Define custom GUC variables. */
623 DefineCustomBoolVariable("pg_hint_plan.enable_hint",
624 "Force planner to use plans specified in the hint comment preceding to the query.",
626 &pg_hint_plan_enable_hint,
634 DefineCustomEnumVariable("pg_hint_plan.debug_print",
635 "Logs results of hint parsing.",
639 parse_debug_level_options,
646 DefineCustomEnumVariable("pg_hint_plan.parse_messages",
647 "Message level of parse errors.",
649 &pg_hint_plan_parse_message_level,
651 parse_messages_level_options,
658 DefineCustomEnumVariable("pg_hint_plan.message_level",
659 "Message level of debug messages.",
661 &pg_hint_plan_debug_message_level,
663 parse_messages_level_options,
670 DefineCustomBoolVariable("pg_hint_plan.enable_hint_table",
671 "Let pg_hint_plan look up the hint table.",
673 &pg_hint_plan_enable_hint_table,
682 prev_post_parse_analyze_hook = post_parse_analyze_hook;
683 post_parse_analyze_hook = pg_hint_plan_post_parse_analyze;
684 prev_planner = planner_hook;
685 planner_hook = pg_hint_plan_planner;
686 prev_join_search = join_search_hook;
687 join_search_hook = pg_hint_plan_join_search;
688 prev_set_rel_pathlist = set_rel_pathlist_hook;
689 set_rel_pathlist_hook = pg_hint_plan_set_rel_pathlist;
691 /* setup PL/pgSQL plugin hook */
692 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
693 *var_ptr = &plugin_funcs;
695 RegisterResourceReleaseCallback(plpgsql_query_erase_callback, NULL);
699 * Module unload callback
705 PLpgSQL_plugin **var_ptr;
707 /* Uninstall hooks. */
708 post_parse_analyze_hook = prev_post_parse_analyze_hook;
709 planner_hook = prev_planner;
710 join_search_hook = prev_join_search;
711 set_rel_pathlist_hook = prev_set_rel_pathlist;
713 /* uninstall PL/pgSQL plugin hook */
714 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
719 * create and delete functions the hint object
723 ScanMethodHintCreate(const char *hint_str, const char *keyword,
724 HintKeyword hint_keyword)
726 ScanMethodHint *hint;
728 hint = palloc(sizeof(ScanMethodHint));
729 hint->base.hint_str = hint_str;
730 hint->base.keyword = keyword;
731 hint->base.hint_keyword = hint_keyword;
732 hint->base.type = HINT_TYPE_SCAN_METHOD;
733 hint->base.state = HINT_STATE_NOTUSED;
734 hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
735 hint->base.desc_func = (HintDescFunction) ScanMethodHintDesc;
736 hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
737 hint->base.parse_func = (HintParseFunction) ScanMethodHintParse;
738 hint->relname = NULL;
739 hint->indexnames = NIL;
740 hint->regexp = false;
741 hint->enforce_mask = 0;
743 return (Hint *) hint;
747 ScanMethodHintDelete(ScanMethodHint *hint)
753 pfree(hint->relname);
754 list_free_deep(hint->indexnames);
759 JoinMethodHintCreate(const char *hint_str, const char *keyword,
760 HintKeyword hint_keyword)
762 JoinMethodHint *hint;
764 hint = palloc(sizeof(JoinMethodHint));
765 hint->base.hint_str = hint_str;
766 hint->base.keyword = keyword;
767 hint->base.hint_keyword = hint_keyword;
768 hint->base.type = HINT_TYPE_JOIN_METHOD;
769 hint->base.state = HINT_STATE_NOTUSED;
770 hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
771 hint->base.desc_func = (HintDescFunction) JoinMethodHintDesc;
772 hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
773 hint->base.parse_func = (HintParseFunction) JoinMethodHintParse;
775 hint->inner_nrels = 0;
776 hint->relnames = NULL;
777 hint->enforce_mask = 0;
778 hint->joinrelids = NULL;
779 hint->inner_joinrelids = NULL;
781 return (Hint *) hint;
785 JoinMethodHintDelete(JoinMethodHint *hint)
794 for (i = 0; i < hint->nrels; i++)
795 pfree(hint->relnames[i]);
796 pfree(hint->relnames);
799 bms_free(hint->joinrelids);
800 bms_free(hint->inner_joinrelids);
805 LeadingHintCreate(const char *hint_str, const char *keyword,
806 HintKeyword hint_keyword)
810 hint = palloc(sizeof(LeadingHint));
811 hint->base.hint_str = hint_str;
812 hint->base.keyword = keyword;
813 hint->base.hint_keyword = hint_keyword;
814 hint->base.type = HINT_TYPE_LEADING;
815 hint->base.state = HINT_STATE_NOTUSED;
816 hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
817 hint->base.desc_func = (HintDescFunction) LeadingHintDesc;
818 hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
819 hint->base.parse_func = (HintParseFunction) LeadingHintParse;
820 hint->relations = NIL;
821 hint->outer_inner = NULL;
823 return (Hint *) hint;
827 LeadingHintDelete(LeadingHint *hint)
832 list_free_deep(hint->relations);
833 if (hint->outer_inner)
834 pfree(hint->outer_inner);
839 SetHintCreate(const char *hint_str, const char *keyword,
840 HintKeyword hint_keyword)
844 hint = palloc(sizeof(SetHint));
845 hint->base.hint_str = hint_str;
846 hint->base.keyword = keyword;
847 hint->base.hint_keyword = hint_keyword;
848 hint->base.type = HINT_TYPE_SET;
849 hint->base.state = HINT_STATE_NOTUSED;
850 hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
851 hint->base.desc_func = (HintDescFunction) SetHintDesc;
852 hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
853 hint->base.parse_func = (HintParseFunction) SetHintParse;
858 return (Hint *) hint;
862 SetHintDelete(SetHint *hint)
872 list_free(hint->words);
877 RowsHintCreate(const char *hint_str, const char *keyword,
878 HintKeyword hint_keyword)
882 hint = palloc(sizeof(RowsHint));
883 hint->base.hint_str = hint_str;
884 hint->base.keyword = keyword;
885 hint->base.hint_keyword = hint_keyword;
886 hint->base.type = HINT_TYPE_ROWS;
887 hint->base.state = HINT_STATE_NOTUSED;
888 hint->base.delete_func = (HintDeleteFunction) RowsHintDelete;
889 hint->base.desc_func = (HintDescFunction) RowsHintDesc;
890 hint->base.cmp_func = (HintCmpFunction) RowsHintCmp;
891 hint->base.parse_func = (HintParseFunction) RowsHintParse;
893 hint->inner_nrels = 0;
894 hint->relnames = NULL;
895 hint->joinrelids = NULL;
896 hint->inner_joinrelids = NULL;
897 hint->rows_str = NULL;
898 hint->value_type = RVT_ABSOLUTE;
901 return (Hint *) hint;
905 RowsHintDelete(RowsHint *hint)
914 for (i = 0; i < hint->nrels; i++)
915 pfree(hint->relnames[i]);
916 pfree(hint->relnames);
919 bms_free(hint->joinrelids);
920 bms_free(hint->inner_joinrelids);
925 ParallelHintCreate(const char *hint_str, const char *keyword,
926 HintKeyword hint_keyword)
930 hint = palloc(sizeof(ScanMethodHint));
931 hint->base.hint_str = hint_str;
932 hint->base.keyword = keyword;
933 hint->base.hint_keyword = hint_keyword;
934 hint->base.type = HINT_TYPE_PARALLEL;
935 hint->base.state = HINT_STATE_NOTUSED;
936 hint->base.delete_func = (HintDeleteFunction) ParallelHintDelete;
937 hint->base.desc_func = (HintDescFunction) ParallelHintDesc;
938 hint->base.cmp_func = (HintCmpFunction) ParallelHintCmp;
939 hint->base.parse_func = (HintParseFunction) ParallelHintParse;
940 hint->relname = NULL;
942 hint->nworkers_str = "0";
944 return (Hint *) hint;
948 ParallelHintDelete(ParallelHint *hint)
954 pfree(hint->relname);
960 HintStateCreate(void)
964 hstate = palloc(sizeof(HintState));
965 hstate->hint_str = NULL;
966 hstate->nall_hints = 0;
967 hstate->max_all_hints = 0;
968 hstate->all_hints = NULL;
969 memset(hstate->num_hints, 0, sizeof(hstate->num_hints));
970 hstate->scan_hints = NULL;
971 hstate->init_scan_mask = 0;
972 hstate->init_nworkers = 0;
973 hstate->init_min_para_tablescan_size = 0;
974 hstate->init_min_para_indexscan_size = 0;
975 hstate->init_paratup_cost = 0;
976 hstate->init_parasetup_cost = 0;
977 hstate->current_root = NULL;
978 hstate->parent_relid = 0;
979 hstate->parent_scan_hint = NULL;
980 hstate->parent_parallel_hint = NULL;
981 hstate->parent_index_infos = NIL;
982 hstate->join_hints = NULL;
983 hstate->init_join_mask = 0;
984 hstate->join_hint_level = NULL;
985 hstate->leading_hint = NULL;
986 hstate->context = superuser() ? PGC_SUSET : PGC_USERSET;
987 hstate->set_hints = NULL;
988 hstate->rows_hints = NULL;
989 hstate->parallel_hints = NULL;
995 HintStateDelete(HintState *hstate)
1002 if (hstate->hint_str)
1003 pfree(hstate->hint_str);
1005 for (i = 0; i < hstate->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
1006 hstate->all_hints[i]->delete_func(hstate->all_hints[i]);
1007 if (hstate->all_hints)
1008 pfree(hstate->all_hints);
1009 if (hstate->parent_index_infos)
1010 list_free(hstate->parent_index_infos);
1014 * Copy given value into buf, with quoting with '"' if necessary.
1017 quote_value(StringInfo buf, const char *value)
1019 bool need_quote = false;
1022 for (str = value; *str != '\0'; str++)
1024 if (isspace(*str) || *str == '(' || *str == ')' || *str == '"')
1027 appendStringInfoCharMacro(buf, '"');
1032 for (str = value; *str != '\0'; str++)
1035 appendStringInfoCharMacro(buf, '"');
1037 appendStringInfoCharMacro(buf, *str);
1041 appendStringInfoCharMacro(buf, '"');
1045 ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf)
1049 appendStringInfo(buf, "%s(", hint->base.keyword);
1050 if (hint->relname != NULL)
1052 quote_value(buf, hint->relname);
1053 foreach(l, hint->indexnames)
1055 appendStringInfoCharMacro(buf, ' ');
1056 quote_value(buf, (char *) lfirst(l));
1059 appendStringInfoString(buf, ")");
1061 appendStringInfoChar(buf, '\n');
1065 JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf)
1069 appendStringInfo(buf, "%s(", hint->base.keyword);
1070 if (hint->relnames != NULL)
1072 quote_value(buf, hint->relnames[0]);
1073 for (i = 1; i < hint->nrels; i++)
1075 appendStringInfoCharMacro(buf, ' ');
1076 quote_value(buf, hint->relnames[i]);
1079 appendStringInfoString(buf, ")");
1081 appendStringInfoChar(buf, '\n');
1085 OuterInnerDesc(OuterInnerRels *outer_inner, StringInfo buf)
1087 if (outer_inner->relation == NULL)
1094 appendStringInfoCharMacro(buf, '(');
1095 foreach(l, outer_inner->outer_inner_pair)
1100 appendStringInfoCharMacro(buf, ' ');
1102 OuterInnerDesc(lfirst(l), buf);
1105 appendStringInfoCharMacro(buf, ')');
1108 quote_value(buf, outer_inner->relation);
1112 LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf)
1114 appendStringInfo(buf, "%s(", HINT_LEADING);
1115 if (hint->outer_inner == NULL)
1122 foreach(l, hint->relations)
1127 appendStringInfoCharMacro(buf, ' ');
1129 quote_value(buf, (char *) lfirst(l));
1133 OuterInnerDesc(hint->outer_inner, buf);
1135 appendStringInfoString(buf, ")");
1137 appendStringInfoChar(buf, '\n');
1141 SetHintDesc(SetHint *hint, StringInfo buf, bool nolf)
1143 bool is_first = true;
1146 appendStringInfo(buf, "%s(", HINT_SET);
1147 foreach(l, hint->words)
1152 appendStringInfoCharMacro(buf, ' ');
1154 quote_value(buf, (char *) lfirst(l));
1156 appendStringInfo(buf, ")");
1158 appendStringInfoChar(buf, '\n');
1162 RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf)
1166 appendStringInfo(buf, "%s(", hint->base.keyword);
1167 if (hint->relnames != NULL)
1169 quote_value(buf, hint->relnames[0]);
1170 for (i = 1; i < hint->nrels; i++)
1172 appendStringInfoCharMacro(buf, ' ');
1173 quote_value(buf, hint->relnames[i]);
1176 appendStringInfo(buf, " %s", hint->rows_str);
1177 appendStringInfoString(buf, ")");
1179 appendStringInfoChar(buf, '\n');
1183 ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf)
1185 appendStringInfo(buf, "%s(", hint->base.keyword);
1186 if (hint->relname != NULL)
1188 quote_value(buf, hint->relname);
1190 /* number of workers */
1191 appendStringInfoCharMacro(buf, ' ');
1192 quote_value(buf, hint->nworkers_str);
1193 /* application mode of num of workers */
1194 appendStringInfoCharMacro(buf, ' ');
1195 appendStringInfoString(buf,
1196 (hint->force_parallel ? "hard" : "soft"));
1198 appendStringInfoString(buf, ")");
1200 appendStringInfoChar(buf, '\n');
1204 * Append string which represents all hints in a given state to buf, with
1205 * preceding title with them.
1208 desc_hint_in_state(HintState *hstate, StringInfo buf, const char *title,
1209 HintStatus state, bool nolf)
1213 appendStringInfo(buf, "%s:", title);
1215 appendStringInfoChar(buf, '\n');
1218 for (i = 0; i < hstate->nall_hints; i++)
1220 if (hstate->all_hints[i]->state != state)
1223 hstate->all_hints[i]->desc_func(hstate->all_hints[i], buf, nolf);
1227 if (nolf && nshown == 0)
1228 appendStringInfoString(buf, "(none)");
1232 * Dump contents of given hstate to server log with log level LOG.
1235 HintStateDump(HintState *hstate)
1241 elog(pg_hint_plan_debug_message_level, "pg_hint_plan:\nno hint");
1245 initStringInfo(&buf);
1247 appendStringInfoString(&buf, "pg_hint_plan:\n");
1248 desc_hint_in_state(hstate, &buf, "used hint", HINT_STATE_USED, false);
1249 desc_hint_in_state(hstate, &buf, "not used hint", HINT_STATE_NOTUSED, false);
1250 desc_hint_in_state(hstate, &buf, "duplication hint", HINT_STATE_DUPLICATION, false);
1251 desc_hint_in_state(hstate, &buf, "error hint", HINT_STATE_ERROR, false);
1253 ereport(pg_hint_plan_debug_message_level,
1254 (errmsg ("%s", buf.data)));
1260 HintStateDump2(HintState *hstate)
1266 elog(pg_hint_plan_debug_message_level,
1267 "pg_hint_plan%s: HintStateDump: no hint", qnostr);
1271 initStringInfo(&buf);
1272 appendStringInfo(&buf, "pg_hint_plan%s: HintStateDump: ", qnostr);
1273 desc_hint_in_state(hstate, &buf, "{used hints", HINT_STATE_USED, true);
1274 desc_hint_in_state(hstate, &buf, "}, {not used hints", HINT_STATE_NOTUSED, true);
1275 desc_hint_in_state(hstate, &buf, "}, {duplicate hints", HINT_STATE_DUPLICATION, true);
1276 desc_hint_in_state(hstate, &buf, "}, {error hints", HINT_STATE_ERROR, true);
1277 appendStringInfoChar(&buf, '}');
1279 ereport(pg_hint_plan_debug_message_level,
1280 (errmsg("%s", buf.data),
1282 errhidecontext(true)));
1292 RelnameCmp(const void *a, const void *b)
1294 const char *relnamea = *((const char **) a);
1295 const char *relnameb = *((const char **) b);
1297 return strcmp(relnamea, relnameb);
1301 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
1303 return RelnameCmp(&a->relname, &b->relname);
1307 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
1311 if (a->nrels != b->nrels)
1312 return a->nrels - b->nrels;
1314 for (i = 0; i < a->nrels; i++)
1317 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1325 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
1331 SetHintCmp(const SetHint *a, const SetHint *b)
1333 return strcmp(a->name, b->name);
1337 RowsHintCmp(const RowsHint *a, const RowsHint *b)
1341 if (a->nrels != b->nrels)
1342 return a->nrels - b->nrels;
1344 for (i = 0; i < a->nrels; i++)
1347 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1355 ParallelHintCmp(const ParallelHint *a, const ParallelHint *b)
1357 return RelnameCmp(&a->relname, &b->relname);
1361 HintCmp(const void *a, const void *b)
1363 const Hint *hinta = *((const Hint **) a);
1364 const Hint *hintb = *((const Hint **) b);
1366 if (hinta->type != hintb->type)
1367 return hinta->type - hintb->type;
1368 if (hinta->state == HINT_STATE_ERROR)
1370 if (hintb->state == HINT_STATE_ERROR)
1372 return hinta->cmp_func(hinta, hintb);
1376 * Returns byte offset of hint b from hint a. If hint a was specified before
1377 * b, positive value is returned.
1380 HintCmpWithPos(const void *a, const void *b)
1382 const Hint *hinta = *((const Hint **) a);
1383 const Hint *hintb = *((const Hint **) b);
1386 result = HintCmp(a, b);
1388 result = hinta->hint_str - hintb->hint_str;
1397 parse_keyword(const char *str, StringInfo buf)
1401 while (!isspace(*str) && *str != '(' && *str != '\0')
1402 appendStringInfoCharMacro(buf, *str++);
1408 skip_parenthesis(const char *str, char parenthesis)
1412 if (*str != parenthesis)
1414 if (parenthesis == '(')
1415 hint_ereport(str, ("Opening parenthesis is necessary."));
1416 else if (parenthesis == ')')
1417 hint_ereport(str, ("Closing parenthesis is necessary."));
1428 * Parse a token from str, and store malloc'd copy into word. A token can be
1429 * quoted with '"'. Return value is pointer to unparsed portion of original
1430 * string, or NULL if an error occurred.
1432 * Parsed token is truncated within NAMEDATALEN-1 bytes, when truncate is true.
1435 parse_quoted_value(const char *str, char **word, bool truncate)
1440 /* Skip leading spaces. */
1443 initStringInfo(&buf);
1456 /* Double quotation must be closed. */
1460 hint_ereport(str, ("Unterminated quoted string."));
1465 * Skip escaped double quotation.
1467 * We don't allow slash-asterisk and asterisk-slash (delimiters of
1468 * block comments) to be an object name, so users must specify
1469 * alias for such object names.
1471 * Those special names can be allowed if we care escaped slashes
1472 * and asterisks, but we don't.
1481 else if (isspace(*str) || *str == '(' || *str == ')' || *str == '"' ||
1485 appendStringInfoCharMacro(&buf, *str++);
1490 hint_ereport(str, ("Zero-length delimited string."));
1497 /* Truncate name if it's too long */
1499 truncate_identifier(buf.data, strlen(buf.data), true);
1506 static OuterInnerRels *
1507 OuterInnerRelsCreate(char *name, List *outer_inner_list)
1509 OuterInnerRels *outer_inner;
1511 outer_inner = palloc(sizeof(OuterInnerRels));
1512 outer_inner->relation = name;
1513 outer_inner->outer_inner_pair = outer_inner_list;
1519 parse_parentheses_Leading_in(const char *str, OuterInnerRels **outer_inner)
1521 List *outer_inner_pair = NIL;
1523 if ((str = skip_parenthesis(str, '(')) == NULL)
1528 /* Store words in parentheses into outer_inner_list. */
1529 while(*str != ')' && *str != '\0')
1531 OuterInnerRels *outer_inner_rels;
1535 str = parse_parentheses_Leading_in(str, &outer_inner_rels);
1543 if ((str = parse_quoted_value(str, &name, true)) == NULL)
1546 outer_inner_rels = OuterInnerRelsCreate(name, NIL);
1549 outer_inner_pair = lappend(outer_inner_pair, outer_inner_rels);
1554 (str = skip_parenthesis(str, ')')) == NULL)
1556 list_free(outer_inner_pair);
1560 *outer_inner = OuterInnerRelsCreate(NULL, outer_inner_pair);
1566 parse_parentheses_Leading(const char *str, List **name_list,
1567 OuterInnerRels **outer_inner)
1570 bool truncate = true;
1572 if ((str = skip_parenthesis(str, '(')) == NULL)
1578 if ((str = parse_parentheses_Leading_in(str, outer_inner)) == NULL)
1583 /* Store words in parentheses into name_list. */
1584 while(*str != ')' && *str != '\0')
1586 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1588 list_free(*name_list);
1592 *name_list = lappend(*name_list, name);
1597 if ((str = skip_parenthesis(str, ')')) == NULL)
1603 parse_parentheses(const char *str, List **name_list, HintKeyword keyword)
1606 bool truncate = true;
1608 if ((str = skip_parenthesis(str, '(')) == NULL)
1613 /* Store words in parentheses into name_list. */
1614 while(*str != ')' && *str != '\0')
1616 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1618 list_free(*name_list);
1622 *name_list = lappend(*name_list, name);
1625 if (keyword == HINT_KEYWORD_INDEXSCANREGEXP ||
1626 keyword == HINT_KEYWORD_INDEXONLYSCANREGEXP ||
1627 keyword == HINT_KEYWORD_BITMAPSCANREGEXP ||
1628 keyword == HINT_KEYWORD_SET)
1634 if ((str = skip_parenthesis(str, ')')) == NULL)
1640 parse_hints(HintState *hstate, Query *parse, const char *str)
1645 initStringInfo(&buf);
1646 while (*str != '\0')
1648 const HintParser *parser;
1650 /* in error message, we output the comment including the keyword. */
1651 head = (char *) str;
1653 /* parse only the keyword of the hint. */
1654 resetStringInfo(&buf);
1655 str = parse_keyword(str, &buf);
1657 for (parser = parsers; parser->keyword != NULL; parser++)
1659 char *keyword = parser->keyword;
1662 if (pg_strcasecmp(buf.data, keyword) != 0)
1665 hint = parser->create_func(head, keyword, parser->hint_keyword);
1667 /* parser of each hint does parse in a parenthesis. */
1668 if ((str = hint->parse_func(hint, hstate, parse, str)) == NULL)
1670 hint->delete_func(hint);
1676 * Add hint information into all_hints array. If we don't have
1677 * enough space, double the array.
1679 if (hstate->nall_hints == 0)
1681 hstate->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
1682 hstate->all_hints = (Hint **)
1683 palloc(sizeof(Hint *) * hstate->max_all_hints);
1685 else if (hstate->nall_hints == hstate->max_all_hints)
1687 hstate->max_all_hints *= 2;
1688 hstate->all_hints = (Hint **)
1689 repalloc(hstate->all_hints,
1690 sizeof(Hint *) * hstate->max_all_hints);
1693 hstate->all_hints[hstate->nall_hints] = hint;
1694 hstate->nall_hints++;
1701 if (parser->keyword == NULL)
1704 ("Unrecognized hint keyword \"%s\".", buf.data));
1715 * Get hints from table by client-supplied query string and application name.
1718 get_hints_from_table(const char *client_query, const char *client_application)
1720 const char *search_query =
1722 " FROM hint_plan.hints "
1723 " WHERE norm_query_string = $1 "
1724 " AND ( application_name = $2 "
1725 " OR application_name = '' ) "
1726 " ORDER BY application_name DESC";
1727 static SPIPlanPtr plan = NULL;
1729 Oid argtypes[2] = { TEXTOID, TEXTOID };
1731 bool nulls[2] = { false, false };
1737 bool snapshot_set = false;
1739 hint_inhibit_level++;
1741 if (!ActiveSnapshotSet())
1743 PushActiveSnapshot(GetTransactionSnapshot());
1744 snapshot_set = true;
1752 p = SPI_prepare(search_query, 2, argtypes);
1753 plan = SPI_saveplan(p);
1757 qry = cstring_to_text(client_query);
1758 app = cstring_to_text(client_application);
1759 values[0] = PointerGetDatum(qry);
1760 values[1] = PointerGetDatum(app);
1762 SPI_execute_plan(plan, values, nulls, true, 1);
1764 if (SPI_processed > 0)
1768 hints = SPI_getvalue(SPI_tuptable->vals[0],
1769 SPI_tuptable->tupdesc, 1);
1771 * Here we use SPI_palloc to ensure that hints string is valid even
1772 * after SPI_finish call. We can't use simple palloc because it
1773 * allocates memory in SPI's context and that context is deleted in
1776 buf = SPI_palloc(strlen(hints) + 1);
1784 PopActiveSnapshot();
1786 hint_inhibit_level--;
1790 hint_inhibit_level--;
1799 * Get client-supplied query string. Addtion to that the jumbled query is
1800 * supplied if the caller requested. From the restriction of JumbleQuery, some
1801 * kind of query needs special amendments. Reutrns NULL if this query doesn't
1802 * change the current hint. This function returns NULL also when something
1803 * wrong has happend and let the caller continue using the current hints.
1806 get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
1808 const char *p = debug_query_string;
1811 * If debug_query_string is set, it is the top level statement. But in some
1812 * cases we reach here with debug_query_string set NULL for example in the
1813 * case of DESCRIBE message handling. We may still see a candidate
1814 * top-level query in pstate in the case.
1818 /* We don't see a query string, return NULL */
1819 if (!pstate->p_sourcetext)
1822 p = pstate->p_sourcetext;
1825 if (jumblequery != NULL)
1826 *jumblequery = query;
1828 if (query->commandType == CMD_UTILITY)
1830 Query *target_query = (Query *)query->utilityStmt;
1833 * Some CMD_UTILITY statements have a subquery that we can hint on.
1834 * Since EXPLAIN can be placed before other kind of utility statements
1835 * and EXECUTE can be contained other kind of utility statements, these
1836 * conditions are not mutually exclusive and should be considered in
1839 if (IsA(target_query, ExplainStmt))
1841 ExplainStmt *stmt = (ExplainStmt *)target_query;
1843 Assert(IsA(stmt->query, Query));
1844 target_query = (Query *)stmt->query;
1846 /* strip out the top-level query for further processing */
1847 if (target_query->commandType == CMD_UTILITY &&
1848 target_query->utilityStmt != NULL)
1849 target_query = (Query *)target_query->utilityStmt;
1852 if (IsA(target_query, DeclareCursorStmt))
1854 DeclareCursorStmt *stmt = (DeclareCursorStmt *)target_query;
1855 Query *query = (Query *)stmt->query;
1857 /* the target must be CMD_SELECT in this case */
1858 Assert(IsA(query, Query) && query->commandType == CMD_SELECT);
1859 target_query = query;
1862 if (IsA(target_query, CreateTableAsStmt))
1864 CreateTableAsStmt *stmt = (CreateTableAsStmt *) target_query;
1866 Assert(IsA(stmt->query, Query));
1867 target_query = (Query *) stmt->query;
1869 /* strip out the top-level query for further processing */
1870 if (target_query->commandType == CMD_UTILITY &&
1871 target_query->utilityStmt != NULL)
1872 target_query = (Query *)target_query->utilityStmt;
1875 if (IsA(target_query, ExecuteStmt))
1878 * Use the prepared query for EXECUTE. The Query for jumble
1879 * also replaced with the corresponding one.
1881 ExecuteStmt *stmt = (ExecuteStmt *)target_query;
1882 PreparedStatement *entry;
1884 entry = FetchPreparedStatement(stmt->name, true);
1885 p = entry->plansource->query_string;
1886 target_query = (Query *) linitial (entry->plansource->query_list);
1889 /* JumbleQuery accespts only a non-utility Query */
1890 if (!IsA(target_query, Query) ||
1891 target_query->utilityStmt != NULL)
1892 target_query = NULL;
1895 *jumblequery = target_query;
1897 /* Return NULL if the pstate is not identical to the top-level query */
1898 else if (strcmp(pstate->p_sourcetext, p) != 0)
1905 * Get hints from the head block comment in client-supplied query string.
1908 get_hints_from_comment(const char *p)
1910 const char *hint_head;
1918 /* extract query head comment. */
1919 hint_head = strstr(p, HINT_START);
1920 if (hint_head == NULL)
1922 for (;p < hint_head; p++)
1925 * Allow these characters precedes hint comment:
1927 * - alphabets which are in ASCII range
1928 * - space, tabs and new-lines
1929 * - underscores, for identifier
1930 * - commas, for SELECT clause, EXPLAIN and PREPARE
1931 * - parentheses, for EXPLAIN and PREPARE
1933 * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1934 * avoid behavior which depends on locale setting.
1936 if (!(*p >= '0' && *p <= '9') &&
1937 !(*p >= 'A' && *p <= 'Z') &&
1938 !(*p >= 'a' && *p <= 'z') &&
1942 *p != '(' && *p != ')')
1946 len = strlen(HINT_START);
1951 /* find hint end keyword. */
1952 if ((tail = strstr(p, HINT_END)) == NULL)
1954 hint_ereport(head, ("Unterminated block comment."));
1958 /* We don't support nested block comments. */
1959 if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1961 hint_ereport(head, ("Nested block comments are not supported."));
1965 /* Make a copy of hint. */
1967 head = palloc(len + 1);
1968 memcpy(head, p, len);
1976 * Parse hints that got, create hint struct from parse tree and parse hints.
1979 create_hintstate(Query *parse, const char *hints)
1988 /* -1 means that no Parallel hint is specified. */
1989 max_hint_nworkers = -1;
1992 hstate = HintStateCreate();
1993 hstate->hint_str = (char *) hints;
1995 /* parse each hint. */
1996 parse_hints(hstate, parse, p);
1998 /* When nothing specified a hint, we free HintState and returns NULL. */
1999 if (hstate->nall_hints == 0)
2001 HintStateDelete(hstate);
2005 /* Sort hints in order of original position. */
2006 qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
2009 /* Count number of hints per hint-type. */
2010 for (i = 0; i < hstate->nall_hints; i++)
2012 Hint *cur_hint = hstate->all_hints[i];
2013 hstate->num_hints[cur_hint->type]++;
2017 * If an object (or a set of objects) has multiple hints of same hint-type,
2018 * only the last hint is valid and others are ignored in planning.
2019 * Hints except the last are marked as 'duplicated' to remember the order.
2021 for (i = 0; i < hstate->nall_hints - 1; i++)
2023 Hint *cur_hint = hstate->all_hints[i];
2024 Hint *next_hint = hstate->all_hints[i + 1];
2027 * Leading hint is marked as 'duplicated' in transform_join_hints.
2029 if (cur_hint->type == HINT_TYPE_LEADING &&
2030 next_hint->type == HINT_TYPE_LEADING)
2034 * Note that we need to pass addresses of hint pointers, because
2035 * HintCmp is designed to sort array of Hint* by qsort.
2037 if (HintCmp(&cur_hint, &next_hint) == 0)
2039 hint_ereport(cur_hint->hint_str,
2040 ("Conflict %s hint.", HintTypeName[cur_hint->type]));
2041 cur_hint->state = HINT_STATE_DUPLICATION;
2046 * Make sure that per-type array pointers point proper position in the
2047 * array which consists of all hints.
2049 hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
2050 hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
2051 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
2052 hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
2053 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
2054 hstate->set_hints = (SetHint **) (hstate->leading_hint +
2055 hstate->num_hints[HINT_TYPE_LEADING]);
2056 hstate->rows_hints = (RowsHint **) (hstate->set_hints +
2057 hstate->num_hints[HINT_TYPE_SET]);
2058 hstate->parallel_hints = (ParallelHint **) (hstate->set_hints +
2059 hstate->num_hints[HINT_TYPE_ROWS]);
2065 * Parse inside of parentheses of scan-method hints.
2068 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
2071 const char *keyword = hint->base.keyword;
2072 HintKeyword hint_keyword = hint->base.hint_keyword;
2073 List *name_list = NIL;
2076 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2079 /* Parse relation name and index name(s) if given hint accepts. */
2080 length = list_length(name_list);
2082 /* at least twp parameters required */
2086 ("%s hint requires a relation.", hint->base.keyword));
2087 hint->base.state = HINT_STATE_ERROR;
2091 hint->relname = linitial(name_list);
2092 hint->indexnames = list_delete_first(name_list);
2094 /* check whether the hint accepts index name(s) */
2095 if (length > 1 && !SCAN_HINT_ACCEPTS_INDEX_NAMES(hint_keyword))
2098 ("%s hint accepts only one relation.",
2099 hint->base.keyword));
2100 hint->base.state = HINT_STATE_ERROR;
2104 /* Set a bit for specified hint. */
2105 switch (hint_keyword)
2107 case HINT_KEYWORD_SEQSCAN:
2108 hint->enforce_mask = ENABLE_SEQSCAN;
2110 case HINT_KEYWORD_INDEXSCAN:
2111 hint->enforce_mask = ENABLE_INDEXSCAN;
2113 case HINT_KEYWORD_INDEXSCANREGEXP:
2114 hint->enforce_mask = ENABLE_INDEXSCAN;
2115 hint->regexp = true;
2117 case HINT_KEYWORD_BITMAPSCAN:
2118 hint->enforce_mask = ENABLE_BITMAPSCAN;
2120 case HINT_KEYWORD_BITMAPSCANREGEXP:
2121 hint->enforce_mask = ENABLE_BITMAPSCAN;
2122 hint->regexp = true;
2124 case HINT_KEYWORD_TIDSCAN:
2125 hint->enforce_mask = ENABLE_TIDSCAN;
2127 case HINT_KEYWORD_NOSEQSCAN:
2128 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
2130 case HINT_KEYWORD_NOINDEXSCAN:
2131 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
2133 case HINT_KEYWORD_NOBITMAPSCAN:
2134 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
2136 case HINT_KEYWORD_NOTIDSCAN:
2137 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
2139 case HINT_KEYWORD_INDEXONLYSCAN:
2140 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2142 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
2143 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2144 hint->regexp = true;
2146 case HINT_KEYWORD_NOINDEXONLYSCAN:
2147 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
2150 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2159 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
2162 const char *keyword = hint->base.keyword;
2163 HintKeyword hint_keyword = hint->base.hint_keyword;
2164 List *name_list = NIL;
2166 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2169 hint->nrels = list_length(name_list);
2171 if (hint->nrels > 0)
2177 * Transform relation names from list to array to sort them with qsort
2180 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2181 foreach (l, name_list)
2183 hint->relnames[i] = lfirst(l);
2188 list_free(name_list);
2190 /* A join hint requires at least two relations */
2191 if (hint->nrels < 2)
2194 ("%s hint requires at least two relations.",
2195 hint->base.keyword));
2196 hint->base.state = HINT_STATE_ERROR;
2200 /* Sort hints in alphabetical order of relation names. */
2201 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2203 switch (hint_keyword)
2205 case HINT_KEYWORD_NESTLOOP:
2206 hint->enforce_mask = ENABLE_NESTLOOP;
2208 case HINT_KEYWORD_MERGEJOIN:
2209 hint->enforce_mask = ENABLE_MERGEJOIN;
2211 case HINT_KEYWORD_HASHJOIN:
2212 hint->enforce_mask = ENABLE_HASHJOIN;
2214 case HINT_KEYWORD_NONESTLOOP:
2215 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
2217 case HINT_KEYWORD_NOMERGEJOIN:
2218 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
2220 case HINT_KEYWORD_NOHASHJOIN:
2221 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
2224 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2233 OuterInnerPairCheck(OuterInnerRels *outer_inner)
2236 if (outer_inner->outer_inner_pair == NIL)
2238 if (outer_inner->relation)
2244 if (list_length(outer_inner->outer_inner_pair) == 2)
2246 foreach(l, outer_inner->outer_inner_pair)
2248 if (!OuterInnerPairCheck(lfirst(l)))
2259 OuterInnerList(OuterInnerRels *outer_inner)
2261 List *outer_inner_list = NIL;
2263 OuterInnerRels *outer_inner_rels;
2265 foreach(l, outer_inner->outer_inner_pair)
2267 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
2269 if (outer_inner_rels->relation != NULL)
2270 outer_inner_list = lappend(outer_inner_list,
2271 outer_inner_rels->relation);
2273 outer_inner_list = list_concat(outer_inner_list,
2274 OuterInnerList(outer_inner_rels));
2276 return outer_inner_list;
2280 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
2283 List *name_list = NIL;
2284 OuterInnerRels *outer_inner = NULL;
2286 if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
2290 if (outer_inner != NULL)
2291 name_list = OuterInnerList(outer_inner);
2293 hint->relations = name_list;
2294 hint->outer_inner = outer_inner;
2296 /* A Leading hint requires at least two relations */
2297 if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
2299 hint_ereport(hint->base.hint_str,
2300 ("%s hint requires at least two relations.",
2302 hint->base.state = HINT_STATE_ERROR;
2304 else if (hint->outer_inner != NULL &&
2305 !OuterInnerPairCheck(hint->outer_inner))
2307 hint_ereport(hint->base.hint_str,
2308 ("%s hint requires two sets of relations when parentheses nests.",
2310 hint->base.state = HINT_STATE_ERROR;
2317 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
2319 List *name_list = NIL;
2321 if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
2325 hint->words = name_list;
2327 /* We need both name and value to set GUC parameter. */
2328 if (list_length(name_list) == 2)
2330 hint->name = linitial(name_list);
2331 hint->value = lsecond(name_list);
2335 hint_ereport(hint->base.hint_str,
2336 ("%s hint requires name and value of GUC parameter.",
2338 hint->base.state = HINT_STATE_ERROR;
2345 RowsHintParse(RowsHint *hint, HintState *hstate, Query *parse,
2348 HintKeyword hint_keyword = hint->base.hint_keyword;
2349 List *name_list = NIL;
2353 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2356 /* Last element must be rows specification */
2357 hint->nrels = list_length(name_list) - 1;
2359 if (hint->nrels > 0)
2365 * Transform relation names from list to array to sort them with qsort
2368 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2369 foreach (l, name_list)
2371 if (hint->nrels <= i)
2373 hint->relnames[i] = lfirst(l);
2378 /* Retieve rows estimation */
2379 rows_str = list_nth(name_list, hint->nrels);
2380 hint->rows_str = rows_str; /* store as-is for error logging */
2381 if (rows_str[0] == '#')
2383 hint->value_type = RVT_ABSOLUTE;
2386 else if (rows_str[0] == '+')
2388 hint->value_type = RVT_ADD;
2391 else if (rows_str[0] == '-')
2393 hint->value_type = RVT_SUB;
2396 else if (rows_str[0] == '*')
2398 hint->value_type = RVT_MULTI;
2403 hint_ereport(rows_str, ("Unrecognized rows value type notation."));
2404 hint->base.state = HINT_STATE_ERROR;
2407 hint->rows = strtod(rows_str, &end_ptr);
2410 hint_ereport(rows_str,
2411 ("%s hint requires valid number as rows estimation.",
2412 hint->base.keyword));
2413 hint->base.state = HINT_STATE_ERROR;
2417 /* A join hint requires at least two relations */
2418 if (hint->nrels < 2)
2421 ("%s hint requires at least two relations.",
2422 hint->base.keyword));
2423 hint->base.state = HINT_STATE_ERROR;
2427 list_free(name_list);
2429 /* Sort relnames in alphabetical order. */
2430 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2436 ParallelHintParse(ParallelHint *hint, HintState *hstate, Query *parse,
2439 HintKeyword hint_keyword = hint->base.hint_keyword;
2440 List *name_list = NIL;
2444 bool force_parallel = false;
2446 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2449 /* Parse relation name and index name(s) if given hint accepts. */
2450 length = list_length(name_list);
2452 if (length < 2 || length > 3)
2455 ("wrong number of arguments (%d): %s",
2456 length, hint->base.keyword));
2457 hint->base.state = HINT_STATE_ERROR;
2461 hint->relname = linitial(name_list);
2463 /* The second parameter is number of workers */
2464 hint->nworkers_str = list_nth(name_list, 1);
2465 nworkers = strtod(hint->nworkers_str, &end_ptr);
2466 if (*end_ptr || nworkers < 0 || nworkers > max_worker_processes)
2469 hint_ereport(hint->nworkers_str,
2470 ("number of workers must be a number: %s",
2471 hint->base.keyword));
2472 else if (nworkers < 0)
2473 hint_ereport(hint->nworkers_str,
2474 ("number of workers must be positive: %s",
2475 hint->base.keyword));
2476 else if (nworkers > max_worker_processes)
2477 hint_ereport(hint->nworkers_str,
2478 ("number of workers = %d is larger than max_worker_processes(%d): %s",
2479 nworkers, max_worker_processes, hint->base.keyword));
2481 hint->base.state = HINT_STATE_ERROR;
2484 hint->nworkers = nworkers;
2486 /* optional third parameter is specified */
2489 const char *modeparam = (const char *)list_nth(name_list, 2);
2490 if (pg_strcasecmp(modeparam, "hard") == 0)
2491 force_parallel = true;
2492 else if (pg_strcasecmp(modeparam, "soft") != 0)
2494 hint_ereport(modeparam,
2495 ("enforcement must be soft or hard: %s",
2496 hint->base.keyword));
2497 hint->base.state = HINT_STATE_ERROR;
2501 hint->force_parallel = force_parallel;
2503 if (hint->base.state != HINT_STATE_ERROR &&
2504 nworkers > max_hint_nworkers)
2505 max_hint_nworkers = nworkers;
2511 * set GUC parameter functions
2515 get_current_scan_mask()
2520 mask |= ENABLE_SEQSCAN;
2521 if (enable_indexscan)
2522 mask |= ENABLE_INDEXSCAN;
2523 if (enable_bitmapscan)
2524 mask |= ENABLE_BITMAPSCAN;
2526 mask |= ENABLE_TIDSCAN;
2527 if (enable_indexonlyscan)
2528 mask |= ENABLE_INDEXONLYSCAN;
2534 get_current_join_mask()
2538 if (enable_nestloop)
2539 mask |= ENABLE_NESTLOOP;
2540 if (enable_mergejoin)
2541 mask |= ENABLE_MERGEJOIN;
2542 if (enable_hashjoin)
2543 mask |= ENABLE_HASHJOIN;
2549 * Sets GUC prameters without throwing exception. Reutrns false if something
2553 set_config_option_noerror(const char *name, const char *value,
2554 GucContext context, GucSource source,
2555 GucAction action, bool changeVal, int elevel)
2558 MemoryContext ccxt = CurrentMemoryContext;
2562 result = set_config_option(name, value, context, source,
2563 action, changeVal, 0, false);
2569 /* Save error info */
2570 MemoryContextSwitchTo(ccxt);
2571 errdata = CopyErrorData();
2575 (errcode(errdata->sqlerrcode),
2576 errmsg("%s", errdata->message),
2577 errdata->detail ? errdetail("%s", errdata->detail) : 0,
2578 errdata->hint ? errhint("%s", errdata->hint) : 0));
2580 FreeErrorData(errdata);
2588 * Sets GUC parameter of int32 type without throwing exceptions. Returns false
2589 * if something wrong.
2592 set_config_int32_option(const char *name, int32 value, GucContext context)
2594 char buf[16]; /* enough for int32 */
2596 if (snprintf(buf, 16, "%d", value) < 0)
2598 ereport(pg_hint_plan_parse_message_level,
2599 (errmsg ("Failed to convert integer to string: %d", value)));
2604 set_config_option_noerror(name, buf, context,
2605 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2606 pg_hint_plan_parse_message_level);
2609 /* setup scan method enforcement according to given options */
2611 setup_guc_enforcement(SetHint **options, int noptions, GucContext context)
2615 for (i = 0; i < noptions; i++)
2617 SetHint *hint = options[i];
2620 if (!hint_state_enabled(hint))
2623 result = set_config_option_noerror(hint->name, hint->value, context,
2624 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2625 pg_hint_plan_parse_message_level);
2627 hint->base.state = HINT_STATE_USED;
2629 hint->base.state = HINT_STATE_ERROR;
2636 * Setup parallel execution environment.
2638 * If hint is not NULL, set up using it, elsewise reset to initial environment.
2641 setup_parallel_plan_enforcement(ParallelHint *hint, HintState *state)
2645 hint->base.state = HINT_STATE_USED;
2646 set_config_int32_option("max_parallel_workers_per_gather",
2647 hint->nworkers, state->context);
2650 set_config_int32_option("max_parallel_workers_per_gather",
2651 state->init_nworkers, state->context);
2653 /* force means that enforce parallel as far as possible */
2654 if (hint && hint->force_parallel)
2656 set_config_int32_option("parallel_tuple_cost", 0, state->context);
2657 set_config_int32_option("parallel_setup_cost", 0, state->context);
2658 set_config_int32_option("min_parallel_table_scan_size", 0,
2660 set_config_int32_option("min_parallel_index_scan_size", 0,
2665 set_config_int32_option("parallel_tuple_cost",
2666 state->init_paratup_cost, state->context);
2667 set_config_int32_option("parallel_setup_cost",
2668 state->init_parasetup_cost, state->context);
2669 set_config_int32_option("min_parallel_table_scan_size",
2670 state->init_min_para_tablescan_size,
2672 set_config_int32_option("min_parallel_index_scan_size",
2673 state->init_min_para_indexscan_size,
2678 #define SET_CONFIG_OPTION(name, type_bits) \
2679 set_config_option_noerror((name), \
2680 (mask & (type_bits)) ? "true" : "false", \
2681 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
2685 * Setup GUC environment to enforce scan methods. If scanhint is NULL, reset
2686 * GUCs to the saved state in state.
2689 setup_scan_method_enforcement(ScanMethodHint *scanhint, HintState *state)
2691 unsigned char enforce_mask = state->init_scan_mask;
2692 GucContext context = state->context;
2697 enforce_mask = scanhint->enforce_mask;
2698 scanhint->base.state = HINT_STATE_USED;
2701 if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
2702 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
2703 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
2705 mask = enforce_mask;
2707 mask = enforce_mask & current_hint_state->init_scan_mask;
2709 SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
2710 SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
2711 SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
2712 SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
2713 SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
2717 set_join_config_options(unsigned char enforce_mask, GucContext context)
2721 if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
2722 enforce_mask == ENABLE_HASHJOIN)
2723 mask = enforce_mask;
2725 mask = enforce_mask & current_hint_state->init_join_mask;
2727 SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
2728 SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
2729 SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
2733 * Push a hint into hint stack which is implemented with List struct. Head of
2734 * list is top of stack.
2737 push_hint(HintState *hstate)
2739 /* Prepend new hint to the list means pushing to stack. */
2740 HintStateStack = lcons(hstate, HintStateStack);
2742 /* Pushed hint is the one which should be used hereafter. */
2743 current_hint_state = hstate;
2746 /* Pop a hint from hint stack. Popped hint is automatically discarded. */
2750 /* Hint stack must not be empty. */
2751 if(HintStateStack == NIL)
2752 elog(ERROR, "hint stack is empty");
2755 * Take a hint at the head from the list, and free it. Switch
2756 * current_hint_state to point new head (NULL if the list is empty).
2758 HintStateStack = list_delete_first(HintStateStack);
2759 HintStateDelete(current_hint_state);
2760 if(HintStateStack == NIL)
2761 current_hint_state = NULL;
2763 current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
2767 * Retrieve and store a hint string from given query or from the hint table.
2768 * If we are using the hint table, the query string is needed to be normalized.
2769 * However, ParseState, which is not available in planner_hook, is required to
2770 * check if the query tree (Query) is surely corresponding to the target query.
2773 pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
2775 const char *query_str;
2776 MemoryContext oldcontext;
2778 if (prev_post_parse_analyze_hook)
2779 prev_post_parse_analyze_hook(pstate, query);
2781 /* do nothing under hint table search */
2782 if (hint_inhibit_level > 0)
2785 if (!pg_hint_plan_enable_hint)
2787 if (current_hint_str)
2789 pfree((void *)current_hint_str);
2790 current_hint_str = NULL;
2795 /* increment the query number */
2797 if (debug_level > 1)
2798 snprintf(qnostr, sizeof(qnostr), "[qno=0x%x]", qno++);
2801 /* search the hint table for a hint if requested */
2802 if (pg_hint_plan_enable_hint_table)
2805 pgssJumbleState jstate;
2807 char *normalized_query = NULL;
2809 query_str = get_query_string(pstate, query, &jumblequery);
2811 /* If this query is not for hint, just return */
2815 /* clear the previous hint string */
2816 if (current_hint_str)
2818 pfree((void *)current_hint_str);
2819 current_hint_str = NULL;
2825 * XXX: normalizing code is copied from pg_stat_statements.c, so be
2826 * careful to PostgreSQL's version up.
2828 jstate.jumble = (unsigned char *) palloc(JUMBLE_SIZE);
2829 jstate.jumble_len = 0;
2830 jstate.clocations_buf_size = 32;
2831 jstate.clocations = (pgssLocationLen *)
2832 palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
2833 jstate.clocations_count = 0;
2835 JumbleQuery(&jstate, jumblequery);
2838 * Normalize the query string by replacing constants with '?'
2841 * Search hint string which is stored keyed by query string
2842 * and application name. The query string is normalized to allow
2845 * Adding 1 byte to query_len ensures that the returned string has
2846 * a terminating NULL.
2848 query_len = strlen(query_str) + 1;
2850 generate_normalized_query(&jstate, query_str,
2851 query->stmt_location,
2853 GetDatabaseEncoding());
2856 * find a hint for the normalized query. the result should be in
2859 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2861 get_hints_from_table(normalized_query, application_name);
2862 MemoryContextSwitchTo(oldcontext);
2864 if (debug_level > 1)
2866 if (current_hint_str)
2867 ereport(pg_hint_plan_debug_message_level,
2868 (errmsg("pg_hint_plan[qno=0x%x]: "
2869 "post_parse_analyze_hook: "
2870 "hints from table: \"%s\": "
2871 "normalized_query=\"%s\", "
2872 "application name =\"%s\"",
2873 qno, current_hint_str,
2874 normalized_query, application_name),
2875 errhidestmt(msgqno != qno),
2876 errhidecontext(msgqno != qno)));
2878 ereport(pg_hint_plan_debug_message_level,
2879 (errmsg("pg_hint_plan[qno=0x%x]: "
2880 "no match found in table: "
2881 "application name = \"%s\", "
2882 "normalized_query=\"%s\"",
2883 qno, application_name,
2885 errhidestmt(msgqno != qno),
2886 errhidecontext(msgqno != qno)));
2892 /* retrun if we have hint here */
2893 if (current_hint_str)
2897 query_str = get_query_string(pstate, query, NULL);
2902 * get hints from the comment. However we may have the same query
2903 * string with the previous call, but just retrieving hints is expected
2904 * to be faster than checking for identicalness before retrieval.
2906 if (current_hint_str)
2907 pfree((void *)current_hint_str);
2909 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2910 current_hint_str = get_hints_from_comment(query_str);
2911 MemoryContextSwitchTo(oldcontext);
2914 if (debug_level > 1)
2916 if (debug_level == 1 &&
2917 (stmt_name || strcmp(query_str, debug_query_string)))
2918 ereport(pg_hint_plan_debug_message_level,
2919 (errmsg("hints in comment=\"%s\"",
2920 current_hint_str ? current_hint_str : "(none)"),
2921 errhidestmt(msgqno != qno),
2922 errhidecontext(msgqno != qno)));
2924 ereport(pg_hint_plan_debug_message_level,
2925 (errmsg("hints in comment=\"%s\", stmt=\"%s\", query=\"%s\", debug_query_string=\"%s\"",
2926 current_hint_str ? current_hint_str : "(none)",
2927 stmt_name, query_str, debug_query_string),
2928 errhidestmt(msgqno != qno),
2929 errhidecontext(msgqno != qno)));
2935 * Read and set up hint information
2937 static PlannedStmt *
2938 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
2941 PlannedStmt *result;
2945 * Use standard planner if pg_hint_plan is disabled or current nesting
2946 * depth is nesting depth of SPI calls. Other hook functions try to change
2947 * plan with current_hint_state if any, so set it to NULL.
2949 if (!pg_hint_plan_enable_hint || hint_inhibit_level > 0)
2951 if (debug_level > 1)
2952 ereport(pg_hint_plan_debug_message_level,
2953 (errmsg ("pg_hint_plan%s: planner: enable_hint=%d,"
2954 " hint_inhibit_level=%d",
2955 qnostr, pg_hint_plan_enable_hint,
2956 hint_inhibit_level),
2957 errhidestmt(msgqno != qno)));
2960 goto standard_planner_proc;
2964 * Support for nested plpgsql functions. This is quite ugly but this is the
2965 * only point I could find where I can get the query string.
2967 if (plpgsql_recurse_level > 0)
2969 MemoryContext oldcontext;
2971 if (current_hint_str)
2972 pfree((void *)current_hint_str);
2974 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2976 get_hints_from_comment((char *)error_context_stack->arg);
2977 MemoryContextSwitchTo(oldcontext);
2980 if (!current_hint_str)
2981 goto standard_planner_proc;
2983 /* parse the hint into hint state struct */
2984 hstate = create_hintstate(parse, pstrdup(current_hint_str));
2986 /* run standard planner if the statement has not valid hint */
2988 goto standard_planner_proc;
2991 * Push new hint struct to the hint stack to disable previous hint context.
2995 /* Set scan enforcement here. */
2996 save_nestlevel = NewGUCNestLevel();
2998 /* Apply Set hints, then save it as the initial state */
2999 setup_guc_enforcement(current_hint_state->set_hints,
3000 current_hint_state->num_hints[HINT_TYPE_SET],
3001 current_hint_state->context);
3003 current_hint_state->init_scan_mask = get_current_scan_mask();
3004 current_hint_state->init_join_mask = get_current_join_mask();
3005 current_hint_state->init_min_para_tablescan_size =
3006 min_parallel_table_scan_size;
3007 current_hint_state->init_min_para_indexscan_size =
3008 min_parallel_index_scan_size;
3009 current_hint_state->init_paratup_cost = parallel_tuple_cost;
3010 current_hint_state->init_parasetup_cost = parallel_setup_cost;
3013 * max_parallel_workers_per_gather should be non-zero here if Workers hint
3016 if (max_hint_nworkers > 0 && max_parallel_workers_per_gather < 1)
3017 set_config_int32_option("max_parallel_workers_per_gather",
3018 1, current_hint_state->context);
3019 current_hint_state->init_nworkers = max_parallel_workers_per_gather;
3021 if (debug_level > 1)
3023 ereport(pg_hint_plan_debug_message_level,
3024 (errhidestmt(msgqno != qno),
3025 errmsg("pg_hint_plan%s: planner", qnostr)));
3030 * Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
3031 * the state when this planner started when error occurred in planner.
3036 result = (*prev_planner) (parse, cursorOptions, boundParams);
3038 result = standard_planner(parse, cursorOptions, boundParams);
3043 * Rollback changes of GUC parameters, and pop current hint context
3044 * from hint stack to rewind the state.
3046 AtEOXact_GUC(true, save_nestlevel);
3052 /* Print hint in debug mode. */
3053 if (debug_level == 1)
3054 HintStateDump(current_hint_state);
3055 else if (debug_level > 1)
3056 HintStateDump2(current_hint_state);
3059 * Rollback changes of GUC parameters, and pop current hint context from
3060 * hint stack to rewind the state.
3062 AtEOXact_GUC(true, save_nestlevel);
3067 standard_planner_proc:
3068 if (debug_level > 1)
3070 ereport(pg_hint_plan_debug_message_level,
3071 (errhidestmt(msgqno != qno),
3072 errmsg("pg_hint_plan%s: planner: no valid hint",
3076 current_hint_state = NULL;
3078 return (*prev_planner) (parse, cursorOptions, boundParams);
3080 return standard_planner(parse, cursorOptions, boundParams);
3084 * Find scan method hint to be applied to the given relation
3087 static ScanMethodHint *
3088 find_scan_hint(PlannerInfo *root, Index relid)
3092 ScanMethodHint *real_name_hint = NULL;
3093 ScanMethodHint *alias_hint = NULL;
3096 /* This should not be a join rel */
3098 rel = root->simple_rel_array[relid];
3101 * This function is called for any RelOptInfo or its inheritance parent if
3102 * any. If we are called from inheritance planner, the RelOptInfo for the
3103 * parent of target child relation is not set in the planner info.
3105 * Otherwise we should check that the reloptinfo is base relation or
3106 * inheritance children.
3109 rel->reloptkind != RELOPT_BASEREL &&
3110 rel->reloptkind != RELOPT_OTHER_MEMBER_REL)
3114 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3116 rte = root->simple_rte_array[relid];
3119 /* We don't hint on other than relation and foreign tables */
3120 if (rte->rtekind != RTE_RELATION ||
3121 rte->relkind == RELKIND_FOREIGN_TABLE)
3124 /* Find scan method hint, which matches given names, from the list. */
3125 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
3127 ScanMethodHint *hint = current_hint_state->scan_hints[i];
3129 /* We ignore disabled hints. */
3130 if (!hint_state_enabled(hint))
3134 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3137 /* check the real name for appendrel children */
3138 if (!real_name_hint &&
3139 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3141 char *realname = get_rel_name(rte->relid);
3143 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3144 real_name_hint = hint;
3147 /* No more match expected, break */
3148 if(alias_hint && real_name_hint)
3152 /* real name match precedes alias match */
3154 return real_name_hint;
3159 static ParallelHint *
3160 find_parallel_hint(PlannerInfo *root, Index relid)
3164 ParallelHint *real_name_hint = NULL;
3165 ParallelHint *alias_hint = NULL;
3168 /* This should not be a join rel */
3170 rel = root->simple_rel_array[relid];
3173 * Parallel planning is appliable only on base relation, which has
3180 * We have set root->glob->parallelModeOK if needed. What we should do here
3181 * is just following the decision of planner.
3183 if (!rel->consider_parallel)
3187 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3189 rte = root->simple_rte_array[relid];
3192 /* Find parallel method hint, which matches given names, from the list. */
3193 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_PARALLEL]; i++)
3195 ParallelHint *hint = current_hint_state->parallel_hints[i];
3197 /* We ignore disabled hints. */
3198 if (!hint_state_enabled(hint))
3202 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3205 /* check the real name for appendrel children */
3206 if (!real_name_hint &&
3207 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3209 char *realname = get_rel_name(rte->relid);
3211 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3212 real_name_hint = hint;
3215 /* No more match expected, break */
3216 if(alias_hint && real_name_hint)
3220 /* real name match precedes alias match */
3222 return real_name_hint;
3230 * Returns TRUE on match, FALSE on no match.
3232 * s1 --- the data to match against
3233 * s2 --- the pattern
3235 * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
3238 regexpeq(const char *s1, const char *s2)
3244 strcpy(name.data, s1);
3245 regexp = cstring_to_text(s2);
3247 result = DirectFunctionCall2Coll(nameregexeq,
3248 DEFAULT_COLLATION_OID,
3249 NameGetDatum(&name),
3250 PointerGetDatum(regexp));
3251 return DatumGetBool(result);
3255 /* Remove indexes instructed not to use by hint. */
3257 restrict_indexes(PlannerInfo *root, ScanMethodHint *hint, RelOptInfo *rel,
3258 bool using_parent_hint)
3264 RangeTblEntry *rte = root->simple_rte_array[rel->relid];
3265 Oid relationObjectId = rte->relid;
3268 * We delete all the IndexOptInfo list and prevent you from being usable by
3271 if (hint->enforce_mask == ENABLE_SEQSCAN ||
3272 hint->enforce_mask == ENABLE_TIDSCAN)
3274 list_free_deep(rel->indexlist);
3275 rel->indexlist = NIL;
3276 hint->base.state = HINT_STATE_USED;
3282 * When a list of indexes is not specified, we just use all indexes.
3284 if (hint->indexnames == NIL)
3288 * Leaving only an specified index, we delete it from a IndexOptInfo list
3292 if (debug_level > 0)
3293 initStringInfo(&buf);
3295 for (cell = list_head(rel->indexlist); cell; cell = next)
3297 IndexOptInfo *info = (IndexOptInfo *) lfirst(cell);
3298 char *indexname = get_rel_name(info->indexoid);
3300 bool use_index = false;
3304 foreach(l, hint->indexnames)
3306 char *hintname = (char *) lfirst(l);
3310 result = regexpeq(indexname, hintname);
3312 result = RelnameCmp(&indexname, &hintname) == 0;
3317 if (debug_level > 0)
3319 appendStringInfoCharMacro(&buf, ' ');
3320 quote_value(&buf, indexname);
3328 * Apply index restriction of parent hint to children. Since index
3329 * inheritance is not explicitly described we should search for an
3330 * children's index with the same definition to that of the parent.
3332 if (using_parent_hint && !use_index)
3334 foreach(l, current_hint_state->parent_index_infos)
3338 ParentIndexInfo *p_info = (ParentIndexInfo *)lfirst(l);
3341 * we check the 'same' index by comparing uniqueness, access
3342 * method and index key columns.
3344 if (p_info->indisunique != info->unique ||
3345 p_info->method != info->relam ||
3346 list_length(p_info->column_names) != info->ncolumns)
3349 /* Check if index key columns match */
3350 for (i = 0; i < info->ncolumns; i++)
3352 char *c_attname = NULL;
3353 char *p_attname = NULL;
3355 p_attname = list_nth(p_info->column_names, i);
3358 * if both of the key of the same position are expressions,
3359 * ignore them for now and check later.
3361 if (info->indexkeys[i] == 0 && !p_attname)
3364 /* deny if one is expression while another is not */
3365 if (info->indexkeys[i] == 0 || !p_attname)
3368 c_attname = get_attname(relationObjectId,
3369 info->indexkeys[i]);
3371 /* deny if any of column attributes don't match */
3372 if (strcmp(p_attname, c_attname) != 0 ||
3373 p_info->indcollation[i] != info->indexcollations[i] ||
3374 p_info->opclass[i] != info->opcintype[i]||
3375 ((p_info->indoption[i] & INDOPTION_DESC) != 0)
3376 != info->reverse_sort[i] ||
3377 ((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0)
3378 != info->nulls_first[i])
3382 /* deny this if any difference found */
3383 if (i != info->ncolumns)
3386 /* check on key expressions */
3387 if ((p_info->expression_str && (info->indexprs != NIL)) ||
3388 (p_info->indpred_str && (info->indpred != NIL)))
3390 /* fetch the index of this child */
3391 ht_idx = SearchSysCache1(INDEXRELID,
3392 ObjectIdGetDatum(info->indexoid));
3394 /* check expressions if both expressions are available */
3395 if (p_info->expression_str &&
3396 !heap_attisnull(ht_idx, Anum_pg_index_indexprs))
3403 * to change the expression's parameter of child's
3406 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3407 Anum_pg_index_indexprs,
3410 result = DirectFunctionCall2(pg_get_expr,
3415 /* deny if expressions don't match */
3416 if (strcmp(p_info->expression_str,
3417 text_to_cstring(DatumGetTextP(result))) != 0)
3420 ReleaseSysCache(ht_idx);
3425 /* compare index predicates */
3426 if (p_info->indpred_str &&
3427 !heap_attisnull(ht_idx, Anum_pg_index_indpred))
3433 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3434 Anum_pg_index_indpred,
3437 result = DirectFunctionCall2(pg_get_expr,
3442 if (strcmp(p_info->indpred_str,
3443 text_to_cstring(DatumGetTextP(result))) != 0)
3446 ReleaseSysCache(ht_idx);
3452 ReleaseSysCache(ht_idx);
3454 else if (p_info->expression_str || (info->indexprs != NIL))
3456 else if (p_info->indpred_str || (info->indpred != NIL))
3461 /* to log the candidate of index */
3462 if (debug_level > 0)
3464 appendStringInfoCharMacro(&buf, ' ');
3465 quote_value(&buf, indexname);
3473 rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
3480 if (debug_level == 1)
3482 StringInfoData rel_buf;
3483 char *disprelname = "";
3486 * If this hint targetted the parent, use the real name of this
3487 * child. Otherwise use hint specification.
3489 if (using_parent_hint)
3490 disprelname = get_rel_name(rte->relid);
3492 disprelname = hint->relname;
3495 initStringInfo(&rel_buf);
3496 quote_value(&rel_buf, disprelname);
3498 ereport(pg_hint_plan_debug_message_level,
3499 (errmsg("available indexes for %s(%s):%s",
3504 pfree(rel_buf.data);
3509 * Return information of index definition.
3511 static ParentIndexInfo *
3512 get_parent_index_info(Oid indexoid, Oid relid)
3514 ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
3515 Relation indexRelation;
3516 Form_pg_index index;
3520 indexRelation = index_open(indexoid, RowExclusiveLock);
3522 index = indexRelation->rd_index;
3524 p_info->indisunique = index->indisunique;
3525 p_info->method = indexRelation->rd_rel->relam;
3527 p_info->column_names = NIL;
3528 p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3529 p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3530 p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
3532 for (i = 0; i < index->indnatts; i++)
3534 attname = get_attname(relid, index->indkey.values[i]);
3535 p_info->column_names = lappend(p_info->column_names, attname);
3537 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
3538 p_info->opclass[i] = indexRelation->rd_opcintype[i];
3539 p_info->indoption[i] = indexRelation->rd_indoption[i];
3543 * to check to match the expression's parameter of index with child indexes
3545 p_info->expression_str = NULL;
3546 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs))
3552 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3553 Anum_pg_index_indexprs, &isnull);
3555 result = DirectFunctionCall2(pg_get_expr,
3557 ObjectIdGetDatum(relid));
3559 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
3563 * to check to match the predicate's parameter of index with child indexes
3565 p_info->indpred_str = NULL;
3566 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred))
3572 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3573 Anum_pg_index_indpred, &isnull);
3575 result = DirectFunctionCall2(pg_get_expr,
3577 ObjectIdGetDatum(relid));
3579 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
3582 index_close(indexRelation, NoLock);
3588 * cancel hint enforcement
3591 reset_hint_enforcement()
3593 setup_scan_method_enforcement(NULL, current_hint_state);
3594 setup_parallel_plan_enforcement(NULL, current_hint_state);
3598 * Set planner guc parameters according to corresponding scan hints. Returns
3599 * bitmap of HintTypeBitmap. If shint or phint is not NULL, set used hint
3600 * there respectively.
3603 setup_hint_enforcement(PlannerInfo *root, RelOptInfo *rel,
3604 ScanMethodHint **rshint, ParallelHint **rphint)
3606 Index new_parent_relid = 0;
3608 ScanMethodHint *shint = NULL;
3609 ParallelHint *phint = NULL;
3610 bool inhparent = root->simple_rte_array[rel->relid]->inh;
3611 Oid relationObjectId = root->simple_rte_array[rel->relid]->relid;
3614 /* reset returns if requested */
3615 if (rshint != NULL) *rshint = NULL;
3616 if (rphint != NULL) *rphint = NULL;
3619 * We could register the parent relation of the following children here
3620 * when inhparent == true but inheritnce planner doesn't call this function
3621 * for parents. Since we cannot distinguish who called this function we
3622 * cannot do other than always seeking the parent regardless of who called
3627 if (debug_level > 1)
3628 ereport(pg_hint_plan_debug_message_level,
3630 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3631 " skipping inh parent: relation=%u(%s), inhparent=%d,"
3632 " current_hint_state=%p, hint_inhibit_level=%d",
3633 qnostr, relationObjectId,
3634 get_rel_name(relationObjectId),
3635 inhparent, current_hint_state, hint_inhibit_level)));
3639 /* Forget about the parent of another subquery */
3640 if (root != current_hint_state->current_root)
3641 current_hint_state->parent_relid = 0;
3643 /* Find the parent for this relation other than the registered parent */
3644 foreach (l, root->append_rel_list)
3646 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3648 if (appinfo->child_relid == rel->relid)
3650 if (current_hint_state->parent_relid != appinfo->parent_relid)
3652 new_parent_relid = appinfo->parent_relid;
3653 current_hint_state->current_root = root;
3661 /* This relation doesn't have a parent. Cancel current_hint_state. */
3662 current_hint_state->parent_relid = 0;
3663 current_hint_state->parent_scan_hint = NULL;
3664 current_hint_state->parent_parallel_hint = NULL;
3667 if (new_parent_relid > 0)
3670 * Here we found a new parent for the current relation. Scan continues
3671 * hint to other childrens of this parent so remember it * to avoid
3672 * hinthintredundant setup cost.
3674 current_hint_state->parent_relid = new_parent_relid;
3676 /* Find hints for the parent */
3677 current_hint_state->parent_scan_hint =
3678 find_scan_hint(root, current_hint_state->parent_relid);
3680 current_hint_state->parent_parallel_hint =
3681 find_parallel_hint(root, current_hint_state->parent_relid);
3684 * If hint is found for the parent, apply it for this child instead
3687 if (current_hint_state->parent_scan_hint)
3689 ScanMethodHint * pshint = current_hint_state->parent_scan_hint;
3691 pshint->base.state = HINT_STATE_USED;
3693 /* Apply index mask in the same manner to the parent. */
3694 if (pshint->indexnames)
3697 Relation parent_rel;
3700 root->simple_rte_array[current_hint_state->parent_relid]->relid;
3701 parent_rel = heap_open(parentrel_oid, NoLock);
3703 /* Search the parent relation for indexes match the hint spec */
3704 foreach(l, RelationGetIndexList(parent_rel))
3706 Oid indexoid = lfirst_oid(l);
3707 char *indexname = get_rel_name(indexoid);
3709 ParentIndexInfo *parent_index_info;
3711 foreach(lc, pshint->indexnames)
3713 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
3720 get_parent_index_info(indexoid, parentrel_oid);
3721 current_hint_state->parent_index_infos =
3722 lappend(current_hint_state->parent_index_infos,
3725 heap_close(parent_rel, NoLock);
3730 shint = find_scan_hint(root, rel->relid);
3732 shint = current_hint_state->parent_scan_hint;
3736 bool using_parent_hint =
3737 (shint == current_hint_state->parent_scan_hint);
3739 ret |= HINT_BM_SCAN_METHOD;
3741 /* Setup scan enforcement environment */
3742 setup_scan_method_enforcement(shint, current_hint_state);
3744 /* restrict unwanted inexes */
3745 restrict_indexes(root, shint, rel, using_parent_hint);
3747 if (debug_level > 1)
3749 char *additional_message = "";
3751 if (shint == current_hint_state->parent_scan_hint)
3752 additional_message = " by parent hint";
3754 ereport(pg_hint_plan_debug_message_level,
3756 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3757 " index deletion%s:"
3758 " relation=%u(%s), inhparent=%d, "
3759 "current_hint_state=%p,"
3760 " hint_inhibit_level=%d, scanmask=0x%x",
3761 qnostr, additional_message,
3763 get_rel_name(relationObjectId),
3764 inhparent, current_hint_state,
3766 shint->enforce_mask)));
3770 /* Do the same for parallel plan enforcement */
3771 phint = find_parallel_hint(root, rel->relid);
3773 phint = current_hint_state->parent_parallel_hint;
3775 setup_parallel_plan_enforcement(phint, current_hint_state);
3778 ret |= HINT_BM_PARALLEL;
3780 /* Nothing to apply. Reset the scan mask to intial state */
3781 if (!shint && ! phint)
3783 if (debug_level > 1)
3784 ereport(pg_hint_plan_debug_message_level,
3785 (errhidestmt (true),
3786 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3788 " relation=%u(%s), inhparent=%d, current_hint=%p,"
3789 " hint_inhibit_level=%d, scanmask=0x%x",
3790 qnostr, relationObjectId,
3791 get_rel_name(relationObjectId),
3792 inhparent, current_hint_state, hint_inhibit_level,
3793 current_hint_state->init_scan_mask)));
3795 setup_scan_method_enforcement(NULL, current_hint_state);
3800 if (rshint != NULL) *rshint = shint;
3801 if (rphint != NULL) *rphint = phint;
3807 * Return index of relation which matches given aliasname, or 0 if not found.
3808 * If same aliasname was used multiple times in a query, return -1.
3811 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
3817 for (i = 1; i < root->simple_rel_array_size; i++)
3821 if (root->simple_rel_array[i] == NULL)
3824 Assert(i == root->simple_rel_array[i]->relid);
3826 if (RelnameCmp(&aliasname,
3827 &root->simple_rte_array[i]->eref->aliasname) != 0)
3830 foreach(l, initial_rels)
3832 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3834 if (rel->reloptkind == RELOPT_BASEREL)
3836 if (rel->relid != i)
3841 Assert(rel->reloptkind == RELOPT_JOINREL);
3843 if (!bms_is_member(i, rel->relids))
3850 ("Relation name \"%s\" is ambiguous.",
3865 * Return join hint which matches given joinrelids.
3867 static JoinMethodHint *
3868 find_join_hint(Relids joinrelids)
3873 join_hint = current_hint_state->join_hint_level[bms_num_members(joinrelids)];
3875 foreach(l, join_hint)
3877 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
3879 if (bms_equal(joinrelids, hint->joinrelids))
3887 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
3888 PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
3890 OuterInnerRels *outer_rels;
3891 OuterInnerRels *inner_rels;
3892 Relids outer_relids;
3893 Relids inner_relids;
3895 JoinMethodHint *hint;
3897 if (outer_inner->relation != NULL)
3899 return bms_make_singleton(
3900 find_relid_aliasname(root, outer_inner->relation,
3902 leading_hint->base.hint_str));
3905 outer_rels = lfirst(outer_inner->outer_inner_pair->head);
3906 inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
3908 outer_relids = OuterInnerJoinCreate(outer_rels,
3914 inner_relids = OuterInnerJoinCreate(inner_rels,
3921 join_relids = bms_add_members(outer_relids, inner_relids);
3923 if (bms_num_members(join_relids) > nbaserel)
3927 * If we don't have join method hint, create new one for the
3928 * join combination with all join methods are enabled.
3930 hint = find_join_hint(join_relids);
3934 * Here relnames is not set, since Relids bitmap is sufficient to
3935 * control paths of this query afterward.
3937 hint = (JoinMethodHint *) JoinMethodHintCreate(
3938 leading_hint->base.hint_str,
3940 HINT_KEYWORD_LEADING);
3941 hint->base.state = HINT_STATE_USED;
3942 hint->nrels = bms_num_members(join_relids);
3943 hint->enforce_mask = ENABLE_ALL_JOIN;
3944 hint->joinrelids = bms_copy(join_relids);
3945 hint->inner_nrels = bms_num_members(inner_relids);
3946 hint->inner_joinrelids = bms_copy(inner_relids);
3948 hstate->join_hint_level[hint->nrels] =
3949 lappend(hstate->join_hint_level[hint->nrels], hint);
3953 hint->inner_nrels = bms_num_members(inner_relids);
3954 hint->inner_joinrelids = bms_copy(inner_relids);
3961 create_bms_of_relids(Hint *base, PlannerInfo *root, List *initial_rels,
3962 int nrels, char **relnames)
3965 Relids relids = NULL;
3969 for (j = 0; j < nrels; j++)
3971 relname = relnames[j];
3973 relid = find_relid_aliasname(root, relname, initial_rels,
3977 base->state = HINT_STATE_ERROR;
3980 * the aliasname is not found(relid == 0) or same aliasname was used
3981 * multiple times in a query(relid == -1)
3988 if (bms_is_member(relid, relids))
3990 hint_ereport(base->hint_str,
3991 ("Relation name \"%s\" is duplicated.", relname));
3992 base->state = HINT_STATE_ERROR;
3996 relids = bms_add_member(relids, relid);
4001 * Transform join method hint into handy form.
4003 * - create bitmap of relids from alias names, to make it easier to check
4004 * whether a join path matches a join method hint.
4005 * - add join method hints which are necessary to enforce join order
4006 * specified by Leading hint
4009 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
4010 List *initial_rels, JoinMethodHint **join_method_hints)
4018 LeadingHint *lhint = NULL;
4021 * Create bitmap of relids from alias names for each join method hint.
4022 * Bitmaps are more handy than strings in join searching.
4024 for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
4026 JoinMethodHint *hint = hstate->join_hints[i];
4028 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4031 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4032 initial_rels, hint->nrels, hint->relnames);
4034 if (hint->joinrelids == NULL || hint->base.state == HINT_STATE_ERROR)
4037 hstate->join_hint_level[hint->nrels] =
4038 lappend(hstate->join_hint_level[hint->nrels], hint);
4042 * Create bitmap of relids from alias names for each rows hint.
4043 * Bitmaps are more handy than strings in join searching.
4045 for (i = 0; i < hstate->num_hints[HINT_TYPE_ROWS]; i++)
4047 RowsHint *hint = hstate->rows_hints[i];
4049 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4052 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4053 initial_rels, hint->nrels, hint->relnames);
4056 /* Do nothing if no Leading hint was supplied. */
4057 if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
4061 * Decide whether to use Leading hint
4063 for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
4065 LeadingHint *leading_hint = (LeadingHint *)hstate->leading_hint[i];
4068 if (leading_hint->base.state == HINT_STATE_ERROR)
4074 foreach(l, leading_hint->relations)
4076 relname = (char *)lfirst(l);;
4078 relid = find_relid_aliasname(root, relname, initial_rels,
4079 leading_hint->base.hint_str);
4081 leading_hint->base.state = HINT_STATE_ERROR;
4086 if (bms_is_member(relid, relids))
4088 hint_ereport(leading_hint->base.hint_str,
4089 ("Relation name \"%s\" is duplicated.", relname));
4090 leading_hint->base.state = HINT_STATE_ERROR;
4094 relids = bms_add_member(relids, relid);
4097 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
4102 hint_ereport(lhint->base.hint_str,
4103 ("Conflict %s hint.", HintTypeName[lhint->base.type]));
4104 lhint->base.state = HINT_STATE_DUPLICATION;
4106 leading_hint->base.state = HINT_STATE_USED;
4107 lhint = leading_hint;
4110 /* check to exist Leading hint marked with 'used'. */
4115 * We need join method hints which fit specified join order in every join
4116 * level. For example, Leading(A B C) virtually requires following join
4117 * method hints, if no join method hint supplied:
4119 * - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
4120 * - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
4122 * If we already have join method hint which fits specified join order in
4123 * that join level, we leave it as-is and don't add new hints.
4127 if (lhint->outer_inner == NULL)
4129 foreach(l, lhint->relations)
4131 JoinMethodHint *hint;
4133 relname = (char *)lfirst(l);
4136 * Find relid of the relation which has given name. If we have the
4137 * name given in Leading hint multiple times in the join, nothing to
4140 relid = find_relid_aliasname(root, relname, initial_rels,
4143 /* Create bitmap of relids for current join level. */
4144 joinrelids = bms_add_member(joinrelids, relid);
4147 /* We never have join method hint for single relation. */
4152 * If we don't have join method hint, create new one for the
4153 * join combination with all join methods are enabled.
4155 hint = find_join_hint(joinrelids);
4159 * Here relnames is not set, since Relids bitmap is sufficient
4160 * to control paths of this query afterward.
4162 hint = (JoinMethodHint *) JoinMethodHintCreate(
4163 lhint->base.hint_str,
4165 HINT_KEYWORD_LEADING);
4166 hint->base.state = HINT_STATE_USED;
4167 hint->nrels = njoinrels;
4168 hint->enforce_mask = ENABLE_ALL_JOIN;
4169 hint->joinrelids = bms_copy(joinrelids);
4172 join_method_hints[njoinrels] = hint;
4174 if (njoinrels >= nbaserel)
4177 bms_free(joinrelids);
4183 * Delete all join hints which have different combination from Leading
4186 for (i = 2; i <= njoinrels; i++)
4188 list_free(hstate->join_hint_level[i]);
4190 hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
4195 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
4202 njoinrels = bms_num_members(joinrelids);
4203 Assert(njoinrels >= 2);
4206 * Delete all join hints which have different combination from Leading
4209 for (i = 2;i <= njoinrels; i++)
4211 if (hstate->join_hint_level[i] != NIL)
4213 ListCell *prev = NULL;
4214 ListCell *next = NULL;
4215 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
4218 JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
4222 if (hint->inner_nrels == 0 &&
4223 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
4224 bms_equal(bms_union(hint->joinrelids, joinrelids),
4227 hstate->join_hint_level[i] =
4228 list_delete_cell(hstate->join_hint_level[i], l,
4237 bms_free(joinrelids);
4240 if (hint_state_enabled(lhint))
4242 set_join_config_options(DISABLE_ALL_JOIN, current_hint_state->context);
4249 * wrapper of make_join_rel()
4251 * call make_join_rel() after changing enable_* parameters according to given
4255 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
4258 JoinMethodHint *hint;
4262 joinrelids = bms_union(rel1->relids, rel2->relids);
4263 hint = find_join_hint(joinrelids);
4264 bms_free(joinrelids);
4267 return pg_hint_plan_make_join_rel(root, rel1, rel2);
4269 if (hint->inner_nrels == 0)
4271 save_nestlevel = NewGUCNestLevel();
4273 set_join_config_options(hint->enforce_mask,
4274 current_hint_state->context);
4276 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4277 hint->base.state = HINT_STATE_USED;
4280 * Restore the GUC variables we set above.
4282 AtEOXact_GUC(true, save_nestlevel);
4285 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4294 add_paths_to_joinrel_wrapper(PlannerInfo *root,
4295 RelOptInfo *joinrel,
4296 RelOptInfo *outerrel,
4297 RelOptInfo *innerrel,
4299 SpecialJoinInfo *sjinfo,
4303 JoinMethodHint *join_hint;
4306 joinrelids = bms_union(outerrel->relids, innerrel->relids);
4307 join_hint = find_join_hint(joinrelids);
4308 bms_free(joinrelids);
4310 if (join_hint && join_hint->inner_nrels != 0)
4312 save_nestlevel = NewGUCNestLevel();
4314 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
4317 set_join_config_options(join_hint->enforce_mask,
4318 current_hint_state->context);
4320 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4321 sjinfo, restrictlist);
4322 join_hint->base.state = HINT_STATE_USED;
4326 set_join_config_options(DISABLE_ALL_JOIN,
4327 current_hint_state->context);
4328 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4329 sjinfo, restrictlist);
4333 * Restore the GUC variables we set above.
4335 AtEOXact_GUC(true, save_nestlevel);
4338 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4339 sjinfo, restrictlist);
4343 get_num_baserels(List *initial_rels)
4348 foreach(l, initial_rels)
4350 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
4352 if (rel->reloptkind == RELOPT_BASEREL)
4354 else if (rel->reloptkind ==RELOPT_JOINREL)
4355 nbaserel+= bms_num_members(rel->relids);
4358 /* other values not expected here */
4359 elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
4367 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
4370 JoinMethodHint **join_method_hints;
4374 bool leading_hint_enable;
4377 * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
4378 * valid hint is supplied or current nesting depth is nesting depth of SPI
4381 if (!current_hint_state || hint_inhibit_level > 0)
4383 if (prev_join_search)
4384 return (*prev_join_search) (root, levels_needed, initial_rels);
4385 else if (enable_geqo && levels_needed >= geqo_threshold)
4386 return geqo(root, levels_needed, initial_rels);
4388 return standard_join_search(root, levels_needed, initial_rels);
4392 * In the case using GEQO, only scan method hints and Set hints have
4393 * effect. Join method and join order is not controllable by hints.
4395 if (enable_geqo && levels_needed >= geqo_threshold)
4396 return geqo(root, levels_needed, initial_rels);
4398 nbaserel = get_num_baserels(initial_rels);
4399 current_hint_state->join_hint_level =
4400 palloc0(sizeof(List *) * (nbaserel + 1));
4401 join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
4403 leading_hint_enable = transform_join_hints(current_hint_state,
4405 initial_rels, join_method_hints);
4407 rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
4409 for (i = 2; i <= nbaserel; i++)
4411 list_free(current_hint_state->join_hint_level[i]);
4413 /* free Leading hint only */
4414 if (join_method_hints[i] != NULL &&
4415 join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
4416 JoinMethodHintDelete(join_method_hints[i]);
4418 pfree(current_hint_state->join_hint_level);
4419 pfree(join_method_hints);
4421 if (leading_hint_enable)
4422 set_join_config_options(current_hint_state->init_join_mask,
4423 current_hint_state->context);
4429 * Force number of wokers if instructed by hint
4432 pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
4433 Index rti, RangeTblEntry *rte)
4435 ParallelHint *phint;
4439 /* call the previous hook */
4440 if (prev_set_rel_pathlist)
4441 prev_set_rel_pathlist(root, rel, rti, rte);
4443 /* Nothing to do if no hint available */
4444 if (current_hint_state == NULL)
4447 /* Don't touch dummy rels. */
4448 if (IS_DUMMY_REL(rel))
4452 * We can accept only plain relations, foreign tables and table saples are
4453 * also unacceptable. See set_rel_pathlist.
4455 if (rel->rtekind != RTE_RELATION ||
4456 rte->relkind == RELKIND_FOREIGN_TABLE ||
4457 rte->tablesample != NULL)
4460 /* We cannot handle if this requires an outer */
4461 if (rel->lateral_relids)
4464 /* Return if this relation gets no enfocement */
4465 if ((found_hints = setup_hint_enforcement(root, rel, NULL, &phint)) == 0)
4468 /* Here, we regenerate paths with the current hint restriction */
4469 if (found_hints & HINT_BM_SCAN_METHOD || found_hints & HINT_BM_PARALLEL)
4471 /* Just discard all the paths considered so far */
4472 list_free_deep(rel->pathlist);
4473 rel->pathlist = NIL;
4475 /* Remove all the partial paths if Parallel hint is specfied */
4476 if ((found_hints & HINT_BM_PARALLEL) && rel->partial_pathlist)
4478 list_free_deep(rel->partial_pathlist);
4479 rel->partial_pathlist = NIL;
4482 /* Regenerate paths with the current enforcement */
4483 set_plain_rel_pathlist(root, rel, rte);
4485 /* Additional work to enforce parallel query execution */
4486 if (phint && phint->nworkers > 0)
4488 /* Lower the priorities of non-parallel paths */
4489 foreach (l, rel->pathlist)
4491 Path *path = (Path *) lfirst(l);
4493 if (path->startup_cost < disable_cost)
4495 path->startup_cost += disable_cost;
4496 path->total_cost += disable_cost;
4500 /* enforce number of workers if requested */
4501 if (phint->force_parallel)
4503 foreach (l, rel->partial_pathlist)
4505 Path *ppath = (Path *) lfirst(l);
4507 ppath->parallel_workers = phint->nworkers;
4511 /* Generate gather paths for base rels */
4512 if (rel->reloptkind == RELOPT_BASEREL)
4513 generate_gather_paths(root, rel);
4517 reset_hint_enforcement();
4522 * Build access paths for a base relation
4524 * This function was copied and edited from set_rel_pathlist() in
4525 * src/backend/optimizer/path/allpaths.c in order not to copy other static
4526 * functions not required here.
4529 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
4530 Index rti, RangeTblEntry *rte)
4532 if (IS_DUMMY_REL(rel))
4534 /* We already proved the relation empty, so nothing more to do */
4538 /* It's an "append relation", process accordingly */
4539 set_append_rel_pathlist(root, rel, rti, rte);
4543 if (rel->rtekind == RTE_RELATION)
4545 if (rte->relkind == RELKIND_RELATION)
4547 if(rte->tablesample != NULL)
4548 elog(ERROR, "sampled relation is not supported");
4550 /* Plain relation */
4551 set_plain_rel_pathlist(root, rel, rte);
4554 elog(ERROR, "unexpected relkind: %c", rte->relkind);
4557 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
4561 * Allow a plugin to editorialize on the set of Paths for this base
4562 * relation. It could add new paths (such as CustomPaths) by calling
4563 * add_path(), or delete or modify paths added by the core code.
4565 if (set_rel_pathlist_hook)
4566 (*set_rel_pathlist_hook) (root, rel, rti, rte);
4568 /* Now find the cheapest of the paths for this rel */
4573 * stmt_beg callback is called when each query in PL/pgSQL function is about
4574 * to be executed. At that timing, we save query string in the global variable
4575 * plpgsql_query_string to use it in planner hook. It's safe to use one global
4576 * variable for the purpose, because its content is only necessary until
4577 * planner hook is called for the query, so recursive PL/pgSQL function calls
4578 * don't harm this mechanism.
4581 pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4583 plpgsql_recurse_level++;
4587 * stmt_end callback is called then each query in PL/pgSQL function has
4588 * finished. At that timing, we clear plpgsql_query_string to tell planner
4589 * hook that next call is not for a query written in PL/pgSQL block.
4592 pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4594 plpgsql_recurse_level--;
4597 void plpgsql_query_erase_callback(ResourceReleasePhase phase,
4602 if (!isTopLevel || phase != RESOURCE_RELEASE_AFTER_LOCKS)
4604 /* Cancel plpgsql nest level*/
4605 plpgsql_recurse_level = 0;
4608 #define standard_join_search pg_hint_plan_standard_join_search
4609 #define join_search_one_level pg_hint_plan_join_search_one_level
4610 #define make_join_rel make_join_rel_wrapper
4613 #undef make_join_rel
4614 #define make_join_rel pg_hint_plan_make_join_rel
4615 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
4616 #include "make_join_rel.c"
4618 #include "pg_stat_statements.c"