OSDN Git Service

Fix a crash bug in case debug_query_string is NULL
[pghintplan/pg_hint_plan.git] / pg_hint_plan.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_hint_plan.c
4  *                hinting on how to execute a query for PostgreSQL
5  *
6  * Copyright (c) 2012-2017, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
7  *
8  *-------------------------------------------------------------------------
9  */
10 #include <string.h>
11
12 #include "postgres.h"
13 #include "catalog/pg_collation.h"
14 #include "catalog/pg_index.h"
15 #include "commands/prepare.h"
16 #include "mb/pg_wchar.h"
17 #include "miscadmin.h"
18 #include "nodes/nodeFuncs.h"
19 #include "nodes/params.h"
20 #include "nodes/relation.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/geqo.h"
24 #include "optimizer/joininfo.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/plancat.h"
28 #include "optimizer/planner.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/restrictinfo.h"
31 #include "parser/analyze.h"
32 #include "parser/parsetree.h"
33 #include "parser/scansup.h"
34 #include "tcop/utility.h"
35 #include "utils/builtins.h"
36 #include "utils/lsyscache.h"
37 #include "utils/memutils.h"
38 #include "utils/rel.h"
39 #include "utils/snapmgr.h"
40 #include "utils/syscache.h"
41 #include "utils/resowner.h"
42
43 #include "catalog/pg_class.h"
44
45 #include "executor/spi.h"
46 #include "catalog/pg_type.h"
47
48 #include "plpgsql.h"
49
50 /* partially copied from pg_stat_statements */
51 #include "normalize_query.h"
52
53 /* PostgreSQL */
54 #include "access/htup_details.h"
55
56 #ifdef PG_MODULE_MAGIC
57 PG_MODULE_MAGIC;
58 #endif
59
60 #define BLOCK_COMMENT_START             "/*"
61 #define BLOCK_COMMENT_END               "*/"
62 #define HINT_COMMENT_KEYWORD    "+"
63 #define HINT_START                              BLOCK_COMMENT_START HINT_COMMENT_KEYWORD
64 #define HINT_END                                BLOCK_COMMENT_END
65
66 /* hint keywords */
67 #define HINT_SEQSCAN                    "SeqScan"
68 #define HINT_INDEXSCAN                  "IndexScan"
69 #define HINT_INDEXSCANREGEXP    "IndexScanRegexp"
70 #define HINT_BITMAPSCAN                 "BitmapScan"
71 #define HINT_BITMAPSCANREGEXP   "BitmapScanRegexp"
72 #define HINT_TIDSCAN                    "TidScan"
73 #define HINT_NOSEQSCAN                  "NoSeqScan"
74 #define HINT_NOINDEXSCAN                "NoIndexScan"
75 #define HINT_NOBITMAPSCAN               "NoBitmapScan"
76 #define HINT_NOTIDSCAN                  "NoTidScan"
77 #define HINT_INDEXONLYSCAN              "IndexOnlyScan"
78 #define HINT_INDEXONLYSCANREGEXP        "IndexOnlyScanRegexp"
79 #define HINT_NOINDEXONLYSCAN    "NoIndexOnlyScan"
80 #define HINT_PARALLEL                   "Parallel"
81
82 #define HINT_NESTLOOP                   "NestLoop"
83 #define HINT_MERGEJOIN                  "MergeJoin"
84 #define HINT_HASHJOIN                   "HashJoin"
85 #define HINT_NONESTLOOP                 "NoNestLoop"
86 #define HINT_NOMERGEJOIN                "NoMergeJoin"
87 #define HINT_NOHASHJOIN                 "NoHashJoin"
88 #define HINT_LEADING                    "Leading"
89 #define HINT_SET                                "Set"
90 #define HINT_ROWS                               "Rows"
91
92 #define HINT_ARRAY_DEFAULT_INITSIZE 8
93
94 #define hint_ereport(str, detail) \
95         do { \
96                 ereport(pg_hint_plan_message_level,             \
97                         (errmsg("pg_hint_plan%s: hint syntax error at or near \"%s\"", qnostr, (str)), \
98                          errdetail detail)); \
99                 msgqno = qno; \
100         } while(0)
101
102 #define skip_space(str) \
103         while (isspace(*str)) \
104                 str++;
105
106 enum
107 {
108         ENABLE_SEQSCAN = 0x01,
109         ENABLE_INDEXSCAN = 0x02,
110         ENABLE_BITMAPSCAN = 0x04,
111         ENABLE_TIDSCAN = 0x08,
112         ENABLE_INDEXONLYSCAN = 0x10
113 } SCAN_TYPE_BITS;
114
115 enum
116 {
117         ENABLE_NESTLOOP = 0x01,
118         ENABLE_MERGEJOIN = 0x02,
119         ENABLE_HASHJOIN = 0x04
120 } JOIN_TYPE_BITS;
121
122 #define ENABLE_ALL_SCAN (ENABLE_SEQSCAN | ENABLE_INDEXSCAN | \
123                                                  ENABLE_BITMAPSCAN | ENABLE_TIDSCAN | \
124                                                  ENABLE_INDEXONLYSCAN)
125 #define ENABLE_ALL_JOIN (ENABLE_NESTLOOP | ENABLE_MERGEJOIN | ENABLE_HASHJOIN)
126 #define DISABLE_ALL_SCAN 0
127 #define DISABLE_ALL_JOIN 0
128
129 /* hint keyword of enum type*/
130 typedef enum HintKeyword
131 {
132         HINT_KEYWORD_SEQSCAN,
133         HINT_KEYWORD_INDEXSCAN,
134         HINT_KEYWORD_INDEXSCANREGEXP,
135         HINT_KEYWORD_BITMAPSCAN,
136         HINT_KEYWORD_BITMAPSCANREGEXP,
137         HINT_KEYWORD_TIDSCAN,
138         HINT_KEYWORD_NOSEQSCAN,
139         HINT_KEYWORD_NOINDEXSCAN,
140         HINT_KEYWORD_NOBITMAPSCAN,
141         HINT_KEYWORD_NOTIDSCAN,
142         HINT_KEYWORD_INDEXONLYSCAN,
143         HINT_KEYWORD_INDEXONLYSCANREGEXP,
144         HINT_KEYWORD_NOINDEXONLYSCAN,
145
146         HINT_KEYWORD_NESTLOOP,
147         HINT_KEYWORD_MERGEJOIN,
148         HINT_KEYWORD_HASHJOIN,
149         HINT_KEYWORD_NONESTLOOP,
150         HINT_KEYWORD_NOMERGEJOIN,
151         HINT_KEYWORD_NOHASHJOIN,
152
153         HINT_KEYWORD_LEADING,
154         HINT_KEYWORD_SET,
155         HINT_KEYWORD_ROWS,
156         HINT_KEYWORD_PARALLEL,
157
158         HINT_KEYWORD_UNRECOGNIZED
159 } HintKeyword;
160
161 #define SCAN_HINT_ACCEPTS_INDEX_NAMES(kw) \
162         (kw == HINT_KEYWORD_INDEXSCAN ||                        \
163          kw == HINT_KEYWORD_INDEXSCANREGEXP ||          \
164          kw == HINT_KEYWORD_INDEXONLYSCAN ||            \
165          kw == HINT_KEYWORD_INDEXONLYSCANREGEXP ||      \
166          kw == HINT_KEYWORD_BITMAPSCAN ||                               \
167          kw == HINT_KEYWORD_BITMAPSCANREGEXP)
168
169
170 typedef struct Hint Hint;
171 typedef struct HintState HintState;
172
173 typedef Hint *(*HintCreateFunction) (const char *hint_str,
174                                                                          const char *keyword,
175                                                                          HintKeyword hint_keyword);
176 typedef void (*HintDeleteFunction) (Hint *hint);
177 typedef void (*HintDescFunction) (Hint *hint, StringInfo buf, bool nolf);
178 typedef int (*HintCmpFunction) (const Hint *a, const Hint *b);
179 typedef const char *(*HintParseFunction) (Hint *hint, HintState *hstate,
180                                                                                   Query *parse, const char *str);
181
182 /* hint types */
183 #define NUM_HINT_TYPE   6
184 typedef enum HintType
185 {
186         HINT_TYPE_SCAN_METHOD,
187         HINT_TYPE_JOIN_METHOD,
188         HINT_TYPE_LEADING,
189         HINT_TYPE_SET,
190         HINT_TYPE_ROWS,
191         HINT_TYPE_PARALLEL
192 } HintType;
193
194 typedef enum HintTypeBitmap
195 {
196         HINT_BM_SCAN_METHOD = 1,
197         HINT_BM_PARALLEL = 2
198 } HintTypeBitmap;
199
200 static const char *HintTypeName[] = {
201         "scan method",
202         "join method",
203         "leading",
204         "set",
205         "rows",
206         "parallel"
207 };
208
209 /* hint status */
210 typedef enum HintStatus
211 {
212         HINT_STATE_NOTUSED = 0,         /* specified relation not used in query */
213         HINT_STATE_USED,                        /* hint is used */
214         HINT_STATE_DUPLICATION,         /* specified hint duplication */
215         HINT_STATE_ERROR                        /* execute error (parse error does not include
216                                                                  * it) */
217 } HintStatus;
218
219 #define hint_state_enabled(hint) ((hint)->base.state == HINT_STATE_NOTUSED || \
220                                                                   (hint)->base.state == HINT_STATE_USED)
221
222 static unsigned int qno = 0;
223 static unsigned int msgqno = 0;
224 static char qnostr[32];
225 static const char *current_hint_str = NULL;
226
227 /* common data for all hints. */
228 struct Hint
229 {
230         const char                 *hint_str;           /* must not do pfree */
231         const char                 *keyword;            /* must not do pfree */
232         HintKeyword                     hint_keyword;
233         HintType                        type;
234         HintStatus                      state;
235         HintDeleteFunction      delete_func;
236         HintDescFunction        desc_func;
237         HintCmpFunction         cmp_func;
238         HintParseFunction       parse_func;
239 };
240
241 /* scan method hints */
242 typedef struct ScanMethodHint
243 {
244         Hint                    base;
245         char               *relname;
246         List               *indexnames;
247         bool                    regexp;
248         unsigned char   enforce_mask;
249 } ScanMethodHint;
250
251 typedef struct ParentIndexInfo
252 {
253         bool            indisunique;
254         Oid                     method;
255         List       *column_names;
256         char       *expression_str;
257         Oid                *indcollation;
258         Oid                *opclass;
259         int16      *indoption;
260         char       *indpred_str;
261 } ParentIndexInfo;
262
263 /* join method hints */
264 typedef struct JoinMethodHint
265 {
266         Hint                    base;
267         int                             nrels;
268         int                             inner_nrels;
269         char              **relnames;
270         unsigned char   enforce_mask;
271         Relids                  joinrelids;
272         Relids                  inner_joinrelids;
273 } JoinMethodHint;
274
275 /* join order hints */
276 typedef struct OuterInnerRels
277 {
278         char   *relation;
279         List   *outer_inner_pair;
280 } OuterInnerRels;
281
282 typedef struct LeadingHint
283 {
284         Hint                    base;
285         List               *relations;  /* relation names specified in Leading hint */
286         OuterInnerRels *outer_inner;
287 } LeadingHint;
288
289 /* change a run-time parameter hints */
290 typedef struct SetHint
291 {
292         Hint    base;
293         char   *name;                           /* name of variable */
294         char   *value;
295         List   *words;
296 } SetHint;
297
298 /* rows hints */
299 typedef enum RowsValueType {
300         RVT_ABSOLUTE,           /* Rows(... #1000) */
301         RVT_ADD,                        /* Rows(... +1000) */
302         RVT_SUB,                        /* Rows(... -1000) */
303         RVT_MULTI,                      /* Rows(... *1.2) */
304 } RowsValueType;
305 typedef struct RowsHint
306 {
307         Hint                    base;
308         int                             nrels;
309         int                             inner_nrels;
310         char              **relnames;
311         Relids                  joinrelids;
312         Relids                  inner_joinrelids;
313         char               *rows_str;
314         RowsValueType   value_type;
315         double                  rows;
316 } RowsHint;
317
318 /* parallel hints */
319 typedef struct ParallelHint
320 {
321         Hint                    base;
322         char               *relname;
323         char               *nworkers_str;       /* original string of nworkers */
324         int                             nworkers;               /* num of workers specified by Worker */
325         bool                    force_parallel; /* force parallel scan */
326 } ParallelHint;
327
328 /*
329  * Describes a context of hint processing.
330  */
331 struct HintState
332 {
333         char               *hint_str;                   /* original hint string */
334
335         /* all hint */
336         int                             nall_hints;                     /* # of valid all hints */
337         int                             max_all_hints;          /* # of slots for all hints */
338         Hint              **all_hints;                  /* parsed all hints */
339
340         /* # of each hints */
341         int                             num_hints[NUM_HINT_TYPE];
342
343         /* for scan method hints */
344         ScanMethodHint **scan_hints;            /* parsed scan hints */
345
346         /* Initial values of parameters  */
347         int                             init_scan_mask;         /* enable_* mask */
348         int                             init_nworkers;          /* max_parallel_workers_per_gather */
349         /* min_parallel_table_scan_size*/
350         int                             init_min_para_tablescan_size;
351         /* min_parallel_index_scan_size*/
352         int                             init_min_para_indexscan_size;
353         int                             init_paratup_cost;      /* parallel_tuple_cost */
354         int                             init_parasetup_cost;/* parallel_setup_cost */
355
356         Index                   parent_relid;           /* inherit parent of table relid */
357         ScanMethodHint *parent_scan_hint;       /* scan hint for the parent */
358         ParallelHint   *parent_parallel_hint; /* parallel hint for the parent */
359         List               *parent_index_infos; /* list of parent table's index */
360
361         JoinMethodHint **join_hints;            /* parsed join hints */
362         int                             init_join_mask;         /* initial value join parameter */
363         List              **join_hint_level;
364         LeadingHint       **leading_hint;               /* parsed Leading hints */
365         SetHint           **set_hints;                  /* parsed Set hints */
366         GucContext              context;                        /* which GUC parameters can we set? */
367         RowsHint          **rows_hints;                 /* parsed Rows hints */
368         ParallelHint  **parallel_hints;         /* parsed Parallel hints */
369 };
370
371 /*
372  * Describes a hint parser module which is bound with particular hint keyword.
373  */
374 typedef struct HintParser
375 {
376         char                       *keyword;
377         HintCreateFunction      create_func;
378         HintKeyword                     hint_keyword;
379 } HintParser;
380
381 /* Module callbacks */
382 void            _PG_init(void);
383 void            _PG_fini(void);
384
385 static void push_hint(HintState *hstate);
386 static void pop_hint(void);
387
388 static void pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query);
389 static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
390                                                                                  ParamListInfo boundParams);
391 static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
392                                                                                         int levels_needed,
393                                                                                         List *initial_rels);
394
395 /* Scan method hint callbacks */
396 static Hint *ScanMethodHintCreate(const char *hint_str, const char *keyword,
397                                                                   HintKeyword hint_keyword);
398 static void ScanMethodHintDelete(ScanMethodHint *hint);
399 static void ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf);
400 static int ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b);
401 static const char *ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate,
402                                                                            Query *parse, const char *str);
403
404 /* Join method hint callbacks */
405 static Hint *JoinMethodHintCreate(const char *hint_str, const char *keyword,
406                                                                   HintKeyword hint_keyword);
407 static void JoinMethodHintDelete(JoinMethodHint *hint);
408 static void JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf);
409 static int JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b);
410 static const char *JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate,
411                                                                            Query *parse, const char *str);
412
413 /* Leading hint callbacks */
414 static Hint *LeadingHintCreate(const char *hint_str, const char *keyword,
415                                                            HintKeyword hint_keyword);
416 static void LeadingHintDelete(LeadingHint *hint);
417 static void LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf);
418 static int LeadingHintCmp(const LeadingHint *a, const LeadingHint *b);
419 static const char *LeadingHintParse(LeadingHint *hint, HintState *hstate,
420                                                                         Query *parse, const char *str);
421
422 /* Set hint callbacks */
423 static Hint *SetHintCreate(const char *hint_str, const char *keyword,
424                                                    HintKeyword hint_keyword);
425 static void SetHintDelete(SetHint *hint);
426 static void SetHintDesc(SetHint *hint, StringInfo buf, bool nolf);
427 static int SetHintCmp(const SetHint *a, const SetHint *b);
428 static const char *SetHintParse(SetHint *hint, HintState *hstate, Query *parse,
429                                                                 const char *str);
430
431 /* Rows hint callbacks */
432 static Hint *RowsHintCreate(const char *hint_str, const char *keyword,
433                                                         HintKeyword hint_keyword);
434 static void RowsHintDelete(RowsHint *hint);
435 static void RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf);
436 static int RowsHintCmp(const RowsHint *a, const RowsHint *b);
437 static const char *RowsHintParse(RowsHint *hint, HintState *hstate,
438                                                                  Query *parse, const char *str);
439
440 /* Parallel hint callbacks */
441 static Hint *ParallelHintCreate(const char *hint_str, const char *keyword,
442                                                                 HintKeyword hint_keyword);
443 static void ParallelHintDelete(ParallelHint *hint);
444 static void ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf);
445 static int ParallelHintCmp(const ParallelHint *a, const ParallelHint *b);
446 static const char *ParallelHintParse(ParallelHint *hint, HintState *hstate,
447                                                                          Query *parse, const char *str);
448
449 static void quote_value(StringInfo buf, const char *value);
450
451 static const char *parse_quoted_value(const char *str, char **word,
452                                                                           bool truncate);
453
454 RelOptInfo *pg_hint_plan_standard_join_search(PlannerInfo *root,
455                                                                                           int levels_needed,
456                                                                                           List *initial_rels);
457 void pg_hint_plan_join_search_one_level(PlannerInfo *root, int level);
458 void pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
459                                                                    Index rti, RangeTblEntry *rte);
460 static void create_plain_partial_paths(PlannerInfo *root,
461                                                                                                         RelOptInfo *rel);
462 static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
463                                                                         List *live_childrels);
464 static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
465                                                                           ListCell *other_rels);
466 static void make_rels_by_clauseless_joins(PlannerInfo *root,
467                                                                                   RelOptInfo *old_rel,
468                                                                                   ListCell *other_rels);
469 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
470 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
471                                                                    RangeTblEntry *rte);
472 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
473                                                                         Index rti, RangeTblEntry *rte);
474 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
475                                                                            List *live_childrels,
476                                                                            List *all_child_pathkeys,
477                                                                            List *partitioned_rels);
478 static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
479                                                                           RelOptInfo *rel,
480                                                                           Relids required_outer);
481 static List *accumulate_append_subpath(List *subpaths, Path *path);
482 RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
483                                                                            RelOptInfo *rel2);
484
485 static void pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate,
486                                                                                   PLpgSQL_stmt *stmt);
487 static void pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate,
488                                                                                   PLpgSQL_stmt *stmt);
489 static void plpgsql_query_erase_callback(ResourceReleasePhase phase,
490                                                                                  bool isCommit,
491                                                                                  bool isTopLevel,
492                                                                                  void *arg);
493 static int set_config_option_noerror(const char *name, const char *value,
494                                                   GucContext context, GucSource source,
495                                                   GucAction action, bool changeVal, int elevel);
496 static void setup_scan_method_enforcement(ScanMethodHint *scanhint,
497                                                                                   HintState *state);
498 static int set_config_int32_option(const char *name, int32 value,
499                                                                         GucContext context);
500
501 /* GUC variables */
502 static bool     pg_hint_plan_enable_hint = true;
503 static int debug_level = 0;
504 static int      pg_hint_plan_message_level = INFO;
505 /* Default is off, to keep backward compatibility. */
506 static bool     pg_hint_plan_enable_hint_table = false;
507
508 static int plpgsql_recurse_level = 0;           /* PLpgSQL recursion level            */
509 static int hint_inhibit_level = 0;                      /* Inhibit hinting if this is above 0 */
510                                                                                         /* (This could not be above 1)        */
511 static int max_hint_nworkers = -1;              /* Maximum nworkers of Workers hints */
512
513 static const struct config_enum_entry parse_messages_level_options[] = {
514         {"debug", DEBUG2, true},
515         {"debug5", DEBUG5, false},
516         {"debug4", DEBUG4, false},
517         {"debug3", DEBUG3, false},
518         {"debug2", DEBUG2, false},
519         {"debug1", DEBUG1, false},
520         {"log", LOG, false},
521         {"info", INFO, false},
522         {"notice", NOTICE, false},
523         {"warning", WARNING, false},
524         {"error", ERROR, false},
525         /*
526          * {"fatal", FATAL, true},
527          * {"panic", PANIC, true},
528          */
529         {NULL, 0, false}
530 };
531
532 static const struct config_enum_entry parse_debug_level_options[] = {
533         {"off", 0, false},
534         {"on", 1, false},
535         {"detailed", 2, false},
536         {"verbose", 3, false},
537         {"0", 0, true},
538         {"1", 1, true},
539         {"2", 2, true},
540         {"3", 3, true},
541         {"no", 0, true},
542         {"yes", 1, true},
543         {"false", 0, true},
544         {"true", 1, true},
545         {NULL, 0, false}
546 };
547
548 /* Saved hook values in case of unload */
549 static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
550 static planner_hook_type prev_planner = NULL;
551 static join_search_hook_type prev_join_search = NULL;
552 static set_rel_pathlist_hook_type prev_set_rel_pathlist = NULL;
553
554 /* Hold reference to currently active hint */
555 static HintState *current_hint_state = NULL;
556
557 /*
558  * List of hint contexts.  We treat the head of the list as the Top of the
559  * context stack, so current_hint_state always points the first element of this
560  * list.
561  */
562 static List *HintStateStack = NIL;
563
564 /*
565  * Holds statement name during executing EXECUTE command.  NULL for other
566  * statements.
567  */
568 static char        *stmt_name = NULL;
569
570 static const HintParser parsers[] = {
571         {HINT_SEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_SEQSCAN},
572         {HINT_INDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCAN},
573         {HINT_INDEXSCANREGEXP, ScanMethodHintCreate, HINT_KEYWORD_INDEXSCANREGEXP},
574         {HINT_BITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_BITMAPSCAN},
575         {HINT_BITMAPSCANREGEXP, ScanMethodHintCreate,
576          HINT_KEYWORD_BITMAPSCANREGEXP},
577         {HINT_TIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_TIDSCAN},
578         {HINT_NOSEQSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOSEQSCAN},
579         {HINT_NOINDEXSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXSCAN},
580         {HINT_NOBITMAPSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOBITMAPSCAN},
581         {HINT_NOTIDSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOTIDSCAN},
582         {HINT_INDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_INDEXONLYSCAN},
583         {HINT_INDEXONLYSCANREGEXP, ScanMethodHintCreate,
584          HINT_KEYWORD_INDEXONLYSCANREGEXP},
585         {HINT_NOINDEXONLYSCAN, ScanMethodHintCreate, HINT_KEYWORD_NOINDEXONLYSCAN},
586
587         {HINT_NESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NESTLOOP},
588         {HINT_MERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_MERGEJOIN},
589         {HINT_HASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_HASHJOIN},
590         {HINT_NONESTLOOP, JoinMethodHintCreate, HINT_KEYWORD_NONESTLOOP},
591         {HINT_NOMERGEJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOMERGEJOIN},
592         {HINT_NOHASHJOIN, JoinMethodHintCreate, HINT_KEYWORD_NOHASHJOIN},
593
594         {HINT_LEADING, LeadingHintCreate, HINT_KEYWORD_LEADING},
595         {HINT_SET, SetHintCreate, HINT_KEYWORD_SET},
596         {HINT_ROWS, RowsHintCreate, HINT_KEYWORD_ROWS},
597         {HINT_PARALLEL, ParallelHintCreate, HINT_KEYWORD_PARALLEL},
598
599         {NULL, NULL, HINT_KEYWORD_UNRECOGNIZED}
600 };
601
602 PLpgSQL_plugin  plugin_funcs = {
603         NULL,
604         NULL,
605         NULL,
606         pg_hint_plan_plpgsql_stmt_beg,
607         pg_hint_plan_plpgsql_stmt_end,
608         NULL,
609         NULL,
610 };
611
612 /*
613  * Module load callbacks
614  */
615 void
616 _PG_init(void)
617 {
618         PLpgSQL_plugin  **var_ptr;
619
620         /* Define custom GUC variables. */
621         DefineCustomBoolVariable("pg_hint_plan.enable_hint",
622                          "Force planner to use plans specified in the hint comment preceding to the query.",
623                                                          NULL,
624                                                          &pg_hint_plan_enable_hint,
625                                                          true,
626                                                      PGC_USERSET,
627                                                          0,
628                                                          NULL,
629                                                          NULL,
630                                                          NULL);
631
632         DefineCustomEnumVariable("pg_hint_plan.debug_print",
633                                                          "Logs results of hint parsing.",
634                                                          NULL,
635                                                          &debug_level,
636                                                          false,
637                                                          parse_debug_level_options,
638                                                          PGC_USERSET,
639                                                          0,
640                                                          NULL,
641                                                          NULL,
642                                                          NULL);
643
644         DefineCustomEnumVariable("pg_hint_plan.parse_messages",
645                                                          "Message level of parse errors.",
646                                                          NULL,
647                                                          &pg_hint_plan_message_level,
648                                                          INFO,
649                                                          parse_messages_level_options,
650                                                          PGC_USERSET,
651                                                          0,
652                                                          NULL,
653                                                          NULL,
654                                                          NULL);
655
656         DefineCustomEnumVariable("pg_hint_plan.message_level",
657                                                          "Message level of debug messages.",
658                                                          NULL,
659                                                          &pg_hint_plan_message_level,
660                                                          INFO,
661                                                          parse_messages_level_options,
662                                                          PGC_USERSET,
663                                                          0,
664                                                          NULL,
665                                                          NULL,
666                                                          NULL);
667
668         DefineCustomBoolVariable("pg_hint_plan.enable_hint_table",
669                                                          "Let pg_hint_plan look up the hint table.",
670                                                          NULL,
671                                                          &pg_hint_plan_enable_hint_table,
672                                                          false,
673                                                          PGC_USERSET,
674                                                          0,
675                                                          NULL,
676                                                          NULL,
677                                                          NULL);
678
679         /* Install hooks. */
680         prev_post_parse_analyze_hook = post_parse_analyze_hook;
681         post_parse_analyze_hook = pg_hint_plan_post_parse_analyze;
682         prev_planner = planner_hook;
683         planner_hook = pg_hint_plan_planner;
684         prev_join_search = join_search_hook;
685         join_search_hook = pg_hint_plan_join_search;
686         prev_set_rel_pathlist = set_rel_pathlist_hook;
687         set_rel_pathlist_hook = pg_hint_plan_set_rel_pathlist;
688
689         /* setup PL/pgSQL plugin hook */
690         var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
691         *var_ptr = &plugin_funcs;
692
693         RegisterResourceReleaseCallback(plpgsql_query_erase_callback, NULL);
694 }
695
696 /*
697  * Module unload callback
698  * XXX never called
699  */
700 void
701 _PG_fini(void)
702 {
703         PLpgSQL_plugin  **var_ptr;
704
705         /* Uninstall hooks. */
706         post_parse_analyze_hook = prev_post_parse_analyze_hook;
707         planner_hook = prev_planner;
708         join_search_hook = prev_join_search;
709         set_rel_pathlist_hook = prev_set_rel_pathlist;
710
711         /* uninstall PL/pgSQL plugin hook */
712         var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
713         *var_ptr = NULL;
714 }
715
716 /*
717  * create and delete functions the hint object
718  */
719
720 static Hint *
721 ScanMethodHintCreate(const char *hint_str, const char *keyword,
722                                          HintKeyword hint_keyword)
723 {
724         ScanMethodHint *hint;
725
726         hint = palloc(sizeof(ScanMethodHint));
727         hint->base.hint_str = hint_str;
728         hint->base.keyword = keyword;
729         hint->base.hint_keyword = hint_keyword;
730         hint->base.type = HINT_TYPE_SCAN_METHOD;
731         hint->base.state = HINT_STATE_NOTUSED;
732         hint->base.delete_func = (HintDeleteFunction) ScanMethodHintDelete;
733         hint->base.desc_func = (HintDescFunction) ScanMethodHintDesc;
734         hint->base.cmp_func = (HintCmpFunction) ScanMethodHintCmp;
735         hint->base.parse_func = (HintParseFunction) ScanMethodHintParse;
736         hint->relname = NULL;
737         hint->indexnames = NIL;
738         hint->regexp = false;
739         hint->enforce_mask = 0;
740
741         return (Hint *) hint;
742 }
743
744 static void
745 ScanMethodHintDelete(ScanMethodHint *hint)
746 {
747         if (!hint)
748                 return;
749
750         if (hint->relname)
751                 pfree(hint->relname);
752         list_free_deep(hint->indexnames);
753         pfree(hint);
754 }
755
756 static Hint *
757 JoinMethodHintCreate(const char *hint_str, const char *keyword,
758                                          HintKeyword hint_keyword)
759 {
760         JoinMethodHint *hint;
761
762         hint = palloc(sizeof(JoinMethodHint));
763         hint->base.hint_str = hint_str;
764         hint->base.keyword = keyword;
765         hint->base.hint_keyword = hint_keyword;
766         hint->base.type = HINT_TYPE_JOIN_METHOD;
767         hint->base.state = HINT_STATE_NOTUSED;
768         hint->base.delete_func = (HintDeleteFunction) JoinMethodHintDelete;
769         hint->base.desc_func = (HintDescFunction) JoinMethodHintDesc;
770         hint->base.cmp_func = (HintCmpFunction) JoinMethodHintCmp;
771         hint->base.parse_func = (HintParseFunction) JoinMethodHintParse;
772         hint->nrels = 0;
773         hint->inner_nrels = 0;
774         hint->relnames = NULL;
775         hint->enforce_mask = 0;
776         hint->joinrelids = NULL;
777         hint->inner_joinrelids = NULL;
778
779         return (Hint *) hint;
780 }
781
782 static void
783 JoinMethodHintDelete(JoinMethodHint *hint)
784 {
785         if (!hint)
786                 return;
787
788         if (hint->relnames)
789         {
790                 int     i;
791
792                 for (i = 0; i < hint->nrels; i++)
793                         pfree(hint->relnames[i]);
794                 pfree(hint->relnames);
795         }
796
797         bms_free(hint->joinrelids);
798         bms_free(hint->inner_joinrelids);
799         pfree(hint);
800 }
801
802 static Hint *
803 LeadingHintCreate(const char *hint_str, const char *keyword,
804                                   HintKeyword hint_keyword)
805 {
806         LeadingHint        *hint;
807
808         hint = palloc(sizeof(LeadingHint));
809         hint->base.hint_str = hint_str;
810         hint->base.keyword = keyword;
811         hint->base.hint_keyword = hint_keyword;
812         hint->base.type = HINT_TYPE_LEADING;
813         hint->base.state = HINT_STATE_NOTUSED;
814         hint->base.delete_func = (HintDeleteFunction)LeadingHintDelete;
815         hint->base.desc_func = (HintDescFunction) LeadingHintDesc;
816         hint->base.cmp_func = (HintCmpFunction) LeadingHintCmp;
817         hint->base.parse_func = (HintParseFunction) LeadingHintParse;
818         hint->relations = NIL;
819         hint->outer_inner = NULL;
820
821         return (Hint *) hint;
822 }
823
824 static void
825 LeadingHintDelete(LeadingHint *hint)
826 {
827         if (!hint)
828                 return;
829
830         list_free_deep(hint->relations);
831         if (hint->outer_inner)
832                 pfree(hint->outer_inner);
833         pfree(hint);
834 }
835
836 static Hint *
837 SetHintCreate(const char *hint_str, const char *keyword,
838                           HintKeyword hint_keyword)
839 {
840         SetHint    *hint;
841
842         hint = palloc(sizeof(SetHint));
843         hint->base.hint_str = hint_str;
844         hint->base.keyword = keyword;
845         hint->base.hint_keyword = hint_keyword;
846         hint->base.type = HINT_TYPE_SET;
847         hint->base.state = HINT_STATE_NOTUSED;
848         hint->base.delete_func = (HintDeleteFunction) SetHintDelete;
849         hint->base.desc_func = (HintDescFunction) SetHintDesc;
850         hint->base.cmp_func = (HintCmpFunction) SetHintCmp;
851         hint->base.parse_func = (HintParseFunction) SetHintParse;
852         hint->name = NULL;
853         hint->value = NULL;
854         hint->words = NIL;
855
856         return (Hint *) hint;
857 }
858
859 static void
860 SetHintDelete(SetHint *hint)
861 {
862         if (!hint)
863                 return;
864
865         if (hint->name)
866                 pfree(hint->name);
867         if (hint->value)
868                 pfree(hint->value);
869         if (hint->words)
870                 list_free(hint->words);
871         pfree(hint);
872 }
873
874 static Hint *
875 RowsHintCreate(const char *hint_str, const char *keyword,
876                            HintKeyword hint_keyword)
877 {
878         RowsHint *hint;
879
880         hint = palloc(sizeof(RowsHint));
881         hint->base.hint_str = hint_str;
882         hint->base.keyword = keyword;
883         hint->base.hint_keyword = hint_keyword;
884         hint->base.type = HINT_TYPE_ROWS;
885         hint->base.state = HINT_STATE_NOTUSED;
886         hint->base.delete_func = (HintDeleteFunction) RowsHintDelete;
887         hint->base.desc_func = (HintDescFunction) RowsHintDesc;
888         hint->base.cmp_func = (HintCmpFunction) RowsHintCmp;
889         hint->base.parse_func = (HintParseFunction) RowsHintParse;
890         hint->nrels = 0;
891         hint->inner_nrels = 0;
892         hint->relnames = NULL;
893         hint->joinrelids = NULL;
894         hint->inner_joinrelids = NULL;
895         hint->rows_str = NULL;
896         hint->value_type = RVT_ABSOLUTE;
897         hint->rows = 0;
898
899         return (Hint *) hint;
900 }
901
902 static void
903 RowsHintDelete(RowsHint *hint)
904 {
905         if (!hint)
906                 return;
907
908         if (hint->relnames)
909         {
910                 int     i;
911
912                 for (i = 0; i < hint->nrels; i++)
913                         pfree(hint->relnames[i]);
914                 pfree(hint->relnames);
915         }
916
917         bms_free(hint->joinrelids);
918         bms_free(hint->inner_joinrelids);
919         pfree(hint);
920 }
921
922 static Hint *
923 ParallelHintCreate(const char *hint_str, const char *keyword,
924                                   HintKeyword hint_keyword)
925 {
926         ParallelHint *hint;
927
928         hint = palloc(sizeof(ScanMethodHint));
929         hint->base.hint_str = hint_str;
930         hint->base.keyword = keyword;
931         hint->base.hint_keyword = hint_keyword;
932         hint->base.type = HINT_TYPE_PARALLEL;
933         hint->base.state = HINT_STATE_NOTUSED;
934         hint->base.delete_func = (HintDeleteFunction) ParallelHintDelete;
935         hint->base.desc_func = (HintDescFunction) ParallelHintDesc;
936         hint->base.cmp_func = (HintCmpFunction) ParallelHintCmp;
937         hint->base.parse_func = (HintParseFunction) ParallelHintParse;
938         hint->relname = NULL;
939         hint->nworkers = 0;
940         hint->nworkers_str = "0";
941
942         return (Hint *) hint;
943 }
944
945 static void
946 ParallelHintDelete(ParallelHint *hint)
947 {
948         if (!hint)
949                 return;
950
951         if (hint->relname)
952                 pfree(hint->relname);
953         pfree(hint);
954 }
955
956
957 static HintState *
958 HintStateCreate(void)
959 {
960         HintState   *hstate;
961
962         hstate = palloc(sizeof(HintState));
963         hstate->hint_str = NULL;
964         hstate->nall_hints = 0;
965         hstate->max_all_hints = 0;
966         hstate->all_hints = NULL;
967         memset(hstate->num_hints, 0, sizeof(hstate->num_hints));
968         hstate->scan_hints = NULL;
969         hstate->init_scan_mask = 0;
970         hstate->init_nworkers = 0;
971         hstate->init_min_para_tablescan_size = 0;
972         hstate->init_min_para_indexscan_size = 0;
973         hstate->init_paratup_cost = 0;
974         hstate->init_parasetup_cost = 0;
975         hstate->parent_relid = 0;
976         hstate->parent_scan_hint = NULL;
977         hstate->parent_parallel_hint = NULL;
978         hstate->parent_index_infos = NIL;
979         hstate->join_hints = NULL;
980         hstate->init_join_mask = 0;
981         hstate->join_hint_level = NULL;
982         hstate->leading_hint = NULL;
983         hstate->context = superuser() ? PGC_SUSET : PGC_USERSET;
984         hstate->set_hints = NULL;
985         hstate->rows_hints = NULL;
986         hstate->parallel_hints = NULL;
987
988         return hstate;
989 }
990
991 static void
992 HintStateDelete(HintState *hstate)
993 {
994         int                     i;
995
996         if (!hstate)
997                 return;
998
999         if (hstate->hint_str)
1000                 pfree(hstate->hint_str);
1001
1002         for (i = 0; i < hstate->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
1003                 hstate->all_hints[i]->delete_func(hstate->all_hints[i]);
1004         if (hstate->all_hints)
1005                 pfree(hstate->all_hints);
1006         if (hstate->parent_index_infos)
1007                 list_free(hstate->parent_index_infos);
1008 }
1009
1010 /*
1011  * Copy given value into buf, with quoting with '"' if necessary.
1012  */
1013 static void
1014 quote_value(StringInfo buf, const char *value)
1015 {
1016         bool            need_quote = false;
1017         const char *str;
1018
1019         for (str = value; *str != '\0'; str++)
1020         {
1021                 if (isspace(*str) || *str == '(' || *str == ')' || *str == '"')
1022                 {
1023                         need_quote = true;
1024                         appendStringInfoCharMacro(buf, '"');
1025                         break;
1026                 }
1027         }
1028
1029         for (str = value; *str != '\0'; str++)
1030         {
1031                 if (*str == '"')
1032                         appendStringInfoCharMacro(buf, '"');
1033
1034                 appendStringInfoCharMacro(buf, *str);
1035         }
1036
1037         if (need_quote)
1038                 appendStringInfoCharMacro(buf, '"');
1039 }
1040
1041 static void
1042 ScanMethodHintDesc(ScanMethodHint *hint, StringInfo buf, bool nolf)
1043 {
1044         ListCell   *l;
1045
1046         appendStringInfo(buf, "%s(", hint->base.keyword);
1047         if (hint->relname != NULL)
1048         {
1049                 quote_value(buf, hint->relname);
1050                 foreach(l, hint->indexnames)
1051                 {
1052                         appendStringInfoCharMacro(buf, ' ');
1053                         quote_value(buf, (char *) lfirst(l));
1054                 }
1055         }
1056         appendStringInfoString(buf, ")");
1057         if (!nolf)
1058                 appendStringInfoChar(buf, '\n');
1059 }
1060
1061 static void
1062 JoinMethodHintDesc(JoinMethodHint *hint, StringInfo buf, bool nolf)
1063 {
1064         int     i;
1065
1066         appendStringInfo(buf, "%s(", hint->base.keyword);
1067         if (hint->relnames != NULL)
1068         {
1069                 quote_value(buf, hint->relnames[0]);
1070                 for (i = 1; i < hint->nrels; i++)
1071                 {
1072                         appendStringInfoCharMacro(buf, ' ');
1073                         quote_value(buf, hint->relnames[i]);
1074                 }
1075         }
1076         appendStringInfoString(buf, ")");
1077         if (!nolf)
1078                 appendStringInfoChar(buf, '\n');
1079 }
1080
1081 static void
1082 OuterInnerDesc(OuterInnerRels *outer_inner, StringInfo buf)
1083 {
1084         if (outer_inner->relation == NULL)
1085         {
1086                 bool            is_first;
1087                 ListCell   *l;
1088
1089                 is_first = true;
1090
1091                 appendStringInfoCharMacro(buf, '(');
1092                 foreach(l, outer_inner->outer_inner_pair)
1093                 {
1094                         if (is_first)
1095                                 is_first = false;
1096                         else
1097                                 appendStringInfoCharMacro(buf, ' ');
1098
1099                         OuterInnerDesc(lfirst(l), buf);
1100                 }
1101
1102                 appendStringInfoCharMacro(buf, ')');
1103         }
1104         else
1105                 quote_value(buf, outer_inner->relation);
1106 }
1107
1108 static void
1109 LeadingHintDesc(LeadingHint *hint, StringInfo buf, bool nolf)
1110 {
1111         appendStringInfo(buf, "%s(", HINT_LEADING);
1112         if (hint->outer_inner == NULL)
1113         {
1114                 ListCell   *l;
1115                 bool            is_first;
1116
1117                 is_first = true;
1118
1119                 foreach(l, hint->relations)
1120                 {
1121                         if (is_first)
1122                                 is_first = false;
1123                         else
1124                                 appendStringInfoCharMacro(buf, ' ');
1125
1126                         quote_value(buf, (char *) lfirst(l));
1127                 }
1128         }
1129         else
1130                 OuterInnerDesc(hint->outer_inner, buf);
1131
1132         appendStringInfoString(buf, ")");
1133         if (!nolf)
1134                 appendStringInfoChar(buf, '\n');
1135 }
1136
1137 static void
1138 SetHintDesc(SetHint *hint, StringInfo buf, bool nolf)
1139 {
1140         bool            is_first = true;
1141         ListCell   *l;
1142
1143         appendStringInfo(buf, "%s(", HINT_SET);
1144         foreach(l, hint->words)
1145         {
1146                 if (is_first)
1147                         is_first = false;
1148                 else
1149                         appendStringInfoCharMacro(buf, ' ');
1150
1151                 quote_value(buf, (char *) lfirst(l));
1152         }
1153         appendStringInfo(buf, ")");
1154         if (!nolf)
1155                 appendStringInfoChar(buf, '\n');
1156 }
1157
1158 static void
1159 RowsHintDesc(RowsHint *hint, StringInfo buf, bool nolf)
1160 {
1161         int     i;
1162
1163         appendStringInfo(buf, "%s(", hint->base.keyword);
1164         if (hint->relnames != NULL)
1165         {
1166                 quote_value(buf, hint->relnames[0]);
1167                 for (i = 1; i < hint->nrels; i++)
1168                 {
1169                         appendStringInfoCharMacro(buf, ' ');
1170                         quote_value(buf, hint->relnames[i]);
1171                 }
1172         }
1173         appendStringInfo(buf, " %s", hint->rows_str);
1174         appendStringInfoString(buf, ")");
1175         if (!nolf)
1176                 appendStringInfoChar(buf, '\n');
1177 }
1178
1179 static void
1180 ParallelHintDesc(ParallelHint *hint, StringInfo buf, bool nolf)
1181 {
1182         appendStringInfo(buf, "%s(", hint->base.keyword);
1183         if (hint->relname != NULL)
1184         {
1185                 quote_value(buf, hint->relname);
1186
1187                 /* number of workers  */
1188                 appendStringInfoCharMacro(buf, ' ');
1189                 quote_value(buf, hint->nworkers_str);
1190                 /* application mode of num of workers */
1191                 appendStringInfoCharMacro(buf, ' ');
1192                 appendStringInfoString(buf,
1193                                                            (hint->force_parallel ? "hard" : "soft"));
1194         }
1195         appendStringInfoString(buf, ")");
1196         if (!nolf)
1197                 appendStringInfoChar(buf, '\n');
1198 }
1199
1200 /*
1201  * Append string which represents all hints in a given state to buf, with
1202  * preceding title with them.
1203  */
1204 static void
1205 desc_hint_in_state(HintState *hstate, StringInfo buf, const char *title,
1206                                    HintStatus state, bool nolf)
1207 {
1208         int     i, nshown;
1209
1210         appendStringInfo(buf, "%s:", title);
1211         if (!nolf)
1212                 appendStringInfoChar(buf, '\n');
1213
1214         nshown = 0;
1215         for (i = 0; i < hstate->nall_hints; i++)
1216         {
1217                 if (hstate->all_hints[i]->state != state)
1218                         continue;
1219
1220                 hstate->all_hints[i]->desc_func(hstate->all_hints[i], buf, nolf);
1221                 nshown++;
1222         }
1223
1224         if (nolf && nshown == 0)
1225                 appendStringInfoString(buf, "(none)");
1226 }
1227
1228 /*
1229  * Dump contents of given hstate to server log with log level LOG.
1230  */
1231 static void
1232 HintStateDump(HintState *hstate)
1233 {
1234         StringInfoData  buf;
1235
1236         if (!hstate)
1237         {
1238                 elog(LOG, "pg_hint_plan:\nno hint");
1239                 return;
1240         }
1241
1242         initStringInfo(&buf);
1243
1244         appendStringInfoString(&buf, "pg_hint_plan:\n");
1245         desc_hint_in_state(hstate, &buf, "used hint", HINT_STATE_USED, false);
1246         desc_hint_in_state(hstate, &buf, "not used hint", HINT_STATE_NOTUSED, false);
1247         desc_hint_in_state(hstate, &buf, "duplication hint", HINT_STATE_DUPLICATION, false);
1248         desc_hint_in_state(hstate, &buf, "error hint", HINT_STATE_ERROR, false);
1249
1250         elog(LOG, "%s", buf.data);
1251
1252         pfree(buf.data);
1253 }
1254
1255 static void
1256 HintStateDump2(HintState *hstate)
1257 {
1258         StringInfoData  buf;
1259
1260         if (!hstate)
1261         {
1262                 elog(pg_hint_plan_message_level,
1263                          "pg_hint_plan%s: HintStateDump: no hint", qnostr);
1264                 return;
1265         }
1266
1267         initStringInfo(&buf);
1268         appendStringInfo(&buf, "pg_hint_plan%s: HintStateDump: ", qnostr);
1269         desc_hint_in_state(hstate, &buf, "{used hints", HINT_STATE_USED, true);
1270         desc_hint_in_state(hstate, &buf, "}, {not used hints", HINT_STATE_NOTUSED, true);
1271         desc_hint_in_state(hstate, &buf, "}, {duplicate hints", HINT_STATE_DUPLICATION, true);
1272         desc_hint_in_state(hstate, &buf, "}, {error hints", HINT_STATE_ERROR, true);
1273         appendStringInfoChar(&buf, '}');
1274
1275         ereport(pg_hint_plan_message_level,
1276                         (errmsg("%s", buf.data),
1277                          errhidestmt(true),
1278                          errhidecontext(true)));
1279
1280         pfree(buf.data);
1281 }
1282
1283 /*
1284  * compare functions
1285  */
1286
1287 static int
1288 RelnameCmp(const void *a, const void *b)
1289 {
1290         const char *relnamea = *((const char **) a);
1291         const char *relnameb = *((const char **) b);
1292
1293         return strcmp(relnamea, relnameb);
1294 }
1295
1296 static int
1297 ScanMethodHintCmp(const ScanMethodHint *a, const ScanMethodHint *b)
1298 {
1299         return RelnameCmp(&a->relname, &b->relname);
1300 }
1301
1302 static int
1303 JoinMethodHintCmp(const JoinMethodHint *a, const JoinMethodHint *b)
1304 {
1305         int     i;
1306
1307         if (a->nrels != b->nrels)
1308                 return a->nrels - b->nrels;
1309
1310         for (i = 0; i < a->nrels; i++)
1311         {
1312                 int     result;
1313                 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1314                         return result;
1315         }
1316
1317         return 0;
1318 }
1319
1320 static int
1321 LeadingHintCmp(const LeadingHint *a, const LeadingHint *b)
1322 {
1323         return 0;
1324 }
1325
1326 static int
1327 SetHintCmp(const SetHint *a, const SetHint *b)
1328 {
1329         return strcmp(a->name, b->name);
1330 }
1331
1332 static int
1333 RowsHintCmp(const RowsHint *a, const RowsHint *b)
1334 {
1335         int     i;
1336
1337         if (a->nrels != b->nrels)
1338                 return a->nrels - b->nrels;
1339
1340         for (i = 0; i < a->nrels; i++)
1341         {
1342                 int     result;
1343                 if ((result = RelnameCmp(&a->relnames[i], &b->relnames[i])) != 0)
1344                         return result;
1345         }
1346
1347         return 0;
1348 }
1349
1350 static int
1351 ParallelHintCmp(const ParallelHint *a, const ParallelHint *b)
1352 {
1353         return RelnameCmp(&a->relname, &b->relname);
1354 }
1355
1356 static int
1357 HintCmp(const void *a, const void *b)
1358 {
1359         const Hint *hinta = *((const Hint **) a);
1360         const Hint *hintb = *((const Hint **) b);
1361
1362         if (hinta->type != hintb->type)
1363                 return hinta->type - hintb->type;
1364         if (hinta->state == HINT_STATE_ERROR)
1365                 return -1;
1366         if (hintb->state == HINT_STATE_ERROR)
1367                 return 1;
1368         return hinta->cmp_func(hinta, hintb);
1369 }
1370
1371 /*
1372  * Returns byte offset of hint b from hint a.  If hint a was specified before
1373  * b, positive value is returned.
1374  */
1375 static int
1376 HintCmpWithPos(const void *a, const void *b)
1377 {
1378         const Hint *hinta = *((const Hint **) a);
1379         const Hint *hintb = *((const Hint **) b);
1380         int             result;
1381
1382         result = HintCmp(a, b);
1383         if (result == 0)
1384                 result = hinta->hint_str - hintb->hint_str;
1385
1386         return result;
1387 }
1388
1389 /*
1390  * parse functions
1391  */
1392 static const char *
1393 parse_keyword(const char *str, StringInfo buf)
1394 {
1395         skip_space(str);
1396
1397         while (!isspace(*str) && *str != '(' && *str != '\0')
1398                 appendStringInfoCharMacro(buf, *str++);
1399
1400         return str;
1401 }
1402
1403 static const char *
1404 skip_parenthesis(const char *str, char parenthesis)
1405 {
1406         skip_space(str);
1407
1408         if (*str != parenthesis)
1409         {
1410                 if (parenthesis == '(')
1411                         hint_ereport(str, ("Opening parenthesis is necessary."));
1412                 else if (parenthesis == ')')
1413                         hint_ereport(str, ("Closing parenthesis is necessary."));
1414
1415                 return NULL;
1416         }
1417
1418         str++;
1419
1420         return str;
1421 }
1422
1423 /*
1424  * Parse a token from str, and store malloc'd copy into word.  A token can be
1425  * quoted with '"'.  Return value is pointer to unparsed portion of original
1426  * string, or NULL if an error occurred.
1427  *
1428  * Parsed token is truncated within NAMEDATALEN-1 bytes, when truncate is true.
1429  */
1430 static const char *
1431 parse_quoted_value(const char *str, char **word, bool truncate)
1432 {
1433         StringInfoData  buf;
1434         bool                    in_quote;
1435
1436         /* Skip leading spaces. */
1437         skip_space(str);
1438
1439         initStringInfo(&buf);
1440         if (*str == '"')
1441         {
1442                 str++;
1443                 in_quote = true;
1444         }
1445         else
1446                 in_quote = false;
1447
1448         while (true)
1449         {
1450                 if (in_quote)
1451                 {
1452                         /* Double quotation must be closed. */
1453                         if (*str == '\0')
1454                         {
1455                                 pfree(buf.data);
1456                                 hint_ereport(str, ("Unterminated quoted string."));
1457                                 return NULL;
1458                         }
1459
1460                         /*
1461                          * Skip escaped double quotation.
1462                          *
1463                          * We don't allow slash-asterisk and asterisk-slash (delimiters of
1464                          * block comments) to be an object name, so users must specify
1465                          * alias for such object names.
1466                          *
1467                          * Those special names can be allowed if we care escaped slashes
1468                          * and asterisks, but we don't.
1469                          */
1470                         if (*str == '"')
1471                         {
1472                                 str++;
1473                                 if (*str != '"')
1474                                         break;
1475                         }
1476                 }
1477                 else if (isspace(*str) || *str == '(' || *str == ')' || *str == '"' ||
1478                                  *str == '\0')
1479                         break;
1480
1481                 appendStringInfoCharMacro(&buf, *str++);
1482         }
1483
1484         if (buf.len == 0)
1485         {
1486                 hint_ereport(str, ("Zero-length delimited string."));
1487
1488                 pfree(buf.data);
1489
1490                 return NULL;
1491         }
1492
1493         /* Truncate name if it's too long */
1494         if (truncate)
1495                 truncate_identifier(buf.data, strlen(buf.data), true);
1496
1497         *word = buf.data;
1498
1499         return str;
1500 }
1501
1502 static OuterInnerRels *
1503 OuterInnerRelsCreate(char *name, List *outer_inner_list)
1504 {
1505         OuterInnerRels *outer_inner;
1506
1507         outer_inner = palloc(sizeof(OuterInnerRels));
1508         outer_inner->relation = name;
1509         outer_inner->outer_inner_pair = outer_inner_list;
1510
1511         return outer_inner;
1512 }
1513
1514 static const char *
1515 parse_parentheses_Leading_in(const char *str, OuterInnerRels **outer_inner)
1516 {
1517         List   *outer_inner_pair = NIL;
1518
1519         if ((str = skip_parenthesis(str, '(')) == NULL)
1520                 return NULL;
1521
1522         skip_space(str);
1523
1524         /* Store words in parentheses into outer_inner_list. */
1525         while(*str != ')' && *str != '\0')
1526         {
1527                 OuterInnerRels *outer_inner_rels;
1528
1529                 if (*str == '(')
1530                 {
1531                         str = parse_parentheses_Leading_in(str, &outer_inner_rels);
1532                         if (str == NULL)
1533                                 break;
1534                 }
1535                 else
1536                 {
1537                         char   *name;
1538
1539                         if ((str = parse_quoted_value(str, &name, true)) == NULL)
1540                                 break;
1541                         else
1542                                 outer_inner_rels = OuterInnerRelsCreate(name, NIL);
1543                 }
1544
1545                 outer_inner_pair = lappend(outer_inner_pair, outer_inner_rels);
1546                 skip_space(str);
1547         }
1548
1549         if (str == NULL ||
1550                 (str = skip_parenthesis(str, ')')) == NULL)
1551         {
1552                 list_free(outer_inner_pair);
1553                 return NULL;
1554         }
1555
1556         *outer_inner = OuterInnerRelsCreate(NULL, outer_inner_pair);
1557
1558         return str;
1559 }
1560
1561 static const char *
1562 parse_parentheses_Leading(const char *str, List **name_list,
1563         OuterInnerRels **outer_inner)
1564 {
1565         char   *name;
1566         bool    truncate = true;
1567
1568         if ((str = skip_parenthesis(str, '(')) == NULL)
1569                 return NULL;
1570
1571         skip_space(str);
1572         if (*str =='(')
1573         {
1574                 if ((str = parse_parentheses_Leading_in(str, outer_inner)) == NULL)
1575                         return NULL;
1576         }
1577         else
1578         {
1579                 /* Store words in parentheses into name_list. */
1580                 while(*str != ')' && *str != '\0')
1581                 {
1582                         if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1583                         {
1584                                 list_free(*name_list);
1585                                 return NULL;
1586                         }
1587
1588                         *name_list = lappend(*name_list, name);
1589                         skip_space(str);
1590                 }
1591         }
1592
1593         if ((str = skip_parenthesis(str, ')')) == NULL)
1594                 return NULL;
1595         return str;
1596 }
1597
1598 static const char *
1599 parse_parentheses(const char *str, List **name_list, HintKeyword keyword)
1600 {
1601         char   *name;
1602         bool    truncate = true;
1603
1604         if ((str = skip_parenthesis(str, '(')) == NULL)
1605                 return NULL;
1606
1607         skip_space(str);
1608
1609         /* Store words in parentheses into name_list. */
1610         while(*str != ')' && *str != '\0')
1611         {
1612                 if ((str = parse_quoted_value(str, &name, truncate)) == NULL)
1613                 {
1614                         list_free(*name_list);
1615                         return NULL;
1616                 }
1617
1618                 *name_list = lappend(*name_list, name);
1619                 skip_space(str);
1620
1621                 if (keyword == HINT_KEYWORD_INDEXSCANREGEXP ||
1622                         keyword == HINT_KEYWORD_INDEXONLYSCANREGEXP ||
1623                         keyword == HINT_KEYWORD_BITMAPSCANREGEXP ||
1624                         keyword == HINT_KEYWORD_SET)
1625                 {
1626                         truncate = false;
1627                 }
1628         }
1629
1630         if ((str = skip_parenthesis(str, ')')) == NULL)
1631                 return NULL;
1632         return str;
1633 }
1634
1635 static void
1636 parse_hints(HintState *hstate, Query *parse, const char *str)
1637 {
1638         StringInfoData  buf;
1639         char               *head;
1640
1641         initStringInfo(&buf);
1642         while (*str != '\0')
1643         {
1644                 const HintParser *parser;
1645
1646                 /* in error message, we output the comment including the keyword. */
1647                 head = (char *) str;
1648
1649                 /* parse only the keyword of the hint. */
1650                 resetStringInfo(&buf);
1651                 str = parse_keyword(str, &buf);
1652
1653                 for (parser = parsers; parser->keyword != NULL; parser++)
1654                 {
1655                         char   *keyword = parser->keyword;
1656                         Hint   *hint;
1657
1658                         if (pg_strcasecmp(buf.data, keyword) != 0)
1659                                 continue;
1660
1661                         hint = parser->create_func(head, keyword, parser->hint_keyword);
1662
1663                         /* parser of each hint does parse in a parenthesis. */
1664                         if ((str = hint->parse_func(hint, hstate, parse, str)) == NULL)
1665                         {
1666                                 hint->delete_func(hint);
1667                                 pfree(buf.data);
1668                                 return;
1669                         }
1670
1671                         /*
1672                          * Add hint information into all_hints array.  If we don't have
1673                          * enough space, double the array.
1674                          */
1675                         if (hstate->nall_hints == 0)
1676                         {
1677                                 hstate->max_all_hints = HINT_ARRAY_DEFAULT_INITSIZE;
1678                                 hstate->all_hints = (Hint **)
1679                                         palloc(sizeof(Hint *) * hstate->max_all_hints);
1680                         }
1681                         else if (hstate->nall_hints == hstate->max_all_hints)
1682                         {
1683                                 hstate->max_all_hints *= 2;
1684                                 hstate->all_hints = (Hint **)
1685                                         repalloc(hstate->all_hints,
1686                                                          sizeof(Hint *) * hstate->max_all_hints);
1687                         }
1688
1689                         hstate->all_hints[hstate->nall_hints] = hint;
1690                         hstate->nall_hints++;
1691
1692                         skip_space(str);
1693
1694                         break;
1695                 }
1696
1697                 if (parser->keyword == NULL)
1698                 {
1699                         hint_ereport(head,
1700                                                  ("Unrecognized hint keyword \"%s\".", buf.data));
1701                         pfree(buf.data);
1702                         return;
1703                 }
1704         }
1705
1706         pfree(buf.data);
1707 }
1708
1709
1710 /* 
1711  * Get hints from table by client-supplied query string and application name.
1712  */
1713 static const char *
1714 get_hints_from_table(const char *client_query, const char *client_application)
1715 {
1716         const char *search_query =
1717                 "SELECT hints "
1718                 "  FROM hint_plan.hints "
1719                 " WHERE norm_query_string = $1 "
1720                 "   AND ( application_name = $2 "
1721                 "    OR application_name = '' ) "
1722                 " ORDER BY application_name DESC";
1723         static SPIPlanPtr plan = NULL;
1724         char   *hints = NULL;
1725         Oid             argtypes[2] = { TEXTOID, TEXTOID };
1726         Datum   values[2];
1727         bool    nulls[2] = { false, false };
1728         text   *qry;
1729         text   *app;
1730
1731         PG_TRY();
1732         {
1733                 bool snapshot_set = false;
1734
1735                 hint_inhibit_level++;
1736
1737                 if (!ActiveSnapshotSet())
1738                 {
1739                         PushActiveSnapshot(GetTransactionSnapshot());
1740                         snapshot_set = true;
1741                 }
1742         
1743                 SPI_connect();
1744         
1745                 if (plan == NULL)
1746                 {
1747                         SPIPlanPtr      p;
1748                         p = SPI_prepare(search_query, 2, argtypes);
1749                         plan = SPI_saveplan(p);
1750                         SPI_freeplan(p);
1751                 }
1752         
1753                 qry = cstring_to_text(client_query);
1754                 app = cstring_to_text(client_application);
1755                 values[0] = PointerGetDatum(qry);
1756                 values[1] = PointerGetDatum(app);
1757         
1758                 SPI_execute_plan(plan, values, nulls, true, 1);
1759         
1760                 if (SPI_processed > 0)
1761                 {
1762                         char    *buf;
1763         
1764                         hints = SPI_getvalue(SPI_tuptable->vals[0],
1765                                                                  SPI_tuptable->tupdesc, 1);
1766                         /*
1767                          * Here we use SPI_palloc to ensure that hints string is valid even
1768                          * after SPI_finish call.  We can't use simple palloc because it
1769                          * allocates memory in SPI's context and that context is deleted in
1770                          * SPI_finish.
1771                          */
1772                         buf = SPI_palloc(strlen(hints) + 1);
1773                         strcpy(buf, hints);
1774                         hints = buf;
1775                 }
1776         
1777                 SPI_finish();
1778
1779                 if (snapshot_set)
1780                         PopActiveSnapshot();
1781
1782                 hint_inhibit_level--;
1783         }
1784         PG_CATCH();
1785         {
1786                 hint_inhibit_level--;
1787                 PG_RE_THROW();
1788         }
1789         PG_END_TRY();
1790
1791         return hints;
1792 }
1793
1794 /*
1795  * Get client-supplied query string. Addtion to that the jumbled query is
1796  * supplied if the caller requested. From the restriction of JumbleQuery, some
1797  * kind of query needs special amendments. Reutrns NULL if this query doesn't
1798  * change the current hint. This function returns NULL also when something
1799  * wrong has happend and let the caller continue using the current hints.
1800  */
1801 static const char *
1802 get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
1803 {
1804         const char *p = debug_query_string;
1805
1806         /*
1807          * If debug_query_string is set, it is the top level statement. But in some
1808          * cases we reach here with debug_query_string set NULL for example in the
1809          * case of DESCRIBE message handling. We may still see a candidate
1810          * top-level query in pstate in the case.
1811          */
1812         if (!p)
1813         {
1814                 /* We don't see a query string, return NULL */
1815                 if (!pstate->p_sourcetext)
1816                         return NULL;
1817
1818                 p = pstate->p_sourcetext;
1819         }
1820
1821         if (jumblequery != NULL)
1822                 *jumblequery = query;
1823
1824         if (query->commandType == CMD_UTILITY)
1825         {
1826                 Query *target_query = (Query *)query->utilityStmt;
1827
1828                 /*
1829                  * Some CMD_UTILITY statements have a subquery that we can hint on.
1830                  * Since EXPLAIN can be placed before other kind of utility statements
1831                  * and EXECUTE can be contained other kind of utility statements, these
1832                  * conditions are not mutually exclusive and should be considered in
1833                  * this order.
1834                  */
1835                 if (IsA(target_query, ExplainStmt))
1836                 {
1837                         ExplainStmt *stmt = (ExplainStmt *)target_query;
1838                         
1839                         Assert(IsA(stmt->query, Query));
1840                         target_query = (Query *)stmt->query;
1841
1842                         /* strip out the top-level query for further processing */
1843                         if (target_query->commandType == CMD_UTILITY &&
1844                                 target_query->utilityStmt != NULL)
1845                                 target_query = (Query *)target_query->utilityStmt;
1846                 }
1847
1848                 if (IsA(target_query, DeclareCursorStmt))
1849                 {
1850                         DeclareCursorStmt *stmt = (DeclareCursorStmt *)target_query;
1851                         Query *query = (Query *)stmt->query;
1852
1853                         /* the target must be CMD_SELECT in this case */
1854                         Assert(IsA(query, Query) && query->commandType == CMD_SELECT);
1855                         target_query = query;
1856                 }
1857
1858                 if (IsA(target_query, CreateTableAsStmt))
1859                 {
1860                         CreateTableAsStmt  *stmt = (CreateTableAsStmt *) target_query;
1861
1862                         Assert(IsA(stmt->query, Query));
1863                         target_query = (Query *) stmt->query;
1864
1865                         /* strip out the top-level query for further processing */
1866                         if (target_query->commandType == CMD_UTILITY &&
1867                                 target_query->utilityStmt != NULL)
1868                                 target_query = (Query *)target_query->utilityStmt;
1869                 }
1870
1871                 if (IsA(target_query, ExecuteStmt))
1872                 {
1873                         /*
1874                          * Use the prepared query for EXECUTE. The Query for jumble
1875                          * also replaced with the corresponding one.
1876                          */
1877                         ExecuteStmt *stmt = (ExecuteStmt *)target_query;
1878                         PreparedStatement  *entry;
1879
1880                         entry = FetchPreparedStatement(stmt->name, true);
1881                         p = entry->plansource->query_string;
1882                         target_query = (Query *) linitial (entry->plansource->query_list);
1883                 }
1884                         
1885                 /* JumbleQuery accespts only a non-utility Query */
1886                 if (!IsA(target_query, Query) ||
1887                         target_query->utilityStmt != NULL)
1888                         target_query = NULL;
1889
1890                 if (jumblequery)
1891                         *jumblequery = target_query;
1892         }
1893         /* Return NULL if the pstate is not identical to the top-level query */
1894         else if (strcmp(pstate->p_sourcetext, p) != 0)
1895                 p = NULL;
1896
1897         return p;
1898 }
1899
1900 /*
1901  * Get hints from the head block comment in client-supplied query string.
1902  */
1903 static const char *
1904 get_hints_from_comment(const char *p)
1905 {
1906         const char *hint_head;
1907         char       *head;
1908         char       *tail;
1909         int                     len;
1910
1911         if (p == NULL)
1912                 return NULL;
1913
1914         /* extract query head comment. */
1915         hint_head = strstr(p, HINT_START);
1916         if (hint_head == NULL)
1917                 return NULL;
1918         for (;p < hint_head; p++)
1919         {
1920                 /*
1921                  * Allow these characters precedes hint comment:
1922                  *   - digits
1923                  *   - alphabets which are in ASCII range
1924                  *   - space, tabs and new-lines
1925                  *   - underscores, for identifier
1926                  *   - commas, for SELECT clause, EXPLAIN and PREPARE
1927                  *   - parentheses, for EXPLAIN and PREPARE
1928                  *
1929                  * Note that we don't use isalpha() nor isalnum() in ctype.h here to
1930                  * avoid behavior which depends on locale setting.
1931                  */
1932                 if (!(*p >= '0' && *p <= '9') &&
1933                         !(*p >= 'A' && *p <= 'Z') &&
1934                         !(*p >= 'a' && *p <= 'z') &&
1935                         !isspace(*p) &&
1936                         *p != '_' &&
1937                         *p != ',' &&
1938                         *p != '(' && *p != ')')
1939                         return NULL;
1940         }
1941
1942         len = strlen(HINT_START);
1943         head = (char *) p;
1944         p += len;
1945         skip_space(p);
1946
1947         /* find hint end keyword. */
1948         if ((tail = strstr(p, HINT_END)) == NULL)
1949         {
1950                 hint_ereport(head, ("Unterminated block comment."));
1951                 return NULL;
1952         }
1953
1954         /* We don't support nested block comments. */
1955         if ((head = strstr(p, BLOCK_COMMENT_START)) != NULL && head < tail)
1956         {
1957                 hint_ereport(head, ("Nested block comments are not supported."));
1958                 return NULL;
1959         }
1960
1961         /* Make a copy of hint. */
1962         len = tail - p;
1963         head = palloc(len + 1);
1964         memcpy(head, p, len);
1965         head[len] = '\0';
1966         p = head;
1967
1968         return p;
1969 }
1970
1971 /*
1972  * Parse hints that got, create hint struct from parse tree and parse hints.
1973  */
1974 static HintState *
1975 create_hintstate(Query *parse, const char *hints)
1976 {
1977         const char *p;
1978         int                     i;
1979         HintState   *hstate;
1980
1981         if (hints == NULL)
1982                 return NULL;
1983
1984         /* -1 means that no Parallel hint is specified. */
1985         max_hint_nworkers = -1;
1986
1987         p = hints;
1988         hstate = HintStateCreate();
1989         hstate->hint_str = (char *) hints;
1990
1991         /* parse each hint. */
1992         parse_hints(hstate, parse, p);
1993
1994         /* When nothing specified a hint, we free HintState and returns NULL. */
1995         if (hstate->nall_hints == 0)
1996         {
1997                 HintStateDelete(hstate);
1998                 return NULL;
1999         }
2000
2001         /* Sort hints in order of original position. */
2002         qsort(hstate->all_hints, hstate->nall_hints, sizeof(Hint *),
2003                   HintCmpWithPos);
2004
2005         /* Count number of hints per hint-type. */
2006         for (i = 0; i < hstate->nall_hints; i++)
2007         {
2008                 Hint   *cur_hint = hstate->all_hints[i];
2009                 hstate->num_hints[cur_hint->type]++;
2010         }
2011
2012         /*
2013          * If an object (or a set of objects) has multiple hints of same hint-type,
2014          * only the last hint is valid and others are ignored in planning.
2015          * Hints except the last are marked as 'duplicated' to remember the order.
2016          */
2017         for (i = 0; i < hstate->nall_hints - 1; i++)
2018         {
2019                 Hint   *cur_hint = hstate->all_hints[i];
2020                 Hint   *next_hint = hstate->all_hints[i + 1];
2021
2022                 /*
2023                  * Leading hint is marked as 'duplicated' in transform_join_hints.
2024                  */
2025                 if (cur_hint->type == HINT_TYPE_LEADING &&
2026                         next_hint->type == HINT_TYPE_LEADING)
2027                         continue;
2028
2029                 /*
2030                  * Note that we need to pass addresses of hint pointers, because
2031                  * HintCmp is designed to sort array of Hint* by qsort.
2032                  */
2033                 if (HintCmp(&cur_hint, &next_hint) == 0)
2034                 {
2035                         hint_ereport(cur_hint->hint_str,
2036                                                  ("Conflict %s hint.", HintTypeName[cur_hint->type]));
2037                         cur_hint->state = HINT_STATE_DUPLICATION;
2038                 }
2039         }
2040
2041         /*
2042          * Make sure that per-type array pointers point proper position in the
2043          * array which consists of all hints.
2044          */
2045         hstate->scan_hints = (ScanMethodHint **) hstate->all_hints;
2046         hstate->join_hints = (JoinMethodHint **) (hstate->scan_hints +
2047                 hstate->num_hints[HINT_TYPE_SCAN_METHOD]);
2048         hstate->leading_hint = (LeadingHint **) (hstate->join_hints +
2049                 hstate->num_hints[HINT_TYPE_JOIN_METHOD]);
2050         hstate->set_hints = (SetHint **) (hstate->leading_hint +
2051                 hstate->num_hints[HINT_TYPE_LEADING]);
2052         hstate->rows_hints = (RowsHint **) (hstate->set_hints +
2053                 hstate->num_hints[HINT_TYPE_SET]);
2054         hstate->parallel_hints = (ParallelHint **) (hstate->set_hints +
2055                 hstate->num_hints[HINT_TYPE_ROWS]);
2056
2057         return hstate;
2058 }
2059
2060 /*
2061  * Parse inside of parentheses of scan-method hints.
2062  */
2063 static const char *
2064 ScanMethodHintParse(ScanMethodHint *hint, HintState *hstate, Query *parse,
2065                                         const char *str)
2066 {
2067         const char         *keyword = hint->base.keyword;
2068         HintKeyword             hint_keyword = hint->base.hint_keyword;
2069         List               *name_list = NIL;
2070         int                             length;
2071
2072         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2073                 return NULL;
2074
2075         /* Parse relation name and index name(s) if given hint accepts. */
2076         length = list_length(name_list);
2077
2078         /* at least twp parameters required */
2079         if (length < 1)
2080         {
2081                 hint_ereport(str,
2082                                          ("%s hint requires a relation.",  hint->base.keyword));
2083                 hint->base.state = HINT_STATE_ERROR;
2084                 return str;
2085         }
2086
2087         hint->relname = linitial(name_list);
2088         hint->indexnames = list_delete_first(name_list);
2089
2090         /* check whether the hint accepts index name(s) */
2091         if (length > 1 && !SCAN_HINT_ACCEPTS_INDEX_NAMES(hint_keyword))
2092         {
2093                 hint_ereport(str,
2094                                          ("%s hint accepts only one relation.",
2095                                           hint->base.keyword));
2096                 hint->base.state = HINT_STATE_ERROR;
2097                 return str;
2098         }
2099
2100         /* Set a bit for specified hint. */
2101         switch (hint_keyword)
2102         {
2103                 case HINT_KEYWORD_SEQSCAN:
2104                         hint->enforce_mask = ENABLE_SEQSCAN;
2105                         break;
2106                 case HINT_KEYWORD_INDEXSCAN:
2107                         hint->enforce_mask = ENABLE_INDEXSCAN;
2108                         break;
2109                 case HINT_KEYWORD_INDEXSCANREGEXP:
2110                         hint->enforce_mask = ENABLE_INDEXSCAN;
2111                         hint->regexp = true;
2112                         break;
2113                 case HINT_KEYWORD_BITMAPSCAN:
2114                         hint->enforce_mask = ENABLE_BITMAPSCAN;
2115                         break;
2116                 case HINT_KEYWORD_BITMAPSCANREGEXP:
2117                         hint->enforce_mask = ENABLE_BITMAPSCAN;
2118                         hint->regexp = true;
2119                         break;
2120                 case HINT_KEYWORD_TIDSCAN:
2121                         hint->enforce_mask = ENABLE_TIDSCAN;
2122                         break;
2123                 case HINT_KEYWORD_NOSEQSCAN:
2124                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_SEQSCAN;
2125                         break;
2126                 case HINT_KEYWORD_NOINDEXSCAN:
2127                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXSCAN;
2128                         break;
2129                 case HINT_KEYWORD_NOBITMAPSCAN:
2130                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_BITMAPSCAN;
2131                         break;
2132                 case HINT_KEYWORD_NOTIDSCAN:
2133                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_TIDSCAN;
2134                         break;
2135                 case HINT_KEYWORD_INDEXONLYSCAN:
2136                         hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2137                         break;
2138                 case HINT_KEYWORD_INDEXONLYSCANREGEXP:
2139                         hint->enforce_mask = ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN;
2140                         hint->regexp = true;
2141                         break;
2142                 case HINT_KEYWORD_NOINDEXONLYSCAN:
2143                         hint->enforce_mask = ENABLE_ALL_SCAN ^ ENABLE_INDEXONLYSCAN;
2144                         break;
2145                 default:
2146                         hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2147                         return NULL;
2148                         break;
2149         }
2150
2151         return str;
2152 }
2153
2154 static const char *
2155 JoinMethodHintParse(JoinMethodHint *hint, HintState *hstate, Query *parse,
2156                                         const char *str)
2157 {
2158         const char         *keyword = hint->base.keyword;
2159         HintKeyword             hint_keyword = hint->base.hint_keyword;
2160         List               *name_list = NIL;
2161
2162         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2163                 return NULL;
2164
2165         hint->nrels = list_length(name_list);
2166
2167         if (hint->nrels > 0)
2168         {
2169                 ListCell   *l;
2170                 int                     i = 0;
2171
2172                 /*
2173                  * Transform relation names from list to array to sort them with qsort
2174                  * after.
2175                  */
2176                 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2177                 foreach (l, name_list)
2178                 {
2179                         hint->relnames[i] = lfirst(l);
2180                         i++;
2181                 }
2182         }
2183
2184         list_free(name_list);
2185
2186         /* A join hint requires at least two relations */
2187         if (hint->nrels < 2)
2188         {
2189                 hint_ereport(str,
2190                                          ("%s hint requires at least two relations.",
2191                                           hint->base.keyword));
2192                 hint->base.state = HINT_STATE_ERROR;
2193                 return str;
2194         }
2195
2196         /* Sort hints in alphabetical order of relation names. */
2197         qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2198
2199         switch (hint_keyword)
2200         {
2201                 case HINT_KEYWORD_NESTLOOP:
2202                         hint->enforce_mask = ENABLE_NESTLOOP;
2203                         break;
2204                 case HINT_KEYWORD_MERGEJOIN:
2205                         hint->enforce_mask = ENABLE_MERGEJOIN;
2206                         break;
2207                 case HINT_KEYWORD_HASHJOIN:
2208                         hint->enforce_mask = ENABLE_HASHJOIN;
2209                         break;
2210                 case HINT_KEYWORD_NONESTLOOP:
2211                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_NESTLOOP;
2212                         break;
2213                 case HINT_KEYWORD_NOMERGEJOIN:
2214                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_MERGEJOIN;
2215                         break;
2216                 case HINT_KEYWORD_NOHASHJOIN:
2217                         hint->enforce_mask = ENABLE_ALL_JOIN ^ ENABLE_HASHJOIN;
2218                         break;
2219                 default:
2220                         hint_ereport(str, ("Unrecognized hint keyword \"%s\".", keyword));
2221                         return NULL;
2222                         break;
2223         }
2224
2225         return str;
2226 }
2227
2228 static bool
2229 OuterInnerPairCheck(OuterInnerRels *outer_inner)
2230 {
2231         ListCell *l;
2232         if (outer_inner->outer_inner_pair == NIL)
2233         {
2234                 if (outer_inner->relation)
2235                         return true;
2236                 else
2237                         return false;
2238         }
2239
2240         if (list_length(outer_inner->outer_inner_pair) == 2)
2241         {
2242                 foreach(l, outer_inner->outer_inner_pair)
2243                 {
2244                         if (!OuterInnerPairCheck(lfirst(l)))
2245                                 return false;
2246                 }
2247         }
2248         else
2249                 return false;
2250
2251         return true;
2252 }
2253
2254 static List *
2255 OuterInnerList(OuterInnerRels *outer_inner)
2256 {
2257         List               *outer_inner_list = NIL;
2258         ListCell           *l;
2259         OuterInnerRels *outer_inner_rels;
2260
2261         foreach(l, outer_inner->outer_inner_pair)
2262         {
2263                 outer_inner_rels = (OuterInnerRels *)(lfirst(l));
2264
2265                 if (outer_inner_rels->relation != NULL)
2266                         outer_inner_list = lappend(outer_inner_list,
2267                                                                            outer_inner_rels->relation);
2268                 else
2269                         outer_inner_list = list_concat(outer_inner_list,
2270                                                                                    OuterInnerList(outer_inner_rels));
2271         }
2272         return outer_inner_list;
2273 }
2274
2275 static const char *
2276 LeadingHintParse(LeadingHint *hint, HintState *hstate, Query *parse,
2277                                  const char *str)
2278 {
2279         List               *name_list = NIL;
2280         OuterInnerRels *outer_inner = NULL;
2281
2282         if ((str = parse_parentheses_Leading(str, &name_list, &outer_inner)) ==
2283                 NULL)
2284                 return NULL;
2285
2286         if (outer_inner != NULL)
2287                 name_list = OuterInnerList(outer_inner);
2288
2289         hint->relations = name_list;
2290         hint->outer_inner = outer_inner;
2291
2292         /* A Leading hint requires at least two relations */
2293         if ( hint->outer_inner == NULL && list_length(hint->relations) < 2)
2294         {
2295                 hint_ereport(hint->base.hint_str,
2296                                          ("%s hint requires at least two relations.",
2297                                           HINT_LEADING));
2298                 hint->base.state = HINT_STATE_ERROR;
2299         }
2300         else if (hint->outer_inner != NULL &&
2301                          !OuterInnerPairCheck(hint->outer_inner))
2302         {
2303                 hint_ereport(hint->base.hint_str,
2304                                          ("%s hint requires two sets of relations when parentheses nests.",
2305                                           HINT_LEADING));
2306                 hint->base.state = HINT_STATE_ERROR;
2307         }
2308
2309         return str;
2310 }
2311
2312 static const char *
2313 SetHintParse(SetHint *hint, HintState *hstate, Query *parse, const char *str)
2314 {
2315         List   *name_list = NIL;
2316
2317         if ((str = parse_parentheses(str, &name_list, hint->base.hint_keyword))
2318                 == NULL)
2319                 return NULL;
2320
2321         hint->words = name_list;
2322
2323         /* We need both name and value to set GUC parameter. */
2324         if (list_length(name_list) == 2)
2325         {
2326                 hint->name = linitial(name_list);
2327                 hint->value = lsecond(name_list);
2328         }
2329         else
2330         {
2331                 hint_ereport(hint->base.hint_str,
2332                                          ("%s hint requires name and value of GUC parameter.",
2333                                           HINT_SET));
2334                 hint->base.state = HINT_STATE_ERROR;
2335         }
2336
2337         return str;
2338 }
2339
2340 static const char *
2341 RowsHintParse(RowsHint *hint, HintState *hstate, Query *parse,
2342                           const char *str)
2343 {
2344         HintKeyword             hint_keyword = hint->base.hint_keyword;
2345         List               *name_list = NIL;
2346         char               *rows_str;
2347         char               *end_ptr;
2348
2349         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2350                 return NULL;
2351
2352         /* Last element must be rows specification */
2353         hint->nrels = list_length(name_list) - 1;
2354
2355         if (hint->nrels > 0)
2356         {
2357                 ListCell   *l;
2358                 int                     i = 0;
2359
2360                 /*
2361                  * Transform relation names from list to array to sort them with qsort
2362                  * after.
2363                  */
2364                 hint->relnames = palloc(sizeof(char *) * hint->nrels);
2365                 foreach (l, name_list)
2366                 {
2367                         if (hint->nrels <= i)
2368                                 break;
2369                         hint->relnames[i] = lfirst(l);
2370                         i++;
2371                 }
2372         }
2373
2374         /* Retieve rows estimation */
2375         rows_str = list_nth(name_list, hint->nrels);
2376         hint->rows_str = rows_str;              /* store as-is for error logging */
2377         if (rows_str[0] == '#')
2378         {
2379                 hint->value_type = RVT_ABSOLUTE;
2380                 rows_str++;
2381         }
2382         else if (rows_str[0] == '+')
2383         {
2384                 hint->value_type = RVT_ADD;
2385                 rows_str++;
2386         }
2387         else if (rows_str[0] == '-')
2388         {
2389                 hint->value_type = RVT_SUB;
2390                 rows_str++;
2391         }
2392         else if (rows_str[0] == '*')
2393         {
2394                 hint->value_type = RVT_MULTI;
2395                 rows_str++;
2396         }
2397         else
2398         {
2399                 hint_ereport(rows_str, ("Unrecognized rows value type notation."));
2400                 hint->base.state = HINT_STATE_ERROR;
2401                 return str;
2402         }
2403         hint->rows = strtod(rows_str, &end_ptr);
2404         if (*end_ptr)
2405         {
2406                 hint_ereport(rows_str,
2407                                          ("%s hint requires valid number as rows estimation.",
2408                                           hint->base.keyword));
2409                 hint->base.state = HINT_STATE_ERROR;
2410                 return str;
2411         }
2412
2413         /* A join hint requires at least two relations */
2414         if (hint->nrels < 2)
2415         {
2416                 hint_ereport(str,
2417                                          ("%s hint requires at least two relations.",
2418                                           hint->base.keyword));
2419                 hint->base.state = HINT_STATE_ERROR;
2420                 return str;
2421         }
2422
2423         list_free(name_list);
2424
2425         /* Sort relnames in alphabetical order. */
2426         qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
2427
2428         return str;
2429 }
2430
2431 static const char *
2432 ParallelHintParse(ParallelHint *hint, HintState *hstate, Query *parse,
2433                                   const char *str)
2434 {
2435         HintKeyword             hint_keyword = hint->base.hint_keyword;
2436         List               *name_list = NIL;
2437         int                             length;
2438         char   *end_ptr;
2439         int             nworkers;
2440         bool    force_parallel = false;
2441
2442         if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
2443                 return NULL;
2444
2445         /* Parse relation name and index name(s) if given hint accepts. */
2446         length = list_length(name_list);
2447
2448         if (length < 2 || length > 3)
2449         {
2450                 hint_ereport(")",
2451                                          ("wrong number of arguments (%d): %s",
2452                                           length,  hint->base.keyword));
2453                 hint->base.state = HINT_STATE_ERROR;
2454                 return str;
2455         }
2456
2457         hint->relname = linitial(name_list);
2458                 
2459         /* The second parameter is number of workers */
2460         hint->nworkers_str = list_nth(name_list, 1);
2461         nworkers = strtod(hint->nworkers_str, &end_ptr);
2462         if (*end_ptr || nworkers < 0 || nworkers > max_worker_processes)
2463         {
2464                 if (*end_ptr)
2465                         hint_ereport(hint->nworkers_str,
2466                                                  ("number of workers must be a number: %s",
2467                                                   hint->base.keyword));
2468                 else if (nworkers < 0)
2469                         hint_ereport(hint->nworkers_str,
2470                                                  ("number of workers must be positive: %s",
2471                                                   hint->base.keyword));
2472                 else if (nworkers > max_worker_processes)
2473                         hint_ereport(hint->nworkers_str,
2474                                                  ("number of workers = %d is larger than max_worker_processes(%d): %s",
2475                                                   nworkers, max_worker_processes, hint->base.keyword));
2476
2477                 hint->base.state = HINT_STATE_ERROR;
2478         }
2479
2480         hint->nworkers = nworkers;
2481
2482         /* optional third parameter is specified */
2483         if (length == 3)
2484         {
2485                 const char *modeparam = (const char *)list_nth(name_list, 2);
2486                 if (pg_strcasecmp(modeparam, "hard") == 0)
2487                         force_parallel = true;
2488                 else if (pg_strcasecmp(modeparam, "soft") != 0)
2489                 {
2490                         hint_ereport(modeparam,
2491                                                  ("enforcement must be soft or hard: %s",
2492                                                          hint->base.keyword));
2493                         hint->base.state = HINT_STATE_ERROR;
2494                 }
2495         }
2496
2497         hint->force_parallel = force_parallel;
2498
2499         if (hint->base.state != HINT_STATE_ERROR &&
2500                 nworkers > max_hint_nworkers)
2501                 max_hint_nworkers = nworkers;
2502
2503         return str;
2504 }
2505
2506 /*
2507  * set GUC parameter functions
2508  */
2509
2510 static int
2511 get_current_scan_mask()
2512 {
2513         int mask = 0;
2514
2515         if (enable_seqscan)
2516                 mask |= ENABLE_SEQSCAN;
2517         if (enable_indexscan)
2518                 mask |= ENABLE_INDEXSCAN;
2519         if (enable_bitmapscan)
2520                 mask |= ENABLE_BITMAPSCAN;
2521         if (enable_tidscan)
2522                 mask |= ENABLE_TIDSCAN;
2523         if (enable_indexonlyscan)
2524                 mask |= ENABLE_INDEXONLYSCAN;
2525
2526         return mask;
2527 }
2528
2529 static int
2530 get_current_join_mask()
2531 {
2532         int mask = 0;
2533
2534         if (enable_nestloop)
2535                 mask |= ENABLE_NESTLOOP;
2536         if (enable_mergejoin)
2537                 mask |= ENABLE_MERGEJOIN;
2538         if (enable_hashjoin)
2539                 mask |= ENABLE_HASHJOIN;
2540
2541         return mask;
2542 }
2543
2544 /*
2545  * Sets GUC prameters without throwing exception. Reutrns false if something
2546  * wrong.
2547  */
2548 static int
2549 set_config_option_noerror(const char *name, const char *value,
2550                                                   GucContext context, GucSource source,
2551                                                   GucAction action, bool changeVal, int elevel)
2552 {
2553         int                             result = 0;
2554         MemoryContext   ccxt = CurrentMemoryContext;
2555
2556         PG_TRY();
2557         {
2558                 result = set_config_option(name, value, context, source,
2559                                                                    action, changeVal, 0, false);
2560         }
2561         PG_CATCH();
2562         {
2563                 ErrorData          *errdata;
2564
2565                 /* Save error info */
2566                 MemoryContextSwitchTo(ccxt);
2567                 errdata = CopyErrorData();
2568                 FlushErrorState();
2569
2570                 ereport(elevel,
2571                                 (errcode(errdata->sqlerrcode),
2572                                  errmsg("%s", errdata->message),
2573                                  errdata->detail ? errdetail("%s", errdata->detail) : 0,
2574                                  errdata->hint ? errhint("%s", errdata->hint) : 0));
2575                 msgqno = qno;
2576                 FreeErrorData(errdata);
2577         }
2578         PG_END_TRY();
2579
2580         return result;
2581 }
2582
2583 /*
2584  * Sets GUC parameter of int32 type without throwing exceptions. Returns false
2585  * if something wrong.
2586  */
2587 static int
2588 set_config_int32_option(const char *name, int32 value, GucContext context)
2589 {
2590         char buf[16];   /* enough for int32 */
2591
2592         if (snprintf(buf, 16, "%d", value) < 0)
2593         {
2594                 ereport(pg_hint_plan_message_level,
2595                                 (errmsg ("Cannot set integer value: %d: %s",
2596                                                  max_hint_nworkers, strerror(errno))));
2597                 return false;
2598         }
2599
2600         return
2601                 set_config_option_noerror(name, buf, context,
2602                                                                   PGC_S_SESSION, GUC_ACTION_SAVE, true,
2603                                                                   pg_hint_plan_message_level);
2604 }
2605
2606 /* setup scan method enforcement according to given options */
2607 static void
2608 setup_guc_enforcement(SetHint **options, int noptions, GucContext context)
2609 {
2610         int     i;
2611
2612         for (i = 0; i < noptions; i++)
2613         {
2614                 SetHint    *hint = options[i];
2615                 int                     result;
2616
2617                 if (!hint_state_enabled(hint))
2618                         continue;
2619
2620                 result = set_config_option_noerror(hint->name, hint->value, context,
2621                                                                                    PGC_S_SESSION, GUC_ACTION_SAVE, true,
2622                                                                                    pg_hint_plan_message_level);
2623                 if (result != 0)
2624                         hint->base.state = HINT_STATE_USED;
2625                 else
2626                         hint->base.state = HINT_STATE_ERROR;
2627         }
2628
2629         return;
2630 }
2631
2632 /*
2633  * Setup parallel execution environment.
2634  *
2635  * If hint is not NULL, set up using it, elsewise reset to initial environment.
2636  */
2637 static void
2638 setup_parallel_plan_enforcement(ParallelHint *hint, HintState *state)
2639 {
2640         if (hint)
2641         {
2642                 hint->base.state = HINT_STATE_USED;
2643                 set_config_int32_option("max_parallel_workers_per_gather",
2644                                                                 hint->nworkers, state->context);
2645         }
2646         else
2647                 set_config_int32_option("max_parallel_workers_per_gather",
2648                                                                 state->init_nworkers, state->context);
2649
2650         /* force means that enforce parallel as far as possible */
2651         if (hint && hint->force_parallel)
2652         {
2653                 set_config_int32_option("parallel_tuple_cost", 0, state->context);
2654                 set_config_int32_option("parallel_setup_cost", 0, state->context);
2655                 set_config_int32_option("min_parallel_table_scan_size", 0,
2656                                                                 state->context);
2657                 set_config_int32_option("min_parallel_index_scan_size", 0,
2658                                                                 state->context);
2659         }
2660         else
2661         {
2662                 set_config_int32_option("parallel_tuple_cost",
2663                                                                 state->init_paratup_cost, state->context);
2664                 set_config_int32_option("parallel_setup_cost",
2665                                                                 state->init_parasetup_cost, state->context);
2666                 set_config_int32_option("min_parallel_table_scan_size",
2667                                                                 state->init_min_para_tablescan_size,
2668                                                                 state->context);
2669                 set_config_int32_option("min_parallel_index_scan_size",
2670                                                                 state->init_min_para_indexscan_size,
2671                                                                 state->context);
2672         }
2673 }
2674
2675 #define SET_CONFIG_OPTION(name, type_bits) \
2676         set_config_option_noerror((name), \
2677                 (mask & (type_bits)) ? "true" : "false", \
2678                 context, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR)
2679
2680
2681 /*
2682  * Setup GUC environment to enforce scan methods. If scanhint is NULL, reset
2683  * GUCs to the saved state in state.
2684  */
2685 static void
2686 setup_scan_method_enforcement(ScanMethodHint *scanhint, HintState *state)
2687 {
2688         unsigned char   enforce_mask = state->init_scan_mask;
2689         GucContext              context = state->context;
2690         unsigned char   mask;
2691
2692         if (scanhint)
2693         {
2694                 enforce_mask = scanhint->enforce_mask;
2695                 scanhint->base.state = HINT_STATE_USED;
2696         }
2697
2698         if (enforce_mask == ENABLE_SEQSCAN || enforce_mask == ENABLE_INDEXSCAN ||
2699                 enforce_mask == ENABLE_BITMAPSCAN || enforce_mask == ENABLE_TIDSCAN
2700                 || enforce_mask == (ENABLE_INDEXSCAN | ENABLE_INDEXONLYSCAN)
2701                 )
2702                 mask = enforce_mask;
2703         else
2704                 mask = enforce_mask & current_hint_state->init_scan_mask;
2705
2706         SET_CONFIG_OPTION("enable_seqscan", ENABLE_SEQSCAN);
2707         SET_CONFIG_OPTION("enable_indexscan", ENABLE_INDEXSCAN);
2708         SET_CONFIG_OPTION("enable_bitmapscan", ENABLE_BITMAPSCAN);
2709         SET_CONFIG_OPTION("enable_tidscan", ENABLE_TIDSCAN);
2710         SET_CONFIG_OPTION("enable_indexonlyscan", ENABLE_INDEXONLYSCAN);
2711 }
2712
2713 static void
2714 set_join_config_options(unsigned char enforce_mask, GucContext context)
2715 {
2716         unsigned char   mask;
2717
2718         if (enforce_mask == ENABLE_NESTLOOP || enforce_mask == ENABLE_MERGEJOIN ||
2719                 enforce_mask == ENABLE_HASHJOIN)
2720                 mask = enforce_mask;
2721         else
2722                 mask = enforce_mask & current_hint_state->init_join_mask;
2723
2724         SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
2725         SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
2726         SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
2727 }
2728
2729 /*
2730  * Push a hint into hint stack which is implemented with List struct.  Head of
2731  * list is top of stack.
2732  */
2733 static void
2734 push_hint(HintState *hstate)
2735 {
2736         /* Prepend new hint to the list means pushing to stack. */
2737         HintStateStack = lcons(hstate, HintStateStack);
2738
2739         /* Pushed hint is the one which should be used hereafter. */
2740         current_hint_state = hstate;
2741 }
2742
2743 /* Pop a hint from hint stack.  Popped hint is automatically discarded. */
2744 static void
2745 pop_hint(void)
2746 {
2747         /* Hint stack must not be empty. */
2748         if(HintStateStack == NIL)
2749                 elog(ERROR, "hint stack is empty");
2750
2751         /*
2752          * Take a hint at the head from the list, and free it.  Switch
2753          * current_hint_state to point new head (NULL if the list is empty).
2754          */
2755         HintStateStack = list_delete_first(HintStateStack);
2756         HintStateDelete(current_hint_state);
2757         if(HintStateStack == NIL)
2758                 current_hint_state = NULL;
2759         else
2760                 current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
2761 }
2762
2763 /*
2764  * Retrieve and store a hint string from given query or from the hint table.
2765  * If we are using the hint table, the query string is needed to be normalized.
2766  * However, ParseState, which is not available in planner_hook, is required to
2767  * check if the query tree (Query) is surely corresponding to the target query.
2768  */
2769 static void
2770 pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
2771 {
2772         const char *query_str;
2773         MemoryContext   oldcontext;
2774
2775         if (prev_post_parse_analyze_hook)
2776                 prev_post_parse_analyze_hook(pstate, query);
2777
2778         /* do nothing under hint table search */
2779         if (hint_inhibit_level > 0)
2780                 return;
2781
2782         if (!pg_hint_plan_enable_hint)
2783         {
2784                 if (current_hint_str)
2785                 {
2786                         pfree((void *)current_hint_str);
2787                         current_hint_str = NULL;
2788                 }
2789                 return;
2790         }
2791
2792         /* increment the query number */
2793         qnostr[0] = 0;
2794         if (debug_level > 1)
2795                 snprintf(qnostr, sizeof(qnostr), "[qno=0x%x]", qno++);
2796         qno++;
2797
2798         /* search the hint table for a hint if requested */
2799         if (pg_hint_plan_enable_hint_table)
2800         {
2801                 int                             query_len;
2802                 pgssJumbleState jstate;
2803                 Query              *jumblequery;
2804                 char               *normalized_query = NULL;
2805
2806                 query_str = get_query_string(pstate, query, &jumblequery);
2807
2808                 /* If this query is not for hint, just return */
2809                 if (!query_str)
2810                         return;
2811
2812                 /* clear the previous hint string */
2813                 if (current_hint_str)
2814                 {
2815                         pfree((void *)current_hint_str);
2816                         current_hint_str = NULL;
2817                 }
2818                 
2819                 if (jumblequery)
2820                 {
2821                         /*
2822                          * XXX: normalizing code is copied from pg_stat_statements.c, so be
2823                          * careful to PostgreSQL's version up.
2824                          */
2825                         jstate.jumble = (unsigned char *) palloc(JUMBLE_SIZE);
2826                         jstate.jumble_len = 0;
2827                         jstate.clocations_buf_size = 32;
2828                         jstate.clocations = (pgssLocationLen *)
2829                                 palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
2830                         jstate.clocations_count = 0;
2831
2832                         JumbleQuery(&jstate, jumblequery);
2833
2834                         /*
2835                          * Normalize the query string by replacing constants with '?'
2836                          */
2837                         /*
2838                          * Search hint string which is stored keyed by query string
2839                          * and application name.  The query string is normalized to allow
2840                          * fuzzy matching.
2841                          *
2842                          * Adding 1 byte to query_len ensures that the returned string has
2843                          * a terminating NULL.
2844                          */
2845                         query_len = strlen(query_str) + 1;
2846                         normalized_query =
2847                                 generate_normalized_query(&jstate, query_str,
2848                                                                                   &query_len,
2849                                                                                   GetDatabaseEncoding());
2850
2851                         /*
2852                          * find a hint for the normalized query. the result should be in
2853                          * TopMemoryContext
2854                          */
2855                         oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2856                         current_hint_str =
2857                                 get_hints_from_table(normalized_query, application_name);
2858                         MemoryContextSwitchTo(oldcontext);
2859
2860                         if (debug_level > 1)
2861                         {
2862                                 if (current_hint_str)
2863                                         ereport(pg_hint_plan_message_level,
2864                                                         (errmsg("pg_hint_plan[qno=0x%x]: "
2865                                                                         "post_parse_analyze_hook: "
2866                                                                         "hints from table: \"%s\": "
2867                                                                         "normalized_query=\"%s\", "
2868                                                                         "application name =\"%s\"",
2869                                                                         qno, current_hint_str,
2870                                                                         normalized_query, application_name),
2871                                                          errhidestmt(msgqno != qno),
2872                                                          errhidecontext(msgqno != qno)));
2873                                 else
2874                                         ereport(pg_hint_plan_message_level,
2875                                                         (errmsg("pg_hint_plan[qno=0x%x]: "
2876                                                                         "no match found in table:  "
2877                                                                         "application name = \"%s\", "
2878                                                                         "normalized_query=\"%s\"",
2879                                                                         qno, application_name,
2880                                                                         normalized_query),
2881                                                          errhidestmt(msgqno != qno),
2882                                                          errhidecontext(msgqno != qno)));
2883
2884                                 msgqno = qno;
2885                         }
2886                 }
2887
2888                 /* retrun if we have hint here */
2889                 if (current_hint_str)
2890                         return;
2891         }
2892         else
2893                 query_str = get_query_string(pstate, query, NULL);
2894
2895         if (query_str)
2896         {
2897                 /*
2898                  * get hints from the comment. However we may have the same query
2899                  * string with the previous call, but just retrieving hints is expected
2900                  * to be faster than checking for identicalness before retrieval.
2901                  */
2902                 if (current_hint_str)
2903                         pfree((void *)current_hint_str);
2904
2905                 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2906                 current_hint_str = get_hints_from_comment(query_str);
2907                 MemoryContextSwitchTo(oldcontext);
2908         }
2909
2910         if (debug_level > 1)
2911         {
2912                 if (debug_level == 1 &&
2913                         (stmt_name || strcmp(query_str, debug_query_string)))
2914                         ereport(pg_hint_plan_message_level,
2915                                         (errmsg("hints in comment=\"%s\"",
2916                                                         current_hint_str ? current_hint_str : "(none)"),
2917                                          errhidestmt(msgqno != qno),
2918                                          errhidecontext(msgqno != qno)));
2919                 else
2920                         ereport(pg_hint_plan_message_level,
2921                                         (errmsg("hints in comment=\"%s\", stmt=\"%s\", query=\"%s\", debug_query_string=\"%s\"",
2922                                                         current_hint_str ? current_hint_str : "(none)",
2923                                                         stmt_name, query_str, debug_query_string),
2924                                          errhidestmt(msgqno != qno),
2925                                          errhidecontext(msgqno != qno)));
2926                 msgqno = qno;
2927         }
2928 }
2929
2930 /*
2931  * Read and set up hint information
2932  */
2933 static PlannedStmt *
2934 pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
2935 {
2936         int                             save_nestlevel;
2937         PlannedStmt        *result;
2938         HintState          *hstate;
2939
2940         /*
2941          * Use standard planner if pg_hint_plan is disabled or current nesting 
2942          * depth is nesting depth of SPI calls. Other hook functions try to change
2943          * plan with current_hint_state if any, so set it to NULL.
2944          */
2945         if (!pg_hint_plan_enable_hint || hint_inhibit_level > 0)
2946         {
2947                 if (debug_level > 1)
2948                         ereport(pg_hint_plan_message_level,
2949                                         (errmsg ("pg_hint_plan%s: planner: enable_hint=%d,"
2950                                                          " hint_inhibit_level=%d",
2951                                                          qnostr, pg_hint_plan_enable_hint,
2952                                                          hint_inhibit_level),
2953                                          errhidestmt(msgqno != qno)));
2954                 msgqno = qno;
2955
2956                 goto standard_planner_proc;
2957         }
2958
2959         /*
2960          * Support for nested plpgsql functions. This is quite ugly but this is the
2961          * only point I could find where I can get the query string.
2962          */
2963         if (plpgsql_recurse_level > 0)
2964         {
2965                 MemoryContext oldcontext;
2966
2967                 if (current_hint_str)
2968                         pfree((void *)current_hint_str);
2969
2970                 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2971                 current_hint_str =
2972                         get_hints_from_comment((char *)error_context_stack->arg);
2973                 MemoryContextSwitchTo(oldcontext);
2974         }
2975
2976         if (!current_hint_str)
2977                 goto standard_planner_proc;
2978
2979         /* parse the hint into hint state struct */
2980         hstate = create_hintstate(parse, pstrdup(current_hint_str));
2981
2982         /* run standard planner if the statement has not valid hint */
2983         if (!hstate)
2984                 goto standard_planner_proc;
2985         
2986         /*
2987          * Push new hint struct to the hint stack to disable previous hint context.
2988          */
2989         push_hint(hstate);
2990
2991         /*  Set scan enforcement here. */
2992         save_nestlevel = NewGUCNestLevel();
2993
2994         /* Apply Set hints, then save it as the initial state  */
2995         setup_guc_enforcement(current_hint_state->set_hints,
2996                                                    current_hint_state->num_hints[HINT_TYPE_SET],
2997                                                    current_hint_state->context);
2998         
2999         current_hint_state->init_scan_mask = get_current_scan_mask();
3000         current_hint_state->init_join_mask = get_current_join_mask();
3001         current_hint_state->init_min_para_tablescan_size =
3002                 min_parallel_table_scan_size;
3003         current_hint_state->init_min_para_indexscan_size =
3004                 min_parallel_index_scan_size;
3005         current_hint_state->init_paratup_cost = parallel_tuple_cost;
3006         current_hint_state->init_parasetup_cost = parallel_setup_cost;
3007
3008         /*
3009          * max_parallel_workers_per_gather should be non-zero here if Workers hint
3010          * is specified.
3011          */
3012         if (max_hint_nworkers > 0 && max_parallel_workers_per_gather < 1)
3013                 set_config_int32_option("max_parallel_workers_per_gather",
3014                                                                 1, current_hint_state->context);
3015         current_hint_state->init_nworkers = max_parallel_workers_per_gather;
3016
3017         if (debug_level > 1)
3018         {
3019                 ereport(pg_hint_plan_message_level,
3020                                 (errhidestmt(msgqno != qno),
3021                                  errmsg("pg_hint_plan%s: planner", qnostr))); 
3022                 msgqno = qno;
3023         }
3024
3025         /*
3026          * Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
3027          * the state when this planner started when error occurred in planner.
3028          */
3029         PG_TRY();
3030         {
3031                 if (prev_planner)
3032                         result = (*prev_planner) (parse, cursorOptions, boundParams);
3033                 else
3034                         result = standard_planner(parse, cursorOptions, boundParams);
3035         }
3036         PG_CATCH();
3037         {
3038                 /*
3039                  * Rollback changes of GUC parameters, and pop current hint context
3040                  * from hint stack to rewind the state.
3041                  */
3042                 AtEOXact_GUC(true, save_nestlevel);
3043                 pop_hint();
3044                 PG_RE_THROW();
3045         }
3046         PG_END_TRY();
3047
3048         /* Print hint in debug mode. */
3049         if (debug_level == 1)
3050                 HintStateDump(current_hint_state);
3051         else if (debug_level > 1)
3052                 HintStateDump2(current_hint_state);
3053
3054         /*
3055          * Rollback changes of GUC parameters, and pop current hint context from
3056          * hint stack to rewind the state.
3057          */
3058         AtEOXact_GUC(true, save_nestlevel);
3059         pop_hint();
3060
3061         return result;
3062
3063 standard_planner_proc:
3064         if (debug_level > 1)
3065         {
3066                 ereport(pg_hint_plan_message_level,
3067                                 (errhidestmt(msgqno != qno),
3068                                  errmsg("pg_hint_plan%s: planner: no valid hint",
3069                                                 qnostr)));
3070                 msgqno = qno;
3071         }
3072         current_hint_state = NULL;
3073         if (prev_planner)
3074                 return (*prev_planner) (parse, cursorOptions, boundParams);
3075         else
3076                 return standard_planner(parse, cursorOptions, boundParams);
3077 }
3078
3079 /*
3080  * Find scan method hint to be applied to the given relation
3081  *
3082  */
3083 static ScanMethodHint *
3084 find_scan_hint(PlannerInfo *root, Index relid)
3085 {
3086         RelOptInfo         *rel;
3087         RangeTblEntry  *rte;
3088         ScanMethodHint  *real_name_hint = NULL;
3089         ScanMethodHint  *alias_hint = NULL;
3090         int                             i;
3091
3092         /* This should not be a join rel */
3093         Assert(relid > 0);
3094         rel = root->simple_rel_array[relid];
3095
3096         /*
3097          * This function is called for any RelOptInfo or its inheritance parent if
3098          * any. If we are called from inheritance planner, the RelOptInfo for the
3099          * parent of target child relation is not set in the planner info.
3100          *
3101          * Otherwise we should check that the reloptinfo is base relation or
3102          * inheritance children.
3103          */
3104         if (rel &&
3105                 rel->reloptkind != RELOPT_BASEREL &&
3106                 rel->reloptkind != RELOPT_OTHER_MEMBER_REL)
3107                 return NULL;
3108
3109         /*
3110          * This is baserel or appendrel children. We can refer to RangeTblEntry.
3111          */
3112         rte = root->simple_rte_array[relid];
3113         Assert(rte);
3114
3115         /* We don't hint on other than relation and foreign tables */
3116         if (rte->rtekind != RTE_RELATION ||
3117                 rte->relkind == RELKIND_FOREIGN_TABLE)
3118                 return NULL;
3119
3120         /* Find scan method hint, which matches given names, from the list. */
3121         for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_SCAN_METHOD]; i++)
3122         {
3123                 ScanMethodHint *hint = current_hint_state->scan_hints[i];
3124
3125                 /* We ignore disabled hints. */
3126                 if (!hint_state_enabled(hint))
3127                         continue;
3128
3129                 if (!alias_hint &&
3130                         RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3131                         alias_hint = hint;
3132
3133                 /* check the real name for appendrel children */
3134                 if (!real_name_hint &&
3135                         rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3136                 {
3137                         char *realname = get_rel_name(rte->relid);
3138
3139                         if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3140                                 real_name_hint = hint;
3141                 }
3142
3143                 /* No more match expected, break  */
3144                 if(alias_hint && real_name_hint)
3145                         break;
3146         }
3147
3148         /* real name match precedes alias match */
3149         if (real_name_hint)
3150                 return real_name_hint;
3151
3152         return alias_hint;
3153 }
3154
3155 static ParallelHint *
3156 find_parallel_hint(PlannerInfo *root, Index relid)
3157 {
3158         RelOptInfo         *rel;
3159         RangeTblEntry  *rte;
3160         ParallelHint    *real_name_hint = NULL;
3161         ParallelHint    *alias_hint = NULL;
3162         int                             i;
3163
3164         /* This should not be a join rel */
3165         Assert(relid > 0);
3166         rel = root->simple_rel_array[relid];
3167
3168         /*
3169          * Parallel planning is appliable only on base relation, which has
3170          * RelOptInfo. 
3171          */
3172         if (!rel)
3173                 return NULL;
3174
3175         /*
3176          * We have set root->glob->parallelModeOK if needed. What we should do here
3177          * is just following the decision of planner.
3178          */
3179         if (!rel->consider_parallel)
3180                 return NULL;
3181
3182         /*
3183          * This is baserel or appendrel children. We can refer to RangeTblEntry.
3184          */
3185         rte = root->simple_rte_array[relid];
3186         Assert(rte);
3187
3188         /* Find parallel method hint, which matches given names, from the list. */
3189         for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_PARALLEL]; i++)
3190         {
3191                 ParallelHint *hint = current_hint_state->parallel_hints[i];
3192
3193                 /* We ignore disabled hints. */
3194                 if (!hint_state_enabled(hint))
3195                         continue;
3196
3197                 if (!alias_hint &&
3198                         RelnameCmp(&rte->eref->aliasname, &hint->relname) == 0)
3199                         alias_hint = hint;
3200
3201                 /* check the real name for appendrel children */
3202                 if (!real_name_hint &&
3203                         rel && rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
3204                 {
3205                         char *realname = get_rel_name(rte->relid);
3206
3207                         if (realname && RelnameCmp(&realname, &hint->relname) == 0)
3208                                 real_name_hint = hint;
3209                 }
3210
3211                 /* No more match expected, break  */
3212                 if(alias_hint && real_name_hint)
3213                         break;
3214         }
3215
3216         /* real name match precedes alias match */
3217         if (real_name_hint)
3218                 return real_name_hint;
3219
3220         return alias_hint;
3221 }
3222
3223 /*
3224  * regexeq
3225  *
3226  * Returns TRUE on match, FALSE on no match.
3227  *
3228  *   s1 --- the data to match against
3229  *   s2 --- the pattern
3230  *
3231  * Because we copy s1 to NameData, make the size of s1 less than NAMEDATALEN.
3232  */
3233 static bool
3234 regexpeq(const char *s1, const char *s2)
3235 {
3236         NameData        name;
3237         text       *regexp;
3238         Datum           result;
3239
3240         strcpy(name.data, s1);
3241         regexp = cstring_to_text(s2);
3242
3243         result = DirectFunctionCall2Coll(nameregexeq,
3244                                                                          DEFAULT_COLLATION_OID,
3245                                                                          NameGetDatum(&name),
3246                                                                          PointerGetDatum(regexp));
3247         return DatumGetBool(result);
3248 }
3249
3250
3251 /* Remove indexes instructed not to use by hint. */
3252 static void
3253 restrict_indexes(PlannerInfo *root, ScanMethodHint *hint, RelOptInfo *rel,
3254                            bool using_parent_hint)
3255 {
3256         ListCell           *cell;
3257         ListCell           *prev;
3258         ListCell           *next;
3259         StringInfoData  buf;
3260         RangeTblEntry  *rte = root->simple_rte_array[rel->relid];
3261         Oid                             relationObjectId = rte->relid;
3262
3263         /*
3264          * We delete all the IndexOptInfo list and prevent you from being usable by
3265          * a scan.
3266          */
3267         if (hint->enforce_mask == ENABLE_SEQSCAN ||
3268                 hint->enforce_mask == ENABLE_TIDSCAN)
3269         {
3270                 list_free_deep(rel->indexlist);
3271                 rel->indexlist = NIL;
3272                 hint->base.state = HINT_STATE_USED;
3273
3274                 return;
3275         }
3276
3277         /*
3278          * When a list of indexes is not specified, we just use all indexes.
3279          */
3280         if (hint->indexnames == NIL)
3281                 return;
3282
3283         /*
3284          * Leaving only an specified index, we delete it from a IndexOptInfo list
3285          * other than it.
3286          */
3287         prev = NULL;
3288         if (debug_level > 0)
3289                 initStringInfo(&buf);
3290
3291         for (cell = list_head(rel->indexlist); cell; cell = next)
3292         {
3293                 IndexOptInfo   *info = (IndexOptInfo *) lfirst(cell);
3294                 char               *indexname = get_rel_name(info->indexoid);
3295                 ListCell           *l;
3296                 bool                    use_index = false;
3297
3298                 next = lnext(cell);
3299
3300                 foreach(l, hint->indexnames)
3301                 {
3302                         char   *hintname = (char *) lfirst(l);
3303                         bool    result;
3304
3305                         if (hint->regexp)
3306                                 result = regexpeq(indexname, hintname);
3307                         else
3308                                 result = RelnameCmp(&indexname, &hintname) == 0;
3309
3310                         if (result)
3311                         {
3312                                 use_index = true;
3313                                 if (debug_level > 0)
3314                                 {
3315                                         appendStringInfoCharMacro(&buf, ' ');
3316                                         quote_value(&buf, indexname);
3317                                 }
3318
3319                                 break;
3320                         }
3321                 }
3322
3323                 /*
3324                  * Apply index restriction of parent hint to children. Since index
3325                  * inheritance is not explicitly described we should search for an
3326                  * children's index with the same definition to that of the parent.
3327                  */
3328                 if (using_parent_hint && !use_index)
3329                 {
3330                         foreach(l, current_hint_state->parent_index_infos)
3331                         {
3332                                 int                                     i;
3333                                 HeapTuple                       ht_idx;
3334                                 ParentIndexInfo    *p_info = (ParentIndexInfo *)lfirst(l);
3335
3336                                 /*
3337                                  * we check the 'same' index by comparing uniqueness, access
3338                                  * method and index key columns.
3339                                  */
3340                                 if (p_info->indisunique != info->unique ||
3341                                         p_info->method != info->relam ||
3342                                         list_length(p_info->column_names) != info->ncolumns)
3343                                         continue;
3344
3345                                 /* Check if index key columns match */
3346                                 for (i = 0; i < info->ncolumns; i++)
3347                                 {
3348                                         char       *c_attname = NULL;
3349                                         char       *p_attname = NULL;
3350
3351                                         p_attname = list_nth(p_info->column_names, i);
3352
3353                                         /*
3354                                          * if both of the key of the same position are expressions,
3355                                          * ignore them for now and check later.
3356                                          */
3357                                         if (info->indexkeys[i] == 0 && !p_attname)
3358                                                 continue;
3359
3360                                         /* deny if one is expression while another is not */
3361                                         if (info->indexkeys[i] == 0 || !p_attname)
3362                                                 break;
3363
3364                                         c_attname = get_attname(relationObjectId,
3365                                                                                                 info->indexkeys[i]);
3366
3367                                         /* deny if any of column attributes don't match */
3368                                         if (strcmp(p_attname, c_attname) != 0 ||
3369                                                 p_info->indcollation[i] != info->indexcollations[i] ||
3370                                                 p_info->opclass[i] != info->opcintype[i]||
3371                                                 ((p_info->indoption[i] & INDOPTION_DESC) != 0)
3372                                                 != info->reverse_sort[i] ||
3373                                                 ((p_info->indoption[i] & INDOPTION_NULLS_FIRST) != 0)
3374                                                 != info->nulls_first[i])
3375                                                 break;
3376                                 }
3377
3378                                 /* deny this if any difference found */
3379                                 if (i != info->ncolumns)
3380                                         continue;
3381
3382                                 /* check on key expressions  */
3383                                 if ((p_info->expression_str && (info->indexprs != NIL)) ||
3384                                         (p_info->indpred_str && (info->indpred != NIL)))
3385                                 {
3386                                         /* fetch the index of this child */
3387                                         ht_idx = SearchSysCache1(INDEXRELID,
3388                                                                                          ObjectIdGetDatum(info->indexoid));
3389
3390                                         /* check expressions if both expressions are available */
3391                                         if (p_info->expression_str &&
3392                                                 !heap_attisnull(ht_idx, Anum_pg_index_indexprs))
3393                                         {
3394                                                 Datum       exprsDatum;
3395                                                 bool        isnull;
3396                                                 Datum       result;
3397
3398                                                 /*
3399                                                  * to change the expression's parameter of child's
3400                                                  * index to strings
3401                                                  */
3402                                                 exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3403                                                                                                          Anum_pg_index_indexprs,
3404                                                                                                          &isnull);
3405
3406                                                 result = DirectFunctionCall2(pg_get_expr,
3407                                                                                                          exprsDatum,
3408                                                                                                          ObjectIdGetDatum(
3409                                                                                                                  relationObjectId));
3410
3411                                                 /* deny if expressions don't match */
3412                                                 if (strcmp(p_info->expression_str,
3413                                                                    text_to_cstring(DatumGetTextP(result))) != 0)
3414                                                 {
3415                                                         /* Clean up */
3416                                                         ReleaseSysCache(ht_idx);
3417                                                         continue;
3418                                                 }
3419                                         }
3420
3421                                         /* compare index predicates  */
3422                                         if (p_info->indpred_str &&
3423                                                 !heap_attisnull(ht_idx, Anum_pg_index_indpred))
3424                                         {
3425                                                 Datum       predDatum;
3426                                                 bool        isnull;
3427                                                 Datum       result;
3428
3429                                                 predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
3430                                                                                                          Anum_pg_index_indpred,
3431                                                                                                          &isnull);
3432
3433                                                 result = DirectFunctionCall2(pg_get_expr,
3434                                                                                                          predDatum,
3435                                                                                                          ObjectIdGetDatum(
3436                                                                                                                  relationObjectId));
3437
3438                                                 if (strcmp(p_info->indpred_str,
3439                                                                    text_to_cstring(DatumGetTextP(result))) != 0)
3440                                                 {
3441                                                         /* Clean up */
3442                                                         ReleaseSysCache(ht_idx);
3443                                                         continue;
3444                                                 }
3445                                         }
3446
3447                                         /* Clean up */
3448                                         ReleaseSysCache(ht_idx);
3449                                 }
3450                                 else if (p_info->expression_str || (info->indexprs != NIL))
3451                                         continue;
3452                                 else if (p_info->indpred_str || (info->indpred != NIL))
3453                                         continue;
3454
3455                                 use_index = true;
3456
3457                                 /* to log the candidate of index */
3458                                 if (debug_level > 0)
3459                                 {
3460                                         appendStringInfoCharMacro(&buf, ' ');
3461                                         quote_value(&buf, indexname);
3462                                 }
3463
3464                                 break;
3465                         }
3466                 }
3467
3468                 if (!use_index)
3469                         rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
3470                 else
3471                         prev = cell;
3472
3473                 pfree(indexname);
3474         }
3475
3476         if (debug_level == 1)
3477         {
3478                 StringInfoData  rel_buf;
3479                 char *disprelname = "";
3480
3481                 /*
3482                  * If this hint targetted the parent, use the real name of this
3483                  * child. Otherwise use hint specification.
3484                  */
3485                 if (using_parent_hint)
3486                         disprelname = get_rel_name(rte->relid);
3487                 else
3488                         disprelname = hint->relname;
3489                         
3490
3491                 initStringInfo(&rel_buf);
3492                 quote_value(&rel_buf, disprelname);
3493
3494                 ereport(LOG,
3495                                 (errmsg("available indexes for %s(%s):%s",
3496                                          hint->base.keyword,
3497                                          rel_buf.data,
3498                                          buf.data)));
3499                 pfree(buf.data);
3500                 pfree(rel_buf.data);
3501         }
3502 }
3503
3504 /* 
3505  * Return information of index definition.
3506  */
3507 static ParentIndexInfo *
3508 get_parent_index_info(Oid indexoid, Oid relid)
3509 {
3510         ParentIndexInfo *p_info = palloc(sizeof(ParentIndexInfo));
3511         Relation            indexRelation;
3512         Form_pg_index   index;
3513         char               *attname;
3514         int                             i;
3515
3516         indexRelation = index_open(indexoid, RowExclusiveLock);
3517
3518         index = indexRelation->rd_index;
3519
3520         p_info->indisunique = index->indisunique;
3521         p_info->method = indexRelation->rd_rel->relam;
3522
3523         p_info->column_names = NIL;
3524         p_info->indcollation = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3525         p_info->opclass = (Oid *) palloc(sizeof(Oid) * index->indnatts);
3526         p_info->indoption = (int16 *) palloc(sizeof(Oid) * index->indnatts);
3527
3528         for (i = 0; i < index->indnatts; i++)
3529         {
3530                 attname = get_attname(relid, index->indkey.values[i]);
3531                 p_info->column_names = lappend(p_info->column_names, attname);
3532
3533                 p_info->indcollation[i] = indexRelation->rd_indcollation[i];
3534                 p_info->opclass[i] = indexRelation->rd_opcintype[i];
3535                 p_info->indoption[i] = indexRelation->rd_indoption[i];
3536         }
3537
3538         /*
3539          * to check to match the expression's parameter of index with child indexes
3540          */
3541         p_info->expression_str = NULL;
3542         if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indexprs))
3543         {
3544                 Datum       exprsDatum;
3545                 bool            isnull;
3546                 Datum           result;
3547
3548                 exprsDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3549                                                                          Anum_pg_index_indexprs, &isnull);
3550
3551                 result = DirectFunctionCall2(pg_get_expr,
3552                                                                          exprsDatum,
3553                                                                          ObjectIdGetDatum(relid));
3554
3555                 p_info->expression_str = text_to_cstring(DatumGetTextP(result));
3556         }
3557
3558         /*
3559          * to check to match the predicate's parameter of index with child indexes
3560          */
3561         p_info->indpred_str = NULL;
3562         if(!heap_attisnull(indexRelation->rd_indextuple, Anum_pg_index_indpred))
3563         {
3564                 Datum       predDatum;
3565                 bool            isnull;
3566                 Datum           result;
3567
3568                 predDatum = SysCacheGetAttr(INDEXRELID, indexRelation->rd_indextuple,
3569                                                                          Anum_pg_index_indpred, &isnull);
3570
3571                 result = DirectFunctionCall2(pg_get_expr,
3572                                                                          predDatum,
3573                                                                          ObjectIdGetDatum(relid));
3574
3575                 p_info->indpred_str = text_to_cstring(DatumGetTextP(result));
3576         }
3577
3578         index_close(indexRelation, NoLock);
3579
3580         return p_info;
3581 }
3582
3583 /*
3584  * cancel hint enforcement
3585  */
3586 static void
3587 reset_hint_enforcement()
3588 {
3589         setup_scan_method_enforcement(NULL, current_hint_state);
3590         setup_parallel_plan_enforcement(NULL, current_hint_state);
3591 }
3592
3593 /*
3594  * Set planner guc parameters according to corresponding scan hints.  Returns
3595  * bitmap of HintTypeBitmap. If shint or phint is not NULL, set used hint
3596  * there respectively.
3597  */
3598 static bool
3599 setup_hint_enforcement(PlannerInfo *root, RelOptInfo *rel,
3600                                            ScanMethodHint **rshint, ParallelHint **rphint)
3601 {
3602         Index   new_parent_relid = 0;
3603         ListCell *l;
3604         ScanMethodHint *shint = NULL;
3605         ParallelHint   *phint = NULL;
3606         bool                    inhparent = root->simple_rte_array[rel->relid]->inh;
3607         Oid             relationObjectId = root->simple_rte_array[rel->relid]->relid;
3608         int                             ret = 0;
3609
3610         /* reset returns if requested  */
3611         if (rshint != NULL) *rshint = NULL;
3612         if (rphint != NULL) *rphint = NULL;
3613
3614         /*
3615          * We could register the parent relation of the following children here
3616          * when inhparent == true but inheritnce planner doesn't call this function
3617          * for parents. Since we cannot distinguish who called this function we
3618          * cannot do other than always seeking the parent regardless of who called
3619          * this function.
3620          */
3621         if (inhparent)
3622         {
3623                 if (debug_level > 1)
3624                         ereport(pg_hint_plan_message_level,
3625                                         (errhidestmt(true),
3626                                          errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3627                                                          " skipping inh parent: relation=%u(%s), inhparent=%d,"
3628                                                          " current_hint_state=%p, hint_inhibit_level=%d",
3629                                                          qnostr, relationObjectId,
3630                                                          get_rel_name(relationObjectId),
3631                                                          inhparent, current_hint_state, hint_inhibit_level)));
3632                 return 0;
3633         }
3634
3635         /* Find the parent for this relation other than the registered parent */
3636         foreach (l, root->append_rel_list)
3637         {
3638                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3639
3640                 if (appinfo->child_relid == rel->relid)
3641                 {
3642                         if (current_hint_state->parent_relid != appinfo->parent_relid)
3643                                 new_parent_relid = appinfo->parent_relid;
3644                         break;
3645                 }
3646         }
3647
3648         if (!l)
3649         {
3650                 /* This relation doesn't have a parent. Cancel current_hint_state. */
3651                 current_hint_state->parent_relid = 0;
3652                 current_hint_state->parent_scan_hint = NULL;
3653                 current_hint_state->parent_parallel_hint = NULL;
3654         }
3655
3656         if (new_parent_relid > 0)
3657         {
3658                 /*
3659                  * Here we found a new parent for the current relation. Scan continues
3660                  * hint to other childrens of this parent so remember it * to avoid
3661                  * hinthintredundant setup cost.
3662                  */
3663                 current_hint_state->parent_relid = new_parent_relid;
3664                                 
3665                 /* Find hints for the parent */
3666                 current_hint_state->parent_scan_hint =
3667                         find_scan_hint(root, current_hint_state->parent_relid);
3668
3669                 current_hint_state->parent_parallel_hint =
3670                         find_parallel_hint(root, current_hint_state->parent_relid);
3671
3672                 /*
3673                  * If hint is found for the parent, apply it for this child instead
3674                  * of its own.
3675                  */
3676                 if (current_hint_state->parent_scan_hint)
3677                 {
3678                         ScanMethodHint * pshint = current_hint_state->parent_scan_hint;
3679
3680                         pshint->base.state = HINT_STATE_USED;
3681
3682                         /* Apply index mask in the same manner to the parent. */
3683                         if (pshint->indexnames)
3684                         {
3685                                 Oid                     parentrel_oid;
3686                                 Relation        parent_rel;
3687
3688                                 parentrel_oid =
3689                                         root->simple_rte_array[current_hint_state->parent_relid]->relid;
3690                                 parent_rel = heap_open(parentrel_oid, NoLock);
3691
3692                                 /* Search the parent relation for indexes match the hint spec */
3693                                 foreach(l, RelationGetIndexList(parent_rel))
3694                                 {
3695                                         Oid         indexoid = lfirst_oid(l);
3696                                         char       *indexname = get_rel_name(indexoid);
3697                                         ListCell   *lc;
3698                                         ParentIndexInfo *parent_index_info;
3699
3700                                         foreach(lc, pshint->indexnames)
3701                                         {
3702                                                 if (RelnameCmp(&indexname, &lfirst(lc)) == 0)
3703                                                         break;
3704                                         }
3705                                         if (!lc)
3706                                                 continue;
3707
3708                                         parent_index_info =
3709                                                 get_parent_index_info(indexoid, parentrel_oid);
3710                                         current_hint_state->parent_index_infos =
3711                                                 lappend(current_hint_state->parent_index_infos,
3712                                                                 parent_index_info);
3713                                 }
3714                                 heap_close(parent_rel, NoLock);
3715                         }
3716                 }
3717         }
3718
3719         shint = find_scan_hint(root, rel->relid);
3720         if (!shint)
3721                 shint = current_hint_state->parent_scan_hint;
3722
3723         if (shint)
3724         {
3725                 bool using_parent_hint =
3726                         (shint == current_hint_state->parent_scan_hint);
3727
3728                 ret |= HINT_BM_SCAN_METHOD;
3729
3730                 /* Setup scan enforcement environment */
3731                 setup_scan_method_enforcement(shint, current_hint_state);
3732
3733                 /* restrict unwanted inexes */
3734                 restrict_indexes(root, shint, rel, using_parent_hint);
3735
3736                 if (debug_level > 1)
3737                 {
3738                         char *additional_message = "";
3739
3740                         if (shint == current_hint_state->parent_scan_hint)
3741                                 additional_message = " by parent hint";
3742
3743                         ereport(pg_hint_plan_message_level,
3744                                         (errhidestmt(true),
3745                                          errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3746                                                          " index deletion%s:"
3747                                                          " relation=%u(%s), inhparent=%d, "
3748                                                          "current_hint_state=%p,"
3749                                                          " hint_inhibit_level=%d, scanmask=0x%x",
3750                                                          qnostr, additional_message,
3751                                                          relationObjectId,
3752                                                          get_rel_name(relationObjectId),
3753                                                          inhparent, current_hint_state,
3754                                                          hint_inhibit_level,
3755                                                          shint->enforce_mask)));
3756                 }
3757         }
3758
3759         /* Do the same for parallel plan enforcement */
3760         phint = find_parallel_hint(root, rel->relid);
3761         if (!phint)
3762                 phint = current_hint_state->parent_parallel_hint;
3763
3764         setup_parallel_plan_enforcement(phint, current_hint_state);
3765
3766         if (phint)
3767                 ret |= HINT_BM_PARALLEL;
3768
3769         /* Nothing to apply. Reset the scan mask to intial state */
3770         if (!shint && ! phint)
3771         {
3772                 if (debug_level > 1)
3773                         ereport(pg_hint_plan_message_level,
3774                                         (errhidestmt (true),
3775                                          errmsg ("pg_hint_plan%s: setup_hint_enforcement"
3776                                                          " no hint applied:"
3777                                                          " relation=%u(%s), inhparent=%d, current_hint=%p,"
3778                                                          " hint_inhibit_level=%d, scanmask=0x%x",
3779                                                          qnostr, relationObjectId,
3780                                                          get_rel_name(relationObjectId),
3781                                                          inhparent, current_hint_state, hint_inhibit_level,
3782                                                          current_hint_state->init_scan_mask)));
3783
3784                 setup_scan_method_enforcement(NULL,     current_hint_state);
3785
3786                 return ret;
3787         }
3788
3789         if (rshint != NULL) *rshint = shint;
3790         if (rphint != NULL) *rphint = phint;
3791
3792         return ret;
3793 }
3794
3795 /*
3796  * Return index of relation which matches given aliasname, or 0 if not found.
3797  * If same aliasname was used multiple times in a query, return -1.
3798  */
3799 static int
3800 find_relid_aliasname(PlannerInfo *root, char *aliasname, List *initial_rels,
3801                                          const char *str)
3802 {
3803         int             i;
3804         Index   found = 0;
3805
3806         for (i = 1; i < root->simple_rel_array_size; i++)
3807         {
3808                 ListCell   *l;
3809
3810                 if (root->simple_rel_array[i] == NULL)
3811                         continue;
3812
3813                 Assert(i == root->simple_rel_array[i]->relid);
3814
3815                 if (RelnameCmp(&aliasname,
3816                                            &root->simple_rte_array[i]->eref->aliasname) != 0)
3817                         continue;
3818
3819                 foreach(l, initial_rels)
3820                 {
3821                         RelOptInfo *rel = (RelOptInfo *) lfirst(l);
3822
3823                         if (rel->reloptkind == RELOPT_BASEREL)
3824                         {
3825                                 if (rel->relid != i)
3826                                         continue;
3827                         }
3828                         else
3829                         {
3830                                 Assert(rel->reloptkind == RELOPT_JOINREL);
3831
3832                                 if (!bms_is_member(i, rel->relids))
3833                                         continue;
3834                         }
3835
3836                         if (found != 0)
3837                         {
3838                                 hint_ereport(str,
3839                                                          ("Relation name \"%s\" is ambiguous.",
3840                                                           aliasname));
3841                                 return -1;
3842                         }
3843
3844                         found = i;
3845                         break;
3846                 }
3847
3848         }
3849
3850         return found;
3851 }
3852
3853 /*
3854  * Return join hint which matches given joinrelids.
3855  */
3856 static JoinMethodHint *
3857 find_join_hint(Relids joinrelids)
3858 {
3859         List       *join_hint;
3860         ListCell   *l;
3861
3862         join_hint = current_hint_state->join_hint_level[bms_num_members(joinrelids)];
3863
3864         foreach(l, join_hint)
3865         {
3866                 JoinMethodHint *hint = (JoinMethodHint *) lfirst(l);
3867
3868                 if (bms_equal(joinrelids, hint->joinrelids))
3869                         return hint;
3870         }
3871
3872         return NULL;
3873 }
3874
3875 static Relids
3876 OuterInnerJoinCreate(OuterInnerRels *outer_inner, LeadingHint *leading_hint,
3877         PlannerInfo *root, List *initial_rels, HintState *hstate, int nbaserel)
3878 {
3879         OuterInnerRels *outer_rels;
3880         OuterInnerRels *inner_rels;
3881         Relids                  outer_relids;
3882         Relids                  inner_relids;
3883         Relids                  join_relids;
3884         JoinMethodHint *hint;
3885
3886         if (outer_inner->relation != NULL)
3887         {
3888                 return bms_make_singleton(
3889                                         find_relid_aliasname(root, outer_inner->relation,
3890                                                                                  initial_rels,
3891                                                                                  leading_hint->base.hint_str));
3892         }
3893
3894         outer_rels = lfirst(outer_inner->outer_inner_pair->head);
3895         inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
3896
3897         outer_relids = OuterInnerJoinCreate(outer_rels,
3898                                                                                 leading_hint,
3899                                                                                 root,
3900                                                                                 initial_rels,
3901                                                                                 hstate,
3902                                                                                 nbaserel);
3903         inner_relids = OuterInnerJoinCreate(inner_rels,
3904                                                                                 leading_hint,
3905                                                                                 root,
3906                                                                                 initial_rels,
3907                                                                                 hstate,
3908                                                                                 nbaserel);
3909
3910         join_relids = bms_add_members(outer_relids, inner_relids);
3911
3912         if (bms_num_members(join_relids) > nbaserel)
3913                 return join_relids;
3914
3915         /*
3916          * If we don't have join method hint, create new one for the
3917          * join combination with all join methods are enabled.
3918          */
3919         hint = find_join_hint(join_relids);
3920         if (hint == NULL)
3921         {
3922                 /*
3923                  * Here relnames is not set, since Relids bitmap is sufficient to
3924                  * control paths of this query afterward.
3925                  */
3926                 hint = (JoinMethodHint *) JoinMethodHintCreate(
3927                                         leading_hint->base.hint_str,
3928                                         HINT_LEADING,
3929                                         HINT_KEYWORD_LEADING);
3930                 hint->base.state = HINT_STATE_USED;
3931                 hint->nrels = bms_num_members(join_relids);
3932                 hint->enforce_mask = ENABLE_ALL_JOIN;
3933                 hint->joinrelids = bms_copy(join_relids);
3934                 hint->inner_nrels = bms_num_members(inner_relids);
3935                 hint->inner_joinrelids = bms_copy(inner_relids);
3936
3937                 hstate->join_hint_level[hint->nrels] =
3938                         lappend(hstate->join_hint_level[hint->nrels], hint);
3939         }
3940         else
3941         {
3942                 hint->inner_nrels = bms_num_members(inner_relids);
3943                 hint->inner_joinrelids = bms_copy(inner_relids);
3944         }
3945
3946         return join_relids;
3947 }
3948
3949 static Relids
3950 create_bms_of_relids(Hint *base, PlannerInfo *root, List *initial_rels,
3951                 int nrels, char **relnames)
3952 {
3953         int             relid;
3954         Relids  relids = NULL;
3955         int             j;
3956         char   *relname;
3957
3958         for (j = 0; j < nrels; j++)
3959         {
3960                 relname = relnames[j];
3961
3962                 relid = find_relid_aliasname(root, relname, initial_rels,
3963                                                                          base->hint_str);
3964
3965                 if (relid == -1)
3966                         base->state = HINT_STATE_ERROR;
3967
3968                 /*
3969                  * the aliasname is not found(relid == 0) or same aliasname was used
3970                  * multiple times in a query(relid == -1)
3971                  */
3972                 if (relid <= 0)
3973                 {
3974                         relids = NULL;
3975                         break;
3976                 }
3977                 if (bms_is_member(relid, relids))
3978                 {
3979                         hint_ereport(base->hint_str,
3980                                                  ("Relation name \"%s\" is duplicated.", relname));
3981                         base->state = HINT_STATE_ERROR;
3982                         break;
3983                 }
3984
3985                 relids = bms_add_member(relids, relid);
3986         }
3987         return relids;
3988 }
3989 /*
3990  * Transform join method hint into handy form.
3991  *
3992  *   - create bitmap of relids from alias names, to make it easier to check
3993  *     whether a join path matches a join method hint.
3994  *   - add join method hints which are necessary to enforce join order
3995  *     specified by Leading hint
3996  */
3997 static bool
3998 transform_join_hints(HintState *hstate, PlannerInfo *root, int nbaserel,
3999                 List *initial_rels, JoinMethodHint **join_method_hints)
4000 {
4001         int                             i;
4002         int                             relid;
4003         Relids                  joinrelids;
4004         int                             njoinrels;
4005         ListCell           *l;
4006         char               *relname;
4007         LeadingHint        *lhint = NULL;
4008
4009         /*
4010          * Create bitmap of relids from alias names for each join method hint.
4011          * Bitmaps are more handy than strings in join searching.
4012          */
4013         for (i = 0; i < hstate->num_hints[HINT_TYPE_JOIN_METHOD]; i++)
4014         {
4015                 JoinMethodHint *hint = hstate->join_hints[i];
4016
4017                 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4018                         continue;
4019
4020                 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4021                                                                          initial_rels, hint->nrels, hint->relnames);
4022
4023                 if (hint->joinrelids == NULL || hint->base.state == HINT_STATE_ERROR)
4024                         continue;
4025
4026                 hstate->join_hint_level[hint->nrels] =
4027                         lappend(hstate->join_hint_level[hint->nrels], hint);
4028         }
4029
4030         /*
4031          * Create bitmap of relids from alias names for each rows hint.
4032          * Bitmaps are more handy than strings in join searching.
4033          */
4034         for (i = 0; i < hstate->num_hints[HINT_TYPE_ROWS]; i++)
4035         {
4036                 RowsHint *hint = hstate->rows_hints[i];
4037
4038                 if (!hint_state_enabled(hint) || hint->nrels > nbaserel)
4039                         continue;
4040
4041                 hint->joinrelids = create_bms_of_relids(&(hint->base), root,
4042                                                                          initial_rels, hint->nrels, hint->relnames);
4043         }
4044
4045         /* Do nothing if no Leading hint was supplied. */
4046         if (hstate->num_hints[HINT_TYPE_LEADING] == 0)
4047                 return false;
4048
4049         /*
4050          * Decide whether to use Leading hint
4051          */
4052         for (i = 0; i < hstate->num_hints[HINT_TYPE_LEADING]; i++)
4053         {
4054                 LeadingHint        *leading_hint = (LeadingHint *)hstate->leading_hint[i];
4055                 Relids                  relids;
4056
4057                 if (leading_hint->base.state == HINT_STATE_ERROR)
4058                         continue;
4059
4060                 relid = 0;
4061                 relids = NULL;
4062
4063                 foreach(l, leading_hint->relations)
4064                 {
4065                         relname = (char *)lfirst(l);;
4066
4067                         relid = find_relid_aliasname(root, relname, initial_rels,
4068                                                                                  leading_hint->base.hint_str);
4069                         if (relid == -1)
4070                                 leading_hint->base.state = HINT_STATE_ERROR;
4071
4072                         if (relid <= 0)
4073                                 break;
4074
4075                         if (bms_is_member(relid, relids))
4076                         {
4077                                 hint_ereport(leading_hint->base.hint_str,
4078                                                          ("Relation name \"%s\" is duplicated.", relname));
4079                                 leading_hint->base.state = HINT_STATE_ERROR;
4080                                 break;
4081                         }
4082
4083                         relids = bms_add_member(relids, relid);
4084                 }
4085
4086                 if (relid <= 0 || leading_hint->base.state == HINT_STATE_ERROR)
4087                         continue;
4088
4089                 if (lhint != NULL)
4090                 {
4091                         hint_ereport(lhint->base.hint_str,
4092                                  ("Conflict %s hint.", HintTypeName[lhint->base.type]));
4093                         lhint->base.state = HINT_STATE_DUPLICATION;
4094                 }
4095                 leading_hint->base.state = HINT_STATE_USED;
4096                 lhint = leading_hint;
4097         }
4098
4099         /* check to exist Leading hint marked with 'used'. */
4100         if (lhint == NULL)
4101                 return false;
4102
4103         /*
4104          * We need join method hints which fit specified join order in every join
4105          * level.  For example, Leading(A B C) virtually requires following join
4106          * method hints, if no join method hint supplied:
4107          *   - level 1: none
4108          *   - level 2: NestLoop(A B), MergeJoin(A B), HashJoin(A B)
4109          *   - level 3: NestLoop(A B C), MergeJoin(A B C), HashJoin(A B C)
4110          *
4111          * If we already have join method hint which fits specified join order in
4112          * that join level, we leave it as-is and don't add new hints.
4113          */
4114         joinrelids = NULL;
4115         njoinrels = 0;
4116         if (lhint->outer_inner == NULL)
4117         {
4118                 foreach(l, lhint->relations)
4119                 {
4120                         JoinMethodHint *hint;
4121
4122                         relname = (char *)lfirst(l);
4123
4124                         /*
4125                          * Find relid of the relation which has given name.  If we have the
4126                          * name given in Leading hint multiple times in the join, nothing to
4127                          * do.
4128                          */
4129                         relid = find_relid_aliasname(root, relname, initial_rels,
4130                                                                                  hstate->hint_str);
4131
4132                         /* Create bitmap of relids for current join level. */
4133                         joinrelids = bms_add_member(joinrelids, relid);
4134                         njoinrels++;
4135
4136                         /* We never have join method hint for single relation. */
4137                         if (njoinrels < 2)
4138                                 continue;
4139
4140                         /*
4141                          * If we don't have join method hint, create new one for the
4142                          * join combination with all join methods are enabled.
4143                          */
4144                         hint = find_join_hint(joinrelids);
4145                         if (hint == NULL)
4146                         {
4147                                 /*
4148                                  * Here relnames is not set, since Relids bitmap is sufficient
4149                                  * to control paths of this query afterward.
4150                                  */
4151                                 hint = (JoinMethodHint *) JoinMethodHintCreate(
4152                                                                                         lhint->base.hint_str,
4153                                                                                         HINT_LEADING,
4154                                                                                         HINT_KEYWORD_LEADING);
4155                                 hint->base.state = HINT_STATE_USED;
4156                                 hint->nrels = njoinrels;
4157                                 hint->enforce_mask = ENABLE_ALL_JOIN;
4158                                 hint->joinrelids = bms_copy(joinrelids);
4159                         }
4160
4161                         join_method_hints[njoinrels] = hint;
4162
4163                         if (njoinrels >= nbaserel)
4164                                 break;
4165                 }
4166                 bms_free(joinrelids);
4167
4168                 if (njoinrels < 2)
4169                         return false;
4170
4171                 /*
4172                  * Delete all join hints which have different combination from Leading
4173                  * hint.
4174                  */
4175                 for (i = 2; i <= njoinrels; i++)
4176                 {
4177                         list_free(hstate->join_hint_level[i]);
4178
4179                         hstate->join_hint_level[i] = lappend(NIL, join_method_hints[i]);
4180                 }
4181         }
4182         else
4183         {
4184                 joinrelids = OuterInnerJoinCreate(lhint->outer_inner,
4185                                                                                   lhint,
4186                                           root,
4187                                           initial_rels,
4188                                                                                   hstate,
4189                                                                                   nbaserel);
4190
4191                 njoinrels = bms_num_members(joinrelids);
4192                 Assert(njoinrels >= 2);
4193
4194                 /*
4195                  * Delete all join hints which have different combination from Leading
4196                  * hint.
4197                  */
4198                 for (i = 2;i <= njoinrels; i++)
4199                 {
4200                         if (hstate->join_hint_level[i] != NIL)
4201                         {
4202                                 ListCell *prev = NULL;
4203                                 ListCell *next = NULL;
4204                                 for(l = list_head(hstate->join_hint_level[i]); l; l = next)
4205                                 {
4206
4207                                         JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
4208
4209                                         next = lnext(l);
4210
4211                                         if (hint->inner_nrels == 0 &&
4212                                                 !(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
4213                                                   bms_equal(bms_union(hint->joinrelids, joinrelids),
4214                                                   hint->joinrelids)))
4215                                         {
4216                                                 hstate->join_hint_level[i] =
4217                                                         list_delete_cell(hstate->join_hint_level[i], l,
4218                                                                                          prev);
4219                                         }
4220                                         else
4221                                                 prev = l;
4222                                 }
4223                         }
4224                 }
4225
4226                 bms_free(joinrelids);
4227         }
4228
4229         if (hint_state_enabled(lhint))
4230         {
4231                 set_join_config_options(DISABLE_ALL_JOIN, current_hint_state->context);
4232                 return true;
4233         }
4234         return false;
4235 }
4236
4237 /*
4238  * wrapper of make_join_rel()
4239  *
4240  * call make_join_rel() after changing enable_* parameters according to given
4241  * hints.
4242  */
4243 static RelOptInfo *
4244 make_join_rel_wrapper(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
4245 {
4246         Relids                  joinrelids;
4247         JoinMethodHint *hint;
4248         RelOptInfo         *rel;
4249         int                             save_nestlevel;
4250
4251         joinrelids = bms_union(rel1->relids, rel2->relids);
4252         hint = find_join_hint(joinrelids);
4253         bms_free(joinrelids);
4254
4255         if (!hint)
4256                 return pg_hint_plan_make_join_rel(root, rel1, rel2);
4257
4258         if (hint->inner_nrels == 0)
4259         {
4260                 save_nestlevel = NewGUCNestLevel();
4261
4262                 set_join_config_options(hint->enforce_mask,
4263                                                                 current_hint_state->context);
4264
4265                 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4266                 hint->base.state = HINT_STATE_USED;
4267
4268                 /*
4269                  * Restore the GUC variables we set above.
4270                  */
4271                 AtEOXact_GUC(true, save_nestlevel);
4272         }
4273         else
4274                 rel = pg_hint_plan_make_join_rel(root, rel1, rel2);
4275
4276         return rel;
4277 }
4278
4279 /*
4280  * TODO : comment
4281  */
4282 static void
4283 add_paths_to_joinrel_wrapper(PlannerInfo *root,
4284                                                          RelOptInfo *joinrel,
4285                                                          RelOptInfo *outerrel,
4286                                                          RelOptInfo *innerrel,
4287                                                          JoinType jointype,
4288                                                          SpecialJoinInfo *sjinfo,
4289                                                          List *restrictlist)
4290 {
4291         Relids                  joinrelids;
4292         JoinMethodHint *join_hint;
4293         int                             save_nestlevel;
4294
4295         joinrelids = bms_union(outerrel->relids, innerrel->relids);
4296         join_hint = find_join_hint(joinrelids);
4297         bms_free(joinrelids);
4298
4299         if (join_hint && join_hint->inner_nrels != 0)
4300         {
4301                 save_nestlevel = NewGUCNestLevel();
4302
4303                 if (bms_equal(join_hint->inner_joinrelids, innerrel->relids))
4304                 {
4305
4306                         set_join_config_options(join_hint->enforce_mask,
4307                                                                         current_hint_state->context);
4308
4309                         add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4310                                                                  sjinfo, restrictlist);
4311                         join_hint->base.state = HINT_STATE_USED;
4312                 }
4313                 else
4314                 {
4315                         set_join_config_options(DISABLE_ALL_JOIN,
4316                                                                         current_hint_state->context);
4317                         add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4318                                                                  sjinfo, restrictlist);
4319                 }
4320
4321                 /*
4322                  * Restore the GUC variables we set above.
4323                  */
4324                 AtEOXact_GUC(true, save_nestlevel);
4325         }
4326         else
4327                 add_paths_to_joinrel(root, joinrel, outerrel, innerrel, jointype,
4328                                                          sjinfo, restrictlist);
4329 }
4330
4331 static int
4332 get_num_baserels(List *initial_rels)
4333 {
4334         int                     nbaserel = 0;
4335         ListCell   *l;
4336
4337         foreach(l, initial_rels)
4338         {
4339                 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
4340
4341                 if (rel->reloptkind == RELOPT_BASEREL)
4342                         nbaserel++;
4343                 else if (rel->reloptkind ==RELOPT_JOINREL)
4344                         nbaserel+= bms_num_members(rel->relids);
4345                 else
4346                 {
4347                         /* other values not expected here */
4348                         elog(ERROR, "unrecognized reloptkind type: %d", rel->reloptkind);
4349                 }
4350         }
4351
4352         return nbaserel;
4353 }
4354
4355 static RelOptInfo *
4356 pg_hint_plan_join_search(PlannerInfo *root, int levels_needed,
4357                                                  List *initial_rels)
4358 {
4359         JoinMethodHint    **join_method_hints;
4360         int                                     nbaserel;
4361         RelOptInfo                 *rel;
4362         int                                     i;
4363         bool                            leading_hint_enable;
4364
4365         /*
4366          * Use standard planner (or geqo planner) if pg_hint_plan is disabled or no
4367          * valid hint is supplied or current nesting depth is nesting depth of SPI
4368          * calls.
4369          */
4370         if (!current_hint_state || hint_inhibit_level > 0)
4371         {
4372                 if (prev_join_search)
4373                         return (*prev_join_search) (root, levels_needed, initial_rels);
4374                 else if (enable_geqo && levels_needed >= geqo_threshold)
4375                         return geqo(root, levels_needed, initial_rels);
4376                 else
4377                         return standard_join_search(root, levels_needed, initial_rels);
4378         }
4379
4380         /*
4381          * In the case using GEQO, only scan method hints and Set hints have
4382          * effect.  Join method and join order is not controllable by hints.
4383          */
4384         if (enable_geqo && levels_needed >= geqo_threshold)
4385                 return geqo(root, levels_needed, initial_rels);
4386
4387         nbaserel = get_num_baserels(initial_rels);
4388         current_hint_state->join_hint_level =
4389                 palloc0(sizeof(List *) * (nbaserel + 1));
4390         join_method_hints = palloc0(sizeof(JoinMethodHint *) * (nbaserel + 1));
4391
4392         leading_hint_enable = transform_join_hints(current_hint_state,
4393                                                                                            root, nbaserel,
4394                                                                                            initial_rels, join_method_hints);
4395
4396         rel = pg_hint_plan_standard_join_search(root, levels_needed, initial_rels);
4397
4398         for (i = 2; i <= nbaserel; i++)
4399         {
4400                 list_free(current_hint_state->join_hint_level[i]);
4401
4402                 /* free Leading hint only */
4403                 if (join_method_hints[i] != NULL &&
4404                         join_method_hints[i]->enforce_mask == ENABLE_ALL_JOIN)
4405                         JoinMethodHintDelete(join_method_hints[i]);
4406         }
4407         pfree(current_hint_state->join_hint_level);
4408         pfree(join_method_hints);
4409
4410         if (leading_hint_enable)
4411                 set_join_config_options(current_hint_state->init_join_mask,
4412                                                                 current_hint_state->context);
4413
4414         return rel;
4415 }
4416
4417 /*
4418  * Force number of wokers if instructed by hint
4419  */
4420 void
4421 pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
4422                                                           Index rti, RangeTblEntry *rte)
4423 {
4424         ParallelHint   *phint;
4425         ListCell           *l;
4426         int                             found_hints;
4427
4428         /* call the previous hook */
4429         if (prev_set_rel_pathlist)
4430                 prev_set_rel_pathlist(root, rel, rti, rte);
4431
4432         /* Nothing to do if no hint available */
4433         if (current_hint_state == NULL)
4434                 return;
4435
4436         /* Don't touch dummy rels. */
4437         if (IS_DUMMY_REL(rel))
4438                 return;
4439
4440         /*
4441          * We can accept only plain relations, foreign tables and table saples are
4442          * also unacceptable. See set_rel_pathlist.
4443          */
4444         if (rel->rtekind != RTE_RELATION ||
4445                 rte->relkind == RELKIND_FOREIGN_TABLE ||
4446                 rte->tablesample != NULL)
4447                 return;
4448
4449         /* We cannot handle if this requires an outer */
4450         if (rel->lateral_relids)
4451                 return;
4452
4453         /* Return if this relation gets no enfocement */
4454         if ((found_hints = setup_hint_enforcement(root, rel, NULL, &phint)) == 0)
4455                 return;
4456
4457         /* Here, we regenerate paths with the current hint restriction */
4458         if (found_hints & HINT_BM_SCAN_METHOD || found_hints & HINT_BM_PARALLEL)
4459         {
4460                 /* Just discard all the paths considered so far */
4461                 list_free_deep(rel->pathlist);
4462                 rel->pathlist = NIL;
4463
4464                 /* Remove all the partial paths if Parallel hint is specfied */
4465                 if ((found_hints & HINT_BM_PARALLEL) && rel->partial_pathlist)
4466                 {
4467                         list_free_deep(rel->partial_pathlist);
4468                         rel->partial_pathlist = NIL;
4469                 }
4470
4471                 /* Regenerate paths with the current enforcement */
4472                 set_plain_rel_pathlist(root, rel, rte);
4473
4474                 /* Additional work to enforce parallel query execution */
4475                 if (phint && phint->nworkers > 0)
4476                 {
4477                         /* Lower the priorities of non-parallel paths */
4478                         foreach (l, rel->pathlist)
4479                         {
4480                                 Path *path = (Path *) lfirst(l);
4481
4482                                 if (path->startup_cost < disable_cost)
4483                                 {
4484                                         path->startup_cost += disable_cost;
4485                                         path->total_cost += disable_cost;
4486                                 }
4487                         }
4488
4489                         /* enforce number of workers if requested */
4490                         if (phint->force_parallel)
4491                         {
4492                                 foreach (l, rel->partial_pathlist)
4493                                 {
4494                                         Path *ppath = (Path *) lfirst(l);
4495
4496                                         ppath->parallel_workers = phint->nworkers;
4497                                 }
4498                         }
4499
4500                         /* Generate gather paths for base rels */
4501                         if (rel->reloptkind == RELOPT_BASEREL)
4502                                 generate_gather_paths(root, rel);
4503                 }
4504         }
4505
4506         reset_hint_enforcement();
4507 }
4508
4509 /*
4510  * set_rel_pathlist
4511  *        Build access paths for a base relation
4512  *
4513  * This function was copied and edited from set_rel_pathlist() in
4514  * src/backend/optimizer/path/allpaths.c in order not to copy other static
4515  * functions not required here.
4516  */
4517 static void
4518 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
4519                                  Index rti, RangeTblEntry *rte)
4520 {
4521         if (IS_DUMMY_REL(rel))
4522         {
4523                 /* We already proved the relation empty, so nothing more to do */
4524         }
4525         else if (rte->inh)
4526         {
4527                 /* It's an "append relation", process accordingly */
4528                 set_append_rel_pathlist(root, rel, rti, rte);
4529         }
4530         else
4531         {
4532                 if (rel->rtekind == RTE_RELATION)
4533                 {
4534                         if (rte->relkind == RELKIND_RELATION)
4535                         {
4536                                 if(rte->tablesample != NULL)
4537                                         elog(ERROR, "sampled relation is not supported");
4538
4539                                 /* Plain relation */
4540                                 set_plain_rel_pathlist(root, rel, rte);
4541                         }
4542                         else
4543                                 elog(ERROR, "unexpected relkind: %c", rte->relkind);
4544                 }
4545                 else
4546                         elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
4547         }
4548
4549         /*
4550          * Allow a plugin to editorialize on the set of Paths for this base
4551          * relation.  It could add new paths (such as CustomPaths) by calling
4552          * add_path(), or delete or modify paths added by the core code.
4553          */
4554         if (set_rel_pathlist_hook)
4555                 (*set_rel_pathlist_hook) (root, rel, rti, rte);
4556
4557         /* Now find the cheapest of the paths for this rel */
4558         set_cheapest(rel);
4559 }
4560
4561 /*
4562  * stmt_beg callback is called when each query in PL/pgSQL function is about
4563  * to be executed.  At that timing, we save query string in the global variable
4564  * plpgsql_query_string to use it in planner hook.  It's safe to use one global
4565  * variable for the purpose, because its content is only necessary until
4566  * planner hook is called for the query, so recursive PL/pgSQL function calls
4567  * don't harm this mechanism.
4568  */
4569 static void
4570 pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4571 {
4572         plpgsql_recurse_level++;
4573 }
4574
4575 /*
4576  * stmt_end callback is called then each query in PL/pgSQL function has
4577  * finished.  At that timing, we clear plpgsql_query_string to tell planner
4578  * hook that next call is not for a query written in PL/pgSQL block.
4579  */
4580 static void
4581 pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
4582 {
4583         plpgsql_recurse_level--;
4584 }
4585
4586 void plpgsql_query_erase_callback(ResourceReleasePhase phase,
4587                                                                   bool isCommit,
4588                                                                   bool isTopLevel,
4589                                                                   void *arg)
4590 {
4591         if (phase != RESOURCE_RELEASE_AFTER_LOCKS)
4592                 return;
4593         /* Cancel plpgsql nest level*/
4594         plpgsql_recurse_level = 0;
4595 }
4596
4597 #define standard_join_search pg_hint_plan_standard_join_search
4598 #define join_search_one_level pg_hint_plan_join_search_one_level
4599 #define make_join_rel make_join_rel_wrapper
4600 #include "core.c"
4601
4602 #undef make_join_rel
4603 #define make_join_rel pg_hint_plan_make_join_rel
4604 #define add_paths_to_joinrel add_paths_to_joinrel_wrapper
4605 #include "make_join_rel.c"
4606
4607 #include "pg_stat_statements.c"