From 15f69279e0e85bc4b3cf9593ad090515e500613c Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Sun, 23 Dec 2018 16:43:56 +0900 Subject: [PATCH] Disable WAL-skipping optimization for COPY on views COPY can skip writing WAL when loading data on a table which has been created in the same transaction as the one loading the data, however this cannot work on views as this would result in trying to flush relation files which do not exist. So disable the optimization so as commands are able to work the same way with any configuration of wal_level. A test is added to cover this case, which needs to have wal_level set to minimal to allow the problem to show up, and that is not the default configuration. Reported-by: Etsuro Fujita Author: Michael Paquier Reviewed-by: Etsuro Fujita Discussion: https://postgr.es/m/15552-c64aa14c5c22f63c@postgresql.org Backpatch-through: 10, where support for COPY on views has been added, while v11 has added support for COPY on foreign tables. --- src/backend/commands/copy.c | 5 +++++ src/test/regress/expected/copy2.out | 18 ++++++++++++++++++ src/test/regress/sql/copy2.sql | 16 ++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 226d2ef3dac..a5528f8307f 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -2408,10 +2408,15 @@ CopyFrom(CopyState cstate) * possible to improve on this, but it does mean maintaining heap insert * option flags per partition and setting them when we first open the * partition. + * + * This optimization is not supported for relation types which do not + * have any physical storage, with views entering in this category. + * Partitioned tables are not supported as per the description above. *---------- */ /* createSubid is creation check, newRelfilenodeSubid is truncation check */ if (cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE && + cstate->rel->rd_rel->relkind != RELKIND_VIEW && (cstate->rel->rd_createSubid != InvalidSubTransactionId || cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId)) { diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index 65e9c626b35..f11eef3277b 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -557,6 +557,23 @@ SELECT * FROM instead_of_insert_tbl; 1 | test1 (1 row) +-- 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 as select ''::text as str; +CREATE TRIGGER trig_instead_of_insert_tbl_view_2 + INSTEAD OF INSERT ON instead_of_insert_tbl_view_2 + FOR EACH ROW EXECUTE PROCEDURE fun_instead_of_insert_tbl(); +COPY instead_of_insert_tbl_view_2 FROM stdin; +SELECT * FROM instead_of_insert_tbl; + id | name +----+------- + 1 | test1 + 2 | test1 +(2 rows) + +COMMIT; -- clean up DROP TABLE forcetest; DROP TABLE vistest; @@ -569,4 +586,5 @@ DROP FUNCTION fn_x_before(); DROP FUNCTION fn_x_after(); DROP TABLE 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(); diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index f3a6d228fae..3022a07d3c2 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -411,6 +411,21 @@ 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 as select ''::text as str; +CREATE TRIGGER trig_instead_of_insert_tbl_view_2 + INSTEAD OF INSERT ON instead_of_insert_tbl_view_2 + FOR EACH 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; -- clean up DROP TABLE forcetest; @@ -424,4 +439,5 @@ DROP FUNCTION fn_x_before(); DROP FUNCTION fn_x_after(); DROP TABLE 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();