OSDN Git Service

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