multi-row INSERT broken with a sublink in a target column subscript

Started by Amit Langote27 days ago5 messageshackers
Jump to latest
#1Amit Langote
amitlangote09@gmail.com

When looking at whether there exist other sites in the parser that are
like bug #19786, Claude found $subject.

CREATE TABLE ta (a int[]);
INSERT INTO ta (a[(SELECT 1)]) VALUES (1), (2);
ERROR: unexpected non-SELECT command in SubLink

The problem is that transformInsertStmt() transforms the target
columns' indirection once per VALUES row without copying it, while
transformSubLink() modifies the raw SubLink in place.

a[(1 IS NULL)::int + 1] fails too, like this:

INSERT INTO ta (a[(1 IS NULL)::int + 1]) VALUES (1), (2);
ERROR: unrecognized node type: 7

Attached patch fixes this.

--
Thanks, Amit Langote

Attachments:

t254418_1
v1-0001-Fix-multi-row-INSERT-with-a-sublink-in-a-target-c.patchapplication/octet-stream; name=v1-0001-Fix-multi-row-INSERT-with-a-sublink-in-a-target-c.patchDownload+15-2
#2Chao Li
li.evan.chao@gmail.com
In reply to: Amit Langote (#1)
Re: multi-row INSERT broken with a sublink in a target column subscript

On Oct 28, 2026, at 08:42, Amit Langote <amitlangote09@gmail.com> wrote:

When looking at whether there exist other sites in the parser that are
like bug #19786, Claude found $subject.

CREATE TABLE ta (a int[]);
INSERT INTO ta (a[(SELECT 1)]) VALUES (1), (2);
ERROR: unexpected non-SELECT command in SubLink

The problem is that transformInsertStmt() transforms the target
columns' indirection once per VALUES row without copying it, while
transformSubLink() modifies the raw SubLink in place.

a[(1 IS NULL)::int + 1] fails too, like this:

INSERT INTO ta (a[(1 IS NULL)::int + 1]) VALUES (1), (2);
ERROR: unrecognized node type: 7

Attached patch fixes this.

--
Thanks, Amit Langote
<v1-0001-Fix-multi-row-INSERT-with-a-sublink-in-a-target-c.patch>

The fix looks correct to me. I just have one nit.

When strip_indirection is false, we're building the final target list, so it should be safe to use col->indirection directly, since it won't be transformed again. Instead of unconditionally calling copyObject(), maybe we can do: strip_indirection ? copyObject(col->indirection) : col->indirection

I tried this change locally, and the new test case still passes.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3shihao zhong
zhong950419@gmail.com
In reply to: Chao Li (#2)
Re: multi-row INSERT broken with a sublink in a target column subscript

Hi Amit,

The fix looks right to me. One small thing in the test. Both
subscripts evaluate to 1, so it only sets f2[1]. Maybe use
(1 IS NOT NULL)::int + 1 and add RETURNING f2, so the output
shows both elements:

insert into inserttest (f2[(select 1)], f2[(1 IS NOT NULL)::int + 1])
values (9,10), (11,12) returning f2;

Thanks,
Shihao

#4Amit Langote
amitlangote09@gmail.com
In reply to: shihao zhong (#3)
Re: multi-row INSERT broken with a sublink in a target column subscript

Hi,

On Wed, Nov 4, 2026 at 12:06 PM shihao zhong <zhong950419@gmail.com> wrote:

Hi Amit,

The fix looks right to me. One small thing in the test. Both
subscripts evaluate to 1, so it only sets f2[1]. Maybe use
(1 IS NOT NULL)::int + 1 and add RETURNING f2, so the output
shows both elements:

insert into inserttest (f2[(select 1)], f2[(1 IS NOT NULL)::int + 1])
values (9,10), (11,12) returning f2;

Ah, my bad I didn't write it like that to begin with. Done like that.

I decided not to go with with Chao's idea of using strip_indirection
to skip the copy in this patch in order to keep things simple. I am
not sure if being clever won't cause issues later. Thanks for the
review though.

I'll push the attached updated patch barring objections.

--
Thanks, Amit Langote

Attachments:

t254418_4
v2-0001-Fix-multi-row-INSERT-with-a-sublink-in-a-target-c.patchapplication/octet-stream; name=v2-0001-Fix-multi-row-INSERT-with-a-sublink-in-a-target-c.patchDownload+21-2
#5Amit Langote
amitlangote09@gmail.com
In reply to: Amit Langote (#4)
Re: multi-row INSERT broken with a sublink in a target column subscript

On Wed, Nov 4, 2026 at 4:24 PM Amit Langote <amitlangote09@gmail.com> wrote:

On Wed, Nov 4, 2026 at 12:06 PM shihao zhong <zhong950419@gmail.com> wrote:

Hi Amit,

The fix looks right to me. One small thing in the test. Both
subscripts evaluate to 1, so it only sets f2[1]. Maybe use
(1 IS NOT NULL)::int + 1 and add RETURNING f2, so the output
shows both elements:

insert into inserttest (f2[(select 1)], f2[(1 IS NOT NULL)::int + 1])
values (9,10), (11,12) returning f2;

Ah, my bad I didn't write it like that to begin with. Done like that.

I decided not to go with with Chao's idea of using strip_indirection
to skip the copy in this patch in order to keep things simple. I am
not sure if being clever won't cause issues later. Thanks for the
review though.

I'll push the attached updated patch barring objections.

FTR, this was pushed and back-patched. 667243c02d in master.

--
Thanks, Amit Langote