1 /*-------------------------------------------------------------------------
4 * hinting on how to execute a query for PostgreSQL
6 * Copyright (c) 2012-2018, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
8 *-------------------------------------------------------------------------
13 #include "catalog/pg_collation.h"
14 #include "catalog/pg_index.h"
15 #include "commands/prepare.h"
16 #include "mb/pg_wchar.h"
17 #include "miscadmin.h"
18 #include "nodes/nodeFuncs.h"
19 #include "nodes/params.h"
20 #include "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 "partitioning/partbounds.h"
35 #include "tcop/utility.h"
36 #include "utils/builtins.h"
37 #include "utils/lsyscache.h"
38 #include "utils/memutils.h"
39 #include "utils/rel.h"
40 #include "utils/snapmgr.h"
41 #include "utils/syscache.h"
42 #include "utils/resowner.h"
44 #include "catalog/pg_class.h"
46 #include "executor/spi.h"
47 #include "catalog/pg_type.h"
51 /* partially copied from pg_stat_statements */
52 #include "normalize_query.h"
55 #include "access/htup_details.h"
57 #ifdef PG_MODULE_MAGIC
61 #define BLOCK_COMMENT_START "/*"
62 #define BLOCK_COMMENT_END "*/"
63 #define HINT_COMMENT_KEYWORD "+"
64 #define HINT_START BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
65 #define HINT_END BLOCK_COMMENT_END
68 #define HINT_SEQSCAN "SeqScan"
69 #define HINT_INDEXSCAN "IndexScan"
70 #define HINT_INDEXSCANREGEXP "IndexScanRegexp"
71 #define HINT_BITMAPSCAN "BitmapScan"
72 #define HINT_BITMAPSCANREGEXP "BitmapScanRegexp"
73 #define HINT_TIDSCAN "TidScan"
74 #define HINT_NOSEQSCAN "NoSeqScan"
75 #define HINT_NOINDEXSCAN "NoIndexScan"
76 #define HINT_NOBITMAPSCAN "NoBitmapScan"
77 #define HINT_NOTIDSCAN "NoTidScan"
78 #define HINT_INDEXONLYSCAN "IndexOnlyScan"
79 #define HINT_INDEXONLYSCANREGEXP "IndexOnlyScanRegexp"
80 #define HINT_NOINDEXONLYSCAN "NoIndexOnlyScan"
81 #define HINT_PARALLEL "Parallel"
83 #define HINT_NESTLOOP "NestLoop"
84 #define HINT_MERGEJOIN "MergeJoin"
85 #define HINT_HASHJOIN "HashJoin"
86 #define HINT_NONESTLOOP "NoNestLoop"
87 #define HINT_NOMERGEJOIN "NoMergeJoin"
88 #define HINT_NOHASHJOIN "NoHashJoin"
89 #define HINT_LEADING "Leading"
90 #define HINT_SET "Set"
91 #define HINT_ROWS "Rows"
93 #define HINT_ARRAY_DEFAULT_INITSIZE 8
95 #define hint_ereport(str, detail) hint_parse_ereport(str, detail)
96 #define hint_parse_ereport(str, detail) \
98 ereport(pg_hint_plan_parse_message_level, \
99 (errmsg("pg_hint_plan: hint syntax error at or near \"%s\"", (str)), \
100 errdetail detail)); \
103 #define skip_space(str) \
104 while (isspace(*str)) \
109 ENABLE_SEQSCAN = 0x01,
110 ENABLE_INDEXSCAN = 0x02,
111 ENABLE_BITMAPSCAN = 0x04,
112 ENABLE_TIDSCAN = 0x08,
113 ENABLE_INDEXONLYSCAN = 0x10
118 ENABLE_NESTLOOP = 0x01,
119 ENABLE_MERGEJOIN = 0x02,
120 ENABLE_HASHJOIN = 0x04
123 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
124 ENABLE_BITMAPSCAN | ENABLE_TIDSCAN | \
125 ENABLE_INDEXONLYSCAN)
126 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
127 #define DISABLE_ALL_SCAN 0
128 #define DISABLE_ALL_JOIN 0
130 /* hint keyword of enum type*/
131 typedef enum HintKeyword
133 HINT_KEYWORD_SEQSCAN,
134 HINT_KEYWORD_INDEXSCAN,
135 HINT_KEYWORD_INDEXSCANREGEXP,
136 HINT_KEYWORD_BITMAPSCAN,
137 HINT_KEYWORD_BITMAPSCANREGEXP,
138 HINT_KEYWORD_TIDSCAN,
139 HINT_KEYWORD_NOSEQSCAN,
140 HINT_KEYWORD_NOINDEXSCAN,
141 HINT_KEYWORD_NOBITMAPSCAN,
142 HINT_KEYWORD_NOTIDSCAN,
143 HINT_KEYWORD_INDEXONLYSCAN,
144 HINT_KEYWORD_INDEXONLYSCANREGEXP,
145 HINT_KEYWORD_NOINDEXONLYSCAN,
147 HINT_KEYWORD_NESTLOOP,
148 HINT_KEYWORD_MERGEJOIN,
149 HINT_KEYWORD_HASHJOIN,
150 HINT_KEYWORD_NONESTLOOP,
151 HINT_KEYWORD_NOMERGEJOIN,
152 HINT_KEYWORD_NOHASHJOIN,
154 HINT_KEYWORD_LEADING,
157 HINT_KEYWORD_PARALLEL,
159 HINT_KEYWORD_UNRECOGNIZED
162 #define SCAN_HINT_ACCEPTS_INDEX_NAMES(kw) \
163 (kw == HINT_KEYWORD_INDEXSCAN || \
164 kw == HINT_KEYWORD_INDEXSCANREGEXP || \
165 kw == HINT_KEYWORD_INDEXONLYSCAN || \
166 kw == HINT_KEYWORD_INDEXONLYSCANREGEXP || \
167 kw == HINT_KEYWORD_BITMAPSCAN || \
168 kw == HINT_KEYWORD_BITMAPSCANREGEXP)
171 typedef struct Hint Hint;
172 typedef struct HintState HintState;
174 typedef Hint *(*HintCreateFunction) (const char *hint_str,
176 HintKeyword hint_keyword);
177 typedef void (*HintDeleteFunction) (Hint *hint);
178 typedef void (*HintDescFunction) (Hint *hint, StringInfo buf, bool nolf);
179 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
180 typedef const char *(*HintParseFunction) (Hint *hint, HintState *hstate,
181 Query *parse, const char *str);
184 #define NUM_HINT_TYPE 6
185 typedef enum HintType
187 HINT_TYPE_SCAN_METHOD,
188 HINT_TYPE_JOIN_METHOD,
195 typedef enum HintTypeBitmap
197 HINT_BM_SCAN_METHOD = 1,
201 static const char *HintTypeName[] = {
211 typedef enum HintStatus
213 HINT_STATE_NOTUSED = 0, /* specified relation not used in query */
214 HINT_STATE_USED, /* hint is used */
215 HINT_STATE_DUPLICATION, /* specified hint duplication */
216 HINT_STATE_ERROR /* execute error (parse error does not include
220 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
221 (hint)->base.state == HINT_STATE_USED)
223 static unsigned int qno = 0;
224 static unsigned int msgqno = 0;
225 static char qnostr[32];
226 static const char *current_hint_str = NULL;
228 /* common data for all hints. */
231 const char *hint_str; /* must not do pfree */
232 const char *keyword; /* must not do pfree */
233 HintKeyword hint_keyword;
236 HintDeleteFunction delete_func;
237 HintDescFunction desc_func;
238 HintCmpFunction cmp_func;
239 HintParseFunction parse_func;
242 /* scan method hints */
243 typedef struct ScanMethodHint
249 unsigned char enforce_mask;
252 typedef struct ParentIndexInfo
257 char *expression_str;
264 /* join method hints */
265 typedef struct JoinMethodHint
271 unsigned char enforce_mask;
273 Relids inner_joinrelids;
276 /* join order hints */
277 typedef struct OuterInnerRels
280 List *outer_inner_pair;
283 typedef struct LeadingHint
286 List *relations; /* relation names specified in Leading hint */
287 OuterInnerRels *outer_inner;
290 /* change a run-time parameter hints */
291 typedef struct SetHint
294 char *name; /* name of variable */
300 typedef enum RowsValueType {
301 RVT_ABSOLUTE, /* Rows(... #1000) */
302 RVT_ADD, /* Rows(... +1000) */
303 RVT_SUB, /* Rows(... -1000) */
304 RVT_MULTI, /* Rows(... *1.2) */
306 typedef struct RowsHint
313 Relids inner_joinrelids;
315 RowsValueType value_type;
320 typedef struct ParallelHint
324 char *nworkers_str; /* original string of nworkers */
325 int nworkers; /* num of workers specified by Worker */
326 bool force_parallel; /* force parallel scan */
330 * Describes a context of hint processing.
334 char *hint_str; /* original hint string */
337 int nall_hints; /* # of valid all hints */
338 int max_all_hints; /* # of slots for all hints */
339 Hint **all_hints; /* parsed all hints */
341 /* # of each hints */
342 int num_hints[NUM_HINT_TYPE];
344 /* for scan method hints */
345 ScanMethodHint **scan_hints; /* parsed scan hints */
347 /* Initial values of parameters */
348 int init_scan_mask; /* enable_* mask */
349 int init_nworkers; /* max_parallel_workers_per_gather */
350 /* min_parallel_table_scan_size*/
351 int init_min_para_tablescan_size;
352 /* min_parallel_index_scan_size*/
353 int init_min_para_indexscan_size;
354 int init_paratup_cost; /* parallel_tuple_cost */
355 int init_parasetup_cost;/* parallel_setup_cost */
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 make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
464 ListCell *other_rels);
465 static void make_rels_by_clauseless_joins(PlannerInfo *root,
467 ListCell *other_rels);
468 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
469 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
471 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
472 Index rti, RangeTblEntry *rte);
473 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
476 static void pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
478 static void pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate,
480 static void plpgsql_query_erase_callback(ResourceReleasePhase phase,
484 static int set_config_option_noerror(const char *name, const char *value,
485 GucContext context, GucSource source,
486 GucAction action, bool changeVal, int elevel);
487 static void setup_scan_method_enforcement(ScanMethodHint *scanhint,
489 static int set_config_int32_option(const char *name, int32 value,
493 static bool pg_hint_plan_enable_hint = true;
494 static int debug_level = 0;
495 static int pg_hint_plan_parse_message_level = INFO;
496 static int pg_hint_plan_debug_message_level = LOG;
497 /* Default is off, to keep backward compatibility. */
498 static bool pg_hint_plan_enable_hint_table = false;
500 static int plpgsql_recurse_level = 0; /* PLpgSQL recursion level */
501 static int hint_inhibit_level = 0; /* Inhibit hinting if this is above 0 */
502 /* (This could not be above 1) */
503 static int max_hint_nworkers = -1; /* Maximum nworkers of Workers hints */
505 static const struct config_enum_entry parse_messages_level_options[] = {
506 {"debug", DEBUG2, true},
507 {"debug5", DEBUG5, false},
508 {"debug4", DEBUG4, false},
509 {"debug3", DEBUG3, false},
510 {"debug2", DEBUG2, false},
511 {"debug1", DEBUG1, false},
513 {"info", INFO, false},
514 {"notice", NOTICE, false},
515 {"warning", WARNING, false},
516 {"error", ERROR, false},
518 * {"fatal", FATAL, true},
519 * {"panic", PANIC, true},
524 static const struct config_enum_entry parse_debug_level_options[] = {
527 {"detailed", 2, false},
528 {"verbose", 3, false},
540 /* Saved hook values in case of unload */
541 static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
542 static planner_hook_type prev_planner = NULL;
543 static join_search_hook_type prev_join_search = NULL;
544 static set_rel_pathlist_hook_type prev_set_rel_pathlist = NULL;
546 /* Hold reference to currently active hint */
547 static HintState *current_hint_state = NULL;
550 * List of hint contexts. We treat the head of the list as the Top of the
551 * context stack, so current_hint_state always points the first element of this
554 static List *HintStateStack = NIL;
557 * Holds statement name during executing EXECUTE command. NULL for other
560 static char *stmt_name = NULL;
562 static const HintParser parsers[] = {
563 {HINT_SEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_SEQSCAN},
564 {HINT_INDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCAN},
565 {HINT_INDEXSCANREGEXP, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCANREGEXP},
566 {HINT_BITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_BITMAPSCAN},
567 {HINT_BITMAPSCANREGEXP, ScanMethodHintCreate,
568 HINT_KEYWORD_BITMAPSCANREGEXP},
569 {HINT_TIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_TIDSCAN},
570 {HINT_NOSEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOSEQSCAN},
571 {HINT_NOINDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXSCAN},
572 {HINT_NOBITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOBITMAPSCAN},
573 {HINT_NOTIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOTIDSCAN},
574 {HINT_INDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXONLYSCAN},
575 {HINT_INDEXONLYSCANREGEXP, ScanMethodHintCreate,
576 HINT_KEYWORD_INDEXONLYSCANREGEXP},
577 {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXONLYSCAN},
579 {HINT_NESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NESTLOOP},
580 {HINT_MERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_MERGEJOIN},
581 {HINT_HASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_HASHJOIN},
582 {HINT_NONESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NONESTLOOP},
583 {HINT_NOMERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOMERGEJOIN},
584 {HINT_NOHASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOHASHJOIN},
586 {HINT_LEADING, LeadingHintCreate, HINT_KEYWORD_LEADING},
587 {HINT_SET, SetHintCreate, HINT_KEYWORD_SET},
588 {HINT_ROWS, RowsHintCreate, HINT_KEYWORD_ROWS},
589 {HINT_PARALLEL, ParallelHintCreate, HINT_KEYWORD_PARALLEL},
591 {NULL, NULL, HINT_KEYWORD_UNRECOGNIZED}
594 PLpgSQL_plugin plugin_funcs = {
598 pg_hint_plan_plpgsql_stmt_beg,
599 pg_hint_plan_plpgsql_stmt_end,
605 * Module load callbacks
610 PLpgSQL_plugin **var_ptr;
612 /* Define custom GUC variables. */
613 DefineCustomBoolVariable("pg_hint_plan.enable_hint",
614 "Force planner to use plans specified in the hint comment preceding to the query.",
616 &pg_hint_plan_enable_hint,
624 DefineCustomEnumVariable("pg_hint_plan.debug_print",
625 "Logs results of hint parsing.",
629 parse_debug_level_options,
636 DefineCustomEnumVariable("pg_hint_plan.parse_messages",
637 "Message level of parse errors.",
639 &pg_hint_plan_parse_message_level,
641 parse_messages_level_options,
648 DefineCustomEnumVariable("pg_hint_plan.message_level",
649 "Message level of debug messages.",
651 &pg_hint_plan_debug_message_level,
653 parse_messages_level_options,
660 DefineCustomBoolVariable("pg_hint_plan.enable_hint_table",
661 "Let pg_hint_plan look up the hint table.",
663 &pg_hint_plan_enable_hint_table,
672 prev_post_parse_analyze_hook = post_parse_analyze_hook;
673 post_parse_analyze_hook = pg_hint_plan_post_parse_analyze;
674 prev_planner = planner_hook;
675 planner_hook = pg_hint_plan_planner;
676 prev_join_search = join_search_hook;
677 join_search_hook = pg_hint_plan_join_search;
678 prev_set_rel_pathlist = set_rel_pathlist_hook;
679 set_rel_pathlist_hook = pg_hint_plan_set_rel_pathlist;
681 /* setup PL/pgSQL plugin hook */
682 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
683 *var_ptr = &plugin_funcs;
685 RegisterResourceReleaseCallback(plpgsql_query_erase_callback, NULL);
689 * Module unload callback
695 PLpgSQL_plugin **var_ptr;
697 /* Uninstall hooks. */
698 post_parse_analyze_hook = prev_post_parse_analyze_hook;
699 planner_hook = prev_planner;
700 join_search_hook = prev_join_search;
701 set_rel_pathlist_hook = prev_set_rel_pathlist;
703 /* uninstall PL/pgSQL plugin hook */
704 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
709 * create and delete functions the hint object
713 ScanMethodHintCreate(const char *hint_str, const char *keyword,
714 HintKeyword hint_keyword)
716 ScanMethodHint *hint;
718 hint = palloc(sizeof(ScanMethodHint));
719 hint->base.hint_str = hint_str;
720 hint->base.keyword = keyword;
721 hint->base.hint_keyword = hint_keyword;
722 hint->base.type = HINT_TYPE_SCAN_METHOD;
723 hint->base.state = HINT_STATE_NOTUSED;
724 hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
725 hint->base.desc_func = (HintDescFunction) ScanMethodHintDesc;
726 hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
727 hint->base.parse_func = (HintParseFunction) ScanMethodHintParse;
728 hint->relname = NULL;
729 hint->indexnames = NIL;
730 hint->regexp = false;
731 hint->enforce_mask = 0;
733 return (Hint *) hint;
737 ScanMethodHintDelete(ScanMethodHint *hint)
743 pfree(hint->relname);
744 list_free_deep(hint->indexnames);
749 JoinMethodHintCreate(const char *hint_str, const char *keyword,
750 HintKeyword hint_keyword)
752 JoinMethodHint *hint;
754 hint = palloc(sizeof(JoinMethodHint));
755 hint->base.hint_str = hint_str;
756 hint->base.keyword = keyword;
757 hint->base.hint_keyword = hint_keyword;
758 hint->base.type = HINT_TYPE_JOIN_METHOD;
759 hint->base.state = HINT_STATE_NOTUSED;
760 hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
761 hint->base.desc_func = (HintDescFunction) JoinMethodHintDesc;
762 hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
763 hint->base.parse_func = (HintParseFunction) JoinMethodHintParse;
765 hint->inner_nrels = 0;
766 hint->relnames = NULL;
767 hint->enforce_mask = 0;
768 hint->joinrelids = NULL;
769 hint->inner_joinrelids = NULL;
771 return (Hint *) hint;
775 JoinMethodHintDelete(JoinMethodHint *hint)
784 for (i = 0; i < hint->nrels; i++)
785 pfree(hint->relnames[i]);
786 pfree(hint->relnames);
789 bms_free(hint->joinrelids);
790 bms_free(hint->inner_joinrelids);
795 LeadingHintCreate(const char *hint_str, const char *keyword,
796 HintKeyword hint_keyword)
800 hint = palloc(sizeof(LeadingHint));
801 hint->base.hint_str = hint_str;
802 hint->base.keyword = keyword;
803 hint->base.hint_keyword = hint_keyword;
804 hint->base.type = HINT_TYPE_LEADING;
805 hint->base.state = HINT_STATE_NOTUSED;
806 hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
807 hint->base.desc_func = (HintDescFunction) LeadingHintDesc;
808 hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
809 hint->base.parse_func = (HintParseFunction) LeadingHintParse;
810 hint->relations = NIL;
811 hint->outer_inner = NULL;
813 return (Hint *) hint;
817 LeadingHintDelete(LeadingHint *hint)
822 list_free_deep(hint->relations);
823 if (hint->outer_inner)
824 pfree(hint->outer_inner);
829 SetHintCreate(const char *hint_str, const char *keyword,
830 HintKeyword hint_keyword)
834 hint = palloc(sizeof(SetHint));
835 hint->base.hint_str = hint_str;
836 hint->base.keyword = keyword;
837 hint->base.hint_keyword = hint_keyword;
838 hint->base.type = HINT_TYPE_SET;
839 hint->base.state = HINT_STATE_NOTUSED;
840 hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
841 hint->base.desc_func = (HintDescFunction) SetHintDesc;
842 hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
843 hint->base.parse_func = (HintParseFunction) SetHintParse;
848 return (Hint *) hint;
852 SetHintDelete(SetHint *hint)
862 list_free(hint->words);
867 RowsHintCreate(const char *hint_str, const char *keyword,
868 HintKeyword hint_keyword)
872 hint = palloc(sizeof(RowsHint));
873 hint->base.hint_str = hint_str;
874 hint->base.keyword = keyword;
875 hint->base.hint_keyword = hint_keyword;
876 hint->base.type = HINT_TYPE_ROWS;
877 hint->base.state = HINT_STATE_NOTUSED;
878 hint->base.delete_func = (HintDeleteFunction) RowsHintDelete;
879 hint->base.desc_func = (HintDescFunction) RowsHintDesc;
880 hint->base.cmp_func = (HintCmpFunction) RowsHintCmp;
881 hint->base.parse_func = (HintParseFunction) RowsHintParse;
883 hint->inner_nrels = 0;
884 hint->relnames = NULL;
885 hint->joinrelids = NULL;
886 hint->inner_joinrelids = NULL;
887 hint->rows_str = NULL;
888 hint->value_type = RVT_ABSOLUTE;
891 return (Hint *) hint;
895 RowsHintDelete(RowsHint *hint)
904 for (i = 0; i < hint->nrels; i++)
905 pfree(hint->relnames[i]);
906 pfree(hint->relnames);
909 bms_free(hint->joinrelids);
910 bms_free(hint->inner_joinrelids);
915 ParallelHintCreate(const char *hint_str, const char *keyword,
916 HintKeyword hint_keyword)
920 hint = palloc(sizeof(ScanMethodHint));
921 hint->base.hint_str = hint_str;
922 hint->base.keyword = keyword;
923 hint->base.hint_keyword = hint_keyword;
924 hint->base.type = HINT_TYPE_PARALLEL;
925 hint->base.state = HINT_STATE_NOTUSED;
926 hint->base.delete_func = (HintDeleteFunction) ParallelHintDelete;
927 hint->base.desc_func = (HintDescFunction) ParallelHintDesc;
928 hint->base.cmp_func = (HintCmpFunction) ParallelHintCmp;
929 hint->base.parse_func = (HintParseFunction) ParallelHintParse;
930 hint->relname = NULL;
932 hint->nworkers_str = "0";
934 return (Hint *) hint;
938 ParallelHintDelete(ParallelHint *hint)
944 pfree(hint->relname);
950 HintStateCreate(void)
954 hstate = palloc(sizeof(HintState));
955 hstate->hint_str = NULL;
956 hstate->nall_hints = 0;
957 hstate->max_all_hints = 0;
958 hstate->all_hints = NULL;
959 memset(hstate->num_hints, 0, sizeof(hstate->num_hints));
960 hstate->scan_hints = NULL;
961 hstate->init_scan_mask = 0;
962 hstate->init_nworkers = 0;
963 hstate->init_min_para_tablescan_size = 0;
964 hstate->init_min_para_indexscan_size = 0;
965 hstate->init_paratup_cost = 0;
966 hstate->init_parasetup_cost = 0;
967 hstate->parent_relid = 0;
968 hstate->parent_scan_hint = NULL;
969 hstate->parent_parallel_hint = NULL;
970 hstate->parent_index_infos = NIL;
971 hstate->join_hints = NULL;
972 hstate->init_join_mask = 0;
973 hstate->join_hint_level = NULL;
974 hstate->leading_hint = NULL;
975 hstate->context = superuser() ? PGC_SUSET : PGC_USERSET;
976 hstate->set_hints = NULL;
977 hstate->rows_hints = NULL;
978 hstate->parallel_hints = NULL;
984 HintStateDelete(HintState *hstate)
991 if (hstate->hint_str)
992 pfree(hstate->hint_str);
994 for (i = 0; i < hstate->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
995 hstate->all_hints[i]->delete_func(hstate->all_hints[i]);
996 if (hstate->all_hints)
997 pfree(hstate->all_hints);
998 if (hstate->parent_index_infos)
999 list_free(hstate->parent_index_infos);
1003 * Copy given value into buf, with quoting with '"' if necessary.
1006 quote_value(StringInfo buf, const char *value)
1008 bool need_quote = false;
1011 for (str = value; *str != '\0'; str++)
1013 if (isspace(*str) || *str == '(' || *str == ')' || *str == '"')
1016 appendStringInfoCharMacro(buf, '"');
1021 for (str = value; *str != '\0'; str++)
1024 appendStringInfoCharMacro(buf, '"');
1026 appendStringInfoCharMacro(buf, *str);
1030 appendStringInfoCharMacro(buf, '"');
1034 ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf)
1038 appendStringInfo(buf, "%s(", hint->base.keyword);
1039 if (hint->relname != NULL)
1041 quote_value(buf, hint->relname);
1042 foreach(l, hint->indexnames)
1044 appendStringInfoCharMacro(buf, ' ');
1045 quote_value(buf, (char *) lfirst(l));
1048 appendStringInfoString(buf, ")");
1050 appendStringInfoChar(buf, '\n');
1054 JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf)
1058 appendStringInfo(buf, "%s(", hint->base.keyword);
1059 if (hint->relnames != NULL)
1061 quote_value(buf, hint->relnames[0]);
1062 for (i = 1; i < hint->nrels; i++)
1064 appendStringInfoCharMacro(buf, ' ');
1065 quote_value(buf, hint->relnames[i]);
1068 appendStringInfoString(buf, ")");
1070 appendStringInfoChar(buf, '\n');
1074 OuterInnerDesc(OuterInnerRels *outer_inner, StringInfo buf)
1076 if (outer_inner->relation == NULL)
1083 appendStringInfoCharMacro(buf, '(');
1084 foreach(l, outer_inner->outer_inner_pair)
1089 appendStringInfoCharMacro(buf, ' ');
1091 OuterInnerDesc(lfirst(l), buf);
1094 appendStringInfoCharMacro(buf, ')');
1097 quote_value(buf, outer_inner->relation);
1101 LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf)
1103 appendStringInfo(buf, "%s(", HINT_LEADING);
1104 if (hint->outer_inner == NULL)
1111 foreach(l, hint->relations)
1116 appendStringInfoCharMacro(buf, ' ');
1118 quote_value(buf, (char *) lfirst(l));
1122 OuterInnerDesc(hint->outer_inner, buf);
1124 appendStringInfoString(buf, ")");
1126 appendStringInfoChar(buf, '\n');
1130 SetHintDesc(SetHint *hint, StringInfo buf, bool nolf)
1132 bool is_first = true;
1135 appendStringInfo(buf, "%s(", HINT_SET);
1136 foreach(l, hint->words)
1141 appendStringInfoCharMacro(buf, ' ');
1143 quote_value(buf, (char *) lfirst(l));
1145 appendStringInfo(buf, ")");
1147 appendStringInfoChar(buf, '\n');
1151 RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf)
1155 appendStringInfo(buf, "%s(", hint->base.keyword);
1156 if (hint->relnames != NULL)
1158 quote_value(buf, hint->relnames[0]);
1159 for (i = 1; i < hint->nrels; i++)
1161 appendStringInfoCharMacro(buf, ' ');
1162 quote_value(buf, hint->relnames[i]);
1165 appendStringInfo(buf, " %s", hint->rows_str);
1166 appendStringInfoString(buf, ")");
1168 appendStringInfoChar(buf, '\n');
1172 ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf)
1174 appendStringInfo(buf, "%s(", hint->base.keyword);
1175 if (hint->relname != NULL)
1177 quote_value(buf, hint->relname);
1179 /* number of workers */
1180 appendStringInfoCharMacro(buf, ' ');
1181 quote_value(buf, hint->nworkers_str);
1182 /* application mode of num of workers */
1183 appendStringInfoCharMacro(buf, ' ');
1184 appendStringInfoString(buf,
1185 (hint->force_parallel ? "hard" : "soft"));
1187 appendStringInfoString(buf, ")");
1189 appendStringInfoChar(buf, '\n');
1193 * Append string which represents all hints in a given state to buf, with
1194 * preceding title with them.
1197 desc_hint_in_state(HintState *hstate, StringInfo buf, const char *title,
1198 HintStatus state, bool nolf)
1202 appendStringInfo(buf, "%s:", title);
1204 appendStringInfoChar(buf, '\n');
1207 for (i = 0; i < hstate->nall_hints; i++)
1209 if (hstate->all_hints[i]->state != state)
1212 hstate->all_hints[i]->desc_func(hstate->all_hints[i], buf, nolf);
1216 if (nolf && nshown == 0)
1217 appendStringInfoString(buf, "(none)");
1221 * Dump contents of given hstate to server log with log level LOG.
1224 HintStateDump(HintState *hstate)
1230 elog(pg_hint_plan_debug_message_level, "pg_hint_plan:\nno hint");
1234 initStringInfo(&buf);
1236 appendStringInfoString(&buf, "pg_hint_plan:\n");
1237 desc_hint_in_state(hstate, &buf, "used hint", HINT_STATE_USED, false);
1238 desc_hint_in_state(hstate, &buf, "not used hint", HINT_STATE_NOTUSED, false);
1239 desc_hint_in_state(hstate, &buf, "duplication hint", HINT_STATE_DUPLICATION, false);
1240 desc_hint_in_state(hstate, &buf, "error hint", HINT_STATE_ERROR, false);
1242 ereport(pg_hint_plan_debug_message_level,
1243 (errmsg ("%s", buf.data)));
1249 HintStateDump2(HintState *hstate)
1255 elog(pg_hint_plan_debug_message_level,
1256 "pg_hint_plan%s: HintStateDump: no hint", qnostr);
1260 initStringInfo(&buf);
1261 appendStringInfo(&buf, "pg_hint_plan%s: HintStateDump: ", qnostr);
1262 desc_hint_in_state(hstate, &buf, "{used hints", HINT_STATE_USED, true);
1263 desc_hint_in_state(hstate, &buf, "}, {not used hints", HINT_STATE_NOTUSED, true);
1264 desc_hint_in_state(hstate, &buf, "}, {duplicate hints", HINT_STATE_DUPLICATION, true);
1265 desc_hint_in_state(hstate, &buf, "}, {error hints", HINT_STATE_ERROR, true);
1266 appendStringInfoChar(&buf, '}');
1268 ereport(pg_hint_plan_debug_message_level,
1269 (errmsg("%s", buf.data),
1271 errhidecontext(true)));
1281 RelnameCmp(const void *a, const void *b)
1283 const char *relnamea = *((const char **) a);
1284 const char *relnameb = *((const char **) b);
1286 return strcmp(relnamea, relnameb);
1290 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
1292 return RelnameCmp(&a->relname, &b->relname);
1296 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
1300 if (a->nrels != b->nrels)
1301 return a->nrels - b->nrels;
1303 for (i = 0; i < a->nrels; i++)
1306 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1314 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
1320 SetHintCmp(const SetHint *a, const SetHint *b)
1322 return strcmp(a->name, b->name);
1326 RowsHintCmp(const RowsHint *a, const RowsHint *b)
1330 if (a->nrels != b->nrels)
1331 return a->nrels - b->nrels;
1333 for (i = 0; i < a->nrels; i++)
1336 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1344 ParallelHintCmp(const ParallelHint *a, const ParallelHint *b)
1346 return RelnameCmp(&a->relname, &b->relname);
1350 HintCmp(const void *a, const void *b)
1352 const Hint *hinta = *((const Hint **) a);
1353 const Hint *hintb = *((const Hint **) b);
1355 if (hinta->type != hintb->type)
1356 return hinta->type - hintb->type;
1357 if (hinta->state == HINT_STATE_ERROR)
1359 if (hintb->state == HINT_STATE_ERROR)
1361 return hinta->cmp_func(hinta, hintb);
1365 * Returns byte offset of hint b from hint a. If hint a was specified before
1366 * b, positive value is returned.
1369 HintCmpWithPos(const void *a, const void *b)
1371 const Hint *hinta = *((const Hint **) a);
1372 const Hint *hintb = *((const Hint **) b);
1375 result = HintCmp(a, b);
1377 result = hinta->hint_str - hintb->hint_str;
1386 parse_keyword(const char *str, StringInfo buf)
1390 while (!isspace(*str) && *str != '(' && *str != '\0')
1391 appendStringInfoCharMacro(buf, *str++);
1397 skip_parenthesis(const char *str, char parenthesis)
1401 if (*str != parenthesis)
1403 if (parenthesis == '(')
1404 hint_ereport(str, ("Opening parenthesis is necessary."));
1405 else if (parenthesis == ')')
1406 hint_ereport(str, ("Closing parenthesis is necessary."));
1417 * Parse a token from str, and store malloc'd copy into word. A token can be
1418 * quoted with '"'. Return value is pointer to unparsed portion of original
1419 * string, or NULL if an error occurred.
1421 * Parsed token is truncated within NAMEDATALEN-1 bytes, when truncate is true.
1424 parse_quoted_value(const char *str, char **word, bool truncate)
1429 /* Skip leading spaces. */
1432 initStringInfo(&buf);
1445 /* Double quotation must be closed. */
1449 hint_ereport(str, ("Unterminated quoted string."));
1454 * Skip escaped double quotation.
1456 * We don't allow slash-asterisk and asterisk-slash (delimiters of
1457 * block comments) to be an object name, so users must specify
1458 * alias for such object names.
1460 * Those special names can be allowed if we care escaped slashes
1461 * and asterisks, but we don't.
1470 else if (isspace(*str) || *str == '(' || *str == ')' || *str == '"' ||
1474 appendStringInfoCharMacro(&buf, *str++);
1479 hint_ereport(str, ("Zero-length delimited string."));
1486 /* Truncate name if it's too long */
1488 truncate_identifier(buf.data, strlen(buf.data), true);
1495 static OuterInnerRels *
1496 OuterInnerRelsCreate(char *name, List *outer_inner_list)
1498 OuterInnerRels *outer_inner;
1500 outer_inner = palloc(sizeof(OuterInnerRels));
1501 outer_inner->relation = name;
1502 outer_inner->outer_inner_pair = outer_inner_list;
1508 parse_parentheses_Leading_in(const char *str, OuterInnerRels **outer_inner)
1510 List *outer_inner_pair = NIL;
1512 if ((str = skip_parenthesis(str, '(')) == NULL)
1517 /* Store words in parentheses into outer_inner_list. */
1518 while(*str != ')' && *str != '\0')
1520 OuterInnerRels *outer_inner_rels;
1524 str = parse_parentheses_Leading_in(str, &outer_inner_rels);
1532 if ((str = parse_quoted_value(str, &name, true)) == NULL)
1535 outer_inner_rels = OuterInnerRelsCreate(name, NIL);
1538 outer_inner_pair = lappend(outer_inner_pair, outer_inner_rels);
1543 (str = skip_parenthesis(str, ')')) == NULL)
1545 list_free(outer_inner_pair);
1549 *outer_inner = OuterInnerRelsCreate(NULL, outer_inner_pair);
1555 parse_parentheses_Leading(const char *str, List **name_list,
1556 OuterInnerRels **outer_inner)
1559 bool truncate = true;
1561 if ((str = skip_parenthesis(str, '(')) == NULL)
1567 if ((str = parse_parentheses_Leading_in(str, outer_inner)) == NULL)
1572 /* Store words in parentheses into name_list. */
1573 while(*str != ')' && *str != '\0')
1575 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1577 list_free(*name_list);
1581 *name_list = lappend(*name_list, name);
1586 if ((str = skip_parenthesis(str, ')')) == NULL)
1592 parse_parentheses(const char *str, List **name_list, HintKeyword keyword)
1595 bool truncate = true;
1597 if ((str = skip_parenthesis(str, '(')) == NULL)
1602 /* Store words in parentheses into name_list. */
1603 while(*str != ')' && *str != '\0')
1605 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1607 list_free(*name_list);
1611 *name_list = lappend(*name_list, name);
1614 if (keyword == HINT_KEYWORD_INDEXSCANREGEXP ||
1615 keyword == HINT_KEYWORD_INDEXONLYSCANREGEXP ||
1616 keyword == HINT_KEYWORD_BITMAPSCANREGEXP ||
1617 keyword == HINT_KEYWORD_SET)
1623 if ((str = skip_parenthesis(str, ')')) == NULL)
1629 parse_hints(HintState *hstate, Query *parse, const char *str)
1634 initStringInfo(&buf);
1635 while (*str != '\0')
1637 const HintParser *parser;
1639 /* in error message, we output the comment including the keyword. */
1640 head = (char *) str;
1642 /* parse only the keyword of the hint. */
1643 resetStringInfo(&buf);
1644 str = parse_keyword(str, &buf);
1646 for (parser = parsers; parser->keyword != NULL; parser++)
1648 char *keyword = parser->keyword;
1651 if (pg_strcasecmp(buf.data, keyword) != 0)
1654 hint = parser->create_func(head, keyword, parser->hint_keyword);
1656 /* parser of each hint does parse in a parenthesis. */
1657 if ((str = hint->parse_func(hint, hstate, parse, str)) == NULL)
1659 hint->delete_func(hint);
1665 * Add hint information into all_hints array. If we don't have
1666 * enough space, double the array.
1668 if (hstate->nall_hints == 0)
1670 hstate->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
1671 hstate->all_hints = (Hint **)
1672 palloc(sizeof(Hint *) * hstate->max_all_hints);
1674 else if (hstate->nall_hints == hstate->max_all_hints)
1676 hstate->max_all_hints *= 2;
1677 hstate->all_hints = (Hint **)
1678 repalloc(hstate->all_hints,
1679 sizeof(Hint *) * hstate->max_all_hints);
1682 hstate->all_hints[hstate->nall_hints] = hint;
1683 hstate->nall_hints++;
1690 if (parser->keyword == NULL)
1693 ("Unrecognized hint keyword \"%s\".", buf.data));
1704 * Get hints from table by client-supplied query string and application name.
1707 get_hints_from_table(const char *client_query, const char *client_application)
1709 const char *search_query =
1711 " FROM hint_plan.hints "
1712 " WHERE norm_query_string = $1 "
1713 " AND ( application_name = $2 "
1714 " OR application_name = '' ) "
1715 " ORDER BY application_name DESC";
1716 static SPIPlanPtr plan = NULL;
1718 Oid argtypes[2] = { TEXTOID, TEXTOID };
1720 char nulls[2] = {' ', ' '};
1726 bool snapshot_set = false;
1728 hint_inhibit_level++;
1730 if (!ActiveSnapshotSet())
1732 PushActiveSnapshot(GetTransactionSnapshot());
1733 snapshot_set = true;
1741 p = SPI_prepare(search_query, 2, argtypes);
1742 plan = SPI_saveplan(p);
1746 qry = cstring_to_text(client_query);
1747 app = cstring_to_text(client_application);
1748 values[0] = PointerGetDatum(qry);
1749 values[1] = PointerGetDatum(app);
1751 SPI_execute_plan(plan, values, nulls, true, 1);
1753 if (SPI_processed > 0)
1757 hints = SPI_getvalue(SPI_tuptable->vals[0],
1758 SPI_tuptable->tupdesc, 1);
1760 * Here we use SPI_palloc to ensure that hints string is valid even
1761 * after SPI_finish call. We can't use simple palloc because it
1762 * allocates memory in SPI's context and that context is deleted in
1765 buf = SPI_palloc(strlen(hints) + 1);
1773 PopActiveSnapshot();
1775 hint_inhibit_level--;
1779 hint_inhibit_level--;
1788 * Get client-supplied query string. Addtion to that the jumbled query is
1789 * supplied if the caller requested. From the restriction of JumbleQuery, some
1790 * kind of query needs special amendments. Reutrns NULL if this query doesn't
1791 * change the current hint. This function returns NULL also when something
1792 * wrong has happend and let the caller continue using the current hints.
1795 get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
1797 const char *p = debug_query_string;
1800 * If debug_query_string is set, it is the top level statement. But in some
1801 * cases we reach here with debug_query_string set NULL for example in the
1802 * case of DESCRIBE message handling. We may still see a candidate
1803 * top-level query in pstate in the case.
1807 /* We don't see a query string, return NULL */
1808 if (!pstate->p_sourcetext)
1811 p = pstate->p_sourcetext;
1814 if (jumblequery != NULL)
1815 *jumblequery = query;
1817 if (query->commandType == CMD_UTILITY)
1819 Query *target_query = (Query *)query->utilityStmt;
1822 * Some CMD_UTILITY statements have a subquery that we can hint on.
1823 * Since EXPLAIN can be placed before other kind of utility statements
1824 * and EXECUTE can be contained other kind of utility statements, these
1825 * conditions are not mutually exclusive and should be considered in
1828 if (IsA(target_query, ExplainStmt))
1830 ExplainStmt *stmt = (ExplainStmt *)target_query;
1832 Assert(IsA(stmt->query, Query));
1833 target_query = (Query *)stmt->query;
1835 /* strip out the top-level query for further processing */
1836 if (target_query->commandType == CMD_UTILITY &&
1837 target_query->utilityStmt != NULL)
1838 target_query = (Query *)target_query->utilityStmt;
1841 if (IsA(target_query, DeclareCursorStmt))
1843 DeclareCursorStmt *stmt = (DeclareCursorStmt *)target_query;
1844 Query *query = (Query *)stmt->query;
1846 /* the target must be CMD_SELECT in this case */
1847 Assert(IsA(query, Query) && query->commandType == CMD_SELECT);
1848 target_query = query;
1851 if (IsA(target_query, CreateTableAsStmt))
1853 CreateTableAsStmt *stmt = (CreateTableAsStmt *) target_query;
1855 Assert(IsA(stmt->query, Query));
1856 target_query = (Query *) stmt->query;
1858 /* strip out the top-level query for further processing */
1859 if (target_query->commandType == CMD_UTILITY &&
1860 target_query->utilityStmt != NULL)
1861 target_query = (Query *)target_query->utilityStmt;
1864 if (IsA(target_query, ExecuteStmt))
1867 * Use the prepared query for EXECUTE. The Query for jumble
1868 * also replaced with the corresponding one.
1870 ExecuteStmt *stmt = (ExecuteStmt *)target_query;
1871 PreparedStatement *entry;
1873 entry = FetchPreparedStatement(stmt->name, true);
1874 p = entry->plansource->query_string;
1875 target_query = (Query *) linitial (entry->plansource->query_list);
1878 /* JumbleQuery accespts only a non-utility Query */
1879 if (!IsA(target_query, Query) ||
1880 target_query->utilityStmt != NULL)
1881 target_query = NULL;
1884 *jumblequery = target_query;
1886 /* Return NULL if the pstate is not identical to the top-level query */
1887 else if (strcmp(pstate->p_sourcetext, p) != 0)
1894 * Get hints from the head block comment in client-supplied query string.
1897 get_hints_from_comment(const char *p)
1899 const char *hint_head;
1907 /* extract query head comment. */
1908 hint_head = strstr(p, HINT_START);
1909 if (hint_head == NULL)
1911 for (;p < hint_head; p++)
1914 * Allow these characters precedes hint comment:
1916 * - alphabets which are in ASCII range
1917 * - space, tabs and new-lines
1918 * - underscores, for identifier
1919 * - commas, for SELECT clause, EXPLAIN and PREPARE
1920 * - parentheses, for EXPLAIN and PREPARE
1922 * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1923 * avoid behavior which depends on locale setting.
1925 if (!(*p >= '0' && *p <= '9') &&
1926 !(*p >= 'A' && *p <= 'Z') &&
1927 !(*p >= 'a' && *p <= 'z') &&
1931 *p != '(' && *p != ')')
1935 len = strlen(HINT_START);
1940 /* find hint end keyword. */
1941 if ((tail = strstr(p, HINT_END)) == NULL)
1943 hint_ereport(head, ("Unterminated block comment."));
1947 /* We don't support nested block comments. */
1948 if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1950 hint_ereport(head, ("Nested block comments are not supported."));
1954 /* Make a copy of hint. */
1956 head = palloc(len + 1);
1957 memcpy(head, p, len);
1965 * Parse hints that got, create hint struct from parse tree and parse hints.
1968 create_hintstate(Query *parse, const char *hints)
1977 /* -1 means that no Parallel hint is specified. */
1978 max_hint_nworkers = -1;
1981 hstate = HintStateCreate();
1982 hstate->hint_str = (char *) hints;
1984 /* parse each hint. */
1985 parse_hints(hstate, parse, p);
1987 /* When nothing specified a hint, we free HintState and returns NULL. */
1988 if (hstate->nall_hints == 0)
1990 HintStateDelete(hstate);
1994 /* Sort hints in order of original position. */
1995 qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
1998 /* Count number of hints per hint-type. */
1999 for (i = 0; i < hstate->nall_hints; i++)
2001 Hint *cur_hint = hstate->all_hints[i];
2002 hstate->num_hints[cur_hint->type]++;
2006 * If an object (or a set of objects) has multiple hints of same hint-type,
2007 * only the last hint is valid and others are ignored in planning.
2008 * Hints except the last are marked as 'duplicated' to remember the order.
2010 for (i = 0; i < hstate->nall_hints - 1; i++)
2012 Hint *cur_hint = hstate->all_hints[i];
2013 Hint *next_hint = hstate->all_hints[i + 1];
2016 * Leading hint is marked as 'duplicated' in transform_join_hints.
2018 if (cur_hint->type == HINT_TYPE_LEADING &&
2019 next_hint->type == HINT_TYPE_LEADING)
2023 * Note that we need to pass addresses of hint pointers, because
2024 * HintCmp is designed to sort array of Hint* by qsort.
2026 if (HintCmp(&cur_hint, &next_hint) == 0)
2028 hint_ereport(cur_hint->hint_str,
2029 ("Conflict %s hint.", HintTypeName[cur_hint->type]));
2030 cur_hint->state = HINT_STATE_DUPLICATION;
2035 * Make sure that per-type array pointers point proper position in the
2036 * array which consists of all hints.
2038 hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
2039 hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
2040 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
2041 hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
2042 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
2043 hstate->set_hints = (SetHint **) (hstate->leading_hint +
2044 hstate->num_hints[HINT_TYPE_LEADING]);
2045 hstate->rows_hints = (RowsHint **) (hstate->set_hints +
2046 hstate->num_hints[HINT_TYPE_SET]);
2047 hstate->parallel_hints = (ParallelHint **) (hstate->set_hints +
2048 hstate->num_hints[HINT_TYPE_ROWS]);
2054 * Parse inside of parentheses of scan-method hints.
2057 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
2060 const char *keyword = hint->base.keyword;
2061 HintKeyword hint_keyword = hint->base.hint_keyword;
2062 List *name_list = NIL;
2065 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2068 /* Parse relation name and index name(s) if given hint accepts. */
2069 length = list_length(name_list);
2071 /* at least twp parameters required */
2075 ("%s hint requires a relation.", hint->base.keyword));
2076 hint->base.state = HINT_STATE_ERROR;
2080 hint->relname = linitial(name_list);
2081 hint->indexnames = list_delete_first(name_list);
2083 /* check whether the hint accepts index name(s) */
2084 if (length > 1 && !SCAN_HINT_ACCEPTS_INDEX_NAMES(hint_keyword))
2087 ("%s hint accepts only one relation.",
2088 hint->base.keyword));
2089 hint->base.state = HINT_STATE_ERROR;
2093 /* Set a bit for specified hint. */
2094 switch (hint_keyword)
2096 case HINT_KEYWORD_SEQSCAN:
2097 hint->enforce_mask = ENABLE_SEQSCAN;
2099 case HINT_KEYWORD_INDEXSCAN:
2100 hint->enforce_mask = ENABLE_INDEXSCAN;
2102 case HINT_KEYWORD_INDEXSCANREGEXP:
2103 hint->enforce_mask = ENABLE_INDEXSCAN;
2104 hint->regexp = true;
2106 case HINT_KEYWORD_BITMAPSCAN:
2107 hint->enforce_mask = ENABLE_BITMAPSCAN;
2109 case HINT_KEYWORD_BITMAPSCANREGEXP:
2110 hint->enforce_mask = ENABLE_BITMAPSCAN;
2111 hint->regexp = true;
2113 case HINT_KEYWORD_TIDSCAN:
2114 hint->enforce_mask = ENABLE_TIDSCAN;
2116 case HINT_KEYWORD_NOSEQSCAN:
2117 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
2119 case HINT_KEYWORD_NOINDEXSCAN:
2120 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
2122 case HINT_KEYWORD_NOBITMAPSCAN:
2123 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
2125 case HINT_KEYWORD_NOTIDSCAN:
2126 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
2128 case HINT_KEYWORD_INDEXONLYSCAN:
2129 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2131 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
2132 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2133 hint->regexp = true;
2135 case HINT_KEYWORD_NOINDEXONLYSCAN:
2136 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
2139 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2148 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
2151 const char *keyword = hint->base.keyword;
2152 HintKeyword hint_keyword = hint->base.hint_keyword;
2153 List *name_list = NIL;
2155 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2158 hint->nrels = list_length(name_list);
2160 if (hint->nrels > 0)
2166 * Transform relation names from list to array to sort them with qsort
2169 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2170 foreach (l, name_list)
2172 hint->relnames[i] = lfirst(l);
2177 list_free(name_list);
2179 /* A join hint requires at least two relations */
2180 if (hint->nrels < 2)
2183 ("%s hint requires at least two relations.",
2184 hint->base.keyword));
2185 hint->base.state = HINT_STATE_ERROR;
2189 /* Sort hints in alphabetical order of relation names. */
2190 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2192 switch (hint_keyword)
2194 case HINT_KEYWORD_NESTLOOP:
2195 hint->enforce_mask = ENABLE_NESTLOOP;
2197 case HINT_KEYWORD_MERGEJOIN:
2198 hint->enforce_mask = ENABLE_MERGEJOIN;
2200 case HINT_KEYWORD_HASHJOIN:
2201 hint->enforce_mask = ENABLE_HASHJOIN;
2203 case HINT_KEYWORD_NONESTLOOP:
2204 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
2206 case HINT_KEYWORD_NOMERGEJOIN:
2207 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
2209 case HINT_KEYWORD_NOHASHJOIN:
2210 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
2213 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2222 OuterInnerPairCheck(OuterInnerRels *outer_inner)
2225 if (outer_inner->outer_inner_pair == NIL)
2227 if (outer_inner->relation)
2233 if (list_length(outer_inner->outer_inner_pair) == 2)
2235 foreach(l, outer_inner->outer_inner_pair)
2237 if (!OuterInnerPairCheck(lfirst(l)))
2248 OuterInnerList(OuterInnerRels *outer_inner)
2250 List *outer_inner_list = NIL;
2252 OuterInnerRels *outer_inner_rels;
2254 foreach(l, outer_inner->outer_inner_pair)
2256 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
2258 if (outer_inner_rels->relation != NULL)
2259 outer_inner_list = lappend(outer_inner_list,
2260 outer_inner_rels->relation);
2262 outer_inner_list = list_concat(outer_inner_list,
2263 OuterInnerList(outer_inner_rels));
2265 return outer_inner_list;
2269 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
2272 List *name_list = NIL;
2273 OuterInnerRels *outer_inner = NULL;
2275 if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
2279 if (outer_inner != NULL)
2280 name_list = OuterInnerList(outer_inner);
2282 hint->relations = name_list;
2283 hint->outer_inner = outer_inner;
2285 /* A Leading hint requires at least two relations */
2286 if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
2288 hint_ereport(hint->base.hint_str,
2289 ("%s hint requires at least two relations.",
2291 hint->base.state = HINT_STATE_ERROR;
2293 else if (hint->outer_inner != NULL &&
2294 !OuterInnerPairCheck(hint->outer_inner))
2296 hint_ereport(hint->base.hint_str,
2297 ("%s hint requires two sets of relations when parentheses nests.",
2299 hint->base.state = HINT_STATE_ERROR;
2306 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
2308 List *name_list = NIL;
2310 if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
2314 hint->words = name_list;
2316 /* We need both name and value to set GUC parameter. */
2317 if (list_length(name_list) == 2)
2319 hint->name = linitial(name_list);
2320 hint->value = lsecond(name_list);
2324 hint_ereport(hint->base.hint_str,
2325 ("%s hint requires name and value of GUC parameter.",
2327 hint->base.state = HINT_STATE_ERROR;
2334 RowsHintParse(RowsHint *hint, HintState *hstate, Query *parse,
2337 HintKeyword hint_keyword = hint->base.hint_keyword;
2338 List *name_list = NIL;
2342 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2345 /* Last element must be rows specification */
2346 hint->nrels = list_length(name_list) - 1;
2348 if (hint->nrels > 0)
2354 * Transform relation names from list to array to sort them with qsort
2357 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2358 foreach (l, name_list)
2360 if (hint->nrels <= i)
2362 hint->relnames[i] = lfirst(l);
2367 /* Retieve rows estimation */
2368 rows_str = list_nth(name_list, hint->nrels);
2369 hint->rows_str = rows_str; /* store as-is for error logging */
2370 if (rows_str[0] == '#')
2372 hint->value_type = RVT_ABSOLUTE;
2375 else if (rows_str[0] == '+')
2377 hint->value_type = RVT_ADD;
2380 else if (rows_str[0] == '-')
2382 hint->value_type = RVT_SUB;
2385 else if (rows_str[0] == '*')
2387 hint->value_type = RVT_MULTI;
2392 hint_ereport(rows_str, ("Unrecognized rows value type notation."));
2393 hint->base.state = HINT_STATE_ERROR;
2396 hint->rows = strtod(rows_str, &end_ptr);
2399 hint_ereport(rows_str,
2400 ("%s hint requires valid number as rows estimation.",
2401 hint->base.keyword));
2402 hint->base.state = HINT_STATE_ERROR;
2406 /* A join hint requires at least two relations */
2407 if (hint->nrels < 2)
2410 ("%s hint requires at least two relations.",
2411 hint->base.keyword));
2412 hint->base.state = HINT_STATE_ERROR;
2416 list_free(name_list);
2418 /* Sort relnames in alphabetical order. */
2419 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2425 ParallelHintParse(ParallelHint *hint, HintState *hstate, Query *parse,
2428 HintKeyword hint_keyword = hint->base.hint_keyword;
2429 List *name_list = NIL;
2433 bool force_parallel = false;
2435 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2438 /* Parse relation name and index name(s) if given hint accepts. */
2439 length = list_length(name_list);
2441 if (length < 2 || length > 3)
2444 ("wrong number of arguments (%d): %s",
2445 length, hint->base.keyword));
2446 hint->base.state = HINT_STATE_ERROR;
2450 hint->relname = linitial(name_list);
2452 /* The second parameter is number of workers */
2453 hint->nworkers_str = list_nth(name_list, 1);
2454 nworkers = strtod(hint->nworkers_str, &end_ptr);
2455 if (*end_ptr || nworkers < 0 || nworkers > max_worker_processes)
2458 hint_ereport(hint->nworkers_str,
2459 ("number of workers must be a number: %s",
2460 hint->base.keyword));
2461 else if (nworkers < 0)
2462 hint_ereport(hint->nworkers_str,
2463 ("number of workers must be positive: %s",
2464 hint->base.keyword));
2465 else if (nworkers > max_worker_processes)
2466 hint_ereport(hint->nworkers_str,
2467 ("number of workers = %d is larger than max_worker_processes(%d): %s",
2468 nworkers, max_worker_processes, hint->base.keyword));
2470 hint->base.state = HINT_STATE_ERROR;
2473 hint->nworkers = nworkers;
2475 /* optional third parameter is specified */
2478 const char *modeparam = (const char *)list_nth(name_list, 2);
2479 if (pg_strcasecmp(modeparam, "hard") == 0)
2480 force_parallel = true;
2481 else if (pg_strcasecmp(modeparam, "soft") != 0)
2483 hint_ereport(modeparam,
2484 ("enforcement must be soft or hard: %s",
2485 hint->base.keyword));
2486 hint->base.state = HINT_STATE_ERROR;
2490 hint->force_parallel = force_parallel;
2492 if (hint->base.state != HINT_STATE_ERROR &&
2493 nworkers > max_hint_nworkers)
2494 max_hint_nworkers = nworkers;
2500 * set GUC parameter functions
2504 get_current_scan_mask()
2509 mask |= ENABLE_SEQSCAN;
2510 if (enable_indexscan)
2511 mask |= ENABLE_INDEXSCAN;
2512 if (enable_bitmapscan)
2513 mask |= ENABLE_BITMAPSCAN;
2515 mask |= ENABLE_TIDSCAN;
2516 if (enable_indexonlyscan)
2517 mask |= ENABLE_INDEXONLYSCAN;
2523 get_current_join_mask()
2527 if (enable_nestloop)
2528 mask |= ENABLE_NESTLOOP;
2529 if (enable_mergejoin)
2530 mask |= ENABLE_MERGEJOIN;
2531 if (enable_hashjoin)
2532 mask |= ENABLE_HASHJOIN;
2538 * Sets GUC prameters without throwing exception. Reutrns false if something
2542 set_config_option_noerror(const char *name, const char *value,
2543 GucContext context, GucSource source,
2544 GucAction action, bool changeVal, int elevel)
2547 MemoryContext ccxt = CurrentMemoryContext;
2551 result = set_config_option(name, value, context, source,
2552 action, changeVal, 0, false);
2558 /* Save error info */
2559 MemoryContextSwitchTo(ccxt);
2560 errdata = CopyErrorData();
2564 (errcode(errdata->sqlerrcode),
2565 errmsg("%s", errdata->message),
2566 errdata->detail ? errdetail("%s", errdata->detail) : 0,
2567 errdata->hint ? errhint("%s", errdata->hint) : 0));
2569 FreeErrorData(errdata);
2577 * Sets GUC parameter of int32 type without throwing exceptions. Returns false
2578 * if something wrong.
2581 set_config_int32_option(const char *name, int32 value, GucContext context)
2583 char buf[16]; /* enough for int32 */
2585 if (snprintf(buf, 16, "%d", value) < 0)
2587 ereport(pg_hint_plan_parse_message_level,
2588 (errmsg ("Failed to convert integer to string: %d", value)));
2593 set_config_option_noerror(name, buf, context,
2594 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2595 pg_hint_plan_parse_message_level);
2598 /* setup scan method enforcement according to given options */
2600 setup_guc_enforcement(SetHint **options, int noptions, GucContext context)
2604 for (i = 0; i < noptions; i++)
2606 SetHint *hint = options[i];
2609 if (!hint_state_enabled(hint))
2612 result = set_config_option_noerror(hint->name, hint->value, context,
2613 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2614 pg_hint_plan_parse_message_level);
2616 hint->base.state = HINT_STATE_USED;
2618 hint->base.state = HINT_STATE_ERROR;
2625 * Setup parallel execution environment.
2627 * If hint is not NULL, set up using it, elsewise reset to initial environment.
2630 setup_parallel_plan_enforcement(ParallelHint *hint, HintState *state)
2634 hint->base.state = HINT_STATE_USED;
2635 set_config_int32_option("max_parallel_workers_per_gather",
2636 hint->nworkers, state->context);
2639 set_config_int32_option("max_parallel_workers_per_gather",
2640 state->init_nworkers, state->context);
2642 /* force means that enforce parallel as far as possible */
2643 if (hint && hint->force_parallel && hint->nworkers > 0)
2645 set_config_int32_option("parallel_tuple_cost", 0, state->context);
2646 set_config_int32_option("parallel_setup_cost", 0, state->context);
2647 set_config_int32_option("min_parallel_table_scan_size", 0,
2649 set_config_int32_option("min_parallel_index_scan_size", 0,
2654 set_config_int32_option("parallel_tuple_cost",
2655 state->init_paratup_cost, state->context);
2656 set_config_int32_option("parallel_setup_cost",
2657 state->init_parasetup_cost, state->context);
2658 set_config_int32_option("min_parallel_table_scan_size",
2659 state->init_min_para_tablescan_size,
2661 set_config_int32_option("min_parallel_index_scan_size",
2662 state->init_min_para_indexscan_size,
2667 #define SET_CONFIG_OPTION(name, type_bits) \
2668 set_config_option_noerror((name), \
2669 (mask & (type_bits)) ? "true" : "false", \
2670 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
2674 * Setup GUC environment to enforce scan methods. If scanhint is NULL, reset
2675 * GUCs to the saved state in state.
2678 setup_scan_method_enforcement(ScanMethodHint *scanhint, HintState *state)
2680 unsigned char enforce_mask = state->init_scan_mask;
2681 GucContext context = state->context;
2686 enforce_mask = scanhint->enforce_mask;
2687 scanhint->base.state = HINT_STATE_USED;
2690 if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
2691 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
2692 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
2694 mask = enforce_mask;
2696 mask = enforce_mask & current_hint_state->init_scan_mask;
2698 SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
2699 SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
2700 SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
2701 SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
2702 SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
2706 set_join_config_options(unsigned char enforce_mask, GucContext context)
2710 if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
2711 enforce_mask == ENABLE_HASHJOIN)
2712 mask = enforce_mask;
2714 mask = enforce_mask & current_hint_state->init_join_mask;
2716 SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
2717 SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
2718 SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
2722 * Push a hint into hint stack which is implemented with List struct. Head of
2723 * list is top of stack.
2726 push_hint(HintState *hstate)
2728 /* Prepend new hint to the list means pushing to stack. */
2729 HintStateStack = lcons(hstate, HintStateStack);
2731 /* Pushed hint is the one which should be used hereafter. */
2732 current_hint_state = hstate;
2735 /* Pop a hint from hint stack. Popped hint is automatically discarded. */
2739 /* Hint stack must not be empty. */
2740 if(HintStateStack == NIL)
2741 elog(ERROR, "hint stack is empty");
2744 * Take a hint at the head from the list, and free it. Switch
2745 * current_hint_state to point new head (NULL if the list is empty).
2747 HintStateStack = list_delete_first(HintStateStack);
2748 HintStateDelete(current_hint_state);
2749 if(HintStateStack == NIL)
2750 current_hint_state = NULL;
2752 current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
2756 * Retrieve and store a hint string from given query or from the hint table.
2757 * If we are using the hint table, the query string is needed to be normalized.
2758 * However, ParseState, which is not available in planner_hook, is required to
2759 * check if the query tree (Query) is surely corresponding to the target query.
2762 pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
2764 const char *query_str;
2765 MemoryContext oldcontext;
2767 if (prev_post_parse_analyze_hook)
2768 prev_post_parse_analyze_hook(pstate, query);
2770 /* do nothing under hint table search */
2771 if (hint_inhibit_level > 0)
2774 if (!pg_hint_plan_enable_hint)
2776 if (current_hint_str)
2778 pfree((void *)current_hint_str);
2779 current_hint_str = NULL;
2784 /* increment the query number */
2786 if (debug_level > 1)
2787 snprintf(qnostr, sizeof(qnostr), "[qno=0x%x]", qno++);
2790 /* search the hint table for a hint if requested */
2791 if (pg_hint_plan_enable_hint_table)
2794 pgssJumbleState jstate;
2796 char *normalized_query = NULL;
2798 query_str = get_query_string(pstate, query, &jumblequery);
2800 /* If this query is not for hint, just return */
2804 /* clear the previous hint string */
2805 if (current_hint_str)
2807 pfree((void *)current_hint_str);
2808 current_hint_str = NULL;
2814 * XXX: normalizing code is copied from pg_stat_statements.c, so be
2815 * careful to PostgreSQL's version up.
2817 jstate.jumble = (unsigned char *) palloc(JUMBLE_SIZE);
2818 jstate.jumble_len = 0;
2819 jstate.clocations_buf_size = 32;
2820 jstate.clocations = (pgssLocationLen *)
2821 palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
2822 jstate.clocations_count = 0;
2824 JumbleQuery(&jstate, jumblequery);
2827 * Normalize the query string by replacing constants with '?'
2830 * Search hint string which is stored keyed by query string
2831 * and application name. The query string is normalized to allow
2834 * Adding 1 byte to query_len ensures that the returned string has
2835 * a terminating NULL.
2837 query_len = strlen(query_str) + 1;
2839 generate_normalized_query(&jstate, query_str,
2840 query->stmt_location,
2842 GetDatabaseEncoding());
2845 * find a hint for the normalized query. the result should be in
2848 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2850 get_hints_from_table(normalized_query, application_name);
2851 MemoryContextSwitchTo(oldcontext);
2853 if (debug_level > 1)
2855 if (current_hint_str)
2856 ereport(pg_hint_plan_debug_message_level,
2857 (errmsg("pg_hint_plan[qno=0x%x]: "
2858 "post_parse_analyze_hook: "
2859 "hints from table: \"%s\": "
2860 "normalized_query=\"%s\", "
2861 "application name =\"%s\"",
2862 qno, current_hint_str,
2863 normalized_query, application_name),
2864 errhidestmt(msgqno != qno),
2865 errhidecontext(msgqno != qno)));
2867 ereport(pg_hint_plan_debug_message_level,
2868 (errmsg("pg_hint_plan[qno=0x%x]: "
2869 "no match found in table: "
2870 "application name = \"%s\", "
2871 "normalized_query=\"%s\"",
2872 qno, application_name,
2874 errhidestmt(msgqno != qno),
2875 errhidecontext(msgqno != qno)));
2881 /* retrun if we have hint here */
2882 if (current_hint_str)
2886 query_str = get_query_string(pstate, query, NULL);
2891 * get hints from the comment. However we may have the same query
2892 * string with the previous call, but just retrieving hints is expected
2893 * to be faster than checking for identicalness before retrieval.
2895 if (current_hint_str)
2896 pfree((void *)current_hint_str);
2898 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2899 current_hint_str = get_hints_from_comment(query_str);
2900 MemoryContextSwitchTo(oldcontext);
2903 if (debug_level > 1)
2905 if (debug_level == 1 &&
2906 (stmt_name || strcmp(query_str, debug_query_string)))
2907 ereport(pg_hint_plan_debug_message_level,
2908 (errmsg("hints in comment=\"%s\"",
2909 current_hint_str ? current_hint_str : "(none)"),
2910 errhidestmt(msgqno != qno),
2911 errhidecontext(msgqno != qno)));
2913 ereport(pg_hint_plan_debug_message_level,
2914 (errmsg("hints in comment=\"%s\", stmt=\"%s\", query=\"%s\", debug_query_string=\"%s\"",
2915 current_hint_str ? current_hint_str : "(none)",
2916 stmt_name, query_str, debug_query_string),
2917 errhidestmt(msgqno != qno),
2918 errhidecontext(msgqno != qno)));
2924 * Read and set up hint information
2926 static PlannedStmt *
2927 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
2930 PlannedStmt *result;
2934 * Use standard planner if pg_hint_plan is disabled or current nesting
2935 * depth is nesting depth of SPI calls. Other hook functions try to change
2936 * plan with current_hint_state if any, so set it to NULL.
2938 if (!pg_hint_plan_enable_hint || hint_inhibit_level > 0)
2940 if (debug_level > 1)
2941 ereport(pg_hint_plan_debug_message_level,
2942 (errmsg ("pg_hint_plan%s: planner: enable_hint=%d,"
2943 " hint_inhibit_level=%d",
2944 qnostr, pg_hint_plan_enable_hint,
2945 hint_inhibit_level),
2946 errhidestmt(msgqno != qno)));
2949 goto standard_planner_proc;
2953 * Support for nested plpgsql functions. This is quite ugly but this is the
2954 * only point I could find where I can get the query string.
2956 if (plpgsql_recurse_level > 0)
2958 MemoryContext oldcontext;
2960 if (current_hint_str)
2961 pfree((void *)current_hint_str);
2963 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2965 get_hints_from_comment((char *)error_context_stack->arg);
2966 MemoryContextSwitchTo(oldcontext);
2969 if (!current_hint_str)
2970 goto standard_planner_proc;
2972 /* parse the hint into hint state struct */
2973 hstate = create_hintstate(parse, pstrdup(current_hint_str));
2975 /* run standard planner if the statement has not valid hint */
2977 goto standard_planner_proc;
2980 * Push new hint struct to the hint stack to disable previous hint context.
2984 /* Set scan enforcement here. */
2985 save_nestlevel = NewGUCNestLevel();
2987 /* Apply Set hints, then save it as the initial state */
2988 setup_guc_enforcement(current_hint_state->set_hints,
2989 current_hint_state->num_hints[HINT_TYPE_SET],
2990 current_hint_state->context);
2992 current_hint_state->init_scan_mask = get_current_scan_mask();
2993 current_hint_state->init_join_mask = get_current_join_mask();
2994 current_hint_state->init_min_para_tablescan_size =
2995 min_parallel_table_scan_size;
2996 current_hint_state->init_min_para_indexscan_size =
2997 min_parallel_index_scan_size;
2998 current_hint_state->init_paratup_cost = parallel_tuple_cost;
2999 current_hint_state->init_parasetup_cost = parallel_setup_cost;
3002 * max_parallel_workers_per_gather should be non-zero here if Workers hint
3005 if (max_hint_nworkers > 0 && max_parallel_workers_per_gather < 1)
3006 set_config_int32_option("max_parallel_workers_per_gather",
3007 1, current_hint_state->context);
3008 current_hint_state->init_nworkers = max_parallel_workers_per_gather;
3010 if (debug_level > 1)
3012 ereport(pg_hint_plan_debug_message_level,
3013 (errhidestmt(msgqno != qno),
3014 errmsg("pg_hint_plan%s: planner", qnostr)));
3019 * Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
3020 * the state when this planner started when error occurred in planner.
3025 result = (*prev_planner) (parse, cursorOptions, boundParams);
3027 result = standard_planner(parse, cursorOptions, boundParams);
3032 * Rollback changes of GUC parameters, and pop current hint context
3033 * from hint stack to rewind the state.
3035 AtEOXact_GUC(true, save_nestlevel);
3041 /* Print hint in debug mode. */
3042 if (debug_level == 1)
3043 HintStateDump(current_hint_state);
3044 else if (debug_level > 1)
3045 HintStateDump2(current_hint_state);
3048 * Rollback changes of GUC parameters, and pop current hint context from
3049 * hint stack to rewind the state.
3051 AtEOXact_GUC(true, save_nestlevel);
3056 standard_planner_proc:
3057 if (debug_level > 1)
3059 ereport(pg_hint_plan_debug_message_level,
3060 (errhidestmt(msgqno != qno),
3061 errmsg("pg_hint_plan%s: planner: no valid hint",
3065 current_hint_state = NULL;
3067 return (*prev_planner) (parse, cursorOptions, boundParams);
3069 return standard_planner(parse, cursorOptions, boundParams);
3073 * Find scan method hint to be applied to the given relation
3076 static ScanMethodHint *
3077 find_scan_hint(PlannerInfo *root, Index relid)
3081 ScanMethodHint *real_name_hint = NULL;
3082 ScanMethodHint *alias_hint = NULL;
3085 /* This should not be a join rel */
3087 rel = root->simple_rel_array[relid];
3090 * This function is called for any RelOptInfo or its inheritance parent if
3091 * any. If we are called from inheritance planner, the RelOptInfo for the
3092 * parent of target child relation is not set in the planner info.
3094 * Otherwise we should check that the reloptinfo is base relation or
3095 * inheritance children.
3098 rel->reloptkind != RELOPT_BASEREL &&
3099 rel->reloptkind != RELOPT_OTHER_MEMBER_REL)
3103 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3105 rte = root->simple_rte_array[relid];
3108 /* We don't hint on other than relation and foreign tables */
3109 if (rte->rtekind != RTE_RELATION ||
3110 rte->relkind == RELKIND_FOREIGN_TABLE)
3113 /* Find scan method hint, which matches given names, from the list. */
3114 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
3116 ScanMethodHint *hint = current_hint_state->scan_hints[i];
3118 /* We ignore disabled hints. */
3119 if (!hint_state_enabled(hint))
3123 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3126 /* check the real name for appendrel children */
3127 if (!real_name_hint &&
3128 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3130 char *realname = get_rel_name(rte->relid);
3132 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3133 real_name_hint = hint;
3136 /* No more match expected, break */
3137 if(alias_hint && real_name_hint)
3141 /* real name match precedes alias match */
3143 return real_name_hint;
3148 static ParallelHint *
3149 find_parallel_hint(PlannerInfo *root, Index relid)
3153 ParallelHint *real_name_hint = NULL;
3154 ParallelHint *alias_hint = NULL;
3157 /* This should not be a join rel */
3159 rel = root->simple_rel_array[relid];
3162 * Parallel planning is appliable only on base relation, which has
3169 * We have set root->glob->parallelModeOK if needed. What we should do here
3170 * is just following the decision of planner.
3172 if (!rel->consider_parallel)
3176 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3178 rte = root->simple_rte_array[relid];
3181 /* Find parallel method hint, which matches given names, from the list. */
3182 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_PARALLEL]; i++)
3184 ParallelHint *hint = current_hint_state->parallel_hints[i];
3186 /* We ignore disabled hints. */
3187 if (!hint_state_enabled(hint))
3191 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3194 /* check the real name for appendrel children */
3195 if (!real_name_hint &&
3196 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3198 char *realname = get_rel_name(rte->relid);
3200 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3201 real_name_hint = hint;
3204 /* No more match expected, break */
3205 if(alias_hint && real_name_hint)
3209 /* real name match precedes alias match */
3211 return real_name_hint;
3219 * Returns TRUE on match, FALSE on no match.
3221 * s1 --- the data to match against
3222 * s2 --- the pattern
3224 * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
3227 regexpeq(const char *s1, const char *s2)
3233 strcpy(name.data, s1);
3234 regexp = cstring_to_text(s2);
3236 result = DirectFunctionCall2Coll(nameregexeq,
3237 DEFAULT_COLLATION_OID,
3238 NameGetDatum(&name),
3239 PointerGetDatum(regexp));
3240 return DatumGetBool(result);
3244 /* Remove indexes instructed not to use by hint. */
3246 restrict_indexes(PlannerInfo *root, ScanMethodHint *hint, RelOptInfo *rel,
3247 bool using_parent_hint)
3253 RangeTblEntry *rte = root->simple_rte_array[rel->relid];
3254 Oid relationObjectId = rte->relid;
3257 * We delete all the IndexOptInfo list and prevent you from being usable by
3260 if (hint->enforce_mask == ENABLE_SEQSCAN ||
3261 hint->enforce_mask == ENABLE_TIDSCAN)
3263 list_free_deep(rel->indexlist);
3264 rel->indexlist = NIL;
3265 hint->base.state = HINT_STATE_USED;
3271 * When a list of indexes is not specified, we just use all indexes.
3273 if (hint->indexnames == NIL)
3277 * Leaving only an specified index, we delete it from a IndexOptInfo list
3281 if (debug_level > 0)
3282 initStringInfo(&buf);
3284 for (cell = list_head(rel->indexlist); cell; cell = next)
3286 IndexOptInfo *info = (IndexOptInfo *) lfirst(cell);
3287 char *indexname = get_rel_name(info->indexoid);
3289 bool use_index = false;
3293 foreach(l, hint->indexnames)
3295 char *hintname = (char *) lfirst(l);
3299 result = regexpeq(indexname, hintname);
3301 result = RelnameCmp(&indexname, &hintname) == 0;
3306 if (debug_level > 0)
3308 appendStringInfoCharMacro(&buf, ' ');
3309 quote_value(&buf, indexname);
3317 * Apply index restriction of parent hint to children. Since index
3318 * inheritance is not explicitly described we should search for an
3319 * children's index with the same definition to that of the parent.
3321 if (using_parent_hint && !use_index)
3323 foreach(l, current_hint_state->parent_index_infos)
3327 ParentIndexInfo *p_info = (ParentIndexInfo *)lfirst(l);
3330 * we check the 'same' index by comparing uniqueness, access
3331 * method and index key columns.
3333 if (p_info->indisunique != info->unique ||
3334 p_info->method != info->relam ||
3335 list_length(p_info->column_names) != info->ncolumns)
3338 /* Check if index key columns match */
3339 for (i = 0; i < info->ncolumns; i++)
3341 char *c_attname = NULL;
3342 char *p_attname = NULL;
3344 p_attname = list_nth(p_info->column_names, i);
3347 * if both of the key of the same position are expressions,
3348 * ignore them for now and check later.
3350 if (info->indexkeys[i] == 0 && !p_attname)
3353 /* deny if one is expression while another is not */
3354 if (info->indexkeys[i] == 0 || !p_attname)
3357 c_attname = get_attname(relationObjectId,
3358 info->indexkeys[i], false);
3360 /* deny if any of column attributes don't match */
3361 if (strcmp(p_attname, c_attname) != 0 ||
3362 p_info->indcollation[i] != info->indexcollations[i] ||
3363 p_info->opclass[i] != info->opcintype[i]||
3364 ((p_info->indoption[i] & INDOPTION_DESC) != 0)
3365 != info->reverse_sort[i] ||
3366 ((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0)
3367 != info->nulls_first[i])
3371 /* deny this if any difference found */
3372 if (i != info->ncolumns)
3375 /* check on key expressions */
3376 if ((p_info->expression_str && (info->indexprs != NIL)) ||
3377 (p_info->indpred_str && (info->indpred != NIL)))
3379 /* fetch the index of this child */
3380 ht_idx = SearchSysCache1(INDEXRELID,
3381 ObjectIdGetDatum(info->indexoid));
3383 /* check expressions if both expressions are available */
3384 if (p_info->expression_str &&
3385 !heap_attisnull(ht_idx, Anum_pg_index_indexprs, NULL))
3392 * to change the expression's parameter of child's
3395 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3396 Anum_pg_index_indexprs,
3399 result = DirectFunctionCall2(pg_get_expr,
3404 /* deny if expressions don't match */
3405 if (strcmp(p_info->expression_str,
3406 text_to_cstring(DatumGetTextP(result))) != 0)
3409 ReleaseSysCache(ht_idx);
3414 /* compare index predicates */
3415 if (p_info->indpred_str &&
3416 !heap_attisnull(ht_idx, Anum_pg_index_indpred, NULL))
3422 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3423 Anum_pg_index_indpred,
3426 result = DirectFunctionCall2(pg_get_expr,
3431 if (strcmp(p_info->indpred_str,
3432 text_to_cstring(DatumGetTextP(result))) != 0)
3435 ReleaseSysCache(ht_idx);
3441 ReleaseSysCache(ht_idx);
3443 else if (p_info->expression_str || (info->indexprs != NIL))
3445 else if (p_info->indpred_str || (info->indpred != NIL))
3450 /* to log the candidate of index */
3451 if (debug_level > 0)
3453 appendStringInfoCharMacro(&buf, ' ');
3454 quote_value(&buf, indexname);
3462 rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
3469 if (debug_level == 1)
3471 StringInfoData rel_buf;
3472 char *disprelname = "";
3475 * If this hint targetted the parent, use the real name of this
3476 * child. Otherwise use hint specification.
3478 if (using_parent_hint)
3479 disprelname = get_rel_name(rte->relid);
3481 disprelname = hint->relname;
3484 initStringInfo(&rel_buf);
3485 quote_value(&rel_buf, disprelname);
3487 ereport(pg_hint_plan_debug_message_level,
3488 (errmsg("available indexes for %s(%s):%s",
3493 pfree(rel_buf.data);
3498 * Return information of index definition.
3500 static ParentIndexInfo *
3501 get_parent_index_info(Oid indexoid, Oid relid)
3503 ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
3504 Relation indexRelation;
3505 Form_pg_index index;
3509 indexRelation = index_open(indexoid, RowExclusiveLock);
3511 index = indexRelation->rd_index;
3513 p_info->indisunique = index->indisunique;
3514 p_info->method = indexRelation->rd_rel->relam;
3516 p_info->column_names = NIL;
3517 p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3518 p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3519 p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
3522 * Collect relation attribute names of index columns for index
3523 * identification, not index attribute names. NULL means expression index
3526 for (i = 0; i < index->indnatts; i++)
3528 attname = get_attname(relid, index->indkey.values[i], true);
3529 p_info->column_names = lappend(p_info->column_names, attname);
3531 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
3532 p_info->opclass[i] = indexRelation->rd_opcintype[i];
3533 p_info->indoption[i] = indexRelation->rd_indoption[i];
3537 * to check to match the expression's parameter of index with child indexes
3539 p_info->expression_str = NULL;
3540 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs,
3547 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3548 Anum_pg_index_indexprs, &isnull);
3550 result = DirectFunctionCall2(pg_get_expr,
3552 ObjectIdGetDatum(relid));
3554 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
3558 * to check to match the predicate's parameter of index with child indexes
3560 p_info->indpred_str = NULL;
3561 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred,
3568 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3569 Anum_pg_index_indpred, &isnull);
3571 result = DirectFunctionCall2(pg_get_expr,
3573 ObjectIdGetDatum(relid));
3575 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
3578 index_close(indexRelation, NoLock);
3584 * cancel hint enforcement
3587 reset_hint_enforcement()
3589 setup_scan_method_enforcement(NULL, current_hint_state);
3590 setup_parallel_plan_enforcement(NULL, current_hint_state);
3594 * Set planner guc parameters according to corresponding scan hints. Returns
3595 * bitmap of HintTypeBitmap. If shint or phint is not NULL, set used hint
3596 * there respectively.
3599 setup_hint_enforcement(PlannerInfo *root, RelOptInfo *rel,
3600 ScanMethodHint **rshint, ParallelHint **rphint)
3602 Index new_parent_relid = 0;
3604 ScanMethodHint *shint = NULL;
3605 ParallelHint *phint = NULL;
3606 bool inhparent = root->simple_rte_array[rel->relid]->inh;
3607 Oid relationObjectId = root->simple_rte_array[rel->relid]->relid;
3610 /* reset returns if requested */
3611 if (rshint != NULL) *rshint = NULL;
3612 if (rphint != NULL) *rphint = NULL;
3615 * We could register the parent relation of the following children here
3616 * when inhparent == true but inheritnce planner doesn't call this function
3617 * for parents. Since we cannot distinguish who called this function we
3618 * cannot do other than always seeking the parent regardless of who called
3623 /* set up only parallel hints for parent relation */
3624 phint = find_parallel_hint(root, rel->relid);
3627 setup_parallel_plan_enforcement(phint, current_hint_state);
3628 if (rphint) *rphint = phint;
3629 ret |= HINT_BM_PARALLEL;
3633 if (debug_level > 1)
3634 ereport(pg_hint_plan_debug_message_level,
3636 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3637 " skipping inh parent: relation=%u(%s), inhparent=%d,"
3638 " current_hint_state=%p, hint_inhibit_level=%d",
3639 qnostr, relationObjectId,
3640 get_rel_name(relationObjectId),
3641 inhparent, current_hint_state, hint_inhibit_level)));
3645 /* Find the parent for this relation other than the registered parent */
3646 foreach (l, root->append_rel_list)
3648 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3650 if (appinfo->child_relid == rel->relid)
3652 if (current_hint_state->parent_relid != appinfo->parent_relid)
3653 new_parent_relid = appinfo->parent_relid;
3660 /* This relation doesn't have a parent. Cancel current_hint_state. */
3661 current_hint_state->parent_relid = 0;
3662 current_hint_state->parent_scan_hint = NULL;
3663 current_hint_state->parent_parallel_hint = NULL;
3666 if (new_parent_relid > 0)
3669 * Here we found a new parent for the current relation. Scan continues
3670 * hint to other childrens of this parent so remember it to avoid
3671 * redundant setup cost.
3673 current_hint_state->parent_relid = new_parent_relid;
3675 /* Find hints for the parent */
3676 current_hint_state->parent_scan_hint =
3677 find_scan_hint(root, current_hint_state->parent_relid);
3679 current_hint_state->parent_parallel_hint =
3680 find_parallel_hint(root, current_hint_state->parent_relid);
3683 * If hint is found for the parent, apply it for this child instead
3686 if (current_hint_state->parent_scan_hint)
3688 ScanMethodHint * pshint = current_hint_state->parent_scan_hint;
3690 pshint->base.state = HINT_STATE_USED;
3692 /* Apply index mask in the same manner to the parent. */
3693 if (pshint->indexnames)
3696 Relation parent_rel;
3699 root->simple_rte_array[current_hint_state->parent_relid]->relid;
3700 parent_rel = heap_open(parentrel_oid, NoLock);
3702 /* Search the parent relation for indexes match the hint spec */
3703 foreach(l, RelationGetIndexList(parent_rel))
3705 Oid indexoid = lfirst_oid(l);
3706 char *indexname = get_rel_name(indexoid);
3708 ParentIndexInfo *parent_index_info;
3710 foreach(lc, pshint->indexnames)
3712 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
3719 get_parent_index_info(indexoid, parentrel_oid);
3720 current_hint_state->parent_index_infos =
3721 lappend(current_hint_state->parent_index_infos,
3724 heap_close(parent_rel, NoLock);
3729 shint = find_scan_hint(root, rel->relid);
3731 shint = current_hint_state->parent_scan_hint;
3735 bool using_parent_hint =
3736 (shint == current_hint_state->parent_scan_hint);
3738 ret |= HINT_BM_SCAN_METHOD;
3740 /* Setup scan enforcement environment */
3741 setup_scan_method_enforcement(shint, current_hint_state);
3743 /* restrict unwanted inexes */
3744 restrict_indexes(root, shint, rel, using_parent_hint);
3746 if (debug_level > 1)
3748 char *additional_message = "";
3750 if (shint == current_hint_state->parent_scan_hint)
3751 additional_message = " by parent hint";
3753 ereport(pg_hint_plan_debug_message_level,
3755 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3756 " index deletion%s:"
3757 " relation=%u(%s), inhparent=%d, "
3758 "current_hint_state=%p,"
3759 " hint_inhibit_level=%d, scanmask=0x%x",
3760 qnostr, additional_message,
3762 get_rel_name(relationObjectId),
3763 inhparent, current_hint_state,
3765 shint->enforce_mask)));
3769 /* Do the same for parallel plan enforcement */
3770 phint = find_parallel_hint(root, rel->relid);
3772 phint = current_hint_state->parent_parallel_hint;
3774 setup_parallel_plan_enforcement(phint, current_hint_state);
3777 ret |= HINT_BM_PARALLEL;
3779 /* Nothing to apply. Reset the scan mask to intial state */
3780 if (!shint && ! phint)
3782 if (debug_level > 1)
3783 ereport(pg_hint_plan_debug_message_level,
3784 (errhidestmt (true),
3785 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3787 " relation=%u(%s), inhparent=%d, current_hint=%p,"
3788 " hint_inhibit_level=%d, scanmask=0x%x",
3789 qnostr, relationObjectId,
3790 get_rel_name(relationObjectId),
3791 inhparent, current_hint_state, hint_inhibit_level,
3792 current_hint_state->init_scan_mask)));
3794 setup_scan_method_enforcement(NULL, current_hint_state);
3799 if (rshint != NULL) *rshint = shint;
3800 if (rphint != NULL) *rphint = phint;
3806 * Return index of relation which matches given aliasname, or 0 if not found.
3807 * If same aliasname was used multiple times in a query, return -1.
3810 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
3816 for (i = 1; i < root->simple_rel_array_size; i++)
3820 if (root->simple_rel_array[i] == NULL)
3823 Assert(i == root->simple_rel_array[i]->relid);
3825 if (RelnameCmp(&aliasname,
3826 &root->simple_rte_array[i]->eref->aliasname) != 0)
3829 foreach(l, initial_rels)
3831 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3833 if (rel->reloptkind == RELOPT_BASEREL)
3835 if (rel->relid != i)
3840 Assert(rel->reloptkind == RELOPT_JOINREL);
3842 if (!bms_is_member(i, rel->relids))
3849 ("Relation name \"%s\" is ambiguous.",
3864 * Return join hint which matches given joinrelids.
3866 static JoinMethodHint *
3867 find_join_hint(Relids joinrelids)
3872 join_hint = current_hint_state->join_hint_level[bms_num_members(joinrelids)];
3874 foreach(l, join_hint)
3876 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
3878 if (bms_equal(joinrelids, hint->joinrelids))
3886 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
3887 PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
3889 OuterInnerRels *outer_rels;
3890 OuterInnerRels *inner_rels;
3891 Relids outer_relids;
3892 Relids inner_relids;
3894 JoinMethodHint *hint;
3896 if (outer_inner->relation != NULL)
3898 return bms_make_singleton(
3899 find_relid_aliasname(root, outer_inner->relation,
3901 leading_hint->base.hint_str));
3904 outer_rels = lfirst(outer_inner->outer_inner_pair->head);
3905 inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
3907 outer_relids = OuterInnerJoinCreate(outer_rels,
3913 inner_relids = OuterInnerJoinCreate(inner_rels,
3920 join_relids = bms_add_members(outer_relids, inner_relids);
3922 if (bms_num_members(join_relids) > nbaserel)
3926 * If we don't have join method hint, create new one for the
3927 * join combination with all join methods are enabled.
3929 hint = find_join_hint(join_relids);
3933 * Here relnames is not set, since Relids bitmap is sufficient to
3934 * control paths of this query afterward.
3936 hint = (JoinMethodHint *) JoinMethodHintCreate(
3937 leading_hint->base.hint_str,
3939 HINT_KEYWORD_LEADING);
3940 hint->base.state = HINT_STATE_USED;
3941 hint->nrels = bms_num_members(join_relids);
3942 hint->enforce_mask = ENABLE_ALL_JOIN;
3943 hint->joinrelids = bms_copy(join_relids);
3944 hint->inner_nrels = bms_num_members(inner_relids);
3945 hint->inner_joinrelids = bms_copy(inner_relids);
3947 hstate->join_hint_level[hint->nrels] =
3948 lappend(hstate->join_hint_level[hint->nrels], hint);
3952 hint->inner_nrels = bms_num_members(inner_relids);
3953 hint->inner_joinrelids = bms_copy(inner_relids);
3960 create_bms_of_relids(Hint *base, PlannerInfo *root, List *initial_rels,
3961 int nrels, char **relnames)
3964 Relids relids = NULL;
3968 for (j = 0; j < nrels; j++)
3970 relname = relnames[j];
3972 relid = find_relid_aliasname(root, relname, initial_rels,
3976 base->state = HINT_STATE_ERROR;
3979 * the aliasname is not found(relid == 0) or same aliasname was used
3980 * multiple times in a query(relid == -1)
3987 if (bms_is_member(relid, relids))
3989 hint_ereport(base->hint_str,
3990 ("Relation name \"%s\" is duplicated.", relname));
3991 base->state = HINT_STATE_ERROR;
3995 relids = bms_add_member(relids, relid);
4000 * Transform join method hint into handy form.
4002 * - create bitmap of relids from alias names, to make it easier to check
4003 * whether a join path matches a join method hint.
4004 * - add join method hints which are necessary to enforce join order
4005 * specified by Leading hint
4008 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
4009 List *initial_rels, JoinMethodHint **join_method_hints)
4017 LeadingHint *lhint = NULL;
4020 * Create bitmap of relids from alias names for each join method hint.
4021 * Bitmaps are more handy than strings in join searching.
4023 for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
4025 JoinMethodHint *hint = hstate->join_hints[i];
4027 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4030 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4031 initial_rels, hint->nrels, hint->relnames);
4033 if (hint->joinrelids == NULL || hint->base.state == HINT_STATE_ERROR)
4036 hstate->join_hint_level[hint->nrels] =
4037 lappend(hstate->join_hint_level[hint->nrels], hint);
4041 * Create bitmap of relids from alias names for each rows hint.
4042 * Bitmaps are more handy than strings in join searching.
4044 for (i = 0; i < hstate->num_hints[HINT_TYPE_ROWS]; i++)
4046 RowsHint *hint = hstate->rows_hints[i];
4048 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4051 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4052 initial_rels, hint->nrels, hint->relnames);
4055 /* Do nothing if no Leading hint was supplied. */
4056 if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
4060 * Decide whether to use Leading hint
4062 for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
4064 LeadingHint *leading_hint = (LeadingHint *)hstate->leading_hint[i];
4067 if (leading_hint->base.state == HINT_STATE_ERROR)
4073 foreach(l, leading_hint->relations)
4075 relname = (char *)lfirst(l);;
4077 relid = find_relid_aliasname(root, relname, initial_rels,
4078 leading_hint->base.hint_str);
4080 leading_hint->base.state = HINT_STATE_ERROR;
4085 if (bms_is_member(relid, relids))
4087 hint_ereport(leading_hint->base.hint_str,
4088 ("Relation name \"%s\" is duplicated.", relname));
4089 leading_hint->base.state = HINT_STATE_ERROR;
4093 relids = bms_add_member(relids, relid);
4096 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
4101 hint_ereport(lhint->base.hint_str,
4102 ("Conflict %s hint.", HintTypeName[lhint->base.type]));
4103 lhint->base.state = HINT_STATE_DUPLICATION;
4105 leading_hint->base.state = HINT_STATE_USED;
4106 lhint = leading_hint;
4109 /* check to exist Leading hint marked with 'used'. */
4114 * We need join method hints which fit specified join order in every join
4115 * level. For example, Leading(A B C) virtually requires following join
4116 * method hints, if no join method hint supplied:
4118 * - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
4119 * - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
4121 * If we already have join method hint which fits specified join order in
4122 * that join level, we leave it as-is and don't add new hints.
4126 if (lhint->outer_inner == NULL)
4128 foreach(l, lhint->relations)
4130 JoinMethodHint *hint;
4132 relname = (char *)lfirst(l);
4135 * Find relid of the relation which has given name. If we have the
4136 * name given in Leading hint multiple times in the join, nothing to
4139 relid = find_relid_aliasname(root, relname, initial_rels,
4142 /* Create bitmap of relids for current join level. */
4143 joinrelids = bms_add_member(joinrelids, relid);
4146 /* We never have join method hint for single relation. */
4151 * If we don't have join method hint, create new one for the
4152 * join combination with all join methods are enabled.
4154 hint = find_join_hint(joinrelids);
4158 * Here relnames is not set, since Relids bitmap is sufficient
4159 * to control paths of this query afterward.
4161 hint = (JoinMethodHint *) JoinMethodHintCreate(
4162 lhint->base.hint_str,
4164 HINT_KEYWORD_LEADING);
4165 hint->base.state = HINT_STATE_USED;
4166 hint->nrels = njoinrels;
4167 hint->enforce_mask = ENABLE_ALL_JOIN;
4168 hint->joinrelids = bms_copy(joinrelids);
4171 join_method_hints[njoinrels] = hint;
4173 if (njoinrels >= nbaserel)
4176 bms_free(joinrelids);
4182 * Delete all join hints which have different combination from Leading
4185 for (i = 2; i <= njoinrels; i++)
4187 list_free(hstate->join_hint_level[i]);
4189 hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
4194 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
4201 njoinrels = bms_num_members(joinrelids);
4202 Assert(njoinrels >= 2);
4205 * Delete all join hints which have different combination from Leading
4208 for (i = 2;i <= njoinrels; i++)
4210 if (hstate->join_hint_level[i] != NIL)
4212 ListCell *prev = NULL;
4213 ListCell *next = NULL;
4214 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
4217 JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
4221 if (hint->inner_nrels == 0 &&
4222 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
4223 bms_equal(bms_union(hint->joinrelids, joinrelids),
4226 hstate->join_hint_level[i] =
4227 list_delete_cell(hstate->join_hint_level[i], l,
4236 bms_free(joinrelids);
4239 if (hint_state_enabled(lhint))
4241 set_join_config_options(DISABLE_ALL_JOIN, current_hint_state->context);
4248 * wrapper of make_join_rel()
4250 * call make_join_rel() after changing enable_* parameters according to given
4254 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
4257 JoinMethodHint *hint;
4261 joinrelids = bms_union(rel1->relids, rel2->relids);
4262 hint = find_join_hint(joinrelids);
4263 bms_free(joinrelids);
4266 return pg_hint_plan_make_join_rel(root, rel1, rel2);
4268 if (hint->inner_nrels == 0)
4270 save_nestlevel = NewGUCNestLevel();
4272 set_join_config_options(hint->enforce_mask,
4273 current_hint_state->context);
4275 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4276 hint->base.state = HINT_STATE_USED;
4279 * Restore the GUC variables we set above.
4281 AtEOXact_GUC(true, save_nestlevel);
4284 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4293 add_paths_to_joinrel_wrapper(PlannerInfo *root,
4294 RelOptInfo *joinrel,
4295 RelOptInfo *outerrel,
4296 RelOptInfo *innerrel,
4298 SpecialJoinInfo *sjinfo,
4302 JoinMethodHint *join_hint;
4305 joinrelids = bms_union(outerrel->relids, innerrel->relids);
4306 join_hint = find_join_hint(joinrelids);
4307 bms_free(joinrelids);
4309 if (join_hint && join_hint->inner_nrels != 0)
4311 save_nestlevel = NewGUCNestLevel();
4313 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
4316 set_join_config_options(join_hint->enforce_mask,
4317 current_hint_state->context);
4319 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4320 sjinfo, restrictlist);
4321 join_hint->base.state = HINT_STATE_USED;
4325 set_join_config_options(DISABLE_ALL_JOIN,
4326 current_hint_state->context);
4327 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4328 sjinfo, restrictlist);
4332 * Restore the GUC variables we set above.
4334 AtEOXact_GUC(true, save_nestlevel);
4337 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4338 sjinfo, restrictlist);
4342 get_num_baserels(List *initial_rels)
4347 foreach(l, initial_rels)
4349 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
4351 if (rel->reloptkind == RELOPT_BASEREL)
4353 else if (rel->reloptkind ==RELOPT_JOINREL)
4354 nbaserel+= bms_num_members(rel->relids);
4357 /* other values not expected here */
4358 elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
4366 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
4369 JoinMethodHint **join_method_hints;
4373 bool leading_hint_enable;
4376 * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
4377 * valid hint is supplied or current nesting depth is nesting depth of SPI
4380 if (!current_hint_state || hint_inhibit_level > 0)
4382 if (prev_join_search)
4383 return (*prev_join_search) (root, levels_needed, initial_rels);
4384 else if (enable_geqo && levels_needed >= geqo_threshold)
4385 return geqo(root, levels_needed, initial_rels);
4387 return standard_join_search(root, levels_needed, initial_rels);
4391 * In the case using GEQO, only scan method hints and Set hints have
4392 * effect. Join method and join order is not controllable by hints.
4394 if (enable_geqo && levels_needed >= geqo_threshold)
4395 return geqo(root, levels_needed, initial_rels);
4397 nbaserel = get_num_baserels(initial_rels);
4398 current_hint_state->join_hint_level =
4399 palloc0(sizeof(List *) * (nbaserel + 1));
4400 join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
4402 leading_hint_enable = transform_join_hints(current_hint_state,
4404 initial_rels, join_method_hints);
4406 rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
4409 * Adjust number of parallel workers of the result rel to the largest
4410 * number of the component paths.
4412 if (current_hint_state->num_hints[HINT_TYPE_PARALLEL] > 0)
4417 foreach (lc, initial_rels)
4420 RelOptInfo *rel = (RelOptInfo *) lfirst(lc);
4422 foreach (lcp, rel->partial_pathlist)
4424 Path *path = (Path *) lfirst(lcp);
4426 if (nworkers < path-> parallel_workers)
4427 nworkers = path-> parallel_workers;
4431 foreach (lc, rel->partial_pathlist)
4433 Path *path = (Path *) lfirst(lc);
4435 if (path->parallel_safe && path->parallel_workers < nworkers)
4436 path->parallel_workers = nworkers;
4440 for (i = 2; i <= nbaserel; i++)
4442 list_free(current_hint_state->join_hint_level[i]);
4444 /* free Leading hint only */
4445 if (join_method_hints[i] != NULL &&
4446 join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
4447 JoinMethodHintDelete(join_method_hints[i]);
4449 pfree(current_hint_state->join_hint_level);
4450 pfree(join_method_hints);
4452 if (leading_hint_enable)
4453 set_join_config_options(current_hint_state->init_join_mask,
4454 current_hint_state->context);
4460 * Force number of wokers if instructed by hint
4463 pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
4464 Index rti, RangeTblEntry *rte)
4466 ParallelHint *phint;
4470 /* call the previous hook */
4471 if (prev_set_rel_pathlist)
4472 prev_set_rel_pathlist(root, rel, rti, rte);
4474 /* Nothing to do if no hint available */
4475 if (current_hint_state == NULL)
4478 /* Don't touch dummy rels. */
4479 if (IS_DUMMY_REL(rel))
4483 * We can accept only plain relations, foreign tables and table saples are
4484 * also unacceptable. See set_rel_pathlist.
4486 if ((rel->rtekind != RTE_RELATION &&
4487 rel->rtekind != RTE_SUBQUERY)||
4488 rte->relkind == RELKIND_FOREIGN_TABLE ||
4489 rte->tablesample != NULL)
4493 * Even though UNION ALL node doesn't have particular name so usually it is
4494 * unhintable, turn on parallel when it contains parallel nodes.
4496 if (rel->rtekind == RTE_SUBQUERY)
4499 bool inhibit_nonparallel = false;
4501 if (rel->partial_pathlist == NIL)
4504 foreach(lc, rel->partial_pathlist)
4507 AppendPath *apath = (AppendPath *) lfirst(lc);
4508 int parallel_workers = 0;
4510 if (!IsA(apath, AppendPath))
4513 foreach (lcp, apath->subpaths)
4515 Path *spath = (Path *) lfirst(lcp);
4517 if (spath->parallel_aware &&
4518 parallel_workers < spath->parallel_workers)
4519 parallel_workers = spath->parallel_workers;
4522 apath->path.parallel_workers = parallel_workers;
4523 inhibit_nonparallel = true;
4526 if (inhibit_nonparallel)
4530 foreach(lc, rel->pathlist)
4532 Path *path = (Path *) lfirst(lc);
4534 if (path->startup_cost < disable_cost)
4536 path->startup_cost += disable_cost;
4537 path->total_cost += disable_cost;
4545 /* We cannot handle if this requires an outer */
4546 if (rel->lateral_relids)
4549 /* Return if this relation gets no enfocement */
4550 if ((found_hints = setup_hint_enforcement(root, rel, NULL, &phint)) == 0)
4553 /* Here, we regenerate paths with the current hint restriction */
4554 if (found_hints & HINT_BM_SCAN_METHOD || found_hints & HINT_BM_PARALLEL)
4557 * When hint is specified on non-parent relations, discard existing
4558 * paths and regenerate based on the hint considered. Otherwise we
4559 * already have hinted childx paths then just adjust the number of
4560 * planned number of workers.
4562 if (root->simple_rte_array[rel->relid]->inh)
4564 /* enforce number of workers if requested */
4565 if (phint && phint->force_parallel)
4567 if (phint->nworkers == 0)
4569 list_free_deep(rel->partial_pathlist);
4570 rel->partial_pathlist = NIL;
4574 /* prioritize partial paths */
4575 foreach (l, rel->partial_pathlist)
4577 Path *ppath = (Path *) lfirst(l);
4579 if (ppath->parallel_safe)
4581 ppath->parallel_workers = phint->nworkers;
4582 ppath->startup_cost = 0;
4583 ppath->total_cost = 0;
4587 /* disable non-partial paths */
4588 foreach (l, rel->pathlist)
4590 Path *ppath = (Path *) lfirst(l);
4592 if (ppath->startup_cost < disable_cost)
4594 ppath->startup_cost += disable_cost;
4595 ppath->total_cost += disable_cost;
4603 /* Just discard all the paths considered so far */
4604 list_free_deep(rel->pathlist);
4605 rel->pathlist = NIL;
4606 list_free_deep(rel->partial_pathlist);
4607 rel->partial_pathlist = NIL;
4609 /* Regenerate paths with the current enforcement */
4610 set_plain_rel_pathlist(root, rel, rte);
4612 /* Additional work to enforce parallel query execution */
4613 if (phint && phint->nworkers > 0)
4616 * For Parallel Append to be planned properly, we shouldn't set
4617 * the costs of non-partial paths to disable-value. Lower the
4618 * priority of non-parallel paths by setting partial path costs
4621 foreach (l, rel->partial_pathlist)
4623 Path *path = (Path *) lfirst(l);
4625 path->startup_cost = 0;
4626 path->total_cost = 0;
4629 /* enforce number of workers if requested */
4630 if (phint->force_parallel)
4632 foreach (l, rel->partial_pathlist)
4634 Path *ppath = (Path *) lfirst(l);
4636 if (ppath->parallel_safe)
4637 ppath->parallel_workers = phint->nworkers;
4641 /* Generate gather paths */
4642 if (rel->reloptkind == RELOPT_BASEREL)
4643 generate_gather_paths(root, rel, false);
4648 reset_hint_enforcement();
4653 * Build access paths for a base relation
4655 * This function was copied and edited from set_rel_pathlist() in
4656 * src/backend/optimizer/path/allpaths.c in order not to copy other static
4657 * functions not required here.
4660 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
4661 Index rti, RangeTblEntry *rte)
4663 if (IS_DUMMY_REL(rel))
4665 /* We already proved the relation empty, so nothing more to do */
4669 /* It's an "append relation", process accordingly */
4670 set_append_rel_pathlist(root, rel, rti, rte);
4674 if (rel->rtekind == RTE_RELATION)
4676 if (rte->relkind == RELKIND_RELATION)
4678 if(rte->tablesample != NULL)
4679 elog(ERROR, "sampled relation is not supported");
4681 /* Plain relation */
4682 set_plain_rel_pathlist(root, rel, rte);
4685 elog(ERROR, "unexpected relkind: %c", rte->relkind);
4688 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
4692 * Allow a plugin to editorialize on the set of Paths for this base
4693 * relation. It could add new paths (such as CustomPaths) by calling
4694 * add_path(), or delete or modify paths added by the core code.
4696 if (set_rel_pathlist_hook)
4697 (*set_rel_pathlist_hook) (root, rel, rti, rte);
4699 /* Now find the cheapest of the paths for this rel */
4704 * stmt_beg callback is called when each query in PL/pgSQL function is about
4705 * to be executed. At that timing, we save query string in the global variable
4706 * plpgsql_query_string to use it in planner hook. It's safe to use one global
4707 * variable for the purpose, because its content is only necessary until
4708 * planner hook is called for the query, so recursive PL/pgSQL function calls
4709 * don't harm this mechanism.
4712 pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4714 plpgsql_recurse_level++;
4718 * stmt_end callback is called then each query in PL/pgSQL function has
4719 * finished. At that timing, we clear plpgsql_query_string to tell planner
4720 * hook that next call is not for a query written in PL/pgSQL block.
4723 pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4725 plpgsql_recurse_level--;
4728 void plpgsql_query_erase_callback(ResourceReleasePhase phase,
4733 if (phase != RESOURCE_RELEASE_AFTER_LOCKS)
4735 /* Cancel plpgsql nest level*/
4736 plpgsql_recurse_level = 0;
4739 #define standard_join_search pg_hint_plan_standard_join_search
4740 #define join_search_one_level pg_hint_plan_join_search_one_level
4741 #define make_join_rel make_join_rel_wrapper
4744 #undef make_join_rel
4745 #define make_join_rel pg_hint_plan_make_join_rel
4746 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
4747 #include "make_join_rel.c"
4749 #include "pg_stat_statements.c"