OSDN Git Service

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