OSDN Git Service

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