OSDN Git Service

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