Skip to content

Commit 78883cd

Browse files
committed
Avoid mislabeling of lateral references, redux.
As I'd feared, commit 5c9d863 was still a few bricks shy of a load. We can't just leave pulled-up lateral-reference Vars with no new nullingrels: we have to carefully compute what subset of the to-be-replaced Var's nullingrels apply to them, else we still get "wrong varnullingrels" errors. This is a bit tedious, but it looks like we can use the nullingrel data this patch computes for other purposes, enabling better optimization. We don't want to inject unnecessary plan changes into stable branches though, so leave that idea for a later HEAD-only patch. Patch by me, but thanks to Richard Guo for devising a test case that broke 5c9d863, and for preliminary investigation about how to fix it. As before, back-patch to v16. Discussion: https://postgr.es/m/E1tGn4j-0003zi-MP@gemulon.postgresql.org
1 parent 72822a9 commit 78883cd

File tree

4 files changed

+233
-5
lines changed

4 files changed

+233
-5
lines changed

src/backend/optimizer/prep/prepjointree.c

+166-5
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,26 @@
4242
#include "rewrite/rewriteManip.h"
4343

4444

45+
typedef struct nullingrel_info
46+
{
47+
/*
48+
* For each leaf RTE, nullingrels[rti] is the set of relids of outer joins
49+
* that potentially null that RTE.
50+
*/
51+
Relids *nullingrels;
52+
/* Length of range table (maximum index in nullingrels[]) */
53+
int rtlength; /* used only for assertion checks */
54+
} nullingrel_info;
55+
4556
typedef struct pullup_replace_vars_context
4657
{
4758
PlannerInfo *root;
4859
List *targetlist; /* tlist of subquery being pulled up */
4960
RangeTblEntry *target_rte; /* RTE of subquery */
5061
Relids relids; /* relids within subquery, as numbered after
5162
* pullup (set only if target_rte->lateral) */
63+
nullingrel_info *nullinfo; /* per-RTE nullingrel info (set only if
64+
* target_rte->lateral) */
5265
bool *outer_hasSubLinks; /* -> outer query's hasSubLinks */
5366
int varno; /* varno of subquery */
5467
bool wrap_non_vars; /* do we need all non-Var outputs to be PHVs? */
@@ -142,6 +155,9 @@ static void substitute_phv_relids(Node *node,
142155
static void fix_append_rel_relids(PlannerInfo *root, int varno,
143156
Relids subrelids);
144157
static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
158+
static nullingrel_info *get_nullingrels(Query *parse);
159+
static void get_nullingrels_recurse(Node *jtnode, Relids upper_nullingrels,
160+
nullingrel_info *info);
145161

146162

147163
/*
@@ -1259,10 +1275,16 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
12591275
rvcontext.targetlist = subquery->targetList;
12601276
rvcontext.target_rte = rte;
12611277
if (rte->lateral)
1278+
{
12621279
rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
12631280
true, true);
1264-
else /* won't need relids */
1281+
rvcontext.nullinfo = get_nullingrels(parse);
1282+
}
1283+
else /* won't need these values */
1284+
{
12651285
rvcontext.relids = NULL;
1286+
rvcontext.nullinfo = NULL;
1287+
}
12661288
rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
12671289
rvcontext.varno = varno;
12681290
/* this flag will be set below, if needed */
@@ -1724,6 +1746,9 @@ is_simple_subquery(PlannerInfo *root, Query *subquery, RangeTblEntry *rte,
17241746
* such refs to be wrapped in PlaceHolderVars, even when they're below
17251747
* the nearest outer join? But it's a pretty hokey usage, so not
17261748
* clear this is worth sweating over.)
1749+
*
1750+
* If you change this, see also the comments about lateral references
1751+
* in pullup_replace_vars_callback().
17271752
*/
17281753
if (lowest_outer_join != NULL)
17291754
{
@@ -1808,7 +1833,8 @@ pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
18081833
rvcontext.root = root;
18091834
rvcontext.targetlist = tlist;
18101835
rvcontext.target_rte = rte;
1811-
rvcontext.relids = NULL;
1836+
rvcontext.relids = NULL; /* can't be any lateral references here */
1837+
rvcontext.nullinfo = NULL;
18121838
rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
18131839
rvcontext.varno = varno;
18141840
rvcontext.wrap_non_vars = false;
@@ -1970,9 +1996,10 @@ pull_up_constant_function(PlannerInfo *root, Node *jtnode,
19701996
/*
19711997
* Since this function was reduced to a Const, it doesn't contain any
19721998
* lateral references, even if it's marked as LATERAL. This means we
1973-
* don't need to fill relids.
1999+
* don't need to fill relids or nullinfo.
19742000
*/
19752001
rvcontext.relids = NULL;
2002+
rvcontext.nullinfo = NULL;
19762003

19772004
rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
19782005
rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex;
@@ -2681,9 +2708,52 @@ pullup_replace_vars_callback(Var *var,
26812708
{
26822709
/*
26832710
* There should be Vars/PHVs within the expression that we can
2684-
* modify. Per above discussion, modify only Vars/PHVs of the
2685-
* subquery, not lateral references.
2711+
* modify. Vars/PHVs of the subquery should have the full
2712+
* var->varnullingrels added to them, but if there are lateral
2713+
* references within the expression, those must be marked with
2714+
* only the nullingrels that potentially apply to them. (This
2715+
* corresponds to the fact that the expression will now be
2716+
* evaluated at the join level of the Var that we are replacing:
2717+
* the lateral references may have bubbled up through fewer outer
2718+
* joins than the subquery's Vars have. Per the discussion above,
2719+
* we'll still get the right answers.) That relid set could be
2720+
* different for different lateral relations, so we have to do
2721+
* this work for each one.
2722+
*
2723+
* (Currently, the restrictions in is_simple_subquery() mean that
2724+
* at most we have to remove the lowest outer join's relid from
2725+
* the nullingrels of a lateral reference. However, we might
2726+
* relax those restrictions someday, so let's do this right.)
26862727
*/
2728+
if (rcon->target_rte->lateral)
2729+
{
2730+
nullingrel_info *nullinfo = rcon->nullinfo;
2731+
Relids lvarnos;
2732+
int lvarno;
2733+
2734+
/*
2735+
* Identify lateral varnos used within newnode. We must do
2736+
* this before injecting var->varnullingrels into the tree.
2737+
*/
2738+
lvarnos = pull_varnos(rcon->root, newnode);
2739+
lvarnos = bms_del_members(lvarnos, rcon->relids);
2740+
/* For each one, add relevant nullingrels if any */
2741+
lvarno = -1;
2742+
while ((lvarno = bms_next_member(lvarnos, lvarno)) >= 0)
2743+
{
2744+
Relids lnullingrels;
2745+
2746+
Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
2747+
lnullingrels = bms_intersect(var->varnullingrels,
2748+
nullinfo->nullingrels[lvarno]);
2749+
if (!bms_is_empty(lnullingrels))
2750+
newnode = add_nulling_relids(newnode,
2751+
bms_make_singleton(lvarno),
2752+
lnullingrels);
2753+
}
2754+
}
2755+
2756+
/* Finally, deal with Vars/PHVs of the subquery itself */
26872757
newnode = add_nulling_relids(newnode,
26882758
rcon->relids,
26892759
var->varnullingrels);
@@ -4121,3 +4191,94 @@ find_jointree_node_for_rel(Node *jtnode, int relid)
41214191
(int) nodeTag(jtnode));
41224192
return NULL;
41234193
}
4194+
4195+
/*
4196+
* get_nullingrels: collect info about which outer joins null which relations
4197+
*
4198+
* The result struct contains, for each leaf relation used in the query,
4199+
* the set of relids of outer joins that potentially null that rel.
4200+
*/
4201+
static nullingrel_info *
4202+
get_nullingrels(Query *parse)
4203+
{
4204+
nullingrel_info *result = palloc_object(nullingrel_info);
4205+
4206+
result->rtlength = list_length(parse->rtable);
4207+
result->nullingrels = palloc0_array(Relids, result->rtlength + 1);
4208+
get_nullingrels_recurse((Node *) parse->jointree, NULL, result);
4209+
return result;
4210+
}
4211+
4212+
/*
4213+
* Recursive guts of get_nullingrels().
4214+
*
4215+
* Note: at any recursion level, the passed-down upper_nullingrels must be
4216+
* treated as a constant, but it can be stored directly into *info
4217+
* if we're at leaf level. Upper recursion levels do not free their mutated
4218+
* copies of the nullingrels, because those are probably referenced by
4219+
* at least one leaf rel.
4220+
*/
4221+
static void
4222+
get_nullingrels_recurse(Node *jtnode, Relids upper_nullingrels,
4223+
nullingrel_info *info)
4224+
{
4225+
if (jtnode == NULL)
4226+
return;
4227+
if (IsA(jtnode, RangeTblRef))
4228+
{
4229+
int varno = ((RangeTblRef *) jtnode)->rtindex;
4230+
4231+
Assert(varno > 0 && varno <= info->rtlength);
4232+
info->nullingrels[varno] = upper_nullingrels;
4233+
}
4234+
else if (IsA(jtnode, FromExpr))
4235+
{
4236+
FromExpr *f = (FromExpr *) jtnode;
4237+
ListCell *l;
4238+
4239+
foreach(l, f->fromlist)
4240+
{
4241+
get_nullingrels_recurse(lfirst(l), upper_nullingrels, info);
4242+
}
4243+
}
4244+
else if (IsA(jtnode, JoinExpr))
4245+
{
4246+
JoinExpr *j = (JoinExpr *) jtnode;
4247+
Relids local_nullingrels;
4248+
4249+
switch (j->jointype)
4250+
{
4251+
case JOIN_INNER:
4252+
get_nullingrels_recurse(j->larg, upper_nullingrels, info);
4253+
get_nullingrels_recurse(j->rarg, upper_nullingrels, info);
4254+
break;
4255+
case JOIN_LEFT:
4256+
case JOIN_SEMI:
4257+
case JOIN_ANTI:
4258+
local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
4259+
j->rtindex);
4260+
get_nullingrels_recurse(j->larg, upper_nullingrels, info);
4261+
get_nullingrels_recurse(j->rarg, local_nullingrels, info);
4262+
break;
4263+
case JOIN_FULL:
4264+
local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
4265+
j->rtindex);
4266+
get_nullingrels_recurse(j->larg, local_nullingrels, info);
4267+
get_nullingrels_recurse(j->rarg, local_nullingrels, info);
4268+
break;
4269+
case JOIN_RIGHT:
4270+
local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
4271+
j->rtindex);
4272+
get_nullingrels_recurse(j->larg, local_nullingrels, info);
4273+
get_nullingrels_recurse(j->rarg, upper_nullingrels, info);
4274+
break;
4275+
default:
4276+
elog(ERROR, "unrecognized join type: %d",
4277+
(int) j->jointype);
4278+
break;
4279+
}
4280+
}
4281+
else
4282+
elog(ERROR, "unrecognized node type: %d",
4283+
(int) nodeTag(jtnode));
4284+
}

