OSDN Git Service

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