OSDN Git Service

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