pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #14952: COPY fails to fill in IDENTITY column default value
14+ messages / 4 participants
[nested] [flat]
* BUG #14952: COPY fails to fill in IDENTITY column default value
@ 2017-12-07 11:29 steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 14+ messages in thread
From: steven.winfield@cantabcapital.com @ 2017-12-07 11:29 UTC (permalink / raw)
To: pgsql-bugs; +Cc: steven.winfield@cantabcapital.com
The following bug has been logged on the website:
Bug reference: 14952
Logged by: Steven Winfield
Email address: steven.winfield@cantabcapital.com
PostgreSQL version: 10.0
Operating system: Linux
Description:
COPYing data to a table with an IDENTITY column, where the column's value
isn't specified in the copied input, fails because COPY attempts to insert a
NULL value for the column:
test=# CREATE TABLE identity_test (id bigint GENERATED ALWAYS AS IDENTITY,
name text);
test=# COPY identity_test (name) FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> foo
>> \.
ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, foo).
CONTEXT: COPY identity_test, line 1: "foo"
Compare this with a serial column:
test=# CREATE TABLE serial_test (id bigserial, name text);
test=# COPY serial_test (name) FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> foo
>> \.
COPY 1
test=# \d identity_test
Table "public.identity_test"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+------------------------------
id | bigint | | not null | generated always as identity
name | text | | |
test=# \d serial_test
Table "public.serial_test"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+-----------------------------------------
id | bigint | | not null |
nextval('serial_test_id_seq'::regclass)
name | text | | |
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
@ 2017-12-07 17:13 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 14+ messages in thread
From: Peter Eisentraut @ 2017-12-07 17:13 UTC (permalink / raw)
To: steven.winfield@cantabcapital.com; pgsql-bugs
On 12/7/17 06:29, steven.winfield@cantabcapital.com wrote:
> COPYing data to a table with an IDENTITY column, where the column's value
> isn't specified in the copied input, fails because COPY attempts to insert a
> NULL value for the column:
That indeed appears to be a bug.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2017-12-08 01:37 ` Michael Paquier <michael.paquier@gmail.com>
2017-12-08 14:44 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 14+ messages in thread
From: Michael Paquier @ 2017-12-08 01:37 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-bugs
On Fri, Dec 8, 2017 at 2:13 AM, Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:
> On 12/7/17 06:29, steven.winfield@cantabcapital.com wrote:
>> COPYing data to a table with an IDENTITY column, where the column's value
>> isn't specified in the copied input, fails because COPY attempts to insert a
>> NULL value for the column:
>
> That indeed appears to be a bug.
That's a bug. When doing a COPY with or without a list of columns, and
that a column is not listed and has a default expression, then this
expression is used. This is a role filled in by build_column_default()
but identity columns need to create a NextValueExpr expression
instead. As this expression is missing, the backend complains about a
NULL input for this column, which is logic without an expression.
Attached is a patch with a regression test.
Peter, what do you think?
Thanks,
--
Michael
Attachments:
copy-identity-fix.patch (application/octet-stream, 2.3K) download
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index bace390470..254be28ae4 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -23,6 +23,7 @@
#include "access/sysattr.h"
#include "access/xact.h"
#include "access/xlog.h"
+#include "catalog/dependency.h"
#include "catalog/pg_type.h"
#include "commands/copy.h"
#include "commands/defrem.h"
@@ -3067,8 +3068,19 @@ BeginCopyFrom(ParseState *pstate,
{
/* attribute is NOT to be copied from input */
/* use default value if one exists */
- Expr *defexpr = (Expr *) build_column_default(cstate->rel,
- attnum);
+ Expr *defexpr;
+
+ if (att->attidentity)
+ {
+ NextValueExpr *nve = makeNode(NextValueExpr);
+
+ nve->seqid = getOwnedSequence(RelationGetRelid(cstate->rel),
+ attnum);
+ nve->typeId = att->atttypid;
+ defexpr = (Expr *) nve;
+ }
+ else
+ defexpr = (Expr *) build_column_default(cstate->rel, attnum);
if (defexpr != NULL)
{
diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out
index 65e9c626b3..b4eb6e21b3 100644
--- a/src/test/regress/expected/copy2.out
+++ b/src/test/regress/expected/copy2.out
@@ -557,6 +557,21 @@ SELECT * FROM instead_of_insert_tbl;
1 | test1
(1 row)
+-- test with IDENTITY columns
+CREATE TABLE copy_tab_identity (a int GENERATED ALWAYS AS IDENTITY,
+ b text, c bigint);
+COPY copy_tab_identity FROM stdin;
+COPY copy_tab_identity (b, c) FROM stdin;
+SELECT * FROM copy_tab_identity;
+ a | b | c
+-----+------+-----
+ 100 | foo | 200
+ 101 | bar | 201
+ 1 | foo2 | 202
+ 2 | bar2 | 203
+(4 rows)
+
+DROP TABLE copy_tab_identity;
-- clean up
DROP TABLE forcetest;
DROP TABLE vistest;
diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql
index f3a6d228fa..9bee10b7fd 100644
--- a/src/test/regress/sql/copy2.sql
+++ b/src/test/regress/sql/copy2.sql
@@ -411,6 +411,22 @@ test1
SELECT * FROM instead_of_insert_tbl;
+-- test with IDENTITY columns
+CREATE TABLE copy_tab_identity (a int GENERATED ALWAYS AS IDENTITY,
+ b text, c bigint);
+COPY copy_tab_identity FROM stdin;
+100 foo 200
+101 bar 201
+\.
+
+COPY copy_tab_identity (b, c) FROM stdin;
+foo2 202
+bar2 203
+\.
+
+SELECT * FROM copy_tab_identity;
+
+DROP TABLE copy_tab_identity;
-- clean up
DROP TABLE forcetest;
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
@ 2017-12-08 14:44 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
1 sibling, 0 replies; 14+ messages in thread
From: Peter Eisentraut @ 2017-12-08 14:44 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs
On 12/7/17 20:37, Michael Paquier wrote:
> On Fri, Dec 8, 2017 at 2:13 AM, Peter Eisentraut
> <peter.eisentraut@2ndquadrant.com> wrote:
>> On 12/7/17 06:29, steven.winfield@cantabcapital.com wrote:
>>> COPYing data to a table with an IDENTITY column, where the column's value
>>> isn't specified in the copied input, fails because COPY attempts to insert a
>>> NULL value for the column:
>>
>> That indeed appears to be a bug.
>
> That's a bug. When doing a COPY with or without a list of columns, and
> that a column is not listed and has a default expression, then this
> expression is used. This is a role filled in by build_column_default()
> but identity columns need to create a NextValueExpr expression
> instead. As this expression is missing, the backend complains about a
> NULL input for this column, which is logic without an expression.
> Attached is a patch with a regression test.
>
> Peter, what do you think?
Committed. I moved the tests to identity.sql though.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
@ 2017-12-14 03:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-12-14 04:22 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
1 sibling, 2 replies; 14+ messages in thread
From: Tom Lane @ 2017-12-14 03:39 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-bugs
Michael Paquier <michael.paquier@gmail.com> writes:
> On Fri, Dec 8, 2017 at 2:13 AM, Peter Eisentraut
> <peter.eisentraut@2ndquadrant.com> wrote:
>> That indeed appears to be a bug.
> That's a bug. When doing a COPY with or without a list of columns, and
> that a column is not listed and has a default expression, then this
> expression is used. This is a role filled in by build_column_default()
> but identity columns need to create a NextValueExpr expression
> instead. As this expression is missing, the backend complains about a
> NULL input for this column, which is logic without an expression.
> Attached is a patch with a regression test.
Now that I look more closely at this patch, isn't it fixing things
in the wrong place? Why is it that COPY needs to know about this,
rather than build_column_default()? Aren't we going to have to
fix every other caller of build_column_default()?
For that matter, should build_column_default() know about it explicitly
either? Why aren't we implementing IDENTITY by storing an appropriate
default expression in the catalogs?
regards, tom lane
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-12-14 04:22 ` Michael Paquier <michael.paquier@gmail.com>
1 sibling, 0 replies; 14+ messages in thread
From: Michael Paquier @ 2017-12-14 04:22 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-bugs
On Thu, Dec 14, 2017 at 12:39 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> For that matter, should build_column_default() know about it explicitly
> either? Why aren't we implementing IDENTITY by storing an appropriate
> default expression in the catalogs?
Wait a minute... If I look at the callers of build_column_default(),
rewriteValuesRTE looks broken as well:
=# CREATE TABLE itest10 (a int GENERATED BY DEFAULT AS IDENTITY, b
text, c bigint);
CREATE TABLE
=# insert into itest10 values (default, 'foo', 0), (default, 'foo2', 1);
ERROR: 23502: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null, foo, 0).
I would expect the second query to work properly.
It seems that slot_fill_defaults() would fail as well for a logical
worker, and so are ATExecAlterColumnType() and ATExecAddColumn()?
Peter, did you do the separation to help in handling better the cases
with INSERT OVERRIDE?
--
Michael
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-12-15 17:37 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
1 sibling, 1 reply; 14+ messages in thread
From: Peter Eisentraut @ 2017-12-15 17:37 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs
On 12/13/17 22:39, Tom Lane wrote:
> Now that I look more closely at this patch, isn't it fixing things
> in the wrong place? Why is it that COPY needs to know about this,
> rather than build_column_default()? Aren't we going to have to
> fix every other caller of build_column_default()?
Yeah, that seems to be a problem. Attached is a patch that puts the
logic into build_column_default() and adds a few more tests covering
other omissions.
One problem is that this breaks the case ALTER TABLE ... ADD COLUMN ...
IDENTITY, because the sequence ownership isn't registered until after
the ALTER TABLE command. (This only worked for empty tables. For
pre-populated tables, it failed because of the above omission.) The
second patch tries to fix that. Needs more thought and documentation,
but it's an idea.
> For that matter, should build_column_default() know about it explicitly
> either? Why aren't we implementing IDENTITY by storing an appropriate
> default expression in the catalogs?
Initial versions were coded that way, but it was pretty messy and buggy
because you have to touch a lot of places to teach them the difference
between a real default expression and a synthetic one. Another problem
is that the NextValueExpr has special permission behavior that is not
really representable by a normal expression, thus introducing more
special cases, and possibly permission or security issues.
Obviously, it would have helped in the above case. But it was really messy.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Attachments: download all (2 files, .tar.gz)
0001-Apply-identity-column-expression-in-build_column_def.patch (text/plain, 7.2K) download
From c1e9c490012c25f0f05561bdad0d56041ac814f6 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Fri, 15 Dec 2017 11:57:09 -0500
Subject: [PATCH 1/2] Apply identity column expression in
build_column_default()
---
src/backend/commands/copy.c | 16 ++--------------
src/backend/rewrite/rewriteHandler.c | 22 +++++++++++-----------
src/test/regress/expected/identity.out | 30 ++++++++++++++++++++++++++++++
src/test/regress/sql/identity.sql | 19 +++++++++++++++++++
src/test/subscription/t/008_diff_schema.pl | 18 ++++++++++++++----
5 files changed, 76 insertions(+), 29 deletions(-)
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 254be28ae4..bace390470 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -23,7 +23,6 @@
#include "access/sysattr.h"
#include "access/xact.h"
#include "access/xlog.h"
-#include "catalog/dependency.h"
#include "catalog/pg_type.h"
#include "commands/copy.h"
#include "commands/defrem.h"
@@ -3068,19 +3067,8 @@ BeginCopyFrom(ParseState *pstate,
{
/* attribute is NOT to be copied from input */
/* use default value if one exists */
- Expr *defexpr;
-
- if (att->attidentity)
- {
- NextValueExpr *nve = makeNode(NextValueExpr);
-
- nve->seqid = getOwnedSequence(RelationGetRelid(cstate->rel),
- attnum);
- nve->typeId = att->atttypid;
- defexpr = (Expr *) nve;
- }
- else
- defexpr = (Expr *) build_column_default(cstate->rel, attnum);
+ Expr *defexpr = (Expr *) build_column_default(cstate->rel,
+ attnum);
if (defexpr != NULL)
{
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index e93552a8f3..93354ecb5b 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -844,17 +844,7 @@ rewriteTargetListIU(List *targetList,
{
Node *new_expr;
- if (att_tup->attidentity)
- {
- NextValueExpr *nve = makeNode(NextValueExpr);
-
- nve->seqid = getOwnedSequence(RelationGetRelid(target_relation), attrno);
- nve->typeId = att_tup->atttypid;
-
- new_expr = (Node *) nve;
- }
- else
- new_expr = build_column_default(target_relation, attrno);
+ new_expr = build_column_default(target_relation, attrno);
/*
* If there is no default (ie, default is effectively NULL), we
@@ -1123,6 +1113,16 @@ build_column_default(Relation rel, int attrno)
Node *expr = NULL;
Oid exprtype;
+ if (att_tup->attidentity)
+ {
+ NextValueExpr *nve = makeNode(NextValueExpr);
+
+ nve->seqid = getOwnedSequence(RelationGetRelid(rel), attrno);
+ nve->typeId = att_tup->atttypid;
+
+ return (Node *) nve;
+ }
+
/*
* Scan to see if relation has a default for this column.
*/
diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out
index 87ef0d3b2a..59b7eb91a8 100644
--- a/src/test/regress/expected/identity.out
+++ b/src/test/regress/expected/identity.out
@@ -104,6 +104,19 @@ SELECT * FROM itest4;
2 |
(2 rows)
+-- VALUES RTEs
+INSERT INTO itest3 VALUES (DEFAULT, 'a');
+INSERT INTO itest3 VALUES (DEFAULT, 'b'), (DEFAULT, 'c');
+SELECT * FROM itest3;
+ a | b
+----+---
+ 7 |
+ 12 |
+ 17 | a
+ 22 | b
+ 27 | c
+(5 rows)
+
-- OVERRIDING tests
INSERT INTO itest1 VALUES (10, 'xyz');
INSERT INTO itest1 OVERRIDING USER VALUE VALUES (10, 'xyz');
@@ -237,6 +250,23 @@ SELECT * FROM itestv11;
11 | xyz
(3 rows)
+-- ADD COLUMN
+CREATE TABLE itest13 (a int);
+-- add column to empty table
+ALTER TABLE itest13 ADD COLUMN b int GENERATED BY DEFAULT AS IDENTITY;
+ERROR: no owned sequence found
+INSERT INTO itest13 VALUES (1), (2), (3);
+-- add column to populated table
+ALTER TABLE itest13 ADD COLUMN c int GENERATED BY DEFAULT AS IDENTITY;
+ERROR: no owned sequence found
+SELECT * FROM itest13;
+ a
+---
+ 1
+ 2
+ 3
+(3 rows)
+
-- various ALTER COLUMN tests
-- fail, not allowed for identity columns
ALTER TABLE itest1 ALTER COLUMN a SET DEFAULT 1;
diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql
index 1b2d11cf34..8be086d7ea 100644
--- a/src/test/regress/sql/identity.sql
+++ b/src/test/regress/sql/identity.sql
@@ -54,6 +54,14 @@ CREATE TABLE itest_err_4 (a serial generated by default as identity);
SELECT * FROM itest4;
+-- VALUES RTEs
+
+INSERT INTO itest3 VALUES (DEFAULT, 'a');
+INSERT INTO itest3 VALUES (DEFAULT, 'b'), (DEFAULT, 'c');
+
+SELECT * FROM itest3;
+
+
-- OVERRIDING tests
INSERT INTO itest1 VALUES (10, 'xyz');
@@ -138,6 +146,17 @@ CREATE VIEW itestv11 AS SELECT * FROM itest11;
SELECT * FROM itestv11;
+-- ADD COLUMN
+
+CREATE TABLE itest13 (a int);
+-- add column to empty table
+ALTER TABLE itest13 ADD COLUMN b int GENERATED BY DEFAULT AS IDENTITY;
+INSERT INTO itest13 VALUES (1), (2), (3);
+-- add column to populated table
+ALTER TABLE itest13 ADD COLUMN c int GENERATED BY DEFAULT AS IDENTITY;
+SELECT * FROM itest13;
+
+
-- various ALTER COLUMN tests
-- fail, not allowed for identity columns
diff --git a/src/test/subscription/t/008_diff_schema.pl b/src/test/subscription/t/008_diff_schema.pl
index b71be6e487..cca4480f52 100644
--- a/src/test/subscription/t/008_diff_schema.pl
+++ b/src/test/subscription/t/008_diff_schema.pl
@@ -3,7 +3,7 @@
use warnings;
use PostgresNode;
use TestLib;
-use Test::More tests => 3;
+use Test::More tests => 4;
sub wait_for_caught_up
{
@@ -31,7 +31,7 @@ sub wait_for_caught_up
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
# Setup structure on subscriber
-$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab (a int primary key, b text, c timestamptz DEFAULT now(), d bigint DEFAULT 999)");
+$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab (a int primary key, b text, c timestamptz DEFAULT now(), d bigint DEFAULT 999, e int GENERATED BY DEFAULT AS IDENTITY)");
# Setup logical replication
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
@@ -61,8 +61,8 @@ sub wait_for_caught_up
wait_for_caught_up($node_publisher, $appname);
$result =
- $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c), count(d = 999) FROM test_tab");
-is($result, qq(2|2|2), 'check extra columns contain local defaults');
+ $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c), count(d = 999), count(e) FROM test_tab");
+is($result, qq(2|2|2|2), 'check extra columns contain local defaults after copy');
# Change the local values of the extra columns on the subscriber,
# update publisher, and check that subscriber retains the expected
@@ -76,5 +76,15 @@ sub wait_for_caught_up
$node_subscriber->safe_psql('postgres', "SELECT count(*), count(extract(epoch from c) = 987654321), count(d = 999) FROM test_tab");
is($result, qq(2|2|2), 'check extra columns contain locally changed data');
+# Another insert
+$node_publisher->safe_psql('postgres',
+ "INSERT INTO test_tab VALUES (3, 'baz')");
+
+wait_for_caught_up($node_publisher, $appname);
+
+$result =
+ $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c), count(d = 999), count(e) FROM test_tab");
+is($result, qq(3|3|3|3), 'check extra columns contain local defaults after apply');
+
$node_subscriber->stop;
$node_publisher->stop;
--
2.15.1
0002-Fix-ALTER-TABLE-ADD-COLUMN-IDENTITY.patch (text/plain, 5.4K) download
From 6ae72a02855baf8e70a0992bf6b778e88f461822 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Fri, 15 Dec 2017 12:16:12 -0500
Subject: [PATCH 2/2] Fix ALTER TABLE / ADD COLUMN / IDENTITY
The previous previous coding completely ignored that existing rows for
the new column should have values filled in from the identity sequence.
The previous coding using build_column_default() fails for this because
the sequence ownership isn't registered until after ALTER TABLE, and we
can't do it before because we don't have the column in the catalog yet.
So we specially remember in ColumnDef the sequence name that we decided
on and build a custom NextValueExpr using that.
---
src/backend/commands/tablecmds.c | 16 +++++++++++++++-
src/backend/nodes/copyfuncs.c | 1 +
src/backend/nodes/equalfuncs.c | 1 +
src/backend/nodes/outfuncs.c | 1 +
src/backend/parser/parse_utilcmd.c | 2 ++
src/include/nodes/parsenodes.h | 1 +
src/test/regress/expected/identity.out | 12 +++++-------
7 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index d979ce266d..e71723c0fa 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5419,7 +5419,21 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE
&& relkind != RELKIND_FOREIGN_TABLE && attribute.attnum > 0)
{
- defval = (Expr *) build_column_default(rel, attribute.attnum);
+ /*
+ * For an identity column, we can't use build_column_default(),
+ * because the sequence ownership isn't set yet. So do it manually.
+ */
+ if (colDef->identity)
+ {
+ NextValueExpr *nve = makeNode(NextValueExpr);
+
+ nve->seqid = RangeVarGetRelid(colDef->identitySequence, NoLock, false);
+ nve->typeId = typeOid;
+
+ defval = (Expr *) nve;
+ }
+ else
+ defval = (Expr *) build_column_default(rel, attribute.attnum);
if (!defval && DomainHasConstraints(typeOid))
{
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index b1515dd8e1..a7d6f62b71 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -2816,6 +2816,7 @@ _copyColumnDef(const ColumnDef *from)
COPY_NODE_FIELD(raw_default);
COPY_NODE_FIELD(cooked_default);
COPY_SCALAR_FIELD(identity);
+ COPY_NODE_FIELD(identitySequence);
COPY_NODE_FIELD(collClause);
COPY_SCALAR_FIELD(collOid);
COPY_NODE_FIELD(constraints);
diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
index 2e869a9d5d..1cc91890a8 100644
--- a/src/backend/nodes/equalfuncs.c
+++ b/src/backend/nodes/equalfuncs.c
@@ -2563,6 +2563,7 @@ _equalColumnDef(const ColumnDef *a, const ColumnDef *b)
COMPARE_NODE_FIELD(raw_default);
COMPARE_NODE_FIELD(cooked_default);
COMPARE_SCALAR_FIELD(identity);
+ COMPARE_NODE_FIELD(identitySequence);
COMPARE_NODE_FIELD(collClause);
COMPARE_SCALAR_FIELD(collOid);
COMPARE_NODE_FIELD(constraints);
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index b59a5219a7..9c760b4094 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -2809,6 +2809,7 @@ _outColumnDef(StringInfo str, const ColumnDef *node)
WRITE_NODE_FIELD(raw_default);
WRITE_NODE_FIELD(cooked_default);
WRITE_CHAR_FIELD(identity);
+ WRITE_NODE_FIELD(identitySequence);
WRITE_NODE_FIELD(collClause);
WRITE_OID_FIELD(collOid);
WRITE_NODE_FIELD(constraints);
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index f67379f8ed..a6cf722815 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -475,6 +475,8 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
cxt->blist = lappend(cxt->blist, seqstmt);
+ column->identitySequence = seqstmt->sequence;
+
/*
* Build an ALTER SEQUENCE ... OWNED BY command to mark the sequence as
* owned by this column, and add it to the list of things to be done after
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 2eaa6b2774..97b7f32e42 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -647,6 +647,7 @@ typedef struct ColumnDef
Node *raw_default; /* default value (untransformed parse tree) */
Node *cooked_default; /* default value (transformed expr tree) */
char identity; /* attidentity setting */
+ RangeVar *identitySequence;
CollateClause *collClause; /* untransformed COLLATE spec, if any */
Oid collOid; /* collation OID (InvalidOid if not set) */
List *constraints; /* other constraints on column */
diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out
index 59b7eb91a8..e054aac45f 100644
--- a/src/test/regress/expected/identity.out
+++ b/src/test/regress/expected/identity.out
@@ -254,17 +254,15 @@ SELECT * FROM itestv11;
CREATE TABLE itest13 (a int);
-- add column to empty table
ALTER TABLE itest13 ADD COLUMN b int GENERATED BY DEFAULT AS IDENTITY;
-ERROR: no owned sequence found
INSERT INTO itest13 VALUES (1), (2), (3);
-- add column to populated table
ALTER TABLE itest13 ADD COLUMN c int GENERATED BY DEFAULT AS IDENTITY;
-ERROR: no owned sequence found
SELECT * FROM itest13;
- a
----
- 1
- 2
- 3
+ a | b | c
+---+---+---
+ 1 | 1 | 1
+ 2 | 2 | 2
+ 3 | 3 | 3
(3 rows)
-- various ALTER COLUMN tests
--
2.15.1
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2017-12-18 05:49 ` Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 14+ messages in thread
From: Michael Paquier @ 2017-12-18 05:49 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs
On Sat, Dec 16, 2017 at 2:37 AM, Peter Eisentraut
<peter.eisentraut@2ndquadrant.com> wrote:
> On 12/13/17 22:39, Tom Lane wrote:
>> For that matter, should build_column_default() know about it explicitly
>> either? Why aren't we implementing IDENTITY by storing an appropriate
>> default expression in the catalogs?
>
> Initial versions were coded that way, but it was pretty messy and buggy
> because you have to touch a lot of places to teach them the difference
> between a real default expression and a synthetic one. Another problem
> is that the NextValueExpr has special permission behavior that is not
> really representable by a normal expression, thus introducing more
> special cases, and possibly permission or security issues.
I have no major objections about 0001. Here are some comments.
@@ -1123,6 +1113,16 @@ build_column_default(Relation rel, int attrno)
Node *expr = NULL;
Oid exprtype;
+ if (att_tup->attidentity)
+ {
I would add a comment explaining why this should come first.
+INSERT INTO itest3 VALUES (DEFAULT, 'a');
+INSERT INTO itest3 VALUES (DEFAULT, 'b'), (DEFAULT, 'c');
+SELECT * FROM itest3;
Thanks for adding a test with INSERT RTEs.
+-- ADD COLUMN
+CREATE TABLE itest13 (a int);
+-- add column to empty table
+ALTER TABLE itest13 ADD COLUMN b int GENERATED BY DEFAULT AS IDENTITY;
+ERROR: no owned sequence found
Those are hitting an elog(), so I think that it would be better to
issue a proper ereport() instead with ERRCODE_FEATURE_NOT_SUPPORTED as
errcode. It is easier to understand the point you are going to
though...
Now coming to 0002...
- defval = (Expr *) build_column_default(rel, attribute.attnum);
+ /*
+ * For an identity column, we can't use build_column_default(),
+ * because the sequence ownership isn't set yet. So do it manually.
+ */
+ if (colDef->identity)
+ {
+ NextValueExpr *nve = makeNode(NextValueExpr);
+
+ nve->seqid = RangeVarGetRelid(colDef->identitySequence,
NoLock, false);
+ nve->typeId = typeOid;
+
+ defval = (Expr *) nve;
+ }
+ else
+ defval = (Expr *) build_column_default(rel, attribute.attnum);
I have a problem with this approach which is basically something
similar to what I complained in the thread about typed and partition
tables for identity columns
(https://www.postgresql.org/message-id/20171023074458.1473.25799@wrigleys.postgresql.org).
In my opinion, this ALTER TABLE handling should be done by treating
identity columns a way similar to default expressions in
transformColumnDefinition(), by storing the FuncExpr node at parsing
time instead of storing the information needed to rebuild it when
executing the query. In short the mapping should get closer to what
default does with nextval or serial.
--
Michael
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
@ 2017-12-27 20:01 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-27 20:43 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 14+ messages in thread
From: Peter Eisentraut @ 2017-12-27 20:01 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs
On 12/18/17 00:49, Michael Paquier wrote:
> I have a problem with this approach which is basically something
> similar to what I complained in the thread about typed and partition
> tables for identity columns
> (https://www.postgresql.org/message-id/20171023074458.1473.25799@wrigleys.postgresql.org).
> In my opinion, this ALTER TABLE handling should be done by treating
> identity columns a way similar to default expressions in
> transformColumnDefinition(), by storing the FuncExpr node at parsing
> time instead of storing the information needed to rebuild it when
> executing the query. In short the mapping should get closer to what
> default does with nextval or serial.
The serial case works because it stores the sequence *name* in the
default value in the catalog. That doesn't work because for the
identity case we don't store the expression in the catalog. The
proposed patch works by storing the sequence *name* in the internal
structures so that it can be used in place of the stored default value.
So I think this approach is pretty consistent.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2017-12-27 20:43 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-12-30 23:26 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 14+ messages in thread
From: Tom Lane @ 2017-12-27 20:43 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: Michael Paquier <michael.paquier@gmail.com>; pgsql-bugs
Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> On 12/18/17 00:49, Michael Paquier wrote:
>> In my opinion, this ALTER TABLE handling should be done by treating
>> identity columns a way similar to default expressions in
>> transformColumnDefinition(), by storing the FuncExpr node at parsing
>> time instead of storing the information needed to rebuild it when
>> executing the query. In short the mapping should get closer to what
>> default does with nextval or serial.
> The serial case works because it stores the sequence *name* in the
> default value in the catalog.
Just to clarify: what's *actually* stored for a regular serial column
is the sequence OID (as a regclass constant), not the name. If it
were a name then ALTER SEQUENCE RENAME would break it.
> That doesn't work because for the
> identity case we don't store the expression in the catalog. The
> proposed patch works by storing the sequence *name* in the internal
> structures so that it can be used in place of the stored default value.
I trust you really mean you're storing an OID ...
regards, tom lane
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-27 20:43 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-12-30 23:26 ` Michael Paquier <michael.paquier@gmail.com>
2018-02-01 15:24 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 14+ messages in thread
From: Michael Paquier @ 2017-12-30 23:26 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-bugs
On Wed, Dec 27, 2017 at 03:43:05PM -0500, Tom Lane wrote:
> Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
>> That doesn't work because for the
>> identity case we don't store the expression in the catalog. The
>> proposed patch works by storing the sequence *name* in the internal
>> structures so that it can be used in place of the stored default value.
>
> I trust you really mean you're storing an OID ...
A RangeVar is used for the proposed patch 0002. Still does it matter? In
the scenario proposed here, which is to fix ALTER TABLE ADD COLUMN
GENERATED, then the sequence gets created and visible only once the
transaction adding the column has been created.
char identity; /* attidentity setting */
+ RangeVar *identitySequence;
CollateClause *collClause; /* untransformed COLLATE spec, if any */
It mat be better to tell that this is used to create a sequence in the
same transaction as the one doing the parsing.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-27 20:43 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-30 23:26 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
@ 2018-02-01 15:24 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-02-02 01:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
0 siblings, 1 reply; 14+ messages in thread
From: Peter Eisentraut @ 2018-02-01 15:24 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On 12/30/17 18:26, Michael Paquier wrote:
> A RangeVar is used for the proposed patch 0002. Still does it matter? In
> the scenario proposed here, which is to fix ALTER TABLE ADD COLUMN
> GENERATED, then the sequence gets created and visible only once the
> transaction adding the column has been created.
>
> char identity; /* attidentity setting */
> + RangeVar *identitySequence;
> CollateClause *collClause; /* untransformed COLLATE spec, if any */
> It mat be better to tell that this is used to create a sequence in the
> same transaction as the one doing the parsing.
I would like to get this into next week's minor release as a bug fix.
Other than tweaking some of the comments, is there any more feedback on
this?
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-27 20:43 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-30 23:26 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2018-02-01 15:24 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2018-02-02 01:49 ` Michael Paquier <michael.paquier@gmail.com>
2018-02-02 20:15 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 14+ messages in thread
From: Michael Paquier @ 2018-02-02 01:49 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs
On Thu, Feb 01, 2018 at 10:24:48AM -0500, Peter Eisentraut wrote:
> On 12/30/17 18:26, Michael Paquier wrote:
>> A RangeVar is used for the proposed patch 0002. Still does it matter? In
>> the scenario proposed here, which is to fix ALTER TABLE ADD COLUMN
>> GENERATED, then the sequence gets created and visible only once the
>> transaction adding the column has been created.
>>
>> char identity; /* attidentity setting */
>> + RangeVar *identitySequence;
>> CollateClause *collClause; /* untransformed COLLATE spec, if any */
>> It mat be better to tell that this is used to create a sequence in the
>> same transaction as the one doing the parsing.
>
> I would like to get this into next week's minor release as a bug fix.
> Other than tweaking some of the comments, is there any more feedback on
> this?
Wrapping again my mind on this one... On top of the comment for
identitySequence, I think that it is important to mention that the
sequence name and a RangeVar are basically safe as they get created
hence they are not visible to other sessions yet. 0001 and 0002 should
be merged.
By the way, on HEAD with two sessions it is easy to bump into sequence
name conflicts with identity columns:
* Session 1:
begin;
create table itest13 (a int);
* Session 2:
create sequence itest13_b_seq;
* Session 1:
alter table itest13 add columns b int generated by default as identity; --blocks
* Session 2:
commit;
And then session 1 reports that:
ERROR: 23505: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
DETAIL: Key (typname, typnamespace)=(itest13_b_seq, 2200) already exists.
SCHEMA NAME: pg_catalog
TABLE NAME: pg_type
CONSTRAINT NAME: pg_type_typname_nsp_index
LOCATION: _bt_check_unique, nbtinsert.c:434
We surely want to be smarter with the choice of the sequence name for
identity columns. Index names for primary keys are if I recall
correctly indexcmds.c and index.c.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 14+ messages in thread
* Re: BUG #14952: COPY fails to fill in IDENTITY column default value
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-14 03:39 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-15 17:37 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-27 20:43 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Tom Lane <tgl@sss.pgh.pa.us>
2017-12-30 23:26 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
2018-02-01 15:24 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-02-02 01:49 ` Re: BUG #14952: COPY fails to fill in IDENTITY column default value Michael Paquier <michael.paquier@gmail.com>
@ 2018-02-02 20:15 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 0 replies; 14+ messages in thread
From: Peter Eisentraut @ 2018-02-02 20:15 UTC (permalink / raw)
To: Michael Paquier <michael.paquier@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs
On 2/1/18 20:49, Michael Paquier wrote:
> Wrapping again my mind on this one... On top of the comment for
> identitySequence, I think that it is important to mention that the
> sequence name and a RangeVar are basically safe as they get created
> hence they are not visible to other sessions yet. 0001 and 0002 should
> be merged.
Committed with more comments and as one patch.
> By the way, on HEAD with two sessions it is easy to bump into sequence
> name conflicts with identity columns:
> * Session 1:
> begin;
> create table itest13 (a int);
> * Session 2:
> create sequence itest13_b_seq;
> * Session 1:
> alter table itest13 add columns b int generated by default as identity; --blocks
> * Session 2:
> commit;
>
> And then session 1 reports that:
> ERROR: 23505: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
> DETAIL: Key (typname, typnamespace)=(itest13_b_seq, 2200) already exists.
> SCHEMA NAME: pg_catalog
> TABLE NAME: pg_type
> CONSTRAINT NAME: pg_type_typname_nsp_index
> LOCATION: _bt_check_unique, nbtinsert.c:434
I think this is equivalent to pre-existing behavior with serial:
S1: begin;
S2: begin;
S2: create sequence t1_a_seq;
S1: create table t1 (a serial, b text); --blocks
S2: commit;
S1: ERROR
I suspect you can also construct situations like this with other
implicitly created objects such as triggers.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 14+ messages in thread
end of thread, other threads:[~2018-02-02 20:15 UTC | newest]
Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-12-07 11:29 BUG #14952: COPY fails to fill in IDENTITY column default value steven.winfield@cantabcapital.com
2017-12-07 17:13 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-08 01:37 ` Michael Paquier <michael.paquier@gmail.com>
2017-12-08 14:44 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-14 03:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-12-14 04:22 ` Michael Paquier <michael.paquier@gmail.com>
2017-12-15 17:37 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-18 05:49 ` Michael Paquier <michael.paquier@gmail.com>
2017-12-27 20:01 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-12-27 20:43 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-12-30 23:26 ` Michael Paquier <michael.paquier@gmail.com>
2018-02-01 15:24 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-02-02 01:49 ` Michael Paquier <michael.paquier@gmail.com>
2018-02-02 20:15 ` 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