drop schema test_foreign_inherit cascade;
create schema test_foreign_inherit;
set search_path = test_foreign_inherit;

create extension postgres_fdw;
CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (dbname 'test');
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;

create extension file_fdw;
CREATE SERVER f FOREIGN DATA WRAPPER file_fdw;

create table parent (
	a int,
	b text,
	c date,
	d path
);
insert into parent values (4, 'foo', current_date, '0,0,1,2');

-- regular child
create table tchild () inherits (parent);
insert into tchild values (5, 'oof', current_date - 2, '-1,-1,0,0');

-- cursor update works
begin;
declare c cursor for select 1 from parent where b = 'oof';
fetch from c;
explain analyze update parent set b = null where current of c;
select * from parent;
rollback;

-- "foreign" side
create table _child (
	c date,
	b text,
	extra numeric,
	d path,
	a int
);

-- Foreign child.  No column reorder.
create foreign table child (
	c date,
	b text,
	extra numeric,
	d path,
	a int
) SERVER LOOPBACK OPTIONS (table_name '_child');
alter table child inherit parent;
insert into child (a, b, c, d, extra)
	values (7, 'bar', current_date - 1, '5,5,9,9', 1.1);
\copy (select * from child) to /tmp/child.pgcopy

-- update works
select * from parent;
update parent set b = 'baz' where b = 'bar';

-- cursor update fails, because postgres_fdw doesn't handle WHERE CURRENT OF
begin;
declare c cursor for select 1 from child where b = 'baz';
fetch from c;
update child set b = 'bak' where current of c;
rollback;

-- cursor update fails, even though target isn't in the foreign table
begin;
declare c cursor for select 1 from parent where b = 'oof';
fetch from c;
update parent set b = null where current of c;
rollback;


-- Add a file_fdw child.  No reorder.
create foreign table fchild0 (
	c date,
	b text,
	extra numeric,
	d path,
	a int
) SERVER f OPTIONS (filename '/tmp/child.pgcopy');
alter table fchild0 inherit parent;

-- no more update, since file_fdw doesn't cooperate
select * from parent;
update parent set b = 'baz' where b = 'foo';

select * from child for update of child;
select * from fchild0 for update of fchild0;
select * from parent for update;

-- No LOCK TABLE for foreign tables, ...
begin; lock table child in exclusive mode; rollback;

-- ... but locking indirectly via a parent works.
begin;
select relation::regclass, mode from pg_locks
where mode = 'ExclusiveLock' and relation is not null order by 1;
lock table parent in exclusive mode;
select relation::regclass, mode from pg_locks
where mode = 'ExclusiveLock' and relation is not null order by 1;
rollback;

alter server loopback options (use_remote_estimate 'true');
select * from parent;

analyze verbose;
analyze verbose parent;

-- message correctly observes that foreign table is now allowed
create view v as select 1;
alter table v inherit parent;

-- for testing dump/reload of column-level options
create foreign table nonlocal () inherits (parent)
server loopback options (table_name '_child');
alter foreign table child alter c options (column_name 'c');
alter foreign table nonlocal alter c options (column_name 'c');

-- column reorder breaks the foreign table
create foreign table fchild1 (
	c date,
	b text,
	extra numeric,
	d path,
	a int
) inherits (parent) SERVER f OPTIONS (filename '/tmp/child.pgcopy');

select * from fchild1;
