diff options
-rw-r--r-- | src/backend/optimizer/path/costsize.c | 18 | ||||
-rw-r--r-- | src/test/regress/expected/subselect.out | 19 | ||||
-rw-r--r-- | src/test/regress/sql/subselect.sql | 9 |
3 files changed, 44 insertions, 2 deletions
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 783dca8a4ac..94077e6a006 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -4570,10 +4570,24 @@ cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan) { QualCost sp_cost; - /* Figure any cost for evaluating the testexpr */ + /* + * Figure any cost for evaluating the testexpr. + * + * Usually, SubPlan nodes are built very early, before we have constructed + * any RelOptInfos for the parent query level, which means the parent root + * does not yet contain enough information to safely consult statistics. + * Therefore, we pass root as NULL here. cost_qual_eval() is already + * well-equipped to handle a NULL root. + * + * One exception is SubPlan nodes built for the initplans of MIN/MAX + * aggregates from indexes (cf. SS_make_initplan_from_plan). In this + * case, having a NULL root is safe because testexpr will be NULL. + * Besides, an initplan will by definition not consult anything from the + * parent plan. + */ cost_qual_eval(&sp_cost, make_ands_implicit((Expr *) subplan->testexpr), - root); + NULL); if (subplan->useHashTable) { diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out index 0563d0cd5a1..c16dff05bc1 100644 --- a/src/test/regress/expected/subselect.out +++ b/src/test/regress/expected/subselect.out @@ -980,6 +980,25 @@ select (select (a.*)::text) from view_a a; (1 row) -- +-- Test case for bug #19037: no relation entry for relid N +-- +explain (costs off) +select (1 = any(array_agg(f1))) = any (select false) from int4_tbl; + QUERY PLAN +---------------------------- + Aggregate + -> Seq Scan on int4_tbl + SubPlan 1 + -> Result +(4 rows) + +select (1 = any(array_agg(f1))) = any (select false) from int4_tbl; + ?column? +---------- + t +(1 row) + +-- -- Check that whole-row Vars reading the result of a subselect don't include -- any junk columns therein -- diff --git a/src/test/regress/sql/subselect.sql b/src/test/regress/sql/subselect.sql index a6d276a115b..8ccebbe51e0 100644 --- a/src/test/regress/sql/subselect.sql +++ b/src/test/regress/sql/subselect.sql @@ -480,6 +480,15 @@ select (select (select view_a)) from view_a; select (select (a.*)::text) from view_a a; -- +-- Test case for bug #19037: no relation entry for relid N +-- + +explain (costs off) +select (1 = any(array_agg(f1))) = any (select false) from int4_tbl; + +select (1 = any(array_agg(f1))) = any (select false) from int4_tbl; + +-- -- Check that whole-row Vars reading the result of a subselect don't include -- any junk columns therein -- |