2009-06-14 02:00:24 +02:00
|
|
|
CREATE TEMP TABLE x (
|
2002-07-18 06:45:51 +02:00
|
|
|
a serial,
|
|
|
|
b int,
|
|
|
|
c text not null default 'stuff',
|
2003-10-10 05:46:51 +02:00
|
|
|
d text,
|
2002-07-18 06:45:51 +02:00
|
|
|
e text
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
) ;
|
2002-08-22 02:01:51 +02:00
|
|
|
CREATE FUNCTION fn_x_before () RETURNS TRIGGER AS '
|
2002-07-18 06:45:51 +02:00
|
|
|
BEGIN
|
|
|
|
NEW.e := ''before trigger fired''::text;
|
|
|
|
return NEW;
|
|
|
|
END;
|
2006-02-27 17:09:50 +01:00
|
|
|
' LANGUAGE plpgsql;
|
2002-08-22 02:01:51 +02:00
|
|
|
CREATE FUNCTION fn_x_after () RETURNS TRIGGER AS '
|
2002-07-18 06:45:51 +02:00
|
|
|
BEGIN
|
|
|
|
UPDATE x set e=''after trigger fired'' where c=''stuff'';
|
|
|
|
return NULL;
|
|
|
|
END;
|
2006-02-27 17:09:50 +01:00
|
|
|
' LANGUAGE plpgsql;
|
2002-07-18 06:45:51 +02:00
|
|
|
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();
|
2002-07-30 18:55:06 +02:00
|
|
|
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;
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: column "xyz" of relation "x" does not exist
|
2002-07-30 18:55:06 +02:00
|
|
|
-- too many columns in column list: should fail
|
|
|
|
COPY x (a, b, c, d, e, d, c) from stdin;
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: column "d" specified more than once
|
2002-07-30 18:55:06 +02:00
|
|
|
-- missing data: should fail
|
|
|
|
COPY x from stdin;
|
2018-07-22 23:58:01 +02:00
|
|
|
ERROR: invalid input syntax for type integer: ""
|
2003-09-30 00:06:40 +02:00
|
|
|
CONTEXT: COPY x, line 1, column a: ""
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x from stdin;
|
2003-07-20 23:56:35 +02:00
|
|
|
ERROR: missing data for column "e"
|
2003-09-30 00:06:40 +02:00
|
|
|
CONTEXT: COPY x, line 1: "2000 230 23 23"
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x from stdin;
|
2003-07-20 23:56:35 +02:00
|
|
|
ERROR: missing data for column "e"
|
2003-09-30 00:06:40 +02:00
|
|
|
CONTEXT: COPY x, line 1: "2001 231 \N \N"
|
2002-07-30 18:55:06 +02:00
|
|
|
-- extra data: should fail
|
|
|
|
COPY x from stdin;
|
2003-07-20 23:56:35 +02:00
|
|
|
ERROR: extra data after last expected column
|
2003-09-30 00:06:40 +02:00
|
|
|
CONTEXT: COPY x, line 1: "2002 232 40 50 60 70 80"
|
2011-02-21 06:08:04 +01:00
|
|
|
-- various COPY options: delimiters, oids, NULL string, encoding
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
COPY x (b, c, d, e) from stdin delimiter ',' null 'x';
|
2003-10-10 05:46:51 +02:00
|
|
|
COPY x from stdin WITH DELIMITER AS ';' NULL AS '';
|
2011-02-21 06:08:04 +01:00
|
|
|
COPY x from stdin WITH DELIMITER AS ':' NULL AS E'\\X' ENCODING 'sql_ascii';
|
2002-08-02 20:15:10 +02:00
|
|
|
-- check results of copy in
|
|
|
|
SELECT * FROM x;
|
2003-10-10 05:46:51 +02:00
|
|
|
a | b | c | d | e
|
|
|
|
-------+----+------------+--------+----------------------
|
|
|
|
9999 | | \N | NN | before trigger fired
|
|
|
|
10000 | 21 | 31 | 41 | before trigger fired
|
|
|
|
10001 | 22 | 32 | 42 | before trigger fired
|
|
|
|
10002 | 23 | 33 | 43 | before trigger fired
|
|
|
|
10003 | 24 | 34 | 44 | before trigger fired
|
|
|
|
10004 | 25 | 35 | 45 | before trigger fired
|
|
|
|
10005 | 26 | 36 | 46 | before trigger fired
|
|
|
|
6 | | 45 | 80 | before trigger fired
|
|
|
|
7 | | x | \x | before trigger fired
|
|
|
|
8 | | , | \, | before trigger fired
|
|
|
|
3000 | | c | | before trigger fired
|
|
|
|
4000 | | C | | before trigger fired
|
|
|
|
4001 | 1 | empty | | before trigger fired
|
|
|
|
4002 | 2 | null | | before trigger fired
|
|
|
|
4003 | 3 | Backslash | \ | before trigger fired
|
|
|
|
4004 | 4 | BackslashX | \X | before trigger fired
|
|
|
|
4005 | 5 | N | N | before trigger fired
|
|
|
|
4006 | 6 | BackslashN | \N | before trigger fired
|
|
|
|
4007 | 7 | XX | XX | before trigger fired
|
|
|
|
4008 | 8 | Delimiter | : | 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
|
|
|
|
(25 rows)
|
2002-08-02 20:15:10 +02:00
|
|
|
|
|
|
|
-- check copy out
|
2002-07-18 06:45:51 +02:00
|
|
|
COPY x TO stdout;
|
2003-10-10 05:46:51 +02:00
|
|
|
9999 \N \\N NN before trigger fired
|
2002-07-18 06:45:51 +02:00
|
|
|
10000 21 31 41 before trigger fired
|
|
|
|
10001 22 32 42 before trigger fired
|
|
|
|
10002 23 33 43 before trigger fired
|
|
|
|
10003 24 34 44 before trigger fired
|
|
|
|
10004 25 35 45 before trigger fired
|
|
|
|
10005 26 36 46 before trigger fired
|
2002-07-30 18:55:06 +02:00
|
|
|
6 \N 45 80 before trigger fired
|
2003-10-10 05:46:51 +02:00
|
|
|
7 \N x \\x before trigger fired
|
|
|
|
8 \N , \\, before trigger fired
|
|
|
|
3000 \N c \N before trigger fired
|
|
|
|
4000 \N C \N before trigger fired
|
|
|
|
4001 1 empty before trigger fired
|
|
|
|
4002 2 null \N before trigger fired
|
|
|
|
4003 3 Backslash \\ before trigger fired
|
|
|
|
4004 4 BackslashX \\X before trigger fired
|
|
|
|
4005 5 N N before trigger fired
|
|
|
|
4006 6 BackslashN \\N before trigger fired
|
|
|
|
4007 7 XX XX before trigger fired
|
|
|
|
4008 8 Delimiter : before trigger fired
|
2002-07-18 06:45:51 +02:00
|
|
|
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
|
2002-07-30 18:55:06 +02:00
|
|
|
COPY x (c, e) TO stdout;
|
2003-10-10 05:46:51 +02:00
|
|
|
\\N before trigger fired
|
2002-07-30 18:55:06 +02:00
|
|
|
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
|
2003-10-10 05:46:51 +02:00
|
|
|
x before trigger fired
|
|
|
|
, before trigger fired
|
|
|
|
c before trigger fired
|
|
|
|
C before trigger fired
|
|
|
|
empty before trigger fired
|
|
|
|
null before trigger fired
|
|
|
|
Backslash before trigger fired
|
|
|
|
BackslashX before trigger fired
|
|
|
|
N before trigger fired
|
|
|
|
BackslashN before trigger fired
|
|
|
|
XX before trigger fired
|
|
|
|
Delimiter before trigger fired
|
2002-07-30 18:55:06 +02:00
|
|
|
stuff after trigger fired
|
|
|
|
stuff after trigger fired
|
|
|
|
stuff after trigger fired
|
|
|
|
stuff after trigger fired
|
|
|
|
stuff after trigger fired
|
2002-08-02 20:15:10 +02:00
|
|
|
COPY x (b, e) TO stdout WITH NULL 'I''m null';
|
2003-10-10 05:46:51 +02:00
|
|
|
I'm null before trigger fired
|
2002-08-02 20:15:10 +02:00
|
|
|
21 before trigger fired
|
|
|
|
22 before trigger fired
|
|
|
|
23 before trigger fired
|
|
|
|
24 before trigger fired
|
|
|
|
25 before trigger fired
|
|
|
|
26 before trigger fired
|
|
|
|
I'm null before trigger fired
|
2003-10-10 05:46:51 +02:00
|
|
|
I'm null before trigger fired
|
|
|
|
I'm null before trigger fired
|
|
|
|
I'm null before trigger fired
|
|
|
|
I'm null before trigger fired
|
|
|
|
1 before trigger fired
|
|
|
|
2 before trigger fired
|
|
|
|
3 before trigger fired
|
|
|
|
4 before trigger fired
|
|
|
|
5 before trigger fired
|
|
|
|
6 before trigger fired
|
|
|
|
7 before trigger fired
|
|
|
|
8 before trigger fired
|
2002-08-02 20:15:10 +02:00
|
|
|
1 after trigger fired
|
|
|
|
2 after trigger fired
|
|
|
|
3 after trigger fired
|
|
|
|
4 after trigger fired
|
|
|
|
5 after trigger fired
|
2009-06-14 02:00:24 +02:00
|
|
|
CREATE TEMP TABLE y (
|
2004-06-10 01:39:24 +02:00
|
|
|
col1 text,
|
|
|
|
col2 text
|
|
|
|
);
|
2005-06-26 05:04:37 +02:00
|
|
|
INSERT INTO y VALUES ('Jackson, Sam', E'\\h');
|
|
|
|
INSERT INTO y VALUES ('It is "perfect".',E'\t');
|
2004-06-10 01:39:24 +02:00
|
|
|
INSERT INTO y VALUES ('', NULL);
|
|
|
|
COPY y TO stdout WITH CSV;
|
|
|
|
"Jackson, Sam",\h
|
|
|
|
"It is ""perfect"".",
|
|
|
|
"",
|
|
|
|
COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|';
|
|
|
|
Jackson, Sam|\h
|
|
|
|
It is "perfect".|
|
|
|
|
''|
|
2011-02-21 06:08:04 +01:00
|
|
|
COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE E'\\' ENCODING 'sql_ascii';
|
2004-06-10 01:39:24 +02:00
|
|
|
"Jackson, Sam","\\h"
|
|
|
|
"It is \"perfect\"."," "
|
|
|
|
"",
|
2009-07-25 02:07:14 +02:00
|
|
|
COPY y TO stdout WITH CSV FORCE QUOTE *;
|
|
|
|
"Jackson, Sam","\h"
|
|
|
|
"It is ""perfect""."," "
|
|
|
|
"",
|
2010-02-17 05:19:41 +01:00
|
|
|
-- Repeat above tests with new 9.0 option syntax
|
2009-09-21 22:10:21 +02:00
|
|
|
COPY y TO stdout (FORMAT CSV);
|
|
|
|
"Jackson, Sam",\h
|
|
|
|
"It is ""perfect"".",
|
|
|
|
"",
|
|
|
|
COPY y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|');
|
|
|
|
Jackson, Sam|\h
|
|
|
|
It is "perfect".|
|
|
|
|
''|
|
|
|
|
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\');
|
|
|
|
"Jackson, Sam","\\h"
|
|
|
|
"It is \"perfect\"."," "
|
|
|
|
"",
|
|
|
|
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE *);
|
|
|
|
"Jackson, Sam","\h"
|
|
|
|
"It is ""perfect""."," "
|
|
|
|
"",
|
|
|
|
\copy y TO stdout (FORMAT CSV)
|
|
|
|
"Jackson, Sam",\h
|
|
|
|
"It is ""perfect"".",
|
|
|
|
"",
|
|
|
|
\copy y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|')
|
|
|
|
Jackson, Sam|\h
|
|
|
|
It is "perfect".|
|
|
|
|
''|
|
|
|
|
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\')
|
|
|
|
"Jackson, Sam","\\h"
|
|
|
|
"It is \"perfect\"."," "
|
|
|
|
"",
|
|
|
|
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE *)
|
|
|
|
"Jackson, Sam","\h"
|
|
|
|
"It is ""perfect""."," "
|
|
|
|
"",
|
2005-05-13 08:33:40 +02:00
|
|
|
--test that we read consecutive LFs properly
|
|
|
|
CREATE TEMP TABLE testnl (a int, b text, c int);
|
|
|
|
COPY testnl FROM stdin CSV;
|
2005-12-28 04:25:32 +01:00
|
|
|
-- test end of copy marker
|
|
|
|
CREATE TEMP TABLE testeoc (a text);
|
|
|
|
COPY testeoc FROM stdin CSV;
|
2005-12-28 16:07:02 +01:00
|
|
|
COPY testeoc TO stdout CSV;
|
|
|
|
a\.
|
|
|
|
\.b
|
|
|
|
c\.d
|
|
|
|
"\."
|
2012-03-26 05:17:22 +02:00
|
|
|
-- test handling of nonstandard null marker that violates escaping rules
|
|
|
|
CREATE TEMP TABLE testnull(a int, b text);
|
|
|
|
INSERT INTO testnull VALUES (1, E'\\0'), (NULL, NULL);
|
|
|
|
COPY testnull TO stdout WITH NULL AS E'\\0';
|
|
|
|
1 \\0
|
|
|
|
\0 \0
|
|
|
|
COPY testnull FROM stdin WITH NULL AS E'\\0';
|
|
|
|
SELECT * FROM testnull;
|
|
|
|
a | b
|
|
|
|
----+----
|
|
|
|
1 | \0
|
|
|
|
|
|
|
|
|
42 | \0
|
|
|
|
|
|
|
|
|
(4 rows)
|
|
|
|
|
2012-12-07 14:26:52 +01:00
|
|
|
BEGIN;
|
2012-12-01 13:54:20 +01:00
|
|
|
CREATE TABLE vistest (LIKE testeoc);
|
2012-12-07 14:26:52 +01:00
|
|
|
COPY vistest FROM stdin CSV;
|
|
|
|
COMMIT;
|
2012-12-07 15:18:47 +01:00
|
|
|
SELECT * FROM vistest;
|
|
|
|
a
|
|
|
|
----
|
|
|
|
a0
|
|
|
|
b
|
2012-12-07 14:26:52 +01:00
|
|
|
(2 rows)
|
|
|
|
|
2012-12-01 13:54:20 +01:00
|
|
|
BEGIN;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
COPY vistest FROM stdin CSV;
|
|
|
|
SELECT * FROM vistest;
|
2012-12-02 21:52:52 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
a1
|
2012-12-01 13:54:20 +01:00
|
|
|
b
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
SAVEPOINT s1;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
COPY vistest FROM stdin CSV;
|
|
|
|
SELECT * FROM vistest;
|
2012-12-02 21:52:52 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
d1
|
2012-12-01 13:54:20 +01:00
|
|
|
e
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
COMMIT;
|
2012-12-02 21:52:52 +01:00
|
|
|
SELECT * FROM vistest;
|
|
|
|
a
|
|
|
|
----
|
|
|
|
d1
|
|
|
|
e
|
|
|
|
(2 rows)
|
|
|
|
|
2012-12-01 13:54:20 +01:00
|
|
|
BEGIN;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
|
|
|
SELECT * FROM vistest;
|
2012-12-02 21:52:52 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
a2
|
2012-12-01 13:54:20 +01:00
|
|
|
b
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
SAVEPOINT s1;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
|
|
|
SELECT * FROM vistest;
|
2012-12-02 21:52:52 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
d2
|
2012-12-01 13:54:20 +01:00
|
|
|
e
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
COMMIT;
|
2012-12-02 21:52:52 +01:00
|
|
|
SELECT * FROM vistest;
|
|
|
|
a
|
|
|
|
----
|
|
|
|
d2
|
|
|
|
e
|
|
|
|
(2 rows)
|
|
|
|
|
2012-12-01 14:46:41 +01:00
|
|
|
BEGIN;
|
2012-12-01 13:54:20 +01:00
|
|
|
TRUNCATE vistest;
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
|
|
|
SELECT * FROM vistest;
|
|
|
|
a
|
|
|
|
---
|
2012-12-01 14:46:41 +01:00
|
|
|
x
|
|
|
|
y
|
2012-12-01 13:54:20 +01:00
|
|
|
(2 rows)
|
|
|
|
|
2012-12-01 14:46:41 +01:00
|
|
|
COMMIT;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
2013-02-02 18:56:14 +01:00
|
|
|
ERROR: cannot perform FREEZE because the table was not created or truncated in the current subtransaction
|
2013-01-26 19:33:24 +01:00
|
|
|
BEGIN;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
SAVEPOINT s1;
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
2013-02-02 18:56:14 +01:00
|
|
|
ERROR: cannot perform FREEZE because the table was not created or truncated in the current subtransaction
|
2013-01-26 19:33:24 +01:00
|
|
|
COMMIT;
|
2012-12-01 13:54:20 +01:00
|
|
|
BEGIN;
|
|
|
|
INSERT INTO vistest VALUES ('z');
|
|
|
|
SAVEPOINT s1;
|
|
|
|
TRUNCATE vistest;
|
|
|
|
ROLLBACK TO SAVEPOINT s1;
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
2013-02-02 18:56:14 +01:00
|
|
|
ERROR: cannot perform FREEZE because the table was not created or truncated in the current subtransaction
|
2012-12-01 13:54:20 +01:00
|
|
|
COMMIT;
|
|
|
|
CREATE FUNCTION truncate_in_subxact() RETURNS VOID AS
|
|
|
|
$$
|
|
|
|
BEGIN
|
2012-12-02 21:52:52 +01:00
|
|
|
TRUNCATE vistest;
|
2012-12-01 13:54:20 +01:00
|
|
|
EXCEPTION
|
|
|
|
WHEN OTHERS THEN
|
2012-12-02 21:52:52 +01:00
|
|
|
INSERT INTO vistest VALUES ('subxact failure');
|
2012-12-01 13:54:20 +01:00
|
|
|
END;
|
|
|
|
$$ language plpgsql;
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO vistest VALUES ('z');
|
|
|
|
SELECT truncate_in_subxact();
|
|
|
|
truncate_in_subxact
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
COPY vistest FROM stdin CSV FREEZE;
|
|
|
|
SELECT * FROM vistest;
|
2012-12-02 21:52:52 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
d4
|
2012-12-01 13:54:20 +01:00
|
|
|
e
|
|
|
|
(2 rows)
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
SELECT * FROM vistest;
|
2012-12-02 21:52:52 +01:00
|
|
|
a
|
|
|
|
----
|
|
|
|
d4
|
2012-12-01 13:54:20 +01:00
|
|
|
e
|
|
|
|
(2 rows)
|
|
|
|
|
2014-03-04 23:31:59 +01:00
|
|
|
-- Test FORCE_NOT_NULL and FORCE_NULL options
|
|
|
|
CREATE TEMP TABLE forcetest (
|
|
|
|
a INT NOT NULL,
|
|
|
|
b TEXT NOT NULL,
|
|
|
|
c TEXT,
|
|
|
|
d TEXT,
|
|
|
|
e TEXT
|
|
|
|
);
|
|
|
|
\pset null NULL
|
2014-04-22 22:06:37 +02:00
|
|
|
-- should succeed with no effect ("b" remains an empty string, "c" remains NULL)
|
2014-03-04 23:31:59 +01:00
|
|
|
BEGIN;
|
|
|
|
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(b), FORCE_NULL(c));
|
|
|
|
COMMIT;
|
|
|
|
SELECT b, c FROM forcetest WHERE a = 1;
|
|
|
|
b | c
|
|
|
|
---+------
|
|
|
|
| NULL
|
|
|
|
(1 row)
|
|
|
|
|
2014-04-22 22:06:37 +02:00
|
|
|
-- should succeed, FORCE_NULL and FORCE_NOT_NULL can be both specified
|
2014-03-04 23:31:59 +01:00
|
|
|
BEGIN;
|
2014-04-22 22:06:37 +02:00
|
|
|
COPY forcetest (a, b, c, d) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(c,d), FORCE_NULL(c,d));
|
2014-03-04 23:31:59 +01:00
|
|
|
COMMIT;
|
2014-04-22 22:06:37 +02:00
|
|
|
SELECT c, d FROM forcetest WHERE a = 2;
|
|
|
|
c | d
|
2014-03-04 23:31:59 +01:00
|
|
|
---+------
|
|
|
|
| NULL
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- should fail with not-null constraint violation
|
|
|
|
BEGIN;
|
|
|
|
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NULL(b), FORCE_NOT_NULL(c));
|
|
|
|
ERROR: null value in column "b" violates not-null constraint
|
|
|
|
DETAIL: Failing row contains (3, null, , null, null).
|
|
|
|
CONTEXT: COPY forcetest, line 1: "3,,"""
|
|
|
|
ROLLBACK;
|
|
|
|
-- should fail with "not referenced by COPY" error
|
|
|
|
BEGIN;
|
|
|
|
COPY forcetest (d, e) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(b));
|
2015-11-05 03:01:26 +01:00
|
|
|
ERROR: FORCE_NOT_NULL column "b" not referenced by COPY
|
2014-03-04 23:31:59 +01:00
|
|
|
ROLLBACK;
|
|
|
|
-- should fail with "not referenced by COPY" error
|
|
|
|
BEGIN;
|
|
|
|
COPY forcetest (d, e) FROM STDIN WITH (FORMAT csv, FORCE_NULL(b));
|
2015-11-05 03:01:26 +01:00
|
|
|
ERROR: FORCE_NULL column "b" not referenced by COPY
|
2014-03-04 23:31:59 +01:00
|
|
|
ROLLBACK;
|
|
|
|
\pset null ''
|
2015-02-16 05:26:45 +01:00
|
|
|
-- test case with whole-row Var in a check constraint
|
|
|
|
create table check_con_tbl (f1 int);
|
|
|
|
create function check_con_function(check_con_tbl) returns bool as $$
|
|
|
|
begin
|
|
|
|
raise notice 'input = %', row_to_json($1);
|
|
|
|
return $1.f1 > 0;
|
|
|
|
end $$ language plpgsql immutable;
|
|
|
|
alter table check_con_tbl add check (check_con_function(check_con_tbl.*));
|
|
|
|
\d+ check_con_tbl
|
2016-11-03 17:00:00 +01:00
|
|
|
Table "public.check_con_tbl"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
--------+---------+-----------+----------+---------+---------+--------------+-------------
|
|
|
|
f1 | integer | | | | plain | |
|
2015-02-16 05:26:45 +01:00
|
|
|
Check constraints:
|
|
|
|
"check_con_tbl_check" CHECK (check_con_function(check_con_tbl.*))
|
|
|
|
|
|
|
|
copy check_con_tbl from stdin;
|
|
|
|
NOTICE: input = {"f1":1}
|
|
|
|
NOTICE: input = {"f1":null}
|
|
|
|
copy check_con_tbl from stdin;
|
|
|
|
NOTICE: input = {"f1":0}
|
|
|
|
ERROR: new row for relation "check_con_tbl" violates check constraint "check_con_tbl_check"
|
|
|
|
DETAIL: Failing row contains (0).
|
|
|
|
CONTEXT: COPY check_con_tbl, line 1: "0"
|
|
|
|
select * from check_con_tbl;
|
|
|
|
f1
|
|
|
|
----
|
|
|
|
1
|
|
|
|
|
|
|
|
(2 rows)
|
|
|
|
|
2016-10-03 22:22:57 +02:00
|
|
|
-- test with RLS enabled.
|
|
|
|
CREATE ROLE regress_rls_copy_user;
|
|
|
|
CREATE ROLE regress_rls_copy_user_colperms;
|
|
|
|
CREATE TABLE rls_t1 (a int, b int, c int);
|
|
|
|
COPY rls_t1 (a, b, c) from stdin;
|
|
|
|
CREATE POLICY p1 ON rls_t1 FOR SELECT USING (a % 2 = 0);
|
|
|
|
ALTER TABLE rls_t1 ENABLE ROW LEVEL SECURITY;
|
|
|
|
ALTER TABLE rls_t1 FORCE ROW LEVEL SECURITY;
|
|
|
|
GRANT SELECT ON TABLE rls_t1 TO regress_rls_copy_user;
|
|
|
|
GRANT SELECT (a, b) ON TABLE rls_t1 TO regress_rls_copy_user_colperms;
|
|
|
|
-- all columns
|
|
|
|
COPY rls_t1 TO stdout;
|
|
|
|
1 4 1
|
|
|
|
2 3 2
|
|
|
|
3 2 3
|
|
|
|
4 1 4
|
|
|
|
COPY rls_t1 (a, b, c) TO stdout;
|
|
|
|
1 4 1
|
|
|
|
2 3 2
|
|
|
|
3 2 3
|
|
|
|
4 1 4
|
|
|
|
-- subset of columns
|
|
|
|
COPY rls_t1 (a) TO stdout;
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
COPY rls_t1 (a, b) TO stdout;
|
|
|
|
1 4
|
|
|
|
2 3
|
|
|
|
3 2
|
|
|
|
4 1
|
|
|
|
-- column reordering
|
|
|
|
COPY rls_t1 (b, a) TO stdout;
|
|
|
|
4 1
|
|
|
|
3 2
|
|
|
|
2 3
|
|
|
|
1 4
|
|
|
|
SET SESSION AUTHORIZATION regress_rls_copy_user;
|
|
|
|
-- all columns
|
|
|
|
COPY rls_t1 TO stdout;
|
|
|
|
2 3 2
|
|
|
|
4 1 4
|
|
|
|
COPY rls_t1 (a, b, c) TO stdout;
|
|
|
|
2 3 2
|
|
|
|
4 1 4
|
|
|
|
-- subset of columns
|
|
|
|
COPY rls_t1 (a) TO stdout;
|
|
|
|
2
|
|
|
|
4
|
|
|
|
COPY rls_t1 (a, b) TO stdout;
|
|
|
|
2 3
|
|
|
|
4 1
|
|
|
|
-- column reordering
|
|
|
|
COPY rls_t1 (b, a) TO stdout;
|
|
|
|
3 2
|
|
|
|
1 4
|
|
|
|
RESET SESSION AUTHORIZATION;
|
|
|
|
SET SESSION AUTHORIZATION regress_rls_copy_user_colperms;
|
|
|
|
-- attempt all columns (should fail)
|
|
|
|
COPY rls_t1 TO stdout;
|
2017-12-02 15:26:34 +01:00
|
|
|
ERROR: permission denied for table rls_t1
|
2016-10-03 22:22:57 +02:00
|
|
|
COPY rls_t1 (a, b, c) TO stdout;
|
2017-12-02 15:26:34 +01:00
|
|
|
ERROR: permission denied for table rls_t1
|
2016-10-03 22:22:57 +02:00
|
|
|
-- try to copy column with no privileges (should fail)
|
|
|
|
COPY rls_t1 (c) TO stdout;
|
2017-12-02 15:26:34 +01:00
|
|
|
ERROR: permission denied for table rls_t1
|
2016-10-03 22:22:57 +02:00
|
|
|
-- subset of columns (should succeed)
|
|
|
|
COPY rls_t1 (a) TO stdout;
|
|
|
|
2
|
|
|
|
4
|
|
|
|
COPY rls_t1 (a, b) TO stdout;
|
|
|
|
2 3
|
|
|
|
4 1
|
|
|
|
RESET SESSION AUTHORIZATION;
|
2016-11-10 20:13:43 +01:00
|
|
|
-- test with INSTEAD OF INSERT trigger on a view
|
|
|
|
CREATE TABLE instead_of_insert_tbl(id serial, name text);
|
|
|
|
CREATE VIEW instead_of_insert_tbl_view AS SELECT ''::text AS str;
|
|
|
|
COPY instead_of_insert_tbl_view FROM stdin; -- fail
|
|
|
|
ERROR: cannot copy to view "instead_of_insert_tbl_view"
|
|
|
|
HINT: To enable copying to a view, provide an INSTEAD OF INSERT trigger.
|
|
|
|
CREATE FUNCTION fun_instead_of_insert_tbl() RETURNS trigger AS $$
|
|
|
|
BEGIN
|
|
|
|
INSERT INTO instead_of_insert_tbl (name) VALUES (NEW.str);
|
|
|
|
RETURN NULL;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE TRIGGER trig_instead_of_insert_tbl_view
|
|
|
|
INSTEAD OF INSERT ON instead_of_insert_tbl_view
|
|
|
|
FOR EACH ROW EXECUTE PROCEDURE fun_instead_of_insert_tbl();
|
|
|
|
COPY instead_of_insert_tbl_view FROM stdin;
|
|
|
|
SELECT * FROM instead_of_insert_tbl;
|
|
|
|
id | name
|
|
|
|
----+-------
|
|
|
|
1 | test1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- clean up
|
2014-03-04 23:31:59 +01:00
|
|
|
DROP TABLE forcetest;
|
2012-12-01 13:54:20 +01:00
|
|
|
DROP TABLE vistest;
|
2012-12-01 14:46:41 +01:00
|
|
|
DROP FUNCTION truncate_in_subxact();
|
2004-06-10 19:56:03 +02:00
|
|
|
DROP TABLE x, y;
|
2016-10-03 22:22:57 +02:00
|
|
|
DROP TABLE rls_t1 CASCADE;
|
|
|
|
DROP ROLE regress_rls_copy_user;
|
|
|
|
DROP ROLE regress_rls_copy_user_colperms;
|
2002-07-18 06:45:51 +02:00
|
|
|
DROP FUNCTION fn_x_before();
|
|
|
|
DROP FUNCTION fn_x_after();
|
2016-11-10 20:13:43 +01:00
|
|
|
DROP TABLE instead_of_insert_tbl;
|
|
|
|
DROP VIEW instead_of_insert_tbl_view;
|
|
|
|
DROP FUNCTION fun_instead_of_insert_tbl();
|