Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 767c598

Browse files
committed
Work around implementation restriction in adjust_appendrel_attrs.
adjust_appendrel_attrs can't transfer nullingrel labeling to a non-Var translation expression (mainly because it's too late to wrap such an expression in a PlaceHolderVar). I'd supposed in commit 2489d76 that that restriction was unreachable because we'd not attempt to push problematic clauses down to an appendrel child relation. I forgot that set_append_rel_size blindly converts all the parent rel's joininfo clauses to child clauses, and that list could well contain clauses from above a nulling outer join. We might eventually have to devise a direct fix for this implementation restriction, but for now it seems enough to filter out troublesome clauses while constructing the child's joininfo list. Such clauses are certainly not useful while constructing paths for the child rel; they'll have to be applied later when we join the completed appendrel to something else. So we don't need them here, and omitting them from the list should save a few cycles while processing the child rel. Per bug #17832 from Marko Tiikkaja. Discussion: https://postgr.es/m/17832-d0a8106cdf1b722e@postgresql.org
1 parent 872e3d1 commit 767c598

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/backend/optimizer/path/allpaths.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,10 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
979979
int childRTindex;
980980
RangeTblEntry *childRTE;
981981
RelOptInfo *childrel;
982+
List *childrinfos;
982983
ListCell *parentvars;
983984
ListCell *childvars;
985+
ListCell *lc;
984986

985987
/* append_rel_list contains all append rels; ignore others */
986988
if (appinfo->parent_relid != parentRTindex)
@@ -1021,17 +1023,35 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
10211023
* Constraint exclusion failed, so copy the parent's join quals and
10221024
* targetlist to the child, with appropriate variable substitutions.
10231025
*
1026+
* We skip join quals that came from above outer joins that can null
1027+
* this rel, since they would be of no value while generating paths
1028+
* for the child. This saves some effort while processing the child
1029+
* rel, and it also avoids an implementation restriction in
1030+
* adjust_appendrel_attrs (it can't apply nullingrels to a non-Var).
1031+
*/
1032+
childrinfos = NIL;
1033+
foreach(lc, rel->joininfo)
1034+
{
1035+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1036+
1037+
if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids))
1038+
childrinfos = lappend(childrinfos,
1039+
adjust_appendrel_attrs(root,
1040+
(Node *) rinfo,
1041+
1, &appinfo));
1042+
}
1043+
childrel->joininfo = childrinfos;
1044+
1045+
/*
1046+
* Now for the child's targetlist.
1047+
*
10241048
* NB: the resulting childrel->reltarget->exprs may contain arbitrary
10251049
* expressions, which otherwise would not occur in a rel's targetlist.
10261050
* Code that might be looking at an appendrel child must cope with
10271051
* such. (Normally, a rel's targetlist would only include Vars and
10281052
* PlaceHolderVars.) XXX we do not bother to update the cost or width
10291053
* fields of childrel->reltarget; not clear if that would be useful.
10301054
*/
1031-
childrel->joininfo = (List *)
1032-
adjust_appendrel_attrs(root,
1033-
(Node *) rel->joininfo,
1034-
1, &appinfo);
10351055
childrel->reltarget->exprs = (List *)
10361056
adjust_appendrel_attrs(root,
10371057
(Node *) rel->reltarget->exprs,

src/test/regress/expected/join.out

+22
Original file line numberDiff line numberDiff line change
@@ -3613,6 +3613,28 @@ from int4_tbl t1
36133613
One-Time Filter: false
36143614
(3 rows)
36153615

3616+
-- Test handling of qual pushdown to appendrel members with non-Var outputs
3617+
explain (verbose, costs off)
3618+
select * from int4_tbl left join (
3619+
select text 'foo' union all select text 'bar'
3620+
) ss(x) on true
3621+
where ss.x is null;
3622+
QUERY PLAN
3623+
-----------------------------------------
3624+
Nested Loop Left Join
3625+
Output: int4_tbl.f1, ('foo'::text)
3626+
Filter: (('foo'::text) IS NULL)
3627+
-> Seq Scan on public.int4_tbl
3628+
Output: int4_tbl.f1
3629+
-> Materialize
3630+
Output: ('foo'::text)
3631+
-> Append
3632+
-> Result
3633+
Output: 'foo'::text
3634+
-> Result
3635+
Output: 'bar'::text
3636+
(12 rows)
3637+
36163638
--
36173639
-- test inlining of immutable functions
36183640
--

src/test/regress/sql/join.sql

+7
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,13 @@ from int4_tbl t1
11891189
left join information_schema.column_udt_usage on null)
11901190
on null;
11911191

1192+
-- Test handling of qual pushdown to appendrel members with non-Var outputs
1193+
explain (verbose, costs off)
1194+
select * from int4_tbl left join (
1195+
select text 'foo' union all select text 'bar'
1196+
) ss(x) on true
1197+
where ss.x is null;
1198+
11921199
--
11931200
-- test inlining of immutable functions
11941201
--

0 commit comments

Comments
 (0)