From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Paquier <michael.paquier@gmail.com>
Cc: steven.winfield@cantabcapital.com, PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14952: COPY fails to fill in IDENTITY column default value
Date: Fri, 15 Dec 2017 12:37:20 -0500
Message-ID: <21a6da72-fb24-4b9e-d300-7439425f7492@2ndquadrant.com> (raw)
In-Reply-To: <31740.1513222740@sss.pgh.pa.us>
References: <20171207112930.21696.79944@wrigleys.postgresql.org>
<12618e2c-0eb0-3e15-c45d-96891c52eb5f@2ndquadrant.com>
<CAB7nPqTMCmO_Y3HcQn9-oKFQBcWXckf24urAk+_ifBjfjhD0sA@mail.gmail.com>
<31740.1513222740@sss.pgh.pa.us>
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.cindex 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.cindex 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.outindex 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.sqlindex 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.plindex 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.cindex 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.cindex 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.cindex 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.cindex 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.cindex 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.hindex 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.outindex 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: peter.eisentraut@2ndquadrant.com, tgl@sss.pgh.pa.us, michael.paquier@gmail.com
Subject: Re: BUG #14952: COPY fails to fill in IDENTITY column default value
In-Reply-To: <21a6da72-fb24-4b9e-d300-7439425f7492@2ndquadrant.com>
References: <20171207112930.21696.79944@wrigleys.postgresql.org> <12618e2c-0eb0-3e15-c45d-96891c52eb5f@2ndquadrant.com> <CAB7nPqTMCmO_Y3HcQn9-oKFQBcWXckf24urAk+_ifBjfjhD0sA@mail.gmail.com> <31740.1513222740@sss.pgh.pa.us> <21a6da72-fb24-4b9e-d300-7439425f7492@2ndquadrant.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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