CREATE TEMP TABLE x (
a serial,
b int,
c text notnulldefault'stuff',
d text,
e text
) ;
CREATE FUNCTION fn_x_before () RETURNS TRIGGERAS'
BEGIN
NEW.e := ''beforetrigger fired''::text; return NEW;
END; ' LANGUAGE plpgsql;
CREATE FUNCTION fn_x_after () RETURNS TRIGGERAS'
BEGIN UPDATE x set e=''after trigger fired''where c=''stuff''; returnNULL;
END; ' LANGUAGE plpgsql;
CREATETRIGGER trg_x_after AFTER INSERTON x FOREACH ROW EXECUTE PROCEDURE fn_x_after();
CREATETRIGGER trg_x_before BEFOREINSERTON x FOREACH ROW EXECUTE PROCEDURE fn_x_before();
COPY x (a, b, c, d, e) from stdin; 9999 \N \\N \NN \N 1000021314151
\.
COPY x (b, d) from stdin; 1 test_1
\.
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; 1000122324252 1000223334353 1000324344454 1000425354555 1000526364656
\.
-- non-existent column in column list: should fail
COPY x (xyz) from stdin;
-- redundant options
COPY x from stdin (format CSV, FORMAT CSV);
COPY x from stdin (freeze off, freeze on);
COPY x from stdin (delimiter ',', delimiter ',');
COPY x from stdin (null' ', null' ');
COPY x from stdin (header off, header on);
COPY x from stdin (quote ':', quote ':');
COPY x from stdin (escape ':', escape ':');
COPY x from stdin (force_quote (a), force_quote *);
COPY x from stdin (force_not_null (a), force_not_null (b));
COPY x from stdin (force_null (a), force_null (b));
COPY x from stdin (convert_selectively (a), convert_selectively (b));
COPY x from stdin (encoding 'sql_ascii', encoding 'sql_ascii');
COPY x from stdin (on_error ignore, on_error ignore);
COPY x from stdin (log_verbosity default, log_verbosity verbose);
-- incorrect options
COPY x from stdin (format BINARY, delimiter ',');
COPY x from stdin (format BINARY, null'x');
COPY x from stdin (format BINARY, on_error ignore);
COPY x from stdin (on_error unsupported);
COPY x from stdin (format TEXT, force_quote(a));
COPY x from stdin (format TEXT, force_quote *);
COPY x from stdin (format CSV, force_quote(a));
COPY x from stdin (format CSV, force_quote *);
COPY x from stdin (format TEXT, force_not_null(a));
COPY x from stdin (format TEXT, force_not_null *);
COPY x to stdout (format CSV, force_not_null(a));
COPY x to stdout (format CSV, force_not_null *);
COPY x from stdin (format TEXT, force_null(a));
COPY x from stdin (format TEXT, force_null *);
COPY x to stdout (format CSV, force_null(a));
COPY x to stdout (format CSV, force_null *);
COPY x to stdout (format BINARY, on_error unsupported);
COPY x from stdin (log_verbosity unsupported);
COPY x from stdin with (reject_limit 1);
COPY x from stdin with (on_error ignore, reject_limit 0);
-- 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; 20002302323
\.
COPY x from stdin; 2001231 \N \N
\.
-- extra data: should fail
COPY x from stdin; 20022324050607080
\.
-- various COPY options: delimiters, oids, NULL string, encoding
COPY x (b, c, d, e) from stdin delimiter ','null'x';
x,45,80,90
x,\x,\\x,\\\x
x,\,,\\\,,\\
\.
COPY x from stdin WITH DELIMITER AS';'NULLAS''; 3000;;c;;
\.
COPY x from stdin WITH DELIMITER AS':'NULLAS E'\\X' ENCODING 'sql_ascii'; 4000:\X:C:\X:\X 4001:1:empty:: 4002:2:null:\X:\X 4003:3:Backslash:\\:\\ 4004:4:BackslashX:\\X:\\X 4005:5:N:\N:\N 4006:6:BackslashN:\\N:\\N 4007:7:XX:\XX:\XX 4008:8:Delimiter:\::\:
\.
COPY x TO stdout WHERE a = 1;
COPY x from stdin WHERE a = 50004; 5000324344454 5000425354555 5000526364656
\.
COPY x from stdin WHERE a > 60003; 6000122324252 6000223334353 6000324344454 6000425354555 6000526364656
\.
COPY x from stdin WHERE f > 60003;
COPY x from stdin WHERE a = max(x.b);
COPY x from stdin WHERE a IN (SELECT1FROM x);
COPY x from stdin WHERE a IN (generate_series(1,5));
COPY x from stdin WHERE a = row_number() over(b);
-- check results of copy in SELECT * FROM x;
-- check copy out
COPY x TO stdout;
COPY x (c, e) TO stdout;
COPY x (b, e) TO stdout WITHNULL'I''m null';
CREATE TEMP TABLE y (
col1 text,
col2 text
);
INSERTINTO y VALUES ('Jackson, Sam', E'\\h'); INSERTINTO y VALUES ('It is "perfect".',E'\t'); INSERTINTO y VALUES ('', NULL);
COPY y TO stdout WITH CSV;
COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|';
COPY y TO stdout WITH CSV FORCE QUOTE col2 ESCAPE E'\\' ENCODING 'sql_ascii';
COPY y TO stdout WITH CSV FORCE QUOTE *;
-- Repeat above tests with new 9.0 option syntax
COPY y TO stdout (FORMAT CSV);
COPY y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|');
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\');
COPY y TO stdout (FORMAT CSV, FORCE_QUOTE *);
\copy y TO stdout (FORMAT CSV)
\copy y TO stdout (FORMAT CSV, QUOTE '''', DELIMITER '|')
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE (col2), ESCAPE E'\\')
\copy y TO stdout (FORMAT CSV, FORCE_QUOTE *)
--test that we read consecutive LFs properly
CREATE TEMP TABLE testnl (a int, b text, c int);
COPY testnl FROM stdin CSV; 1,"a field with two LFs
inside",2
\.
-- test end of copy marker CREATE TEMP TABLE testeoc (a text);
COPY testeoc FROM stdin CSV;
a\.
\.b
c\.d "\."
\.
COPY testeoc TO stdout CSV;
-- test handling of nonstandard null marker that violates escaping rules
BEGIN; CREATETABLE vistest (LIKE testeoc);
COPY vistest FROM stdin CSV;
a0
b
\. COMMIT; SELECT * FROM vistest;
BEGIN;
TRUNCATE vistest;
COPY vistest FROM stdin CSV;
a1
b
\. SELECT * FROM vistest;
SAVEPOINT s1;
TRUNCATE vistest;
COPY vistest FROM stdin CSV;
d1
e
\. SELECT * FROM vistest; COMMIT; SELECT * FROM vistest;
BEGIN;
TRUNCATE vistest;
COPY vistest FROM stdin CSV FREEZE;
a2
b
\. SELECT * FROM vistest;
SAVEPOINT s1;
TRUNCATE vistest;
COPY vistest FROM stdin CSV FREEZE;
d2
e
\. SELECT * FROM vistest; COMMIT; SELECT * FROM vistest;
BEGIN;
TRUNCATE vistest;
COPY vistest FROM stdin CSV FREEZE;
x
y
\. SELECT * FROM vistest; COMMIT;
TRUNCATE vistest;
COPY vistest FROM stdin CSV FREEZE;
p
g
\.
BEGIN;
TRUNCATE vistest;
SAVEPOINT s1;
COPY vistest FROM stdin CSV FREEZE;
m
k
\. COMMIT;
BEGIN; INSERTINTO vistest VALUES ('z');
SAVEPOINT s1;
TRUNCATE vistest;
ROLLBACK TO SAVEPOINT s1;
COPY vistest FROM stdin CSV FREEZE;
d3
e
\. COMMIT; CREATE FUNCTION truncate_in_subxact() RETURNS VOID AS
$$
BEGIN
TRUNCATE vistest;
EXCEPTION WHEN OTHERS THEN INSERTINTO vistest VALUES ('subxact failure');
END;
$$ language plpgsql;
BEGIN; INSERTINTO vistest VALUES ('z'); SELECT truncate_in_subxact();
COPY vistest FROM stdin CSV FREEZE;
d4
e
\. SELECT * FROM vistest; COMMIT; SELECT * FROM vistest; -- Test FORCE_NOT_NULL and FORCE_NULL options CREATE TEMP TABLE forcetest (
a INTNOTNULL,
b TEXT NOTNULL,
c TEXT,
d TEXT,
e TEXT
);
\pset nullNULL -- should succeed with no effect ("b" remains an empty string, "c" remains NULL)
BEGIN;
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(b), FORCE_NULL(c)); 1,,""
\. COMMIT; SELECT b, c FROM forcetest WHERE a = 1; -- should succeed, FORCE_NULL and FORCE_NOT_NULL can be both specified
BEGIN;
COPY forcetest (a, b, c, d) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(c,d), FORCE_NULL(c,d)); 2,'a',,""
\. COMMIT; SELECT c, d FROM forcetest WHERE a = 2; -- 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)); 3,,""
\.
ROLLBACK; -- should fail with "not referenced by COPY" error
BEGIN;
COPY forcetest (d, e) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(b));
ROLLBACK; -- should fail with "not referenced by COPY" error
BEGIN;
COPY forcetest (d, e) FROM STDIN WITH (FORMAT csv, FORCE_NULL(b));
ROLLBACK; -- should succeed with no effect ("b" remains an empty string, "c" remains NULL)
BEGIN;
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL *, FORCE_NULL *); 4,,""
\. COMMIT; SELECT b, c FROM forcetest WHERE a = 4; -- should succeed with effect ("b" remains an empty string)
BEGIN;
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL *); 5,,""
\. COMMIT; SELECT b, c FROM forcetest WHERE a = 5; -- should succeed with effect ("c" remains NULL)
BEGIN;
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NULL *); 6,"b",""
\. COMMIT; SELECT b, c FROM forcetest WHERE a = 6; -- should fail with "conflicting or redundant options" error
BEGIN;
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL *, FORCE_NOT_NULL(b));
ROLLBACK; -- should fail with "conflicting or redundant options" error
BEGIN;
COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv, FORCE_NULL *, FORCE_NULL(b));
ROLLBACK;
\pset null''
-- test case with whole-row Var in a check constraint createtable 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; altertable check_con_tbl addcheck (check_con_function(check_con_tbl.*));
\d+ check_con_tbl
copy check_con_tbl from stdin; 1
\N
\.
copy check_con_tbl from stdin; 0
\. select * from check_con_tbl;
-- test with RLS enabled. CREATE ROLE regress_rls_copy_user; CREATE ROLE regress_rls_copy_user_colperms; CREATETABLE rls_t1 (a int, b int, c int);
COPY rls_t1 (a, b, c) from stdin; 141 232 323 414
\.
CREATE POLICY p1 ON rls_t1 FORSELECTUSING (a % 2 = 0); ALTERTABLE rls_t1 ENABLE ROW LEVEL SECURITY; ALTERTABLE rls_t1 FORCE ROW LEVEL SECURITY;
GRANTSELECTONTABLE rls_t1 TO regress_rls_copy_user; GRANTSELECT (a, b) ONTABLE rls_t1 TO regress_rls_copy_user_colperms;
-- all columns
COPY rls_t1 TO stdout;
COPY rls_t1 (a, b, c) TO stdout;
-- subset of columns
COPY rls_t1 (a) TO stdout;
COPY rls_t1 (a, b) TO stdout;
-- column reordering
COPY rls_t1 (b, a) TO stdout;
SET SESSION AUTHORIZATION regress_rls_copy_user;
-- all columns
COPY rls_t1 TO stdout;
COPY rls_t1 (a, b, c) TO stdout;
-- subset of columns
COPY rls_t1 (a) TO stdout;
COPY rls_t1 (a, b) TO stdout;
-- column reordering
COPY rls_t1 (b, a) TO stdout;
RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION regress_rls_copy_user_colperms;
-- attempt all columns (should fail)
COPY rls_t1 TO stdout;
COPY rls_t1 (a, b, c) TO stdout;
-- try to copy column with no privileges (should fail)
COPY rls_t1 (c) TO stdout;
-- subset of columns (should succeed)
COPY rls_t1 (a) TO stdout;
COPY rls_t1 (a, b) TO stdout;
RESET SESSION AUTHORIZATION;
-- test with INSTEAD OF INSERT trigger on a view CREATETABLE instead_of_insert_tbl(id serial, name text); CREATE VIEW instead_of_insert_tbl_view ASSELECT''::text AS str;
COPY instead_of_insert_tbl_view FROM stdin; -- fail
test1
\.
CREATE FUNCTION fun_instead_of_insert_tbl() RETURNS triggerAS $$
BEGIN INSERTINTO instead_of_insert_tbl (name) VALUES (NEW.str); RETURNNULL;
END;
$$ LANGUAGE plpgsql; CREATETRIGGER trig_instead_of_insert_tbl_view
INSTEAD OF INSERTON instead_of_insert_tbl_view FOREACH ROW EXECUTE PROCEDURE fun_instead_of_insert_tbl();
COPY instead_of_insert_tbl_view FROM stdin;
test1
\.
SELECT * FROM instead_of_insert_tbl;
-- Test of COPY optimization with view using INSTEAD OF INSERT -- trigger when relation is created in the same transaction as -- when COPY is executed.
BEGIN; CREATE VIEW instead_of_insert_tbl_view_2 asselect''::text as str; CREATETRIGGER trig_instead_of_insert_tbl_view_2
INSTEAD OF INSERTON instead_of_insert_tbl_view_2 FOREACH ROW EXECUTE PROCEDURE fun_instead_of_insert_tbl();
COPY instead_of_insert_tbl_view_2 FROM stdin;
test1
\.
SELECT * FROM instead_of_insert_tbl; COMMIT;
-- tests for on_error option CREATETABLE check_ign_err (n int, m int[], k int);
COPY check_ign_err FROM STDIN WITH (on_error stop); 1 {1} 1
a {2} 2 3 {3} 3333333333 4 {a, 4} 4
5 {5} 5
\.
-- want context for notices
\set SHOW_CONTEXT always
COPY check_ign_err FROM STDIN WITH (on_error ignore, log_verbosity verbose); 1 {1} 1
a {2} 2 3 {3} 3333333333 4 {a, 4} 4
5 {5} 5 6 a 7 {7} a 8 {8} 8
\.
-- tests for on_error option with log_verbosity and null constraint via domain CREATE DOMAIN dcheck_ign_err2 varchar(15) NOTNULL; CREATETABLE check_ign_err2 (n int, m int[], k int, l dcheck_ign_err2);
COPY check_ign_err2 FROM STDIN WITH (on_error ignore, log_verbosity verbose); 1 {1} 1'foo' 2 {2} 2 \N
\.
COPY check_ign_err2 FROM STDIN WITH (on_error ignore, log_verbosity silent); 3 {3} 3'bar' 4 {4} 4 \N
\.
-- reset context choice
\set SHOW_CONTEXT errors
SELECT * FROM check_ign_err;
SELECT * FROM check_ign_err2;
-- test datatype error that can't be handled as soft: should fail CREATETABLE hard_err(foo widget);
COPY hard_err FROM STDIN WITH (on_error ignore); 1
\.
-- test missing data: should fail
COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1}
\.
-- test extra data: should fail
COPY check_ign_err FROM STDIN WITH (on_error ignore); 1 {1} 3 abc
\.
-- tests for reject_limit option
COPY check_ign_err FROM STDIN WITH (on_error ignore, reject_limit 3); 6 {6} 6
a {7} 7 8 {8} 8888888888 9 {a, 9} 9
10 {10} 10
\.
COPY check_ign_err FROM STDIN WITH (on_error ignore, reject_limit 4); 6 {6} 6
a {7} 7 8 {8} 8888888888 9 {a, 9} 9
10 {10} 10
\.
-- clean up DROPTABLE forcetest; DROPTABLE vistest; DROP FUNCTION truncate_in_subxact(); DROPTABLE x, y; DROPTABLE rls_t1 CASCADE; DROP ROLE regress_rls_copy_user; DROP ROLE regress_rls_copy_user_colperms; DROP FUNCTION fn_x_before(); DROP FUNCTION fn_x_after(); DROPTABLE instead_of_insert_tbl; DROP VIEW instead_of_insert_tbl_view; DROP VIEW instead_of_insert_tbl_view_2; DROP FUNCTION fun_instead_of_insert_tbl(); DROPTABLE check_ign_err; DROPTABLE check_ign_err2; DROP DOMAIN dcheck_ign_err2; DROPTABLE hard_err;
-- -- COPY FROM ... DEFAULT --
create temp table copy_default (
id integerprimarykey,
text_value text notnulldefault'test',
ts_value timestamp without time zone notnulldefault'2022-07-05'
);
-- if DEFAULT is not specified, then the marker will be regular data
copy copy_default from stdin; 1 value '2022-07-04' 2 \D '2022-07-05'
\.
select id, text_value, ts_value from copy_default;
truncate copy_default;
copy copy_default from stdin with (format csv); 1,value,2022-07-04 2,\D,2022-07-05
\.
select id, text_value, ts_value from copy_default;
truncate copy_default;
-- DEFAULT cannot be used in binary mode
copy copy_default from stdin with (format binary, default'\D');
-- DEFAULT cannot be new line nor carriage return
copy copy_default from stdin with (default E'\n');
copy copy_default from stdin with (default E'\r');
-- DELIMITER cannot appear in DEFAULT spec
copy copy_default from stdin with (delimiter ';', default'test;test');
-- CSV quote cannot appear in DEFAULT spec
copy copy_default from stdin with (format csv, quote '"', default'test"test');
-- NULL and DEFAULT spec must be different
copy copy_default from stdin with (default'\N');
-- cannot use DEFAULT marker in column that has no DEFAULT value
copy copy_default from stdin with (default'\D');
\D value '2022-07-04' 2 \D '2022-07-05'
\.
copy copy_default from stdin with (format csv, default'\D');
\D,value,2022-07-04 2,\D,2022-07-05
\.
-- The DEFAULT marker must be unquoted and unescaped or it's not recognized
copy copy_default from stdin with (default'\D'); 1 \D '2022-07-04' 2 \\D '2022-07-04' 3"\D"'2022-07-04'
\.
select id, text_value, ts_value from copy_default;
truncate copy_default;
copy copy_default from stdin with (format csv, default'\D'); 1,\D,2022-07-04 2,\\D,2022-07-04 3,"\D",2022-07-04
\.
select id, text_value, ts_value from copy_default;
truncate copy_default;
-- successful usage of DEFAULT option in COPY
copy copy_default from stdin with (default'\D'); 1 value '2022-07-04' 2 \D '2022-07-03' 3 \D \D
\.
select id, text_value, ts_value from copy_default;
truncate copy_default;
copy copy_default from stdin with (format csv, default'\D'); 1,value,2022-07-04 2,\D,2022-07-03 3,\D,\D
\.
select id, text_value, ts_value from copy_default;
truncate copy_default;
-- DEFAULT cannot be used in COPY TO
copy (select1as test) TO stdout with (default'\D');
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.14Bemerkung:
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.