OSDN Git Service

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