OSDN Git Service

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