OSDN Git Service

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