Skip to content

Commit b100a52

Browse files
committed
Teach reparameterize_path() to handle AppendPaths.
If we're inside a lateral subquery, there may be no unparameterized paths for a particular child relation of an appendrel, in which case we *must* be able to create similarly-parameterized paths for each other child relation, else the planner will fail with "could not devise a query plan for the given query". This means that there are situations where we'd better be able to reparameterize at least one path for each child. This calls into question the assumption in reparameterize_path() that it can just punt if it feels like it. However, the only case that is known broken right now is where the child is itself an appendrel so that all its paths are AppendPaths. (I think possibly I disregarded that in the original coding on the theory that nested appendrels would get folded together --- but that only happens *after* reparameterize_path(), so it's not excused from handling a child AppendPath.) Given that this code's been like this since 9.3 when LATERAL was introduced, it seems likely we'd have heard of other cases by now if there were a larger problem. Per report from Elvis Pranskevichus. Back-patch to 9.3. Discussion: https://postgr.es/m/5981018.zdth1YWmNy@hammer.magicstack.net
1 parent b049ae3 commit b100a52

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/backend/optimizer/util/pathnode.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,28 @@ reparameterize_path(PlannerInfo *root, Path *path,
20752075
case T_SubqueryScan:
20762076
return create_subqueryscan_path(root, rel, path->pathkeys,
20772077
required_outer);
2078+
case T_Append:
2079+
{
2080+
AppendPath *apath = (AppendPath *) path;
2081+
List *childpaths = NIL;
2082+
ListCell *lc;
2083+
2084+
/* Reparameterize the children */
2085+
foreach(lc, apath->subpaths)
2086+
{
2087+
Path *spath = (Path *) lfirst(lc);
2088+
2089+
spath = reparameterize_path(root, spath,
2090+
required_outer,
2091+
loop_count);
2092+
if (spath == NULL)
2093+
return NULL;
2094+
childpaths = lappend(childpaths, spath);
2095+
}
2096+
return (Path *)
2097+
create_append_path(rel, childpaths,
2098+
required_outer);
2099+
}
20782100
default:
20792101
break;
20802102
}

src/test/regress/expected/join.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4880,6 +4880,25 @@ select * from
48804880
Output: 3
48814881
(11 rows)
48824882

4883+
-- check handling of nested appendrels inside LATERAL
4884+
select * from
4885+
((select 2 as v) union all (select 3 as v)) as q1
4886+
cross join lateral
4887+
((select * from
4888+
((select 4 as v) union all (select 5 as v)) as q3)
4889+
union all
4890+
(select q1.v)
4891+
) as q2;
4892+
v | v
4893+
---+---
4894+
2 | 4
4895+
2 | 5
4896+
2 | 2
4897+
3 | 4
4898+
3 | 5
4899+
3 | 3
4900+
(6 rows)
4901+
48834902
-- check we don't try to do a unique-ified semijoin with LATERAL
48844903
explain (verbose, costs off)
48854904
select * from

src/test/regress/sql/join.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,16 @@ select * from
15531553
select * from (select 3 as z) z where z.z = x.x
15541554
) zz on zz.z = y.y;
15551555

1556+
-- check handling of nested appendrels inside LATERAL
1557+
select * from
1558+
((select 2 as v) union all (select 3 as v)) as q1
1559+
cross join lateral
1560+
((select * from
1561+
((select 4 as v) union all (select 5 as v)) as q3)
1562+
union all
1563+
(select q1.v)
1564+
) as q2;
1565+
15561566
-- check we don't try to do a unique-ified semijoin with LATERAL
15571567
explain (verbose, costs off)
15581568
select * from

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy