OSDN Git Service

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