mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Allow insert and update tuple routing and COPY for foreign tables.
Also enable this for postgres_fdw. Etsuro Fujita, based on an earlier patch by Amit Langote. The larger patch series of which this is a part has been reviewed by Amit Langote, David Fetter, Maksim Milyutin, Álvaro Herrera, Stephen Frost, and me. Minor documentation changes to the final version by me. Discussion: http://postgr.es/m/29906a26-da12-8c86-4fb9-d8f88442f2b9@lab.ntt.co.jp
This commit is contained in:
@ -1767,6 +1767,243 @@ drop table bar cascade;
|
||||
drop table loct1;
|
||||
drop table loct2;
|
||||
|
||||
-- ===================================================================
|
||||
-- test tuple routing for foreign-table partitions
|
||||
-- ===================================================================
|
||||
|
||||
-- Test insert tuple routing
|
||||
create table itrtest (a int, b text) partition by list (a);
|
||||
create table loct1 (a int check (a in (1)), b text);
|
||||
create foreign table remp1 (a int check (a in (1)), b text) server loopback options (table_name 'loct1');
|
||||
create table loct2 (a int check (a in (2)), b text);
|
||||
create foreign table remp2 (b text, a int check (a in (2))) server loopback options (table_name 'loct2');
|
||||
alter table itrtest attach partition remp1 for values in (1);
|
||||
alter table itrtest attach partition remp2 for values in (2);
|
||||
|
||||
insert into itrtest values (1, 'foo');
|
||||
insert into itrtest values (1, 'bar') returning *;
|
||||
insert into itrtest values (2, 'baz');
|
||||
insert into itrtest values (2, 'qux') returning *;
|
||||
insert into itrtest values (1, 'test1'), (2, 'test2') returning *;
|
||||
|
||||
select tableoid::regclass, * FROM itrtest;
|
||||
select tableoid::regclass, * FROM remp1;
|
||||
select tableoid::regclass, * FROM remp2;
|
||||
|
||||
delete from itrtest;
|
||||
|
||||
create unique index loct1_idx on loct1 (a);
|
||||
|
||||
-- DO NOTHING without an inference specification is supported
|
||||
insert into itrtest values (1, 'foo') on conflict do nothing returning *;
|
||||
insert into itrtest values (1, 'foo') on conflict do nothing returning *;
|
||||
|
||||
-- But other cases are not supported
|
||||
insert into itrtest values (1, 'bar') on conflict (a) do nothing;
|
||||
insert into itrtest values (1, 'bar') on conflict (a) do update set b = excluded.b;
|
||||
|
||||
select tableoid::regclass, * FROM itrtest;
|
||||
|
||||
drop table itrtest;
|
||||
drop table loct1;
|
||||
drop table loct2;
|
||||
|
||||
-- Test update tuple routing
|
||||
create table utrtest (a int, b text) partition by list (a);
|
||||
create table loct (a int check (a in (1)), b text);
|
||||
create foreign table remp (a int check (a in (1)), b text) server loopback options (table_name 'loct');
|
||||
create table locp (a int check (a in (2)), b text);
|
||||
alter table utrtest attach partition remp for values in (1);
|
||||
alter table utrtest attach partition locp for values in (2);
|
||||
|
||||
insert into utrtest values (1, 'foo');
|
||||
insert into utrtest values (2, 'qux');
|
||||
|
||||
select tableoid::regclass, * FROM utrtest;
|
||||
select tableoid::regclass, * FROM remp;
|
||||
select tableoid::regclass, * FROM locp;
|
||||
|
||||
-- It's not allowed to move a row from a partition that is foreign to another
|
||||
update utrtest set a = 2 where b = 'foo' returning *;
|
||||
|
||||
-- But the reverse is allowed
|
||||
update utrtest set a = 1 where b = 'qux' returning *;
|
||||
|
||||
select tableoid::regclass, * FROM utrtest;
|
||||
select tableoid::regclass, * FROM remp;
|
||||
select tableoid::regclass, * FROM locp;
|
||||
|
||||
-- The executor should not let unexercised FDWs shut down
|
||||
update utrtest set a = 1 where b = 'foo';
|
||||
|
||||
drop table utrtest;
|
||||
drop table loct;
|
||||
|
||||
-- Test copy tuple routing
|
||||
create table ctrtest (a int, b text) partition by list (a);
|
||||
create table loct1 (a int check (a in (1)), b text);
|
||||
create foreign table remp1 (a int check (a in (1)), b text) server loopback options (table_name 'loct1');
|
||||
create table loct2 (a int check (a in (2)), b text);
|
||||
create foreign table remp2 (b text, a int check (a in (2))) server loopback options (table_name 'loct2');
|
||||
alter table ctrtest attach partition remp1 for values in (1);
|
||||
alter table ctrtest attach partition remp2 for values in (2);
|
||||
|
||||
copy ctrtest from stdin;
|
||||
1 foo
|
||||
2 qux
|
||||
\.
|
||||
|
||||
select tableoid::regclass, * FROM ctrtest;
|
||||
select tableoid::regclass, * FROM remp1;
|
||||
select tableoid::regclass, * FROM remp2;
|
||||
|
||||
-- Copying into foreign partitions directly should work as well
|
||||
copy remp1 from stdin;
|
||||
1 bar
|
||||
\.
|
||||
|
||||
select tableoid::regclass, * FROM remp1;
|
||||
|
||||
drop table ctrtest;
|
||||
drop table loct1;
|
||||
drop table loct2;
|
||||
|
||||
-- ===================================================================
|
||||
-- test COPY FROM
|
||||
-- ===================================================================
|
||||
|
||||
create table loc2 (f1 int, f2 text);
|
||||
alter table loc2 set (autovacuum_enabled = 'false');
|
||||
create foreign table rem2 (f1 int, f2 text) server loopback options(table_name 'loc2');
|
||||
|
||||
-- Test basic functionality
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
-- Test check constraints
|
||||
alter table loc2 add constraint loc2_f1positive check (f1 >= 0);
|
||||
alter foreign table rem2 add constraint rem2_f1positive check (f1 >= 0);
|
||||
|
||||
-- check constraint is enforced on the remote side, not locally
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
copy rem2 from stdin; -- ERROR
|
||||
-1 xyzzy
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
alter foreign table rem2 drop constraint rem2_f1positive;
|
||||
alter table loc2 drop constraint loc2_f1positive;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
-- Test local triggers
|
||||
create trigger trig_stmt_before before insert on rem2
|
||||
for each statement execute procedure trigger_func();
|
||||
create trigger trig_stmt_after after insert on rem2
|
||||
for each statement execute procedure trigger_func();
|
||||
create trigger trig_row_before before insert on rem2
|
||||
for each row execute procedure trigger_data(23,'skidoo');
|
||||
create trigger trig_row_after after insert on rem2
|
||||
for each row execute procedure trigger_data(23,'skidoo');
|
||||
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
drop trigger trig_row_before on rem2;
|
||||
drop trigger trig_row_after on rem2;
|
||||
drop trigger trig_stmt_before on rem2;
|
||||
drop trigger trig_stmt_after on rem2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
create trigger trig_row_before_insert before insert on rem2
|
||||
for each row execute procedure trig_row_before_insupdate();
|
||||
|
||||
-- The new values are concatenated with ' triggered !'
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
drop trigger trig_row_before_insert on rem2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
create trigger trig_null before insert on rem2
|
||||
for each row execute procedure trig_null();
|
||||
|
||||
-- Nothing happens
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
drop trigger trig_null on rem2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
-- Test remote triggers
|
||||
create trigger trig_row_before_insert before insert on loc2
|
||||
for each row execute procedure trig_row_before_insupdate();
|
||||
|
||||
-- The new values are concatenated with ' triggered !'
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
drop trigger trig_row_before_insert on loc2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
create trigger trig_null before insert on loc2
|
||||
for each row execute procedure trig_null();
|
||||
|
||||
-- Nothing happens
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
drop trigger trig_null on loc2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
-- Test a combination of local and remote triggers
|
||||
create trigger rem2_trig_row_before before insert on rem2
|
||||
for each row execute procedure trigger_data(23,'skidoo');
|
||||
create trigger rem2_trig_row_after after insert on rem2
|
||||
for each row execute procedure trigger_data(23,'skidoo');
|
||||
create trigger loc2_trig_row_before_insert before insert on loc2
|
||||
for each row execute procedure trig_row_before_insupdate();
|
||||
|
||||
copy rem2 from stdin;
|
||||
1 foo
|
||||
2 bar
|
||||
\.
|
||||
select * from rem2;
|
||||
|
||||
drop trigger rem2_trig_row_before on rem2;
|
||||
drop trigger rem2_trig_row_after on rem2;
|
||||
drop trigger loc2_trig_row_before_insert on loc2;
|
||||
|
||||
delete from rem2;
|
||||
|
||||
-- ===================================================================
|
||||
-- test IMPORT FOREIGN SCHEMA
|
||||
-- ===================================================================
|
||||
|
Reference in New Issue
Block a user