OSDN Git Service

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