OSDN Git Service

4ae79e399d0372eccaf425cd5217ef8e19471a81
[pghintplan/pg_hint_plan.git] / pg_hint_plan.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_hint_plan.c
4  *                do instructions or hints to the planner using C-style block comments
5  *                of the SQL.
6  *
7  * Copyright (c) 2012-2013, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
8  *
9  *-------------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 #include "catalog/pg_collation.h"
13 #include "catalog/pg_index.h"
14 #include "commands/prepare.h"
15 #include "mb/pg_wchar.h"
16 #include "miscadmin.h"
17 #include "nodes/nodeFuncs.h"
18 #include "optimizer/clauses.h"
19 #include "optimizer/cost.h"
20 #include "optimizer/geqo.h"
21 #include "optimizer/joininfo.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/plancat.h"
25 #include "optimizer/planner.h"
26 #include "optimizer/prep.h"
27 #include "optimizer/restrictinfo.h"
28 #include "parser/scansup.h"
29 #include "tcop/utility.h"
30 #include "utils/builtins.h"
31 #include "utils/lsyscache.h"
32 #include "utils/memutils.h"
33 #include "utils/rel.h"
34 #include "utils/syscache.h"
35 #if PG_VERSION_NUM >= 90200
36 #include "catalog/pg_class.h"
37 #endif
38
39 #include "executor/spi.h"
40 #include "catalog/pg_type.h"
41
42 #ifdef PG_MODULE_MAGIC
43 PG_MODULE_MAGIC;
44 #endif
45
46 #if PG_VERSION_NUM < 90100
47 #error unsupported PostgreSQL version
48 #endif
49
50 #define BLOCK_COMMENT_START             "/*"
51 #define BLOCK_COMMENT_END               "*/"
52 #define HINT_COMMENT_KEYWORD    "+"
53 #define HINT_START                              BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
54 #define HINT_END                                BLOCK_COMMENT_END
55
56 /* hint keywords */
57 #define HINT_SEQSCAN                    "SeqScan"
58 #define HINT_INDEXSCAN                  "IndexScan"
59 #define HINT_INDEXSCANREGEXP    "IndexScanRegexp"
60 #define HINT_BITMAPSCAN                 "BitmapScan"
61 #define HINT_BITMAPSCANREGEXP   "BitmapScanRegexp"
62 #define HINT_TIDSCAN                    "TidScan"
63 #define HINT_NOSEQSCAN                  "NoSeqScan"
64 #define HINT_NOINDEXSCAN                "NoIndexScan"
65 #define HINT_NOBITMAPSCAN               "NoBitmapScan"
66 #define HINT_NOTIDSCAN                  "NoTidScan"
67 #if PG_VERSION_NUM >= 90200
68 #define HINT_INDEXONLYSCAN              "IndexOnlyScan"
69 #define HINT_INDEXONLYSCANREGEXP        "IndexOnlyScanRegexp"
70 #define HINT_NOINDEXONLYSCAN    "NoIndexOnlyScan"
71 #endif
72 #define HINT_NESTLOOP                   "NestLoop"
73 #define HINT_MERGEJOIN                  "MergeJoin"
74 #define HINT_HASHJOIN                   "HashJoin"
75 #define HINT_NONESTLOOP                 "NoNestLoop"
76 #define HINT_NOMERGEJOIN                "NoMergeJoin"
77 #define HINT_NOHASHJOIN                 "NoHashJoin"
78 #define HINT_LEADING                    "Leading"
79 #define HINT_SET                                "Set"
80
81 #define HINT_ARRAY_DEFAULT_INITSIZE 8
82
83 #define hint_ereport(str, detail) \
84         ereport(pg_hint_plan_parse_messages, \
85                         (errmsg("hint syntax error at or near \"%s\"", (str)), \
86                          errdetail detail))
87
88 #define skip_space(str) \
89         while (isspace(*str)) \
90                 str++;
91
92 enum
93 {
94         ENABLE_SEQSCAN = 0x01,
95         ENABLE_INDEXSCAN = 0x02,
96         ENABLE_BITMAPSCAN = 0x04,
97         ENABLE_TIDSCAN = 0x08,
98 #if PG_VERSION_NUM >= 90200
99         ENABLE_INDEXONLYSCAN = 0x10
100 #endif
101 } SCAN_TYPE_BITS;
102
103 enum
104 {
105         ENABLE_NESTLOOP = 0x01,
106         ENABLE_MERGEJOIN = 0x02,
107         ENABLE_HASHJOIN = 0x04
108 } JOIN_TYPE_BITS;
109
110 #if PG_VERSION_NUM >= 90200
111 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
112                                                  ENABLE_BITMAPSCAN | ENABLE_TIDSCAN | \
113                                                  ENABLE_INDEXONLYSCAN)
114 #else
115 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
116                                                  ENABLE_BITMAPSCAN | ENABLE_TIDSCAN)
117 #endif
118 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
119 #define DISABLE_ALL_SCAN 0
120 #define DISABLE_ALL_JOIN 0
121
122 /* hint keyword of enum type*/
123 typedef enum HintKeyword
124 {
125         HINT_KEYWORD_SEQSCAN,
126         HINT_KEYWORD_INDEXSCAN,
127         HINT_KEYWORD_INDEXSCANREGEXP,
128         HINT_KEYWORD_BITMAPSCAN,
129         HINT_KEYWORD_BITMAPSCANREGEXP,
130         HINT_KEYWORD_TIDSCAN,
131         HINT_KEYWORD_NOSEQSCAN,
132         HINT_KEYWORD_NOINDEXSCAN,
133         HINT_KEYWORD_NOBITMAPSCAN,
134         HINT_KEYWORD_NOTIDSCAN,
135 #if PG_VERSION_NUM >= 90200
136         HINT_KEYWORD_INDEXONLYSCAN,
137         HINT_KEYWORD_INDEXONLYSCANREGEXP,
138         HINT_KEYWORD_NOINDEXONLYSCAN,
139 #endif
140         HINT_KEYWORD_NESTLOOP,
141         HINT_KEYWORD_MERGEJOIN,
142         HINT_KEYWORD_HASHJOIN,
143         HINT_KEYWORD_NONESTLOOP,
144         HINT_KEYWORD_NOMERGEJOIN,
145         HINT_KEYWORD_NOHASHJOIN,
146         HINT_KEYWORD_LEADING,
147         HINT_KEYWORD_SET,
148         HINT_KEYWORD_UNRECOGNIZED
149 } HintKeyword;
150
151 typedef struct Hint Hint;
152 typedef struct HintState HintState;
153
154 typedef Hint *(*HintCreateFunction) (const char *hint_str,
155                                                                          const char *keyword,
156                                                                          HintKeyword hint_keyword);
157 typedef void (*HintDeleteFunction) (Hint *hint);
158 typedef void (*HintDescFunction) (Hint *hint, StringInfo buf);
159 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
160 typedef const char *(*HintParseFunction) (Hint *hint, HintState *hstate,
161                                                                                   Query *parse, const char *str);
162
163 /* hint types */
164 #define NUM_HINT_TYPE   4
165 typedef enum HintType
166 {
167         HINT_TYPE_SCAN_METHOD,
168         HINT_TYPE_JOIN_METHOD,
169         HINT_TYPE_LEADING,
170         HINT_TYPE_SET
171 } HintType;
172
173 static const char *HintTypeName[] = {
174         "scan method",
175         "join method",
176         "leading",
177         "set"
178 };
179
180 /* hint status */
181 typedef enum HintStatus
182 {
183         HINT_STATE_NOTUSED = 0,         /* specified relation not used in query */
184         HINT_STATE_USED,                        /* hint is used */
185         HINT_STATE_DUPLICATION,         /* specified hint duplication */
186         HINT_STATE_ERROR                        /* execute error (parse error does not include
187                                                                  * it) */
188 } HintStatus;
189
190 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
191                                                                   (hint)->base.state == HINT_STATE_USED)
192
193 /* common data for all hints. */
194 struct Hint
195 {
196         const char                 *hint_str;           /* must not do pfree */
197         const char                 *keyword;            /* must not do pfree */
198         HintKeyword                     hint_keyword;
199         HintType                        type;
200         HintStatus                      state;
201         HintDeleteFunction      delete_func;
202         HintDescFunction        desc_func;
203         HintCmpFunction         cmp_func;
204         HintParseFunction       parse_func;
205 };
206
207 /* scan method hints */
208 typedef struct ScanMethodHint
209 {
210         Hint                    base;
211         char               *relname;
212         List               *indexnames;
213         bool                    regexp;
214         unsigned char   enforce_mask;
215 } ScanMethodHint;
216
217 typedef struct ParentIndexInfo
218 {
219         bool            indisunique;
220         Oid                     method;
221         List       *column_names;
222         char       *expression_str;
223         Oid                *indcollation;
224         Oid                *opclass;
225         int16      *indoption;
226         char       *indpred_str;
227 } ParentIndexInfo;
228
229 /* join method hints */
230 typedef struct JoinMethodHint
231 {
232         Hint                    base;
233         int                             nrels;
234         int                             inner_nrels;
235         char              **relnames;
236         unsigned char   enforce_mask;
237         Relids                  joinrelids;
238         Relids                  inner_joinrelids;
239 } JoinMethodHint;
240
241 /* join order hints */
242 typedef struct OuterInnerRels
243 {
244         char   *relation;
245         List   *outer_inner_pair;
246 } OuterInnerRels;
247
248 typedef struct LeadingHint
249 {
250         Hint                    base;
251         List               *relations;  /* relation names specified in Leading hint */
252         OuterInnerRels *outer_inner;
253 } LeadingHint;
254
255 /* change a run-time parameter hints */
256 typedef struct SetHint
257 {
258         Hint    base;
259         char   *name;                           /* name of variable */
260         char   *value;
261         List   *words;
262 } SetHint;
263
264 /*
265  * Describes a context of hint processing.
266  */
267 struct HintState
268 {
269         char               *hint_str;                   /* original hint string */
270
271         /* all hint */
272         int                             nall_hints;                     /* # of valid all hints */
273         int                             max_all_hints;          /* # of slots for all hints */
274         Hint              **all_hints;                  /* parsed all hints */
275
276         /* # of each hints */
277         int                             num_hints[NUM_HINT_TYPE];
278
279         /* for scan method hints */
280         ScanMethodHint **scan_hints;            /* parsed scan hints */
281         int                             init_scan_mask;         /* initial value scan parameter */
282         Index                   parent_relid;           /* inherit parent table relid */
283         Oid                             parent_rel_oid;     /* inherit parent table relid */
284         ScanMethodHint *parent_hint;            /* inherit parent table scan hint */
285         List               *parent_index_infos; /* infomation of inherit parent table's
286                                                                                  * index */
287
288         /* for join method hints */
289         JoinMethodHint **join_hints;            /* parsed join hints */
290         int                             init_join_mask;         /* initial value join parameter */
291         List              **join_hint_level;
292
293         /* for Leading hint */
294         LeadingHint       **leading_hint;               /* parsed Leading hints */
295
296         /* for Set hints */
297         SetHint           **set_hints;                  /* parsed Set hints */
298         GucContext              context;                        /* which GUC parameters can we set? */
299 };
300
301 /*
302  * Describes a hint parser module which is bound with particular hint keyword.
303  */
304 typedef struct HintParser
305 {
306         char                       *keyword;
307         HintCreateFunction      create_func;
308         HintKeyword                     hint_keyword;
309 } HintParser;
310
311 /* Module callbacks */
312 void            _PG_init(void);
313 void            _PG_fini(void);
314
315 static void push_hint(HintState *hstate);
316 static void pop_hint(void);
317
318 static void pg_hint_plan_ProcessUtility(Node *parsetree,
319                                                                                 const char *queryString,
320                                                                                 ParamListInfo params, bool isTopLevel,
321                                                                                 DestReceiver *dest,
322                                                                                 char *completionTag);
323 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
324                                                                                  ParamListInfo boundParams);
325 static void pg_hint_plan_get_relation_info(PlannerInfo *root,
326                                                                                    Oid relationObjectId,
327                                                                                    bool inhparent, RelOptInfo *rel);
328 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
329                                                                                         int levels_needed,
330                                                                                         List *initial_rels);
331
332 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword,
333                                                                   HintKeyword hint_keyword);
334 static void ScanMethodHintDelete(ScanMethodHint *hint);
335 static void ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf);
336 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
337 static const char *ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate,
338                                                                            Query *parse, const char *str);
339 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword,
340                                                                   HintKeyword hint_keyword);
341 static void JoinMethodHintDelete(JoinMethodHint *hint);
342 static void JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf);
343 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
344 static const char *JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate,
345                                                                            Query *parse, const char *str);
346 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword,
347                                                            HintKeyword hint_keyword);
348 static void LeadingHintDelete(LeadingHint *hint);
349 static void LeadingHintDesc(LeadingHint *hint, StringInfo buf);
350 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
351 static const char *LeadingHintParse(LeadingHint *hint, HintState *hstate,
352                                                                         Query *parse, const char *str);
353 static Hint *SetHintCreate(const char *hint_str, const char *keyword,
354                                                    HintKeyword hint_keyword);
355 static void SetHintDelete(SetHint *hint);
356 static void SetHintDesc(SetHint *hint, StringInfo buf);
357 static int SetHintCmp(const SetHint *a, const SetHint *b);
358 static const char *SetHintParse(SetHint *hint, HintState *hstate, Query *parse,
359                                                                 const char *str);
360
361 static void quote_value(StringInfo buf, const char *value);
362
363 static const char *parse_quoted_value(const char *str, char **word,
364                                                                           bool truncate);
365
366 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
367                                                                                           int levels_needed,
368                                                                                           List *initial_rels);
369 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
370 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
371                                                                           ListCell *other_rels);
372 static void make_rels_by_clauseless_joins(PlannerInfo *root,
373                                                                                   RelOptInfo *old_rel,
374                                                                                   ListCell *other_rels);
375 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
376 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
377                                                                         Index rti, RangeTblEntry *rte);
378 #if PG_VERSION_NUM >= 90200
379 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
380                                                    List *live_childrels,
381                                                    List *all_child_pathkeys);
382 #endif
383 static List *accumulate_append_subpath(List *subpaths, Path *path);
384 #if PG_VERSION_NUM < 90200
385 static void set_dummy_rel_pathlist(RelOptInfo *rel);
386 #endif
387 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
388                                                                            RelOptInfo *rel2);
389
390 /* GUC variables */
391 static bool     pg_hint_plan_enable_hint = true;
392 static bool     pg_hint_plan_debug_print = false;
393 static int      pg_hint_plan_parse_messages = INFO;
394
395 static const struct config_enum_entry parse_messages_level_options[] = {
396         {"debug", DEBUG2, true},
397         {"debug5", DEBUG5, false},
398         {"debug4", DEBUG4, false},
399         {"debug3", DEBUG3, false},
400         {"debug2", DEBUG2, false},
401         {"debug1", DEBUG1, false},
402         {"log", LOG, false},
403         {"info", INFO, false},
404         {"notice", NOTICE, false},
405         {"warning", WARNING, false},
406         {"error", ERROR, false},
407         /*
408          * {"fatal", FATAL, true},
409          * {"panic", PANIC, true},
410          */
411         {NULL, 0, false}
412 };
413
414 /* Saved hook values in case of unload */
415 static ProcessUtility_hook_type prev_ProcessUtility = NULL;
416 static planner_hook_type prev_planner = NULL;
417 static get_relation_info_hook_type prev_get_relation_info = NULL;
418 static join_search_hook_type prev_join_search = NULL;
419
420 /* Hold reference to currently active hint */
421 static HintState *current_hint = NULL;
422
423 /*
424  * List of hint contexts.  We treat the head of the list as the Top of the
425  * context stack, so current_hint always points the first element of this list.
426  */
427 static List *HintStateStack = NIL;
428
429 /*
430  * Holds statement name during executing EXECUTE command.  NULL for other
431  * statements.
432  */
433 static char        *stmt_name = NULL;
434
435 static const HintParser parsers[] = {
436         {HINT_SEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_SEQSCAN},
437         {HINT_INDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCAN},
438         {HINT_INDEXSCANREGEXP, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCANREGEXP},
439         {HINT_BITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_BITMAPSCAN},
440         {HINT_BITMAPSCANREGEXP, ScanMethodHintCreate,
441          HINT_KEYWORD_BITMAPSCANREGEXP},
442         {HINT_TIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_TIDSCAN},
443         {HINT_NOSEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOSEQSCAN},
444         {HINT_NOINDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXSCAN},
445         {HINT_NOBITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOBITMAPSCAN},
446         {HINT_NOTIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOTIDSCAN},
447 #if PG_VERSION_NUM >= 90200
448         {HINT_INDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXONLYSCAN},
449         {HINT_INDEXONLYSCANREGEXP, ScanMethodHintCreate,
450          HINT_KEYWORD_INDEXONLYSCANREGEXP},
451         {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXONLYSCAN},
452 #endif
453         {HINT_NESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NESTLOOP},
454         {HINT_MERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_MERGEJOIN},
455         {HINT_HASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_HASHJOIN},
456         {HINT_NONESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NONESTLOOP},
457         {HINT_NOMERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOMERGEJOIN},
458         {HINT_NOHASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOHASHJOIN},
459         {HINT_LEADING, LeadingHintCreate, HINT_KEYWORD_LEADING},
460         {HINT_SET, SetHintCreate, HINT_KEYWORD_SET},
461         {NULL, NULL, HINT_KEYWORD_UNRECOGNIZED}
462 };
463
464 /* search hint. */
465 static const char *search_query =
466         "SELECT hints "
467         "  FROM hint_plan.hints "
468         " WHERE norm_query_string = $1 "
469         "   AND ( application_name = $2 "
470         "    OR application_name = '' ) "
471         " ORDER BY application_name DESC";
472
473 /*
474  * Module load callbacks
475  */
476 void
477 _PG_init(void)
478 {
479         /* Define custom GUC variables. */
480         DefineCustomBoolVariable("pg_hint_plan.enable_hint",
481                          "Force planner to use plans specified in the hint comment preceding to the query.",
482                                                          NULL,
483                                                          &pg_hint_plan_enable_hint,
484                                                          true,
485                                                          PGC_USERSET,
486                                                          0,
487                                                          NULL,
488                                                          NULL,
489                                                          NULL);
490
491         DefineCustomBoolVariable("pg_hint_plan.debug_print",
492                                                          "Logs results of hint parsing.",
493                                                          NULL,
494                                                          &pg_hint_plan_debug_print,
495                                                          false,
496                                                          PGC_USERSET,
497                                                          0,
498                                                          NULL,
499                                                          NULL,
500                                                          NULL);
501
502         DefineCustomEnumVariable("pg_hint_plan.parse_messages",
503                                                          "Message level of parse errors.",
504                                                          NULL,
505                                                          &pg_hint_plan_parse_messages,
506                                                          INFO,
507                                                          parse_messages_level_options,
508                                                          PGC_USERSET,
509                                                          0,
510                                                          NULL,
511                                                          NULL,
512                                                          NULL);
513
514         /* Install hooks. */
515         prev_ProcessUtility = ProcessUtility_hook;
516         ProcessUtility_hook = pg_hint_plan_ProcessUtility;
517         prev_planner = planner_hook;
518         planner_hook = pg_hint_plan_planner;
519         prev_get_relation_info = get_relation_info_hook;
520         get_relation_info_hook = pg_hint_plan_get_relation_info;
521         prev_join_search = join_search_hook;
522         join_search_hook = pg_hint_plan_join_search;
523 }
524
525 /*
526  * Module unload callback
527  * XXX never called
528  */
529 void
530 _PG_fini(void)
531 {
532         /* Uninstall hooks. */
533         ProcessUtility_hook = prev_ProcessUtility;
534         planner_hook = prev_planner;
535         get_relation_info_hook = prev_get_relation_info;
536         join_search_hook = prev_join_search;
537 }
538
539 /*
540  * create and delete functions the hint object
541  */
542
543 static Hint *
544 ScanMethodHintCreate(const char *hint_str, const char *keyword,
545                                          HintKeyword hint_keyword)
546 {
547         ScanMethodHint *hint;
548
549         hint = palloc(sizeof(ScanMethodHint));
550         hint->base.hint_str = hint_str;
551         hint->base.keyword = keyword;
552         hint->base.hint_keyword = hint_keyword;
553         hint->base.type = HINT_TYPE_SCAN_METHOD;
554         hint->base.state = HINT_STATE_NOTUSED;
555         hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
556         hint->base.desc_func = (HintDescFunction) ScanMethodHintDesc;
557         hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
558         hint->base.parse_func = (HintParseFunction) ScanMethodHintParse;
559         hint->relname = NULL;
560         hint->indexnames = NIL;
561         hint->regexp = false;
562         hint->enforce_mask = 0;
563
564         return (Hint *) hint;
565 }
566
567 static void
568 ScanMethodHintDelete(ScanMethodHint *hint)
569 {
570         if (!hint)
571                 return;
572
573         if (hint->relname)
574                 pfree(hint->relname);
575         list_free_deep(hint->indexnames);
576         pfree(hint);
577 }
578
579 static Hint *
580 JoinMethodHintCreate(const char *hint_str, const char *keyword,
581                                          HintKeyword hint_keyword)
582 {
583         JoinMethodHint *hint;
584
585         hint = palloc(sizeof(JoinMethodHint));
586         hint->base.hint_str = hint_str;
587         hint->base.keyword = keyword;
588         hint->base.hint_keyword = hint_keyword;
589         hint->base.type = HINT_TYPE_JOIN_METHOD;
590         hint->base.state = HINT_STATE_NOTUSED;
591         hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
592         hint->base.desc_func = (HintDescFunction) JoinMethodHintDesc;
593         hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
594         hint->base.parse_func = (HintParseFunction) JoinMethodHintParse;
595         hint->nrels = 0;
596         hint->inner_nrels = 0;
597         hint->relnames = NULL;
598         hint->enforce_mask = 0;
599         hint->joinrelids = NULL;
600         hint->inner_joinrelids = NULL;
601
602         return (Hint *) hint;
603 }
604
605 static void
606 JoinMethodHintDelete(JoinMethodHint *hint)
607 {
608         if (!hint)
609                 return;
610
611         if (hint->relnames)
612         {
613                 int     i;
614
615                 for (i = 0; i < hint->nrels; i++)
616                         pfree(hint->relnames[i]);
617                 pfree(hint->relnames);
618         }
619
620         bms_free(hint->joinrelids);
621         bms_free(hint->inner_joinrelids);
622         pfree(hint);
623 }
624
625 static Hint *
626 LeadingHintCreate(const char *hint_str, const char *keyword,
627                                   HintKeyword hint_keyword)
628 {
629         LeadingHint        *hint;
630
631         hint = palloc(sizeof(LeadingHint));
632         hint->base.hint_str = hint_str;
633         hint->base.keyword = keyword;
634         hint->base.hint_keyword = hint_keyword;
635         hint->base.type = HINT_TYPE_LEADING;
636         hint->base.state = HINT_STATE_NOTUSED;
637         hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
638         hint->base.desc_func = (HintDescFunction) LeadingHintDesc;
639         hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
640         hint->base.parse_func = (HintParseFunction) LeadingHintParse;
641         hint->relations = NIL;
642         hint->outer_inner = NULL;
643
644         return (Hint *) hint;
645 }
646
647 static void
648 LeadingHintDelete(LeadingHint *hint)
649 {
650         if (!hint)
651                 return;
652
653         list_free_deep(hint->relations);
654         if (hint->outer_inner)
655                 pfree(hint->outer_inner);
656         pfree(hint);
657 }
658
659 static Hint *
660 SetHintCreate(const char *hint_str, const char *keyword,
661                           HintKeyword hint_keyword)
662 {
663         SetHint    *hint;
664
665         hint = palloc(sizeof(SetHint));
666         hint->base.hint_str = hint_str;
667         hint->base.keyword = keyword;
668         hint->base.hint_keyword = hint_keyword;
669         hint->base.type = HINT_TYPE_SET;
670         hint->base.state = HINT_STATE_NOTUSED;
671         hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
672         hint->base.desc_func = (HintDescFunction) SetHintDesc;
673         hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
674         hint->base.parse_func = (HintParseFunction) SetHintParse;
675         hint->name = NULL;
676         hint->value = NULL;
677         hint->words = NIL;
678
679         return (Hint *) hint;
680 }
681
682 static void
683 SetHintDelete(SetHint *hint)
684 {
685         if (!hint)
686                 return;
687
688         if (hint->name)
689                 pfree(hint->name);
690         if (hint->value)
691                 pfree(hint->value);
692         if (hint->words)
693                 list_free(hint->words);
694         pfree(hint);
695 }
696
697 static HintState *
698 HintStateCreate(void)
699 {
700         HintState   *hstate;
701
702         hstate = palloc(sizeof(HintState));
703         hstate->hint_str = NULL;
704         hstate->nall_hints = 0;
705         hstate->max_all_hints = 0;
706         hstate->all_hints = NULL;
707         memset(hstate->num_hints, 0, sizeof(hstate->num_hints));
708         hstate->scan_hints = NULL;
709         hstate->init_scan_mask = 0;
710         hstate->parent_relid = 0;
711         hstate->parent_rel_oid = InvalidOid;
712         hstate->parent_hint = NULL;
713         hstate->parent_index_infos = NIL;
714         hstate->join_hints = NULL;
715         hstate->init_join_mask = 0;
716         hstate->join_hint_level = NULL;
717         hstate->leading_hint = NULL;
718         hstate->context = superuser() ? PGC_SUSET : PGC_USERSET;
719         hstate->set_hints = NULL;
720
721         return hstate;
722 }
723
724 static void
725 HintStateDelete(HintState *hstate)
726 {
727         int                     i;
728
729         if (!hstate)
730                 return;
731
732         if (hstate->hint_str)
733                 pfree(hstate->hint_str);
734
735         for (i = 0; i < hstate->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
736                 hstate->all_hints[i]->delete_func(hstate->all_hints[i]);
737         if (hstate->all_hints)
738                 pfree(hstate->all_hints);
739         if (hstate->parent_index_infos)
740                 list_free(hstate->parent_index_infos);
741 }
742
743 /*
744  * Copy given value into buf, with quoting with '"' if necessary.
745  */
746 static void
747 quote_value(StringInfo buf, const char *value)
748 {
749         bool            need_quote = false;
750         const char *str;
751
752         for (str = value; *str != '\0'; str++)
753         {
754                 if (isspace(*str) || *str == '(' || *str == ')' || *str == '"')
755                 {
756                         need_quote = true;
757                         appendStringInfoCharMacro(buf, '"');
758                         break;
759                 }
760         }
761
762         for (str = value; *str != '\0'; str++)
763         {
764                 if (*str == '"')
765                         appendStringInfoCharMacro(buf, '"');
766
767                 appendStringInfoCharMacro(buf, *str);
768         }
769
770         if (need_quote)
771                 appendStringInfoCharMacro(buf, '"');
772 }
773
774 static void
775 ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf)
776 {
777         ListCell   *l;
778
779         appendStringInfo(buf, "%s(", hint->base.keyword);
780         if (hint->relname != NULL)
781         {
782                 quote_value(buf, hint->relname);
783                 foreach(l, hint->indexnames)
784                 {
785                         appendStringInfoCharMacro(buf, ' ');
786                         quote_value(buf, (char *) lfirst(l));
787                 }
788         }
789         appendStringInfoString(buf, ")\n");
790 }
791
792 static void
793 JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf)
794 {
795         int     i;
796
797         appendStringInfo(buf, "%s(", hint->base.keyword);
798         if (hint->relnames != NULL)
799         {
800                 quote_value(buf, hint->relnames[0]);
801                 for (i = 1; i < hint->nrels; i++)
802                 {
803                         appendStringInfoCharMacro(buf, ' ');
804                         quote_value(buf, hint->relnames[i]);
805                 }
806         }
807         appendStringInfoString(buf, ")\n");
808
809 }
810
811 static void
812 OuterInnerDesc(OuterInnerRels *outer_inner, StringInfo buf)
813 {
814         if (outer_inner->relation == NULL)
815         {
816                 bool            is_first;
817                 ListCell   *l;
818
819                 is_first = true;
820
821                 appendStringInfoCharMacro(buf, '(');
822                 foreach(l, outer_inner->outer_inner_pair)
823                 {
824                         if (is_first)
825                                 is_first = false;
826                         else
827                                 appendStringInfoCharMacro(buf, ' ');
828
829                         OuterInnerDesc(lfirst(l), buf);
830                 }
831
832                 appendStringInfoCharMacro(buf, ')');
833         }
834         else
835                 quote_value(buf, outer_inner->relation);
836 }
837
838 static void
839 LeadingHintDesc(LeadingHint *hint, StringInfo buf)
840 {
841         appendStringInfo(buf, "%s(", HINT_LEADING);
842         if (hint->outer_inner == NULL)
843         {
844                 ListCell   *l;
845                 bool            is_first;
846
847                 is_first = true;
848
849                 foreach(l, hint->relations)
850                 {
851                         if (is_first)
852                                 is_first = false;
853                         else
854                                 appendStringInfoCharMacro(buf, ' ');
855
856                         quote_value(buf, (char *) lfirst(l));
857                 }
858         }
859         else
860                 OuterInnerDesc(hint->outer_inner, buf);
861
862         appendStringInfoString(buf, ")\n");
863 }
864
865 static void
866 SetHintDesc(SetHint *hint, StringInfo buf)
867 {
868         bool            is_first = true;
869         ListCell   *l;
870
871         appendStringInfo(buf, "%s(", HINT_SET);
872         foreach(l, hint->words)
873         {
874                 if (is_first)
875                         is_first = false;
876                 else
877                         appendStringInfoCharMacro(buf, ' ');
878
879                 quote_value(buf, (char *) lfirst(l));
880         }
881         appendStringInfo(buf, ")\n");
882 }
883
884 /*
885  * Append string which repserents all hints in a given state to buf, with
886  * preceding title with them.
887  */
888 static void
889 desc_hint_in_state(HintState *hstate, StringInfo buf, const char *title,
890                                         HintStatus state)
891 {
892         int     i;
893
894         appendStringInfo(buf, "%s:\n", title);
895         for (i = 0; i < hstate->nall_hints; i++)
896         {
897                 if (hstate->all_hints[i]->state != state)
898                         continue;
899
900                 hstate->all_hints[i]->desc_func(hstate->all_hints[i], buf);
901         }
902 }
903
904 /*
905  * Dump contents of given hstate to server log with log level LOG.
906  */
907 static void
908 HintStateDump(HintState *hstate)
909 {
910         StringInfoData  buf;
911
912         if (!hstate)
913         {
914                 elog(LOG, "pg_hint_plan:\nno hint");
915                 return;
916         }
917
918         initStringInfo(&buf);
919
920         appendStringInfoString(&buf, "pg_hint_plan:\n");
921         desc_hint_in_state(hstate, &buf, "used hint", HINT_STATE_USED);
922         desc_hint_in_state(hstate, &buf, "not used hint", HINT_STATE_NOTUSED);
923         desc_hint_in_state(hstate, &buf, "duplication hint", HINT_STATE_DUPLICATION);
924         desc_hint_in_state(hstate, &buf, "error hint", HINT_STATE_ERROR);
925
926         elog(LOG, "%s", buf.data);
927
928         pfree(buf.data);
929 }
930
931 /*
932  * compare functions
933  */
934
935 static int
936 RelnameCmp(const void *a, const void *b)
937 {
938         const char *relnamea = *((const char **) a);
939         const char *relnameb = *((const char **) b);
940
941         return strcmp(relnamea, relnameb);
942 }
943
944 static int
945 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
946 {
947         return RelnameCmp(&a->relname, &b->relname);
948 }
949
950 static int
951 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
952 {
953         int     i;
954
955         if (a->nrels != b->nrels)
956                 return a->nrels - b->nrels;
957
958         for (i = 0; i < a->nrels; i++)
959         {
960                 int     result;
961                 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
962                         return result;
963         }
964
965         return 0;
966 }
967
968 static int
969 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
970 {
971         return 0;
972 }
973
974 static int
975 SetHintCmp(const SetHint *a, const SetHint *b)
976 {
977         return strcmp(a->name, b->name);
978 }
979
980 static int
981 HintCmp(const void *a, const void *b)
982 {
983         const Hint *hinta = *((const Hint **) a);
984         const Hint *hintb = *((const Hint **) b);
985
986         if (hinta->type != hintb->type)
987                 return hinta->type - hintb->type;
988         if (hinta->state == HINT_STATE_ERROR)
989                 return -1;
990         if (hintb->state == HINT_STATE_ERROR)
991                 return 1;
992         return hinta->cmp_func(hinta, hintb);
993 }
994
995 /*
996  * Returns byte offset of hint b from hint a.  If hint a was specified before
997  * b, positive value is returned.
998  */
999 static int
1000 HintCmpWithPos(const void *a, const void *b)
1001 {
1002         const Hint *hinta = *((const Hint **) a);
1003         const Hint *hintb = *((const Hint **) b);
1004         int             result;
1005
1006         result = HintCmp(a, b);
1007         if (result == 0)
1008                 result = hinta->hint_str - hintb->hint_str;
1009
1010         return result;
1011 }
1012
1013 /*
1014  * parse functions
1015  */
1016 static const char *
1017 parse_keyword(const char *str, StringInfo buf)
1018 {
1019         skip_space(str);
1020
1021         while (!isspace(*str) && *str != '(' && *str != '\0')
1022                 appendStringInfoCharMacro(buf, *str++);
1023
1024         return str;
1025 }
1026
1027 static const char *
1028 skip_parenthesis(const char *str, char parenthesis)
1029 {
1030         skip_space(str);
1031
1032         if (*str != parenthesis)
1033         {
1034                 if (parenthesis == '(')
1035                         hint_ereport(str, ("Opening parenthesis is necessary."));
1036                 else if (parenthesis == ')')
1037                         hint_ereport(str, ("Closing parenthesis is necessary."));
1038
1039                 return NULL;
1040         }
1041
1042         str++;
1043
1044         return str;
1045 }
1046
1047 /*
1048  * Parse a token from str, and store malloc'd copy into word.  A token can be
1049  * quoted with '"'.  Return value is pointer to unparsed portion of original
1050  * string, or NULL if an error occurred.
1051  *
1052  * Parsed token is truncated within NAMEDATALEN-1 bytes, when truncate is true.
1053  */
1054 static const char *
1055 parse_quoted_value(const char *str, char **word, bool truncate)
1056 {
1057         StringInfoData  buf;
1058         bool                    in_quote;
1059
1060         /* Skip leading spaces. */
1061         skip_space(str);
1062
1063         initStringInfo(&buf);
1064         if (*str == '"')
1065         {
1066                 str++;
1067                 in_quote = true;
1068         }
1069         else
1070                 in_quote = false;
1071
1072         while (true)
1073         {
1074                 if (in_quote)
1075                 {
1076                         /* Double quotation must be closed. */
1077                         if (*str == '\0')
1078                         {
1079                                 pfree(buf.data);
1080                                 hint_ereport(str, ("Unterminated quoted string."));
1081                                 return NULL;
1082                         }
1083
1084                         /*
1085                          * Skip escaped double quotation.
1086                          *
1087                          * We don't allow slash-asterisk and asterisk-slash (delimiters of
1088                          * block comments) to be an object name, so users must specify
1089                          * alias for such object names.
1090                          *
1091                          * Those special names can be allowed if we care escaped slashes
1092                          * and asterisks, but we don't.
1093                          */
1094                         if (*str == '"')
1095                         {
1096                                 str++;
1097                                 if (*str != '"')
1098                                         break;
1099                         }
1100                 }
1101                 else if (isspace(*str) || *str == '(' || *str == ')' || *str == '"' ||
1102                                  *str == '\0')
1103                         break;
1104
1105                 appendStringInfoCharMacro(&buf, *str++);
1106         }
1107
1108         if (buf.len == 0)
1109         {
1110                 hint_ereport(str, ("Zero-length delimited string."));
1111
1112                 pfree(buf.data);
1113
1114                 return NULL;
1115         }
1116
1117         /* Truncate name if it's too long */
1118         if (truncate)
1119                 truncate_identifier(buf.data, strlen(buf.data), true);
1120
1121         *word = buf.data;
1122
1123         return str;
1124 }
1125
1126 static OuterInnerRels *
1127 OuterInnerRelsCreate(char *name, List *outer_inner_list)
1128 {
1129         OuterInnerRels *outer_inner;
1130
1131         outer_inner = palloc(sizeof(OuterInnerRels));
1132         outer_inner->relation = name;
1133         outer_inner->outer_inner_pair = outer_inner_list;
1134
1135         return outer_inner;
1136 }
1137
1138 static const char *
1139 parse_parentheses_Leading_in(const char *str, OuterInnerRels **outer_inner)
1140 {
1141         List   *outer_inner_pair = NIL;
1142
1143         if ((str = skip_parenthesis(str, '(')) == NULL)
1144                 return NULL;
1145
1146         skip_space(str);
1147
1148         /* Store words in parentheses into outer_inner_list. */
1149         while(*str != ')' && *str != '\0')
1150         {
1151                 OuterInnerRels *outer_inner_rels;
1152
1153                 if (*str == '(')
1154                 {
1155                         str = parse_parentheses_Leading_in(str, &outer_inner_rels);
1156                         if (str == NULL)
1157                                 break;
1158                 }
1159                 else
1160                 {
1161                         char   *name;
1162
1163                         if ((str = parse_quoted_value(str, &name, true)) == NULL)
1164                                 break;
1165                         else
1166                                 outer_inner_rels = OuterInnerRelsCreate(name, NIL);
1167                 }
1168
1169                 outer_inner_pair = lappend(outer_inner_pair, outer_inner_rels);
1170                 skip_space(str);
1171         }
1172
1173         if (str == NULL ||
1174                 (str = skip_parenthesis(str, ')')) == NULL)
1175         {
1176                 list_free(outer_inner_pair);
1177                 return NULL;
1178         }
1179
1180         *outer_inner = OuterInnerRelsCreate(NULL, outer_inner_pair);
1181
1182         return str;
1183 }
1184
1185 static const char *
1186 parse_parentheses_Leading(const char *str, List **name_list,
1187         OuterInnerRels **outer_inner)
1188 {
1189         char   *name;
1190         bool    truncate = true;
1191
1192         if ((str = skip_parenthesis(str, '(')) == NULL)
1193                 return NULL;
1194
1195         skip_space(str);
1196         if (*str =='(')
1197         {
1198                 if ((str = parse_parentheses_Leading_in(str, outer_inner)) == NULL)
1199                         return NULL;
1200         }
1201         else
1202         {
1203                 /* Store words in parentheses into name_list. */
1204                 while(*str != ')' && *str != '\0')
1205                 {
1206                         if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1207                         {
1208                                 list_free(*name_list);
1209                                 return NULL;
1210                         }
1211
1212                         *name_list = lappend(*name_list, name);
1213                         skip_space(str);
1214                 }
1215         }
1216
1217         if ((str = skip_parenthesis(str, ')')) == NULL)
1218                 return NULL;
1219         return str;
1220 }
1221
1222 static const char *
1223 parse_parentheses(const char *str, List **name_list, HintKeyword keyword)
1224 {
1225         char   *name;
1226         bool    truncate = true;
1227
1228         if ((str = skip_parenthesis(str, '(')) == NULL)
1229                 return NULL;
1230
1231         skip_space(str);
1232
1233         /* Store words in parentheses into name_list. */
1234         while(*str != ')' && *str != '\0')
1235         {
1236                 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1237                 {
1238                         list_free(*name_list);
1239                         return NULL;
1240                 }
1241
1242                 *name_list = lappend(*name_list, name);
1243                 skip_space(str);
1244
1245                 if (keyword == HINT_KEYWORD_INDEXSCANREGEXP ||
1246 #if PG_VERSION_NUM >= 90200
1247                         keyword == HINT_KEYWORD_INDEXONLYSCANREGEXP ||
1248 #endif
1249                         keyword == HINT_KEYWORD_BITMAPSCANREGEXP ||
1250                         keyword == HINT_KEYWORD_SET)
1251                 {
1252                         truncate = false;
1253                 }
1254         }
1255
1256         if ((str = skip_parenthesis(str, ')')) == NULL)
1257                 return NULL;
1258         return str;
1259 }
1260
1261 static void
1262 parse_hints(HintState *hstate, Query *parse, const char *str)
1263 {
1264         StringInfoData  buf;
1265         char               *head;
1266
1267         initStringInfo(&buf);
1268         while (*str != '\0')
1269         {
1270                 const HintParser *parser;
1271
1272                 /* in error message, we output the comment including the keyword. */
1273                 head = (char *) str;
1274
1275                 /* parse only the keyword of the hint. */
1276                 resetStringInfo(&buf);
1277                 str = parse_keyword(str, &buf);
1278
1279                 for (parser = parsers; parser->keyword != NULL; parser++)
1280                 {
1281                         char   *keyword = parser->keyword;
1282                         Hint   *hint;
1283
1284                         if (strcasecmp(buf.data, keyword) != 0)
1285                                 continue;
1286
1287                         hint = parser->create_func(head, keyword, parser->hint_keyword);
1288
1289                         /* parser of each hint does parse in a parenthesis. */
1290                         if ((str = hint->parse_func(hint, hstate, parse, str)) == NULL)
1291                         {
1292                                 hint->delete_func(hint);
1293                                 pfree(buf.data);
1294                                 return;
1295                         }
1296
1297                         /*
1298                          * Add hint information into all_hints array.  If we don't have
1299                          * enough space, double the array.
1300                          */
1301                         if (hstate->nall_hints == 0)
1302                         {
1303                                 hstate->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
1304                                 hstate->all_hints = (Hint **)
1305                                         palloc(sizeof(Hint *) * hstate->max_all_hints);
1306                         }
1307                         else if (hstate->nall_hints == hstate->max_all_hints)
1308                         {
1309                                 hstate->max_all_hints *= 2;
1310                                 hstate->all_hints = (Hint **)
1311                                         repalloc(hstate->all_hints,
1312                                                          sizeof(Hint *) * hstate->max_all_hints);
1313                         }
1314
1315                         hstate->all_hints[hstate->nall_hints] = hint;
1316                         hstate->nall_hints++;
1317
1318                         skip_space(str);
1319
1320                         break;
1321                 }
1322
1323                 if (parser->keyword == NULL)
1324                 {
1325                         hint_ereport(head,
1326                                                  ("Unrecognized hint keyword \"%s\".", buf.data));
1327                         pfree(buf.data);
1328                         return;
1329                 }
1330         }
1331
1332         pfree(buf.data);
1333 }
1334
1335
1336 /* search hint. */
1337 static bool
1338 search_hints(
1339                          const char *query,
1340                          const char *query_string,
1341                          const char *app_name)
1342 {
1343         static SPIPlanPtr plan = NULL;
1344         int             ret;
1345         char    buf[8192];
1346         Oid             argtypes[2] = { TEXTOID, TEXTOID };
1347         Datum   values[2];
1348         bool    nulls[2] = { false, false };
1349         text   *str;
1350         text   *app;
1351
1352         ret = SPI_connect();
1353         if (ret != SPI_OK_CONNECT)
1354                 elog(ERROR, "pg_hint_plan: SPI_connect => %d", ret);
1355
1356         if (plan == NULL)
1357         {
1358                 SPIPlanPtr      p;
1359                 p = SPI_prepare(query, 2, argtypes);
1360                 if (p == NULL)
1361                         elog(ERROR, "pg_hint_plan: SPI_prepare => %d", SPI_result);
1362                 plan = SPI_saveplan(p);
1363                 SPI_freeplan(p);
1364         }
1365
1366         str = cstring_to_text(query_string);
1367         app = cstring_to_text(app_name);
1368         values[0] = PointerGetDatum(str);
1369         values[1] = PointerGetDatum(app);
1370
1371         pg_hint_plan_enable_hint = false;
1372         ret = SPI_execute_plan(plan, values, nulls, true, 1);
1373         pg_hint_plan_enable_hint = true;
1374         if (ret != SPI_OK_SELECT)
1375                 elog(ERROR, "pg_hint_plan: SPI_execute_plan => %d", ret);
1376
1377         if (SPI_processed > 0)
1378         {
1379                 sprintf(buf, "%s", SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1));
1380                 elog(LOG, "pg_hint_plan: SPI_execute_plan vals => %s", buf);
1381         }
1382         else
1383                 elog(LOG, "pg_hint_plan: SPI_execute_plan rows => 0");
1384         
1385         SPI_finish();
1386
1387         return SPI_processed > 0;
1388 }
1389
1390 /*
1391  * Do basic parsing of the query head comment.
1392  */
1393 static HintState *
1394 parse_head_comment(Query *parse)
1395 {
1396         const char *p;
1397         const char *hint_head;
1398         char       *head;
1399         char       *tail;
1400         int                     len;
1401         int                     i;
1402         HintState   *hstate;
1403
1404         /* get client-supplied query string. */
1405         if (stmt_name)
1406         {
1407                 PreparedStatement  *entry;
1408
1409                 entry = FetchPreparedStatement(stmt_name, true);
1410                 p = entry->plansource->query_string;
1411         }
1412         else
1413                 p = debug_query_string;
1414
1415         if (p == NULL)
1416                 return NULL;
1417
1418         /* extract query head comment. */
1419         hint_head = strstr(p, HINT_START);
1420         if (hint_head == NULL)
1421                 return NULL;
1422         for (;p < hint_head; p++)
1423         {
1424                 /*
1425                  * Allow these characters precedes hint comment:
1426                  *   - digits
1427                  *   - alphabets which are in ASCII range
1428                  *   - space, tabs and new-lines
1429                  *   - underscores, for identifier
1430                  *   - commas, for SELECT clause, EXPLAIN and PREPARE
1431                  *   - parentheses, for EXPLAIN and PREPARE
1432                  *
1433                  * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1434                  * avoid behavior which depends on locale setting.
1435                  */
1436                 if (!(*p >= '0' && *p <= '9') &&
1437                         !(*p >= 'A' && *p <= 'Z') &&
1438                         !(*p >= 'a' && *p <= 'z') &&
1439                         !isspace(*p) &&
1440                         *p != '_' &&
1441                         *p != ',' &&
1442                         *p != '(' && *p != ')')
1443                         return NULL;
1444         }
1445
1446         len = strlen(HINT_START);
1447         head = (char *) p;
1448         p += len;
1449         skip_space(p);
1450
1451         /* find hint end keyword. */
1452         if ((tail = strstr(p, HINT_END)) == NULL)
1453         {
1454                 hint_ereport(head, ("Unterminated block comment."));
1455                 return NULL;
1456         }
1457
1458         /* We don't support nested block comments. */
1459         if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1460         {
1461                 hint_ereport(head, ("Nested block comments are not supported."));
1462                 return NULL;
1463         }
1464
1465         /* Make a copy of hint. */
1466         len = tail - p;
1467         head = palloc(len + 1);
1468         memcpy(head, p, len);
1469         head[len] = '\0';
1470         p = head;
1471
1472         hstate = HintStateCreate();
1473         hstate->hint_str = head;
1474
1475         /* parse each hint. */
1476         parse_hints(hstate, parse, p);
1477
1478         /* When nothing specified a hint, we free HintState and returns NULL. */
1479         if (hstate->nall_hints == 0)
1480         {
1481                 HintStateDelete(hstate);
1482                 return NULL;
1483         }
1484
1485         /* Sort hints in order of original position. */
1486         qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
1487                   HintCmpWithPos);
1488
1489         /* Count number of hints per hint-type. */
1490         for (i = 0; i < hstate->nall_hints; i++)
1491         {
1492                 Hint   *cur_hint = hstate->all_hints[i];
1493                 hstate->num_hints[cur_hint->type]++;
1494         }
1495
1496         /*
1497          * If an object (or a set of objects) has multiple hints of same hint-type,
1498          * only the last hint is valid and others are igonred in planning.
1499          * Hints except the last are marked as 'duplicated' to remember the order.
1500          */
1501         for (i = 0; i < hstate->nall_hints - 1; i++)
1502         {
1503                 Hint   *cur_hint = hstate->all_hints[i];
1504                 Hint   *next_hint = hstate->all_hints[i + 1];
1505
1506                 /*
1507                  * Leading hint is marked as 'duplicated' in transform_join_hints.
1508                  */
1509                 if (cur_hint->type == HINT_TYPE_LEADING &&
1510                         next_hint->type == HINT_TYPE_LEADING)
1511                         continue;
1512
1513                 /*
1514                  * Note that we need to pass addresses of hint pointers, because
1515                  * HintCmp is designed to sort array of Hint* by qsort.
1516                  */
1517                 if (HintCmp(&cur_hint, &next_hint) == 0)
1518                 {
1519                         hint_ereport(cur_hint->hint_str,
1520                                                  ("Conflict %s hint.", HintTypeName[cur_hint->type]));
1521                         cur_hint->state = HINT_STATE_DUPLICATION;
1522                 }
1523         }
1524
1525         /*
1526          * Make sure that per-type array pointers point proper position in the
1527          * array which consists of all hints.
1528          */
1529         hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
1530         hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
1531                 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
1532         hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
1533                 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
1534         hstate->set_hints = (SetHint **) (hstate->leading_hint +
1535                 hstate->num_hints[HINT_TYPE_LEADING]);
1536
1537         return hstate;
1538 }
1539
1540 /*
1541  * Parse inside of parentheses of scan-method hints.
1542  */
1543 static const char *
1544 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
1545                                         const char *str)
1546 {
1547         const char         *keyword = hint->base.keyword;
1548         HintKeyword             hint_keyword = hint->base.hint_keyword;
1549         List               *name_list = NIL;
1550         int                             length;
1551
1552         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
1553                 return NULL;
1554
1555         /* Parse relation name and index name(s) if given hint accepts. */
1556         length = list_length(name_list);
1557         if (length > 0)
1558         {
1559                 hint->relname = linitial(name_list);
1560                 hint->indexnames = list_delete_first(name_list);
1561
1562                 /* check whether the hint accepts index name(s). */
1563                 if (length != 1 &&
1564                         hint_keyword != HINT_KEYWORD_INDEXSCAN &&
1565                         hint_keyword != HINT_KEYWORD_INDEXSCANREGEXP &&
1566 #if PG_VERSION_NUM >= 90200
1567                         hint_keyword != HINT_KEYWORD_INDEXONLYSCAN &&
1568                         hint_keyword != HINT_KEYWORD_INDEXONLYSCANREGEXP &&
1569 #endif
1570                         hint_keyword != HINT_KEYWORD_BITMAPSCAN &&
1571                         hint_keyword != HINT_KEYWORD_BITMAPSCANREGEXP)
1572                 {
1573                         hint_ereport(str,
1574                                                  ("%s hint accepts only one relation.",
1575                                                   hint->base.keyword));
1576                         hint->base.state = HINT_STATE_ERROR;
1577                         return str;
1578                 }
1579         }
1580         else
1581         {
1582                 hint_ereport(str,
1583                                          ("%s hint requires a relation.",
1584                                           hint->base.keyword));
1585                 hint->base.state = HINT_STATE_ERROR;
1586                 return str;
1587         }
1588
1589         /* Set a bit for specified hint. */
1590         switch (hint_keyword)
1591         {
1592                 case HINT_KEYWORD_SEQSCAN:
1593                         hint->enforce_mask = ENABLE_SEQSCAN;
1594                         break;
1595                 case HINT_KEYWORD_INDEXSCAN:
1596                         hint->enforce_mask = ENABLE_INDEXSCAN;
1597                         break;
1598                 case HINT_KEYWORD_INDEXSCANREGEXP:
1599                         hint->enforce_mask = ENABLE_INDEXSCAN;
1600                         hint->regexp = true;
1601                         break;
1602                 case HINT_KEYWORD_BITMAPSCAN:
1603                         hint->enforce_mask = ENABLE_BITMAPSCAN;
1604                         break;
1605                 case HINT_KEYWORD_BITMAPSCANREGEXP:
1606                         hint->enforce_mask = ENABLE_BITMAPSCAN;
1607                         hint->regexp = true;
1608                         break;
1609                 case HINT_KEYWORD_TIDSCAN:
1610                         hint->enforce_mask = ENABLE_TIDSCAN;
1611                         break;
1612                 case HINT_KEYWORD_NOSEQSCAN:
1613                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
1614                         break;
1615                 case HINT_KEYWORD_NOINDEXSCAN:
1616                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
1617                         break;
1618                 case HINT_KEYWORD_NOBITMAPSCAN:
1619                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
1620                         break;
1621                 case HINT_KEYWORD_NOTIDSCAN:
1622                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
1623                         break;
1624 #if PG_VERSION_NUM >= 90200
1625                 case HINT_KEYWORD_INDEXONLYSCAN:
1626                         hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
1627                         break;
1628                 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
1629                         hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
1630                         hint->regexp = true;
1631                         break;
1632                 case HINT_KEYWORD_NOINDEXONLYSCAN:
1633                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
1634                         break;
1635 #endif
1636                 default:
1637                         hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1638                         return NULL;
1639                         break;
1640         }
1641
1642         return str;
1643 }
1644
1645 static const char *
1646 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
1647                                         const char *str)
1648 {
1649         const char         *keyword = hint->base.keyword;
1650         HintKeyword             hint_keyword = hint->base.hint_keyword;
1651         List               *name_list = NIL;
1652
1653         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
1654                 return NULL;
1655
1656         hint->nrels = list_length(name_list);
1657
1658         if (hint->nrels > 0)
1659         {
1660                 ListCell   *l;
1661                 int                     i = 0;
1662
1663                 /*
1664                  * Transform relation names from list to array to sort them with qsort
1665                  * after.
1666                  */
1667                 hint->relnames = palloc(sizeof(char *) * hint->nrels);
1668                 foreach (l, name_list)
1669                 {
1670                         hint->relnames[i] = lfirst(l);
1671                         i++;
1672                 }
1673         }
1674
1675         list_free(name_list);
1676
1677         /* A join hint requires at least two relations */
1678         if (hint->nrels < 2)
1679         {
1680                 hint_ereport(str,
1681                                          ("%s hint requires at least two relations.",
1682                                           hint->base.keyword));
1683                 hint->base.state = HINT_STATE_ERROR;
1684                 return str;
1685         }
1686
1687         /* Sort hints in alphabetical order of relation names. */
1688         qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
1689
1690         switch (hint_keyword)
1691         {
1692                 case HINT_KEYWORD_NESTLOOP:
1693                         hint->enforce_mask = ENABLE_NESTLOOP;
1694                         break;
1695                 case HINT_KEYWORD_MERGEJOIN:
1696                         hint->enforce_mask = ENABLE_MERGEJOIN;
1697                         break;
1698                 case HINT_KEYWORD_HASHJOIN:
1699                         hint->enforce_mask = ENABLE_HASHJOIN;
1700                         break;
1701                 case HINT_KEYWORD_NONESTLOOP:
1702                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
1703                         break;
1704                 case HINT_KEYWORD_NOMERGEJOIN:
1705                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
1706                         break;
1707                 case HINT_KEYWORD_NOHASHJOIN:
1708                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
1709                         break;
1710                 default:
1711                         hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1712                         return NULL;
1713                         break;
1714         }
1715
1716         return str;
1717 }
1718
1719 static bool
1720 OuterInnerPairCheck(OuterInnerRels *outer_inner)
1721 {
1722         ListCell *l;
1723         if (outer_inner->outer_inner_pair == NIL)
1724         {
1725                 if (outer_inner->relation)
1726                         return true;
1727                 else
1728                         return false;
1729         }
1730
1731         if (list_length(outer_inner->outer_inner_pair) == 2)
1732         {
1733                 foreach(l, outer_inner->outer_inner_pair)
1734                 {
1735                         if (!OuterInnerPairCheck(lfirst(l)))
1736                                 return false;
1737                 }
1738         }
1739         else
1740                 return false;
1741
1742         return true;
1743 }
1744
1745 static List *
1746 OuterInnerList(OuterInnerRels *outer_inner)
1747 {
1748         List               *outer_inner_list = NIL;
1749         ListCell           *l;
1750         OuterInnerRels *outer_inner_rels;
1751
1752         foreach(l, outer_inner->outer_inner_pair)
1753         {
1754                 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
1755
1756                 if (outer_inner_rels->relation != NULL)
1757                         outer_inner_list = lappend(outer_inner_list,
1758                                                                            outer_inner_rels->relation);
1759                 else
1760                         outer_inner_list = list_concat(outer_inner_list,
1761                                                                                    OuterInnerList(outer_inner_rels));
1762         }
1763         return outer_inner_list;
1764 }
1765
1766 static const char *
1767 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
1768                                  const char *str)
1769 {
1770         List               *name_list = NIL;
1771         OuterInnerRels *outer_inner = NULL;
1772
1773         if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
1774                 NULL)
1775                 return NULL;
1776
1777         if (outer_inner != NULL)
1778                 name_list = OuterInnerList(outer_inner);
1779
1780         hint->relations = name_list;
1781         hint->outer_inner = outer_inner;
1782
1783         /* A Leading hint requires at least two relations */
1784         if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
1785         {
1786                 hint_ereport(hint->base.hint_str,
1787                                          ("%s hint requires at least two relations.",
1788                                           HINT_LEADING));
1789                 hint->base.state = HINT_STATE_ERROR;
1790         }
1791         else if (hint->outer_inner != NULL &&
1792                          !OuterInnerPairCheck(hint->outer_inner))
1793         {
1794                 hint_ereport(hint->base.hint_str,
1795                                          ("%s hint requires two sets of relations when parentheses nests.",
1796                                           HINT_LEADING));
1797                 hint->base.state = HINT_STATE_ERROR;
1798         }
1799
1800         return str;
1801 }
1802
1803 static const char *
1804 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
1805 {
1806         List   *name_list = NIL;
1807
1808         if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
1809                 == NULL)
1810                 return NULL;
1811
1812         hint->words = name_list;
1813
1814         /* We need both name and value to set GUC parameter. */
1815         if (list_length(name_list) == 2)
1816         {
1817                 hint->name = linitial(name_list);
1818                 hint->value = lsecond(name_list);
1819         }
1820         else
1821         {
1822                 hint_ereport(hint->base.hint_str,
1823                                          ("%s hint requires name and value of GUC parameter.",
1824                                           HINT_SET));
1825                 hint->base.state = HINT_STATE_ERROR;
1826         }
1827
1828         return str;
1829 }
1830
1831 /*
1832  * set GUC parameter functions
1833  */
1834
1835 static int
1836 set_config_option_wrapper(const char *name, const char *value,
1837                                                   GucContext context, GucSource source,
1838                                                   GucAction action, bool changeVal, int elevel)
1839 {
1840         int                             result = 0;
1841         MemoryContext   ccxt = CurrentMemoryContext;
1842
1843         PG_TRY();
1844         {
1845 #if PG_VERSION_NUM >= 90200
1846                 result = set_config_option(name, value, context, source,
1847                                                                    action, changeVal, 0);
1848 #else
1849                 result = set_config_option(name, value, context, source,
1850                                                                    action, changeVal);
1851 #endif
1852         }
1853         PG_CATCH();
1854         {
1855                 ErrorData          *errdata;
1856
1857                 /* Save error info */
1858                 MemoryContextSwitchTo(ccxt);
1859                 errdata = CopyErrorData();
1860                 FlushErrorState();
1861
1862                 ereport(elevel, (errcode(errdata->sqlerrcode),
1863                                 errmsg("%s", errdata->message),
1864                                 errdata->detail ? errdetail("%s", errdata->detail) : 0,
1865                                 errdata->hint ? errhint("%s", errdata->hint) : 0));
1866                 FreeErrorData(errdata);
1867         }
1868         PG_END_TRY();
1869
1870         return result;
1871 }
1872
1873 static int
1874 set_config_options(SetHint **options, int noptions, GucContext context)
1875 {
1876         int     i;
1877         int     save_nestlevel;
1878
1879         save_nestlevel = NewGUCNestLevel();
1880
1881         for (i = 0; i < noptions; i++)
1882         {
1883                 SetHint    *hint = options[i];
1884                 int                     result;
1885
1886                 if (!hint_state_enabled(hint))
1887                         continue;
1888
1889                 result = set_config_option_wrapper(hint->name, hint->value, context,
1890                                                                                    PGC_S_SESSION, GUC_ACTION_SAVE, true,
1891                                                                                    pg_hint_plan_parse_messages);
1892                 if (result != 0)
1893                         hint->base.state = HINT_STATE_USED;
1894                 else
1895                         hint->base.state = HINT_STATE_ERROR;
1896         }
1897
1898         return save_nestlevel;
1899 }
1900
1901 #define SET_CONFIG_OPTION(name, type_bits) \
1902         set_config_option_wrapper((name), \
1903                 (mask & (type_bits)) ? "true" : "false", \
1904                 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
1905
1906 static void
1907 set_scan_config_options(unsigned char enforce_mask, GucContext context)
1908 {
1909         unsigned char   mask;
1910
1911         if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
1912                 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
1913 #if PG_VERSION_NUM >= 90200
1914                 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
1915 #endif
1916                 )
1917                 mask = enforce_mask;
1918         else
1919                 mask = enforce_mask & current_hint->init_scan_mask;
1920
1921         SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
1922         SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
1923         SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
1924         SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
1925 #if PG_VERSION_NUM >= 90200
1926         SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
1927 #endif
1928 }
1929
1930 static void
1931 set_join_config_options(unsigned char enforce_mask, GucContext context)
1932 {
1933         unsigned char   mask;
1934
1935         if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
1936                 enforce_mask == ENABLE_HASHJOIN)
1937                 mask = enforce_mask;
1938         else
1939                 mask = enforce_mask & current_hint->init_join_mask;
1940
1941         SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
1942         SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
1943         SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
1944 }
1945
1946 /*
1947  * pg_hint_plan hook functions
1948  */
1949
1950 static void
1951 pg_hint_plan_ProcessUtility(Node *parsetree, const char *queryString,
1952                                                         ParamListInfo params, bool isTopLevel,
1953                                                         DestReceiver *dest, char *completionTag)
1954 {
1955         Node                               *node;
1956
1957         if (!pg_hint_plan_enable_hint)
1958         {
1959                 if (prev_ProcessUtility)
1960                         (*prev_ProcessUtility) (parsetree, queryString, params,
1961                                                                         isTopLevel, dest, completionTag);
1962                 else
1963                         standard_ProcessUtility(parsetree, queryString, params,
1964                                                                         isTopLevel, dest, completionTag);
1965
1966                 return;
1967         }
1968
1969         node = parsetree;
1970         if (IsA(node, ExplainStmt))
1971         {
1972                 /*
1973                  * Draw out parse tree of actual query from Query struct of EXPLAIN
1974                  * statement.
1975                  */
1976                 ExplainStmt        *stmt;
1977                 Query              *query;
1978
1979                 stmt = (ExplainStmt *) node;
1980
1981                 Assert(IsA(stmt->query, Query));
1982                 query = (Query *) stmt->query;
1983
1984                 if (query->commandType == CMD_UTILITY && query->utilityStmt != NULL)
1985                         node = query->utilityStmt;
1986         }
1987
1988         /*
1989          * If the query was a EXECUTE or CREATE TABLE AS EXECUTE, get query string
1990          * specified to preceding PREPARE command to use it as source of hints.
1991          */
1992         if (IsA(node, ExecuteStmt))
1993         {
1994                 ExecuteStmt        *stmt;
1995
1996                 stmt = (ExecuteStmt *) node;
1997                 stmt_name = stmt->name;
1998         }
1999 #if PG_VERSION_NUM >= 90200
2000         /*
2001          * CREATE AS EXECUTE behavior has changed since 9.2, so we must handle it
2002          * specially here.
2003          */
2004         if (IsA(node, CreateTableAsStmt))
2005         {
2006                 CreateTableAsStmt          *stmt;
2007                 Query              *query;
2008
2009                 stmt = (CreateTableAsStmt *) node;
2010                 Assert(IsA(stmt->query, Query));
2011                 query = (Query *) stmt->query;
2012
2013                 if (query->commandType == CMD_UTILITY &&
2014                         IsA(query->utilityStmt, ExecuteStmt))
2015                 {
2016                         ExecuteStmt *estmt = (ExecuteStmt *) query->utilityStmt;
2017                         stmt_name = estmt->name;
2018                 }
2019         }
2020 #endif
2021         if (stmt_name)
2022         {
2023                 PG_TRY();
2024                 {
2025                         if (prev_ProcessUtility)
2026                                 (*prev_ProcessUtility) (parsetree, queryString, params,
2027                                                                                 isTopLevel, dest, completionTag);
2028                         else
2029                                 standard_ProcessUtility(parsetree, queryString, params,
2030                                                                                 isTopLevel, dest, completionTag);
2031                 }
2032                 PG_CATCH();
2033                 {
2034                         stmt_name = NULL;
2035                         PG_RE_THROW();
2036                 }
2037                 PG_END_TRY();
2038
2039                 stmt_name = NULL;
2040
2041                 return;
2042         }
2043
2044         if (prev_ProcessUtility)
2045                 (*prev_ProcessUtility) (parsetree, queryString, params,
2046                                                                 isTopLevel, dest, completionTag);
2047         else
2048                 standard_ProcessUtility(parsetree, queryString, params,
2049                                                                 isTopLevel, dest, completionTag);
2050 }
2051
2052 /*
2053  * Push a hint into hint stack which is implemented with List struct.  Head of
2054  * list is top of stack.
2055  */
2056 static void
2057 push_hint(HintState *hstate)
2058 {
2059         /* Prepend new hint to the list means pushing to stack. */
2060         HintStateStack = lcons(hstate, HintStateStack);
2061
2062         /* Pushed hint is the one which should be used hereafter. */
2063         current_hint = hstate;
2064 }
2065
2066 /* Pop a hint from hint stack.  Popped hint is automatically discarded. */
2067 static void
2068 pop_hint(void)
2069 {
2070         /* Hint stack must not be empty. */
2071         if(HintStateStack == NIL)
2072                 elog(ERROR, "hint stack is empty");
2073
2074         /*
2075          * Take a hint at the head from the list, and free it.  Switch current_hint
2076          * to point new head (NULL if the list is empty).
2077          */
2078         HintStateStack = list_delete_first(HintStateStack);
2079         HintStateDelete(current_hint);
2080         if(HintStateStack == NIL)
2081                 current_hint = NULL;
2082         else
2083                 current_hint = (HintState *) lfirst(list_head(HintStateStack));
2084 }
2085
2086 static PlannedStmt *
2087 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
2088 {
2089         int                             save_nestlevel;
2090         PlannedStmt        *result;
2091         HintState          *hstate;
2092
2093         /*
2094          * Use standard planner if pg_hint_plan is disabled.  Other hook functions
2095          * try to change plan with current_hint if any, so set it to NULL.
2096          */
2097         if (!pg_hint_plan_enable_hint)
2098         {
2099                 current_hint = NULL;
2100
2101                 if (prev_planner)
2102                         return (*prev_planner) (parse, cursorOptions, boundParams);
2103                 else
2104                         return standard_planner(parse, cursorOptions, boundParams);
2105         }
2106
2107         /*
2108          *search hint.
2109          * TODO: replace "app1" with current application_name setting of the
2110          * session.
2111          * XXX: use something instead of debug_query_string?
2112          */
2113         search_hints(search_query, debug_query_string, "app1");
2114
2115         /* Create hint struct from parse tree. */
2116         hstate = parse_head_comment(parse);
2117
2118         /*
2119          * Use standard planner if the statement has not valid hint.  Other hook
2120          * functions try to change plan with current_hint if any, so set it to
2121          * NULL.
2122          */
2123         if (!hstate)
2124         {
2125                 current_hint = NULL;
2126
2127                 if (prev_planner)
2128                         return (*prev_planner) (parse, cursorOptions, boundParams);
2129                 else
2130                         return standard_planner(parse, cursorOptions, boundParams);
2131         }
2132
2133         /*
2134          * Push new hint struct to the hint stack to disable previous hint context.
2135          */
2136         push_hint(hstate);
2137
2138         /* Set GUC parameters which are specified with Set hint. */
2139         save_nestlevel = set_config_options(current_hint->set_hints,
2140                                                                                 current_hint->num_hints[HINT_TYPE_SET],
2141                                                                                 current_hint->context);
2142
2143         if (enable_seqscan)
2144                 current_hint->init_scan_mask |= ENABLE_SEQSCAN;
2145         if (enable_indexscan)
2146                 current_hint->init_scan_mask |= ENABLE_INDEXSCAN;
2147         if (enable_bitmapscan)
2148                 current_hint->init_scan_mask |= ENABLE_BITMAPSCAN;
2149         if (enable_tidscan)
2150                 current_hint->init_scan_mask |= ENABLE_TIDSCAN;
2151 #if PG_VERSION_NUM >= 90200
2152         if (enable_indexonlyscan)
2153                 current_hint->init_scan_mask |= ENABLE_INDEXONLYSCAN;
2154 #endif
2155         if (enable_nestloop)
2156                 current_hint->init_join_mask |= ENABLE_NESTLOOP;
2157         if (enable_mergejoin)
2158                 current_hint->init_join_mask |= ENABLE_MERGEJOIN;
2159         if (enable_hashjoin)
2160                 current_hint->init_join_mask |= ENABLE_HASHJOIN;
2161
2162         /*
2163          * Use PG_TRY mechanism to recover GUC parameters and current_hint to the
2164          * state when this planner started when error occurred in planner.
2165          */
2166         PG_TRY();
2167         {
2168                 if (prev_planner)
2169                         result = (*prev_planner) (parse, cursorOptions, boundParams);
2170                 else
2171                         result = standard_planner(parse, cursorOptions, boundParams);
2172         }
2173         PG_CATCH();
2174         {
2175                 /*
2176                  * Rollback changes of GUC parameters, and pop current hint context
2177                  * from hint stack to rewind the state.
2178                  */
2179                 AtEOXact_GUC(true, save_nestlevel);
2180                 pop_hint();
2181                 PG_RE_THROW();
2182         }
2183         PG_END_TRY();
2184
2185         /* Print hint in debug mode. */
2186         if (pg_hint_plan_debug_print)
2187                 HintStateDump(current_hint);
2188
2189         /*
2190          * Rollback changes of GUC parameters, and pop current hint context from
2191          * hint stack to rewind the state.
2192          */
2193         AtEOXact_GUC(true, save_nestlevel);
2194         pop_hint();
2195
2196         return result;
2197 }
2198
2199 /*
2200  * Return scan method hint which matches given aliasname.
2201  */
2202 static ScanMethodHint *
2203 find_scan_hint(PlannerInfo *root, RelOptInfo *rel)
2204 {
2205         RangeTblEntry  *rte;
2206         int                             i;
2207
2208         /*
2209          * We can't apply scan method hint if the relation is:
2210          *   - not a base relation
2211          *   - not an ordinary relation (such as join and subquery)
2212          */
2213         if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
2214                 return NULL;
2215
2216         rte = root->simple_rte_array[rel->relid];
2217
2218         /* We can't force scan method of foreign tables */
2219         if (rte->relkind == RELKIND_FOREIGN_TABLE)
2220                 return NULL;
2221
2222         /* Find scan method hint, which matches given names, from the list. */
2223         for (i = 0; i < current_hint->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
2224         {
2225                 ScanMethodHint *hint = current_hint->scan_hints[i];
2226
2227                 /* We ignore disabled hints. */
2228                 if (!hint_state_enabled(hint))
2229                         continue;
2230
2231                 if (RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
2232                         return hint;
2233         }
2234
2235         return NULL;
2236 }
2237
2238 /*
2239  * regexeq
2240  *
2241  * Returns TRUE on match, FALSE on no match.
2242  *
2243  *   s1 --- the data to match against
2244  *   s2 --- the pattern
2245  *
2246  * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
2247  */
2248 static bool
2249 regexpeq(const char *s1, const char *s2)
2250 {
2251         NameData        name;
2252         text       *regexp;
2253         Datum           result;
2254
2255         strcpy(name.data, s1);
2256         regexp = cstring_to_text(s2);
2257
2258         result = DirectFunctionCall2Coll(nameregexeq,
2259                                                                          DEFAULT_COLLATION_OID,
2260                                                                          NameGetDatum(&name),
2261                                                                          PointerGetDatum(regexp));
2262         return DatumGetBool(result);
2263 }
2264
2265 static void
2266 delete_indexes(ScanMethodHint *hint, RelOptInfo *rel, Oid relationObjectId)
2267 {
2268         ListCell           *cell;
2269         ListCell           *prev;
2270         ListCell           *next;
2271         StringInfoData  buf;
2272
2273         /*
2274          * We delete all the IndexOptInfo list and prevent you from being usable by
2275          * a scan.
2276          */
2277         if (hint->enforce_mask == ENABLE_SEQSCAN ||
2278                 hint->enforce_mask == ENABLE_TIDSCAN)
2279         {
2280                 list_free_deep(rel->indexlist);
2281                 rel->indexlist = NIL;
2282                 hint->base.state = HINT_STATE_USED;
2283
2284                 return;
2285         }
2286
2287         /*
2288          * When a list of indexes is not specified, we just use all indexes.
2289          */
2290         if (hint->indexnames == NIL)
2291                 return;
2292
2293         /*
2294          * Leaving only an specified index, we delete it from a IndexOptInfo list
2295          * other than it.
2296          */
2297         prev = NULL;
2298         if (pg_hint_plan_debug_print)
2299                 initStringInfo(&buf);
2300
2301         for (cell = list_head(rel->indexlist); cell; cell = next)
2302         {
2303                 IndexOptInfo   *info = (IndexOptInfo *) lfirst(cell);
2304                 char               *indexname = get_rel_name(info->indexoid);
2305                 ListCell           *l;
2306                 bool                    use_index = false;
2307
2308                 next = lnext(cell);
2309
2310                 foreach(l, hint->indexnames)
2311                 {
2312                         char   *hintname = (char *) lfirst(l);
2313                         bool    result;
2314
2315                         if (hint->regexp)
2316                                 result = regexpeq(indexname, hintname);
2317                         else
2318                                 result = RelnameCmp(&indexname, &hintname) == 0;
2319
2320                         if (result)
2321                         {
2322                                 use_index = true;
2323                                 if (pg_hint_plan_debug_print)
2324                                 {
2325                                         appendStringInfoCharMacro(&buf, ' ');
2326                                         quote_value(&buf, indexname);
2327                                 }
2328
2329                                 break;
2330                         }
2331                 }
2332
2333                 /*
2334                  * to make the index a candidate when definition of this index is
2335                  * matched with the index's definition of current_hint.
2336                  */
2337                 if (OidIsValid(relationObjectId) && !use_index)
2338                 {
2339                         foreach(l, current_hint->parent_index_infos)
2340                         {
2341                                 int                                     i;
2342                                 HeapTuple                       ht_idx;
2343                                 ParentIndexInfo    *p_info = (ParentIndexInfo *)lfirst(l);
2344
2345                                 /* check to match the parameter of unique */
2346                                 if (p_info->indisunique != info->unique)
2347                                         continue;
2348
2349                                 /* check to match the parameter of index's method */
2350                                 if (p_info->method != info->relam)
2351                                         continue;
2352
2353                                 /* to check to match the indexkey's configuration */
2354                                 if ((list_length(p_info->column_names)) !=
2355                                          info->ncolumns)
2356                                         continue;
2357
2358                                 /* check to match the indexkey's configuration */
2359                                 for (i = 0; i < info->ncolumns; i++)
2360                                 {
2361                                         char       *c_attname = NULL;
2362                                         char       *p_attname = NULL;
2363
2364                                         p_attname =
2365                                                 list_nth(p_info->column_names, i);
2366
2367                                         /* both are expressions */
2368                                         if (info->indexkeys[i] == 0 && !p_attname)
2369                                                 continue;
2370
2371                                         /* one's column is expression, the other is not */
2372                                         if (info->indexkeys[i] == 0 || !p_attname)
2373                                                 break;
2374
2375                                         c_attname = get_attname(relationObjectId,
2376                                                                                                 info->indexkeys[i]);
2377
2378                                         if (strcmp(p_attname, c_attname) != 0)
2379                                                 break;
2380
2381                                         if (p_info->indcollation[i] != info->indexcollations[i])
2382                                                 break;
2383
2384                                         if (p_info->opclass[i] != info->opcintype[i])
2385                                                 break;
2386
2387                                         if (((p_info->indoption[i] & INDOPTION_DESC) != 0) !=
2388                                                 info->reverse_sort[i])
2389                                                 break;
2390
2391                                         if (((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0) !=
2392                                                 info->nulls_first[i])
2393                                                 break;
2394
2395                                 }
2396
2397                                 if (i != info->ncolumns)
2398                                         continue;
2399
2400                                 if ((p_info->expression_str && (info->indexprs != NIL)) ||
2401                                         (p_info->indpred_str && (info->indpred != NIL)))
2402                                 {
2403                                         /*
2404                                          * Fetch the pg_index tuple by the Oid of the index
2405                                          */
2406                                         ht_idx = SearchSysCache1(INDEXRELID,
2407                                                                                          ObjectIdGetDatum(info->indexoid));
2408
2409                                         /* check to match the expression's parameter of index */
2410                                         if (p_info->expression_str &&
2411                                                 !heap_attisnull(ht_idx, Anum_pg_index_indexprs))
2412                                         {
2413                                                 Datum       exprsDatum;
2414                                                 bool        isnull;
2415                                                 Datum       result;
2416
2417                                                 /*
2418                                                  * to change the expression's parameter of child's
2419                                                  * index to strings
2420                                                  */
2421                                                 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
2422                                                                                                          Anum_pg_index_indexprs,
2423                                                                                                          &isnull);
2424
2425                                                 result = DirectFunctionCall2(pg_get_expr,
2426                                                                                                          exprsDatum,
2427                                                                                                          ObjectIdGetDatum(
2428                                                                                                                  relationObjectId));
2429
2430                                                 if (strcmp(p_info->expression_str,
2431                                                                    text_to_cstring(DatumGetTextP(result))) != 0)
2432                                                 {
2433                                                         /* Clean up */
2434                                                         ReleaseSysCache(ht_idx);
2435
2436                                                         continue;
2437                                                 }
2438                                         }
2439
2440                                         /* Check to match the predicate's paraameter of index */
2441                                         if (p_info->indpred_str &&
2442                                                 !heap_attisnull(ht_idx, Anum_pg_index_indpred))
2443                                         {
2444                                                 Datum       predDatum;
2445                                                 bool        isnull;
2446                                                 Datum       result;
2447
2448                                                 /*
2449                                                  * to change the predicate's parabeter of child's
2450                                                  * index to strings
2451                                                  */
2452                                                 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
2453                                                                                                          Anum_pg_index_indpred,
2454                                                                                                          &isnull);
2455
2456                                                 result = DirectFunctionCall2(pg_get_expr,
2457                                                                                                          predDatum,
2458                                                                                                          ObjectIdGetDatum(
2459                                                                                                                  relationObjectId));
2460
2461                                                 if (strcmp(p_info->indpred_str,
2462                                                                    text_to_cstring(DatumGetTextP(result))) != 0)
2463                                                 {
2464                                                         /* Clean up */
2465                                                         ReleaseSysCache(ht_idx);
2466
2467                                                         continue;
2468                                                 }
2469                                         }
2470
2471                                         /* Clean up */
2472                                         ReleaseSysCache(ht_idx);
2473                                 }
2474                                 else if (p_info->expression_str || (info->indexprs != NIL))
2475                                         continue;
2476                                 else if (p_info->indpred_str || (info->indpred != NIL))
2477                                         continue;
2478
2479                                 use_index = true;
2480
2481                                 /* to log the candidate of index */
2482                                 if (pg_hint_plan_debug_print)
2483                                 {
2484                                         appendStringInfoCharMacro(&buf, ' ');
2485                                         quote_value(&buf, indexname);
2486                                 }
2487
2488                                 break;
2489                         }
2490                 }
2491
2492                 if (!use_index)
2493                         rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
2494                 else
2495                         prev = cell;
2496
2497                 pfree(indexname);
2498         }
2499
2500         if (pg_hint_plan_debug_print)
2501         {
2502                 char   *relname;
2503                 StringInfoData  rel_buf;
2504
2505                 if (OidIsValid(relationObjectId))
2506                         relname = get_rel_name(relationObjectId);
2507                 else
2508                         relname = hint->relname;
2509
2510                 initStringInfo(&rel_buf);
2511                 quote_value(&rel_buf, relname);
2512
2513                 ereport(LOG,
2514                                 (errmsg("available indexes for %s(%s):%s",
2515                                          hint->base.keyword,
2516                                          rel_buf.data,
2517                                          buf.data)));
2518                 pfree(buf.data);
2519                 pfree(rel_buf.data);
2520         }
2521 }
2522
2523 /* 
2524  * Return information of index definition.
2525  */
2526 static ParentIndexInfo *
2527 get_parent_index_info(Oid indexoid, Oid relid)
2528 {
2529         ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
2530         Relation            indexRelation;
2531         Form_pg_index   index;
2532         char               *attname;
2533         int                             i;
2534
2535         indexRelation = index_open(indexoid, RowExclusiveLock);
2536
2537         index = indexRelation->rd_index;
2538
2539         p_info->indisunique = index->indisunique;
2540         p_info->method = indexRelation->rd_rel->relam;
2541
2542         p_info->column_names = NIL;
2543         p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
2544         p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
2545         p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
2546
2547         for (i = 0; i < index->indnatts; i++)
2548         {
2549                 attname = get_attname(relid, index->indkey.values[i]);
2550                 p_info->column_names = lappend(p_info->column_names, attname);
2551
2552                 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
2553                 p_info->opclass[i] = indexRelation->rd_opcintype[i];
2554                 p_info->indoption[i] = indexRelation->rd_indoption[i];
2555         }
2556
2557         /*
2558          * to check to match the expression's paraameter of index with child indexes
2559          */
2560         p_info->expression_str = NULL;
2561         if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs))
2562         {
2563                 Datum       exprsDatum;
2564                 bool            isnull;
2565                 Datum           result;
2566
2567                 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
2568                                                                          Anum_pg_index_indexprs, &isnull);
2569
2570                 result = DirectFunctionCall2(pg_get_expr,
2571                                                                          exprsDatum,
2572                                                                          ObjectIdGetDatum(relid));
2573
2574                 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
2575         }
2576
2577         /*
2578          * to check to match the predicate's paraameter of index with child indexes
2579          */
2580         p_info->indpred_str = NULL;
2581         if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred))
2582         {
2583                 Datum       predDatum;
2584                 bool            isnull;
2585                 Datum           result;
2586
2587                 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
2588                                                                          Anum_pg_index_indpred, &isnull);
2589
2590                 result = DirectFunctionCall2(pg_get_expr,
2591                                                                          predDatum,
2592                                                                          ObjectIdGetDatum(relid));
2593
2594                 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
2595         }
2596
2597         index_close(indexRelation, NoLock);
2598
2599         return p_info;
2600 }
2601
2602 static void
2603 pg_hint_plan_get_relation_info(PlannerInfo *root, Oid relationObjectId,
2604                                                            bool inhparent, RelOptInfo *rel)
2605 {
2606         ScanMethodHint *hint;
2607
2608         if (prev_get_relation_info)
2609                 (*prev_get_relation_info) (root, relationObjectId, inhparent, rel);
2610
2611         /* Do nothing if we don't have valid hint in this context. */
2612         if (!current_hint)
2613                 return;
2614
2615         if (inhparent)
2616         {
2617                 /* store does relids of parent table. */
2618                 current_hint->parent_relid = rel->relid;
2619                 current_hint->parent_rel_oid = relationObjectId;
2620         }
2621         else if (current_hint->parent_relid != 0)
2622         {
2623                 /*
2624                  * We use the same GUC parameter if this table is the child table of a
2625                  * table called pg_hint_plan_get_relation_info just before that.
2626                  */
2627                 ListCell   *l;
2628
2629                 /* append_rel_list contains all append rels; ignore others */
2630                 foreach(l, root->append_rel_list)
2631                 {
2632                         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
2633
2634                         /* This rel is child table. */
2635                         if (appinfo->parent_relid == current_hint->parent_relid &&
2636                                 appinfo->child_relid == rel->relid)
2637                         {
2638                                 if (current_hint->parent_hint)
2639                                         delete_indexes(current_hint->parent_hint, rel,
2640                                                                    relationObjectId);
2641
2642                                 return;
2643                         }
2644                 }
2645
2646                 /* This rel is not inherit table. */
2647                 current_hint->parent_relid = 0;
2648                 current_hint->parent_rel_oid = InvalidOid;
2649                 current_hint->parent_hint = NULL;
2650         }
2651
2652         /*
2653          * If scan method hint was given, reset GUC parameters which control
2654          * planner behavior about choosing scan methods.
2655          */
2656         if ((hint = find_scan_hint(root, rel)) == NULL)
2657         {
2658                 set_scan_config_options(current_hint->init_scan_mask,
2659                                                                 current_hint->context);
2660                 return;
2661         }
2662         set_scan_config_options(hint->enforce_mask, current_hint->context);
2663         hint->base.state = HINT_STATE_USED;
2664
2665         if (inhparent)
2666         {
2667                 Relation    relation;
2668                 List       *indexoidlist;
2669                 ListCell   *l;
2670
2671                 current_hint->parent_hint = hint;
2672
2673                 relation = heap_open(relationObjectId, NoLock);
2674                 indexoidlist = RelationGetIndexList(relation);
2675
2676                 foreach(l, indexoidlist)
2677                 {
2678                         Oid         indexoid = lfirst_oid(l);
2679                         char       *indexname = get_rel_name(indexoid);
2680                         bool        use_index = false;
2681                         ListCell   *lc;
2682                         ParentIndexInfo *parent_index_info;
2683
2684                         foreach(lc, hint->indexnames)
2685                         {
2686                                 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
2687                                 {
2688                                         use_index = true;
2689                                         break;
2690                                 }
2691                         }
2692                         if (!use_index)
2693                                 continue;
2694
2695                         parent_index_info = get_parent_index_info(indexoid,
2696                                                                                                           relationObjectId);
2697                         current_hint->parent_index_infos =
2698                                 lappend(current_hint->parent_index_infos, parent_index_info);
2699                 }
2700                 heap_close(relation, NoLock);
2701         }
2702         else
2703                 delete_indexes(hint, rel, InvalidOid);
2704 }
2705
2706 /*
2707  * Return index of relation which matches given aliasname, or 0 if not found.
2708  * If same aliasname was used multiple times in a query, return -1.
2709  */
2710 static int
2711 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
2712                                          const char *str)
2713 {
2714         int             i;
2715         Index   found = 0;
2716
2717         for (i = 1; i < root->simple_rel_array_size; i++)
2718         {
2719                 ListCell   *l;
2720
2721                 if (root->simple_rel_array[i] == NULL)
2722                         continue;
2723
2724                 Assert(i == root->simple_rel_array[i]->relid);
2725
2726                 if (RelnameCmp(&aliasname,
2727                                            &root->simple_rte_array[i]->eref->aliasname) != 0)
2728                         continue;
2729
2730                 foreach(l, initial_rels)
2731                 {
2732                         RelOptInfo *rel = (RelOptInfo *) lfirst(l);
2733
2734                         if (rel->reloptkind == RELOPT_BASEREL)
2735                         {
2736                                 if (rel->relid != i)
2737                                         continue;
2738                         }
2739                         else
2740                         {
2741                                 Assert(rel->reloptkind == RELOPT_JOINREL);
2742
2743                                 if (!bms_is_member(i, rel->relids))
2744                                         continue;
2745                         }
2746
2747                         if (found != 0)
2748                         {
2749                                 hint_ereport(str,
2750                                                          ("Relation name \"%s\" is ambiguous.",
2751                                                           aliasname));
2752                                 return -1;
2753                         }
2754
2755                         found = i;
2756                         break;
2757                 }
2758
2759         }
2760
2761         return found;
2762 }
2763
2764 /*
2765  * Return join hint which matches given joinrelids.
2766  */
2767 static JoinMethodHint *
2768 find_join_hint(Relids joinrelids)
2769 {
2770         List       *join_hint;
2771         ListCell   *l;
2772
2773         join_hint = current_hint->join_hint_level[bms_num_members(joinrelids)];
2774
2775         foreach(l, join_hint)
2776         {
2777                 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
2778
2779                 if (bms_equal(joinrelids, hint->joinrelids))
2780                         return hint;
2781         }
2782
2783         return NULL;
2784 }
2785
2786 static Relids
2787 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
2788         PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
2789 {
2790         OuterInnerRels *outer_rels;
2791         OuterInnerRels *inner_rels;
2792         Relids                  outer_relids;
2793         Relids                  inner_relids;
2794         Relids                  join_relids;
2795         JoinMethodHint *hint;
2796
2797         if (outer_inner->relation != NULL)
2798         {
2799                 return bms_make_singleton(
2800                                         find_relid_aliasname(root, outer_inner->relation,
2801                                                                                  initial_rels,
2802                                                                                  leading_hint->base.hint_str));
2803         }
2804
2805         outer_rels = lfirst(outer_inner->outer_inner_pair->head);
2806         inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
2807
2808         outer_relids = OuterInnerJoinCreate(outer_rels,
2809                                                                                 leading_hint,
2810                                                                                 root,
2811                                                                                 initial_rels,
2812                                                                                 hstate,
2813                                                                                 nbaserel);
2814         inner_relids = OuterInnerJoinCreate(inner_rels,
2815                                                                                 leading_hint,
2816                                                                                 root,
2817                                                                                 initial_rels,
2818                                                                                 hstate,
2819                                                                                 nbaserel);
2820
2821         join_relids = bms_add_members(outer_relids, inner_relids);
2822
2823         if (bms_num_members(join_relids) > nbaserel)
2824                 return join_relids;
2825
2826         /*
2827          * If we don't have join method hint, create new one for the
2828          * join combination with all join methods are enabled.
2829          */
2830         hint = find_join_hint(join_relids);
2831         if (hint == NULL)
2832         {
2833                 /*
2834                  * Here relnames is not set, since Relids bitmap is sufficient to
2835                  * control paths of this query afterward.
2836                  */
2837                 hint = (JoinMethodHint *) JoinMethodHintCreate(
2838                                         leading_hint->base.hint_str,
2839                                         HINT_LEADING,
2840                                         HINT_KEYWORD_LEADING);
2841                 hint->base.state = HINT_STATE_USED;
2842                 hint->nrels = bms_num_members(join_relids);
2843                 hint->enforce_mask = ENABLE_ALL_JOIN;
2844                 hint->joinrelids = bms_copy(join_relids);
2845                 hint->inner_nrels = bms_num_members(inner_relids);
2846                 hint->inner_joinrelids = bms_copy(inner_relids);
2847
2848                 hstate->join_hint_level[hint->nrels] =
2849                         lappend(hstate->join_hint_level[hint->nrels], hint);
2850         }
2851         else
2852         {
2853                 hint->inner_nrels = bms_num_members(inner_relids);
2854                 hint->inner_joinrelids = bms_copy(inner_relids);
2855         }
2856
2857         return join_relids;
2858 }
2859
2860 /*
2861  * Transform join method hint into handy form.
2862  *
2863  *   - create bitmap of relids from alias names, to make it easier to check
2864  *     whether a join path matches a join method hint.
2865  *   - add join method hints which are necessary to enforce join order
2866  *     specified by Leading hint
2867  */
2868 static bool
2869 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
2870                 List *initial_rels, JoinMethodHint **join_method_hints)
2871 {
2872         int                             i;
2873         int                             relid;
2874         Relids                  joinrelids;
2875         int                             njoinrels;
2876         ListCell           *l;
2877         char               *relname;
2878         LeadingHint        *lhint = NULL;
2879
2880         /*
2881          * Create bitmap of relids from alias names for each join method hint.
2882          * Bitmaps are more handy than strings in join searching.
2883          */
2884         for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
2885         {
2886                 JoinMethodHint *hint = hstate->join_hints[i];
2887                 int     j;
2888
2889                 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
2890                         continue;
2891
2892                 bms_free(hint->joinrelids);
2893                 hint->joinrelids = NULL;
2894                 relid = 0;
2895                 for (j = 0; j < hint->nrels; j++)
2896                 {
2897                         relname = hint->relnames[j];
2898
2899                         relid = find_relid_aliasname(root, relname, initial_rels,
2900                                                                                  hint->base.hint_str);
2901
2902                         if (relid == -1)
2903                                 hint->base.state = HINT_STATE_ERROR;
2904
2905                         if (relid <= 0)
2906                                 break;
2907
2908                         if (bms_is_member(relid, hint->joinrelids))
2909                         {
2910                                 hint_ereport(hint->base.hint_str,
2911                                                          ("Relation name \"%s\" is duplicated.", relname));
2912                                 hint->base.state = HINT_STATE_ERROR;
2913                                 break;
2914                         }
2915
2916                         hint->joinrelids = bms_add_member(hint->joinrelids, relid);
2917                 }
2918
2919                 if (relid <= 0 || hint->base.state == HINT_STATE_ERROR)
2920                         continue;
2921
2922                 hstate->join_hint_level[hint->nrels] =
2923                         lappend(hstate->join_hint_level[hint->nrels], hint);
2924         }
2925
2926         /* Do nothing if no Leading hint was supplied. */
2927         if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
2928                 return false;
2929
2930         /*
2931          * Decide to use Leading hint。
2932          */
2933         for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
2934         {
2935                 LeadingHint        *leading_hint = (LeadingHint *)hstate->leading_hint[i];
2936                 Relids                  relids;
2937
2938                 if (leading_hint->base.state == HINT_STATE_ERROR)
2939                         continue;
2940
2941                 relid = 0;
2942                 relids = NULL;
2943
2944                 foreach(l, leading_hint->relations)
2945                 {
2946                         relname = (char *)lfirst(l);;
2947
2948                         relid = find_relid_aliasname(root, relname, initial_rels,
2949                                                                                  leading_hint->base.hint_str);
2950                         if (relid == -1)
2951                                 leading_hint->base.state = HINT_STATE_ERROR;
2952
2953                         if (relid <= 0)
2954                                 break;
2955
2956                         if (bms_is_member(relid, relids))
2957                         {
2958                                 hint_ereport(leading_hint->base.hint_str,
2959                                                          ("Relation name \"%s\" is duplicated.", relname));
2960                                 leading_hint->base.state = HINT_STATE_ERROR;
2961                                 break;
2962                         }
2963
2964                         relids = bms_add_member(relids, relid);
2965                 }
2966
2967                 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
2968                         continue;
2969
2970                 if (lhint != NULL)
2971                 {
2972                         hint_ereport(lhint->base.hint_str,
2973                                  ("Conflict %s hint.", HintTypeName[lhint->base.type]));
2974                         lhint->base.state = HINT_STATE_DUPLICATION;
2975                 }
2976                 leading_hint->base.state = HINT_STATE_USED;
2977                 lhint = leading_hint;
2978         }
2979
2980         /* check to exist Leading hint marked with 'used'. */
2981         if (lhint == NULL)
2982                 return false;
2983
2984         /*
2985          * We need join method hints which fit specified join order in every join
2986          * level.  For example, Leading(A B C) virtually requires following join
2987          * method hints, if no join method hint supplied:
2988          *   - level 1: none
2989          *   - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
2990          *   - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
2991          *
2992          * If we already have join method hint which fits specified join order in
2993          * that join level, we leave it as-is and don't add new hints.
2994          */
2995         joinrelids = NULL;
2996         njoinrels = 0;
2997         if (lhint->outer_inner == NULL)
2998         {
2999                 foreach(l, lhint->relations)
3000                 {
3001                         JoinMethodHint *hint;
3002
3003                         relname = (char *)lfirst(l);
3004
3005                         /*
3006                          * Find relid of the relation which has given name.  If we have the
3007                          * name given in Leading hint multiple times in the join, nothing to
3008                          * do.
3009                          */
3010                         relid = find_relid_aliasname(root, relname, initial_rels,
3011                                                                                  hstate->hint_str);
3012
3013                         /* Create bitmap of relids for current join level. */
3014                         joinrelids = bms_add_member(joinrelids, relid);
3015                         njoinrels++;
3016
3017                         /* We never have join method hint for single relation. */
3018                         if (njoinrels < 2)
3019                                 continue;
3020
3021                         /*
3022                          * If we don't have join method hint, create new one for the
3023                          * join combination with all join methods are enabled.
3024                          */
3025                         hint = find_join_hint(joinrelids);
3026                         if (hint == NULL)
3027                         {
3028                                 /*
3029                                  * Here relnames is not set, since Relids bitmap is sufficient
3030                                  * to control paths of this query afterward.
3031                                  */
3032                                 hint = (JoinMethodHint *) JoinMethodHintCreate(
3033                                                                                         lhint->base.hint_str,
3034                                                                                         HINT_LEADING,
3035                                                                                         HINT_KEYWORD_LEADING);
3036                                 hint->base.state = HINT_STATE_USED;
3037                                 hint->nrels = njoinrels;
3038                                 hint->enforce_mask = ENABLE_ALL_JOIN;
3039                                 hint->joinrelids = bms_copy(joinrelids);
3040                         }
3041
3042                         join_method_hints[njoinrels] = hint;
3043
3044                         if (njoinrels >= nbaserel)
3045                                 break;
3046                 }
3047                 bms_free(joinrelids);
3048
3049                 if (njoinrels < 2)
3050                         return false;
3051
3052                 /*
3053                  * Delete all join hints which have different combination from Leading
3054                  * hint.
3055                  */
3056                 for (i = 2; i <= njoinrels; i++)
3057                 {
3058                         list_free(hstate->join_hint_level[i]);
3059
3060                         hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
3061                 }
3062         }
3063         else
3064         {
3065                 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
3066                                                                                   lhint,
3067                                           root,
3068                                           initial_rels,
3069                                                                                   hstate,
3070                                                                                   nbaserel);
3071
3072                 njoinrels = bms_num_members(joinrelids);
3073                 Assert(njoinrels >= 2);
3074
3075                 /*
3076                  * Delete all join hints which have different combination from Leading
3077                  * hint.
3078                  */
3079                 for (i = 2;i <= njoinrels; i++)
3080                 {
3081                         if (hstate->join_hint_level[i] != NIL)
3082                         {
3083                                 ListCell *prev = NULL;
3084                                 ListCell *next = NULL;
3085                                 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
3086                                 {
3087
3088                                         JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
3089
3090                                         next = lnext(l);
3091
3092                                         if (hint->inner_nrels == 0 &&
3093                                                 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
3094                                                   bms_equal(bms_union(hint->joinrelids, joinrelids),
3095                                                   hint->joinrelids)))
3096                                         {
3097                                                 hstate->join_hint_level[i] =
3098                                                         list_delete_cell(hstate->join_hint_level[i], l,
3099                                                                                          prev);
3100                                         }
3101                                         else
3102                                                 prev = l;
3103                                 }
3104                         }
3105                 }
3106
3107                 bms_free(joinrelids);
3108         }
3109
3110         if (hint_state_enabled(lhint))
3111         {
3112                 set_join_config_options(DISABLE_ALL_JOIN, current_hint->context);
3113                 return true;
3114         }
3115         return false;
3116 }
3117
3118 /*
3119  * set_plain_rel_pathlist
3120  *        Build access paths for a plain relation (no subquery, no inheritance)
3121  *
3122  * This function was copied and edited from set_plain_rel_pathlist() in
3123  * src/backend/optimizer/path/allpaths.c
3124  */
3125 static void
3126 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
3127 {
3128         /* Consider sequential scan */
3129 #if PG_VERSION_NUM >= 90200
3130         add_path(rel, create_seqscan_path(root, rel, NULL));
3131 #else
3132         add_path(rel, create_seqscan_path(root, rel));
3133 #endif
3134
3135         /* Consider index scans */
3136         create_index_paths(root, rel);
3137
3138         /* Consider TID scans */
3139         create_tidscan_paths(root, rel);
3140
3141         /* Now find the cheapest of the paths for this rel */
3142         set_cheapest(rel);
3143 }
3144
3145 static void
3146 rebuild_scan_path(HintState *hstate, PlannerInfo *root, int level,
3147                                   List *initial_rels)
3148 {
3149         ListCell   *l;
3150
3151         foreach(l, initial_rels)
3152         {
3153                 RelOptInfo         *rel = (RelOptInfo *) lfirst(l);
3154                 RangeTblEntry  *rte;
3155                 ScanMethodHint *hint;
3156
3157                 /* Skip relations which we can't choose scan method. */
3158                 if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
3159                         continue;
3160
3161                 rte = root->simple_rte_array[rel->relid];
3162
3163                 /* We can't force scan method of foreign tables */
3164                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
3165                         continue;
3166
3167                 /*
3168                  * Create scan paths with GUC parameters which are at the beginning of
3169                  * planner if scan method hint is not specified, otherwise use
3170                  * specified hints and mark the hint as used.
3171                  */
3172                 if ((hint = find_scan_hint(root, rel)) == NULL)
3173                         set_scan_config_options(hstate->init_scan_mask,
3174                                                                         hstate->context);
3175                 else
3176                 {
3177                         set_scan_config_options(hint->enforce_mask, hstate->context);
3178                         hint->base.state = HINT_STATE_USED;
3179                 }
3180
3181                 list_free_deep(rel->pathlist);
3182                 rel->pathlist = NIL;
3183                 if (rte->inh)
3184                 {
3185                         /* It's an "append relation", process accordingly */
3186                         set_append_rel_pathlist(root, rel, rel->relid, rte);
3187                 }
3188                 else
3189                 {
3190                         set_plain_rel_pathlist(root, rel, rte);
3191                 }
3192         }
3193
3194         /*
3195          * Restore the GUC variables we set above.
3196          */
3197         set_scan_config_options(hstate->init_scan_mask, hstate->context);
3198 }
3199
3200 /*
3201  * wrapper of make_join_rel()
3202  *
3203  * call make_join_rel() after changing enable_* parameters according to given
3204  * hints.
3205  */
3206 static RelOptInfo *
3207 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
3208 {
3209         Relids                  joinrelids;
3210         JoinMethodHint *hint;
3211         RelOptInfo         *rel;
3212         int                             save_nestlevel;
3213
3214         joinrelids = bms_union(rel1->relids, rel2->relids);
3215         hint = find_join_hint(joinrelids);
3216         bms_free(joinrelids);
3217
3218         if (!hint)
3219                 return pg_hint_plan_make_join_rel(root, rel1, rel2);
3220
3221         if (hint->inner_nrels == 0)
3222         {
3223                 save_nestlevel = NewGUCNestLevel();
3224
3225                 set_join_config_options(hint->enforce_mask, current_hint->context);
3226
3227                 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
3228                 hint->base.state = HINT_STATE_USED;
3229
3230                 /*
3231                  * Restore the GUC variables we set above.
3232                  */
3233                 AtEOXact_GUC(true, save_nestlevel);
3234         }
3235         else
3236                 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
3237
3238         return rel;
3239 }
3240
3241 /*
3242  * TODO : comment
3243  */
3244 static void
3245 add_paths_to_joinrel_wrapper(PlannerInfo *root,
3246                                                          RelOptInfo *joinrel,
3247                                                          RelOptInfo *outerrel,
3248                                                          RelOptInfo *innerrel,
3249                                                          JoinType jointype,
3250                                                          SpecialJoinInfo *sjinfo,
3251                                                          List *restrictlist)
3252 {
3253         ScanMethodHint *scan_hint = NULL;
3254         Relids                  joinrelids;
3255         JoinMethodHint *join_hint;
3256         int                             save_nestlevel;
3257
3258         if ((scan_hint = find_scan_hint(root, innerrel)) != NULL)
3259         {
3260                 set_scan_config_options(scan_hint->enforce_mask, current_hint->context);
3261                 scan_hint->base.state = HINT_STATE_USED;
3262         }
3263
3264         joinrelids = bms_union(outerrel->relids, innerrel->relids);
3265         join_hint = find_join_hint(joinrelids);
3266         bms_free(joinrelids);
3267
3268         if (join_hint && join_hint->inner_nrels != 0)
3269         {
3270                 save_nestlevel = NewGUCNestLevel();
3271
3272                 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
3273                 {
3274
3275                         set_join_config_options(join_hint->enforce_mask,
3276                                                                         current_hint->context);
3277
3278                         add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
3279                                                                  sjinfo, restrictlist);
3280                         join_hint->base.state = HINT_STATE_USED;
3281                 }
3282                 else
3283                 {
3284                         set_join_config_options(DISABLE_ALL_JOIN, current_hint->context);
3285                         add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
3286                                                                  sjinfo, restrictlist);
3287                 }
3288
3289                 /*
3290                  * Restore the GUC variables we set above.
3291                  */
3292                 AtEOXact_GUC(true, save_nestlevel);
3293         }
3294         else
3295                 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
3296                                                          sjinfo, restrictlist);
3297
3298         if (scan_hint != NULL)
3299                 set_scan_config_options(current_hint->init_scan_mask,
3300                                                                 current_hint->context);
3301 }
3302
3303 static int
3304 get_num_baserels(List *initial_rels)
3305 {
3306         int                     nbaserel = 0;
3307         ListCell   *l;
3308
3309         foreach(l, initial_rels)
3310         {
3311                 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3312
3313                 if (rel->reloptkind == RELOPT_BASEREL)
3314                         nbaserel++;
3315                 else if (rel->reloptkind ==RELOPT_JOINREL)
3316                         nbaserel+= bms_num_members(rel->relids);
3317                 else
3318                 {
3319                         /* other values not expected here */
3320                         elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
3321                 }
3322         }
3323
3324         return nbaserel;
3325 }
3326
3327 static RelOptInfo *
3328 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
3329                                                  List *initial_rels)
3330 {
3331         JoinMethodHint    **join_method_hints;
3332         int                                     nbaserel;
3333         RelOptInfo                 *rel;
3334         int                                     i;
3335         bool                            leading_hint_enable;
3336
3337         /*
3338          * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
3339          * valid hint is supplied.
3340          */
3341         if (!current_hint)
3342         {
3343                 if (prev_join_search)
3344                         return (*prev_join_search) (root, levels_needed, initial_rels);
3345                 else if (enable_geqo && levels_needed >= geqo_threshold)
3346                         return geqo(root, levels_needed, initial_rels);
3347                 else
3348                         return standard_join_search(root, levels_needed, initial_rels);
3349         }
3350
3351         /* We apply scan method hint rebuild scan path. */
3352         rebuild_scan_path(current_hint, root, levels_needed, initial_rels);
3353
3354         /*
3355          * In the case using GEQO, only scan method hints and Set hints have
3356          * effect.  Join method and join order is not controllable by hints.
3357          */
3358         if (enable_geqo && levels_needed >= geqo_threshold)
3359                 return geqo(root, levels_needed, initial_rels);
3360
3361         nbaserel = get_num_baserels(initial_rels);
3362         current_hint->join_hint_level = palloc0(sizeof(List *) * (nbaserel + 1));
3363         join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
3364
3365         leading_hint_enable = transform_join_hints(current_hint, root, nbaserel,
3366                                                                                            initial_rels, join_method_hints);
3367
3368         rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
3369
3370         for (i = 2; i <= nbaserel; i++)
3371         {
3372                 list_free(current_hint->join_hint_level[i]);
3373
3374                 /* free Leading hint only */
3375                 if (join_method_hints[i] != NULL &&
3376                         join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
3377                         JoinMethodHintDelete(join_method_hints[i]);
3378         }
3379         pfree(current_hint->join_hint_level);
3380         pfree(join_method_hints);
3381
3382         if (leading_hint_enable)
3383                 set_join_config_options(current_hint->init_join_mask,
3384                                                                 current_hint->context);
3385
3386         return rel;
3387 }
3388
3389 /*
3390  * set_rel_pathlist
3391  *        Build access paths for a base relation
3392  *
3393  * This function was copied and edited from set_rel_pathlist() in
3394  * src/backend/optimizer/path/allpaths.c
3395  */
3396 static void
3397 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
3398                                  Index rti, RangeTblEntry *rte)
3399 {
3400 #if PG_VERSION_NUM >= 90200
3401         if (IS_DUMMY_REL(rel))
3402         {
3403                 /* We already proved the relation empty, so nothing more to do */
3404         }
3405         else if (rte->inh)
3406 #else
3407         if (rte->inh)
3408 #endif
3409         {
3410                 /* It's an "append relation", process accordingly */
3411                 set_append_rel_pathlist(root, rel, rti, rte);
3412         }
3413         else
3414         {
3415                 if (rel->rtekind == RTE_RELATION)
3416                 {
3417                         if (rte->relkind == RELKIND_RELATION)
3418                         {
3419                                 /* Plain relation */
3420                                 set_plain_rel_pathlist(root, rel, rte);
3421                         }
3422                         else
3423                                 elog(ERROR, "unexpected relkind: %c", rte->relkind);
3424                 }
3425                 else
3426                         elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
3427         }
3428 }
3429
3430 #define standard_join_search pg_hint_plan_standard_join_search
3431 #define join_search_one_level pg_hint_plan_join_search_one_level
3432 #define make_join_rel make_join_rel_wrapper
3433 #include "core.c"
3434
3435 #undef make_join_rel
3436 #define make_join_rel pg_hint_plan_make_join_rel
3437 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
3438 #include "make_join_rel.c"