pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedIdentity columns, DEFAULT keyword and multi-row inserts
6+ messages / 5 participants
[nested] [flat]
* Identity columns, DEFAULT keyword and multi-row inserts
@ 2019-12-10 07:41 Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 2 replies; 6+ messages in thread
From: Thomas Kellerer @ 2019-12-10 07:41 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hello,
assume the following table:
create table test
(
id integer not null generated always as identity,
data integer not null
);
The following insert works fine:
insert into test (id, data)
values (default,1);
However, a multi-row insert like the following:
insert into test (id, data)
values
(default,1),
(default,2);
fails with:
ERROR: cannot insert into column "id"
Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
Hint: Use OVERRIDING SYSTEM VALUE to override.
My question is:
* If DEFAULT is not allowed for identity columns, then why does the single-row insert work?
* If DEFAULT _is_ allowed, then why does the multi-row insert fail?
The above happens with Postgres 10,11 and 12
Regards
Thomas
^ permalink raw reply [nested|flat] 6+ messages in thread
* RE: Identity columns, DEFAULT keyword and multi-row inserts
@ 2019-12-10 07:56 Patrick FICHE <Patrick.Fiche@aqsacom.com>
parent: Thomas Kellerer <spam_eater@gmx.net>
1 sibling, 1 reply; 6+ messages in thread
From: Patrick FICHE @ 2019-12-10 07:56 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi Thomas,
I agree that it does not seem very consistent.
But is there any specific reason why are you using DEFAULT ?
Why don't you simply execute :
insert into test (data)
values
(1),
(2);
If you want / have to specify DEFAULT, then you should probably create your identity as "generated by default".
Regards,
Patrick Fiche
Database Engineer, Aqsacom Sas.
c. 33 6 82 80 69 96
-----Original Message-----
From: Thomas Kellerer <spam_eater@gmx.net>
Sent: Tuesday, December 10, 2019 8:42 AM
To: pgsql-general@lists.postgresql.org
Subject: Identity columns, DEFAULT keyword and multi-row inserts
Hello,
assume the following table:
create table test
(
id integer not null generated always as identity,
data integer not null
);
The following insert works fine:
insert into test (id, data)
values (default,1);
However, a multi-row insert like the following:
insert into test (id, data)
values
(default,1),
(default,2);
fails with:
ERROR: cannot insert into column "id"
Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
Hint: Use OVERRIDING SYSTEM VALUE to override.
My question is:
* If DEFAULT is not allowed for identity columns, then why does the single-row insert work?
* If DEFAULT _is_ allowed, then why does the multi-row insert fail?
The above happens with Postgres 10,11 and 12
Regards
Thomas
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Identity columns, DEFAULT keyword and multi-row inserts
@ 2019-12-10 08:15 Thomas Kellerer <spam_eater@gmx.net>
parent: Patrick FICHE <Patrick.Fiche@aqsacom.com>
0 siblings, 1 reply; 6+ messages in thread
From: Thomas Kellerer @ 2019-12-10 08:15 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Patrick FICHE schrieb am 10.12.2019 um 08:56:
>> -----Original Message-----
>> From: Thomas Kellerer <spam_eater@gmx.net>
>>
>> assume the following table:
>>
>> create table test
>> (
>> id integer not null generated always as identity,
>> data integer not null
>> );
>>
>> However, a multi-row insert like the following:
>>
>> insert into test (id, data)
>> values
>> (default,1),
>> (default,2);
>>
>> fails with:
>>
>> ERROR: cannot insert into column "id"
>> Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
>> Hint: Use OVERRIDING SYSTEM VALUE to override.
>>
>>
>> My question is:
>>
>> * If DEFAULT is not allowed for identity columns, then why does the single-row insert work?
>> * If DEFAULT _is_ allowed, then why does the multi-row insert fail?
>
>
> I agree that it does not seem very consistent.
>
> But is there any specific reason why are you using DEFAULT ?
>
> If you want / have to specify DEFAULT, then you should probably
> create your identity as "generated by default".
I don't really need (or use) it, I just stumbled upon this: https://stackoverflow.com/questions/59261048
And I think if the single row insert is allowed the multi-row should be as well.
Not sure if this is a bug - and if it is, which one is the bug: the failing statement or the working one?
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Identity columns, DEFAULT keyword and multi-row inserts
@ 2019-12-10 16:08 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 0 replies; 6+ messages in thread
From: Adrian Klaver @ 2019-12-10 16:08 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; pgsql-general@lists.postgresql.org
On 12/10/19 12:15 AM, Thomas Kellerer wrote:
> Patrick FICHE schrieb am 10.12.2019 um 08:56:
>>> -----Original Message-----
>>> From: Thomas Kellerer <spam_eater@gmx.net>
>>>
>>> assume the following table:
>>>
>>> create table test
>>> (
>>> id integer not null generated always as identity,
>>> data integer not null
>>> );
>>>
>>> However, a multi-row insert like the following:
>>>
>>> insert into test (id, data)
>>> values
>>> (default,1),
>>> (default,2);
>>>
>>> fails with:
>>>
>>> ERROR: cannot insert into column "id"
>>> Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
>>> Hint: Use OVERRIDING SYSTEM VALUE to override.
>>>
>>>
>>> My question is:
>>>
>>> * If DEFAULT is not allowed for identity columns, then why does the single-row insert work?
>>> * If DEFAULT _is_ allowed, then why does the multi-row insert fail?
>>
>>
>> I agree that it does not seem very consistent.
>>
>> But is there any specific reason why are you using DEFAULT ?
>>
>> If you want / have to specify DEFAULT, then you should probably
>> create your identity as "generated by default".
> I don't really need (or use) it, I just stumbled upon this: https://stackoverflow.com/questions/59261048
>
> And I think if the single row insert is allowed the multi-row should be as well.
>
> Not sure if this is a bug - and if it is, which one is the bug: the failing statement or the working one?
I would say the failing one:
https://www.postgresql.org/docs/12/sql-insert.html
"OVERRIDING SYSTEM VALUE
Without this clause, it is an error to specify an explicit value
(other than DEFAULT) for an identity column defined as GENERATED ALWAYS.
This clause overrides that restriction.
"
>
>
>
>
>
>
>
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Identity columns, DEFAULT keyword and multi-row inserts
@ 2019-12-10 16:53 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas Kellerer <spam_eater@gmx.net>
1 sibling, 1 reply; 6+ messages in thread
From: Tom Lane @ 2019-12-10 16:53 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-general@lists.postgresql.org; Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Thomas Kellerer <spam_eater@gmx.net> writes:
> assume the following table:
> create table test
> (
> id integer not null generated always as identity,
> data integer not null
> );
> The following insert works fine:
> insert into test (id, data)
> values (default,1);
> However, a multi-row insert like the following:
> insert into test (id, data)
> values
> (default,1),
> (default,2);
> fails with:
> ERROR: cannot insert into column "id"
> Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
> Hint: Use OVERRIDING SYSTEM VALUE to override.
Yeah, in principle a multi-row INSERT could grovel through all the
rows of the VALUES clause and confirm that every one of them has
DEFAULT there. Not sure it's worth the trouble, but if it's not
a lot of code then maybe. It certainly seems a bit inconsistent.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Identity columns, DEFAULT keyword and multi-row inserts
@ 2019-12-11 15:24 Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 6+ messages in thread
From: Peter Eisentraut @ 2019-12-11 15:24 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-general@lists.postgresql.org
On 2019-12-10 17:53, Tom Lane wrote:
>> However, a multi-row insert like the following:
>> insert into test (id, data)
>> values
>> (default,1),
>> (default,2);
>> fails with:
>> ERROR: cannot insert into column "id"
>> Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
>> Hint: Use OVERRIDING SYSTEM VALUE to override.
> Yeah, in principle a multi-row INSERT could grovel through all the
> rows of the VALUES clause and confirm that every one of them has
> DEFAULT there. Not sure it's worth the trouble, but if it's not
> a lot of code then maybe. It certainly seems a bit inconsistent.
It looks like the multi-row case in transformInsertStmt() would have to
develop a bit more smarts to discover this case and then replace the RTE
reference in the target list with a single SetToDefault node?
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2019-12-11 15:24 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-12-10 07:41 Identity columns, DEFAULT keyword and multi-row inserts Thomas Kellerer <spam_eater@gmx.net>
2019-12-10 07:56 ` Patrick FICHE <Patrick.Fiche@aqsacom.com>
2019-12-10 08:15 ` Thomas Kellerer <spam_eater@gmx.net>
2019-12-10 16:08 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-12-10 16:53 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-12-11 15:24 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox