OSDN Git Service

Fix behavior of nested planning
authorKyotaro Horiguchi <horikyota.ntt@gmail.com>
Fri, 14 Feb 2020 04:44:27 +0000 (13:44 +0900)
committerKyotaro Horiguchi <horikyota.ntt@gmail.com>
Fri, 14 Feb 2020 07:31:26 +0000 (16:31 +0900)
Issue: https://github.com/ossc-db/pg_hint_plan/issues/39

When planning is nested by function call, the hints in the function
should affect the corresponding query. Maybe 9599067 introduced
that. Fix the nested planning behavior.

expected/ut-A.out
pg_hint_plan.c
sql/ut-A.sql

index f0f1bbc..c35545f 100644 (file)
@@ -4289,6 +4289,11 @@ CREATE OR REPLACE FUNCTION recall_planner() RETURNS int AS $$
         ORDER BY t_1.c1 LIMIT 1;
 $$ LANGUAGE SQL IMMUTABLE;
 --No.13-4-1
+-- recall_planner() is reduced to constant while planning using the
+-- hint defined in the function. Then the outer query is planned based
+-- on the following hint. pg_hint_plan shows the log for the function
+-- but the resulting explain output doesn't contain the corresponding
+-- plan.
 /*+HashJoin(t_1 t_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
@@ -4296,7 +4301,7 @@ EXPLAIN (COSTS false)
   ORDER BY t_1.c1;
 LOG:  pg_hint_plan:
 used hint:
-HashJoin(t_1 t_2)
+IndexScan(t_1)
 not used hint:
 duplication hint:
 error hint:
@@ -4320,6 +4325,7 @@ error hint:
 (7 rows)
 
 --No.13-4-2
+--See description for No.13-4-1
 /*+HashJoin(st_1 st_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 st_1
@@ -4327,8 +4333,8 @@ EXPLAIN (COSTS false)
   ORDER BY st_1.c1;
 LOG:  pg_hint_plan:
 used hint:
+IndexScan(t_1)
 not used hint:
-HashJoin(st_1 st_2)
 duplication hint:
 error hint:
 
@@ -4351,6 +4357,7 @@ error hint:
 (7 rows)
 
 --No.13-4-3
+--See description for No.13-4-1
 /*+HashJoin(t_1 t_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 st_1
@@ -4358,7 +4365,7 @@ EXPLAIN (COSTS false)
   ORDER BY st_1.c1;
 LOG:  pg_hint_plan:
 used hint:
-HashJoin(t_1 t_2)
+IndexScan(t_1)
 not used hint:
 duplication hint:
 error hint:
@@ -4381,6 +4388,7 @@ error hint:
 (6 rows)
 
 --No.13-4-4
+--See description for No.13-4-1
 /*+HashJoin(st_1 st_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
@@ -4388,8 +4396,8 @@ EXPLAIN (COSTS false)
   ORDER BY t_1.c1;
 LOG:  pg_hint_plan:
 used hint:
+IndexScan(t_1)
 not used hint:
-HashJoin(st_1 st_2)
 duplication hint:
 error hint:
 
@@ -4411,18 +4419,18 @@ error hint:
 (6 rows)
 
 --No.13-4-5
+-- See description for No.13-4-1. No joins in ths plan, so
+-- pg_hint_plan doesn't complain on the wrongly written error hint.
 /*+HashJoin(t_1 t_1)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
   ORDER BY t_1.c1;
-INFO:  pg_hint_plan: hint syntax error at or near "HashJoin(t_1 t_1)"
-DETAIL:  Relation name "t_1" is duplicated.
 LOG:  pg_hint_plan:
 used hint:
+IndexScan(t_1)
 not used hint:
 duplication hint:
 error hint:
-HashJoin(t_1 t_1)
 
 LOG:  pg_hint_plan:
 used hint:
@@ -4446,6 +4454,13 @@ EXPLAIN (COSTS false)
  SELECT recall_planner_one_t() FROM s1.t1 t_1
    JOIN s1.t2 t_2 ON (t_1.c1 = t_2.c1)
   ORDER BY t_1.c1;
+LOG:  pg_hint_plan:
+used hint:
+IndexScan(t_1)
+not used hint:
+duplication hint:
+error hint:
+
                  QUERY PLAN                  
 ---------------------------------------------
  Merge Join
@@ -4463,8 +4478,8 @@ EXPLAIN (COSTS false)
   ORDER BY t_1.c1;
 LOG:  pg_hint_plan:
 used hint:
+IndexScan(t_1)
 not used hint:
-HashJoin(t_1 t_1)
 duplication hint:
 error hint:
 
@@ -4490,19 +4505,18 @@ HashJoin(t_1 t_1)
 DROP FUNCTION recall_planner_one_t(int);
 ERROR:  function recall_planner_one_t(integer) does not exist
 --No.13-4-7
+-- See description for No.13-4-1. Complains on the wrongly written hint.
 /*+HashJoin(t_1 t_1)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
    JOIN s1.t2 t_2 ON (t_1.c1 = t_2.c1)
   ORDER BY t_1.c1;
-INFO:  pg_hint_plan: hint syntax error at or near "HashJoin(t_1 t_1)"
-DETAIL:  Relation name "t_1" is duplicated.
 LOG:  pg_hint_plan:
 used hint:
+IndexScan(t_1)
 not used hint:
 duplication hint:
 error hint:
-HashJoin(t_1 t_1)
 
 INFO:  pg_hint_plan: hint syntax error at or near "HashJoin(t_1 t_1)"
 DETAIL:  Relation name "t_1" is duplicated.
@@ -4531,14 +4545,11 @@ EXPLAIN (COSTS false)
   ORDER BY t_1.c1;
 INFO:  pg_hint_plan: hint syntax error at or near "MergeJoin(t_1 t_2)HashJoin(t_1 t_2)"
 DETAIL:  Conflict join method hint.
-INFO:  pg_hint_plan: hint syntax error at or near "MergeJoin(t_1 t_2)HashJoin(t_1 t_2)"
-DETAIL:  Conflict join method hint.
 LOG:  pg_hint_plan:
 used hint:
-HashJoin(t_1 t_2)
+IndexScan(t_1)
 not used hint:
 duplication hint:
-MergeJoin(t_1 t_2)
 error hint:
 
 LOG:  pg_hint_plan:
index b73e33d..a1c525a 100644 (file)
@@ -1825,7 +1825,7 @@ get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
         * case of DESCRIBE message handling or EXECUTE command. We may still see a
         * candidate top-level query in pstate in the case.
         */
-       if (!p && pstate)
+       if (pstate && pstate->p_sourcetext)
                p = pstate->p_sourcetext;
 
        /* We don't see a query string, return NULL */
@@ -3094,6 +3094,7 @@ pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
         */
        recurse_level++;
        prev_hint_str = current_hint_str;
+       current_hint_str = NULL;
        
        /*
         * Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
index 7fa720c..d25137a 100644 (file)
@@ -1152,6 +1152,11 @@ CREATE OR REPLACE FUNCTION recall_planner() RETURNS int AS $$
 $$ LANGUAGE SQL IMMUTABLE;
 
 --No.13-4-1
+-- recall_planner() is reduced to constant while planning using the
+-- hint defined in the function. Then the outer query is planned based
+-- on the following hint. pg_hint_plan shows the log for the function
+-- but the resulting explain output doesn't contain the corresponding
+-- plan.
 /*+HashJoin(t_1 t_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
@@ -1159,6 +1164,7 @@ EXPLAIN (COSTS false)
   ORDER BY t_1.c1;
 
 --No.13-4-2
+--See description for No.13-4-1
 /*+HashJoin(st_1 st_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 st_1
@@ -1166,6 +1172,7 @@ EXPLAIN (COSTS false)
   ORDER BY st_1.c1;
 
 --No.13-4-3
+--See description for No.13-4-1
 /*+HashJoin(t_1 t_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 st_1
@@ -1173,6 +1180,7 @@ EXPLAIN (COSTS false)
   ORDER BY st_1.c1;
 
 --No.13-4-4
+--See description for No.13-4-1
 /*+HashJoin(st_1 st_2)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
@@ -1180,6 +1188,8 @@ EXPLAIN (COSTS false)
   ORDER BY t_1.c1;
 
 --No.13-4-5
+-- See description for No.13-4-1. No joins in ths plan, so
+-- pg_hint_plan doesn't complain on the wrongly written error hint.
 /*+HashJoin(t_1 t_1)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1
@@ -1205,6 +1215,7 @@ EXPLAIN (COSTS false)
 DROP FUNCTION recall_planner_one_t(int);
 
 --No.13-4-7
+-- See description for No.13-4-1. Complains on the wrongly written hint.
 /*+HashJoin(t_1 t_1)*/
 EXPLAIN (COSTS false)
  SELECT recall_planner() FROM s1.t1 t_1