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

Commit 276393f

Browse files
committed
Only evaluate default values as required when doing COPY FROM
Commit 9f8377f was a little too eager in fetching default values. Normally this would not matter, but if the default value is not valid for the type (e.g. a varchar that's too long) it caused an unnecessary error. Complaint and fix from Laurenz Albe Backpatch to release 16. Discussion: https://postgr.es/m/75a7b7483aeb331aa017328d606d568fc715b90d.camel@cybertec.at
1 parent b1a8dc8 commit 276393f

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/backend/commands/copyfrom.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,14 @@ BeginCopyFrom(ParseState *pstate,
15711571
/* Get default info if available */
15721572
defexprs[attnum - 1] = NULL;
15731573

1574-
if (!att->attgenerated)
1574+
/*
1575+
* We only need the default values for columns that do not appear in
1576+
* the column list, unless the DEFAULT option was given. We never need
1577+
* default values for generated columns.
1578+
*/
1579+
if ((cstate->opts.default_print != NULL ||
1580+
!list_member_int(cstate->attnumlist, attnum)) &&
1581+
!att->attgenerated)
15751582
{
15761583
Expr *defexpr = (Expr *) build_column_default(cstate->rel,
15771584
attnum);

src/test/regress/expected/copy.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,20 @@ SELECT * FROM header_copytest ORDER BY a;
240240
(5 rows)
241241

242242
drop table header_copytest;
243+
-- test COPY with overlong column defaults
244+
create temp table oversized_column_default (
245+
col1 varchar(5) DEFAULT 'more than 5 chars',
246+
col2 varchar(5));
247+
-- normal COPY should work
248+
copy oversized_column_default from stdin;
249+
-- error if the column is excluded
250+
copy oversized_column_default (col2) from stdin;
251+
ERROR: value too long for type character varying(5)
252+
\.
253+
invalid command \.
254+
-- error if the DEFAULT option is given
255+
copy oversized_column_default from stdin (default '');
256+
ERROR: value too long for type character varying(5)
257+
\.
258+
invalid command \.
259+
drop table oversized_column_default;

src/test/regress/sql/copy.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,18 @@ a c b
268268

269269
SELECT * FROM header_copytest ORDER BY a;
270270
drop table header_copytest;
271+
272+
-- test COPY with overlong column defaults
273+
create temp table oversized_column_default (
274+
col1 varchar(5) DEFAULT 'more than 5 chars',
275+
col2 varchar(5));
276+
-- normal COPY should work
277+
copy oversized_column_default from stdin;
278+
\.
279+
-- error if the column is excluded
280+
copy oversized_column_default (col2) from stdin;
281+
\.
282+
-- error if the DEFAULT option is given
283+
copy oversized_column_default from stdin (default '');
284+
\.
285+
drop table oversized_column_default;

0 commit comments

Comments
 (0)