Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd514ea3

Browse files
committed
Fix an old thinko in SS_make_initplan_from_plan, which is used when optimizing
a MIN or MAX aggregate call into an indexscan: the initplan is being made atthe current query nesting level and so we shouldn't increment query_level.Though usually harmless, this mistake could lead to bogus "plan should notreference subplan's variable" failures on complex queries. Per bug reportfrom David Sanchez i Gregori.
1 parentcdedfe6 commitd514ea3

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

‎src/backend/optimizer/plan/subselect.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.122 2007/02/27 01:11:25 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.123 2007/07/18 21:40:57 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1328,11 +1328,14 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
13281328
Param*prm;
13291329

13301330
/*
1331-
* Set up for a new level of subquery.This is just to keep
1332-
* SS_finalize_plan from becoming confused; we don't bother with making
1333-
* a whole new PlannerInfo struct.
1331+
* We must run SS_finalize_plan(), since that's normally done before a
1332+
* subplan gets put into the initplan list. However it will try to attach
1333+
* any pre-existing initplans to this one, which we don't want (they are
1334+
* siblings not children of this initplan). So, a quick kluge to hide
1335+
* them. (This is something else that could perhaps be cleaner if we did
1336+
* extParam/allParam processing in setrefs.c instead of here? See notes
1337+
* for materialize_finished_plan.)
13341338
*/
1335-
root->query_level++;
13361339
saved_init_plans=root->init_plans;
13371340
root->init_plans=NIL;
13381341

@@ -1341,8 +1344,7 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
13411344
*/
13421345
SS_finalize_plan(root,plan);
13431346

1344-
/* Return to outer subquery context */
1345-
root->query_level--;
1347+
/* Restore outer initplan list */
13461348
root->init_plans=saved_init_plans;
13471349

13481350
/*

‎src/test/regress/expected/subselect.out

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,25 @@ select f1, ss1 as relabel from
386386
-2147483647 | 0
387387
(5 rows)
388388

389+
--
390+
-- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
391+
-- Per bug report from David Sanchez i Gregori.
392+
--
393+
select * from (
394+
select max(unique1) from tenk1 as a
395+
where exists (select 1 from tenk1 as b where b.thousand = a.unique2)
396+
) ss;
397+
max
398+
------
399+
9997
400+
(1 row)
401+
402+
select * from (
403+
select min(unique1) from tenk1 as a
404+
where not exists (select 1 from tenk1 as b where b.unique2 = 10000)
405+
) ss;
406+
min
407+
-----
408+
0
409+
(1 row)
410+

‎src/test/regress/sql/subselect.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,18 @@ select * from shipped_view;
236236
select f1, ss1as relabelfrom
237237
(select*, (selectsum(f1)from int4_tbl bwhere f1>=a.f1)as ss1
238238
from int4_tbl a) ss;
239+
240+
--
241+
-- Test cases involving PARAM_EXEC parameters and min/max index optimizations.
242+
-- Per bug report from David Sanchez i Gregori.
243+
--
244+
245+
select*from (
246+
selectmax(unique1)from tenk1as a
247+
where exists (select1from tenk1as bwhereb.thousand=a.unique2)
248+
) ss;
249+
250+
select*from (
251+
selectmin(unique1)from tenk1as a
252+
where not exists (select1from tenk1as bwhereb.unique2=10000)
253+
) ss;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp