Commit 1858b105 authored by Michael Paquier's avatar Michael Paquier

Add tests for COPY in PL/pgSQL

This stresses the error handling of COPY inside SPI which does not
support the operation using stdin or stdout, and these scenarios were
not tested up to now.

Author: Mark Dilger
Discussion: https://postgr.es/m/a6e9b130-7fd5-387b-4ec5-89bda24373ab@gmail.com
parent aae50236
...@@ -32,8 +32,8 @@ DATA = plpgsql.control plpgsql--1.0.sql plpgsql--unpackaged--1.0.sql ...@@ -32,8 +32,8 @@ DATA = plpgsql.control plpgsql--1.0.sql plpgsql--unpackaged--1.0.sql
REGRESS_OPTS = --dbname=$(PL_TESTDB) REGRESS_OPTS = --dbname=$(PL_TESTDB)
REGRESS = plpgsql_call plpgsql_control plpgsql_domain plpgsql_record \ REGRESS = plpgsql_call plpgsql_control plpgsql_copy plpgsql_domain \
plpgsql_cache plpgsql_transaction plpgsql_trap \ plpgsql_record plpgsql_cache plpgsql_transaction plpgsql_trap \
plpgsql_trigger plpgsql_varprops plpgsql_trigger plpgsql_varprops
# where to find gen_keywordlist.pl and subsidiary files # where to find gen_keywordlist.pl and subsidiary files
......
CREATE TABLE copy1 (a int, b float);
-- COPY TO/FROM not authorized from client.
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 TO stdout;
END;
$$;
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 FROM stdin;
END;
$$;
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 TO stdout';
END;
$$;
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 FROM stdin';
END;
$$;
-- Valid cases
-- COPY FROM
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 FROM '@abs_srcdir@/data/copy1.data';
END;
$$;
SELECT * FROM copy1 ORDER BY 1;
TRUNCATE copy1;
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 FROM ''@abs_srcdir@/data/copy1.data''';
END;
$$;
SELECT * FROM copy1 ORDER BY 1;
-- COPY TO
-- Copy the data externally once, then process it back to the table.
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 TO '@abs_builddir@/results/copy1.data';
END;
$$;
TRUNCATE copy1;
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 FROM '@abs_builddir@/results/copy1.data';
END;
$$;
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 FROM ''@abs_builddir@/results/copy1.data''';
END;
$$;
SELECT * FROM copy1 ORDER BY 1;
DROP TABLE copy1;
CREATE TABLE copy1 (a int, b float);
-- COPY TO/FROM not authorized from client.
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 TO stdout;
END;
$$;
ERROR: cannot COPY to/from client in PL/pgSQL
CONTEXT: PL/pgSQL function inline_code_block line 3 at SQL statement
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 FROM stdin;
END;
$$;
ERROR: cannot COPY to/from client in PL/pgSQL
CONTEXT: PL/pgSQL function inline_code_block line 3 at SQL statement
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 TO stdout';
END;
$$;
ERROR: cannot COPY to/from client in PL/pgSQL
CONTEXT: PL/pgSQL function inline_code_block line 3 at EXECUTE
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 FROM stdin';
END;
$$;
ERROR: cannot COPY to/from client in PL/pgSQL
CONTEXT: PL/pgSQL function inline_code_block line 3 at EXECUTE
-- Valid cases
-- COPY FROM
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 FROM '@abs_builddir@/data/copy1.data';
END;
$$;
SELECT * FROM copy1 ORDER BY 1;
a | b
---+-----
1 | 1.1
2 | 2.2
3 | 3.3
(3 rows)
TRUNCATE copy1;
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 FROM ''@abs_builddir@/data/copy1.data''';
END;
$$;
SELECT * FROM copy1 ORDER BY 1;
a | b
---+-----
1 | 1.1
2 | 2.2
3 | 3.3
(3 rows)
-- COPY TO
-- Copy the data externally once, then process it back to the table.
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 TO '@abs_builddir@/results/copy1.data';
END;
$$;
TRUNCATE copy1;
DO LANGUAGE plpgsql $$
BEGIN
COPY copy1 FROM '@abs_builddir@/results/copy1.data';
END;
$$;
DO LANGUAGE plpgsql $$
BEGIN
EXECUTE 'COPY copy1 FROM ''@abs_builddir@/results/copy1.data''';
END;
$$;
SELECT * FROM copy1 ORDER BY 1;
a | b
---+-----
1 | 1.1
1 | 1.1
2 | 2.2
2 | 2.2
3 | 3.3
3 | 3.3
(6 rows)
DROP TABLE copy1;
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