OSDN Git Service

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