OSDN Git Service

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