1 /*-------------------------------------------------------------------------
4 * do instructions or hints to the planner using C-style block comments
7 * Copyright (c) 2012, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
9 *-------------------------------------------------------------------------
12 #include "commands/prepare.h"
13 #include "mb/pg_wchar.h"
14 #include "miscadmin.h"
15 #include "nodes/nodeFuncs.h"
16 #include "optimizer/clauses.h"
17 #include "optimizer/cost.h"
18 #include "optimizer/geqo.h"
19 #include "optimizer/joininfo.h"
20 #include "optimizer/pathnode.h"
21 #include "optimizer/paths.h"
22 #include "optimizer/plancat.h"
23 #include "optimizer/planner.h"
24 #include "optimizer/prep.h"
25 #include "optimizer/restrictinfo.h"
26 #include "parser/scansup.h"
27 #include "tcop/utility.h"
28 #include "utils/lsyscache.h"
29 #include "utils/memutils.h"
31 #ifdef PG_MODULE_MAGIC
35 #if PG_VERSION_NUM < 90100
36 #error unsupported PostgreSQL version
39 #define BLOCK_COMMENT_START "/*"
40 #define BLOCK_COMMENT_END "*/"
41 #define HINT_COMMENT_KEYWORD "+"
42 #define HINT_START BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
43 #define HINT_END BLOCK_COMMENT_END
46 #define HINT_SEQSCAN "SeqScan"
47 #define HINT_INDEXSCAN "IndexScan"
48 #define HINT_BITMAPSCAN "BitmapScan"
49 #define HINT_TIDSCAN "TidScan"
50 #define HINT_NOSEQSCAN "NoSeqScan"
51 #define HINT_NOINDEXSCAN "NoIndexScan"
52 #define HINT_NOBITMAPSCAN "NoBitmapScan"
53 #define HINT_NOTIDSCAN "NoTidScan"
54 #define HINT_NESTLOOP "NestLoop"
55 #define HINT_MERGEJOIN "MergeJoin"
56 #define HINT_HASHJOIN "HashJoin"
57 #define HINT_NONESTLOOP "NoNestLoop"
58 #define HINT_NOMERGEJOIN "NoMergeJoin"
59 #define HINT_NOHASHJOIN "NoHashJoin"
60 #define HINT_LEADING "Leading"
61 #define HINT_SET "Set"
63 #define HINT_ARRAY_DEFAULT_INITSIZE 8
65 #define parse_ereport(str, detail) \
66 ereport(pg_hint_plan_parse_messages, \
67 (errmsg("hint syntax error at or near \"%s\"", (str)), \
70 #define skip_space(str) \
71 while (isspace(*str)) \
76 ENABLE_SEQSCAN = 0x01,
77 ENABLE_INDEXSCAN = 0x02,
78 ENABLE_BITMAPSCAN = 0x04,
79 ENABLE_TIDSCAN = 0x08,
84 ENABLE_NESTLOOP = 0x01,
85 ENABLE_MERGEJOIN = 0x02,
86 ENABLE_HASHJOIN = 0x04
89 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | ENABLE_BITMAPSCAN \
91 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
92 #define DISABLE_ALL_SCAN 0
93 #define DISABLE_ALL_JOIN 0
95 typedef struct Hint Hint;
96 typedef struct PlanHint PlanHint;
98 typedef Hint *(*HintCreateFunction) (const char *hint_str,
100 typedef void (*HintDeleteFunction) (Hint *hint);
101 typedef void (*HintDumpFunction) (Hint *hint, StringInfo buf);
102 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
103 typedef const char *(*HintParseFunction) (Hint *hint, PlanHint *plan,
104 Query *parse, const char *str);
107 #define NUM_HINT_TYPE 4
108 typedef enum HintType
110 HINT_TYPE_SCAN_METHOD,
111 HINT_TYPE_JOIN_METHOD,
117 typedef enum HintStatus
119 HINT_STATE_NOTUSED = 0, /* specified relation not used in query */
120 HINT_STATE_USED, /* hint is used */
121 HINT_STATE_DUPLICATION, /* specified hint duplication */
122 HINT_STATE_ERROR /* execute error (parse error does not include
126 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
127 (hint)->base.state == HINT_STATE_USED)
129 /* common data for all hints. */
132 const char *hint_str; /* must not do pfree */
133 const char *keyword; /* must not do pfree */
136 HintDeleteFunction delete_func;
137 HintDumpFunction dump_func;
138 HintCmpFunction cmp_func;
139 HintParseFunction parser_func;
142 /* scan method hints */
143 typedef struct ScanMethodHint
148 unsigned char enforce_mask;
151 /* join method hints */
152 typedef struct JoinMethodHint
157 unsigned char enforce_mask;
161 /* join order hints */
162 typedef struct LeadingHint
165 List *relations; /* relation names specified in Leading hint */
168 /* change a run-time parameter hints */
169 typedef struct SetHint
172 char *name; /* name of variable */
177 * Describes a context of hint processing.
181 char *hint_str; /* original hint string */
184 int nall_hints; /* # of valid all hints */
185 int max_all_hints; /* # of slots for all hints */
186 Hint **all_hints; /* parsed all hints */
188 /* # of each hints */
189 int num_hints[NUM_HINT_TYPE];
191 /* for scan method hints */
192 ScanMethodHint **scan_hints; /* parsed scan hints */
193 int init_scan_mask; /* initial value scan parameter */
194 Index parent_relid; /* inherit parent table relid */
195 ScanMethodHint *parent_hint; /* inherit parent table scan hint */
197 /* for join method hints */
198 JoinMethodHint **join_hints; /* parsed join hints */
199 int init_join_mask; /* initial value join parameter */
200 List **join_hint_level;
202 /* for Leading hint */
203 LeadingHint *leading_hint; /* parsed last specified Leading hint */
206 SetHint **set_hints; /* parsed Set hints */
207 GucContext context; /* which GUC parameters can we set? */
211 * Describes a hint parser module which is bound with particular hint keyword.
213 typedef struct HintParser
216 HintCreateFunction create_func;
219 /* Module callbacks */
223 static void push_hint(PlanHint *plan);
224 static void pop_hint(void);
226 static void pg_hint_plan_ProcessUtility(Node *parsetree,
227 const char *queryString,
228 ParamListInfo params, bool isTopLevel,
230 char *completionTag);
231 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
232 ParamListInfo boundParams);
233 static void pg_hint_plan_get_relation_info(PlannerInfo *root,
234 Oid relationObjectId,
235 bool inhparent, RelOptInfo *rel);
236 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
240 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword);
241 static void ScanMethodHintDelete(ScanMethodHint *hint);
242 static void ScanMethodHintDump(ScanMethodHint *hint, StringInfo buf);
243 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
244 static const char *ScanMethodHintParse(ScanMethodHint *hint, PlanHint *plan,
245 Query *parse, const char *str);
246 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword);
247 static void JoinMethodHintDelete(JoinMethodHint *hint);
248 static void JoinMethodHintDump(JoinMethodHint *hint, StringInfo buf);
249 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
250 static const char *JoinMethodHintParse(JoinMethodHint *hint, PlanHint *plan,
251 Query *parse, const char *str);
252 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword);
253 static void LeadingHintDelete(LeadingHint *hint);
254 static void LeadingHintDump(LeadingHint *hint, StringInfo buf);
255 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
256 static const char *LeadingHintParse(LeadingHint *hint, PlanHint *plan,
257 Query *parse, const char *str);
258 static Hint *SetHintCreate(const char *hint_str, const char *keyword);
259 static void SetHintDelete(SetHint *hint);
260 static void SetHintDump(SetHint *hint, StringInfo buf);
261 static int SetHintCmp(const SetHint *a, const SetHint *b);
262 static const char *SetHintParse(SetHint *hint, PlanHint *plan, Query *parse,
265 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
268 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
269 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
270 ListCell *other_rels);
271 static void make_rels_by_clauseless_joins(PlannerInfo *root,
273 ListCell *other_rels);
274 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
275 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
276 Index rti, RangeTblEntry *rte);
277 static List *accumulate_append_subpath(List *subpaths, Path *path);
278 static void set_dummy_rel_pathlist(RelOptInfo *rel);
279 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
283 static bool pg_hint_plan_enable = true;
284 static bool pg_hint_plan_debug_print = false;
285 static int pg_hint_plan_parse_messages = INFO;
287 static const struct config_enum_entry parse_messages_level_options[] = {
288 {"debug", DEBUG2, true},
289 {"debug5", DEBUG5, false},
290 {"debug4", DEBUG4, false},
291 {"debug3", DEBUG3, false},
292 {"debug2", DEBUG2, false},
293 {"debug1", DEBUG1, false},
295 {"info", INFO, false},
296 {"notice", NOTICE, false},
297 {"warning", WARNING, false},
298 {"error", ERROR, false},
300 * {"fatal", FATAL, true},
301 * {"panic", PANIC, true},
306 /* Saved hook values in case of unload */
307 static ProcessUtility_hook_type prev_ProcessUtility = NULL;
308 static planner_hook_type prev_planner = NULL;
309 static get_relation_info_hook_type prev_get_relation_info = NULL;
310 static join_search_hook_type prev_join_search = NULL;
312 /* フック関数をまたがって使用する情報を管理する */
313 static PlanHint *current_hint = NULL;
315 /* 有効なヒントをスタック構造で管理する */
316 static List *PlanHintStack = NIL;
319 * EXECUTEコマンド実行時に、ステートメント名を格納する。
320 * その他のコマンドの場合は、NULLに設定する。
322 static char *stmt_name = NULL;
324 static const HintParser parsers[] = {
325 {HINT_SEQSCAN, ScanMethodHintCreate},
326 {HINT_INDEXSCAN, ScanMethodHintCreate},
327 {HINT_BITMAPSCAN, ScanMethodHintCreate},
328 {HINT_TIDSCAN, ScanMethodHintCreate},
329 {HINT_NOSEQSCAN, ScanMethodHintCreate},
330 {HINT_NOINDEXSCAN, ScanMethodHintCreate},
331 {HINT_NOBITMAPSCAN, ScanMethodHintCreate},
332 {HINT_NOTIDSCAN, ScanMethodHintCreate},
333 {HINT_NESTLOOP, JoinMethodHintCreate},
334 {HINT_MERGEJOIN, JoinMethodHintCreate},
335 {HINT_HASHJOIN, JoinMethodHintCreate},
336 {HINT_NONESTLOOP, JoinMethodHintCreate},
337 {HINT_NOMERGEJOIN, JoinMethodHintCreate},
338 {HINT_NOHASHJOIN, JoinMethodHintCreate},
339 {HINT_LEADING, LeadingHintCreate},
340 {HINT_SET, SetHintCreate},
345 * Module load callbacks
350 /* Define custom GUC variables. */
351 DefineCustomBoolVariable("pg_hint_plan.enable",
352 "Force planner to use plans specified in the hint comment preceding to the query.",
354 &pg_hint_plan_enable,
362 DefineCustomBoolVariable("pg_hint_plan.debug_print",
363 "Logs results of hint parsing.",
365 &pg_hint_plan_debug_print,
373 DefineCustomEnumVariable("pg_hint_plan.parse_messages",
374 "Messege level of parse errors.",
376 &pg_hint_plan_parse_messages,
378 parse_messages_level_options,
386 prev_ProcessUtility = ProcessUtility_hook;
387 ProcessUtility_hook = pg_hint_plan_ProcessUtility;
388 prev_planner = planner_hook;
389 planner_hook = pg_hint_plan_planner;
390 prev_get_relation_info = get_relation_info_hook;
391 get_relation_info_hook = pg_hint_plan_get_relation_info;
392 prev_join_search = join_search_hook;
393 join_search_hook = pg_hint_plan_join_search;
397 * Module unload callback
403 /* Uninstall hooks. */
404 ProcessUtility_hook = prev_ProcessUtility;
405 planner_hook = prev_planner;
406 get_relation_info_hook = prev_get_relation_info;
407 join_search_hook = prev_join_search;
411 * create and delete functions the hint object
415 ScanMethodHintCreate(const char *hint_str, const char *keyword)
417 ScanMethodHint *hint;
419 hint = palloc(sizeof(ScanMethodHint));
420 hint->base.hint_str = hint_str;
421 hint->base.keyword = keyword;
422 hint->base.type = HINT_TYPE_SCAN_METHOD;
423 hint->base.state = HINT_STATE_NOTUSED;
424 hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
425 hint->base.dump_func = (HintDumpFunction) ScanMethodHintDump;
426 hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
427 hint->base.parser_func = (HintParseFunction) ScanMethodHintParse;
428 hint->relname = NULL;
429 hint->indexnames = NIL;
430 hint->enforce_mask = 0;
432 return (Hint *) hint;
436 ScanMethodHintDelete(ScanMethodHint *hint)
442 pfree(hint->relname);
443 list_free_deep(hint->indexnames);
448 JoinMethodHintCreate(const char *hint_str, const char *keyword)
450 JoinMethodHint *hint;
452 hint = palloc(sizeof(JoinMethodHint));
453 hint->base.hint_str = hint_str;
454 hint->base.keyword = keyword;
455 hint->base.type = HINT_TYPE_JOIN_METHOD;
456 hint->base.state = HINT_STATE_NOTUSED;
457 hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
458 hint->base.dump_func = (HintDumpFunction) JoinMethodHintDump;
459 hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
460 hint->base.parser_func = (HintParseFunction) JoinMethodHintParse;
462 hint->relnames = NULL;
463 hint->enforce_mask = 0;
464 hint->joinrelids = NULL;
466 return (Hint *) hint;
470 JoinMethodHintDelete(JoinMethodHint *hint)
479 for (i = 0; i < hint->nrels; i++)
480 pfree(hint->relnames[i]);
481 pfree(hint->relnames);
483 bms_free(hint->joinrelids);
488 LeadingHintCreate(const char *hint_str, const char *keyword)
492 hint = palloc(sizeof(LeadingHint));
493 hint->base.hint_str = hint_str;
494 hint->base.keyword = keyword;
495 hint->base.type = HINT_TYPE_LEADING;
496 hint->base.state = HINT_STATE_NOTUSED;
497 hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
498 hint->base.dump_func = (HintDumpFunction) LeadingHintDump;
499 hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
500 hint->base.parser_func = (HintParseFunction) LeadingHintParse;
501 hint->relations = NIL;
503 return (Hint *) hint;
507 LeadingHintDelete(LeadingHint *hint)
512 list_free_deep(hint->relations);
517 SetHintCreate(const char *hint_str, const char *keyword)
521 hint = palloc(sizeof(SetHint));
522 hint->base.hint_str = hint_str;
523 hint->base.keyword = keyword;
524 hint->base.type = HINT_TYPE_SET;
525 hint->base.state = HINT_STATE_NOTUSED;
526 hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
527 hint->base.dump_func = (HintDumpFunction) SetHintDump;
528 hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
529 hint->base.parser_func = (HintParseFunction) SetHintParse;
533 return (Hint *) hint;
537 SetHintDelete(SetHint *hint)
554 hint = palloc(sizeof(PlanHint));
555 hint->hint_str = NULL;
556 hint->nall_hints = 0;
557 hint->max_all_hints = 0;
558 hint->all_hints = NULL;
559 memset(hint->num_hints, 0, sizeof(hint->num_hints));
560 hint->scan_hints = NULL;
561 hint->init_scan_mask = 0;
562 hint->parent_relid = 0;
563 hint->parent_hint = NULL;
564 hint->join_hints = NULL;
565 hint->init_join_mask = 0;
566 hint->join_hint_level = NULL;
567 hint->leading_hint = NULL;
568 hint->context = superuser() ? PGC_SUSET : PGC_USERSET;
569 hint->set_hints = NULL;
575 PlanHintDelete(PlanHint *hint)
583 pfree(hint->hint_str);
585 for (i = 0; i < hint->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
586 hint->all_hints[i]->delete_func(hint->all_hints[i]);
588 pfree(hint->all_hints);
596 dump_quote_value(StringInfo buf, const char *value)
598 bool need_quote = false;
601 for (str = value; *str != '\0'; str++)
603 if (isspace(*str) || *str == ')' || *str == '"')
606 appendStringInfoCharMacro(buf, '"');
611 for (str = value; *str != '\0'; str++)
614 appendStringInfoCharMacro(buf, '"');
616 appendStringInfoCharMacro(buf, *str);
620 appendStringInfoCharMacro(buf, '"');
624 ScanMethodHintDump(ScanMethodHint *hint, StringInfo buf)
628 appendStringInfo(buf, "%s(", hint->base.keyword);
629 dump_quote_value(buf, hint->relname);
630 foreach(l, hint->indexnames)
632 appendStringInfoCharMacro(buf, ' ');
633 dump_quote_value(buf, (char *) lfirst(l));
635 appendStringInfoString(buf, ")\n");
639 JoinMethodHintDump(JoinMethodHint *hint, StringInfo buf)
643 appendStringInfo(buf, "%s(", hint->base.keyword);
644 dump_quote_value(buf, hint->relnames[0]);
645 for (i = 1; i < hint->nrels; i++)
647 appendStringInfoCharMacro(buf, ' ');
648 dump_quote_value(buf, hint->relnames[i]);
650 appendStringInfoString(buf, ")\n");
655 LeadingHintDump(LeadingHint *hint, StringInfo buf)
660 appendStringInfo(buf, "%s(", HINT_LEADING);
662 foreach(l, hint->relations)
667 appendStringInfoCharMacro(buf, ' ');
669 dump_quote_value(buf, (char *) lfirst(l));
672 appendStringInfoString(buf, ")\n");
676 SetHintDump(SetHint *hint, StringInfo buf)
678 appendStringInfo(buf, "%s(", HINT_SET);
679 dump_quote_value(buf, hint->name);
680 appendStringInfoCharMacro(buf, ' ');
681 dump_quote_value(buf, hint->value);
682 appendStringInfo(buf, ")\n");
686 all_hint_dump(PlanHint *hint, StringInfo buf, const char *title,
691 appendStringInfo(buf, "%s:\n", title);
692 for (i = 0; i < hint->nall_hints; i++)
694 if (hint->all_hints[i]->state != state)
697 hint->all_hints[i]->dump_func(hint->all_hints[i], buf);
702 PlanHintDump(PlanHint *hint)
708 elog(LOG, "pg_hint_plan:\nno hint");
712 initStringInfo(&buf);
714 appendStringInfoString(&buf, "pg_hint_plan:\n");
715 all_hint_dump(hint, &buf, "used hint", HINT_STATE_USED);
716 all_hint_dump(hint, &buf, "not used hint", HINT_STATE_NOTUSED);
717 all_hint_dump(hint, &buf, "duplication hint", HINT_STATE_DUPLICATION);
718 all_hint_dump(hint, &buf, "error hint", HINT_STATE_ERROR);
720 elog(LOG, "%s", buf.data);
730 RelnameCmp(const void *a, const void *b)
732 const char *relnamea = *((const char **) a);
733 const char *relnameb = *((const char **) b);
735 return strcmp(relnamea, relnameb);
739 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
741 return RelnameCmp(&a->relname, &b->relname);
745 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
749 if (a->nrels != b->nrels)
750 return a->nrels - b->nrels;
752 for (i = 0; i < a->nrels; i++)
755 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
763 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
769 SetHintCmp(const SetHint *a, const SetHint *b)
771 return strcmp(a->name, b->name);
775 AllHintCmp(const void *a, const void *b, bool order)
777 const Hint *hinta = *((const Hint **) a);
778 const Hint *hintb = *((const Hint **) b);
781 if (hinta->type != hintb->type)
782 return hinta->type - hintb->type;
784 if ((result = hinta->cmp_func(hinta, hintb)) != 0 || !order)
788 return hinta->hint_str - hintb->hint_str;
792 AllHintCmpIsOrder(const void *a, const void *b)
794 return AllHintCmp(a, b, true);
802 parse_keyword(const char *str, StringInfo buf)
806 while (!isspace(*str) && *str != '(' && *str != '\0')
807 appendStringInfoCharMacro(buf, *str++);
813 skip_opened_parenthesis(const char *str)
819 parse_ereport(str, ("Opening parenthesis is necessary."));
829 skip_closed_parenthesis(const char *str)
835 parse_ereport(str, ("Closing parenthesis is necessary."));
845 * 二重引用符で囲まれているかもしれないトークンを読み取り word 引数に palloc
846 * で確保したバッファに格納してそのポインタを返す。
848 * 正常にパースできた場合は残りの文字列の先頭位置を、異常があった場合は NULL を
850 * truncateがtrueの場合は、NAMEDATALENに切り詰める。
853 parse_quote_value(const char *str, char **word, char *value_type, bool truncate)
861 initStringInfo(&buf);
874 /* 二重引用符が閉じられていない場合はパース中断 */
878 parse_ereport(str, ("Unterminated quoted %s.", value_type));
883 * エスケープ対象のダブルクウォートをスキップする。
884 * もしブロックコメントの開始文字列や終了文字列もオブジェクト名とし
885 * て使用したい場合は、/ と * もエスケープ対象とすることで使用できる
886 * が、処理対象としていない。もしテーブル名にこれらの文字が含まれる
887 * 場合は、エイリアスを指定する必要がある。
896 else if (isspace(*str) || *str == ')' || *str == '"' || *str == '\0')
899 appendStringInfoCharMacro(&buf, *str++);
906 type = pstrdup(value_type);
907 type[0] = toupper(type[0]);
908 parse_ereport(str, ("%s is necessary.", type));
916 /* Truncate name if it's overlength */
918 truncate_identifier(buf.data, strlen(buf.data), true);
926 parse_hints(PlanHint *plan, Query *parse, const char *str)
931 initStringInfo(&buf);
934 const HintParser *parser;
936 /* in error message, we output the comment including the keyword. */
939 /* parse only the keyword of the hint. */
940 resetStringInfo(&buf);
941 str = parse_keyword(str, &buf);
943 for (parser = parsers; parser->keyword != NULL; parser++)
945 char *keyword = parser->keyword;
948 if (strcasecmp(buf.data, keyword) != 0)
951 hint = parser->create_func(head, keyword);
953 /* parser of each hint does parse in a parenthesis. */
954 if ((str = skip_opened_parenthesis(str)) == NULL ||
955 (str = hint->parser_func(hint, plan, parse, str)) == NULL ||
956 (str = skip_closed_parenthesis(str)) == NULL)
958 hint->delete_func(hint);
964 * 出来上がったヒント情報を追加。スロットが足りない場合は二倍に拡張
967 if (plan->nall_hints == 0)
969 plan->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
970 plan->all_hints = palloc(sizeof(Hint *) * plan->max_all_hints);
972 else if (plan->nall_hints == plan->max_all_hints)
974 plan->max_all_hints *= 2;
975 plan->all_hints = repalloc(plan->all_hints,
976 sizeof(Hint *) * plan->max_all_hints);
979 plan->all_hints[plan->nall_hints] = hint;
987 if (parser->keyword == NULL)
989 parse_ereport(head, ("Unrecognized hint keyword \"%s\".",
1000 * Do basic parsing of the query head comment.
1003 parse_head_comment(Query *parse)
1012 /* get client-supplied query string. */
1015 PreparedStatement *entry;
1017 entry = FetchPreparedStatement(stmt_name, true);
1018 p = entry->plansource->query_string;
1021 p = debug_query_string;
1026 /* extract query head comment. */
1027 len = strlen(HINT_START);
1029 if (strncmp(p, HINT_START, len))
1036 /* find hint end keyword. */
1037 if ((tail = strstr(p, HINT_END)) == NULL)
1039 parse_ereport(head, ("Unterminated block comment."));
1043 /* 入れ子にしたブロックコメントはサポートしない */
1044 if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1046 parse_ereport(head, ("Nested block comments are not supported."));
1052 head = palloc(len + 1);
1053 memcpy(head, p, len);
1057 plan = PlanHintCreate();
1058 plan->hint_str = head;
1060 /* parse each hint. */
1061 parse_hints(plan, parse, p);
1063 /* When nothing specified a hint, we free PlanHint and returns NULL. */
1064 if (plan->nall_hints == 0)
1066 PlanHintDelete(plan);
1070 /* パースしたヒントを並び替える */
1071 qsort(plan->all_hints, plan->nall_hints, sizeof(Hint *), AllHintCmpIsOrder);
1074 for (i = 0; i < plan->nall_hints; i++)
1076 Hint *hint = plan->all_hints[i];
1078 plan->num_hints[hint->type]++;
1080 if (i + 1 >= plan->nall_hints)
1083 if (AllHintCmp(plan->all_hints + i, plan->all_hints + i + 1, false)
1086 const char *HintTypeName[] = {"scan method", "join method",
1089 parse_ereport(plan->all_hints[i]->hint_str,
1090 ("Conflict %s hint.", HintTypeName[hint->type]));
1091 plan->all_hints[i]->state = HINT_STATE_DUPLICATION;
1095 plan->scan_hints = (ScanMethodHint **) plan->all_hints;
1096 plan->join_hints = (JoinMethodHint **) plan->all_hints +
1097 plan->num_hints[HINT_TYPE_SCAN_METHOD];
1098 plan->leading_hint = (LeadingHint *) plan->all_hints[
1099 plan->num_hints[HINT_TYPE_SCAN_METHOD] +
1100 plan->num_hints[HINT_TYPE_JOIN_METHOD] +
1101 plan->num_hints[HINT_TYPE_LEADING] - 1];
1102 plan->set_hints = (SetHint **) plan->all_hints +
1103 plan->num_hints[HINT_TYPE_SCAN_METHOD] +
1104 plan->num_hints[HINT_TYPE_JOIN_METHOD] +
1105 plan->num_hints[HINT_TYPE_LEADING];
1111 * スキャン方式ヒントのカッコ内をパースする
1114 ScanMethodHintParse(ScanMethodHint *hint, PlanHint *plan, Query *parse,
1117 const char *keyword = hint->base.keyword;
1120 * スキャン方式のヒントでリレーション名が読み取れない場合はヒント無効
1122 if ((str = parse_quote_value(str, &hint->relname, "relation name", true))
1129 * インデックスリストを受け付けるヒントであれば、インデックス参照をパース
1132 if (strcmp(keyword, HINT_INDEXSCAN) == 0 ||
1133 strcmp(keyword, HINT_BITMAPSCAN) == 0)
1135 while (*str != ')' && *str != '\0')
1139 str = parse_quote_value(str, &indexname, "index name", true);
1143 hint->indexnames = lappend(hint->indexnames, indexname);
1149 * ヒントごとに決まっている許容スキャン方式をビットマスクとして設定
1151 if (strcasecmp(keyword, HINT_SEQSCAN) == 0)
1152 hint->enforce_mask = ENABLE_SEQSCAN;
1153 else if (strcasecmp(keyword, HINT_INDEXSCAN) == 0)
1154 hint->enforce_mask = ENABLE_INDEXSCAN;
1155 else if (strcasecmp(keyword, HINT_BITMAPSCAN) == 0)
1156 hint->enforce_mask = ENABLE_BITMAPSCAN;
1157 else if (strcasecmp(keyword, HINT_TIDSCAN) == 0)
1158 hint->enforce_mask = ENABLE_TIDSCAN;
1159 else if (strcasecmp(keyword, HINT_NOSEQSCAN) == 0)
1160 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
1161 else if (strcasecmp(keyword, HINT_NOINDEXSCAN) == 0)
1162 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
1163 else if (strcasecmp(keyword, HINT_NOBITMAPSCAN) == 0)
1164 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
1165 else if (strcasecmp(keyword, HINT_NOTIDSCAN) == 0)
1166 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
1169 parse_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1177 JoinMethodHintParse(JoinMethodHint *hint, PlanHint *plan, Query *parse,
1181 const char *keyword = hint->base.keyword;
1185 hint->relnames = palloc(sizeof(char *));
1187 while ((str = parse_quote_value(str, &relname, "relation name", true))
1191 hint->relnames = repalloc(hint->relnames, sizeof(char *) * hint->nrels);
1192 hint->relnames[hint->nrels - 1] = relname;
1202 /* Join 対象のテーブルは最低でも2つ指定する必要がある */
1203 if (hint->nrels < 2)
1206 ("%s hint requires at least two relations.",
1207 hint->base.keyword));
1208 hint->base.state = HINT_STATE_ERROR;
1212 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
1214 if (strcasecmp(keyword, HINT_NESTLOOP) == 0)
1215 hint->enforce_mask = ENABLE_NESTLOOP;
1216 else if (strcasecmp(keyword, HINT_MERGEJOIN) == 0)
1217 hint->enforce_mask = ENABLE_MERGEJOIN;
1218 else if (strcasecmp(keyword, HINT_HASHJOIN) == 0)
1219 hint->enforce_mask = ENABLE_HASHJOIN;
1220 else if (strcasecmp(keyword, HINT_NONESTLOOP) == 0)
1221 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
1222 else if (strcasecmp(keyword, HINT_NOMERGEJOIN) == 0)
1223 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
1224 else if (strcasecmp(keyword, HINT_NOHASHJOIN) == 0)
1225 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
1228 parse_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1236 LeadingHintParse(LeadingHint *hint, PlanHint *plan, Query *parse,
1245 if ((str = parse_quote_value(str, &relname, "relation name", true))
1249 hint->relations = lappend(hint->relations, relname);
1254 /* テーブル指定が2つ未満の場合は、Leading ヒントはエラーとする */
1255 if (list_length(hint->relations) < 2)
1257 parse_ereport(hint->base.hint_str,
1258 ("%s hint requires at least two relations.",
1260 hint->base.state = HINT_STATE_ERROR;
1267 SetHintParse(SetHint *hint, PlanHint *plan, Query *parse, const char *str)
1269 if ((str = parse_quote_value(str, &hint->name, "parameter name", true))
1271 (str = parse_quote_value(str, &hint->value, "parameter value", false))
1279 * set GUC parameter functions
1283 set_config_option_wrapper(const char *name, const char *value,
1284 GucContext context, GucSource source,
1285 GucAction action, bool changeVal, int elevel)
1288 MemoryContext ccxt = CurrentMemoryContext;
1292 result = set_config_option(name, value, context, source,
1299 /* Save error info */
1300 MemoryContextSwitchTo(ccxt);
1301 errdata = CopyErrorData();
1304 ereport(elevel, (errcode(errdata->sqlerrcode),
1305 errmsg("%s", errdata->message),
1306 errdata->detail ? errdetail("%s", errdata->detail) : 0,
1307 errdata->hint ? errhint("%s", errdata->hint) : 0));
1308 FreeErrorData(errdata);
1316 set_config_options(SetHint **options, int noptions, GucContext context)
1321 save_nestlevel = NewGUCNestLevel();
1323 for (i = 0; i < noptions; i++)
1325 SetHint *hint = options[i];
1328 if (!hint_state_enabled(hint))
1331 result = set_config_option_wrapper(hint->name, hint->value, context,
1332 PGC_S_SESSION, GUC_ACTION_SAVE, true,
1333 pg_hint_plan_parse_messages);
1335 hint->base.state = HINT_STATE_USED;
1337 hint->base.state = HINT_STATE_ERROR;
1340 return save_nestlevel;
1343 #define SET_CONFIG_OPTION(name, type_bits) \
1344 set_config_option_wrapper((name), \
1345 (mask & (type_bits)) ? "true" : "false", \
1346 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
1349 set_scan_config_options(unsigned char enforce_mask, GucContext context)
1353 if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
1354 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
1356 mask = enforce_mask;
1358 mask = enforce_mask & current_hint->init_scan_mask;
1360 SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
1361 SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
1362 SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
1363 SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
1367 set_join_config_options(unsigned char enforce_mask, GucContext context)
1371 if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
1372 enforce_mask == ENABLE_HASHJOIN)
1373 mask = enforce_mask;
1375 mask = enforce_mask & current_hint->init_join_mask;
1377 SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
1378 SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
1379 SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
1383 * pg_hint_plan hook functions
1387 pg_hint_plan_ProcessUtility(Node *parsetree, const char *queryString,
1388 ParamListInfo params, bool isTopLevel,
1389 DestReceiver *dest, char *completionTag)
1393 if (!pg_hint_plan_enable)
1395 if (prev_ProcessUtility)
1396 (*prev_ProcessUtility) (parsetree, queryString, params,
1397 isTopLevel, dest, completionTag);
1399 standard_ProcessUtility(parsetree, queryString, params,
1400 isTopLevel, dest, completionTag);
1406 if (IsA(node, ExplainStmt))
1409 * EXPLAIN対象のクエリのパースツリーを取得する
1414 stmt = (ExplainStmt *) node;
1416 Assert(IsA(stmt->query, Query));
1417 query = (Query *) stmt->query;
1419 if (query->commandType == CMD_UTILITY && query->utilityStmt != NULL)
1420 node = query->utilityStmt;
1424 * EXECUTEコマンドならば、PREPARE時に指定されたクエリ文字列を取得し、ヒント
1427 if (IsA(node, ExecuteStmt))
1431 stmt = (ExecuteStmt *) node;
1432 stmt_name = stmt->name;
1439 if (prev_ProcessUtility)
1440 (*prev_ProcessUtility) (parsetree, queryString, params,
1441 isTopLevel, dest, completionTag);
1443 standard_ProcessUtility(parsetree, queryString, params,
1444 isTopLevel, dest, completionTag);
1458 if (prev_ProcessUtility)
1459 (*prev_ProcessUtility) (parsetree, queryString, params,
1460 isTopLevel, dest, completionTag);
1462 standard_ProcessUtility(parsetree, queryString, params,
1463 isTopLevel, dest, completionTag);
1467 * ヒント用スタック構造にヒントをプッシュする。なお、List構造体でヒント用スタッ
1468 * ク構造を実装していて、リストの先頭がスタックの一番上に該当する。
1471 push_hint(PlanHint *plan)
1473 /* 新しいヒントをスタックに積む。 */
1474 PlanHintStack = lcons(plan, PlanHintStack);
1477 * 先ほどスタックに積んだヒントを現在のヒントとしてcurrent_hintに格納する。
1479 current_hint = plan;
1485 /* ヒントのスタックが空の場合はエラーを返す */
1486 if(PlanHintStack == NIL)
1487 elog(ERROR, "hint stack is empty");
1490 * ヒントのスタックから一番上のものを取り出して解放する。 current_hint
1491 * 常に最上段ヒントを指すが、スタックが空の場合はNULLにする。
1493 PlanHintStack = list_delete_first(PlanHintStack);
1494 PlanHintDelete(current_hint);
1495 if(PlanHintStack == NIL)
1496 current_hint = NULL;
1498 current_hint = (PlanHint *) lfirst(list_head(PlanHintStack));
1501 static PlannedStmt *
1502 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
1505 PlannedStmt *result;
1509 * pg_hint_planが無効である場合は通常のparser処理をおこなう。
1510 * 他のフック関数で実行されるhint処理をスキップするために、current_hint 変数
1513 if (!pg_hint_plan_enable)
1515 current_hint = NULL;
1518 return (*prev_planner) (parse, cursorOptions, boundParams);
1520 return standard_planner(parse, cursorOptions, boundParams);
1524 plan = parse_head_comment(parse);
1527 * hintが指定されない、または空のhintを指定された場合は通常のparser処理をお
1529 * 他のフック関数で実行されるhint処理をスキップするために、current_hint 変数
1534 current_hint = NULL;
1537 return (*prev_planner) (parse, cursorOptions, boundParams);
1539 return standard_planner(parse, cursorOptions, boundParams);
1542 /* 現在のヒントをスタックに積む。 */
1545 /* Set hint で指定されたGUCパラメータを設定する */
1546 save_nestlevel = set_config_options(current_hint->set_hints,
1547 current_hint->num_hints[HINT_TYPE_SET],
1548 current_hint->context);
1551 current_hint->init_scan_mask |= ENABLE_SEQSCAN;
1552 if (enable_indexscan)
1553 current_hint->init_scan_mask |= ENABLE_INDEXSCAN;
1554 if (enable_bitmapscan)
1555 current_hint->init_scan_mask |= ENABLE_BITMAPSCAN;
1557 current_hint->init_scan_mask |= ENABLE_TIDSCAN;
1558 if (enable_nestloop)
1559 current_hint->init_join_mask |= ENABLE_NESTLOOP;
1560 if (enable_mergejoin)
1561 current_hint->init_join_mask |= ENABLE_MERGEJOIN;
1562 if (enable_hashjoin)
1563 current_hint->init_join_mask |= ENABLE_HASHJOIN;
1566 * プラン作成中にエラーとなった場合、GUCパラメータと current_hintを
1567 * pg_hint_plan_planner 関数の実行前の状態に戻す。
1572 result = (*prev_planner) (parse, cursorOptions, boundParams);
1574 result = standard_planner(parse, cursorOptions, boundParams);
1579 * プランナ起動前の状態に戻すため、GUCパラメータを復元し、ヒント情報を
1582 AtEOXact_GUC(true, save_nestlevel);
1589 * Print hint if debugging.
1591 if (pg_hint_plan_debug_print)
1592 PlanHintDump(current_hint);
1595 * プランナ起動前の状態に戻すため、GUCパラメータを復元し、ヒント情報を一つ
1598 AtEOXact_GUC(true, save_nestlevel);
1605 * aliasnameと一致するSCANヒントを探す
1607 static ScanMethodHint *
1608 find_scan_hint(PlannerInfo *root, RelOptInfo *rel)
1614 * RELOPT_BASEREL でなければ、scan method ヒントが適用しない。
1615 * 子テーブルの場合はRELOPT_OTHER_MEMBER_RELとなるが、サポート対象外とする。
1616 * また、通常のリレーション以外は、スキャン方式を選択できない。
1618 if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
1621 rte = root->simple_rte_array[rel->relid];
1623 /* 外部表はスキャン方式が選択できない。 */
1624 if (rte->relkind == RELKIND_FOREIGN_TABLE)
1628 * スキャン方式のヒントのリストから、検索対象のリレーションと名称が一致する
1631 for (i = 0; i < current_hint->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
1633 ScanMethodHint *hint = current_hint->scan_hints[i];
1635 /* すでに無効となっているヒントは検索対象にしない。 */
1636 if (!hint_state_enabled(hint))
1639 if (RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
1647 delete_indexes(ScanMethodHint *hint, RelOptInfo *rel)
1654 * We delete all the IndexOptInfo list and prevent you from being usable by
1657 if (hint->enforce_mask == ENABLE_SEQSCAN ||
1658 hint->enforce_mask == ENABLE_TIDSCAN)
1660 list_free_deep(rel->indexlist);
1661 rel->indexlist = NIL;
1662 hint->base.state = HINT_STATE_USED;
1668 * When a list of indexes is not specified, we just use all indexes.
1670 if (hint->indexnames == NIL)
1674 * Leaving only an specified index, we delete it from a IndexOptInfo list
1678 for (cell = list_head(rel->indexlist); cell; cell = next)
1680 IndexOptInfo *info = (IndexOptInfo *) lfirst(cell);
1681 char *indexname = get_rel_name(info->indexoid);
1683 bool use_index = false;
1687 foreach(l, hint->indexnames)
1689 if (RelnameCmp(&indexname, &lfirst(l)) == 0)
1697 rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
1706 pg_hint_plan_get_relation_info(PlannerInfo *root, Oid relationObjectId,
1707 bool inhparent, RelOptInfo *rel)
1709 ScanMethodHint *hint;
1711 if (prev_get_relation_info)
1712 (*prev_get_relation_info) (root, relationObjectId, inhparent, rel);
1714 /* 有効なヒントが指定されなかった場合は処理をスキップする。 */
1720 /* store does relids of parent table. */
1721 current_hint->parent_relid = rel->relid;
1723 else if (current_hint->parent_relid != 0)
1726 * We use the same GUC parameter if this table is the child table of a
1727 * table called pg_hint_plan_get_relation_info just before that.
1731 /* append_rel_list contains all append rels; ignore others */
1732 foreach(l, root->append_rel_list)
1734 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1736 /* This rel is child table. */
1737 if (appinfo->parent_relid == current_hint->parent_relid &&
1738 appinfo->child_relid == rel->relid)
1740 if (current_hint->parent_hint)
1741 delete_indexes(current_hint->parent_hint, rel);
1747 /* This rel is not inherit table. */
1748 current_hint->parent_relid = 0;
1749 current_hint->parent_hint = NULL;
1752 /* scan hint が指定されない場合は、GUCパラメータをリセットする。 */
1753 if ((hint = find_scan_hint(root, rel)) == NULL)
1755 set_scan_config_options(current_hint->init_scan_mask,
1756 current_hint->context);
1759 set_scan_config_options(hint->enforce_mask, current_hint->context);
1760 hint->base.state = HINT_STATE_USED;
1762 current_hint->parent_hint = hint;
1764 delete_indexes(hint, rel);
1768 * aliasnameがクエリ中に指定した別名と一致する場合は、そのインデックスを返し、一
1770 * aliasnameがクエリ中に複数回指定された場合は、-1を返す。
1773 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
1779 for (i = 1; i < root->simple_rel_array_size; i++)
1783 if (root->simple_rel_array[i] == NULL)
1786 Assert(i == root->simple_rel_array[i]->relid);
1788 if (RelnameCmp(&aliasname, &root->simple_rte_array[i]->eref->aliasname)
1792 foreach(l, initial_rels)
1794 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
1796 if (rel->reloptkind == RELOPT_BASEREL)
1798 if (rel->relid != i)
1803 Assert(rel->reloptkind == RELOPT_JOINREL);
1805 if (!bms_is_member(i, rel->relids))
1812 ("Relation name \"%s\" is ambiguous.",
1827 * relidビットマスクと一致するヒントを探す
1829 static JoinMethodHint *
1830 find_join_hint(Relids joinrelids)
1835 join_hint = current_hint->join_hint_level[bms_num_members(joinrelids)];
1837 foreach(l, join_hint)
1839 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
1841 if (bms_equal(joinrelids, hint->joinrelids))
1849 * 結合方式のヒントを使用しやすい構造に変換する。
1852 transform_join_hints(PlanHint *plan, PlannerInfo *root, int nbaserel,
1853 List *initial_rels, JoinMethodHint **join_method_hints)
1862 for (i = 0; i < plan->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
1864 JoinMethodHint *hint = plan->join_hints[i];
1867 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
1870 bms_free(hint->joinrelids);
1871 hint->joinrelids = NULL;
1873 for (j = 0; j < hint->nrels; j++)
1875 char *relname = hint->relnames[j];
1877 relid = find_relid_aliasname(root, relname, initial_rels,
1878 hint->base.hint_str);
1881 hint->base.state = HINT_STATE_ERROR;
1886 if (bms_is_member(relid, hint->joinrelids))
1888 parse_ereport(hint->base.hint_str,
1889 ("Relation name \"%s\" is duplicated.", relname));
1890 hint->base.state = HINT_STATE_ERROR;
1894 hint->joinrelids = bms_add_member(hint->joinrelids, relid);
1897 if (relid <= 0 || hint->base.state == HINT_STATE_ERROR)
1900 plan->join_hint_level[hint->nrels] =
1901 lappend(plan->join_hint_level[hint->nrels], hint);
1905 * 有効なLeading ヒントが指定されている場合は、結合順にあわせて
1906 * join method hint のフォーマットに変換する。
1908 if (plan->num_hints[HINT_TYPE_LEADING] == 0)
1911 lhint = plan->leading_hint;
1912 if (!hint_state_enabled(lhint))
1915 /* Leading hint は、全ての join 方式が有効な hint として登録する */
1918 foreach(l, lhint->relations)
1920 char *relname = (char *)lfirst(l);
1921 JoinMethodHint *hint;
1924 find_relid_aliasname(root, relname, initial_rels, plan->hint_str);
1928 bms_free(joinrelids);
1935 if (bms_is_member(relid, joinrelids))
1937 parse_ereport(lhint->base.hint_str,
1938 ("Relation name \"%s\" is duplicated.", relname));
1939 lhint->base.state = HINT_STATE_ERROR;
1940 bms_free(joinrelids);
1944 joinrelids = bms_add_member(joinrelids, relid);
1950 hint = find_join_hint(joinrelids);
1954 * Here relnames is not set, since Relids bitmap is sufficient to
1955 * control paths of this query afterwards.
1957 hint = (JoinMethodHint *) JoinMethodHintCreate(lhint->base.hint_str,
1959 hint->base.state = HINT_STATE_USED;
1960 hint->nrels = njoinrels;
1961 hint->enforce_mask = ENABLE_ALL_JOIN;
1962 hint->joinrelids = bms_copy(joinrelids);
1965 join_method_hints[njoinrels] = hint;
1967 if (njoinrels >= nbaserel)
1971 bms_free(joinrelids);
1976 for (i = 2; i <= njoinrels; i++)
1978 /* Leading で指定した組み合わせ以外の join hint を削除する */
1979 list_free(plan->join_hint_level[i]);
1981 plan->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
1984 if (hint_state_enabled(lhint))
1985 set_join_config_options(DISABLE_ALL_JOIN, current_hint->context);
1987 lhint->base.state = HINT_STATE_USED;
1992 * set_plain_rel_pathlist
1993 * Build access paths for a plain relation (no subquery, no inheritance)
1995 * This function was copied and edited from set_plain_rel_pathlist() in
1996 * src/backend/optimizer/path/allpaths.c
1999 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2001 /* Consider sequential scan */
2002 add_path(rel, create_seqscan_path(root, rel));
2004 /* Consider index scans */
2005 create_index_paths(root, rel);
2007 /* Consider TID scans */
2008 create_tidscan_paths(root, rel);
2010 /* Now find the cheapest of the paths for this rel */
2015 rebuild_scan_path(PlanHint *plan, PlannerInfo *root, int level,
2020 foreach(l, initial_rels)
2022 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
2024 ScanMethodHint *hint;
2027 * スキャン方式が選択できるリレーションのみ、スキャンパスを再生成する。
2029 if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
2032 rte = root->simple_rte_array[rel->relid];
2034 /* 外部表はスキャン方式が選択できない。 */
2035 if (rte->relkind == RELKIND_FOREIGN_TABLE)
2039 * scan method hint が指定されていなければ、初期値のGUCパラメータでscan
2042 if ((hint = find_scan_hint(root, rel)) == NULL)
2043 set_scan_config_options(plan->init_scan_mask, plan->context);
2046 set_scan_config_options(hint->enforce_mask, plan->context);
2047 hint->base.state = HINT_STATE_USED;
2050 list_free_deep(rel->pathlist);
2051 rel->pathlist = NIL;
2054 /* It's an "append relation", process accordingly */
2055 set_append_rel_pathlist(root, rel, rel->relid, rte);
2059 set_plain_rel_pathlist(root, rel, rte);
2064 * Restore the GUC variables we set above.
2066 set_scan_config_options(plan->init_scan_mask, plan->context);
2070 * make_join_rel() をラップする関数
2072 * ヒントにしたがって、enabele_* パラメータを変更した上で、make_join_rel()を
2076 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
2079 JoinMethodHint *hint;
2083 joinrelids = bms_union(rel1->relids, rel2->relids);
2084 hint = find_join_hint(joinrelids);
2085 bms_free(joinrelids);
2088 return pg_hint_plan_make_join_rel(root, rel1, rel2);
2090 save_nestlevel = NewGUCNestLevel();
2092 set_join_config_options(hint->enforce_mask, current_hint->context);
2094 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
2095 hint->base.state = HINT_STATE_USED;
2098 * Restore the GUC variables we set above.
2100 AtEOXact_GUC(true, save_nestlevel);
2106 get_num_baserels(List *initial_rels)
2111 foreach(l, initial_rels)
2113 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
2115 if (rel->reloptkind == RELOPT_BASEREL)
2117 else if (rel->reloptkind ==RELOPT_JOINREL)
2118 nbaserel+= bms_num_members(rel->relids);
2121 /* other values not expected here */
2122 elog(ERROR, "Unrecognized reloptkind type: %d", rel->reloptkind);
2130 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
2133 JoinMethodHint **join_method_hints;
2139 * pg_hint_planが無効、または有効なヒントが1つも指定されなかった場合は、標準
2144 if (prev_join_search)
2145 return (*prev_join_search) (root, levels_needed, initial_rels);
2146 else if (enable_geqo && levels_needed >= geqo_threshold)
2147 return geqo(root, levels_needed, initial_rels);
2149 return standard_join_search(root, levels_needed, initial_rels);
2152 /* We apply scan method hint rebuild scan path. */
2153 rebuild_scan_path(current_hint, root, levels_needed, initial_rels);
2156 * GEQOを使用する条件を満たした場合は、GEQOを用いた結合方式の検索を行う。
2157 * このとき、スキャン方式のヒントとSetヒントのみが有効になり、結合方式や結合
2158 * 順序はヒント句は無効になりGEQOのアルゴリズムで決定される。
2160 if (enable_geqo && levels_needed >= geqo_threshold)
2161 return geqo(root, levels_needed, initial_rels);
2163 nbaserel = get_num_baserels(initial_rels);
2164 current_hint->join_hint_level = palloc0(sizeof(List *) * (nbaserel + 1));
2165 join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
2167 transform_join_hints(current_hint, root, nbaserel, initial_rels,
2170 rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
2172 for (i = 2; i <= nbaserel; i++)
2174 list_free(current_hint->join_hint_level[i]);
2176 /* free Leading hint only */
2177 if (join_method_hints[i] != NULL &&
2178 join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
2179 JoinMethodHintDelete(join_method_hints[i]);
2181 pfree(current_hint->join_hint_level);
2182 pfree(join_method_hints);
2184 if (current_hint->num_hints[HINT_TYPE_LEADING] > 0 &&
2185 hint_state_enabled(current_hint->leading_hint))
2186 set_join_config_options(current_hint->init_join_mask, current_hint->context);
2193 * Build access paths for a base relation
2195 * This function was copied and edited from set_rel_pathlist() in
2196 * src/backend/optimizer/path/allpaths.c
2199 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
2200 Index rti, RangeTblEntry *rte)
2204 /* It's an "append relation", process accordingly */
2205 set_append_rel_pathlist(root, rel, rti, rte);
2209 if (rel->rtekind == RTE_RELATION)
2211 if (rte->relkind == RELKIND_RELATION)
2213 /* Plain relation */
2214 set_plain_rel_pathlist(root, rel, rte);
2217 elog(ERROR, "Unexpected relkind: %c", rte->relkind);
2220 elog(ERROR, "Unexpected rtekind: %d", (int) rel->rtekind);
2224 #define standard_join_search pg_hint_plan_standard_join_search
2225 #define join_search_one_level pg_hint_plan_join_search_one_level
2226 #define make_join_rel make_join_rel_wrapper
2229 #undef make_join_rel
2230 #define make_join_rel pg_hint_plan_make_join_rel
2231 #define add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype, sjinfo, restrictlist) \
2233 ScanMethodHint *hint = NULL; \
2234 if ((hint = find_scan_hint((root), (innerrel))) != NULL) \
2236 set_scan_config_options(hint->enforce_mask, current_hint->context); \
2237 hint->base.state = HINT_STATE_USED; \
2239 add_paths_to_joinrel((root), (joinrel), (outerrel), (innerrel), (jointype), (sjinfo), (restrictlist)); \
2241 set_scan_config_options(current_hint->init_scan_mask, current_hint->context); \
2243 #include "make_join_rel.c"