OSDN Git Service

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