• R/O
  • HTTP
  • SSH
  • HTTPS

pg_hint_plan: Commit

firtst release


Commit MetaInfo

Revisiona9bcfa75e63483f3bf671d4185438e3febf22fe4 (tree)
Time2020-02-17 21:08:03
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
@@ -4324,6 +4324,9 @@ BEGIN
43244324 RETURN new_cnt;
43254325 END;
43264326 $$ LANGUAGE plpgsql IMMUTABLE;
4327+-- The function called at the bottom desn't use a hint, the immediate
4328+-- caller level should restore its own hint. So, the first LOG from
4329+-- pg_hint_plan should use the IndexScan(t_1) hint
43274330 EXPLAIN (COSTS false) SELECT nested_planner(5) FROM s1.t1 t_1 ORDER BY t_1.c1;
43284331 NOTICE: nested_planner(5)
43294332 NOTICE: nested_planner(4)
@@ -4331,7 +4334,12 @@ NOTICE: nested_planner(3)
43314334 NOTICE: nested_planner(2)
43324335 NOTICE: nested_planner(1)
43334336 LOG: pg_hint_plan:
4334-no hint
4337+used hint:
4338+IndexScan(t_1)
4339+not used hint:
4340+duplication hint:
4341+error hint:
4342+
43354343 LOG: pg_hint_plan:
43364344 used hint:
43374345 IndexScan(t_1)
@@ -4358,7 +4366,9 @@ error hint:
43584366 Index Only Scan using t1_i1 on t1 t_1
43594367 (1 row)
43604368
4361-/*+SeqScan(t_2)*/
4369+-- The top level uses SeqScan(t_1), but the function should use only
4370+-- the hint in the function.
4371+/*+SeqScan(t_1) SeqScan(t_2)*/
43624372 EXPLAIN (COSTS false) SELECT nested_planner(5) FROM s1.t1 t_1 ORDER BY t_1.c1;
43634373 NOTICE: nested_planner(5)
43644374 NOTICE: nested_planner(4)
@@ -4395,15 +4405,18 @@ error hint:
43954405
43964406 LOG: pg_hint_plan:
43974407 used hint:
4408+SeqScan(t_1)
43984409 not used hint:
43994410 SeqScan(t_2)
44004411 duplication hint:
44014412 error hint:
44024413
4403- QUERY PLAN
4404----------------------------------------
4405- Index Only Scan using t1_i1 on t1 t_1
4406-(1 row)
4414+ QUERY PLAN
4415+--------------------------
4416+ Sort
4417+ Sort Key: c1
4418+ -> Seq Scan on t1 t_1
4419+(3 rows)
44074420
44084421 ----
44094422 ---- No. A-13-4 output of debugging log on hint status
--- a/pg_hint_plan.c
+++ b/pg_hint_plan.c
@@ -3196,9 +3196,15 @@ standard_planner_proc:
31963196 }
31973197 current_hint_state = NULL;
31983198 if (prev_planner)
3199- return (*prev_planner) (parse, cursorOptions, boundParams);
3199+ result = (*prev_planner) (parse, cursorOptions, boundParams);
32003200 else
3201- return standard_planner(parse, cursorOptions, boundParams);
3201+ result = standard_planner(parse, cursorOptions, boundParams);
3202+
3203+ /* The upper-level planner still needs the current hint state */
3204+ if (HintStateStack != NIL)
3205+ current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
3206+
3207+ return result;
32023208 }
32033209
32043210 /*
--- 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