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 "miscadmin.h"
14 #include "nodes/nodeFuncs.h"
15 #include "optimizer/clauses.h"
16 #include "optimizer/cost.h"
17 #include "optimizer/geqo.h"
18 #include "optimizer/joininfo.h"
19 #include "optimizer/pathnode.h"
20 #include "optimizer/paths.h"
21 #include "optimizer/plancat.h"
22 #include "optimizer/planner.h"
23 #include "optimizer/prep.h"
24 #include "optimizer/restrictinfo.h"
25 #include "tcop/utility.h"
26 #include "utils/lsyscache.h"
27 #include "utils/memutils.h"
29 #ifdef PG_MODULE_MAGIC
33 #if PG_VERSION_NUM < 90100
34 #error unsupported PostgreSQL version
37 #define BLOCK_COMMENT_START "/*"
38 #define BLOCK_COMMENT_END "*/"
39 #define HINT_COMMENT_KEYWORD "+"
40 #define HINT_START BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
41 #define HINT_END BLOCK_COMMENT_END
44 #define HINT_SEQSCAN "SeqScan"
45 #define HINT_INDEXSCAN "IndexScan"
46 #define HINT_BITMAPSCAN "BitmapScan"
47 #define HINT_TIDSCAN "TidScan"
48 #define HINT_NOSEQSCAN "NoSeqScan"
49 #define HINT_NOINDEXSCAN "NoIndexScan"
50 #define HINT_NOBITMAPSCAN "NoBitmapScan"
51 #define HINT_NOTIDSCAN "NoTidScan"
52 #if PG_VERSION_NUM >= 90200
53 #define HINT_INDEXONLYSCAN "IndexonlyScan"
54 #define HINT_NOINDEXONLYSCAN "NoIndexonlyScan"
56 #define HINT_NESTLOOP "NestLoop"
57 #define HINT_MERGEJOIN "MergeJoin"
58 #define HINT_HASHJOIN "HashJoin"
59 #define HINT_NONESTLOOP "NoNestLoop"
60 #define HINT_NOMERGEJOIN "NoMergeJoin"
61 #define HINT_NOHASHJOIN "NoHashJoin"
62 #define HINT_LEADING "Leading"
63 #define HINT_SET "Set"
66 #define HINT_ARRAY_DEFAULT_INITSIZE 8
68 #define parse_ereport(str, detail) \
69 ereport(pg_hint_plan_parse_messages, \
70 (errmsg("hint syntax error at or near \"%s\"", (str)), \
73 #define skip_space(str) \
74 while (isspace(*str)) \
79 ENABLE_SEQSCAN = 0x01,
80 ENABLE_INDEXSCAN = 0x02,
81 ENABLE_BITMAPSCAN = 0x04,
82 ENABLE_TIDSCAN = 0x08,
83 #if PG_VERSION_NUM >= 90200
84 ENABLE_INDEXONLYSCAN = 0x10
90 ENABLE_NESTLOOP = 0x01,
91 ENABLE_MERGEJOIN = 0x02,
92 ENABLE_HASHJOIN = 0x04
95 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | ENABLE_BITMAPSCAN \
97 #if PG_VERSION_NUM >= 90200
98 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | ENABLE_BITMAPSCAN \
99 | ENABLE_TIDSCAN | ENABLE_INDEXONLYSCAN)
101 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
102 #define DISABLE_ALL_SCAN 0
103 #define DISABLE_ALL_JOIN 0
105 typedef struct Hint Hint;
106 typedef struct PlanHint PlanHint;
108 typedef Hint *(*HintCreateFunction) (const char *hint_str,
109 const char *keyword);
110 typedef void (*HintDeleteFunction) (Hint *hint);
111 typedef void (*HintDumpFunction) (Hint *hint, StringInfo buf);
112 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
113 typedef const char *(*HintParseFunction) (Hint *hint, PlanHint *plan,
114 Query *parse, const char *str);
117 #define NUM_HINT_TYPE 4
118 typedef enum HintType
120 HINT_TYPE_SCAN_METHOD,
121 HINT_TYPE_JOIN_METHOD,
127 typedef enum HintStatus
129 HINT_STATE_NOTUSED = 0, /* specified relation not used in query */
130 HINT_STATE_USED, /* hint is used */
131 HINT_STATE_DUPLICATION, /* specified hint duplication */
132 HINT_STATE_ERROR /* execute error (parse error does not include
136 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
137 (hint)->base.state == HINT_STATE_USED)
139 /* common data for all hints. */
142 const char *hint_str; /* must not do pfree */
143 const char *keyword; /* must not do pfree */
146 HintDeleteFunction delete_func;
147 HintDumpFunction dump_func;
148 HintCmpFunction cmp_func;
149 HintParseFunction parser_func;
152 /* scan method hints */
153 typedef struct ScanMethodHint
158 unsigned char enforce_mask;
161 /* join method hints */
162 typedef struct JoinMethodHint
167 unsigned char enforce_mask;
171 /* join order hints */
172 typedef struct LeadingHint
175 List *relations; /* relation names specified in Leading hint */
178 /* change a run-time parameter hints */
179 typedef struct SetHint
182 char *name; /* name of variable */
187 * Describes a context of hint processing.
191 char *hint_str; /* original hint string */
194 int nall_hints; /* # of valid all hints */
195 int max_all_hints; /* # of slots for all hints */
196 Hint **all_hints; /* parsed all hints */
198 /* # of each hints */
199 int num_hints[NUM_HINT_TYPE];
201 /* for scan method hints */
202 ScanMethodHint **scan_hints; /* parsed scan hints */
203 int init_scan_mask; /* initial value scan parameter */
204 Index parent_relid; /* inherit parent table relid */
205 ScanMethodHint *parent_hint; /* inherit parent table scan hint */
207 /* for join method hints */
208 JoinMethodHint **join_hints; /* parsed join hints */
209 int init_join_mask; /* initial value join parameter */
210 List **join_hint_level;
212 /* for Leading hint */
213 LeadingHint *leading_hint; /* parsed last specified Leading hint */
216 SetHint **set_hints; /* parsed Set hints */
217 GucContext context; /* which GUC parameters can we set? */
221 * Describes a hint parser module which is bound with particular hint keyword.
223 typedef struct HintParser
226 HintCreateFunction create_func;
229 /* Module callbacks */
233 static void pg_hint_plan_ProcessUtility(Node *parsetree,
234 const char *queryString,
235 ParamListInfo params, bool isTopLevel,
237 char *completionTag);
238 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
239 ParamListInfo boundParams);
240 static void pg_hint_plan_get_relation_info(PlannerInfo *root,
241 Oid relationObjectId,
242 bool inhparent, RelOptInfo *rel);
243 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
247 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword);
248 static void ScanMethodHintDelete(ScanMethodHint *hint);
249 static void ScanMethodHintDump(ScanMethodHint *hint, StringInfo buf);
250 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
251 static const char *ScanMethodHintParse(ScanMethodHint *hint, PlanHint *plan,
252 Query *parse, const char *str);
253 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword);
254 static void JoinMethodHintDelete(JoinMethodHint *hint);
255 static void JoinMethodHintDump(JoinMethodHint *hint, StringInfo buf);
256 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
257 static const char *JoinMethodHintParse(JoinMethodHint *hint, PlanHint *plan,
258 Query *parse, const char *str);
259 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword);
260 static void LeadingHintDelete(LeadingHint *hint);
261 static void LeadingHintDump(LeadingHint *hint, StringInfo buf);
262 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
263 static const char *LeadingHintParse(LeadingHint *hint, PlanHint *plan,
264 Query *parse, const char *str);
265 static Hint *SetHintCreate(const char *hint_str, const char *keyword);
266 static void SetHintDelete(SetHint *hint);
267 static void SetHintDump(SetHint *hint, StringInfo buf);
268 static int SetHintCmp(const SetHint *a, const SetHint *b);
269 static const char *SetHintParse(SetHint *hint, PlanHint *plan, Query *parse,
272 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
275 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
276 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
277 ListCell *other_rels);
278 static void make_rels_by_clauseless_joins(PlannerInfo *root,
280 ListCell *other_rels);
281 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
282 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
283 Index rti, RangeTblEntry *rte);
284 static List *accumulate_append_subpath(List *subpaths, Path *path);
285 static void set_dummy_rel_pathlist(RelOptInfo *rel);
286 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
291 static bool pg_hint_plan_enable = true;
292 static bool pg_hint_plan_debug_print = false;
293 static int pg_hint_plan_parse_messages = INFO;
295 static const struct config_enum_entry parse_messages_level_options[] = {
296 {"debug", DEBUG2, true},
297 {"debug5", DEBUG5, false},
298 {"debug4", DEBUG4, false},
299 {"debug3", DEBUG3, false},
300 {"debug2", DEBUG2, false},
301 {"debug1", DEBUG1, false},
303 {"info", INFO, false},
304 {"notice", NOTICE, false},
305 {"warning", WARNING, false},
306 {"error", ERROR, false},
308 * {"fatal", FATAL, true},
309 * {"panic", PANIC, true},
314 /* Saved hook values in case of unload */
315 static ProcessUtility_hook_type prev_ProcessUtility = NULL;
316 static planner_hook_type prev_planner = NULL;
317 static get_relation_info_hook_type prev_get_relation_info = NULL;
318 static join_search_hook_type prev_join_search = NULL;
320 /* フック関数をまたがって使用する情報を管理する */
321 static PlanHint *global = NULL;
324 * EXECUTEコマンド実行時に、ステートメント名を格納する。
325 * その他のコマンドの場合は、NULLに設定する。
327 static char *stmt_name = NULL;
329 static const HintParser parsers[] = {
330 {HINT_SEQSCAN, ScanMethodHintCreate},
331 {HINT_INDEXSCAN, ScanMethodHintCreate},
332 {HINT_BITMAPSCAN, ScanMethodHintCreate},
333 {HINT_TIDSCAN, ScanMethodHintCreate},
334 {HINT_NOSEQSCAN, ScanMethodHintCreate},
335 {HINT_NOINDEXSCAN, ScanMethodHintCreate},
336 {HINT_NOBITMAPSCAN, ScanMethodHintCreate},
337 {HINT_NOTIDSCAN, ScanMethodHintCreate},
338 #if PG_VERSION_NUM >= 90200
339 {HINT_INDEXONLYSCAN, ScanMethodHintCreate},
340 {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate},
342 {HINT_NESTLOOP, JoinMethodHintCreate},
343 {HINT_MERGEJOIN, JoinMethodHintCreate},
344 {HINT_HASHJOIN, JoinMethodHintCreate},
345 {HINT_NONESTLOOP, JoinMethodHintCreate},
346 {HINT_NOMERGEJOIN, JoinMethodHintCreate},
347 {HINT_NOHASHJOIN, JoinMethodHintCreate},
348 {HINT_LEADING, LeadingHintCreate},
349 {HINT_SET, SetHintCreate},
354 * Module load callbacks
359 /* Define custom GUC variables. */
360 DefineCustomBoolVariable("pg_hint_plan.enable",
361 "Instructions or hints to the planner using block comments.",
363 &pg_hint_plan_enable,
371 DefineCustomBoolVariable("pg_hint_plan.debug_print",
372 "Logs each query's parse results of the hint.",
374 &pg_hint_plan_debug_print,
382 DefineCustomEnumVariable("pg_hint_plan.parse_messages",
383 "Messege level of the parse error.",
385 &pg_hint_plan_parse_messages,
387 parse_messages_level_options,
395 prev_ProcessUtility = ProcessUtility_hook;
396 ProcessUtility_hook = pg_hint_plan_ProcessUtility;
397 prev_planner = planner_hook;
398 planner_hook = pg_hint_plan_planner;
399 prev_get_relation_info = get_relation_info_hook;
400 get_relation_info_hook = pg_hint_plan_get_relation_info;
401 prev_join_search = join_search_hook;
402 join_search_hook = pg_hint_plan_join_search;
406 * Module unload callback
412 /* Uninstall hooks. */
413 ProcessUtility_hook = prev_ProcessUtility;
414 planner_hook = prev_planner;
415 get_relation_info_hook = prev_get_relation_info;
416 join_search_hook = prev_join_search;
420 * create and delete functions the hint object
424 ScanMethodHintCreate(const char *hint_str, const char *keyword)
426 ScanMethodHint *hint;
428 hint = palloc(sizeof(ScanMethodHint));
429 hint->base.hint_str = hint_str;
430 hint->base.keyword = keyword;
431 hint->base.type = HINT_TYPE_SCAN_METHOD;
432 hint->base.state = HINT_STATE_NOTUSED;
433 hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
434 hint->base.dump_func = (HintDumpFunction) ScanMethodHintDump;
435 hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
436 hint->base.parser_func = (HintParseFunction) ScanMethodHintParse;
437 hint->relname = NULL;
438 hint->indexnames = NIL;
439 hint->enforce_mask = 0;
441 return (Hint *) hint;
445 ScanMethodHintDelete(ScanMethodHint *hint)
451 pfree(hint->relname);
452 list_free_deep(hint->indexnames);
457 JoinMethodHintCreate(const char *hint_str, const char *keyword)
459 JoinMethodHint *hint;
461 hint = palloc(sizeof(JoinMethodHint));
462 hint->base.hint_str = hint_str;
463 hint->base.keyword = keyword;
464 hint->base.type = HINT_TYPE_JOIN_METHOD;
465 hint->base.state = HINT_STATE_NOTUSED;
466 hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
467 hint->base.dump_func = (HintDumpFunction) JoinMethodHintDump;
468 hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
469 hint->base.parser_func = (HintParseFunction) JoinMethodHintParse;
471 hint->relnames = NULL;
472 hint->enforce_mask = 0;
473 hint->joinrelids = NULL;
475 return (Hint *) hint;
479 JoinMethodHintDelete(JoinMethodHint *hint)
488 for (i = 0; i < hint->nrels; i++)
489 pfree(hint->relnames[i]);
490 pfree(hint->relnames);
492 bms_free(hint->joinrelids);
497 LeadingHintCreate(const char *hint_str, const char *keyword)
501 hint = palloc(sizeof(LeadingHint));
502 hint->base.hint_str = hint_str;
503 hint->base.keyword = keyword;
504 hint->base.type = HINT_TYPE_LEADING;
505 hint->base.state = HINT_STATE_NOTUSED;
506 hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
507 hint->base.dump_func = (HintDumpFunction) LeadingHintDump;
508 hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
509 hint->base.parser_func = (HintParseFunction) LeadingHintParse;
510 hint->relations = NIL;
512 return (Hint *) hint;
516 LeadingHintDelete(LeadingHint *hint)
521 list_free_deep(hint->relations);
526 SetHintCreate(const char *hint_str, const char *keyword)
530 hint = palloc(sizeof(SetHint));
531 hint->base.hint_str = hint_str;
532 hint->base.keyword = keyword;
533 hint->base.type = HINT_TYPE_SET;
534 hint->base.state = HINT_STATE_NOTUSED;
535 hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
536 hint->base.dump_func = (HintDumpFunction) SetHintDump;
537 hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
538 hint->base.parser_func = (HintParseFunction) SetHintParse;
542 return (Hint *) hint;
546 SetHintDelete(SetHint *hint)
563 hint = palloc(sizeof(PlanHint));
564 hint->hint_str = NULL;
565 hint->nall_hints = 0;
566 hint->max_all_hints = 0;
567 hint->all_hints = NULL;
568 memset(hint->num_hints, 0, sizeof(hint->num_hints));
569 hint->scan_hints = NULL;
570 hint->init_scan_mask = 0;
571 hint->parent_relid = 0;
572 hint->parent_hint = NULL;
573 hint->join_hints = NULL;
574 hint->init_join_mask = 0;
575 hint->join_hint_level = NULL;
576 hint->leading_hint = NULL;
577 hint->context = superuser() ? PGC_SUSET : PGC_USERSET;
578 hint->set_hints = NULL;
584 PlanHintDelete(PlanHint *hint)
592 pfree(hint->hint_str);
594 for (i = 0; i < hint->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
595 hint->all_hints[i]->delete_func(hint->all_hints[i]);
597 pfree(hint->all_hints);
605 dump_quote_value(StringInfo buf, const char *value)
607 bool need_quote = false;
610 for (str = value; *str != '\0'; str++)
612 if (isspace(*str) || *str == ')' || *str == '"')
615 appendStringInfoCharMacro(buf, '"');
620 for (str = value; *str != '\0'; str++)
623 appendStringInfoCharMacro(buf, '"');
625 appendStringInfoCharMacro(buf, *str);
629 appendStringInfoCharMacro(buf, '"');
633 ScanMethodHintDump(ScanMethodHint *hint, StringInfo buf)
637 appendStringInfo(buf, "%s(", hint->base.keyword);
638 dump_quote_value(buf, hint->relname);
639 foreach(l, hint->indexnames)
641 appendStringInfoCharMacro(buf, ' ');
642 dump_quote_value(buf, (char *) lfirst(l));
644 appendStringInfoString(buf, ")\n");
648 JoinMethodHintDump(JoinMethodHint *hint, StringInfo buf)
652 appendStringInfo(buf, "%s(", hint->base.keyword);
653 dump_quote_value(buf, hint->relnames[0]);
654 for (i = 1; i < hint->nrels; i++)
656 appendStringInfoCharMacro(buf, ' ');
657 dump_quote_value(buf, hint->relnames[i]);
659 appendStringInfoString(buf, ")\n");
664 LeadingHintDump(LeadingHint *hint, StringInfo buf)
669 appendStringInfo(buf, "%s(", HINT_LEADING);
671 foreach(l, hint->relations)
676 appendStringInfoCharMacro(buf, ' ');
678 dump_quote_value(buf, (char *) lfirst(l));
681 appendStringInfoString(buf, ")\n");
685 SetHintDump(SetHint *hint, StringInfo buf)
687 appendStringInfo(buf, "%s(", HINT_SET);
688 dump_quote_value(buf, hint->name);
689 appendStringInfoCharMacro(buf, ' ');
690 dump_quote_value(buf, hint->value);
691 appendStringInfo(buf, ")\n");
695 all_hint_dump(PlanHint *hint, StringInfo buf, const char *title,
700 appendStringInfo(buf, "%s:\n", title);
701 for (i = 0; i < hint->nall_hints; i++)
703 if (hint->all_hints[i]->state != state)
706 hint->all_hints[i]->dump_func(hint->all_hints[i], buf);
711 PlanHintDump(PlanHint *hint)
717 elog(LOG, "pg_hint_plan:\nno hint");
721 initStringInfo(&buf);
723 appendStringInfoString(&buf, "pg_hint_plan:\n");
724 all_hint_dump(hint, &buf, "used hint", HINT_STATE_USED);
725 all_hint_dump(hint, &buf, "not used hint", HINT_STATE_NOTUSED);
726 all_hint_dump(hint, &buf, "duplication hint", HINT_STATE_DUPLICATION);
727 all_hint_dump(hint, &buf, "error hint", HINT_STATE_ERROR);
729 elog(LOG, "%s", buf.data);
739 RelnameCmp(const void *a, const void *b)
741 const char *relnamea = *((const char **) a);
742 const char *relnameb = *((const char **) b);
744 return strcmp(relnamea, relnameb);
748 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
750 return RelnameCmp(&a->relname, &b->relname);
754 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
758 if (a->nrels != b->nrels)
759 return a->nrels - b->nrels;
761 for (i = 0; i < a->nrels; i++)
764 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
772 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
778 SetHintCmp(const SetHint *a, const SetHint *b)
780 return strcmp(a->name, b->name);
784 AllHintCmp(const void *a, const void *b, bool order)
786 const Hint *hinta = *((const Hint **) a);
787 const Hint *hintb = *((const Hint **) b);
790 if (hinta->type != hintb->type)
791 return hinta->type - hintb->type;
793 if ((result = hinta->cmp_func(hinta, hintb)) != 0 || !order)
797 return hinta->hint_str - hintb->hint_str;
801 AllHintCmpIsOrder(const void *a, const void *b)
803 return AllHintCmp(a, b, true);
811 parse_keyword(const char *str, StringInfo buf)
815 while (!isspace(*str) && *str != '(' && *str != '\0')
816 appendStringInfoCharMacro(buf, *str++);
822 skip_opened_parenthesis(const char *str)
828 parse_ereport(str, ("Opened parenthesis is necessary."));
838 skip_closed_parenthesis(const char *str)
844 parse_ereport(str, ("Closed parenthesis is necessary."));
854 * 二重引用符で囲まれているかもしれないトークンを読み取り word 引数に palloc
855 * で確保したバッファに格納してそのポインタを返す。
857 * 正常にパースできた場合は残りの文字列の先頭位置を、異常があった場合は NULL を
861 parse_quote_value(const char *str, char **word, char *value_type)
869 initStringInfo(&buf);
882 /* 二重引用符が閉じられていない場合はパース中断 */
886 parse_ereport(str, ("Unterminated quoted %s.", value_type));
891 * エスケープ対象のダブルクウォートをスキップする。
892 * もしブロックコメントの開始文字列や終了文字列もオブジェクト名とし
893 * て使用したい場合は、/ と * もエスケープ対象とすることで使用できる
894 * が、処理対象としていない。もしテーブル名にこれらの文字が含まれる
895 * 場合は、エイリアスを指定する必要がある。
904 else if (isspace(*str) || *str == ')' || *str == '"' || *str == '\0')
907 appendStringInfoCharMacro(&buf, *str++);
914 type = pstrdup(value_type);
915 type[0] = toupper(type[0]);
916 parse_ereport(str, ("%s is necessary.", type));
930 parse_hints(PlanHint *plan, Query *parse, const char *str)
935 initStringInfo(&buf);
938 const HintParser *parser;
940 /* in error message, we output the comment including the keyword. */
943 /* parse only the keyword of the hint. */
944 resetStringInfo(&buf);
945 str = parse_keyword(str, &buf);
947 for (parser = parsers; parser->keyword != NULL; parser++)
949 char *keyword = parser->keyword;
952 if (strcasecmp(buf.data, keyword) != 0)
955 hint = parser->create_func(head, keyword);
957 /* parser of each hint does parse in a parenthesis. */
958 if ((str = skip_opened_parenthesis(str)) == NULL ||
959 (str = hint->parser_func(hint, plan, parse, str)) == NULL ||
960 (str = skip_closed_parenthesis(str)) == NULL)
962 hint->delete_func(hint);
968 * 出来上がったヒント情報を追加。スロットが足りない場合は二倍に拡張する。
970 if (plan->nall_hints == 0)
972 plan->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
973 plan->all_hints = palloc(sizeof(Hint *) * plan->max_all_hints);
975 else if (plan->nall_hints == plan->max_all_hints)
977 plan->max_all_hints *= 2;
978 plan->all_hints = repalloc(plan->all_hints,
979 sizeof(Hint *) * plan->max_all_hints);
982 plan->all_hints[plan->nall_hints] = hint;
990 if (parser->keyword == NULL)
992 parse_ereport(head, ("Keyword \"%s\" does not exist.", buf.data));
1002 * Do basic parsing of the query head comment.
1005 parse_head_comment(Query *parse)
1014 /* get client-supplied query string. */
1017 PreparedStatement *entry;
1019 entry = FetchPreparedStatement(stmt_name, true);
1020 p = entry->plansource->query_string;
1023 p = debug_query_string;
1028 /* extract query head comment. */
1029 len = strlen(HINT_START);
1031 if (strncmp(p, HINT_START, len))
1038 /* find hint end keyword. */
1039 if ((tail = strstr(p, HINT_END)) == NULL)
1041 parse_ereport(head, ("Unterminated block comment."));
1045 /* 入れ子にしたブロックコメントはサポートしない */
1046 if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1048 parse_ereport(head, ("Block comments nest doesn't supported."));
1054 head = palloc(len + 1);
1055 memcpy(head, p, len);
1059 plan = PlanHintCreate();
1060 plan->hint_str = head;
1062 /* parse each hint. */
1063 parse_hints(plan, parse, p);
1065 /* When nothing specified a hint, we free PlanHint and returns NULL. */
1066 if (plan->nall_hints == 0)
1068 PlanHintDelete(plan);
1072 /* パースしたヒントを並び替える */
1073 qsort(plan->all_hints, plan->nall_hints, sizeof(Hint *), AllHintCmpIsOrder);
1076 for (i = 0; i < plan->nall_hints; i++)
1078 Hint *hint = plan->all_hints[i];
1080 plan->num_hints[hint->type]++;
1082 if (i + 1 >= plan->nall_hints)
1085 if (AllHintCmp(plan->all_hints + i, plan->all_hints + i + 1, false) ==
1088 const char *HintTypeName[] = {"scan method", "join method",
1091 parse_ereport(plan->all_hints[i]->hint_str,
1092 ("Conflict %s hint.", HintTypeName[hint->type]));
1093 plan->all_hints[i]->state = HINT_STATE_DUPLICATION;
1097 plan->scan_hints = (ScanMethodHint **) plan->all_hints;
1098 plan->join_hints = (JoinMethodHint **) plan->all_hints +
1099 plan->num_hints[HINT_TYPE_SCAN_METHOD];
1100 plan->leading_hint = (LeadingHint *) plan->all_hints[
1101 plan->num_hints[HINT_TYPE_SCAN_METHOD] +
1102 plan->num_hints[HINT_TYPE_JOIN_METHOD] +
1103 plan->num_hints[HINT_TYPE_LEADING] - 1];
1104 plan->set_hints = (SetHint **) plan->all_hints +
1105 plan->num_hints[HINT_TYPE_SCAN_METHOD] +
1106 plan->num_hints[HINT_TYPE_JOIN_METHOD] +
1107 plan->num_hints[HINT_TYPE_LEADING];
1113 * スキャン方式ヒントのカッコ内をパースする
1116 ScanMethodHintParse(ScanMethodHint *hint, PlanHint *plan, Query *parse,
1119 const char *keyword = hint->base.keyword;
1122 * スキャン方式のヒントでリレーション名が読み取れない場合はヒント無効
1124 if ((str = parse_quote_value(str, &hint->relname, "relation name")) == NULL)
1130 * インデックスリストを受け付けるヒントであれば、インデックス参照をパース
1133 if (strcmp(keyword, HINT_INDEXSCAN) == 0 ||
1134 #if PG_VERSION_NUM >= 90200
1135 strcmp(keyword, HINT_INDEXONLYSCAN) == 0 ||
1137 strcmp(keyword, HINT_BITMAPSCAN) == 0)
1139 while (*str != ')' && *str != '\0')
1143 str = parse_quote_value(str, &indexname, "index name");
1147 hint->indexnames = lappend(hint->indexnames, indexname);
1153 * ヒントごとに決まっている許容スキャン方式をビットマスクとして設定
1155 if (strcasecmp(keyword, HINT_SEQSCAN) == 0)
1156 hint->enforce_mask = ENABLE_SEQSCAN;
1157 else if (strcasecmp(keyword, HINT_INDEXSCAN) == 0)
1158 hint->enforce_mask = ENABLE_INDEXSCAN;
1159 else if (strcasecmp(keyword, HINT_BITMAPSCAN) == 0)
1160 hint->enforce_mask = ENABLE_BITMAPSCAN;
1161 else if (strcasecmp(keyword, HINT_TIDSCAN) == 0)
1162 hint->enforce_mask = ENABLE_TIDSCAN;
1163 #if PG_VERSION_NUM >= 90200
1164 else if (strcasecmp(keyword, HINT_INDEXONLYSCAN) == 0)
1165 hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
1167 else if (strcasecmp(keyword, HINT_NOSEQSCAN) == 0)
1168 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
1169 else if (strcasecmp(keyword, HINT_NOINDEXSCAN) == 0)
1170 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
1171 else if (strcasecmp(keyword, HINT_NOBITMAPSCAN) == 0)
1172 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
1173 else if (strcasecmp(keyword, HINT_NOTIDSCAN) == 0)
1174 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
1175 #if PG_VERSION_NUM >= 90200
1176 else if (strcasecmp(keyword, HINT_NOINDEXONLYSCAN) == 0)
1177 hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
1181 parse_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1189 JoinMethodHintParse(JoinMethodHint *hint, PlanHint *plan, Query *parse,
1193 const char *keyword = hint->base.keyword;
1197 hint->relnames = palloc(sizeof(char *));
1199 while ((str = parse_quote_value(str, &relname, "relation name")) != NULL)
1202 hint->relnames = repalloc(hint->relnames, sizeof(char *) * hint->nrels);
1203 hint->relnames[hint->nrels - 1] = relname;
1213 /* Join 対象のテーブルは最低でも2つ指定する必要がある */
1214 if (hint->nrels < 2)
1216 parse_ereport(str, ("Specified relation more than two."));
1217 hint->base.state = HINT_STATE_ERROR;
1221 qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
1223 if (strcasecmp(keyword, HINT_NESTLOOP) == 0)
1224 hint->enforce_mask = ENABLE_NESTLOOP;
1225 else if (strcasecmp(keyword, HINT_MERGEJOIN) == 0)
1226 hint->enforce_mask = ENABLE_MERGEJOIN;
1227 else if (strcasecmp(keyword, HINT_HASHJOIN) == 0)
1228 hint->enforce_mask = ENABLE_HASHJOIN;
1229 else if (strcasecmp(keyword, HINT_NONESTLOOP) == 0)
1230 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
1231 else if (strcasecmp(keyword, HINT_NOMERGEJOIN) == 0)
1232 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
1233 else if (strcasecmp(keyword, HINT_NOHASHJOIN) == 0)
1234 hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
1237 parse_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1245 LeadingHintParse(LeadingHint *hint, PlanHint *plan, Query *parse,
1254 if ((str = parse_quote_value(str, &relname, "relation name")) == NULL)
1257 hint->relations = lappend(hint->relations, relname);
1262 /* テーブル指定が2つ未満の場合は、Leading ヒントはエラーとする */
1263 if (list_length(hint->relations) < 2)
1265 parse_ereport(hint->base.hint_str,
1266 ("In %s hint, specified relation name 2 or more.",
1268 hint->base.state = HINT_STATE_ERROR;
1275 SetHintParse(SetHint *hint, PlanHint *plan, Query *parse, const char *str)
1277 if ((str = parse_quote_value(str, &hint->name, "parameter name")) == NULL ||
1278 (str = parse_quote_value(str, &hint->value, "parameter value")) == NULL)
1285 * set GUC parameter functions
1288 #if PG_VERSION_NUM < 90200
1290 set_config_option_wrapper(const char *name, const char *value,
1291 GucContext context, GucSource source,
1292 GucAction action, bool changeVal, int elevel)
1295 MemoryContext ccxt = CurrentMemoryContext;
1299 result = set_config_option(name, value, context, source,
1307 if (elevel >= ERROR)
1310 ecxt = MemoryContextSwitchTo(ccxt);
1311 errdata = CopyErrorData();
1312 ereport(elevel, (errcode(errdata->sqlerrcode),
1313 errmsg("%s", errdata->message),
1314 errdata->detail ? errdetail("%s", errdata->detail) : 0,
1315 errdata->hint ? errhint("%s", errdata->hint) : 0));
1316 FreeErrorData(errdata);
1318 MemoryContextSwitchTo(ecxt);
1325 #define set_config_option(name, value, context, source, \
1326 action, changeVal, elevel) \
1327 set_config_option_wrapper(name, value, context, source, \
1328 action, changeVal, elevel)
1332 set_config_options(SetHint **options, int noptions, GucContext context)
1337 save_nestlevel = NewGUCNestLevel();
1339 for (i = 0; i < noptions; i++)
1341 SetHint *hint = options[i];
1344 if (!hint_state_enabled(hint))
1347 result = set_config_option(hint->name, hint->value, context,
1348 PGC_S_SESSION, GUC_ACTION_SAVE, true,
1349 pg_hint_plan_parse_messages);
1351 hint->base.state = HINT_STATE_USED;
1353 hint->base.state = HINT_STATE_ERROR;
1356 return save_nestlevel;
1359 #define SET_CONFIG_OPTION(name, type_bits) \
1360 set_config_option((name), \
1361 (mask & (type_bits)) ? "true" : "false", \
1362 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
1365 set_scan_config_options(unsigned char enforce_mask, GucContext context)
1369 if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
1370 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
1371 #if PG_VERSION_NUM >= 90200
1372 || enforce_mask == ENABLE_INDEXONLYSCAN
1375 mask = enforce_mask;
1377 mask = enforce_mask & global->init_scan_mask;
1379 SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
1380 SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
1381 SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
1382 SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
1383 #if PG_VERSION_NUM >= 90200
1384 SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
1389 set_join_config_options(unsigned char enforce_mask, GucContext context)
1393 if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
1394 enforce_mask == ENABLE_HASHJOIN)
1395 mask = enforce_mask;
1397 mask = enforce_mask & global->init_scan_mask;
1399 SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
1400 SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
1401 SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
1405 * pg_hint_plan hook functions
1409 pg_hint_plan_ProcessUtility(Node *parsetree, const char *queryString,
1410 ParamListInfo params, bool isTopLevel,
1411 DestReceiver *dest, char *completionTag)
1415 if (!pg_hint_plan_enable)
1417 if (prev_ProcessUtility)
1418 (*prev_ProcessUtility) (parsetree, queryString, params,
1419 isTopLevel, dest, completionTag);
1421 standard_ProcessUtility(parsetree, queryString, params,
1422 isTopLevel, dest, completionTag);
1428 if (IsA(node, ExplainStmt))
1431 * EXPLAIN対象のクエリのパースツリーを取得する
1436 stmt = (ExplainStmt *) node;
1438 Assert(IsA(stmt->query, Query));
1439 query = (Query *) stmt->query;
1441 if (query->commandType == CMD_UTILITY && query->utilityStmt != NULL)
1442 node = query->utilityStmt;
1446 * EXECUTEコマンドならば、PREPARE時に指定されたクエリ文字列を取得し、ヒント
1449 if (IsA(node, ExecuteStmt))
1453 stmt = (ExecuteStmt *) node;
1454 stmt_name = stmt->name;
1461 if (prev_ProcessUtility)
1462 (*prev_ProcessUtility) (parsetree, queryString, params,
1463 isTopLevel, dest, completionTag);
1465 standard_ProcessUtility(parsetree, queryString, params,
1466 isTopLevel, dest, completionTag);
1480 if (prev_ProcessUtility)
1481 (*prev_ProcessUtility) (parsetree, queryString, params,
1482 isTopLevel, dest, completionTag);
1484 standard_ProcessUtility(parsetree, queryString, params,
1485 isTopLevel, dest, completionTag);
1488 static PlannedStmt *
1489 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
1492 PlannedStmt *result;
1495 * hintが指定されない、または空のhintを指定された場合は通常のparser処理をお
1497 * 他のフック関数で実行されるhint処理をスキップするために、global 変数をNULL
1500 if (!pg_hint_plan_enable || (global = parse_head_comment(parse)) == NULL)
1505 return (*prev_planner) (parse, cursorOptions, boundParams);
1507 return standard_planner(parse, cursorOptions, boundParams);
1510 /* Set hint で指定されたGUCパラメータを設定する */
1511 save_nestlevel = set_config_options(global->set_hints,
1512 global->num_hints[HINT_TYPE_SET],
1516 global->init_scan_mask |= ENABLE_SEQSCAN;
1517 if (enable_indexscan)
1518 global->init_scan_mask |= ENABLE_INDEXSCAN;
1519 if (enable_bitmapscan)
1520 global->init_scan_mask |= ENABLE_BITMAPSCAN;
1522 global->init_scan_mask |= ENABLE_TIDSCAN;
1523 #if PG_VERSION_NUM >= 90200
1524 if (enable_indexonlyscan)
1525 global->init_scan_mask |= ENABLE_INDEXONLYSCAN;
1527 if (enable_nestloop)
1528 global->init_join_mask |= ENABLE_NESTLOOP;
1529 if (enable_mergejoin)
1530 global->init_join_mask |= ENABLE_MERGEJOIN;
1531 if (enable_hashjoin)
1532 global->init_join_mask |= ENABLE_HASHJOIN;
1535 result = (*prev_planner) (parse, cursorOptions, boundParams);
1537 result = standard_planner(parse, cursorOptions, boundParams);
1540 * Restore the GUC variables we set above.
1542 AtEOXact_GUC(true, save_nestlevel);
1545 * Print hint if debugging.
1547 if (pg_hint_plan_debug_print)
1548 PlanHintDump(global);
1550 PlanHintDelete(global);
1557 * aliasnameと一致するSCANヒントを探す
1559 static ScanMethodHint *
1560 find_scan_hint(PlannerInfo *root, RelOptInfo *rel)
1566 * RELOPT_BASEREL でなければ、scan method ヒントが適用しない。
1567 * 子テーブルの場合はRELOPT_OTHER_MEMBER_RELとなるが、サポート対象外とする。
1568 * また、通常のリレーション以外は、スキャン方式を選択できない。
1570 if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
1573 rte = root->simple_rte_array[rel->relid];
1575 /* 外部表はスキャン方式が選択できない。 */
1576 if (rte->relkind == RELKIND_FOREIGN_TABLE)
1580 * スキャン方式のヒントのリストから、検索対象のリレーションと名称が一致する
1583 for (i = 0; i < global->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
1585 ScanMethodHint *hint = global->scan_hints[i];
1587 /* すでに無効となっているヒントは検索対象にしない。 */
1588 if (!hint_state_enabled(hint))
1591 if (RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
1599 delete_indexes(ScanMethodHint *hint, RelOptInfo *rel)
1606 * We delete all the IndexOptInfo list and prevent you from being usable by
1609 if (hint->enforce_mask == ENABLE_SEQSCAN ||
1610 hint->enforce_mask == ENABLE_TIDSCAN)
1612 list_free_deep(rel->indexlist);
1613 rel->indexlist = NIL;
1614 hint->base.state = HINT_STATE_USED;
1620 * When a list of indexes is not specified, we just use all indexes.
1622 if (hint->indexnames == NIL)
1626 * Leaving only an specified index, we delete it from a IndexOptInfo list
1630 for (cell = list_head(rel->indexlist); cell; cell = next)
1632 IndexOptInfo *info = (IndexOptInfo *) lfirst(cell);
1633 char *indexname = get_rel_name(info->indexoid);
1635 bool use_index = false;
1639 foreach(l, hint->indexnames)
1641 if (RelnameCmp(&indexname, &lfirst(l)) == 0)
1649 rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
1658 pg_hint_plan_get_relation_info(PlannerInfo *root, Oid relationObjectId,
1659 bool inhparent, RelOptInfo *rel)
1661 ScanMethodHint *hint;
1663 if (prev_get_relation_info)
1664 (*prev_get_relation_info) (root, relationObjectId, inhparent, rel);
1666 /* 有効なヒントが指定されなかった場合は処理をスキップする。 */
1672 /* store does relids of parent table. */
1673 global->parent_relid = rel->relid;
1675 else if (global->parent_relid != 0)
1678 * We use the same GUC parameter if this table is the child table of a
1679 * table called pg_hint_plan_get_relation_info just before that.
1683 /* append_rel_list contains all append rels; ignore others */
1684 foreach(l, root->append_rel_list)
1686 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1688 /* This rel is child table. */
1689 if (appinfo->parent_relid == global->parent_relid &&
1690 appinfo->child_relid == rel->relid)
1692 if (global->parent_hint)
1693 delete_indexes(global->parent_hint, rel);
1699 /* This rel is not inherit table. */
1700 global->parent_relid = 0;
1701 global->parent_hint = NULL;
1704 /* scan hint が指定されない場合は、GUCパラメータをリセットする。 */
1705 if ((hint = find_scan_hint(root, rel)) == NULL)
1707 set_scan_config_options(global->init_scan_mask, global->context);
1710 set_scan_config_options(hint->enforce_mask, global->context);
1711 hint->base.state = HINT_STATE_USED;
1713 global->parent_hint = hint;
1715 delete_indexes(hint, rel);
1719 * aliasnameがクエリ中に指定した別名と一致する場合は、そのインデックスを返し、一致
1721 * aliasnameがクエリ中に複数回指定された場合は、-1を返す。
1724 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
1730 for (i = 1; i < root->simple_rel_array_size; i++)
1734 if (root->simple_rel_array[i] == NULL)
1737 Assert(i == root->simple_rel_array[i]->relid);
1739 if (RelnameCmp(&aliasname, &root->simple_rte_array[i]->eref->aliasname)
1743 foreach(l, initial_rels)
1745 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
1747 if (rel->reloptkind == RELOPT_BASEREL)
1749 if (rel->relid != i)
1754 Assert(rel->reloptkind == RELOPT_JOINREL);
1756 if (!bms_is_member(i, rel->relids))
1763 ("Relation name \"%s\" is ambiguous.",
1778 * relidビットマスクと一致するヒントを探す
1780 static JoinMethodHint *
1781 find_join_hint(Relids joinrelids)
1786 join_hint = global->join_hint_level[bms_num_members(joinrelids)];
1788 foreach(l, join_hint)
1790 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
1792 if (bms_equal(joinrelids, hint->joinrelids))
1800 * 結合方式のヒントを使用しやすい構造に変換する。
1803 transform_join_hints(PlanHint *plan, PlannerInfo *root, int nbaserel,
1804 List *initial_rels, JoinMethodHint **join_method_hints)
1813 for (i = 0; i < plan->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
1815 JoinMethodHint *hint = plan->join_hints[i];
1818 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
1821 bms_free(hint->joinrelids);
1822 hint->joinrelids = NULL;
1824 for (j = 0; j < hint->nrels; j++)
1826 char *relname = hint->relnames[j];
1828 relid = find_relid_aliasname(root, relname, initial_rels,
1829 hint->base.hint_str);
1832 hint->base.state = HINT_STATE_ERROR;
1837 if (bms_is_member(relid, hint->joinrelids))
1839 parse_ereport(hint->base.hint_str,
1840 ("Relation name \"%s\" is duplicate.", relname));
1841 hint->base.state = HINT_STATE_ERROR;
1845 hint->joinrelids = bms_add_member(hint->joinrelids, relid);
1848 if (relid <= 0 || hint->base.state == HINT_STATE_ERROR)
1851 plan->join_hint_level[hint->nrels] =
1852 lappend(plan->join_hint_level[hint->nrels], hint);
1856 * 有効なLeading ヒントが指定されている場合は、結合順にあわせてjoin method hint
1859 if (plan->num_hints[HINT_TYPE_LEADING] == 0)
1862 lhint = plan->leading_hint;
1863 if (!hint_state_enabled(lhint))
1866 /* Leading hint は、全ての join 方式が有効な hint として登録する */
1869 foreach(l, lhint->relations)
1871 char *relname = (char *)lfirst(l);
1872 JoinMethodHint *hint;
1875 find_relid_aliasname(root, relname, initial_rels, plan->hint_str);
1879 bms_free(joinrelids);
1886 if (bms_is_member(relid, joinrelids))
1888 parse_ereport(lhint->base.hint_str,
1889 ("Relation name \"%s\" is duplicate.", relname));
1890 lhint->base.state = HINT_STATE_ERROR;
1891 bms_free(joinrelids);
1895 joinrelids = bms_add_member(joinrelids, relid);
1901 hint = find_join_hint(joinrelids);
1905 * Here relnames is not set, since Relids bitmap is sufficient to
1906 * control paths of this query afterwards.
1908 hint = (JoinMethodHint *) JoinMethodHintCreate(lhint->base.hint_str,
1910 hint->base.state = HINT_STATE_USED;
1911 hint->nrels = njoinrels;
1912 hint->enforce_mask = ENABLE_ALL_JOIN;
1913 hint->joinrelids = bms_copy(joinrelids);
1916 join_method_hints[njoinrels] = hint;
1918 if (njoinrels >= nbaserel)
1922 bms_free(joinrelids);
1927 for (i = 2; i <= njoinrels; i++)
1929 /* Leading で指定した組み合わせ以外の join hint を削除する */
1930 list_free(plan->join_hint_level[i]);
1932 plan->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
1935 if (hint_state_enabled(lhint))
1936 set_join_config_options(DISABLE_ALL_JOIN, global->context);
1938 lhint->base.state = HINT_STATE_USED;
1943 * set_plain_rel_pathlist
1944 * Build access paths for a plain relation (no subquery, no inheritance)
1946 * This function was copied and edited from set_plain_rel_pathlist() in
1947 * src/backend/optimizer/path/allpaths.c
1950 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1952 /* Consider sequential scan */
1953 add_path(rel, create_seqscan_path(root, rel));
1955 /* Consider index scans */
1956 create_index_paths(root, rel);
1958 /* Consider TID scans */
1959 create_tidscan_paths(root, rel);
1961 /* Now find the cheapest of the paths for this rel */
1966 rebuild_scan_path(PlanHint *plan, PlannerInfo *root, int level,
1971 foreach(l, initial_rels)
1973 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
1975 ScanMethodHint *hint;
1978 * スキャン方式が選択できるリレーションのみ、スキャンパスを再生成
1981 if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
1984 rte = root->simple_rte_array[rel->relid];
1986 /* 外部表はスキャン方式が選択できない。 */
1987 if (rte->relkind == RELKIND_FOREIGN_TABLE)
1991 * scan method hint が指定されていなければ、初期値のGUCパラメータでscan
1994 if ((hint = find_scan_hint(root, rel)) == NULL)
1995 set_scan_config_options(plan->init_scan_mask, plan->context);
1998 set_scan_config_options(hint->enforce_mask, plan->context);
1999 hint->base.state = HINT_STATE_USED;
2002 list_free_deep(rel->pathlist);
2003 rel->pathlist = NIL;
2006 /* It's an "append relation", process accordingly */
2007 set_append_rel_pathlist(root, rel, rel->relid, rte);
2011 set_plain_rel_pathlist(root, rel, rte);
2016 * Restore the GUC variables we set above.
2018 set_scan_config_options(plan->init_scan_mask, plan->context);
2022 * make_join_rel() をラップする関数
2024 * ヒントにしたがって、enabele_* パラメータを変更した上で、make_join_rel()を
2028 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
2031 JoinMethodHint *hint;
2035 joinrelids = bms_union(rel1->relids, rel2->relids);
2036 hint = find_join_hint(joinrelids);
2037 bms_free(joinrelids);
2040 return pg_hint_plan_make_join_rel(root, rel1, rel2);
2042 save_nestlevel = NewGUCNestLevel();
2044 set_join_config_options(hint->enforce_mask, global->context);
2046 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
2047 hint->base.state = HINT_STATE_USED;
2050 * Restore the GUC variables we set above.
2052 AtEOXact_GUC(true, save_nestlevel);
2058 get_num_baserels(List *initial_rels)
2063 foreach(l, initial_rels)
2065 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
2067 if (rel->reloptkind == RELOPT_BASEREL)
2069 else if (rel->reloptkind ==RELOPT_JOINREL)
2070 nbaserel+= bms_num_members(rel->relids);
2073 /* other values not expected here */
2074 elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
2082 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
2085 JoinMethodHint **join_method_hints;
2091 * pg_hint_planが無効、または有効なヒントが1つも指定されなかった場合は、標準
2096 if (prev_join_search)
2097 return (*prev_join_search) (root, levels_needed, initial_rels);
2098 else if (enable_geqo && levels_needed >= geqo_threshold)
2099 return geqo(root, levels_needed, initial_rels);
2101 return standard_join_search(root, levels_needed, initial_rels);
2104 /* We apply scan method hint rebuild scan path. */
2105 rebuild_scan_path(global, root, levels_needed, initial_rels);
2108 * GEQOを使用する条件を満たした場合は、GEQOを用いた結合方式の検索を行う。
2109 * このとき、スキャン方式のヒントとSetヒントのみが有効になり、結合方式や結合
2110 * 順序はヒント句は無効になりGEQOのアルゴリズムで決定される。
2112 if (enable_geqo && levels_needed >= geqo_threshold)
2113 return geqo(root, levels_needed, initial_rels);
2115 nbaserel = get_num_baserels(initial_rels);
2116 global->join_hint_level = palloc0(sizeof(List *) * (nbaserel + 1));
2117 join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
2119 transform_join_hints(global, root, nbaserel, initial_rels,
2122 rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
2124 for (i = 2; i <= nbaserel; i++)
2126 list_free(global->join_hint_level[i]);
2128 /* free Leading hint only */
2129 if (join_method_hints[i] != NULL &&
2130 join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
2131 JoinMethodHintDelete(join_method_hints[i]);
2133 pfree(global->join_hint_level);
2134 pfree(join_method_hints);
2136 if (global->num_hints[HINT_TYPE_LEADING] > 0 &&
2137 hint_state_enabled(global->leading_hint))
2138 set_join_config_options(global->init_join_mask, global->context);
2145 * Build access paths for a base relation
2147 * This function was copied and edited from set_rel_pathlist() in
2148 * src/backend/optimizer/path/allpaths.c
2151 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
2152 Index rti, RangeTblEntry *rte)
2156 /* It's an "append relation", process accordingly */
2157 set_append_rel_pathlist(root, rel, rti, rte);
2161 if (rel->rtekind == RTE_RELATION)
2163 if (rte->relkind == RELKIND_RELATION)
2165 /* Plain relation */
2166 set_plain_rel_pathlist(root, rel, rte);
2169 elog(ERROR, "unexpected relkind: %c", rte->relkind);
2172 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
2176 #define standard_join_search pg_hint_plan_standard_join_search
2177 #define join_search_one_level pg_hint_plan_join_search_one_level
2178 #define make_join_rel make_join_rel_wrapper
2181 #undef make_join_rel
2182 #define make_join_rel pg_hint_plan_make_join_rel
2183 #define add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype, sjinfo, restrictlist) \
2185 ScanMethodHint *hint = NULL; \
2186 if ((hint = find_scan_hint((root), (innerrel))) != NULL) \
2188 set_scan_config_options(hint->enforce_mask, global->context); \
2189 hint->base.state = HINT_STATE_USED; \
2191 add_paths_to_joinrel((root), (joinrel), (outerrel), (innerrel), (jointype), (sjinfo), (restrictlist)); \
2193 set_scan_config_options(global->init_scan_mask, global->context); \
2195 #include "make_join_rel.c"