1 /*-------------------------------------------------------------------------
4 * hinting on how to execute a query for PostgreSQL
6 * Copyright (c) 2012-2019, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
8 *-------------------------------------------------------------------------
13 #include "catalog/pg_collation.h"
14 #include "catalog/pg_index.h"
15 #include "commands/prepare.h"
16 #include "mb/pg_wchar.h"
17 #include "miscadmin.h"
18 #include "nodes/nodeFuncs.h"
19 #include "nodes/params.h"
20 #include "nodes/relation.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/geqo.h"
24 #include "optimizer/joininfo.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/plancat.h"
28 #include "optimizer/planner.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/restrictinfo.h"
31 #include "parser/analyze.h"
32 #include "parser/parsetree.h"
33 #include "parser/scansup.h"
34 #include "tcop/utility.h"
35 #include "utils/builtins.h"
36 #include "utils/lsyscache.h"
37 #include "utils/memutils.h"
38 #include "utils/rel.h"
39 #include "utils/snapmgr.h"
40 #include "utils/syscache.h"
41 #include "utils/resowner.h"
43 #include "catalog/pg_class.h"
45 #include "executor/spi.h"
46 #include "catalog/pg_type.h"
50 /* partially copied from pg_stat_statements */
51 #include "normalize_query.h"
54 #include "access/htup_details.h"
56 #ifdef PG_MODULE_MAGIC
60 #define BLOCK_COMMENT_START "/*"
61 #define BLOCK_COMMENT_END "*/"
62 #define HINT_COMMENT_KEYWORD "+"
63 #define HINT_START BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
64 #define HINT_END BLOCK_COMMENT_END
67 #define HINT_SEQSCAN "SeqScan"
68 #define HINT_INDEXSCAN "IndexScan"
69 #define HINT_INDEXSCANREGEXP "IndexScanRegexp"
70 #define HINT_BITMAPSCAN "BitmapScan"
71 #define HINT_BITMAPSCANREGEXP "BitmapScanRegexp"
72 #define HINT_TIDSCAN "TidScan"
73 #define HINT_NOSEQSCAN "NoSeqScan"
74 #define HINT_NOINDEXSCAN "NoIndexScan"
75 #define HINT_NOBITMAPSCAN "NoBitmapScan"
76 #define HINT_NOTIDSCAN "NoTidScan"
77 #define HINT_INDEXONLYSCAN "IndexOnlyScan"
78 #define HINT_INDEXONLYSCANREGEXP "IndexOnlyScanRegexp"
79 #define HINT_NOINDEXONLYSCAN "NoIndexOnlyScan"
80 #define HINT_PARALLEL "Parallel"
82 #define HINT_NESTLOOP "NestLoop"
83 #define HINT_MERGEJOIN "MergeJoin"
84 #define HINT_HASHJOIN "HashJoin"
85 #define HINT_NONESTLOOP "NoNestLoop"
86 #define HINT_NOMERGEJOIN "NoMergeJoin"
87 #define HINT_NOHASHJOIN "NoHashJoin"
88 #define HINT_LEADING "Leading"
89 #define HINT_SET "Set"
90 #define HINT_ROWS "Rows"
92 #define HINT_ARRAY_DEFAULT_INITSIZE 8
94 #define hint_ereport(str, detail) hint_parse_ereport(str, detail)
95 #define hint_parse_ereport(str, detail) \
97 ereport(pg_hint_plan_parse_message_level, \
98 (errmsg("pg_hint_plan: hint syntax error at or near \"%s\"", (str)), \
102 #define skip_space(str) \
103 while (isspace(*str)) \
108 ENABLE_SEQSCAN = 0x01,
109 ENABLE_INDEXSCAN = 0x02,
110 ENABLE_BITMAPSCAN = 0x04,
111 ENABLE_TIDSCAN = 0x08,
112 ENABLE_INDEXONLYSCAN = 0x10
117 ENABLE_NESTLOOP = 0x01,
118 ENABLE_MERGEJOIN = 0x02,
119 ENABLE_HASHJOIN = 0x04
122 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
123 ENABLE_BITMAPSCAN | ENABLE_TIDSCAN | \
124 ENABLE_INDEXONLYSCAN)
125 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
126 #define DISABLE_ALL_SCAN 0
127 #define DISABLE_ALL_JOIN 0
129 /* hint keyword of enum type*/
130 typedef enum HintKeyword
132 HINT_KEYWORD_SEQSCAN,
133 HINT_KEYWORD_INDEXSCAN,
134 HINT_KEYWORD_INDEXSCANREGEXP,
135 HINT_KEYWORD_BITMAPSCAN,
136 HINT_KEYWORD_BITMAPSCANREGEXP,
137 HINT_KEYWORD_TIDSCAN,
138 HINT_KEYWORD_NOSEQSCAN,
139 HINT_KEYWORD_NOINDEXSCAN,
140 HINT_KEYWORD_NOBITMAPSCAN,
141 HINT_KEYWORD_NOTIDSCAN,
142 HINT_KEYWORD_INDEXONLYSCAN,
143 HINT_KEYWORD_INDEXONLYSCANREGEXP,
144 HINT_KEYWORD_NOINDEXONLYSCAN,
146 HINT_KEYWORD_NESTLOOP,
147 HINT_KEYWORD_MERGEJOIN,
148 HINT_KEYWORD_HASHJOIN,
149 HINT_KEYWORD_NONESTLOOP,
150 HINT_KEYWORD_NOMERGEJOIN,
151 HINT_KEYWORD_NOHASHJOIN,
153 HINT_KEYWORD_LEADING,
156 HINT_KEYWORD_PARALLEL,
158 HINT_KEYWORD_UNRECOGNIZED
161 #define SCAN_HINT_ACCEPTS_INDEX_NAMES(kw) \
162 (kw == HINT_KEYWORD_INDEXSCAN || \
163 kw == HINT_KEYWORD_INDEXSCANREGEXP || \
164 kw == HINT_KEYWORD_INDEXONLYSCAN || \
165 kw == HINT_KEYWORD_INDEXONLYSCANREGEXP || \
166 kw == HINT_KEYWORD_BITMAPSCAN || \
167 kw == HINT_KEYWORD_BITMAPSCANREGEXP)
170 typedef struct Hint Hint;
171 typedef struct HintState HintState;
173 typedef Hint *(*HintCreateFunction) (const char *hint_str,
175 HintKeyword hint_keyword);
176 typedef void (*HintDeleteFunction) (Hint *hint);
177 typedef void (*HintDescFunction) (Hint *hint, StringInfo buf, bool nolf);
178 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
179 typedef const char *(*HintParseFunction) (Hint *hint, HintState *hstate,
180 Query *parse, const char *str);
183 #define NUM_HINT_TYPE 6
184 typedef enum HintType
186 HINT_TYPE_SCAN_METHOD,
187 HINT_TYPE_JOIN_METHOD,
194 typedef enum HintTypeBitmap
196 HINT_BM_SCAN_METHOD = 1,
200 static const char *HintTypeName[] = {
210 typedef enum HintStatus
212 HINT_STATE_NOTUSED = 0, /* specified relation not used in query */
213 HINT_STATE_USED, /* hint is used */
214 HINT_STATE_DUPLICATION, /* specified hint duplication */
215 HINT_STATE_ERROR /* execute error (parse error does not include
219 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
220 (hint)->base.state == HINT_STATE_USED)
222 static unsigned int qno = 0;
223 static unsigned int msgqno = 0;
224 static char qnostr[32];
225 static const char *current_hint_str = NULL;
228 * However we usually take a hint stirng in post_parse_analyze_hook, we still
229 * need to do so in planner_hook when client starts query execution from the
230 * bind message on a prepared query. This variable prevent duplicate and
231 * sometimes harmful hint string retrieval.
233 static bool current_hint_retrieved = false;
235 /* common data for all hints. */
238 const char *hint_str; /* must not do pfree */
239 const char *keyword; /* must not do pfree */
240 HintKeyword hint_keyword;
243 HintDeleteFunction delete_func;
244 HintDescFunction desc_func;
245 HintCmpFunction cmp_func;
246 HintParseFunction parse_func;
249 /* scan method hints */
250 typedef struct ScanMethodHint
256 unsigned char enforce_mask;
259 typedef struct ParentIndexInfo
264 char *expression_str;
271 /* join method hints */
272 typedef struct JoinMethodHint
278 unsigned char enforce_mask;
280 Relids inner_joinrelids;
283 /* join order hints */
284 typedef struct OuterInnerRels
287 List *outer_inner_pair;
290 typedef struct LeadingHint
293 List *relations; /* relation names specified in Leading hint */
294 OuterInnerRels *outer_inner;
297 /* change a run-time parameter hints */
298 typedef struct SetHint
301 char *name; /* name of variable */
307 typedef enum RowsValueType {
308 RVT_ABSOLUTE, /* Rows(... #1000) */
309 RVT_ADD, /* Rows(... +1000) */
310 RVT_SUB, /* Rows(... -1000) */
311 RVT_MULTI, /* Rows(... *1.2) */
313 typedef struct RowsHint
320 Relids inner_joinrelids;
322 RowsValueType value_type;
327 typedef struct ParallelHint
331 char *nworkers_str; /* original string of nworkers */
332 int nworkers; /* num of workers specified by Worker */
333 bool force_parallel; /* force parallel scan */
337 * Describes a context of hint processing.
341 char *hint_str; /* original hint string */
344 int nall_hints; /* # of valid all hints */
345 int max_all_hints; /* # of slots for all hints */
346 Hint **all_hints; /* parsed all hints */
348 /* # of each hints */
349 int num_hints[NUM_HINT_TYPE];
351 /* for scan method hints */
352 ScanMethodHint **scan_hints; /* parsed scan hints */
354 /* Initial values of parameters */
355 int init_scan_mask; /* enable_* mask */
356 int init_nworkers; /* max_parallel_workers_per_gather */
357 /* min_parallel_table_scan_size*/
358 int init_min_para_tablescan_size;
359 /* min_parallel_index_scan_size*/
360 int init_min_para_indexscan_size;
361 double init_paratup_cost; /* parallel_tuple_cost */
362 double init_parasetup_cost;/* parallel_setup_cost */
364 PlannerInfo *current_root; /* PlannerInfo for the followings */
365 Index parent_relid; /* inherit parent of table relid */
366 ScanMethodHint *parent_scan_hint; /* scan hint for the parent */
367 ParallelHint *parent_parallel_hint; /* parallel hint for the parent */
368 List *parent_index_infos; /* list of parent table's index */
370 JoinMethodHint **join_hints; /* parsed join hints */
371 int init_join_mask; /* initial value join parameter */
372 List **join_hint_level;
373 LeadingHint **leading_hint; /* parsed Leading hints */
374 SetHint **set_hints; /* parsed Set hints */
375 GucContext context; /* which GUC parameters can we set? */
376 RowsHint **rows_hints; /* parsed Rows hints */
377 ParallelHint **parallel_hints; /* parsed Parallel hints */
381 * Describes a hint parser module which is bound with particular hint keyword.
383 typedef struct HintParser
386 HintCreateFunction create_func;
387 HintKeyword hint_keyword;
390 /* Module callbacks */
394 static void push_hint(HintState *hstate);
395 static void pop_hint(void);
397 static void pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query);
398 static void pg_hint_plan_ProcessUtility(PlannedStmt *pstmt,
399 const char *queryString,
400 ProcessUtilityContext context,
401 ParamListInfo params, QueryEnvironment *queryEnv,
402 DestReceiver *dest, char *completionTag);
403 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
404 ParamListInfo boundParams);
405 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
409 /* Scan method hint callbacks */
410 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword,
411 HintKeyword hint_keyword);
412 static void ScanMethodHintDelete(ScanMethodHint *hint);
413 static void ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf);
414 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
415 static const char *ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate,
416 Query *parse, const char *str);
418 /* Join method hint callbacks */
419 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword,
420 HintKeyword hint_keyword);
421 static void JoinMethodHintDelete(JoinMethodHint *hint);
422 static void JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf);
423 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
424 static const char *JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate,
425 Query *parse, const char *str);
427 /* Leading hint callbacks */
428 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword,
429 HintKeyword hint_keyword);
430 static void LeadingHintDelete(LeadingHint *hint);
431 static void LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf);
432 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
433 static const char *LeadingHintParse(LeadingHint *hint, HintState *hstate,
434 Query *parse, const char *str);
436 /* Set hint callbacks */
437 static Hint *SetHintCreate(const char *hint_str, const char *keyword,
438 HintKeyword hint_keyword);
439 static void SetHintDelete(SetHint *hint);
440 static void SetHintDesc(SetHint *hint, StringInfo buf, bool nolf);
441 static int SetHintCmp(const SetHint *a, const SetHint *b);
442 static const char *SetHintParse(SetHint *hint, HintState *hstate, Query *parse,
445 /* Rows hint callbacks */
446 static Hint *RowsHintCreate(const char *hint_str, const char *keyword,
447 HintKeyword hint_keyword);
448 static void RowsHintDelete(RowsHint *hint);
449 static void RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf);
450 static int RowsHintCmp(const RowsHint *a, const RowsHint *b);
451 static const char *RowsHintParse(RowsHint *hint, HintState *hstate,
452 Query *parse, const char *str);
454 /* Parallel hint callbacks */
455 static Hint *ParallelHintCreate(const char *hint_str, const char *keyword,
456 HintKeyword hint_keyword);
457 static void ParallelHintDelete(ParallelHint *hint);
458 static void ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf);
459 static int ParallelHintCmp(const ParallelHint *a, const ParallelHint *b);
460 static const char *ParallelHintParse(ParallelHint *hint, HintState *hstate,
461 Query *parse, const char *str);
463 static void quote_value(StringInfo buf, const char *value);
465 static const char *parse_quoted_value(const char *str, char **word,
468 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
471 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
472 void pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
473 Index rti, RangeTblEntry *rte);
474 static void create_plain_partial_paths(PlannerInfo *root,
476 static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
477 List *live_childrels);
478 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
479 ListCell *other_rels);
480 static void make_rels_by_clauseless_joins(PlannerInfo *root,
482 ListCell *other_rels);
483 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
484 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
486 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
487 Index rti, RangeTblEntry *rte);
488 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
489 List *live_childrels,
490 List *all_child_pathkeys,
491 List *partitioned_rels);
492 static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
494 Relids required_outer);
495 static List *accumulate_append_subpath(List *subpaths, Path *path);
496 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
499 static void pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
501 static void pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate,
503 static void plpgsql_query_erase_callback(ResourceReleasePhase phase,
507 static int set_config_option_noerror(const char *name, const char *value,
508 GucContext context, GucSource source,
509 GucAction action, bool changeVal, int elevel);
510 static void setup_scan_method_enforcement(ScanMethodHint *scanhint,
512 static int set_config_int32_option(const char *name, int32 value,
514 static int set_config_double_option(const char *name, double value,
518 static bool pg_hint_plan_enable_hint = true;
519 static int debug_level = 0;
520 static int pg_hint_plan_parse_message_level = INFO;
521 static int pg_hint_plan_debug_message_level = LOG;
522 /* Default is off, to keep backward compatibility. */
523 static bool pg_hint_plan_enable_hint_table = false;
525 static int plpgsql_recurse_level = 0; /* PLpgSQL recursion level */
526 static int recurse_level = 0; /* recursion level incl. direct SPI calls */
527 static int hint_inhibit_level = 0; /* Inhibit hinting if this is above 0 */
528 /* (This could not be above 1) */
529 static int max_hint_nworkers = -1; /* Maximum nworkers of Workers hints */
531 static const struct config_enum_entry parse_messages_level_options[] = {
532 {"debug", DEBUG2, true},
533 {"debug5", DEBUG5, false},
534 {"debug4", DEBUG4, false},
535 {"debug3", DEBUG3, false},
536 {"debug2", DEBUG2, false},
537 {"debug1", DEBUG1, false},
539 {"info", INFO, false},
540 {"notice", NOTICE, false},
541 {"warning", WARNING, false},
542 {"error", ERROR, false},
544 * {"fatal", FATAL, true},
545 * {"panic", PANIC, true},
550 static const struct config_enum_entry parse_debug_level_options[] = {
553 {"detailed", 2, false},
554 {"verbose", 3, false},
566 /* Saved hook values in case of unload */
567 static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
568 static planner_hook_type prev_planner = NULL;
569 static join_search_hook_type prev_join_search = NULL;
570 static set_rel_pathlist_hook_type prev_set_rel_pathlist = NULL;
571 static ProcessUtility_hook_type prev_ProcessUtility_hook = NULL;
573 /* Hold reference to currently active hint */
574 static HintState *current_hint_state = NULL;
577 * List of hint contexts. We treat the head of the list as the Top of the
578 * context stack, so current_hint_state always points the first element of this
581 static List *HintStateStack = NIL;
583 static const HintParser parsers[] = {
584 {HINT_SEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_SEQSCAN},
585 {HINT_INDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCAN},
586 {HINT_INDEXSCANREGEXP, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCANREGEXP},
587 {HINT_BITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_BITMAPSCAN},
588 {HINT_BITMAPSCANREGEXP, ScanMethodHintCreate,
589 HINT_KEYWORD_BITMAPSCANREGEXP},
590 {HINT_TIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_TIDSCAN},
591 {HINT_NOSEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOSEQSCAN},
592 {HINT_NOINDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXSCAN},
593 {HINT_NOBITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOBITMAPSCAN},
594 {HINT_NOTIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOTIDSCAN},
595 {HINT_INDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXONLYSCAN},
596 {HINT_INDEXONLYSCANREGEXP, ScanMethodHintCreate,
597 HINT_KEYWORD_INDEXONLYSCANREGEXP},
598 {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXONLYSCAN},
600 {HINT_NESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NESTLOOP},
601 {HINT_MERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_MERGEJOIN},
602 {HINT_HASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_HASHJOIN},
603 {HINT_NONESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NONESTLOOP},
604 {HINT_NOMERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOMERGEJOIN},
605 {HINT_NOHASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOHASHJOIN},
607 {HINT_LEADING, LeadingHintCreate, HINT_KEYWORD_LEADING},
608 {HINT_SET, SetHintCreate, HINT_KEYWORD_SET},
609 {HINT_ROWS, RowsHintCreate, HINT_KEYWORD_ROWS},
610 {HINT_PARALLEL, ParallelHintCreate, HINT_KEYWORD_PARALLEL},
612 {NULL, NULL, HINT_KEYWORD_UNRECOGNIZED}
615 PLpgSQL_plugin plugin_funcs = {
619 pg_hint_plan_plpgsql_stmt_beg,
620 pg_hint_plan_plpgsql_stmt_end,
626 * Module load callbacks
631 PLpgSQL_plugin **var_ptr;
633 /* Define custom GUC variables. */
634 DefineCustomBoolVariable("pg_hint_plan.enable_hint",
635 "Force planner to use plans specified in the hint comment preceding to the query.",
637 &pg_hint_plan_enable_hint,
645 DefineCustomEnumVariable("pg_hint_plan.debug_print",
646 "Logs results of hint parsing.",
650 parse_debug_level_options,
657 DefineCustomEnumVariable("pg_hint_plan.parse_messages",
658 "Message level of parse errors.",
660 &pg_hint_plan_parse_message_level,
662 parse_messages_level_options,
669 DefineCustomEnumVariable("pg_hint_plan.message_level",
670 "Message level of debug messages.",
672 &pg_hint_plan_debug_message_level,
674 parse_messages_level_options,
681 DefineCustomBoolVariable("pg_hint_plan.enable_hint_table",
682 "Let pg_hint_plan look up the hint table.",
684 &pg_hint_plan_enable_hint_table,
693 prev_post_parse_analyze_hook = post_parse_analyze_hook;
694 post_parse_analyze_hook = pg_hint_plan_post_parse_analyze;
695 prev_planner = planner_hook;
696 planner_hook = pg_hint_plan_planner;
697 prev_join_search = join_search_hook;
698 join_search_hook = pg_hint_plan_join_search;
699 prev_set_rel_pathlist = set_rel_pathlist_hook;
700 set_rel_pathlist_hook = pg_hint_plan_set_rel_pathlist;
701 prev_ProcessUtility_hook = ProcessUtility_hook;
702 ProcessUtility_hook = pg_hint_plan_ProcessUtility;
704 /* setup PL/pgSQL plugin hook */
705 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
706 *var_ptr = &plugin_funcs;
708 RegisterResourceReleaseCallback(plpgsql_query_erase_callback, NULL);
712 * Module unload callback
718 PLpgSQL_plugin **var_ptr;
720 /* Uninstall hooks. */
721 post_parse_analyze_hook = prev_post_parse_analyze_hook;
722 planner_hook = prev_planner;
723 join_search_hook = prev_join_search;
724 set_rel_pathlist_hook = prev_set_rel_pathlist;
725 ProcessUtility_hook = prev_ProcessUtility_hook;
727 /* uninstall PL/pgSQL plugin hook */
728 var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
733 * create and delete functions the hint object
737 ScanMethodHintCreate(const char *hint_str, const char *keyword,
738 HintKeyword hint_keyword)
740 ScanMethodHint *hint;
742 hint = palloc(sizeof(ScanMethodHint));
743 hint->base.hint_str = hint_str;
744 hint->base.keyword = keyword;
745 hint->base.hint_keyword = hint_keyword;
746 hint->base.type = HINT_TYPE_SCAN_METHOD;
747 hint->base.state = HINT_STATE_NOTUSED;
748 hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
749 hint->base.desc_func = (HintDescFunction) ScanMethodHintDesc;
750 hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
751 hint->base.parse_func = (HintParseFunction) ScanMethodHintParse;
752 hint->relname = NULL;
753 hint->indexnames = NIL;
754 hint->regexp = false;
755 hint->enforce_mask = 0;
757 return (Hint *) hint;
761 ScanMethodHintDelete(ScanMethodHint *hint)
767 pfree(hint->relname);
768 list_free_deep(hint->indexnames);
773 JoinMethodHintCreate(const char *hint_str, const char *keyword,
774 HintKeyword hint_keyword)
776 JoinMethodHint *hint;
778 hint = palloc(sizeof(JoinMethodHint));
779 hint->base.hint_str = hint_str;
780 hint->base.keyword = keyword;
781 hint->base.hint_keyword = hint_keyword;
782 hint->base.type = HINT_TYPE_JOIN_METHOD;
783 hint->base.state = HINT_STATE_NOTUSED;
784 hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
785 hint->base.desc_func = (HintDescFunction) JoinMethodHintDesc;
786 hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
787 hint->base.parse_func = (HintParseFunction) JoinMethodHintParse;
789 hint->inner_nrels = 0;
790 hint->relnames = NULL;
791 hint->enforce_mask = 0;
792 hint->joinrelids = NULL;
793 hint->inner_joinrelids = NULL;
795 return (Hint *) hint;
799 JoinMethodHintDelete(JoinMethodHint *hint)
808 for (i = 0; i < hint->nrels; i++)
809 pfree(hint->relnames[i]);
810 pfree(hint->relnames);
813 bms_free(hint->joinrelids);
814 bms_free(hint->inner_joinrelids);
819 LeadingHintCreate(const char *hint_str, const char *keyword,
820 HintKeyword hint_keyword)
824 hint = palloc(sizeof(LeadingHint));
825 hint->base.hint_str = hint_str;
826 hint->base.keyword = keyword;
827 hint->base.hint_keyword = hint_keyword;
828 hint->base.type = HINT_TYPE_LEADING;
829 hint->base.state = HINT_STATE_NOTUSED;
830 hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
831 hint->base.desc_func = (HintDescFunction) LeadingHintDesc;
832 hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
833 hint->base.parse_func = (HintParseFunction) LeadingHintParse;
834 hint->relations = NIL;
835 hint->outer_inner = NULL;
837 return (Hint *) hint;
841 LeadingHintDelete(LeadingHint *hint)
846 list_free_deep(hint->relations);
847 if (hint->outer_inner)
848 pfree(hint->outer_inner);
853 SetHintCreate(const char *hint_str, const char *keyword,
854 HintKeyword hint_keyword)
858 hint = palloc(sizeof(SetHint));
859 hint->base.hint_str = hint_str;
860 hint->base.keyword = keyword;
861 hint->base.hint_keyword = hint_keyword;
862 hint->base.type = HINT_TYPE_SET;
863 hint->base.state = HINT_STATE_NOTUSED;
864 hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
865 hint->base.desc_func = (HintDescFunction) SetHintDesc;
866 hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
867 hint->base.parse_func = (HintParseFunction) SetHintParse;
872 return (Hint *) hint;
876 SetHintDelete(SetHint *hint)
886 list_free(hint->words);
891 RowsHintCreate(const char *hint_str, const char *keyword,
892 HintKeyword hint_keyword)
896 hint = palloc(sizeof(RowsHint));
897 hint->base.hint_str = hint_str;
898 hint->base.keyword = keyword;
899 hint->base.hint_keyword = hint_keyword;
900 hint->base.type = HINT_TYPE_ROWS;
901 hint->base.state = HINT_STATE_NOTUSED;
902 hint->base.delete_func = (HintDeleteFunction) RowsHintDelete;
903 hint->base.desc_func = (HintDescFunction) RowsHintDesc;
904 hint->base.cmp_func = (HintCmpFunction) RowsHintCmp;
905 hint->base.parse_func = (HintParseFunction) RowsHintParse;
907 hint->inner_nrels = 0;
908 hint->relnames = NULL;
909 hint->joinrelids = NULL;
910 hint->inner_joinrelids = NULL;
911 hint->rows_str = NULL;
912 hint->value_type = RVT_ABSOLUTE;
915 return (Hint *) hint;
919 RowsHintDelete(RowsHint *hint)
928 for (i = 0; i < hint->nrels; i++)
929 pfree(hint->relnames[i]);
930 pfree(hint->relnames);
933 bms_free(hint->joinrelids);
934 bms_free(hint->inner_joinrelids);
939 ParallelHintCreate(const char *hint_str, const char *keyword,
940 HintKeyword hint_keyword)
944 hint = palloc(sizeof(ParallelHint));
945 hint->base.hint_str = hint_str;
946 hint->base.keyword = keyword;
947 hint->base.hint_keyword = hint_keyword;
948 hint->base.type = HINT_TYPE_PARALLEL;
949 hint->base.state = HINT_STATE_NOTUSED;
950 hint->base.delete_func = (HintDeleteFunction) ParallelHintDelete;
951 hint->base.desc_func = (HintDescFunction) ParallelHintDesc;
952 hint->base.cmp_func = (HintCmpFunction) ParallelHintCmp;
953 hint->base.parse_func = (HintParseFunction) ParallelHintParse;
954 hint->relname = NULL;
956 hint->nworkers_str = "0";
958 return (Hint *) hint;
962 ParallelHintDelete(ParallelHint *hint)
968 pfree(hint->relname);
974 HintStateCreate(void)
978 hstate = palloc(sizeof(HintState));
979 hstate->hint_str = NULL;
980 hstate->nall_hints = 0;
981 hstate->max_all_hints = 0;
982 hstate->all_hints = NULL;
983 memset(hstate->num_hints, 0, sizeof(hstate->num_hints));
984 hstate->scan_hints = NULL;
985 hstate->init_scan_mask = 0;
986 hstate->init_nworkers = 0;
987 hstate->init_min_para_tablescan_size = 0;
988 hstate->init_min_para_indexscan_size = 0;
989 hstate->init_paratup_cost = 0;
990 hstate->init_parasetup_cost = 0;
991 hstate->current_root = NULL;
992 hstate->parent_relid = 0;
993 hstate->parent_scan_hint = NULL;
994 hstate->parent_parallel_hint = NULL;
995 hstate->parent_index_infos = NIL;
996 hstate->join_hints = NULL;
997 hstate->init_join_mask = 0;
998 hstate->join_hint_level = NULL;
999 hstate->leading_hint = NULL;
1000 hstate->context = superuser() ? PGC_SUSET : PGC_USERSET;
1001 hstate->set_hints = NULL;
1002 hstate->rows_hints = NULL;
1003 hstate->parallel_hints = NULL;
1009 HintStateDelete(HintState *hstate)
1016 if (hstate->hint_str)
1017 pfree(hstate->hint_str);
1019 for (i = 0; i < hstate->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
1020 hstate->all_hints[i]->delete_func(hstate->all_hints[i]);
1021 if (hstate->all_hints)
1022 pfree(hstate->all_hints);
1023 if (hstate->parent_index_infos)
1024 list_free(hstate->parent_index_infos);
1028 * Copy given value into buf, with quoting with '"' if necessary.
1031 quote_value(StringInfo buf, const char *value)
1033 bool need_quote = false;
1036 for (str = value; *str != '\0'; str++)
1038 if (isspace(*str) || *str == '(' || *str == ')' || *str == '"')
1041 appendStringInfoCharMacro(buf, '"');
1046 for (str = value; *str != '\0'; str++)
1049 appendStringInfoCharMacro(buf, '"');
1051 appendStringInfoCharMacro(buf, *str);
1055 appendStringInfoCharMacro(buf, '"');
1059 ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf)
1063 appendStringInfo(buf, "%s(", hint->base.keyword);
1064 if (hint->relname != NULL)
1066 quote_value(buf, hint->relname);
1067 foreach(l, hint->indexnames)
1069 appendStringInfoCharMacro(buf, ' ');
1070 quote_value(buf, (char *) lfirst(l));
1073 appendStringInfoString(buf, ")");
1075 appendStringInfoChar(buf, '\n');
1079 JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf)
1083 appendStringInfo(buf, "%s(", hint->base.keyword);
1084 if (hint->relnames != NULL)
1086 quote_value(buf, hint->relnames[0]);
1087 for (i = 1; i < hint->nrels; i++)
1089 appendStringInfoCharMacro(buf, ' ');
1090 quote_value(buf, hint->relnames[i]);
1093 appendStringInfoString(buf, ")");
1095 appendStringInfoChar(buf, '\n');
1099 OuterInnerDesc(OuterInnerRels *outer_inner, StringInfo buf)
1101 if (outer_inner->relation == NULL)
1108 appendStringInfoCharMacro(buf, '(');
1109 foreach(l, outer_inner->outer_inner_pair)
1114 appendStringInfoCharMacro(buf, ' ');
1116 OuterInnerDesc(lfirst(l), buf);
1119 appendStringInfoCharMacro(buf, ')');
1122 quote_value(buf, outer_inner->relation);
1126 LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf)
1128 appendStringInfo(buf, "%s(", HINT_LEADING);
1129 if (hint->outer_inner == NULL)
1136 foreach(l, hint->relations)
1141 appendStringInfoCharMacro(buf, ' ');
1143 quote_value(buf, (char *) lfirst(l));
1147 OuterInnerDesc(hint->outer_inner, buf);
1149 appendStringInfoString(buf, ")");
1151 appendStringInfoChar(buf, '\n');
1155 SetHintDesc(SetHint *hint, StringInfo buf, bool nolf)
1157 bool is_first = true;
1160 appendStringInfo(buf, "%s(", HINT_SET);
1161 foreach(l, hint->words)
1166 appendStringInfoCharMacro(buf, ' ');
1168 quote_value(buf, (char *) lfirst(l));
1170 appendStringInfo(buf, ")");
1172 appendStringInfoChar(buf, '\n');
1176 RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf)
1180 appendStringInfo(buf, "%s(", hint->base.keyword);
1181 if (hint->relnames != NULL)
1183 quote_value(buf, hint->relnames[0]);
1184 for (i = 1; i < hint->nrels; i++)
1186 appendStringInfoCharMacro(buf, ' ');
1187 quote_value(buf, hint->relnames[i]);
1190 if (hint->rows_str != NULL)
1191 appendStringInfo(buf, " %s", hint->rows_str);
1192 appendStringInfoString(buf, ")");
1194 appendStringInfoChar(buf, '\n');
1198 ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf)
1200 appendStringInfo(buf, "%s(", hint->base.keyword);
1201 if (hint->relname != NULL)
1203 quote_value(buf, hint->relname);
1205 /* number of workers */
1206 appendStringInfoCharMacro(buf, ' ');
1207 quote_value(buf, hint->nworkers_str);
1208 /* application mode of num of workers */
1209 appendStringInfoCharMacro(buf, ' ');
1210 appendStringInfoString(buf,
1211 (hint->force_parallel ? "hard" : "soft"));
1213 appendStringInfoString(buf, ")");
1215 appendStringInfoChar(buf, '\n');
1219 * Append string which represents all hints in a given state to buf, with
1220 * preceding title with them.
1223 desc_hint_in_state(HintState *hstate, StringInfo buf, const char *title,
1224 HintStatus state, bool nolf)
1228 appendStringInfo(buf, "%s:", title);
1230 appendStringInfoChar(buf, '\n');
1233 for (i = 0; i < hstate->nall_hints; i++)
1235 if (hstate->all_hints[i]->state != state)
1238 hstate->all_hints[i]->desc_func(hstate->all_hints[i], buf, nolf);
1242 if (nolf && nshown == 0)
1243 appendStringInfoString(buf, "(none)");
1247 * Dump contents of given hstate to server log with log level LOG.
1250 HintStateDump(HintState *hstate)
1256 elog(pg_hint_plan_debug_message_level, "pg_hint_plan:\nno hint");
1260 initStringInfo(&buf);
1262 appendStringInfoString(&buf, "pg_hint_plan:\n");
1263 desc_hint_in_state(hstate, &buf, "used hint", HINT_STATE_USED, false);
1264 desc_hint_in_state(hstate, &buf, "not used hint", HINT_STATE_NOTUSED, false);
1265 desc_hint_in_state(hstate, &buf, "duplication hint", HINT_STATE_DUPLICATION, false);
1266 desc_hint_in_state(hstate, &buf, "error hint", HINT_STATE_ERROR, false);
1268 ereport(pg_hint_plan_debug_message_level,
1269 (errmsg ("%s", buf.data)));
1275 HintStateDump2(HintState *hstate)
1281 elog(pg_hint_plan_debug_message_level,
1282 "pg_hint_plan%s: HintStateDump: no hint", qnostr);
1286 initStringInfo(&buf);
1287 appendStringInfo(&buf, "pg_hint_plan%s: HintStateDump: ", qnostr);
1288 desc_hint_in_state(hstate, &buf, "{used hints", HINT_STATE_USED, true);
1289 desc_hint_in_state(hstate, &buf, "}, {not used hints", HINT_STATE_NOTUSED, true);
1290 desc_hint_in_state(hstate, &buf, "}, {duplicate hints", HINT_STATE_DUPLICATION, true);
1291 desc_hint_in_state(hstate, &buf, "}, {error hints", HINT_STATE_ERROR, true);
1292 appendStringInfoChar(&buf, '}');
1294 ereport(pg_hint_plan_debug_message_level,
1295 (errmsg("%s", buf.data),
1297 errhidecontext(true)));
1307 RelnameCmp(const void *a, const void *b)
1309 const char *relnamea = *((const char **) a);
1310 const char *relnameb = *((const char **) b);
1312 return strcmp(relnamea, relnameb);
1316 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
1318 return RelnameCmp(&a->relname, &b->relname);
1322 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
1326 if (a->nrels != b->nrels)
1327 return a->nrels - b->nrels;
1329 for (i = 0; i < a->nrels; i++)
1332 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1340 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
1346 SetHintCmp(const SetHint *a, const SetHint *b)
1348 return strcmp(a->name, b->name);
1352 RowsHintCmp(const RowsHint *a, const RowsHint *b)
1356 if (a->nrels != b->nrels)
1357 return a->nrels - b->nrels;
1359 for (i = 0; i < a->nrels; i++)
1362 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1370 ParallelHintCmp(const ParallelHint *a, const ParallelHint *b)
1372 return RelnameCmp(&a->relname, &b->relname);
1376 HintCmp(const void *a, const void *b)
1378 const Hint *hinta = *((const Hint **) a);
1379 const Hint *hintb = *((const Hint **) b);
1381 if (hinta->type != hintb->type)
1382 return hinta->type - hintb->type;
1383 if (hinta->state == HINT_STATE_ERROR)
1385 if (hintb->state == HINT_STATE_ERROR)
1387 return hinta->cmp_func(hinta, hintb);
1391 * Returns byte offset of hint b from hint a. If hint a was specified before
1392 * b, positive value is returned.
1395 HintCmpWithPos(const void *a, const void *b)
1397 const Hint *hinta = *((const Hint **) a);
1398 const Hint *hintb = *((const Hint **) b);
1401 result = HintCmp(a, b);
1403 result = hinta->hint_str - hintb->hint_str;
1412 parse_keyword(const char *str, StringInfo buf)
1416 while (!isspace(*str) && *str != '(' && *str != '\0')
1417 appendStringInfoCharMacro(buf, *str++);
1423 skip_parenthesis(const char *str, char parenthesis)
1427 if (*str != parenthesis)
1429 if (parenthesis == '(')
1430 hint_ereport(str, ("Opening parenthesis is necessary."));
1431 else if (parenthesis == ')')
1432 hint_ereport(str, ("Closing parenthesis is necessary."));
1443 * Parse a token from str, and store malloc'd copy into word. A token can be
1444 * quoted with '"'. Return value is pointer to unparsed portion of original
1445 * string, or NULL if an error occurred.
1447 * Parsed token is truncated within NAMEDATALEN-1 bytes, when truncate is true.
1450 parse_quoted_value(const char *str, char **word, bool truncate)
1455 /* Skip leading spaces. */
1458 initStringInfo(&buf);
1471 /* Double quotation must be closed. */
1475 hint_ereport(str, ("Unterminated quoted string."));
1480 * Skip escaped double quotation.
1482 * We don't allow slash-asterisk and asterisk-slash (delimiters of
1483 * block comments) to be an object name, so users must specify
1484 * alias for such object names.
1486 * Those special names can be allowed if we care escaped slashes
1487 * and asterisks, but we don't.
1496 else if (isspace(*str) || *str == '(' || *str == ')' || *str == '"' ||
1500 appendStringInfoCharMacro(&buf, *str++);
1505 hint_ereport(str, ("Zero-length delimited string."));
1512 /* Truncate name if it's too long */
1514 truncate_identifier(buf.data, strlen(buf.data), true);
1521 static OuterInnerRels *
1522 OuterInnerRelsCreate(char *name, List *outer_inner_list)
1524 OuterInnerRels *outer_inner;
1526 outer_inner = palloc(sizeof(OuterInnerRels));
1527 outer_inner->relation = name;
1528 outer_inner->outer_inner_pair = outer_inner_list;
1534 parse_parentheses_Leading_in(const char *str, OuterInnerRels **outer_inner)
1536 List *outer_inner_pair = NIL;
1538 if ((str = skip_parenthesis(str, '(')) == NULL)
1543 /* Store words in parentheses into outer_inner_list. */
1544 while(*str != ')' && *str != '\0')
1546 OuterInnerRels *outer_inner_rels;
1550 str = parse_parentheses_Leading_in(str, &outer_inner_rels);
1558 if ((str = parse_quoted_value(str, &name, true)) == NULL)
1561 outer_inner_rels = OuterInnerRelsCreate(name, NIL);
1564 outer_inner_pair = lappend(outer_inner_pair, outer_inner_rels);
1569 (str = skip_parenthesis(str, ')')) == NULL)
1571 list_free(outer_inner_pair);
1575 *outer_inner = OuterInnerRelsCreate(NULL, outer_inner_pair);
1581 parse_parentheses_Leading(const char *str, List **name_list,
1582 OuterInnerRels **outer_inner)
1585 bool truncate = true;
1587 if ((str = skip_parenthesis(str, '(')) == NULL)
1593 if ((str = parse_parentheses_Leading_in(str, outer_inner)) == NULL)
1598 /* Store words in parentheses into name_list. */
1599 while(*str != ')' && *str != '\0')
1601 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1603 list_free(*name_list);
1607 *name_list = lappend(*name_list, name);
1612 if ((str = skip_parenthesis(str, ')')) == NULL)
1618 parse_parentheses(const char *str, List **name_list, HintKeyword keyword)
1621 bool truncate = true;
1623 if ((str = skip_parenthesis(str, '(')) == NULL)
1628 /* Store words in parentheses into name_list. */
1629 while(*str != ')' && *str != '\0')
1631 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1633 list_free(*name_list);
1637 *name_list = lappend(*name_list, name);
1640 if (keyword == HINT_KEYWORD_INDEXSCANREGEXP ||
1641 keyword == HINT_KEYWORD_INDEXONLYSCANREGEXP ||
1642 keyword == HINT_KEYWORD_BITMAPSCANREGEXP ||
1643 keyword == HINT_KEYWORD_SET)
1649 if ((str = skip_parenthesis(str, ')')) == NULL)
1655 parse_hints(HintState *hstate, Query *parse, const char *str)
1660 initStringInfo(&buf);
1661 while (*str != '\0')
1663 const HintParser *parser;
1665 /* in error message, we output the comment including the keyword. */
1666 head = (char *) str;
1668 /* parse only the keyword of the hint. */
1669 resetStringInfo(&buf);
1670 str = parse_keyword(str, &buf);
1672 for (parser = parsers; parser->keyword != NULL; parser++)
1674 char *keyword = parser->keyword;
1677 if (pg_strcasecmp(buf.data, keyword) != 0)
1680 hint = parser->create_func(head, keyword, parser->hint_keyword);
1682 /* parser of each hint does parse in a parenthesis. */
1683 if ((str = hint->parse_func(hint, hstate, parse, str)) == NULL)
1685 hint->delete_func(hint);
1691 * Add hint information into all_hints array. If we don't have
1692 * enough space, double the array.
1694 if (hstate->nall_hints == 0)
1696 hstate->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
1697 hstate->all_hints = (Hint **)
1698 palloc(sizeof(Hint *) * hstate->max_all_hints);
1700 else if (hstate->nall_hints == hstate->max_all_hints)
1702 hstate->max_all_hints *= 2;
1703 hstate->all_hints = (Hint **)
1704 repalloc(hstate->all_hints,
1705 sizeof(Hint *) * hstate->max_all_hints);
1708 hstate->all_hints[hstate->nall_hints] = hint;
1709 hstate->nall_hints++;
1716 if (parser->keyword == NULL)
1719 ("Unrecognized hint keyword \"%s\".", buf.data));
1730 * Get hints from table by client-supplied query string and application name.
1733 get_hints_from_table(const char *client_query, const char *client_application)
1735 const char *search_query =
1737 " FROM hint_plan.hints "
1738 " WHERE norm_query_string = $1 "
1739 " AND ( application_name = $2 "
1740 " OR application_name = '' ) "
1741 " ORDER BY application_name DESC";
1742 static SPIPlanPtr plan = NULL;
1744 Oid argtypes[2] = { TEXTOID, TEXTOID };
1746 bool nulls[2] = { false, false };
1752 bool snapshot_set = false;
1754 hint_inhibit_level++;
1756 if (!ActiveSnapshotSet())
1758 PushActiveSnapshot(GetTransactionSnapshot());
1759 snapshot_set = true;
1767 p = SPI_prepare(search_query, 2, argtypes);
1768 plan = SPI_saveplan(p);
1772 qry = cstring_to_text(client_query);
1773 app = cstring_to_text(client_application);
1774 values[0] = PointerGetDatum(qry);
1775 values[1] = PointerGetDatum(app);
1777 SPI_execute_plan(plan, values, nulls, true, 1);
1779 if (SPI_processed > 0)
1783 hints = SPI_getvalue(SPI_tuptable->vals[0],
1784 SPI_tuptable->tupdesc, 1);
1786 * Here we use SPI_palloc to ensure that hints string is valid even
1787 * after SPI_finish call. We can't use simple palloc because it
1788 * allocates memory in SPI's context and that context is deleted in
1791 buf = SPI_palloc(strlen(hints) + 1);
1799 PopActiveSnapshot();
1801 hint_inhibit_level--;
1805 hint_inhibit_level--;
1814 * Get client-supplied query string. Addtion to that the jumbled query is
1815 * supplied if the caller requested. From the restriction of JumbleQuery, some
1816 * kind of query needs special amendments. Reutrns NULL if this query doesn't
1817 * change the current hint. This function returns NULL also when something
1818 * wrong has happend and let the caller continue using the current hints.
1821 get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
1823 const char *p = debug_query_string;
1826 * If debug_query_string is set, it is the top level statement. But in some
1827 * cases we reach here with debug_query_string set NULL for example in the
1828 * case of DESCRIBE message handling or EXECUTE command. We may still see a
1829 * candidate top-level query in pstate in the case.
1831 if (pstate && pstate->p_sourcetext)
1832 p = pstate->p_sourcetext;
1834 /* We don't see a query string, return NULL */
1838 if (jumblequery != NULL)
1839 *jumblequery = query;
1841 if (query->commandType == CMD_UTILITY)
1843 Query *target_query = (Query *)query->utilityStmt;
1846 * Some CMD_UTILITY statements have a subquery that we can hint on.
1847 * Since EXPLAIN can be placed before other kind of utility statements
1848 * and EXECUTE can be contained other kind of utility statements, these
1849 * conditions are not mutually exclusive and should be considered in
1852 if (IsA(target_query, ExplainStmt))
1854 ExplainStmt *stmt = (ExplainStmt *)target_query;
1856 Assert(IsA(stmt->query, Query));
1857 target_query = (Query *)stmt->query;
1859 /* strip out the top-level query for further processing */
1860 if (target_query->commandType == CMD_UTILITY &&
1861 target_query->utilityStmt != NULL)
1862 target_query = (Query *)target_query->utilityStmt;
1865 if (IsA(target_query, DeclareCursorStmt))
1867 DeclareCursorStmt *stmt = (DeclareCursorStmt *)target_query;
1868 Query *query = (Query *)stmt->query;
1870 /* the target must be CMD_SELECT in this case */
1871 Assert(IsA(query, Query) && query->commandType == CMD_SELECT);
1872 target_query = query;
1875 if (IsA(target_query, CreateTableAsStmt))
1877 CreateTableAsStmt *stmt = (CreateTableAsStmt *) target_query;
1879 Assert(IsA(stmt->query, Query));
1880 target_query = (Query *) stmt->query;
1882 /* strip out the top-level query for further processing */
1883 if (target_query->commandType == CMD_UTILITY &&
1884 target_query->utilityStmt != NULL)
1885 target_query = (Query *)target_query->utilityStmt;
1888 if (IsA(target_query, ExecuteStmt))
1891 * Use the prepared query for EXECUTE. The Query for jumble
1892 * also replaced with the corresponding one.
1894 ExecuteStmt *stmt = (ExecuteStmt *)target_query;
1895 PreparedStatement *entry;
1897 entry = FetchPreparedStatement(stmt->name, true);
1899 if (entry->plansource->is_valid)
1901 p = entry->plansource->query_string;
1902 target_query = (Query *) linitial (entry->plansource->query_list);
1906 /* igonre the hint for EXECUTE if invalidated */
1908 target_query = NULL;
1912 /* JumbleQuery accespts only a non-utility Query */
1914 (!IsA(target_query, Query) ||
1915 target_query->utilityStmt != NULL))
1916 target_query = NULL;
1919 *jumblequery = target_query;
1922 * Return NULL if pstate is not of top-level query. We don't need this
1923 * when jumble info is not requested or cannot do this when pstate is NULL.
1925 else if (!jumblequery && pstate && pstate->p_sourcetext != p &&
1926 strcmp(pstate->p_sourcetext, p) != 0)
1933 * Get hints from the head block comment in client-supplied query string.
1936 get_hints_from_comment(const char *p)
1938 const char *hint_head;
1946 /* extract query head comment. */
1947 hint_head = strstr(p, HINT_START);
1948 if (hint_head == NULL)
1950 for (;p < hint_head; p++)
1953 * Allow these characters precedes hint comment:
1955 * - alphabets which are in ASCII range
1956 * - space, tabs and new-lines
1957 * - underscores, for identifier
1958 * - commas, for SELECT clause, EXPLAIN and PREPARE
1959 * - parentheses, for EXPLAIN and PREPARE
1961 * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1962 * avoid behavior which depends on locale setting.
1964 if (!(*p >= '0' && *p <= '9') &&
1965 !(*p >= 'A' && *p <= 'Z') &&
1966 !(*p >= 'a' && *p <= 'z') &&
1970 *p != '(' && *p != ')')
1974 len = strlen(HINT_START);
1979 /* find hint end keyword. */
1980 if ((tail = strstr(p, HINT_END)) == NULL)
1982 hint_ereport(head, ("Unterminated block comment."));
1986 /* We don't support nested block comments. */
1987 if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1989 hint_ereport(head, ("Nested block comments are not supported."));
1993 /* Make a copy of hint. */
1995 head = palloc(len + 1);
1996 memcpy(head, p, len);
2004 * Parse hints that got, create hint struct from parse tree and parse hints.
2007 create_hintstate(Query *parse, const char *hints)
2016 /* -1 means that no Parallel hint is specified. */
2017 max_hint_nworkers = -1;
2020 hstate = HintStateCreate();
2021 hstate->hint_str = (char *) hints;
2023 /* parse each hint. */
2024 parse_hints(hstate, parse, p);
2026 /* When nothing specified a hint, we free HintState and returns NULL. */
2027 if (hstate->nall_hints == 0)
2029 HintStateDelete(hstate);
2033 /* Sort hints in order of original position. */
2034 qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
2037 /* Count number of hints per hint-type. */
2038 for (i = 0; i < hstate->nall_hints; i++)
2040 Hint *cur_hint = hstate->all_hints[i];
2041 hstate->num_hints[cur_hint->type]++;
2045 * If an object (or a set of objects) has multiple hints of same hint-type,
2046 * only the last hint is valid and others are ignored in planning.
2047 * Hints except the last are marked as 'duplicated' to remember the order.
2049 for (i = 0; i < hstate->nall_hints - 1; i++)
2051 Hint *cur_hint = hstate->all_hints[i];
2052 Hint *next_hint = hstate->all_hints[i + 1];
2055 * Leading hint is marked as 'duplicated' in transform_join_hints.
2057 if (cur_hint->type == HINT_TYPE_LEADING &&
2058 next_hint->type == HINT_TYPE_LEADING)
2062 * Note that we need to pass addresses of hint pointers, because
2063 * HintCmp is designed to sort array of Hint* by qsort.
2065 if (HintCmp(&cur_hint, &next_hint) == 0)
2067 hint_ereport(cur_hint->hint_str,
2068 ("Conflict %s hint.", HintTypeName[cur_hint->type]));
2069 cur_hint->state = HINT_STATE_DUPLICATION;
2074 * Make sure that per-type array pointers point proper position in the
2075 * array which consists of all hints.
2077 hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
2078 hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
2079 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
2080 hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
2081 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
2082 hstate->set_hints = (SetHint **) (hstate->leading_hint +
2083 hstate->num_hints[HINT_TYPE_LEADING]);
2084 hstate->rows_hints = (RowsHint **) (hstate->set_hints +
2085 hstate->num_hints[HINT_TYPE_SET]);
2086 hstate->parallel_hints = (ParallelHint **) (hstate->set_hints +
2087 hstate->num_hints[HINT_TYPE_ROWS]);
2093 * Parse inside of parentheses of scan-method hints.
2096 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
2099 const char *keyword = hint->base.keyword;
2100 HintKeyword hint_keyword = hint->base.hint_keyword;
2101 List *name_list = NIL;
2104 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2107 /* Parse relation name and index name(s) if given hint accepts. */
2108 length = list_length(name_list);
2110 /* at least twp parameters required */
2114 ("%s hint requires a relation.", hint->base.keyword));
2115 hint->base.state = HINT_STATE_ERROR;
2119 hint->relname = linitial(name_list);
2120 hint->indexnames = list_delete_first(name_list);
2122 /* check whether the hint accepts index name(s) */
2123 if (length > 1 && !SCAN_HINT_ACCEPTS_INDEX_NAMES(hint_keyword))
2126 ("%s hint accepts only one relation.",
2127 hint->base.keyword));
2128 hint->base.state = HINT_STATE_ERROR;
2132 /* Set a bit for specified hint. */
2133 switch (hint_keyword)
2135 case HINT_KEYWORD_SEQSCAN:
2136 hint->enforce_mask = ENABLE_SEQSCAN;
2138 case HINT_KEYWORD_INDEXSCAN:
2139 hint->enforce_mask = ENABLE_INDEXSCAN;
2141 case HINT_KEYWORD_INDEXSCANREGEXP:
2142 hint->enforce_mask = ENABLE_INDEXSCAN;
2143 hint->regexp = true;
2145 case HINT_KEYWORD_BITMAPSCAN:
2146 hint->enforce_mask = ENABLE_BITMAPSCAN;
2148 case HINT_KEYWORD_BITMAPSCANREGEXP:
2149 hint->enforce_mask = ENABLE_BITMAPSCAN;
2150 hint->regexp = true;
2152 case HINT_KEYWORD_TIDSCAN:
2153 hint->enforce_mask = ENABLE_TIDSCAN;
2155 case HINT_KEYWORD_NOSEQSCAN:
2156 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
2158 case HINT_KEYWORD_NOINDEXSCAN:
2159 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
2161 case HINT_KEYWORD_NOBITMAPSCAN:
2162 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
2164 case HINT_KEYWORD_NOTIDSCAN:
2165 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
2167 case HINT_KEYWORD_INDEXONLYSCAN:
2168 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2170 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
2171 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2172 hint->regexp = true;
2174 case HINT_KEYWORD_NOINDEXONLYSCAN:
2175 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
2178 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2187 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
2190 const char *keyword = hint->base.keyword;
2191 HintKeyword hint_keyword = hint->base.hint_keyword;
2192 List *name_list = NIL;
2194 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2197 hint->nrels = list_length(name_list);
2199 if (hint->nrels > 0)
2205 * Transform relation names from list to array to sort them with qsort
2208 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2209 foreach (l, name_list)
2211 hint->relnames[i] = lfirst(l);
2216 list_free(name_list);
2218 /* A join hint requires at least two relations */
2219 if (hint->nrels < 2)
2222 ("%s hint requires at least two relations.",
2223 hint->base.keyword));
2224 hint->base.state = HINT_STATE_ERROR;
2228 /* Sort hints in alphabetical order of relation names. */
2229 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2231 switch (hint_keyword)
2233 case HINT_KEYWORD_NESTLOOP:
2234 hint->enforce_mask = ENABLE_NESTLOOP;
2236 case HINT_KEYWORD_MERGEJOIN:
2237 hint->enforce_mask = ENABLE_MERGEJOIN;
2239 case HINT_KEYWORD_HASHJOIN:
2240 hint->enforce_mask = ENABLE_HASHJOIN;
2242 case HINT_KEYWORD_NONESTLOOP:
2243 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
2245 case HINT_KEYWORD_NOMERGEJOIN:
2246 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
2248 case HINT_KEYWORD_NOHASHJOIN:
2249 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
2252 hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2261 OuterInnerPairCheck(OuterInnerRels *outer_inner)
2264 if (outer_inner->outer_inner_pair == NIL)
2266 if (outer_inner->relation)
2272 if (list_length(outer_inner->outer_inner_pair) == 2)
2274 foreach(l, outer_inner->outer_inner_pair)
2276 if (!OuterInnerPairCheck(lfirst(l)))
2287 OuterInnerList(OuterInnerRels *outer_inner)
2289 List *outer_inner_list = NIL;
2291 OuterInnerRels *outer_inner_rels;
2293 foreach(l, outer_inner->outer_inner_pair)
2295 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
2297 if (outer_inner_rels->relation != NULL)
2298 outer_inner_list = lappend(outer_inner_list,
2299 outer_inner_rels->relation);
2301 outer_inner_list = list_concat(outer_inner_list,
2302 OuterInnerList(outer_inner_rels));
2304 return outer_inner_list;
2308 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
2311 List *name_list = NIL;
2312 OuterInnerRels *outer_inner = NULL;
2314 if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
2318 if (outer_inner != NULL)
2319 name_list = OuterInnerList(outer_inner);
2321 hint->relations = name_list;
2322 hint->outer_inner = outer_inner;
2324 /* A Leading hint requires at least two relations */
2325 if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
2327 hint_ereport(hint->base.hint_str,
2328 ("%s hint requires at least two relations.",
2330 hint->base.state = HINT_STATE_ERROR;
2332 else if (hint->outer_inner != NULL &&
2333 !OuterInnerPairCheck(hint->outer_inner))
2335 hint_ereport(hint->base.hint_str,
2336 ("%s hint requires two sets of relations when parentheses nests.",
2338 hint->base.state = HINT_STATE_ERROR;
2345 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
2347 List *name_list = NIL;
2349 if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
2353 hint->words = name_list;
2355 /* We need both name and value to set GUC parameter. */
2356 if (list_length(name_list) == 2)
2358 hint->name = linitial(name_list);
2359 hint->value = lsecond(name_list);
2363 hint_ereport(hint->base.hint_str,
2364 ("%s hint requires name and value of GUC parameter.",
2366 hint->base.state = HINT_STATE_ERROR;
2373 RowsHintParse(RowsHint *hint, HintState *hstate, Query *parse,
2376 HintKeyword hint_keyword = hint->base.hint_keyword;
2377 List *name_list = NIL;
2383 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2386 /* Last element must be rows specification */
2387 hint->nrels = list_length(name_list) - 1;
2389 if (hint->nrels < 1)
2392 ("%s hint needs at least one relation followed by one correction term.",
2393 hint->base.keyword));
2394 hint->base.state = HINT_STATE_ERROR;
2401 * Transform relation names from list to array to sort them with qsort
2404 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2405 foreach (l, name_list)
2407 if (hint->nrels <= i)
2409 hint->relnames[i] = lfirst(l);
2413 /* Retieve rows estimation */
2414 rows_str = list_nth(name_list, hint->nrels);
2415 hint->rows_str = rows_str; /* store as-is for error logging */
2416 if (rows_str[0] == '#')
2418 hint->value_type = RVT_ABSOLUTE;
2421 else if (rows_str[0] == '+')
2423 hint->value_type = RVT_ADD;
2426 else if (rows_str[0] == '-')
2428 hint->value_type = RVT_SUB;
2431 else if (rows_str[0] == '*')
2433 hint->value_type = RVT_MULTI;
2438 hint_ereport(rows_str, ("Unrecognized rows value type notation."));
2439 hint->base.state = HINT_STATE_ERROR;
2442 hint->rows = strtod(rows_str, &end_ptr);
2445 hint_ereport(rows_str,
2446 ("%s hint requires valid number as rows estimation.",
2447 hint->base.keyword));
2448 hint->base.state = HINT_STATE_ERROR;
2452 /* A join hint requires at least two relations */
2453 if (hint->nrels < 2)
2456 ("%s hint requires at least two relations.",
2457 hint->base.keyword));
2458 hint->base.state = HINT_STATE_ERROR;
2462 list_free(name_list);
2464 /* Sort relnames in alphabetical order. */
2465 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2471 ParallelHintParse(ParallelHint *hint, HintState *hstate, Query *parse,
2474 HintKeyword hint_keyword = hint->base.hint_keyword;
2475 List *name_list = NIL;
2479 bool force_parallel = false;
2481 if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2484 /* Parse relation name and index name(s) if given hint accepts. */
2485 length = list_length(name_list);
2487 if (length < 2 || length > 3)
2490 ("wrong number of arguments (%d): %s",
2491 length, hint->base.keyword));
2492 hint->base.state = HINT_STATE_ERROR;
2496 hint->relname = linitial(name_list);
2498 /* The second parameter is number of workers */
2499 hint->nworkers_str = list_nth(name_list, 1);
2500 nworkers = strtod(hint->nworkers_str, &end_ptr);
2501 if (*end_ptr || nworkers < 0 || nworkers > max_worker_processes)
2504 hint_ereport(hint->nworkers_str,
2505 ("number of workers must be a number: %s",
2506 hint->base.keyword));
2507 else if (nworkers < 0)
2508 hint_ereport(hint->nworkers_str,
2509 ("number of workers must be positive: %s",
2510 hint->base.keyword));
2511 else if (nworkers > max_worker_processes)
2512 hint_ereport(hint->nworkers_str,
2513 ("number of workers = %d is larger than max_worker_processes(%d): %s",
2514 nworkers, max_worker_processes, hint->base.keyword));
2516 hint->base.state = HINT_STATE_ERROR;
2519 hint->nworkers = nworkers;
2521 /* optional third parameter is specified */
2524 const char *modeparam = (const char *)list_nth(name_list, 2);
2525 if (pg_strcasecmp(modeparam, "hard") == 0)
2526 force_parallel = true;
2527 else if (pg_strcasecmp(modeparam, "soft") != 0)
2529 hint_ereport(modeparam,
2530 ("enforcement must be soft or hard: %s",
2531 hint->base.keyword));
2532 hint->base.state = HINT_STATE_ERROR;
2536 hint->force_parallel = force_parallel;
2538 if (hint->base.state != HINT_STATE_ERROR &&
2539 nworkers > max_hint_nworkers)
2540 max_hint_nworkers = nworkers;
2546 * set GUC parameter functions
2550 get_current_scan_mask()
2555 mask |= ENABLE_SEQSCAN;
2556 if (enable_indexscan)
2557 mask |= ENABLE_INDEXSCAN;
2558 if (enable_bitmapscan)
2559 mask |= ENABLE_BITMAPSCAN;
2561 mask |= ENABLE_TIDSCAN;
2562 if (enable_indexonlyscan)
2563 mask |= ENABLE_INDEXONLYSCAN;
2569 get_current_join_mask()
2573 if (enable_nestloop)
2574 mask |= ENABLE_NESTLOOP;
2575 if (enable_mergejoin)
2576 mask |= ENABLE_MERGEJOIN;
2577 if (enable_hashjoin)
2578 mask |= ENABLE_HASHJOIN;
2584 * Sets GUC prameters without throwing exception. Reutrns false if something
2588 set_config_option_noerror(const char *name, const char *value,
2589 GucContext context, GucSource source,
2590 GucAction action, bool changeVal, int elevel)
2593 MemoryContext ccxt = CurrentMemoryContext;
2597 result = set_config_option(name, value, context, source,
2598 action, changeVal, 0, false);
2604 /* Save error info */
2605 MemoryContextSwitchTo(ccxt);
2606 errdata = CopyErrorData();
2610 (errcode(errdata->sqlerrcode),
2611 errmsg("%s", errdata->message),
2612 errdata->detail ? errdetail("%s", errdata->detail) : 0,
2613 errdata->hint ? errhint("%s", errdata->hint) : 0));
2615 FreeErrorData(errdata);
2623 * Sets GUC parameter of int32 type without throwing exceptions. Returns false
2624 * if something wrong.
2627 set_config_int32_option(const char *name, int32 value, GucContext context)
2629 char buf[16]; /* enough for int32 */
2631 if (snprintf(buf, 16, "%d", value) < 0)
2633 ereport(pg_hint_plan_parse_message_level,
2634 (errmsg ("Failed to convert integer to string: %d", value)));
2639 set_config_option_noerror(name, buf, context,
2640 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2641 pg_hint_plan_parse_message_level);
2645 * Sets GUC parameter of double type without throwing exceptions. Returns false
2646 * if something wrong.
2649 set_config_double_option(const char *name, double value, GucContext context)
2651 char *buf = float8out_internal(value);
2654 result = set_config_option_noerror(name, buf, context,
2655 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2656 pg_hint_plan_parse_message_level);
2661 /* setup scan method enforcement according to given options */
2663 setup_guc_enforcement(SetHint **options, int noptions, GucContext context)
2667 for (i = 0; i < noptions; i++)
2669 SetHint *hint = options[i];
2672 if (!hint_state_enabled(hint))
2675 result = set_config_option_noerror(hint->name, hint->value, context,
2676 PGC_S_SESSION, GUC_ACTION_SAVE, true,
2677 pg_hint_plan_parse_message_level);
2679 hint->base.state = HINT_STATE_USED;
2681 hint->base.state = HINT_STATE_ERROR;
2688 * Setup parallel execution environment.
2690 * If hint is not NULL, set up using it, elsewise reset to initial environment.
2693 setup_parallel_plan_enforcement(ParallelHint *hint, HintState *state)
2697 hint->base.state = HINT_STATE_USED;
2698 set_config_int32_option("max_parallel_workers_per_gather",
2699 hint->nworkers, state->context);
2702 set_config_int32_option("max_parallel_workers_per_gather",
2703 state->init_nworkers, state->context);
2705 /* force means that enforce parallel as far as possible */
2706 if (hint && hint->force_parallel)
2708 set_config_double_option("parallel_tuple_cost", 0.0, state->context);
2709 set_config_double_option("parallel_setup_cost", 0.0, state->context);
2710 set_config_int32_option("min_parallel_table_scan_size", 0,
2712 set_config_int32_option("min_parallel_index_scan_size", 0,
2717 set_config_double_option("parallel_tuple_cost",
2718 state->init_paratup_cost, state->context);
2719 set_config_double_option("parallel_setup_cost",
2720 state->init_parasetup_cost, state->context);
2721 set_config_int32_option("min_parallel_table_scan_size",
2722 state->init_min_para_tablescan_size,
2724 set_config_int32_option("min_parallel_index_scan_size",
2725 state->init_min_para_indexscan_size,
2730 #define SET_CONFIG_OPTION(name, type_bits) \
2731 set_config_option_noerror((name), \
2732 (mask & (type_bits)) ? "true" : "false", \
2733 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
2737 * Setup GUC environment to enforce scan methods. If scanhint is NULL, reset
2738 * GUCs to the saved state in state.
2741 setup_scan_method_enforcement(ScanMethodHint *scanhint, HintState *state)
2743 unsigned char enforce_mask = state->init_scan_mask;
2744 GucContext context = state->context;
2749 enforce_mask = scanhint->enforce_mask;
2750 scanhint->base.state = HINT_STATE_USED;
2753 if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
2754 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
2755 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
2757 mask = enforce_mask;
2759 mask = enforce_mask & current_hint_state->init_scan_mask;
2761 SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
2762 SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
2763 SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
2764 SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
2765 SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
2769 set_join_config_options(unsigned char enforce_mask, GucContext context)
2773 if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
2774 enforce_mask == ENABLE_HASHJOIN)
2775 mask = enforce_mask;
2777 mask = enforce_mask & current_hint_state->init_join_mask;
2779 SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
2780 SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
2781 SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
2785 * Push a hint into hint stack which is implemented with List struct. Head of
2786 * list is top of stack.
2789 push_hint(HintState *hstate)
2791 /* Prepend new hint to the list means pushing to stack. */
2792 HintStateStack = lcons(hstate, HintStateStack);
2794 /* Pushed hint is the one which should be used hereafter. */
2795 current_hint_state = hstate;
2798 /* Pop a hint from hint stack. Popped hint is automatically discarded. */
2802 /* Hint stack must not be empty. */
2803 if(HintStateStack == NIL)
2804 elog(ERROR, "hint stack is empty");
2807 * Take a hint at the head from the list, and free it. Switch
2808 * current_hint_state to point new head (NULL if the list is empty).
2810 HintStateStack = list_delete_first(HintStateStack);
2811 HintStateDelete(current_hint_state);
2812 if(HintStateStack == NIL)
2813 current_hint_state = NULL;
2815 current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
2819 * Retrieve and store hint string from given query or from the hint table.
2822 get_current_hint_string(ParseState *pstate, Query *query)
2824 const char *query_str;
2825 MemoryContext oldcontext;
2827 /* do nothing under hint table search */
2828 if (hint_inhibit_level > 0)
2831 /* We alredy have one, don't parse it again. */
2832 if (current_hint_retrieved)
2835 /* Don't parse the current query hereafter */
2836 current_hint_retrieved = true;
2838 if (!pg_hint_plan_enable_hint)
2840 if (current_hint_str)
2842 pfree((void *)current_hint_str);
2843 current_hint_str = NULL;
2848 /* increment the query number */
2850 if (debug_level > 1)
2851 snprintf(qnostr, sizeof(qnostr), "[qno=0x%x]", qno++);
2854 /* search the hint table for a hint if requested */
2855 if (pg_hint_plan_enable_hint_table)
2858 pgssJumbleState jstate;
2860 char *normalized_query = NULL;
2862 query_str = get_query_string(pstate, query, &jumblequery);
2864 /* If this query is not for hint, just return */
2868 /* clear the previous hint string */
2869 if (current_hint_str)
2871 pfree((void *)current_hint_str);
2872 current_hint_str = NULL;
2878 * XXX: normalization code is copied from pg_stat_statements.c.
2879 * Make sure to keep up-to-date with it.
2881 jstate.jumble = (unsigned char *) palloc(JUMBLE_SIZE);
2882 jstate.jumble_len = 0;
2883 jstate.clocations_buf_size = 32;
2884 jstate.clocations = (pgssLocationLen *)
2885 palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
2886 jstate.clocations_count = 0;
2888 JumbleQuery(&jstate, jumblequery);
2891 * Normalize the query string by replacing constants with '?'
2894 * Search hint string which is stored keyed by query string
2895 * and application name. The query string is normalized to allow
2898 * Adding 1 byte to query_len ensures that the returned string has
2899 * a terminating NULL.
2901 query_len = strlen(query_str) + 1;
2903 generate_normalized_query(&jstate, query_str, 0, &query_len,
2904 GetDatabaseEncoding());
2907 * find a hint for the normalized query. the result should be in
2910 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2912 get_hints_from_table(normalized_query, application_name);
2913 MemoryContextSwitchTo(oldcontext);
2915 if (debug_level > 1)
2917 if (current_hint_str)
2918 ereport(pg_hint_plan_debug_message_level,
2919 (errmsg("pg_hint_plan[qno=0x%x]: "
2920 "post_parse_analyze_hook: "
2921 "hints from table: \"%s\": "
2922 "normalized_query=\"%s\", "
2923 "application name =\"%s\"",
2924 qno, current_hint_str,
2925 normalized_query, application_name),
2926 errhidestmt(msgqno != qno),
2927 errhidecontext(msgqno != qno)));
2929 ereport(pg_hint_plan_debug_message_level,
2930 (errmsg("pg_hint_plan[qno=0x%x]: "
2931 "no match found in table: "
2932 "application name = \"%s\", "
2933 "normalized_query=\"%s\"",
2934 qno, application_name,
2936 errhidestmt(msgqno != qno),
2937 errhidecontext(msgqno != qno)));
2943 /* retrun if we have hint here */
2944 if (current_hint_str)
2948 query_str = get_query_string(pstate, query, NULL);
2953 * get hints from the comment. However we may have the same query
2954 * string with the previous call, but the extra comparison seems no
2957 if (current_hint_str)
2958 pfree((void *)current_hint_str);
2960 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2961 current_hint_str = get_hints_from_comment(query_str);
2962 MemoryContextSwitchTo(oldcontext);
2967 * Failed to get query. We would be in fetching invalidated
2968 * plancache. Try the next chance.
2970 current_hint_retrieved = false;
2973 if (debug_level > 1)
2975 if (debug_level == 1 && query_str && debug_query_string &&
2976 strcmp(query_str, debug_query_string))
2977 ereport(pg_hint_plan_debug_message_level,
2978 (errmsg("hints in comment=\"%s\"",
2979 current_hint_str ? current_hint_str : "(none)"),
2980 errhidestmt(msgqno != qno),
2981 errhidecontext(msgqno != qno)));
2983 ereport(pg_hint_plan_debug_message_level,
2984 (errmsg("hints in comment=\"%s\", query=\"%s\", debug_query_string=\"%s\"",
2985 current_hint_str ? current_hint_str : "(none)",
2986 query_str ? query_str : "(none)",
2987 debug_query_string ? debug_query_string : "(none)"),
2988 errhidestmt(msgqno != qno),
2989 errhidecontext(msgqno != qno)));
2995 * Retrieve hint string from the current query.
2998 pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
3000 if (prev_post_parse_analyze_hook)
3001 prev_post_parse_analyze_hook(pstate, query);
3003 /* always retrieve hint from the top-level query string */
3004 if (plpgsql_recurse_level == 0)
3005 current_hint_retrieved = false;
3007 get_current_hint_string(pstate, query);
3011 * We need to reset current_hint_retrieved flag always when a command execution
3012 * is finished. This is true even for a pure utility command that doesn't
3013 * involve planning phase.
3016 pg_hint_plan_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
3017 ProcessUtilityContext context,
3018 ParamListInfo params, QueryEnvironment *queryEnv,
3019 DestReceiver *dest, char *completionTag)
3021 if (prev_ProcessUtility_hook)
3022 prev_ProcessUtility_hook(pstmt, queryString, context, params, queryEnv,
3023 dest, completionTag);
3025 standard_ProcessUtility(pstmt, queryString, context, params, queryEnv,
3026 dest, completionTag);
3028 if (plpgsql_recurse_level == 0)
3029 current_hint_retrieved = false;
3033 * Read and set up hint information
3035 static PlannedStmt *
3036 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
3039 PlannedStmt *result;
3041 const char *prev_hint_str = NULL;
3044 * Use standard planner if pg_hint_plan is disabled or current nesting
3045 * depth is nesting depth of SPI calls. Other hook functions try to change
3046 * plan with current_hint_state if any, so set it to NULL.
3048 if (!pg_hint_plan_enable_hint || hint_inhibit_level > 0)
3050 if (debug_level > 1)
3051 ereport(pg_hint_plan_debug_message_level,
3052 (errmsg ("pg_hint_plan%s: planner: enable_hint=%d,"
3053 " hint_inhibit_level=%d",
3054 qnostr, pg_hint_plan_enable_hint,
3055 hint_inhibit_level),
3056 errhidestmt(msgqno != qno)));
3059 goto standard_planner_proc;
3063 * Support for nested plpgsql functions. This is quite ugly but this is the
3064 * only point I could find where I can get the query string.
3066 if (plpgsql_recurse_level > 0 &&
3067 error_context_stack && error_context_stack->arg)
3069 MemoryContext oldcontext;
3071 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
3073 get_hints_from_comment((char *)error_context_stack->arg);
3074 MemoryContextSwitchTo(oldcontext);
3078 * Query execution in extended protocol can be started without the analyze
3079 * phase. In the case retrieve hint string here.
3081 if (!current_hint_str)
3082 get_current_hint_string(NULL, parse);
3084 /* No hint, go the normal way */
3085 if (!current_hint_str)
3086 goto standard_planner_proc;
3088 /* parse the hint into hint state struct */
3089 hstate = create_hintstate(parse, pstrdup(current_hint_str));
3091 /* run standard planner if the statement has not valid hint */
3093 goto standard_planner_proc;
3096 * Push new hint struct to the hint stack to disable previous hint context.
3100 /* Set scan enforcement here. */
3101 save_nestlevel = NewGUCNestLevel();
3103 /* Apply Set hints, then save it as the initial state */
3104 setup_guc_enforcement(current_hint_state->set_hints,
3105 current_hint_state->num_hints[HINT_TYPE_SET],
3106 current_hint_state->context);
3108 current_hint_state->init_scan_mask = get_current_scan_mask();
3109 current_hint_state->init_join_mask = get_current_join_mask();
3110 current_hint_state->init_min_para_tablescan_size =
3111 min_parallel_table_scan_size;
3112 current_hint_state->init_min_para_indexscan_size =
3113 min_parallel_index_scan_size;
3114 current_hint_state->init_paratup_cost = parallel_tuple_cost;
3115 current_hint_state->init_parasetup_cost = parallel_setup_cost;
3118 * max_parallel_workers_per_gather should be non-zero here if Workers hint
3121 if (max_hint_nworkers > 0 && max_parallel_workers_per_gather < 1)
3122 set_config_int32_option("max_parallel_workers_per_gather",
3123 1, current_hint_state->context);
3124 current_hint_state->init_nworkers = max_parallel_workers_per_gather;
3126 if (debug_level > 1)
3128 ereport(pg_hint_plan_debug_message_level,
3129 (errhidestmt(msgqno != qno),
3130 errmsg("pg_hint_plan%s: planner", qnostr)));
3135 * The planner call below may replace current_hint_str. Store and restore
3136 * it so that the subsequent planning in the upper level doesn't get
3140 prev_hint_str = current_hint_str;
3141 current_hint_str = NULL;
3144 * Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
3145 * the state when this planner started when error occurred in planner.
3150 result = (*prev_planner) (parse, cursorOptions, boundParams);
3152 result = standard_planner(parse, cursorOptions, boundParams);
3154 current_hint_str = prev_hint_str;
3160 * Rollback changes of GUC parameters, and pop current hint context
3161 * from hint stack to rewind the state.
3163 current_hint_str = prev_hint_str;
3165 AtEOXact_GUC(true, save_nestlevel);
3173 * current_hint_str is useless after planning of the top-level query.
3175 if (recurse_level < 1 && current_hint_str)
3177 pfree((void *)current_hint_str);
3178 current_hint_str = NULL;
3179 current_hint_retrieved = false;
3182 /* Print hint in debug mode. */
3183 if (debug_level == 1)
3184 HintStateDump(current_hint_state);
3185 else if (debug_level > 1)
3186 HintStateDump2(current_hint_state);
3189 * Rollback changes of GUC parameters, and pop current hint context from
3190 * hint stack to rewind the state.
3192 AtEOXact_GUC(true, save_nestlevel);
3197 standard_planner_proc:
3198 if (debug_level > 1)
3200 ereport(pg_hint_plan_debug_message_level,
3201 (errhidestmt(msgqno != qno),
3202 errmsg("pg_hint_plan%s: planner: no valid hint",
3206 current_hint_state = NULL;
3208 result = (*prev_planner) (parse, cursorOptions, boundParams);
3210 result = standard_planner(parse, cursorOptions, boundParams);
3212 /* The upper-level planner still needs the current hint state */
3213 if (HintStateStack != NIL)
3214 current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
3220 * Find scan method hint to be applied to the given relation
3223 static ScanMethodHint *
3224 find_scan_hint(PlannerInfo *root, Index relid)
3228 ScanMethodHint *real_name_hint = NULL;
3229 ScanMethodHint *alias_hint = NULL;
3232 /* This should not be a join rel */
3234 rel = root->simple_rel_array[relid];
3237 * This function is called for any RelOptInfo or its inheritance parent if
3238 * any. If we are called from inheritance planner, the RelOptInfo for the
3239 * parent of target child relation is not set in the planner info.
3241 * Otherwise we should check that the reloptinfo is base relation or
3242 * inheritance children.
3245 rel->reloptkind != RELOPT_BASEREL &&
3246 rel->reloptkind != RELOPT_OTHER_MEMBER_REL)
3250 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3252 rte = root->simple_rte_array[relid];
3255 /* We don't hint on other than relation and foreign tables */
3256 if (rte->rtekind != RTE_RELATION ||
3257 rte->relkind == RELKIND_FOREIGN_TABLE)
3260 /* Find scan method hint, which matches given names, from the list. */
3261 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
3263 ScanMethodHint *hint = current_hint_state->scan_hints[i];
3265 /* We ignore disabled hints. */
3266 if (!hint_state_enabled(hint))
3270 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3273 /* check the real name for appendrel children */
3274 if (!real_name_hint &&
3275 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3277 char *realname = get_rel_name(rte->relid);
3279 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3280 real_name_hint = hint;
3283 /* No more match expected, break */
3284 if(alias_hint && real_name_hint)
3288 /* real name match precedes alias match */
3290 return real_name_hint;
3295 static ParallelHint *
3296 find_parallel_hint(PlannerInfo *root, Index relid)
3300 ParallelHint *real_name_hint = NULL;
3301 ParallelHint *alias_hint = NULL;
3304 /* This should not be a join rel */
3306 rel = root->simple_rel_array[relid];
3309 * Parallel planning is appliable only on base relation, which has
3316 * We have set root->glob->parallelModeOK if needed. What we should do here
3317 * is just following the decision of planner.
3319 if (!rel->consider_parallel)
3323 * This is baserel or appendrel children. We can refer to RangeTblEntry.
3325 rte = root->simple_rte_array[relid];
3328 /* Find parallel method hint, which matches given names, from the list. */
3329 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_PARALLEL]; i++)
3331 ParallelHint *hint = current_hint_state->parallel_hints[i];
3333 /* We ignore disabled hints. */
3334 if (!hint_state_enabled(hint))
3338 RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3341 /* check the real name for appendrel children */
3342 if (!real_name_hint &&
3343 rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3345 char *realname = get_rel_name(rte->relid);
3347 if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3348 real_name_hint = hint;
3351 /* No more match expected, break */
3352 if(alias_hint && real_name_hint)
3356 /* real name match precedes alias match */
3358 return real_name_hint;
3366 * Returns TRUE on match, FALSE on no match.
3368 * s1 --- the data to match against
3369 * s2 --- the pattern
3371 * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
3374 regexpeq(const char *s1, const char *s2)
3380 strcpy(name.data, s1);
3381 regexp = cstring_to_text(s2);
3383 result = DirectFunctionCall2Coll(nameregexeq,
3384 DEFAULT_COLLATION_OID,
3385 NameGetDatum(&name),
3386 PointerGetDatum(regexp));
3387 return DatumGetBool(result);
3391 /* Remove indexes instructed not to use by hint. */
3393 restrict_indexes(PlannerInfo *root, ScanMethodHint *hint, RelOptInfo *rel,
3394 bool using_parent_hint)
3400 RangeTblEntry *rte = root->simple_rte_array[rel->relid];
3401 Oid relationObjectId = rte->relid;
3404 * We delete all the IndexOptInfo list and prevent you from being usable by
3407 if (hint->enforce_mask == ENABLE_SEQSCAN ||
3408 hint->enforce_mask == ENABLE_TIDSCAN)
3410 list_free_deep(rel->indexlist);
3411 rel->indexlist = NIL;
3412 hint->base.state = HINT_STATE_USED;
3418 * When a list of indexes is not specified, we just use all indexes.
3420 if (hint->indexnames == NIL)
3424 * Leaving only an specified index, we delete it from a IndexOptInfo list
3428 if (debug_level > 0)
3429 initStringInfo(&buf);
3431 for (cell = list_head(rel->indexlist); cell; cell = next)
3433 IndexOptInfo *info = (IndexOptInfo *) lfirst(cell);
3434 char *indexname = get_rel_name(info->indexoid);
3436 bool use_index = false;
3440 foreach(l, hint->indexnames)
3442 char *hintname = (char *) lfirst(l);
3446 result = regexpeq(indexname, hintname);
3448 result = RelnameCmp(&indexname, &hintname) == 0;
3453 if (debug_level > 0)
3455 appendStringInfoCharMacro(&buf, ' ');
3456 quote_value(&buf, indexname);
3464 * Apply index restriction of parent hint to children. Since index
3465 * inheritance is not explicitly described we should search for an
3466 * children's index with the same definition to that of the parent.
3468 if (using_parent_hint && !use_index)
3470 foreach(l, current_hint_state->parent_index_infos)
3474 ParentIndexInfo *p_info = (ParentIndexInfo *)lfirst(l);
3477 * we check the 'same' index by comparing uniqueness, access
3478 * method and index key columns.
3480 if (p_info->indisunique != info->unique ||
3481 p_info->method != info->relam ||
3482 list_length(p_info->column_names) != info->ncolumns)
3485 /* Check if index key columns match */
3486 for (i = 0; i < info->ncolumns; i++)
3488 char *c_attname = NULL;
3489 char *p_attname = NULL;
3491 p_attname = list_nth(p_info->column_names, i);
3494 * if both of the key of the same position are expressions,
3495 * ignore them for now and check later.
3497 if (info->indexkeys[i] == 0 && !p_attname)
3500 /* deny if one is expression while another is not */
3501 if (info->indexkeys[i] == 0 || !p_attname)
3504 c_attname = get_attname(relationObjectId,
3505 info->indexkeys[i]);
3507 /* deny if any of column attributes don't match */
3508 if (strcmp(p_attname, c_attname) != 0 ||
3509 p_info->indcollation[i] != info->indexcollations[i] ||
3510 p_info->opclass[i] != info->opcintype[i]||
3511 ((p_info->indoption[i] & INDOPTION_DESC) != 0)
3512 != info->reverse_sort[i] ||
3513 ((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0)
3514 != info->nulls_first[i])
3518 /* deny this if any difference found */
3519 if (i != info->ncolumns)
3522 /* check on key expressions */
3523 if ((p_info->expression_str && (info->indexprs != NIL)) ||
3524 (p_info->indpred_str && (info->indpred != NIL)))
3526 /* fetch the index of this child */
3527 ht_idx = SearchSysCache1(INDEXRELID,
3528 ObjectIdGetDatum(info->indexoid));
3530 /* check expressions if both expressions are available */
3531 if (p_info->expression_str &&
3532 !heap_attisnull(ht_idx, Anum_pg_index_indexprs))
3539 * to change the expression's parameter of child's
3542 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3543 Anum_pg_index_indexprs,
3546 result = DirectFunctionCall2(pg_get_expr,
3551 /* deny if expressions don't match */
3552 if (strcmp(p_info->expression_str,
3553 text_to_cstring(DatumGetTextP(result))) != 0)
3556 ReleaseSysCache(ht_idx);
3561 /* compare index predicates */
3562 if (p_info->indpred_str &&
3563 !heap_attisnull(ht_idx, Anum_pg_index_indpred))
3569 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3570 Anum_pg_index_indpred,
3573 result = DirectFunctionCall2(pg_get_expr,
3578 if (strcmp(p_info->indpred_str,
3579 text_to_cstring(DatumGetTextP(result))) != 0)
3582 ReleaseSysCache(ht_idx);
3588 ReleaseSysCache(ht_idx);
3590 else if (p_info->expression_str || (info->indexprs != NIL))
3592 else if (p_info->indpred_str || (info->indpred != NIL))
3597 /* to log the candidate of index */
3598 if (debug_level > 0)
3600 appendStringInfoCharMacro(&buf, ' ');
3601 quote_value(&buf, indexname);
3609 rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
3616 if (debug_level == 1)
3618 StringInfoData rel_buf;
3619 char *disprelname = "";
3622 * If this hint targetted the parent, use the real name of this
3623 * child. Otherwise use hint specification.
3625 if (using_parent_hint)
3626 disprelname = get_rel_name(rte->relid);
3628 disprelname = hint->relname;
3631 initStringInfo(&rel_buf);
3632 quote_value(&rel_buf, disprelname);
3634 ereport(pg_hint_plan_debug_message_level,
3635 (errmsg("available indexes for %s(%s):%s",
3640 pfree(rel_buf.data);
3645 * Return information of index definition.
3647 static ParentIndexInfo *
3648 get_parent_index_info(Oid indexoid, Oid relid)
3650 ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
3651 Relation indexRelation;
3652 Form_pg_index index;
3656 indexRelation = index_open(indexoid, RowExclusiveLock);
3658 index = indexRelation->rd_index;
3660 p_info->indisunique = index->indisunique;
3661 p_info->method = indexRelation->rd_rel->relam;
3663 p_info->column_names = NIL;
3664 p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3665 p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3666 p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
3668 for (i = 0; i < index->indnatts; i++)
3670 attname = get_attname(relid, index->indkey.values[i]);
3671 p_info->column_names = lappend(p_info->column_names, attname);
3673 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
3674 p_info->opclass[i] = indexRelation->rd_opcintype[i];
3675 p_info->indoption[i] = indexRelation->rd_indoption[i];
3679 * to check to match the expression's parameter of index with child indexes
3681 p_info->expression_str = NULL;
3682 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs))
3688 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3689 Anum_pg_index_indexprs, &isnull);
3691 result = DirectFunctionCall2(pg_get_expr,
3693 ObjectIdGetDatum(relid));
3695 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
3699 * to check to match the predicate's parameter of index with child indexes
3701 p_info->indpred_str = NULL;
3702 if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred))
3708 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3709 Anum_pg_index_indpred, &isnull);
3711 result = DirectFunctionCall2(pg_get_expr,
3713 ObjectIdGetDatum(relid));
3715 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
3718 index_close(indexRelation, NoLock);
3724 * cancel hint enforcement
3727 reset_hint_enforcement()
3729 setup_scan_method_enforcement(NULL, current_hint_state);
3730 setup_parallel_plan_enforcement(NULL, current_hint_state);
3734 * Set planner guc parameters according to corresponding scan hints. Returns
3735 * bitmap of HintTypeBitmap. If shint or phint is not NULL, set used hint
3736 * there respectively.
3739 setup_hint_enforcement(PlannerInfo *root, RelOptInfo *rel,
3740 ScanMethodHint **rshint, ParallelHint **rphint)
3742 Index new_parent_relid = 0;
3744 ScanMethodHint *shint = NULL;
3745 ParallelHint *phint = NULL;
3746 bool inhparent = root->simple_rte_array[rel->relid]->inh;
3747 Oid relationObjectId = root->simple_rte_array[rel->relid]->relid;
3750 /* reset returns if requested */
3751 if (rshint != NULL) *rshint = NULL;
3752 if (rphint != NULL) *rphint = NULL;
3755 * We could register the parent relation of the following children here
3756 * when inhparent == true but inheritnce planner doesn't call this function
3757 * for parents. Since we cannot distinguish who called this function we
3758 * cannot do other than always seeking the parent regardless of who called
3763 if (debug_level > 1)
3764 ereport(pg_hint_plan_debug_message_level,
3766 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3767 " skipping inh parent: relation=%u(%s), inhparent=%d,"
3768 " current_hint_state=%p, hint_inhibit_level=%d",
3769 qnostr, relationObjectId,
3770 get_rel_name(relationObjectId),
3771 inhparent, current_hint_state, hint_inhibit_level)));
3775 /* Forget about the parent of another subquery */
3776 if (root != current_hint_state->current_root)
3777 current_hint_state->parent_relid = 0;
3779 /* Find the parent for this relation other than the registered parent */
3780 foreach (l, root->append_rel_list)
3782 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3784 if (appinfo->child_relid == rel->relid)
3786 if (current_hint_state->parent_relid != appinfo->parent_relid)
3788 new_parent_relid = appinfo->parent_relid;
3789 current_hint_state->current_root = root;
3797 /* This relation doesn't have a parent. Cancel current_hint_state. */
3798 current_hint_state->parent_relid = 0;
3799 current_hint_state->parent_scan_hint = NULL;
3800 current_hint_state->parent_parallel_hint = NULL;
3803 if (new_parent_relid > 0)
3806 * Here we found a new parent for the current relation. Scan continues
3807 * hint to other childrens of this parent so remember it * to avoid
3808 * hinthintredundant setup cost.
3810 current_hint_state->parent_relid = new_parent_relid;
3812 /* Find hints for the parent */
3813 current_hint_state->parent_scan_hint =
3814 find_scan_hint(root, current_hint_state->parent_relid);
3816 current_hint_state->parent_parallel_hint =
3817 find_parallel_hint(root, current_hint_state->parent_relid);
3820 * If hint is found for the parent, apply it for this child instead
3823 if (current_hint_state->parent_scan_hint)
3825 ScanMethodHint * pshint = current_hint_state->parent_scan_hint;
3827 pshint->base.state = HINT_STATE_USED;
3829 /* Apply index mask in the same manner to the parent. */
3830 if (pshint->indexnames)
3833 Relation parent_rel;
3836 root->simple_rte_array[current_hint_state->parent_relid]->relid;
3837 parent_rel = heap_open(parentrel_oid, NoLock);
3839 /* Search the parent relation for indexes match the hint spec */
3840 foreach(l, RelationGetIndexList(parent_rel))
3842 Oid indexoid = lfirst_oid(l);
3843 char *indexname = get_rel_name(indexoid);
3845 ParentIndexInfo *parent_index_info;
3847 foreach(lc, pshint->indexnames)
3849 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
3856 get_parent_index_info(indexoid, parentrel_oid);
3857 current_hint_state->parent_index_infos =
3858 lappend(current_hint_state->parent_index_infos,
3861 heap_close(parent_rel, NoLock);
3866 shint = find_scan_hint(root, rel->relid);
3868 shint = current_hint_state->parent_scan_hint;
3872 bool using_parent_hint =
3873 (shint == current_hint_state->parent_scan_hint);
3875 ret |= HINT_BM_SCAN_METHOD;
3877 /* Setup scan enforcement environment */
3878 setup_scan_method_enforcement(shint, current_hint_state);
3880 /* restrict unwanted inexes */
3881 restrict_indexes(root, shint, rel, using_parent_hint);
3883 if (debug_level > 1)
3885 char *additional_message = "";
3887 if (shint == current_hint_state->parent_scan_hint)
3888 additional_message = " by parent hint";
3890 ereport(pg_hint_plan_debug_message_level,
3892 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3893 " index deletion%s:"
3894 " relation=%u(%s), inhparent=%d, "
3895 "current_hint_state=%p,"
3896 " hint_inhibit_level=%d, scanmask=0x%x",
3897 qnostr, additional_message,
3899 get_rel_name(relationObjectId),
3900 inhparent, current_hint_state,
3902 shint->enforce_mask)));
3906 /* Do the same for parallel plan enforcement */
3907 phint = find_parallel_hint(root, rel->relid);
3909 phint = current_hint_state->parent_parallel_hint;
3911 setup_parallel_plan_enforcement(phint, current_hint_state);
3914 ret |= HINT_BM_PARALLEL;
3916 /* Nothing to apply. Reset the scan mask to intial state */
3917 if (!shint && ! phint)
3919 if (debug_level > 1)
3920 ereport(pg_hint_plan_debug_message_level,
3921 (errhidestmt (true),
3922 errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3924 " relation=%u(%s), inhparent=%d, current_hint=%p,"
3925 " hint_inhibit_level=%d, scanmask=0x%x",
3926 qnostr, relationObjectId,
3927 get_rel_name(relationObjectId),
3928 inhparent, current_hint_state, hint_inhibit_level,
3929 current_hint_state->init_scan_mask)));
3931 setup_scan_method_enforcement(NULL, current_hint_state);
3936 if (rshint != NULL) *rshint = shint;
3937 if (rphint != NULL) *rphint = phint;
3943 * Return index of relation which matches given aliasname, or 0 if not found.
3944 * If same aliasname was used multiple times in a query, return -1.
3947 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
3953 for (i = 1; i < root->simple_rel_array_size; i++)
3957 if (root->simple_rel_array[i] == NULL)
3960 Assert(i == root->simple_rel_array[i]->relid);
3962 if (RelnameCmp(&aliasname,
3963 &root->simple_rte_array[i]->eref->aliasname) != 0)
3966 foreach(l, initial_rels)
3968 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3970 if (rel->reloptkind == RELOPT_BASEREL)
3972 if (rel->relid != i)
3977 Assert(rel->reloptkind == RELOPT_JOINREL);
3979 if (!bms_is_member(i, rel->relids))
3986 ("Relation name \"%s\" is ambiguous.",
4001 * Return join hint which matches given joinrelids.
4003 static JoinMethodHint *
4004 find_join_hint(Relids joinrelids)
4009 join_hint = current_hint_state->join_hint_level[bms_num_members(joinrelids)];
4011 foreach(l, join_hint)
4013 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
4015 if (bms_equal(joinrelids, hint->joinrelids))
4023 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
4024 PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
4026 OuterInnerRels *outer_rels;
4027 OuterInnerRels *inner_rels;
4028 Relids outer_relids;
4029 Relids inner_relids;
4031 JoinMethodHint *hint;
4033 if (outer_inner->relation != NULL)
4035 return bms_make_singleton(
4036 find_relid_aliasname(root, outer_inner->relation,
4038 leading_hint->base.hint_str));
4041 outer_rels = lfirst(outer_inner->outer_inner_pair->head);
4042 inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
4044 outer_relids = OuterInnerJoinCreate(outer_rels,
4050 inner_relids = OuterInnerJoinCreate(inner_rels,
4057 join_relids = bms_add_members(outer_relids, inner_relids);
4059 if (bms_num_members(join_relids) > nbaserel)
4063 * If we don't have join method hint, create new one for the
4064 * join combination with all join methods are enabled.
4066 hint = find_join_hint(join_relids);
4070 * Here relnames is not set, since Relids bitmap is sufficient to
4071 * control paths of this query afterward.
4073 hint = (JoinMethodHint *) JoinMethodHintCreate(
4074 leading_hint->base.hint_str,
4076 HINT_KEYWORD_LEADING);
4077 hint->base.state = HINT_STATE_USED;
4078 hint->nrels = bms_num_members(join_relids);
4079 hint->enforce_mask = ENABLE_ALL_JOIN;
4080 hint->joinrelids = bms_copy(join_relids);
4081 hint->inner_nrels = bms_num_members(inner_relids);
4082 hint->inner_joinrelids = bms_copy(inner_relids);
4084 hstate->join_hint_level[hint->nrels] =
4085 lappend(hstate->join_hint_level[hint->nrels], hint);
4089 hint->inner_nrels = bms_num_members(inner_relids);
4090 hint->inner_joinrelids = bms_copy(inner_relids);
4097 create_bms_of_relids(Hint *base, PlannerInfo *root, List *initial_rels,
4098 int nrels, char **relnames)
4101 Relids relids = NULL;
4105 for (j = 0; j < nrels; j++)
4107 relname = relnames[j];
4109 relid = find_relid_aliasname(root, relname, initial_rels,
4113 base->state = HINT_STATE_ERROR;
4116 * the aliasname is not found(relid == 0) or same aliasname was used
4117 * multiple times in a query(relid == -1)
4124 if (bms_is_member(relid, relids))
4126 hint_ereport(base->hint_str,
4127 ("Relation name \"%s\" is duplicated.", relname));
4128 base->state = HINT_STATE_ERROR;
4132 relids = bms_add_member(relids, relid);
4137 * Transform join method hint into handy form.
4139 * - create bitmap of relids from alias names, to make it easier to check
4140 * whether a join path matches a join method hint.
4141 * - add join method hints which are necessary to enforce join order
4142 * specified by Leading hint
4145 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
4146 List *initial_rels, JoinMethodHint **join_method_hints)
4154 LeadingHint *lhint = NULL;
4157 * Create bitmap of relids from alias names for each join method hint.
4158 * Bitmaps are more handy than strings in join searching.
4160 for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
4162 JoinMethodHint *hint = hstate->join_hints[i];
4164 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4167 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4168 initial_rels, hint->nrels, hint->relnames);
4170 if (hint->joinrelids == NULL || hint->base.state == HINT_STATE_ERROR)
4173 hstate->join_hint_level[hint->nrels] =
4174 lappend(hstate->join_hint_level[hint->nrels], hint);
4178 * Create bitmap of relids from alias names for each rows hint.
4179 * Bitmaps are more handy than strings in join searching.
4181 for (i = 0; i < hstate->num_hints[HINT_TYPE_ROWS]; i++)
4183 RowsHint *hint = hstate->rows_hints[i];
4185 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4188 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4189 initial_rels, hint->nrels, hint->relnames);
4192 /* Do nothing if no Leading hint was supplied. */
4193 if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
4197 * Decide whether to use Leading hint
4199 for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
4201 LeadingHint *leading_hint = (LeadingHint *)hstate->leading_hint[i];
4204 if (leading_hint->base.state == HINT_STATE_ERROR)
4210 foreach(l, leading_hint->relations)
4212 relname = (char *)lfirst(l);;
4214 relid = find_relid_aliasname(root, relname, initial_rels,
4215 leading_hint->base.hint_str);
4217 leading_hint->base.state = HINT_STATE_ERROR;
4222 if (bms_is_member(relid, relids))
4224 hint_ereport(leading_hint->base.hint_str,
4225 ("Relation name \"%s\" is duplicated.", relname));
4226 leading_hint->base.state = HINT_STATE_ERROR;
4230 relids = bms_add_member(relids, relid);
4233 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
4238 hint_ereport(lhint->base.hint_str,
4239 ("Conflict %s hint.", HintTypeName[lhint->base.type]));
4240 lhint->base.state = HINT_STATE_DUPLICATION;
4242 leading_hint->base.state = HINT_STATE_USED;
4243 lhint = leading_hint;
4246 /* check to exist Leading hint marked with 'used'. */
4251 * We need join method hints which fit specified join order in every join
4252 * level. For example, Leading(A B C) virtually requires following join
4253 * method hints, if no join method hint supplied:
4255 * - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
4256 * - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
4258 * If we already have join method hint which fits specified join order in
4259 * that join level, we leave it as-is and don't add new hints.
4263 if (lhint->outer_inner == NULL)
4265 foreach(l, lhint->relations)
4267 JoinMethodHint *hint;
4269 relname = (char *)lfirst(l);
4272 * Find relid of the relation which has given name. If we have the
4273 * name given in Leading hint multiple times in the join, nothing to
4276 relid = find_relid_aliasname(root, relname, initial_rels,
4279 /* Create bitmap of relids for current join level. */
4280 joinrelids = bms_add_member(joinrelids, relid);
4283 /* We never have join method hint for single relation. */
4288 * If we don't have join method hint, create new one for the
4289 * join combination with all join methods are enabled.
4291 hint = find_join_hint(joinrelids);
4295 * Here relnames is not set, since Relids bitmap is sufficient
4296 * to control paths of this query afterward.
4298 hint = (JoinMethodHint *) JoinMethodHintCreate(
4299 lhint->base.hint_str,
4301 HINT_KEYWORD_LEADING);
4302 hint->base.state = HINT_STATE_USED;
4303 hint->nrels = njoinrels;
4304 hint->enforce_mask = ENABLE_ALL_JOIN;
4305 hint->joinrelids = bms_copy(joinrelids);
4308 join_method_hints[njoinrels] = hint;
4310 if (njoinrels >= nbaserel)
4313 bms_free(joinrelids);
4319 * Delete all join hints which have different combination from Leading
4322 for (i = 2; i <= njoinrels; i++)
4324 list_free(hstate->join_hint_level[i]);
4326 hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
4331 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
4338 njoinrels = bms_num_members(joinrelids);
4339 Assert(njoinrels >= 2);
4342 * Delete all join hints which have different combination from Leading
4345 for (i = 2;i <= njoinrels; i++)
4347 if (hstate->join_hint_level[i] != NIL)
4349 ListCell *prev = NULL;
4350 ListCell *next = NULL;
4351 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
4354 JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
4358 if (hint->inner_nrels == 0 &&
4359 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
4360 bms_equal(bms_union(hint->joinrelids, joinrelids),
4363 hstate->join_hint_level[i] =
4364 list_delete_cell(hstate->join_hint_level[i], l,
4373 bms_free(joinrelids);
4376 if (hint_state_enabled(lhint))
4378 set_join_config_options(DISABLE_ALL_JOIN, current_hint_state->context);
4385 * wrapper of make_join_rel()
4387 * call make_join_rel() after changing enable_* parameters according to given
4391 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
4394 JoinMethodHint *hint;
4398 joinrelids = bms_union(rel1->relids, rel2->relids);
4399 hint = find_join_hint(joinrelids);
4400 bms_free(joinrelids);
4403 return pg_hint_plan_make_join_rel(root, rel1, rel2);
4405 if (hint->inner_nrels == 0)
4407 save_nestlevel = NewGUCNestLevel();
4409 set_join_config_options(hint->enforce_mask,
4410 current_hint_state->context);
4412 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4413 hint->base.state = HINT_STATE_USED;
4416 * Restore the GUC variables we set above.
4418 AtEOXact_GUC(true, save_nestlevel);
4421 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4430 add_paths_to_joinrel_wrapper(PlannerInfo *root,
4431 RelOptInfo *joinrel,
4432 RelOptInfo *outerrel,
4433 RelOptInfo *innerrel,
4435 SpecialJoinInfo *sjinfo,
4439 JoinMethodHint *join_hint;
4442 joinrelids = bms_union(outerrel->relids, innerrel->relids);
4443 join_hint = find_join_hint(joinrelids);
4444 bms_free(joinrelids);
4446 if (join_hint && join_hint->inner_nrels != 0)
4448 save_nestlevel = NewGUCNestLevel();
4450 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
4453 set_join_config_options(join_hint->enforce_mask,
4454 current_hint_state->context);
4456 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4457 sjinfo, restrictlist);
4458 join_hint->base.state = HINT_STATE_USED;
4462 set_join_config_options(DISABLE_ALL_JOIN,
4463 current_hint_state->context);
4464 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4465 sjinfo, restrictlist);
4469 * Restore the GUC variables we set above.
4471 AtEOXact_GUC(true, save_nestlevel);
4474 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4475 sjinfo, restrictlist);
4479 get_num_baserels(List *initial_rels)
4484 foreach(l, initial_rels)
4486 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
4488 if (rel->reloptkind == RELOPT_BASEREL)
4490 else if (rel->reloptkind ==RELOPT_JOINREL)
4491 nbaserel+= bms_num_members(rel->relids);
4494 /* other values not expected here */
4495 elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
4503 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
4506 JoinMethodHint **join_method_hints;
4510 bool leading_hint_enable;
4513 * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
4514 * valid hint is supplied or current nesting depth is nesting depth of SPI
4517 if (!current_hint_state || hint_inhibit_level > 0)
4519 if (prev_join_search)
4520 return (*prev_join_search) (root, levels_needed, initial_rels);
4521 else if (enable_geqo && levels_needed >= geqo_threshold)
4522 return geqo(root, levels_needed, initial_rels);
4524 return standard_join_search(root, levels_needed, initial_rels);
4528 * In the case using GEQO, only scan method hints and Set hints have
4529 * effect. Join method and join order is not controllable by hints.
4531 if (enable_geqo && levels_needed >= geqo_threshold)
4532 return geqo(root, levels_needed, initial_rels);
4534 nbaserel = get_num_baserels(initial_rels);
4535 current_hint_state->join_hint_level =
4536 palloc0(sizeof(List *) * (nbaserel + 1));
4537 join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
4539 leading_hint_enable = transform_join_hints(current_hint_state,
4541 initial_rels, join_method_hints);
4543 rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
4545 for (i = 2; i <= nbaserel; i++)
4547 list_free(current_hint_state->join_hint_level[i]);
4549 /* free Leading hint only */
4550 if (join_method_hints[i] != NULL &&
4551 join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
4552 JoinMethodHintDelete(join_method_hints[i]);
4554 pfree(current_hint_state->join_hint_level);
4555 pfree(join_method_hints);
4557 if (leading_hint_enable)
4558 set_join_config_options(current_hint_state->init_join_mask,
4559 current_hint_state->context);
4565 * Force number of wokers if instructed by hint
4568 pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
4569 Index rti, RangeTblEntry *rte)
4571 ParallelHint *phint;
4575 /* call the previous hook */
4576 if (prev_set_rel_pathlist)
4577 prev_set_rel_pathlist(root, rel, rti, rte);
4579 /* Nothing to do if no hint available */
4580 if (current_hint_state == NULL)
4583 /* Don't touch dummy rels. */
4584 if (IS_DUMMY_REL(rel))
4588 * We can accept only plain relations, foreign tables and table saples are
4589 * also unacceptable. See set_rel_pathlist.
4591 if (rel->rtekind != RTE_RELATION ||
4592 rte->relkind == RELKIND_FOREIGN_TABLE ||
4593 rte->tablesample != NULL)
4596 /* We cannot handle if this requires an outer */
4597 if (rel->lateral_relids)
4600 /* Return if this relation gets no enfocement */
4601 if ((found_hints = setup_hint_enforcement(root, rel, NULL, &phint)) == 0)
4604 /* Here, we regenerate paths with the current hint restriction */
4605 if (found_hints & HINT_BM_SCAN_METHOD || found_hints & HINT_BM_PARALLEL)
4607 /* Just discard all the paths considered so far */
4608 list_free_deep(rel->pathlist);
4609 rel->pathlist = NIL;
4611 list_free_deep(rel->partial_pathlist);
4612 rel->partial_pathlist = NIL;
4614 /* Regenerate paths with the current enforcement */
4615 set_plain_rel_pathlist(root, rel, rte);
4617 /* Additional work to enforce parallel query execution */
4618 if (phint && phint->nworkers > 0)
4620 /* Lower the priorities of non-parallel paths */
4621 foreach (l, rel->pathlist)
4623 Path *path = (Path *) lfirst(l);
4625 if (path->startup_cost < disable_cost)
4627 path->startup_cost += disable_cost;
4628 path->total_cost += disable_cost;
4632 /* enforce number of workers if requested */
4633 if (phint->force_parallel)
4635 foreach (l, rel->partial_pathlist)
4637 Path *ppath = (Path *) lfirst(l);
4639 ppath->parallel_workers = phint->nworkers;
4643 /* Generate gather paths for base rels */
4644 if (rel->reloptkind == RELOPT_BASEREL)
4645 generate_gather_paths(root, rel);
4649 reset_hint_enforcement();
4654 * Build access paths for a base relation
4656 * This function was copied and edited from set_rel_pathlist() in
4657 * src/backend/optimizer/path/allpaths.c in order not to copy other static
4658 * functions not required here.
4661 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
4662 Index rti, RangeTblEntry *rte)
4664 if (IS_DUMMY_REL(rel))
4666 /* We already proved the relation empty, so nothing more to do */
4670 /* It's an "append relation", process accordingly */
4671 set_append_rel_pathlist(root, rel, rti, rte);
4675 if (rel->rtekind == RTE_RELATION)
4677 if (rte->relkind == RELKIND_RELATION)
4679 if(rte->tablesample != NULL)
4680 elog(ERROR, "sampled relation is not supported");
4682 /* Plain relation */
4683 set_plain_rel_pathlist(root, rel, rte);
4686 elog(ERROR, "unexpected relkind: %c", rte->relkind);
4689 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
4693 * Allow a plugin to editorialize on the set of Paths for this base
4694 * relation. It could add new paths (such as CustomPaths) by calling
4695 * add_path(), or delete or modify paths added by the core code.
4697 if (set_rel_pathlist_hook)
4698 (*set_rel_pathlist_hook) (root, rel, rti, rte);
4700 /* Now find the cheapest of the paths for this rel */
4705 * stmt_beg callback is called when each query in PL/pgSQL function is about
4706 * to be executed. At that timing, we save query string in the global variable
4707 * plpgsql_query_string to use it in planner hook. It's safe to use one global
4708 * variable for the purpose, because its content is only necessary until
4709 * planner hook is called for the query, so recursive PL/pgSQL function calls
4710 * don't harm this mechanism.
4713 pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4715 plpgsql_recurse_level++;
4719 * stmt_end callback is called then each query in PL/pgSQL function has
4720 * finished. At that timing, we clear plpgsql_query_string to tell planner
4721 * hook that next call is not for a query written in PL/pgSQL block.
4724 pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4726 plpgsql_recurse_level--;
4729 void plpgsql_query_erase_callback(ResourceReleasePhase phase,
4734 if (!isTopLevel || phase != RESOURCE_RELEASE_AFTER_LOCKS)
4736 /* Cancel plpgsql nest level*/
4737 plpgsql_recurse_level = 0;
4740 #define standard_join_search pg_hint_plan_standard_join_search
4741 #define join_search_one_level pg_hint_plan_join_search_one_level
4742 #define make_join_rel make_join_rel_wrapper
4745 #undef make_join_rel
4746 #define make_join_rel pg_hint_plan_make_join_rel
4747 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
4748 #include "make_join_rel.c"
4750 #include "pg_stat_statements.c"