src/test/regress/expected/subselect.out

+51
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,57 @@ order by t1.ten;
17971797
9 | 18000
17981798
(10 rows)
17991799

1800+
explain (verbose, costs off)
1801+
select t1.q1, x from
1802+
int8_tbl t1 left join
1803+
(int8_tbl t2 left join
1804+
lateral (select t2.q1+t3.q1 as x, * from int8_tbl t3) t3 on t2.q2 = t3.q2)
1805+
on t1.q2 = t2.q2
1806+
order by 1, 2;
1807+
QUERY PLAN
1808+
--------------------------------------------------------
1809+
Sort
1810+
Output: t1.q1, ((t2.q1 + t3.q1))
1811+
Sort Key: t1.q1, ((t2.q1 + t3.q1))
1812+
-> Hash Left Join
1813+
Output: t1.q1, (t2.q1 + t3.q1)
1814+
Hash Cond: (t2.q2 = t3.q2)
1815+
-> Hash Left Join
1816+
Output: t1.q1, t2.q1, t2.q2
1817+
Hash Cond: (t1.q2 = t2.q2)
1818+
-> Seq Scan on public.int8_tbl t1
1819+
Output: t1.q1, t1.q2
1820+
-> Hash
1821+
Output: t2.q1, t2.q2
1822+
-> Seq Scan on public.int8_tbl t2
1823+
Output: t2.q1, t2.q2
1824+
-> Hash
1825+
Output: t3.q1, t3.q2
1826+
-> Seq Scan on public.int8_tbl t3
1827+
Output: t3.q1, t3.q2
1828+
(19 rows)
1829+
1830+
select t1.q1, x from
1831+
int8_tbl t1 left join
1832+
(int8_tbl t2 left join
1833+
lateral (select t2.q1+t3.q1 as x, * from int8_tbl t3) t3 on t2.q2 = t3.q2)
1834+
on t1.q2 = t2.q2
1835+
order by 1, 2;
1836+
q1 | x
1837+
------------------+------------------
1838+
123 | 246
1839+
123 | 246
1840+
123 | 4567890123456912
1841+
123 | 4567890123456912
1842+
123 | 9135780246913578
1843+
4567890123456789 | 246
1844+
4567890123456789 | 4567890123456912
1845+
4567890123456789 | 4567890123456912
1846+
4567890123456789 | 9135780246913578
1847+
4567890123456789 | 9135780246913578
1848+
4567890123456789 | 9135780246913578
1849+
(11 rows)
1850+
18001851
--
18011852
-- Tests for CTE inlining behavior
18021853
--

