• R/O
  • HTTP
  • SSH
  • HTTPS

pg_hint_plan: Commit

firtst release


Commit MetaInfo

Revision1d86d3b185ec7508f76e0218cc9a0dcaf2053d72 (tree)
Time2020-02-17 21:08:15
AuthorKyotaro Horiguchi <horikyota.ntt@gmai...>
CommiterKyotaro Horiguchi

Log Message

Restore current hint state when returned from non-hinted query planning.

If no hint is given for the current level query, pg_hint_plan_planner
calls the next level of planner after erasing the
current_hint_state. But it forgot to restore the state before the
planning of the rest part of the current-level query. It is
(a-kind-of) broken by the commit d422966 but overlooked as an
inevitable side-effect of the fix. Get back the behavior by restoring
current_hint_state after returned from the lower level planner with
unhinted query.

Issue: https://github.com/ossc-db/pg_hint_plan/issues/30
Reported-by: higuchi-daisuke

Change Summary

Incremental Difference

--- a/expected/ut-A.out
+++ b/expected/ut-A.out
@@ -4198,6 +4198,9 @@ BEGIN
41984198 RETURN new_cnt;
41994199 END;
42004200 $$ LANGUAGE plpgsql IMMUTABLE;
4201+-- The function called at the bottom desn't use a hint, the immediate
4202+-- caller level should restore its own hint. So, the first LOG from
4203+-- pg_hint_plan should use the IndexScan(t_1) hint
42014204 EXPLAIN (COSTS false) SELECT nested_planner(5) FROM s1.t1 t_1 ORDER BY t_1.c1;
42024205 NOTICE: nested_planner(5)
42034206 NOTICE: nested_planner(4)
@@ -4205,7 +4208,12 @@ NOTICE: nested_planner(3)
42054208 NOTICE: nested_planner(2)
42064209 NOTICE: nested_planner(1)
42074210 LOG: pg_hint_plan:
4208-no hint
4211+used hint:
4212+IndexScan(t_1)
4213+not used hint:
4214+duplication hint:
4215+error hint:
4216+
42094217 LOG: pg_hint_plan:
42104218 used hint:
42114219 IndexScan(t_1)
@@ -4232,7 +4240,9 @@ error hint:
42324240 Index Only Scan using t1_i1 on t1 t_1
42334241 (1 row)
42344242
4235-/*+SeqScan(t_2)*/
4243+-- The top level uses SeqScan(t_1), but the function should use only
4244+-- the hint in the function.
4245+/*+SeqScan(t_1) SeqScan(t_2)*/
42364246 EXPLAIN (COSTS false) SELECT nested_planner(5) FROM s1.t1 t_1 ORDER BY t_1.c1;
42374247 NOTICE: nested_planner(5)
42384248 NOTICE: nested_planner(4)
@@ -4269,15 +4279,18 @@ error hint:
42694279
42704280 LOG: pg_hint_plan:
42714281 used hint:
4282+SeqScan(t_1)
42724283 not used hint:
42734284 SeqScan(t_2)
42744285 duplication hint:
42754286 error hint:
42764287
4277- QUERY PLAN
4278----------------------------------------
4279- Index Only Scan using t1_i1 on t1 t_1
4280-(1 row)
4288+ QUERY PLAN
4289+--------------------------
4290+ Sort
4291+ Sort Key: c1
4292+ -> Seq Scan on t1 t_1
4293+(3 rows)
42814294
42824295 ----
42834296 ---- No. A-13-4 output of debugging log on hint status
--- a/pg_hint_plan.c
+++ b/pg_hint_plan.c
@@ -3205,9 +3205,15 @@ standard_planner_proc:
32053205 }
32063206 current_hint_state = NULL;
32073207 if (prev_planner)
3208- return (*prev_planner) (parse, cursorOptions, boundParams);
3208+ result = (*prev_planner) (parse, cursorOptions, boundParams);
32093209 else
3210- return standard_planner(parse, cursorOptions, boundParams);
3210+ result = standard_planner(parse, cursorOptions, boundParams);
3211+
3212+ /* The upper-level planner still needs the current hint state */
3213+ if (HintStateStack != NIL)
3214+ current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
3215+
3216+ return result;
32113217 }
32123218
32133219 /*
--- a/sql/ut-A.sql
+++ b/sql/ut-A.sql
@@ -1137,8 +1137,14 @@ BEGIN
11371137 END;
11381138 $$ LANGUAGE plpgsql IMMUTABLE;
11391139
1140+-- The function called at the bottom desn't use a hint, the immediate
1141+-- caller level should restore its own hint. So, the first LOG from
1142+-- pg_hint_plan should use the IndexScan(t_1) hint
11401143 EXPLAIN (COSTS false) SELECT nested_planner(5) FROM s1.t1 t_1 ORDER BY t_1.c1;
1141-/*+SeqScan(t_2)*/
1144+
1145+-- The top level uses SeqScan(t_1), but the function should use only
1146+-- the hint in the function.
1147+/*+SeqScan(t_1) SeqScan(t_2)*/
11421148 EXPLAIN (COSTS false) SELECT nested_planner(5) FROM s1.t1 t_1 ORDER BY t_1.c1;
11431149
11441150 ----
Show on old repository browser