postgres.git / summary / log / commit / refs

commit    9e77656a91f4df3f584329aba9fdfc5ca6704d28
Author:   Amit Langote <amitlan@postgresql.org>
Date:     Wed Nov 04 07:21:42 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Fix multi-row INSERT with a sublink in a target column subscript
    
    For INSERT ... VALUES with more than one row, transformInsertStmt()
    calls transformInsertRow() once for each VALUES row and once more for
    the final targetlist, and each call transformed the same raw
    indirection from the target column list.  That doesn't work for
    sublinks, because transformSubLink() replaces the raw subselect with
    its analyzed Query, so the second pass failed with "unexpected
    non-SELECT command in SubLink".  Other nodes that are transformed in
    place, such as NullTest, failed with "unrecognized node type".
    
    Fix by giving each transformation its own copy of the indirection.
    
    Claude found this issue when reviewing another bug.
    
    Discussion: https://postgr.es/m/CA+HiwqHCZLZV_20nz3fQX+1=vyFLDMbG0_3wY=QujNKZWts0nw@mail.gmail.com
    Reviewed-by: Chao Li <li.evan.chao@gmail.com>
    Reviewed-by: Shihao Zhong <zhong950419@gmail.com>
    Backpatch-through: 14


src/backend/parser/analyze.c | 8 +++++++- src/test/regress/expected/insert.out | 10 ++++++++++ src/test/regress/sql/insert.sql | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index fcb79c12fb5..8c28ec34f94 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -1011,11 +1011,17 @@ transformInsertRow(ParseState *pstate, List *exprlist, ResTarget *col = lfirst_node(ResTarget, icols); int attno = lfirst_int(attnos); + /* + * Pass a copy of the raw indirection, since transforming it may + * modify it in place (a SubLink in a subscript, for instance), and + * for a multi-row VALUES list we are called with the same target + * columns for each row and then once more. + */ expr = transformAssignedExpr(pstate, expr, EXPR_KIND_INSERT_TARGET, col->name, attno, - col->indirection, + copyObject(col->indirection), col->location); if (strip_indirection) diff --git a/src/test/regress/expected/insert.out b/src/test/regress/expected/insert.out index 4e90fe5686f..6632f0ee5b5 100644 --- a/src/test/regress/expected/insert.out +++ b/src/test/regress/expected/insert.out @@ -117,6 +117,16 @@ insert into inserttest (f2[1], f2[2]) values (1,default); -- not supported ERROR: cannot set an array element to DEFAULT LINE 1: insert into inserttest (f2[1], f2[2]) values (1,default); ^ +-- subscripts are transformed again for each VALUES row +begin; +insert into inserttest (f2[(select 1)], f2[(1 IS NOT NULL)::int + 1]) values (9,10), (11,12) RETURNING f2; + f2 +--------- + {9,10} + {11,12} +(2 rows) + +rollback; insert into inserttest (f3.if1, f3.if2) values (1,array['foo']); insert into inserttest (f3.if1, f3.if2) values (1,'{foo}'), (2,'{bar}'); insert into inserttest (f3.if1, f3.if2) select 3, '{baz,quux}'; diff --git a/src/test/regress/sql/insert.sql b/src/test/regress/sql/insert.sql index b40a1b99cc6..b5e232c7c0e 100644 --- a/src/test/regress/sql/insert.sql +++ b/src/test/regress/sql/insert.sql @@ -74,6 +74,10 @@ insert into inserttest (f2[1], f2[2]) values (1,2); insert into inserttest (f2[1], f2[2]) values (3,4), (5,6); insert into inserttest (f2[1], f2[2]) select 7,8; insert into inserttest (f2[1], f2[2]) values (1,default); -- not supported +-- subscripts are transformed again for each VALUES row +begin; +insert into inserttest (f2[(select 1)], f2[(1 IS NOT NULL)::int + 1]) values (9,10), (11,12) RETURNING f2; +rollback; insert into inserttest (f3.if1, f3.if2) values (1,array['foo']); insert into inserttest (f3.if1, f3.if2) values (1,'{foo}'), (2,'{bar}'); [parent: 83efe9602ecd]