src/test/regress/sql/subselect.sql

+15
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,21 @@ select t1.ten, sum(x) from
924924
group by t1.ten
925925
order by t1.ten;
926926

927+
explain (verbose, costs off)
928+
select t1.q1, x from
929+
int8_tbl t1 left join
930+
(int8_tbl t2 left join
931+
lateral (select t2.q1+t3.q1 as x, * from int8_tbl t3) t3 on t2.q2 = t3.q2)
932+
on t1.q2 = t2.q2
933+
order by 1, 2;
934+
935+
select t1.q1, x from
936+
int8_tbl t1 left join
937+
(int8_tbl t2 left join
938+
lateral (select t2.q1+t3.q1 as x, * from int8_tbl t3) t3 on t2.q2 = t3.q2)
939+
on t1.q2 = t2.q2
940+
order by 1, 2;
941+
927942
--
928943
-- Tests for CTE inlining behavior
929944
--

src/tools/pgindent/typedefs.list

+1
Original file line numberDiff line numberDiff line change
@@ -3639,6 +3639,7 @@ nodeitem
36393639
normal_rand_fctx
36403640
nsphash_hash
36413641
ntile_context
3642+
nullingrel_info
36423643
numeric
36433644
object_access_hook_type
36443645
object_access_hook_type_str

0 commit comments

Comments
 (0)