Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff9c222

Browse files
committed
Disable WAL-skipping optimization for COPY on views and foreign tables
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 or foreign table 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. Tests are added to cover the different cases, which need to have wal_level set to minimal to allow the problem to show up, and that is not the default configuration. Reported-by: Luis M. Carril, Etsuro Fujita Author: Amit Langote, 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 018923c commit ff9c222

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7883,6 +7883,22 @@ drop trigger rem2_trig_row_before on rem2;
78837883
drop trigger rem2_trig_row_after on rem2;
78847884
drop trigger loc2_trig_row_before_insert on loc2;
78857885
delete from rem2;
7886+
-- test COPY FROM with foreign table created in the same transaction
7887+
create table loc3 (f1 int, f2 text);
7888+
begin;
7889+
create foreign table rem3 (f1 int, f2 text)
7890+
server loopback options(table_name 'loc3');
7891+
copy rem3 from stdin;
7892+
commit;
7893+
select * from rem3;
7894+
f1 | f2
7895+
----+-----
7896+
1 | foo
7897+
2 | bar
7898+
(2 rows)
7899+
7900+
drop foreign table rem3;
7901+
drop table loc3;
78867902
-- ===================================================================
78877903
-- test IMPORT FOREIGN SCHEMA
78887904
-- ===================================================================

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,20 @@ drop trigger loc2_trig_row_before_insert on loc2;
20852085

20862086
delete from rem2;
20872087

2088+
-- test COPY FROM with foreign table created in the same transaction
2089+
create table loc3 (f1 int, f2 text);
2090+
begin;
2091+
create foreign table rem3 (f1 int, f2 text)
2092+
server loopback options(table_name 'loc3');
2093+
copy rem3 from stdin;
2094+
1 foo
2095+
2 bar
2096+
\.
2097+
commit;
2098+
select * from rem3;
2099+
drop foreign table rem3;
2100+
drop table loc3;
2101+
20882102
-- ===================================================================
20892103
-- test IMPORT FOREIGN SCHEMA
20902104
-- ===================================================================

src/backend/commands/copy.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2423,10 +2423,17 @@ CopyFrom(CopyState cstate)
24232423
* possible to improve on this, but it does mean maintaining heap insert
24242424
* option flags per partition and setting them when we first open the
24252425
* partition.
2426+
*
2427+
* This optimization is not supported for relation types which do not
2428+
* have any physical storage, with foreign tables and views using
2429+
* INSTEAD OF triggers entering in this category. Partitioned tables
2430+
* are not supported as per the description above.
24262431
*----------
24272432
*/
24282433
/* createSubid is creation check, newRelfilenodeSubid is truncation check */
2429-
if (cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2434+
if (cstate->rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
2435+
cstate->rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
2436+
cstate->rel->rd_rel->relkind != RELKIND_VIEW &&
24302437
(cstate->rel->rd_createSubid != InvalidSubTransactionId ||
24312438
cstate->rel->rd_newRelfilenodeSubid != InvalidSubTransactionId))
24322439
{

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)