| From: | Kacper Kuras <kacperkuras(at)hotmail(dot)com> |
|---|---|
| To: | Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com> |
| Cc: | PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Vaibhav Dalvi <vaibhav(dot)dalvi(at)enterprisedb(dot)com>, Marcos Pegoraro <marcos(at)f10(dot)com(dot)br>, Andreas Karlsson <andreas(at)proxel(dot)se>, Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>, Peter Eisentraut <peter(at)eisentraut(dot)org> |
| Subject: | Re: Proposal: INSERT ... BY NAME |
| Date: | 2026-12-05 18:16:56 |
| Message-ID: | VI0P193MB3111800BF8FA6374F1B66733BF492@VI0P193MB3111.EURP193.PROD.OUTLOOK.COM |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Ayush,
I see the entry is in Drafts now. I tested v2 anyway, in case it
helps with a v3: on master from December 4 the regression tests pass
and everything I tried works. One question and a few small things.
Andreas wrote [1]:
> INSERT INTO t (a,b) BY NAME SELECT 37 AS b, 42 AS a;
>
> is effectively the same thing as:
>
> INSERT INTO t (a,b) SELECT a, b FROM (SELECT 37 AS b, 42 AS a);
That example has the same columns in the list and in the source, so it
doesn't show what happens when they differ. With
CREATE TABLE t (a int, b int, c int DEFAULT 42), v2 does this:
INSERT INTO t (a, b) BY NAME SELECT 30 AS a;
inserts (30, NULL, 42): b is listed but gets its default
INSERT INTO t (a, b) BY NAME SELECT 1 AS a, 2 AS b, 3 AS c;
ERROR: source column "c" has no matching target column
Andreas, does the accepted proposal say what should happen in these
two cases?
If that can't be settled before this is committed, the safe choice is
an error in both: with a column list, every listed column has to come
from the source and every source column has to be in the list. v2
already does that for the second case. For the first, it is what
INSERT already does by position: a listed column the source doesn't
fill is an error ("INSERT has more target columns than expressions"),
and only without a list do the missing columns get their defaults.
An error can be relaxed later without breaking existing queries;
turning the first case from a default into an error later would break
them.
The grammar accepts BY before or after OVERRIDING, and the commit
message says so, but the synopsis in insert.sgml shows only BY first
and the text doesn't mention the other order.
Vaibhav's two comments on v2 (tab completion after BY NAME / BY
POSITION, and a test for BY NAME TABLE) still apply. On the
completion side, after "OVERRIDING SYSTEM VALUE" psql offers BY NAME
and BY POSITION even when BY was already written before OVERRIDING.
What I want BY NAME for is this, together with SELECT * EXCLUDE [2]:
a prepared statement that takes an array of the table's row type and
lets the table fill in the rest.
PREPARE ins(post[]) AS
INSERT INTO post BY NAME
SELECT * (EXCLUDE (id, created_at)) FROM unnest($1);
With both patches applied, it works.
[1] https://postgr.es/m/c5090d60-09b3-4ca3-97cc-ddb99d87a301@proxel.se
[2] https://www.postgresql.org/message-id/flat/CAMWA6yb9GGiJbe9iU88yjurVQCsFgZvW7xSxa0aFLC0Wtz4Aqg%40mail.gmail.com
Regards,
Kacper Kuras
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Raja Sai pranav | 2026-12-05 18:06:30 | Re: Incremental backup inconsistency after vacuum truncation + COPY regrowth |