Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2d2d06b

Browse files
committed
Apply identity sequence values on COPY
A COPY into a table should apply identity sequence values just like it does for ordinary defaults. This was previously forgotten, leading to null values being inserted, which in turn would fail because identity columns have not-null constraints. Author: Michael Paquier <michael.paquier@gmail.com> Reported-by: Steven Winfield <steven.winfield@cantabcapital.com> Bug: #14952
1 parent 0a3edbb commit 2d2d06b

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/backend/commands/copy.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "access/sysattr.h"
2424
#include "access/xact.h"
2525
#include "access/xlog.h"
26+
#include "catalog/dependency.h"
2627
#include "catalog/pg_type.h"
2728
#include "commands/copy.h"
2829
#include "commands/defrem.h"
@@ -3067,8 +3068,19 @@ BeginCopyFrom(ParseState *pstate,
30673068
{
30683069
/* attribute is NOT to be copied from input */
30693070
/* use default value if one exists */
3070-
Expr *defexpr = (Expr *) build_column_default(cstate->rel,
3071-
attnum);
3071+
Expr *defexpr;
3072+
3073+
if (att->attidentity)
3074+
{
3075+
NextValueExpr *nve = makeNode(NextValueExpr);
3076+
3077+
nve->seqid = getOwnedSequence(RelationGetRelid(cstate->rel),
3078+
attnum);
3079+
nve->typeId = att->atttypid;
3080+
defexpr = (Expr *) nve;
3081+
}
3082+
else
3083+
defexpr = (Expr *) build_column_default(cstate->rel, attnum);
30723084

30733085
if (defexpr != NULL)
30743086
{

src/test/regress/expected/identity.out

+13
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ SELECT * FROM itest2;
153153
3 |
154154
(3 rows)
155155

156+
-- COPY tests
157+
CREATE TABLE itest9 (a int GENERATED ALWAYS AS IDENTITY, b text, c bigint);
158+
COPY itest9 FROM stdin;
159+
COPY itest9 (b, c) FROM stdin;
160+
SELECT * FROM itest9 ORDER BY c;
161+
a | b | c
162+
-----+------+-----
163+
100 | foo | 200
164+
101 | bar | 201
165+
1 | foo2 | 202
166+
2 | bar2 | 203
167+
(4 rows)
168+
156169
-- DROP IDENTITY tests
157170
ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY;
158171
ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY; -- error

src/test/regress/sql/identity.sql

+17
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ UPDATE itest2 SET a = DEFAULT WHERE a = 2;
7878
SELECT * FROM itest2;
7979

8080

81+
-- COPY tests
82+
83+
CREATE TABLE itest9 (a int GENERATED ALWAYS AS IDENTITY, b text, c bigint);
84+
85+
COPY itest9 FROM stdin;
86+
100 foo 200
87+
101 bar 201
88+
\.
89+
90+
COPY itest9 (b, c) FROM stdin;
91+
foo2 202
92+
bar2 203
93+
\.
94+
95+
SELECT * FROM itest9 ORDER BY c;
96+
97+
8198
-- DROP IDENTITY tests
8299

83100
ALTER TABLE itest4 ALTER COLUMN a DROP IDENTITY;

0 commit comments

Comments
 (0)