OSDN Git Service

Now search_hints() returns hint string.
[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 const char *
1338 search_hints(const char *query,
1339                          const char *query_string,
1340                          const char *app_name)
1341 {
1342         static SPIPlanPtr plan = NULL;
1343         int             ret;
1344         char   *hints = NULL;
1345         Oid             argtypes[2] = { TEXTOID, TEXTOID };
1346         Datum   values[2];
1347         bool    nulls[2] = { false, false };
1348         text   *str;
1349         text   *app;
1350
1351         ret = SPI_connect();
1352         if (ret != SPI_OK_CONNECT)
1353                 elog(ERROR, "pg_hint_plan: SPI_connect => %d", ret);
1354
1355         if (plan == NULL)
1356         {
1357                 SPIPlanPtr      p;
1358                 p = SPI_prepare(query, 2, argtypes);
1359                 if (p == NULL)
1360                         elog(ERROR, "pg_hint_plan: SPI_prepare => %d", SPI_result);
1361                 plan = SPI_saveplan(p);
1362                 SPI_freeplan(p);
1363         }
1364
1365         str = cstring_to_text(query_string);
1366         app = cstring_to_text(app_name);
1367         values[0] = PointerGetDatum(str);
1368         values[1] = PointerGetDatum(app);
1369
1370         pg_hint_plan_enable_hint = false;
1371         ret = SPI_execute_plan(plan, values, nulls, true, 1);
1372         pg_hint_plan_enable_hint = true;
1373         if (ret != SPI_OK_SELECT)
1374                 elog(ERROR, "pg_hint_plan: SPI_execute_plan => %d", ret);
1375
1376         if (SPI_processed > 0)
1377         {
1378                 hints = pstrdup(SPI_getvalue(SPI_tuptable->vals[0],
1379                                                                            SPI_tuptable->tupdesc, 1));
1380         }
1381         SPI_finish();
1382
1383         return hints;
1384 }
1385
1386 /*
1387  * Do basic parsing of the query head comment.
1388  */
1389 static HintState *
1390 parse_head_comment(Query *parse)
1391 {
1392         const char *p;
1393         const char *hint_head;
1394         char       *head;
1395         char       *tail;
1396         int                     len;
1397         int                     i;
1398         HintState   *hstate;
1399
1400         /* get client-supplied query string. */
1401         if (stmt_name)
1402         {
1403                 PreparedStatement  *entry;
1404
1405                 entry = FetchPreparedStatement(stmt_name, true);
1406                 p = entry->plansource->query_string;
1407         }
1408         else
1409                 p = debug_query_string;
1410
1411         if (p == NULL)
1412                 return NULL;
1413
1414         /* extract query head comment. */
1415         hint_head = strstr(p, HINT_START);
1416         if (hint_head == NULL)
1417                 return NULL;
1418         for (;p < hint_head; p++)
1419         {
1420                 /*
1421                  * Allow these characters precedes hint comment:
1422                  *   - digits
1423                  *   - alphabets which are in ASCII range
1424                  *   - space, tabs and new-lines
1425                  *   - underscores, for identifier
1426                  *   - commas, for SELECT clause, EXPLAIN and PREPARE
1427                  *   - parentheses, for EXPLAIN and PREPARE
1428                  *
1429                  * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1430                  * avoid behavior which depends on locale setting.
1431                  */
1432                 if (!(*p >= '0' && *p <= '9') &&
1433                         !(*p >= 'A' && *p <= 'Z') &&
1434                         !(*p >= 'a' && *p <= 'z') &&
1435                         !isspace(*p) &&
1436                         *p != '_' &&
1437                         *p != ',' &&
1438                         *p != '(' && *p != ')')
1439                         return NULL;
1440         }
1441
1442         len = strlen(HINT_START);
1443         head = (char *) p;
1444         p += len;
1445         skip_space(p);
1446
1447         /* find hint end keyword. */
1448         if ((tail = strstr(p, HINT_END)) == NULL)
1449         {
1450                 hint_ereport(head, ("Unterminated block comment."));
1451                 return NULL;
1452         }
1453
1454         /* We don't support nested block comments. */
1455         if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1456         {
1457                 hint_ereport(head, ("Nested block comments are not supported."));
1458                 return NULL;
1459         }
1460
1461         /* Make a copy of hint. */
1462         len = tail - p;
1463         head = palloc(len + 1);
1464         memcpy(head, p, len);
1465         head[len] = '\0';
1466         p = head;
1467
1468         hstate = HintStateCreate();
1469         hstate->hint_str = head;
1470
1471         /* parse each hint. */
1472         parse_hints(hstate, parse, p);
1473
1474         /* When nothing specified a hint, we free HintState and returns NULL. */
1475         if (hstate->nall_hints == 0)
1476         {
1477                 HintStateDelete(hstate);
1478                 return NULL;
1479         }
1480
1481         /* Sort hints in order of original position. */
1482         qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
1483                   HintCmpWithPos);
1484
1485         /* Count number of hints per hint-type. */
1486         for (i = 0; i < hstate->nall_hints; i++)
1487         {
1488                 Hint   *cur_hint = hstate->all_hints[i];
1489                 hstate->num_hints[cur_hint->type]++;
1490         }
1491
1492         /*
1493          * If an object (or a set of objects) has multiple hints of same hint-type,
1494          * only the last hint is valid and others are igonred in planning.
1495          * Hints except the last are marked as 'duplicated' to remember the order.
1496          */
1497         for (i = 0; i < hstate->nall_hints - 1; i++)
1498         {
1499                 Hint   *cur_hint = hstate->all_hints[i];
1500                 Hint   *next_hint = hstate->all_hints[i + 1];
1501
1502                 /*
1503                  * Leading hint is marked as 'duplicated' in transform_join_hints.
1504                  */
1505                 if (cur_hint->type == HINT_TYPE_LEADING &&
1506                         next_hint->type == HINT_TYPE_LEADING)
1507                         continue;
1508
1509                 /*
1510                  * Note that we need to pass addresses of hint pointers, because
1511                  * HintCmp is designed to sort array of Hint* by qsort.
1512                  */
1513                 if (HintCmp(&cur_hint, &next_hint) == 0)
1514                 {
1515                         hint_ereport(cur_hint->hint_str,
1516                                                  ("Conflict %s hint.", HintTypeName[cur_hint->type]));
1517                         cur_hint->state = HINT_STATE_DUPLICATION;
1518                 }
1519         }
1520
1521         /*
1522          * Make sure that per-type array pointers point proper position in the
1523          * array which consists of all hints.
1524          */
1525         hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
1526         hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
1527                 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
1528         hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
1529                 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
1530         hstate->set_hints = (SetHint **) (hstate->leading_hint +
1531                 hstate->num_hints[HINT_TYPE_LEADING]);
1532
1533         return hstate;
1534 }
1535
1536 /*
1537  * Parse inside of parentheses of scan-method hints.
1538  */
1539 static const char *
1540 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
1541                                         const char *str)
1542 {
1543         const char         *keyword = hint->base.keyword;
1544         HintKeyword             hint_keyword = hint->base.hint_keyword;
1545         List               *name_list = NIL;
1546         int                             length;
1547
1548         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
1549                 return NULL;
1550
1551         /* Parse relation name and index name(s) if given hint accepts. */
1552         length = list_length(name_list);
1553         if (length > 0)
1554         {
1555                 hint->relname = linitial(name_list);
1556                 hint->indexnames = list_delete_first(name_list);
1557
1558                 /* check whether the hint accepts index name(s). */
1559                 if (length != 1 &&
1560                         hint_keyword != HINT_KEYWORD_INDEXSCAN &&
1561                         hint_keyword != HINT_KEYWORD_INDEXSCANREGEXP &&
1562 #if PG_VERSION_NUM >= 90200
1563                         hint_keyword != HINT_KEYWORD_INDEXONLYSCAN &&
1564                         hint_keyword != HINT_KEYWORD_INDEXONLYSCANREGEXP &&
1565 #endif
1566                         hint_keyword != HINT_KEYWORD_BITMAPSCAN &&
1567                         hint_keyword != HINT_KEYWORD_BITMAPSCANREGEXP)
1568                 {
1569                         hint_ereport(str,
1570                                                  ("%s hint accepts only one relation.",
1571                                                   hint->base.keyword));
1572                         hint->base.state = HINT_STATE_ERROR;
1573                         return str;
1574                 }
1575         }
1576         else
1577         {
1578                 hint_ereport(str,
1579                                          ("%s hint requires a relation.",
1580                                           hint->base.keyword));
1581                 hint->base.state = HINT_STATE_ERROR;
1582                 return str;
1583         }
1584
1585         /* Set a bit for specified hint. */
1586         switch (hint_keyword)
1587         {
1588                 case HINT_KEYWORD_SEQSCAN:
1589                         hint->enforce_mask = ENABLE_SEQSCAN;
1590                         break;
1591                 case HINT_KEYWORD_INDEXSCAN:
1592                         hint->enforce_mask = ENABLE_INDEXSCAN;
1593                         break;
1594                 case HINT_KEYWORD_INDEXSCANREGEXP:
1595                         hint->enforce_mask = ENABLE_INDEXSCAN;
1596                         hint->regexp = true;
1597                         break;
1598                 case HINT_KEYWORD_BITMAPSCAN:
1599                         hint->enforce_mask = ENABLE_BITMAPSCAN;
1600                         break;
1601                 case HINT_KEYWORD_BITMAPSCANREGEXP:
1602                         hint->enforce_mask = ENABLE_BITMAPSCAN;
1603                         hint->regexp = true;
1604                         break;
1605                 case HINT_KEYWORD_TIDSCAN:
1606                         hint->enforce_mask = ENABLE_TIDSCAN;
1607                         break;
1608                 case HINT_KEYWORD_NOSEQSCAN:
1609                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
1610                         break;
1611                 case HINT_KEYWORD_NOINDEXSCAN:
1612                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
1613                         break;
1614                 case HINT_KEYWORD_NOBITMAPSCAN:
1615                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
1616                         break;
1617                 case HINT_KEYWORD_NOTIDSCAN:
1618                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
1619                         break;
1620 #if PG_VERSION_NUM >= 90200
1621                 case HINT_KEYWORD_INDEXONLYSCAN:
1622                         hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
1623                         break;
1624                 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
1625                         hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
1626                         hint->regexp = true;
1627                         break;
1628                 case HINT_KEYWORD_NOINDEXONLYSCAN:
1629                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
1630                         break;
1631 #endif
1632                 default:
1633                         hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1634                         return NULL;
1635                         break;
1636         }
1637
1638         return str;
1639 }
1640
1641 static const char *
1642 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
1643                                         const char *str)
1644 {
1645         const char         *keyword = hint->base.keyword;
1646         HintKeyword             hint_keyword = hint->base.hint_keyword;
1647         List               *name_list = NIL;
1648
1649         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
1650                 return NULL;
1651
1652         hint->nrels = list_length(name_list);
1653
1654         if (hint->nrels > 0)
1655         {
1656                 ListCell   *l;
1657                 int                     i = 0;
1658
1659                 /*
1660                  * Transform relation names from list to array to sort them with qsort
1661                  * after.
1662                  */
1663                 hint->relnames = palloc(sizeof(char *) * hint->nrels);
1664                 foreach (l, name_list)
1665                 {
1666                         hint->relnames[i] = lfirst(l);
1667                         i++;
1668                 }
1669         }
1670
1671         list_free(name_list);
1672
1673         /* A join hint requires at least two relations */
1674         if (hint->nrels < 2)
1675         {
1676                 hint_ereport(str,
1677                                          ("%s hint requires at least two relations.",
1678                                           hint->base.keyword));
1679                 hint->base.state = HINT_STATE_ERROR;
1680                 return str;
1681         }
1682
1683         /* Sort hints in alphabetical order of relation names. */
1684         qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
1685
1686         switch (hint_keyword)
1687         {
1688                 case HINT_KEYWORD_NESTLOOP:
1689                         hint->enforce_mask = ENABLE_NESTLOOP;
1690                         break;
1691                 case HINT_KEYWORD_MERGEJOIN:
1692                         hint->enforce_mask = ENABLE_MERGEJOIN;
1693                         break;
1694                 case HINT_KEYWORD_HASHJOIN:
1695                         hint->enforce_mask = ENABLE_HASHJOIN;
1696                         break;
1697                 case HINT_KEYWORD_NONESTLOOP:
1698                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
1699                         break;
1700                 case HINT_KEYWORD_NOMERGEJOIN:
1701                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
1702                         break;
1703                 case HINT_KEYWORD_NOHASHJOIN:
1704                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
1705                         break;
1706                 default:
1707                         hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
1708                         return NULL;
1709                         break;
1710         }
1711
1712         return str;
1713 }
1714
1715 static bool
1716 OuterInnerPairCheck(OuterInnerRels *outer_inner)
1717 {
1718         ListCell *l;
1719         if (outer_inner->outer_inner_pair == NIL)
1720         {
1721                 if (outer_inner->relation)
1722                         return true;
1723                 else
1724                         return false;
1725         }
1726
1727         if (list_length(outer_inner->outer_inner_pair) == 2)
1728         {
1729                 foreach(l, outer_inner->outer_inner_pair)
1730                 {
1731                         if (!OuterInnerPairCheck(lfirst(l)))
1732                                 return false;
1733                 }
1734         }
1735         else
1736                 return false;
1737
1738         return true;
1739 }
1740
1741 static List *
1742 OuterInnerList(OuterInnerRels *outer_inner)
1743 {
1744         List               *outer_inner_list = NIL;
1745         ListCell           *l;
1746         OuterInnerRels *outer_inner_rels;
1747
1748         foreach(l, outer_inner->outer_inner_pair)
1749         {
1750                 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
1751
1752                 if (outer_inner_rels->relation != NULL)
1753                         outer_inner_list = lappend(outer_inner_list,
1754                                                                            outer_inner_rels->relation);
1755                 else
1756                         outer_inner_list = list_concat(outer_inner_list,
1757                                                                                    OuterInnerList(outer_inner_rels));
1758         }
1759         return outer_inner_list;
1760 }
1761
1762 static const char *
1763 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
1764                                  const char *str)
1765 {
1766         List               *name_list = NIL;
1767         OuterInnerRels *outer_inner = NULL;
1768
1769         if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
1770                 NULL)
1771                 return NULL;
1772
1773         if (outer_inner != NULL)
1774                 name_list = OuterInnerList(outer_inner);
1775
1776         hint->relations = name_list;
1777         hint->outer_inner = outer_inner;
1778
1779         /* A Leading hint requires at least two relations */
1780         if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
1781         {
1782                 hint_ereport(hint->base.hint_str,
1783                                          ("%s hint requires at least two relations.",
1784                                           HINT_LEADING));
1785                 hint->base.state = HINT_STATE_ERROR;
1786         }
1787         else if (hint->outer_inner != NULL &&
1788                          !OuterInnerPairCheck(hint->outer_inner))
1789         {
1790                 hint_ereport(hint->base.hint_str,
1791                                          ("%s hint requires two sets of relations when parentheses nests.",
1792                                           HINT_LEADING));
1793                 hint->base.state = HINT_STATE_ERROR;
1794         }
1795
1796         return str;
1797 }
1798
1799 static const char *
1800 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
1801 {
1802         List   *name_list = NIL;
1803
1804         if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
1805                 == NULL)
1806                 return NULL;
1807
1808         hint->words = name_list;
1809
1810         /* We need both name and value to set GUC parameter. */
1811         if (list_length(name_list) == 2)
1812         {
1813                 hint->name = linitial(name_list);
1814                 hint->value = lsecond(name_list);
1815         }
1816         else
1817         {
1818                 hint_ereport(hint->base.hint_str,
1819                                          ("%s hint requires name and value of GUC parameter.",
1820                                           HINT_SET));
1821                 hint->base.state = HINT_STATE_ERROR;
1822         }
1823
1824         return str;
1825 }
1826
1827 /*
1828  * set GUC parameter functions
1829  */
1830
1831 static int
1832 set_config_option_wrapper(const char *name, const char *value,
1833                                                   GucContext context, GucSource source,
1834                                                   GucAction action, bool changeVal, int elevel)
1835 {
1836         int                             result = 0;
1837         MemoryContext   ccxt = CurrentMemoryContext;
1838
1839         PG_TRY();
1840         {
1841 #if PG_VERSION_NUM >= 90200
1842                 result = set_config_option(name, value, context, source,
1843                                                                    action, changeVal, 0);
1844 #else
1845                 result = set_config_option(name, value, context, source,
1846                                                                    action, changeVal);
1847 #endif
1848         }
1849         PG_CATCH();
1850         {
1851                 ErrorData          *errdata;
1852
1853                 /* Save error info */
1854                 MemoryContextSwitchTo(ccxt);
1855                 errdata = CopyErrorData();
1856                 FlushErrorState();
1857
1858                 ereport(elevel, (errcode(errdata->sqlerrcode),
1859                                 errmsg("%s", errdata->message),
1860                                 errdata->detail ? errdetail("%s", errdata->detail) : 0,
1861                                 errdata->hint ? errhint("%s", errdata->hint) : 0));
1862                 FreeErrorData(errdata);
1863         }
1864         PG_END_TRY();
1865
1866         return result;
1867 }
1868
1869 static int
1870 set_config_options(SetHint **options, int noptions, GucContext context)
1871 {
1872         int     i;
1873         int     save_nestlevel;
1874
1875         save_nestlevel = NewGUCNestLevel();
1876
1877         for (i = 0; i < noptions; i++)
1878         {
1879                 SetHint    *hint = options[i];
1880                 int                     result;
1881
1882                 if (!hint_state_enabled(hint))
1883                         continue;
1884
1885                 result = set_config_option_wrapper(hint->name, hint->value, context,
1886                                                                                    PGC_S_SESSION, GUC_ACTION_SAVE, true,
1887                                                                                    pg_hint_plan_parse_messages);
1888                 if (result != 0)
1889                         hint->base.state = HINT_STATE_USED;
1890                 else
1891                         hint->base.state = HINT_STATE_ERROR;
1892         }
1893
1894         return save_nestlevel;
1895 }
1896
1897 #define SET_CONFIG_OPTION(name, type_bits) \
1898         set_config_option_wrapper((name), \
1899                 (mask & (type_bits)) ? "true" : "false", \
1900                 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
1901
1902 static void
1903 set_scan_config_options(unsigned char enforce_mask, GucContext context)
1904 {
1905         unsigned char   mask;
1906
1907         if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
1908                 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
1909 #if PG_VERSION_NUM >= 90200
1910                 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
1911 #endif
1912                 )
1913                 mask = enforce_mask;
1914         else
1915                 mask = enforce_mask & current_hint->init_scan_mask;
1916
1917         SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
1918         SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
1919         SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
1920         SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
1921 #if PG_VERSION_NUM >= 90200
1922         SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
1923 #endif
1924 }
1925
1926 static void
1927 set_join_config_options(unsigned char enforce_mask, GucContext context)
1928 {
1929         unsigned char   mask;
1930
1931         if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
1932                 enforce_mask == ENABLE_HASHJOIN)
1933                 mask = enforce_mask;
1934         else
1935                 mask = enforce_mask & current_hint->init_join_mask;
1936
1937         SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
1938         SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
1939         SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
1940 }
1941
1942 /*
1943  * pg_hint_plan hook functions
1944  */
1945
1946 static void
1947 pg_hint_plan_ProcessUtility(Node *parsetree, const char *queryString,
1948                                                         ParamListInfo params, bool isTopLevel,
1949                                                         DestReceiver *dest, char *completionTag)
1950 {
1951         Node                               *node;
1952
1953         if (!pg_hint_plan_enable_hint)
1954         {
1955                 if (prev_ProcessUtility)
1956                         (*prev_ProcessUtility) (parsetree, queryString, params,
1957                                                                         isTopLevel, dest, completionTag);
1958                 else
1959                         standard_ProcessUtility(parsetree, queryString, params,
1960                                                                         isTopLevel, dest, completionTag);
1961
1962                 return;
1963         }
1964
1965         node = parsetree;
1966         if (IsA(node, ExplainStmt))
1967         {
1968                 /*
1969                  * Draw out parse tree of actual query from Query struct of EXPLAIN
1970                  * statement.
1971                  */
1972                 ExplainStmt        *stmt;
1973                 Query              *query;
1974
1975                 stmt = (ExplainStmt *) node;
1976
1977                 Assert(IsA(stmt->query, Query));
1978                 query = (Query *) stmt->query;
1979
1980                 if (query->commandType == CMD_UTILITY && query->utilityStmt != NULL)
1981                         node = query->utilityStmt;
1982         }
1983
1984         /*
1985          * If the query was a EXECUTE or CREATE TABLE AS EXECUTE, get query string
1986          * specified to preceding PREPARE command to use it as source of hints.
1987          */
1988         if (IsA(node, ExecuteStmt))
1989         {
1990                 ExecuteStmt        *stmt;
1991
1992                 stmt = (ExecuteStmt *) node;
1993                 stmt_name = stmt->name;
1994         }
1995 #if PG_VERSION_NUM >= 90200
1996         /*
1997          * CREATE AS EXECUTE behavior has changed since 9.2, so we must handle it
1998          * specially here.
1999          */
2000         if (IsA(node, CreateTableAsStmt))
2001         {
2002                 CreateTableAsStmt          *stmt;
2003                 Query              *query;
2004
2005                 stmt = (CreateTableAsStmt *) node;
2006                 Assert(IsA(stmt->query, Query));
2007                 query = (Query *) stmt->query;
2008
2009                 if (query->commandType == CMD_UTILITY &&
2010                         IsA(query->utilityStmt, ExecuteStmt))
2011                 {
2012                         ExecuteStmt *estmt = (ExecuteStmt *) query->utilityStmt;
2013                         stmt_name = estmt->name;
2014                 }
2015         }
2016 #endif
2017         if (stmt_name)
2018         {
2019                 PG_TRY();
2020                 {
2021                         if (prev_ProcessUtility)
2022                                 (*prev_ProcessUtility) (parsetree, queryString, params,
2023                                                                                 isTopLevel, dest, completionTag);
2024                         else
2025                                 standard_ProcessUtility(parsetree, queryString, params,
2026                                                                                 isTopLevel, dest, completionTag);
2027                 }
2028                 PG_CATCH();
2029                 {
2030                         stmt_name = NULL;
2031                         PG_RE_THROW();
2032                 }
2033                 PG_END_TRY();
2034
2035                 stmt_name = NULL;
2036
2037                 return;
2038         }
2039
2040         if (prev_ProcessUtility)
2041                 (*prev_ProcessUtility) (parsetree, queryString, params,
2042                                                                 isTopLevel, dest, completionTag);
2043         else
2044                 standard_ProcessUtility(parsetree, queryString, params,
2045                                                                 isTopLevel, dest, completionTag);
2046 }
2047
2048 /*
2049  * Push a hint into hint stack which is implemented with List struct.  Head of
2050  * list is top of stack.
2051  */
2052 static void
2053 push_hint(HintState *hstate)
2054 {
2055         /* Prepend new hint to the list means pushing to stack. */
2056         HintStateStack = lcons(hstate, HintStateStack);
2057
2058         /* Pushed hint is the one which should be used hereafter. */
2059         current_hint = hstate;
2060 }
2061
2062 /* Pop a hint from hint stack.  Popped hint is automatically discarded. */
2063 static void
2064 pop_hint(void)
2065 {
2066         /* Hint stack must not be empty. */
2067         if(HintStateStack == NIL)
2068                 elog(ERROR, "hint stack is empty");
2069
2070         /*
2071          * Take a hint at the head from the list, and free it.  Switch current_hint
2072          * to point new head (NULL if the list is empty).
2073          */
2074         HintStateStack = list_delete_first(HintStateStack);
2075         HintStateDelete(current_hint);
2076         if(HintStateStack == NIL)
2077                 current_hint = NULL;
2078         else
2079                 current_hint = (HintState *) lfirst(list_head(HintStateStack));
2080 }
2081
2082 static PlannedStmt *
2083 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
2084 {
2085         const char         *hints;
2086         int                             save_nestlevel;
2087         PlannedStmt        *result;
2088         HintState          *hstate;
2089
2090         /*
2091          * Use standard planner if pg_hint_plan is disabled.  Other hook functions
2092          * try to change plan with current_hint if any, so set it to NULL.
2093          */
2094         if (!pg_hint_plan_enable_hint)
2095         {
2096                 current_hint = NULL;
2097
2098                 if (prev_planner)
2099                         return (*prev_planner) (parse, cursorOptions, boundParams);
2100                 else
2101                         return standard_planner(parse, cursorOptions, boundParams);
2102         }
2103
2104         /*
2105          *search hint.
2106          * TODO: replace "app1" with current application_name setting of the
2107          * session.
2108          * XXX: use something instead of debug_query_string?
2109          */
2110         hints = search_hints(search_query, debug_query_string, application_name);
2111         elog(LOG,
2112                  "pg_hint_plan: search_hints [%s][%s]=>[%s]",
2113                  debug_query_string, application_name,
2114                  hints ? hints : "(none)");
2115
2116         /* Create hint struct from parse tree. */
2117         hstate = parse_head_comment(parse);
2118
2119         /*
2120          * Use standard planner if the statement has not valid hint.  Other hook
2121          * functions try to change plan with current_hint if any, so set it to
2122          * NULL.
2123          */
2124         if (!hstate)
2125         {
2126                 current_hint = NULL;
2127
2128                 if (prev_planner)
2129                         return (*prev_planner) (parse, cursorOptions, boundParams);
2130                 else
2131                         return standard_planner(parse, cursorOptions, boundParams);
2132         }
2133
2134         /*
2135          * Push new hint struct to the hint stack to disable previous hint context.
2136          */
2137         push_hint(hstate);
2138
2139         /* Set GUC parameters which are specified with Set hint. */
2140         save_nestlevel = set_config_options(current_hint->set_hints,
2141                                                                                 current_hint->num_hints[HINT_TYPE_SET],
2142                                                                                 current_hint->context);
2143
2144         if (enable_seqscan)
2145                 current_hint->init_scan_mask |= ENABLE_SEQSCAN;
2146         if (enable_indexscan)
2147                 current_hint->init_scan_mask |= ENABLE_INDEXSCAN;
2148         if (enable_bitmapscan)
2149                 current_hint->init_scan_mask |= ENABLE_BITMAPSCAN;
2150         if (enable_tidscan)
2151                 current_hint->init_scan_mask |= ENABLE_TIDSCAN;
2152 #if PG_VERSION_NUM >= 90200
2153         if (enable_indexonlyscan)
2154                 current_hint->init_scan_mask |= ENABLE_INDEXONLYSCAN;
2155 #endif
2156         if (enable_nestloop)
2157                 current_hint->init_join_mask |= ENABLE_NESTLOOP;
2158         if (enable_mergejoin)
2159                 current_hint->init_join_mask |= ENABLE_MERGEJOIN;
2160         if (enable_hashjoin)
2161                 current_hint->init_join_mask |= ENABLE_HASHJOIN;
2162
2163         /*
2164          * Use PG_TRY mechanism to recover GUC parameters and current_hint to the
2165          * state when this planner started when error occurred in planner.
2166          */
2167         PG_TRY();
2168         {
2169                 if (prev_planner)
2170                         result = (*prev_planner) (parse, cursorOptions, boundParams);
2171                 else
2172                         result = standard_planner(parse, cursorOptions, boundParams);
2173         }
2174         PG_CATCH();
2175         {
2176                 /*
2177                  * Rollback changes of GUC parameters, and pop current hint context
2178                  * from hint stack to rewind the state.
2179                  */
2180                 AtEOXact_GUC(true, save_nestlevel);
2181                 pop_hint();
2182                 PG_RE_THROW();
2183         }
2184         PG_END_TRY();
2185
2186         /* Print hint in debug mode. */
2187         if (pg_hint_plan_debug_print)
2188                 HintStateDump(current_hint);
2189
2190         /*
2191          * Rollback changes of GUC parameters, and pop current hint context from
2192          * hint stack to rewind the state.
2193          */
2194         AtEOXact_GUC(true, save_nestlevel);
2195         pop_hint();
2196
2197         return result;
2198 }
2199
2200 /*
2201  * Return scan method hint which matches given aliasname.
2202  */
2203 static ScanMethodHint *
2204 find_scan_hint(PlannerInfo *root, RelOptInfo *rel)
2205 {
2206         RangeTblEntry  *rte;
2207         int                             i;
2208
2209         /*
2210          * We can't apply scan method hint if the relation is:
2211          *   - not a base relation
2212          *   - not an ordinary relation (such as join and subquery)
2213          */
2214         if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
2215                 return NULL;
2216
2217         rte = root->simple_rte_array[rel->relid];
2218
2219         /* We can't force scan method of foreign tables */
2220         if (rte->relkind == RELKIND_FOREIGN_TABLE)
2221                 return NULL;
2222
2223         /* Find scan method hint, which matches given names, from the list. */
2224         for (i = 0; i < current_hint->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
2225         {
2226                 ScanMethodHint *hint = current_hint->scan_hints[i];
2227
2228                 /* We ignore disabled hints. */
2229                 if (!hint_state_enabled(hint))
2230                         continue;
2231
2232                 if (RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
2233                         return hint;
2234         }
2235
2236         return NULL;
2237 }
2238
2239 /*
2240  * regexeq
2241  *
2242  * Returns TRUE on match, FALSE on no match.
2243  *
2244  *   s1 --- the data to match against
2245  *   s2 --- the pattern
2246  *
2247  * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
2248  */
2249 static bool
2250 regexpeq(const char *s1, const char *s2)
2251 {
2252         NameData        name;
2253         text       *regexp;
2254         Datum           result;
2255
2256         strcpy(name.data, s1);
2257         regexp = cstring_to_text(s2);
2258
2259         result = DirectFunctionCall2Coll(nameregexeq,
2260                                                                          DEFAULT_COLLATION_OID,
2261                                                                          NameGetDatum(&name),
2262                                                                          PointerGetDatum(regexp));
2263         return DatumGetBool(result);
2264 }
2265
2266 static void
2267 delete_indexes(ScanMethodHint *hint, RelOptInfo *rel, Oid relationObjectId)
2268 {
2269         ListCell           *cell;
2270         ListCell           *prev;
2271         ListCell           *next;
2272         StringInfoData  buf;
2273
2274         /*
2275          * We delete all the IndexOptInfo list and prevent you from being usable by
2276          * a scan.
2277          */
2278         if (hint->enforce_mask == ENABLE_SEQSCAN ||
2279                 hint->enforce_mask == ENABLE_TIDSCAN)
2280         {
2281                 list_free_deep(rel->indexlist);
2282                 rel->indexlist = NIL;
2283                 hint->base.state = HINT_STATE_USED;
2284
2285                 return;
2286         }
2287
2288         /*
2289          * When a list of indexes is not specified, we just use all indexes.
2290          */
2291         if (hint->indexnames == NIL)
2292                 return;
2293
2294         /*
2295          * Leaving only an specified index, we delete it from a IndexOptInfo list
2296          * other than it.
2297          */
2298         prev = NULL;
2299         if (pg_hint_plan_debug_print)
2300                 initStringInfo(&buf);
2301
2302         for (cell = list_head(rel->indexlist); cell; cell = next)
2303         {
2304                 IndexOptInfo   *info = (IndexOptInfo *) lfirst(cell);
2305                 char               *indexname = get_rel_name(info->indexoid);
2306                 ListCell           *l;
2307                 bool                    use_index = false;
2308
2309                 next = lnext(cell);
2310
2311                 foreach(l, hint->indexnames)
2312                 {
2313                         char   *hintname = (char *) lfirst(l);
2314                         bool    result;
2315
2316                         if (hint->regexp)
2317                                 result = regexpeq(indexname, hintname);
2318                         else
2319                                 result = RelnameCmp(&indexname, &hintname) == 0;
2320
2321                         if (result)
2322                         {
2323                                 use_index = true;
2324                                 if (pg_hint_plan_debug_print)
2325                                 {
2326                                         appendStringInfoCharMacro(&buf, ' ');
2327                                         quote_value(&buf, indexname);
2328                                 }
2329
2330                                 break;
2331                         }
2332                 }
2333
2334                 /*
2335                  * to make the index a candidate when definition of this index is
2336                  * matched with the index's definition of current_hint.
2337                  */
2338                 if (OidIsValid(relationObjectId) && !use_index)
2339                 {
2340                         foreach(l, current_hint->parent_index_infos)
2341                         {
2342                                 int                                     i;
2343                                 HeapTuple                       ht_idx;
2344                                 ParentIndexInfo    *p_info = (ParentIndexInfo *)lfirst(l);
2345
2346                                 /* check to match the parameter of unique */
2347                                 if (p_info->indisunique != info->unique)
2348                                         continue;
2349
2350                                 /* check to match the parameter of index's method */
2351                                 if (p_info->method != info->relam)
2352                                         continue;
2353
2354                                 /* to check to match the indexkey's configuration */
2355                                 if ((list_length(p_info->column_names)) !=
2356                                          info->ncolumns)
2357                                         continue;
2358
2359                                 /* check to match the indexkey's configuration */
2360                                 for (i = 0; i < info->ncolumns; i++)
2361                                 {
2362                                         char       *c_attname = NULL;
2363                                         char       *p_attname = NULL;
2364
2365                                         p_attname =
2366                                                 list_nth(p_info->column_names, i);
2367
2368                                         /* both are expressions */
2369                                         if (info->indexkeys[i] == 0 && !p_attname)
2370                                                 continue;
2371
2372                                         /* one's column is expression, the other is not */
2373                                         if (info->indexkeys[i] == 0 || !p_attname)
2374                                                 break;
2375
2376                                         c_attname = get_attname(relationObjectId,
2377                                                                                                 info->indexkeys[i]);
2378
2379                                         if (strcmp(p_attname, c_attname) != 0)
2380                                                 break;
2381
2382                                         if (p_info->indcollation[i] != info->indexcollations[i])
2383                                                 break;
2384
2385                                         if (p_info->opclass[i] != info->opcintype[i])
2386                                                 break;
2387
2388                                         if (((p_info->indoption[i] & INDOPTION_DESC) != 0) !=
2389                                                 info->reverse_sort[i])
2390                                                 break;
2391
2392                                         if (((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0) !=
2393                                                 info->nulls_first[i])
2394                                                 break;
2395
2396                                 }
2397
2398                                 if (i != info->ncolumns)
2399                                         continue;
2400
2401                                 if ((p_info->expression_str && (info->indexprs != NIL)) ||
2402                                         (p_info->indpred_str && (info->indpred != NIL)))
2403                                 {
2404                                         /*
2405                                          * Fetch the pg_index tuple by the Oid of the index
2406                                          */
2407                                         ht_idx = SearchSysCache1(INDEXRELID,
2408                                                                                          ObjectIdGetDatum(info->indexoid));
2409
2410                                         /* check to match the expression's parameter of index */
2411                                         if (p_info->expression_str &&
2412                                                 !heap_attisnull(ht_idx, Anum_pg_index_indexprs))
2413                                         {
2414                                                 Datum       exprsDatum;
2415                                                 bool        isnull;
2416                                                 Datum       result;
2417
2418                                                 /*
2419                                                  * to change the expression's parameter of child's
2420                                                  * index to strings
2421                                                  */
2422                                                 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
2423                                                                                                          Anum_pg_index_indexprs,
2424                                                                                                          &isnull);
2425
2426                                                 result = DirectFunctionCall2(pg_get_expr,
2427                                                                                                          exprsDatum,
2428                                                                                                          ObjectIdGetDatum(
2429                                                                                                                  relationObjectId));
2430
2431                                                 if (strcmp(p_info->expression_str,
2432                                                                    text_to_cstring(DatumGetTextP(result))) != 0)
2433                                                 {
2434                                                         /* Clean up */
2435                                                         ReleaseSysCache(ht_idx);
2436
2437                                                         continue;
2438                                                 }
2439                                         }
2440
2441                                         /* Check to match the predicate's paraameter of index */
2442                                         if (p_info->indpred_str &&
2443                                                 !heap_attisnull(ht_idx, Anum_pg_index_indpred))
2444                                         {
2445                                                 Datum       predDatum;
2446                                                 bool        isnull;
2447                                                 Datum       result;
2448
2449                                                 /*
2450                                                  * to change the predicate's parabeter of child's
2451                                                  * index to strings
2452                                                  */
2453                                                 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
2454                                                                                                          Anum_pg_index_indpred,
2455                                                                                                          &isnull);
2456
2457                                                 result = DirectFunctionCall2(pg_get_expr,
2458                                                                                                          predDatum,
2459                                                                                                          ObjectIdGetDatum(
2460                                                                                                                  relationObjectId));
2461
2462                                                 if (strcmp(p_info->indpred_str,
2463                                                                    text_to_cstring(DatumGetTextP(result))) != 0)
2464                                                 {
2465                                                         /* Clean up */
2466                                                         ReleaseSysCache(ht_idx);
2467
2468                                                         continue;
2469                                                 }
2470                                         }
2471
2472                                         /* Clean up */
2473                                         ReleaseSysCache(ht_idx);
2474                                 }
2475                                 else if (p_info->expression_str || (info->indexprs != NIL))
2476                                         continue;
2477                                 else if (p_info->indpred_str || (info->indpred != NIL))
2478                                         continue;
2479
2480                                 use_index = true;
2481
2482                                 /* to log the candidate of index */
2483                                 if (pg_hint_plan_debug_print)
2484                                 {
2485                                         appendStringInfoCharMacro(&buf, ' ');
2486                                         quote_value(&buf, indexname);
2487                                 }
2488
2489                                 break;
2490                         }
2491                 }
2492
2493                 if (!use_index)
2494                         rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
2495                 else
2496                         prev = cell;
2497
2498                 pfree(indexname);
2499         }
2500
2501         if (pg_hint_plan_debug_print)
2502         {
2503                 char   *relname;
2504                 StringInfoData  rel_buf;
2505
2506                 if (OidIsValid(relationObjectId))
2507                         relname = get_rel_name(relationObjectId);
2508                 else
2509                         relname = hint->relname;
2510
2511                 initStringInfo(&rel_buf);
2512                 quote_value(&rel_buf, relname);
2513
2514                 ereport(LOG,
2515                                 (errmsg("available indexes for %s(%s):%s",
2516                                          hint->base.keyword,
2517                                          rel_buf.data,
2518                                          buf.data)));
2519                 pfree(buf.data);
2520                 pfree(rel_buf.data);
2521         }
2522 }
2523
2524 /* 
2525  * Return information of index definition.
2526  */
2527 static ParentIndexInfo *
2528 get_parent_index_info(Oid indexoid, Oid relid)
2529 {
2530         ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
2531         Relation            indexRelation;
2532         Form_pg_index   index;
2533         char               *attname;
2534         int                             i;
2535
2536         indexRelation = index_open(indexoid, RowExclusiveLock);
2537
2538         index = indexRelation->rd_index;
2539
2540         p_info->indisunique = index->indisunique;
2541         p_info->method = indexRelation->rd_rel->relam;
2542
2543         p_info->column_names = NIL;
2544         p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
2545         p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
2546         p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
2547
2548         for (i = 0; i < index->indnatts; i++)
2549         {
2550                 attname = get_attname(relid, index->indkey.values[i]);
2551                 p_info->column_names = lappend(p_info->column_names, attname);
2552
2553                 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
2554                 p_info->opclass[i] = indexRelation->rd_opcintype[i];
2555                 p_info->indoption[i] = indexRelation->rd_indoption[i];
2556         }
2557
2558         /*
2559          * to check to match the expression's paraameter of index with child indexes
2560          */
2561         p_info->expression_str = NULL;
2562         if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs))
2563         {
2564                 Datum       exprsDatum;
2565                 bool            isnull;
2566                 Datum           result;
2567
2568                 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
2569                                                                          Anum_pg_index_indexprs, &isnull);
2570
2571                 result = DirectFunctionCall2(pg_get_expr,
2572                                                                          exprsDatum,
2573                                                                          ObjectIdGetDatum(relid));
2574
2575                 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
2576         }
2577
2578         /*
2579          * to check to match the predicate's paraameter of index with child indexes
2580          */
2581         p_info->indpred_str = NULL;
2582         if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred))
2583         {
2584                 Datum       predDatum;
2585                 bool            isnull;
2586                 Datum           result;
2587
2588                 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
2589                                                                          Anum_pg_index_indpred, &isnull);
2590
2591                 result = DirectFunctionCall2(pg_get_expr,
2592                                                                          predDatum,
2593                                                                          ObjectIdGetDatum(relid));
2594
2595                 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
2596         }
2597
2598         index_close(indexRelation, NoLock);
2599
2600         return p_info;
2601 }
2602
2603 static void
2604 pg_hint_plan_get_relation_info(PlannerInfo *root, Oid relationObjectId,
2605                                                            bool inhparent, RelOptInfo *rel)
2606 {
2607         ScanMethodHint *hint;
2608
2609         if (prev_get_relation_info)
2610                 (*prev_get_relation_info) (root, relationObjectId, inhparent, rel);
2611
2612         /* Do nothing if we don't have valid hint in this context. */
2613         if (!current_hint)
2614                 return;
2615
2616         if (inhparent)
2617         {
2618                 /* store does relids of parent table. */
2619                 current_hint->parent_relid = rel->relid;
2620                 current_hint->parent_rel_oid = relationObjectId;
2621         }
2622         else if (current_hint->parent_relid != 0)
2623         {
2624                 /*
2625                  * We use the same GUC parameter if this table is the child table of a
2626                  * table called pg_hint_plan_get_relation_info just before that.
2627                  */
2628                 ListCell   *l;
2629
2630                 /* append_rel_list contains all append rels; ignore others */
2631                 foreach(l, root->append_rel_list)
2632                 {
2633                         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
2634
2635                         /* This rel is child table. */
2636                         if (appinfo->parent_relid == current_hint->parent_relid &&
2637                                 appinfo->child_relid == rel->relid)
2638                         {
2639                                 if (current_hint->parent_hint)
2640                                         delete_indexes(current_hint->parent_hint, rel,
2641                                                                    relationObjectId);
2642
2643                                 return;
2644                         }
2645                 }
2646
2647                 /* This rel is not inherit table. */
2648                 current_hint->parent_relid = 0;
2649                 current_hint->parent_rel_oid = InvalidOid;
2650                 current_hint->parent_hint = NULL;
2651         }
2652
2653         /*
2654          * If scan method hint was given, reset GUC parameters which control
2655          * planner behavior about choosing scan methods.
2656          */
2657         if ((hint = find_scan_hint(root, rel)) == NULL)
2658         {
2659                 set_scan_config_options(current_hint->init_scan_mask,
2660                                                                 current_hint->context);
2661                 return;
2662         }
2663         set_scan_config_options(hint->enforce_mask, current_hint->context);
2664         hint->base.state = HINT_STATE_USED;
2665
2666         if (inhparent)
2667         {
2668                 Relation    relation;
2669                 List       *indexoidlist;
2670                 ListCell   *l;
2671
2672                 current_hint->parent_hint = hint;
2673
2674                 relation = heap_open(relationObjectId, NoLock);
2675                 indexoidlist = RelationGetIndexList(relation);
2676
2677                 foreach(l, indexoidlist)
2678                 {
2679                         Oid         indexoid = lfirst_oid(l);
2680                         char       *indexname = get_rel_name(indexoid);
2681                         bool        use_index = false;
2682                         ListCell   *lc;
2683                         ParentIndexInfo *parent_index_info;
2684
2685                         foreach(lc, hint->indexnames)
2686                         {
2687                                 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
2688                                 {
2689                                         use_index = true;
2690                                         break;
2691                                 }
2692                         }
2693                         if (!use_index)
2694                                 continue;
2695
2696                         parent_index_info = get_parent_index_info(indexoid,
2697                                                                                                           relationObjectId);
2698                         current_hint->parent_index_infos =
2699                                 lappend(current_hint->parent_index_infos, parent_index_info);
2700                 }
2701                 heap_close(relation, NoLock);
2702         }
2703         else
2704                 delete_indexes(hint, rel, InvalidOid);
2705 }
2706
2707 /*
2708  * Return index of relation which matches given aliasname, or 0 if not found.
2709  * If same aliasname was used multiple times in a query, return -1.
2710  */
2711 static int
2712 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
2713                                          const char *str)
2714 {
2715         int             i;
2716         Index   found = 0;
2717
2718         for (i = 1; i < root->simple_rel_array_size; i++)
2719         {
2720                 ListCell   *l;
2721
2722                 if (root->simple_rel_array[i] == NULL)
2723                         continue;
2724
2725                 Assert(i == root->simple_rel_array[i]->relid);
2726
2727                 if (RelnameCmp(&aliasname,
2728                                            &root->simple_rte_array[i]->eref->aliasname) != 0)
2729                         continue;
2730
2731                 foreach(l, initial_rels)
2732                 {
2733                         RelOptInfo *rel = (RelOptInfo *) lfirst(l);
2734
2735                         if (rel->reloptkind == RELOPT_BASEREL)
2736                         {
2737                                 if (rel->relid != i)
2738                                         continue;
2739                         }
2740                         else
2741                         {
2742                                 Assert(rel->reloptkind == RELOPT_JOINREL);
2743
2744                                 if (!bms_is_member(i, rel->relids))
2745                                         continue;
2746                         }
2747
2748                         if (found != 0)
2749                         {
2750                                 hint_ereport(str,
2751                                                          ("Relation name \"%s\" is ambiguous.",
2752                                                           aliasname));
2753                                 return -1;
2754                         }
2755
2756                         found = i;
2757                         break;
2758                 }
2759
2760         }
2761
2762         return found;
2763 }
2764
2765 /*
2766  * Return join hint which matches given joinrelids.
2767  */
2768 static JoinMethodHint *
2769 find_join_hint(Relids joinrelids)
2770 {
2771         List       *join_hint;
2772         ListCell   *l;
2773
2774         join_hint = current_hint->join_hint_level[bms_num_members(joinrelids)];
2775
2776         foreach(l, join_hint)
2777         {
2778                 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
2779
2780                 if (bms_equal(joinrelids, hint->joinrelids))
2781                         return hint;
2782         }
2783
2784         return NULL;
2785 }
2786
2787 static Relids
2788 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
2789         PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
2790 {
2791         OuterInnerRels *outer_rels;
2792         OuterInnerRels *inner_rels;
2793         Relids                  outer_relids;
2794         Relids                  inner_relids;
2795         Relids                  join_relids;
2796         JoinMethodHint *hint;
2797
2798         if (outer_inner->relation != NULL)
2799         {
2800                 return bms_make_singleton(
2801                                         find_relid_aliasname(root, outer_inner->relation,
2802                                                                                  initial_rels,
2803                                                                                  leading_hint->base.hint_str));
2804         }
2805
2806         outer_rels = lfirst(outer_inner->outer_inner_pair->head);
2807         inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
2808
2809         outer_relids = OuterInnerJoinCreate(outer_rels,
2810                                                                                 leading_hint,
2811                                                                                 root,
2812                                                                                 initial_rels,
2813                                                                                 hstate,
2814                                                                                 nbaserel);
2815         inner_relids = OuterInnerJoinCreate(inner_rels,
2816                                                                                 leading_hint,
2817                                                                                 root,
2818                                                                                 initial_rels,
2819                                                                                 hstate,
2820                                                                                 nbaserel);
2821
2822         join_relids = bms_add_members(outer_relids, inner_relids);
2823
2824         if (bms_num_members(join_relids) > nbaserel)
2825                 return join_relids;
2826
2827         /*
2828          * If we don't have join method hint, create new one for the
2829          * join combination with all join methods are enabled.
2830          */
2831         hint = find_join_hint(join_relids);
2832         if (hint == NULL)
2833         {
2834                 /*
2835                  * Here relnames is not set, since Relids bitmap is sufficient to
2836                  * control paths of this query afterward.
2837                  */
2838                 hint = (JoinMethodHint *) JoinMethodHintCreate(
2839                                         leading_hint->base.hint_str,
2840                                         HINT_LEADING,
2841                                         HINT_KEYWORD_LEADING);
2842                 hint->base.state = HINT_STATE_USED;
2843                 hint->nrels = bms_num_members(join_relids);
2844                 hint->enforce_mask = ENABLE_ALL_JOIN;
2845                 hint->joinrelids = bms_copy(join_relids);
2846                 hint->inner_nrels = bms_num_members(inner_relids);
2847                 hint->inner_joinrelids = bms_copy(inner_relids);
2848
2849                 hstate->join_hint_level[hint->nrels] =
2850                         lappend(hstate->join_hint_level[hint->nrels], hint);
2851         }
2852         else
2853         {
2854                 hint->inner_nrels = bms_num_members(inner_relids);
2855                 hint->inner_joinrelids = bms_copy(inner_relids);
2856         }
2857
2858         return join_relids;
2859 }
2860
2861 /*
2862  * Transform join method hint into handy form.
2863  *
2864  *   - create bitmap of relids from alias names, to make it easier to check
2865  *     whether a join path matches a join method hint.
2866  *   - add join method hints which are necessary to enforce join order
2867  *     specified by Leading hint
2868  */
2869 static bool
2870 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
2871                 List *initial_rels, JoinMethodHint **join_method_hints)
2872 {
2873         int                             i;
2874         int                             relid;
2875         Relids                  joinrelids;
2876         int                             njoinrels;
2877         ListCell           *l;
2878         char               *relname;
2879         LeadingHint        *lhint = NULL;
2880
2881         /*
2882          * Create bitmap of relids from alias names for each join method hint.
2883          * Bitmaps are more handy than strings in join searching.
2884          */
2885         for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
2886         {
2887                 JoinMethodHint *hint = hstate->join_hints[i];
2888                 int     j;
2889
2890                 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
2891                         continue;
2892
2893                 bms_free(hint->joinrelids);
2894                 hint->joinrelids = NULL;
2895                 relid = 0;
2896                 for (j = 0; j < hint->nrels; j++)
2897                 {
2898                         relname = hint->relnames[j];
2899
2900                         relid = find_relid_aliasname(root, relname, initial_rels,
2901                                                                                  hint->base.hint_str);
2902
2903                         if (relid == -1)
2904                                 hint->base.state = HINT_STATE_ERROR;
2905
2906                         if (relid <= 0)
2907                                 break;
2908
2909                         if (bms_is_member(relid, hint->joinrelids))
2910                         {
2911                                 hint_ereport(hint->base.hint_str,
2912                                                          ("Relation name \"%s\" is duplicated.", relname));
2913                                 hint->base.state = HINT_STATE_ERROR;
2914                                 break;
2915                         }
2916
2917                         hint->joinrelids = bms_add_member(hint->joinrelids, relid);
2918                 }
2919
2920                 if (relid <= 0 || hint->base.state == HINT_STATE_ERROR)
2921                         continue;
2922
2923                 hstate->join_hint_level[hint->nrels] =
2924                         lappend(hstate->join_hint_level[hint->nrels], hint);
2925         }
2926
2927         /* Do nothing if no Leading hint was supplied. */
2928         if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
2929                 return false;
2930
2931         /*
2932          * Decide to use Leading hint。
2933          */
2934         for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
2935         {
2936                 LeadingHint        *leading_hint = (LeadingHint *)hstate->leading_hint[i];
2937                 Relids                  relids;
2938
2939                 if (leading_hint->base.state == HINT_STATE_ERROR)
2940                         continue;
2941
2942                 relid = 0;
2943                 relids = NULL;
2944
2945                 foreach(l, leading_hint->relations)
2946                 {
2947                         relname = (char *)lfirst(l);;
2948
2949                         relid = find_relid_aliasname(root, relname, initial_rels,
2950                                                                                  leading_hint->base.hint_str);
2951                         if (relid == -1)
2952                                 leading_hint->base.state = HINT_STATE_ERROR;
2953
2954                         if (relid <= 0)
2955                                 break;
2956
2957                         if (bms_is_member(relid, relids))
2958                         {
2959                                 hint_ereport(leading_hint->base.hint_str,
2960                                                          ("Relation name \"%s\" is duplicated.", relname));
2961                                 leading_hint->base.state = HINT_STATE_ERROR;
2962                                 break;
2963                         }
2964
2965                         relids = bms_add_member(relids, relid);
2966                 }
2967
2968                 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
2969                         continue;
2970
2971                 if (lhint != NULL)
2972                 {
2973                         hint_ereport(lhint->base.hint_str,
2974                                  ("Conflict %s hint.", HintTypeName[lhint->base.type]));
2975                         lhint->base.state = HINT_STATE_DUPLICATION;
2976                 }
2977                 leading_hint->base.state = HINT_STATE_USED;
2978                 lhint = leading_hint;
2979         }
2980
2981         /* check to exist Leading hint marked with 'used'. */
2982         if (lhint == NULL)
2983                 return false;
2984
2985         /*
2986          * We need join method hints which fit specified join order in every join
2987          * level.  For example, Leading(A B C) virtually requires following join
2988          * method hints, if no join method hint supplied:
2989          *   - level 1: none
2990          *   - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
2991          *   - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
2992          *
2993          * If we already have join method hint which fits specified join order in
2994          * that join level, we leave it as-is and don't add new hints.
2995          */
2996         joinrelids = NULL;
2997         njoinrels = 0;
2998         if (lhint->outer_inner == NULL)
2999         {
3000                 foreach(l, lhint->relations)
3001                 {
3002                         JoinMethodHint *hint;
3003
3004                         relname = (char *)lfirst(l);
3005
3006                         /*
3007                          * Find relid of the relation which has given name.  If we have the
3008                          * name given in Leading hint multiple times in the join, nothing to
3009                          * do.
3010                          */
3011                         relid = find_relid_aliasname(root, relname, initial_rels,
3012                                                                                  hstate->hint_str);
3013
3014                         /* Create bitmap of relids for current join level. */
3015                         joinrelids = bms_add_member(joinrelids, relid);
3016                         njoinrels++;
3017
3018                         /* We never have join method hint for single relation. */
3019                         if (njoinrels < 2)
3020                                 continue;
3021
3022                         /*
3023                          * If we don't have join method hint, create new one for the
3024                          * join combination with all join methods are enabled.
3025                          */
3026                         hint = find_join_hint(joinrelids);
3027                         if (hint == NULL)
3028                         {
3029                                 /*
3030                                  * Here relnames is not set, since Relids bitmap is sufficient
3031                                  * to control paths of this query afterward.
3032                                  */
3033                                 hint = (JoinMethodHint *) JoinMethodHintCreate(
3034                                                                                         lhint->base.hint_str,
3035                                                                                         HINT_LEADING,
3036                                                                                         HINT_KEYWORD_LEADING);
3037                                 hint->base.state = HINT_STATE_USED;
3038                                 hint->nrels = njoinrels;
3039                                 hint->enforce_mask = ENABLE_ALL_JOIN;
3040                                 hint->joinrelids = bms_copy(joinrelids);
3041                         }
3042
3043                         join_method_hints[njoinrels] = hint;
3044
3045                         if (njoinrels >= nbaserel)
3046                                 break;
3047                 }
3048                 bms_free(joinrelids);
3049
3050                 if (njoinrels < 2)
3051                         return false;
3052
3053                 /*
3054                  * Delete all join hints which have different combination from Leading
3055                  * hint.
3056                  */
3057                 for (i = 2; i <= njoinrels; i++)
3058                 {
3059                         list_free(hstate->join_hint_level[i]);
3060
3061                         hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
3062                 }
3063         }
3064         else
3065         {
3066                 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
3067                                                                                   lhint,
3068                                           root,
3069                                           initial_rels,
3070                                                                                   hstate,
3071                                                                                   nbaserel);
3072
3073                 njoinrels = bms_num_members(joinrelids);
3074                 Assert(njoinrels >= 2);
3075
3076                 /*
3077                  * Delete all join hints which have different combination from Leading
3078                  * hint.
3079                  */
3080                 for (i = 2;i <= njoinrels; i++)
3081                 {
3082                         if (hstate->join_hint_level[i] != NIL)
3083                         {
3084                                 ListCell *prev = NULL;
3085                                 ListCell *next = NULL;
3086                                 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
3087                                 {
3088
3089                                         JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
3090
3091                                         next = lnext(l);
3092
3093                                         if (hint->inner_nrels == 0 &&
3094                                                 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
3095                                                   bms_equal(bms_union(hint->joinrelids, joinrelids),
3096                                                   hint->joinrelids)))
3097                                         {
3098                                                 hstate->join_hint_level[i] =
3099                                                         list_delete_cell(hstate->join_hint_level[i], l,
3100                                                                                          prev);
3101                                         }
3102                                         else
3103                                                 prev = l;
3104                                 }
3105                         }
3106                 }
3107
3108                 bms_free(joinrelids);
3109         }
3110
3111         if (hint_state_enabled(lhint))
3112         {
3113                 set_join_config_options(DISABLE_ALL_JOIN, current_hint->context);
3114                 return true;
3115         }
3116         return false;
3117 }
3118
3119 /*
3120  * set_plain_rel_pathlist
3121  *        Build access paths for a plain relation (no subquery, no inheritance)
3122  *
3123  * This function was copied and edited from set_plain_rel_pathlist() in
3124  * src/backend/optimizer/path/allpaths.c
3125  */
3126 static void
3127 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
3128 {
3129         /* Consider sequential scan */
3130 #if PG_VERSION_NUM >= 90200
3131         add_path(rel, create_seqscan_path(root, rel, NULL));
3132 #else
3133         add_path(rel, create_seqscan_path(root, rel));
3134 #endif
3135
3136         /* Consider index scans */
3137         create_index_paths(root, rel);
3138
3139         /* Consider TID scans */
3140         create_tidscan_paths(root, rel);
3141
3142         /* Now find the cheapest of the paths for this rel */
3143         set_cheapest(rel);
3144 }
3145
3146 static void
3147 rebuild_scan_path(HintState *hstate, PlannerInfo *root, int level,
3148                                   List *initial_rels)
3149 {
3150         ListCell   *l;
3151
3152         foreach(l, initial_rels)
3153         {
3154                 RelOptInfo         *rel = (RelOptInfo *) lfirst(l);
3155                 RangeTblEntry  *rte;
3156                 ScanMethodHint *hint;
3157
3158                 /* Skip relations which we can't choose scan method. */
3159                 if (rel->reloptkind != RELOPT_BASEREL || rel->rtekind != RTE_RELATION)
3160                         continue;
3161
3162                 rte = root->simple_rte_array[rel->relid];
3163
3164                 /* We can't force scan method of foreign tables */
3165                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
3166                         continue;
3167
3168                 /*
3169                  * Create scan paths with GUC parameters which are at the beginning of
3170                  * planner if scan method hint is not specified, otherwise use
3171                  * specified hints and mark the hint as used.
3172                  */
3173                 if ((hint = find_scan_hint(root, rel)) == NULL)
3174                         set_scan_config_options(hstate->init_scan_mask,
3175                                                                         hstate->context);
3176                 else
3177                 {
3178                         set_scan_config_options(hint->enforce_mask, hstate->context);
3179                         hint->base.state = HINT_STATE_USED;
3180                 }
3181
3182                 list_free_deep(rel->pathlist);
3183                 rel->pathlist = NIL;
3184                 if (rte->inh)
3185                 {
3186                         /* It's an "append relation", process accordingly */
3187                         set_append_rel_pathlist(root, rel, rel->relid, rte);
3188                 }
3189                 else
3190                 {
3191                         set_plain_rel_pathlist(root, rel, rte);
3192                 }
3193         }
3194
3195         /*
3196          * Restore the GUC variables we set above.
3197          */
3198         set_scan_config_options(hstate->init_scan_mask, hstate->context);
3199 }
3200
3201 /*
3202  * wrapper of make_join_rel()
3203  *
3204  * call make_join_rel() after changing enable_* parameters according to given
3205  * hints.
3206  */
3207 static RelOptInfo *
3208 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
3209 {
3210         Relids                  joinrelids;
3211         JoinMethodHint *hint;
3212         RelOptInfo         *rel;
3213         int                             save_nestlevel;
3214
3215         joinrelids = bms_union(rel1->relids, rel2->relids);
3216         hint = find_join_hint(joinrelids);
3217         bms_free(joinrelids);
3218
3219         if (!hint)
3220                 return pg_hint_plan_make_join_rel(root, rel1, rel2);
3221
3222         if (hint->inner_nrels == 0)
3223         {
3224                 save_nestlevel = NewGUCNestLevel();
3225
3226                 set_join_config_options(hint->enforce_mask, current_hint->context);
3227
3228                 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
3229                 hint->base.state = HINT_STATE_USED;
3230
3231                 /*
3232                  * Restore the GUC variables we set above.
3233                  */
3234                 AtEOXact_GUC(true, save_nestlevel);
3235         }
3236         else
3237                 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
3238
3239         return rel;
3240 }
3241
3242 /*
3243  * TODO : comment
3244  */
3245 static void
3246 add_paths_to_joinrel_wrapper(PlannerInfo *root,
3247                                                          RelOptInfo *joinrel,
3248                                                          RelOptInfo *outerrel,
3249                                                          RelOptInfo *innerrel,
3250                                                          JoinType jointype,
3251                                                          SpecialJoinInfo *sjinfo,
3252                                                          List *restrictlist)
3253 {
3254         ScanMethodHint *scan_hint = NULL;
3255         Relids                  joinrelids;
3256         JoinMethodHint *join_hint;
3257         int                             save_nestlevel;
3258
3259         if ((scan_hint = find_scan_hint(root, innerrel)) != NULL)
3260         {
3261                 set_scan_config_options(scan_hint->enforce_mask, current_hint->context);
3262                 scan_hint->base.state = HINT_STATE_USED;
3263         }
3264
3265         joinrelids = bms_union(outerrel->relids, innerrel->relids);
3266         join_hint = find_join_hint(joinrelids);
3267         bms_free(joinrelids);
3268
3269         if (join_hint && join_hint->inner_nrels != 0)
3270         {
3271                 save_nestlevel = NewGUCNestLevel();
3272
3273                 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
3274                 {
3275
3276                         set_join_config_options(join_hint->enforce_mask,
3277                                                                         current_hint->context);
3278
3279                         add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
3280                                                                  sjinfo, restrictlist);
3281                         join_hint->base.state = HINT_STATE_USED;
3282                 }
3283                 else
3284                 {
3285                         set_join_config_options(DISABLE_ALL_JOIN, current_hint->context);
3286                         add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
3287                                                                  sjinfo, restrictlist);
3288                 }
3289
3290                 /*
3291                  * Restore the GUC variables we set above.
3292                  */
3293                 AtEOXact_GUC(true, save_nestlevel);
3294         }
3295         else
3296                 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
3297                                                          sjinfo, restrictlist);
3298
3299         if (scan_hint != NULL)
3300                 set_scan_config_options(current_hint->init_scan_mask,
3301                                                                 current_hint->context);
3302 }
3303
3304 static int
3305 get_num_baserels(List *initial_rels)
3306 {
3307         int                     nbaserel = 0;
3308         ListCell   *l;
3309
3310         foreach(l, initial_rels)
3311         {
3312                 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3313
3314                 if (rel->reloptkind == RELOPT_BASEREL)
3315                         nbaserel++;
3316                 else if (rel->reloptkind ==RELOPT_JOINREL)
3317                         nbaserel+= bms_num_members(rel->relids);
3318                 else
3319                 {
3320                         /* other values not expected here */
3321                         elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
3322                 }
3323         }
3324
3325         return nbaserel;
3326 }
3327
3328 static RelOptInfo *
3329 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
3330                                                  List *initial_rels)
3331 {
3332         JoinMethodHint    **join_method_hints;
3333         int                                     nbaserel;
3334         RelOptInfo                 *rel;
3335         int                                     i;
3336         bool                            leading_hint_enable;
3337
3338         /*
3339          * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
3340          * valid hint is supplied.
3341          */
3342         if (!current_hint)
3343         {
3344                 if (prev_join_search)
3345                         return (*prev_join_search) (root, levels_needed, initial_rels);
3346                 else if (enable_geqo && levels_needed >= geqo_threshold)
3347                         return geqo(root, levels_needed, initial_rels);
3348                 else
3349                         return standard_join_search(root, levels_needed, initial_rels);
3350         }
3351
3352         /* We apply scan method hint rebuild scan path. */
3353         rebuild_scan_path(current_hint, root, levels_needed, initial_rels);
3354
3355         /*
3356          * In the case using GEQO, only scan method hints and Set hints have
3357          * effect.  Join method and join order is not controllable by hints.
3358          */
3359         if (enable_geqo && levels_needed >= geqo_threshold)
3360                 return geqo(root, levels_needed, initial_rels);
3361
3362         nbaserel = get_num_baserels(initial_rels);
3363         current_hint->join_hint_level = palloc0(sizeof(List *) * (nbaserel + 1));
3364         join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
3365
3366         leading_hint_enable = transform_join_hints(current_hint, root, nbaserel,
3367                                                                                            initial_rels, join_method_hints);
3368
3369         rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
3370
3371         for (i = 2; i <= nbaserel; i++)
3372         {
3373                 list_free(current_hint->join_hint_level[i]);
3374
3375                 /* free Leading hint only */
3376                 if (join_method_hints[i] != NULL &&
3377                         join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
3378                         JoinMethodHintDelete(join_method_hints[i]);
3379         }
3380         pfree(current_hint->join_hint_level);
3381         pfree(join_method_hints);
3382
3383         if (leading_hint_enable)
3384                 set_join_config_options(current_hint->init_join_mask,
3385                                                                 current_hint->context);
3386
3387         return rel;
3388 }
3389
3390 /*
3391  * set_rel_pathlist
3392  *        Build access paths for a base relation
3393  *
3394  * This function was copied and edited from set_rel_pathlist() in
3395  * src/backend/optimizer/path/allpaths.c
3396  */
3397 static void
3398 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
3399                                  Index rti, RangeTblEntry *rte)
3400 {
3401 #if PG_VERSION_NUM >= 90200
3402         if (IS_DUMMY_REL(rel))
3403         {
3404                 /* We already proved the relation empty, so nothing more to do */
3405         }
3406         else if (rte->inh)
3407 #else
3408         if (rte->inh)
3409 #endif
3410         {
3411                 /* It's an "append relation", process accordingly */
3412                 set_append_rel_pathlist(root, rel, rti, rte);
3413         }
3414         else
3415         {
3416                 if (rel->rtekind == RTE_RELATION)
3417                 {
3418                         if (rte->relkind == RELKIND_RELATION)
3419                         {
3420                                 /* Plain relation */
3421                                 set_plain_rel_pathlist(root, rel, rte);
3422                         }
3423                         else
3424                                 elog(ERROR, "unexpected relkind: %c", rte->relkind);
3425                 }
3426                 else
3427                         elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
3428         }
3429 }
3430
3431 #define standard_join_search pg_hint_plan_standard_join_search
3432 #define join_search_one_level pg_hint_plan_join_search_one_level
3433 #define make_join_rel make_join_rel_wrapper
3434 #include "core.c"
3435
3436 #undef make_join_rel
3437 #define make_join_rel pg_hint_plan_make_join_rel
3438 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
3439 #include "make_join_rel.c"