Skip to content

Commit 15f6927

Browse files
committed
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.
1 parent 669d9ea commit 15f6927

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/backend/commands/copy.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,10 +2408,15 @@ CopyFrom(CopyState cstate)
24082408
* possible to improve on this, but it does mean maintaining heap insert
24092409
* option flags per partition and setting them when we first open the
24102410
* partition.
2411+
*
2412+
* This optimization is not supported for relation types which do not
2413+
* have any physical storage, with views entering in this category.
2414+
* Partitioned tables are not supported as per the description above.
24112415
*----------
24122416
*/
24132417
/* createSubid is creation check, newRelfilenodeSubid is truncation check */
24142418
if (cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2419+
cstate->rel->rd_rel->relkind != RELKIND_VIEW &&
24152420
(cstate->rel->rd_createSubid != InvalidSubTransactionId ||
24162421
cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId))
24172422
{

src/test/regress/expected/copy2.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,23 @@ SELECT * FROM instead_of_insert_tbl;
557557
1 | test1
558558
(1 row)
559559

560+
-- Test of COPY optimization with view using INSTEAD OF INSERT
561+
-- trigger when relation is created in the same transaction as
562+
-- when COPY is executed.
563+
BEGIN;
564+
CREATE VIEW instead_of_insert_tbl_view_2 as select ''::text as str;
565+
CREATE TRIGGER trig_instead_of_insert_tbl_view_2
566+
INSTEAD OF INSERT ON instead_of_insert_tbl_view_2
567+
FOR EACH ROW EXECUTE PROCEDURE fun_instead_of_insert_tbl();
568+
COPY instead_of_insert_tbl_view_2 FROM stdin;
569+
SELECT * FROM instead_of_insert_tbl;
570+
id | name
571+
----+-------
572+
1 | test1
573+
2 | test1
574+
(2 rows)
575+
576+
COMMIT;
560577
-- clean up
561578
DROP TABLE forcetest;
562579
DROP TABLE vistest;
@@ -569,4 +586,5 @@ DROP FUNCTION fn_x_before();
569586
DROP FUNCTION fn_x_after();
570587
DROP TABLE instead_of_insert_tbl;
571588
DROP VIEW instead_of_insert_tbl_view;
589+
DROP VIEW instead_of_insert_tbl_view_2;
572590
DROP FUNCTION fun_instead_of_insert_tbl();

src/test/regress/sql/copy2.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,21 @@ test1
411411

412412
SELECT * FROM instead_of_insert_tbl;
413413

414+
-- Test of COPY optimization with view using INSTEAD OF INSERT
415+
-- trigger when relation is created in the same transaction as
416+
-- when COPY is executed.
417+
BEGIN;
418+
CREATE VIEW instead_of_insert_tbl_view_2 as select ''::text as str;
419+
CREATE TRIGGER trig_instead_of_insert_tbl_view_2
420+
INSTEAD OF INSERT ON instead_of_insert_tbl_view_2
421+
FOR EACH ROW EXECUTE PROCEDURE fun_instead_of_insert_tbl();
422+
423+
COPY instead_of_insert_tbl_view_2 FROM stdin;
424+
test1
425+
\.
426+
427+
SELECT * FROM instead_of_insert_tbl;
428+
COMMIT;
414429

415430
-- clean up
416431
DROP TABLE forcetest;
@@ -424,4 +439,5 @@ DROP FUNCTION fn_x_before();
424439
DROP FUNCTION fn_x_after();
425440
DROP TABLE instead_of_insert_tbl;
426441
DROP VIEW instead_of_insert_tbl_view;
442+
DROP VIEW instead_of_insert_tbl_view_2;
427443
DROP FUNCTION fun_instead_of_insert_tbl();

0 commit comments

Comments
 (0)