postgres.git / summary / log / commit / refs
commit d719e2ecb1886d8eae15bb46384751db783719a8
Author: Richard Guo <rguo@postgresql.org>
Date: Tue Sep 16 09:42:20 2025 +0000
First released in: 16.11, 17.7, 18.0
Treat JsonConstructorExpr as non-strict
JsonConstructorExpr can produce non-NULL output with a NULL input, so
it should be treated as a non-strict construct. Failing to do so can
lead to incorrect query behavior.
For example, in the reported case, when pulling up a subquery that is
under an outer join, if the subquery's target list contains a
JsonConstructorExpr that uses subquery variables and it is mistakenly
treated as strict, it will be pulled up without being wrapped in a
PlaceHolderVar. As a result, the expression will be evaluated at the
wrong place and will not be forced to null when the outer join should
do so.
Back-patch to v16 where JsonConstructorExpr was introduced.
Bug: #19046
Reported-by: Runyuan He <runyuan@berkeley.edu>
Author: Tender Wang <tndrwang@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/19046-765b6602b0a8cfdf@postgresql.org
Backpatch-through: 16
src/backend/optimizer/util/clauses.c | 2 ++
src/test/regress/expected/subselect.out | 28 ++++++++++++++++++++++++++++
src/test/regress/sql/subselect.sql | 17 +++++++++++++++++
3 files changed, 47 insertions(+)
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 2a599f84086..125966028aa 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -1111,6 +1111,8 @@ contain_nonstrict_functions_walker(Node *node, void *context)
return true;
if (IsA(node, BooleanTest))
return true;
+ if (IsA(node, JsonConstructorExpr))
+ return true;
/* Check other function-containing nodes */
if (check_functions_in_node(node, contain_nonstrict_functions_checker,
diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out
index e41d31b3084..ec20ef253fb 100644
--- a/src/test/regress/expected/subselect.out
+++ b/src/test/regress/expected/subselect.out
@@ -1741,6 +1741,34 @@ fetch backward all in c1;
commit;
--
+-- Check that JsonConstructorExpr is treated as non-strict, and thus can be
+-- wrapped in a PlaceHolderVar
+--
+begin;
+create temp table json_tab (a int);
+insert into json_tab values (1);
+explain (verbose, costs off)
+select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
+ QUERY PLAN
+---------------------------------------------------
+ Nested Loop Left Join
+ Output: t1.a, (JSON_ARRAY(1, a RETURNING json))
+ Join Filter: false
+ -> Seq Scan on pg_temp.json_tab t1
+ Output: t1.a
+ -> Result
+ Output: JSON_ARRAY(1, a RETURNING json)
+ One-Time Filter: false
+(8 rows)
+
+select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
+ a | json_array
+---+------------
+ 1 |
+(1 row)
+
+rollback;
+--
-- Verify that we correctly flatten cases involving a subquery output
-- expression that doesn't need to be wrapped in a PlaceHolderVar
--
diff --git a/src/test/regress/sql/subselect.sql b/src/test/regress/sql/subselect.sql
index fb0a901a516..583cbd62fa0 100644
--- a/src/test/regress/sql/subselect.sql
+++ b/src/test/regress/sql/subselect.sql
@@ -899,6 +899,23 @@ fetch backward all in c1;
commit;
+--
+-- Check that JsonConstructorExpr is treated as non-strict, and thus can be
+-- wrapped in a PlaceHolderVar
+--
+
+begin;
+
+create temp table json_tab (a int);
+insert into json_tab values (1);
+
+explain (verbose, costs off)
+select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
+
+select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
+
+rollback;
+
--
-- Verify that we correctly flatten cases involving a subquery output
-- expression that doesn't need to be wrapped in a PlaceHolderVar
[parent: 968141898777]