OSDN Git Service

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