Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2d2d06b

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

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

‎src/backend/commands/copy.c

Lines changed: 14 additions & 2 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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+
CREATETABLEitest9 (aint GENERATED ALWAYSAS IDENTITY, btext, cbigint);
84+
85+
COPY itest9FROM stdin;
86+
100foo200
87+
101bar201
88+
\.
89+
90+
COPY itest9 (b, c)FROM stdin;
91+
foo2202
92+
bar2203
93+
\.
94+
95+
SELECT*FROM itest9ORDER BY c;
96+
97+
8198
-- DROP IDENTITY tests
8299

83100
ALTERTABLE itest4 ALTER COLUMN a DROP IDENTITY;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp