Commit 874148fe authored by Bruce Momjian's avatar Bruce Momjian

IMPROVED VERSION APPLIED:

The attached patch completes the following TODO item:

    * Generate failure on short COPY lines rather than pad NULLs

I also restructed a lot of the existing COPY code, did some code
review on the column list patch sent in by Brent Verner a little
while ago, and added some regression tests. I also added an
explicit check (and resultant error) for extra data before
the end-of-line.

Neil Conway
parent 1dedbf2d
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/copy.sgml,v 1.33 2002/07/18 04:43:50 momjian Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/copy.sgml,v 1.34 2002/07/30 16:55:05 momjian Exp $
PostgreSQL documentation
-->
......@@ -199,12 +199,12 @@ ERROR: <replaceable>reason</replaceable>
whatever is in the table already).
</para>
<para>
When using the optional column list syntax, <command>COPY TO</command>
and <command>COPY FROM</command> will only copy the specified
columns' values to/from the table. If a column in the table
is not in the column list, <command>COPY FROM</command> will insert
default values for that column if a default value is defined.
<para>
If a list of columns is specified, <command>COPY</command> will
only copy the data in the specified columns to or from the table.
If there are any columns in the table that are not in the table,
<command>COPY FROM</command> will insert the default value for
that column.
</para>
<para>
......@@ -266,8 +266,8 @@ ERROR: <replaceable>reason</replaceable>
</para>
<para>
<command>COPY FROM</command> neither invokes rules nor acts on column
defaults. It does invoke triggers and check constraints.
<command>COPY FROM</command> will invoke any triggers or check
constraints. However, it will not invoke rules.
</para>
<para>
......@@ -330,7 +330,12 @@ ERROR: <replaceable>reason</replaceable>
The attribute values themselves are strings generated by the
output function, or acceptable to the input function, of each
attribute's data type. The specified null-value string is used in
place of attributes that are NULL.
place of attributes that are NULL. When using <command>COPY
FROM</command> without a column list, each row of the input file
must contain data for each attribute in the table: no missing data
is allowed. Similarly, <command>COPY FROM</command> will raise
an error if it encounters any data in the input file that would
not be inserted into the table: extra data is not allowed.
</para>
<para>
If OIDS is specified, the OID is read or written as the first column,
......
This diff is collapsed.
......@@ -23,10 +23,44 @@ CREATE TRIGGER trg_x_after AFTER INSERT ON x
FOR EACH ROW EXECUTE PROCEDURE fn_x_after();
CREATE TRIGGER trg_x_before BEFORE INSERT ON x
FOR EACH ROW EXECUTE PROCEDURE fn_x_before();
COPY x (a,b,c,d,e) from stdin;
COPY x (b,d) from stdin;
COPY x (b,d) from stdin;
COPY x (a,b,c,d,e) from stdin;
COPY x (a, b, c, d, e) from stdin;
COPY x (b, d) from stdin;
COPY x (b, d) from stdin;
COPY x (a, b, c, d, e) from stdin;
-- non-existent column in column list: should fail
COPY x (xyz) from stdin;
ERROR: COPY: Specified column "xyz" does not exist
-- too many columns in column list: should fail
COPY x (a, b, c, d, e, d, c) from stdin;
ERROR: COPY: Too many columns specified
-- missing data: should fail
COPY x from stdin;
ERROR: copy: line 1, COPY TEXT: Missing data for attribute 1
lost synchronization with server, resetting connection
COPY x from stdin;
ERROR: copy: line 1, COPY TEXT: Missing data for attribute 4
lost synchronization with server, resetting connection
COPY x from stdin;
ERROR: copy: line 1, COPY TEXT: Missing data for attribute 4
lost synchronization with server, resetting connection
-- extra data: should fail
COPY x from stdin;
ERROR: copy: line 1, COPY TEXT: Extra data encountered
lost synchronization with server, resetting connection
-- various COPY options: delimiters, oids, NULL string
COPY x (b, c, d, e) from stdin with oids delimiter ',' null 'x';
-- COPY w/ oids on a table w/o oids should fail
CREATE TABLE no_oids (
a int,
b int
) WITHOUT OIDS;
INSERT INTO no_oids (a, b) VALUES (5, 10);
INSERT INTO no_oids (a, b) VALUES (20, 30);
-- should fail
COPY no_oids FROM stdin WITH OIDS;
ERROR: COPY: table "no_oids" does not have OIDs
COPY no_oids TO stdout WITH OIDS;
ERROR: COPY: table "no_oids" does not have OIDs
COPY x TO stdout;
10000 21 31 41 before trigger fired
10001 22 32 42 before trigger fired
......@@ -34,11 +68,25 @@ COPY x TO stdout;
10003 24 34 44 before trigger fired
10004 25 35 45 before trigger fired
10005 26 36 46 before trigger fired
6 \N 45 80 before trigger fired
1 1 stuff test_1 after trigger fired
2 2 stuff test_2 after trigger fired
3 3 stuff test_3 after trigger fired
4 4 stuff test_4 after trigger fired
5 5 stuff test_5 after trigger fired
COPY x (c, e) TO stdout;
31 before trigger fired
32 before trigger fired
33 before trigger fired
34 before trigger fired
35 before trigger fired
36 before trigger fired
45 before trigger fired
stuff after trigger fired
stuff after trigger fired
stuff after trigger fired
stuff after trigger fired
stuff after trigger fired
DROP TABLE x;
DROP FUNCTION fn_x_before();
DROP FUNCTION fn_x_after();
......@@ -26,22 +26,22 @@ FOR EACH ROW EXECUTE PROCEDURE fn_x_after();
CREATE TRIGGER trg_x_before BEFORE INSERT ON x
FOR EACH ROW EXECUTE PROCEDURE fn_x_before();
COPY x (a,b,c,d,e) from stdin;
COPY x (a, b, c, d, e) from stdin;
10000 21 31 41 51
\.
COPY x (b,d) from stdin;
COPY x (b, d) from stdin;
1 test_1
\.
COPY x (b,d) from stdin;
COPY x (b, d) from stdin;
2 test_2
3 test_3
4 test_4
5 test_5
\.
COPY x (a,b,c,d,e) from stdin;
COPY x (a, b, c, d, e) from stdin;
10001 22 32 42 52
10002 23 33 43 53
10003 24 34 44 54
......@@ -49,7 +49,48 @@ COPY x (a,b,c,d,e) from stdin;
10005 26 36 46 56
\.
-- non-existent column in column list: should fail
COPY x (xyz) from stdin;
-- too many columns in column list: should fail
COPY x (a, b, c, d, e, d, c) from stdin;
-- missing data: should fail
COPY x from stdin;
\.
COPY x from stdin;
2000 230 23 23
\.
COPY x from stdin;
2001 231 \N \N
\.
-- extra data: should fail
COPY x from stdin;
2002 232 40 50 60 70 80
\.
-- various COPY options: delimiters, oids, NULL string
COPY x (b, c, d, e) from stdin with oids delimiter ',' null 'x';
500000,x,45,80,90
\.
-- COPY w/ oids on a table w/o oids should fail
CREATE TABLE no_oids (
a int,
b int
) WITHOUT OIDS;
INSERT INTO no_oids (a, b) VALUES (5, 10);
INSERT INTO no_oids (a, b) VALUES (20, 30);
-- should fail
COPY no_oids FROM stdin WITH OIDS;
COPY no_oids TO stdout WITH OIDS;
COPY x TO stdout;
COPY x (c, e) TO stdout;
DROP TABLE x;
DROP FUNCTION fn_x_before();
DROP FUNCTION fn_x_after();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment