-- create table S (id integer primary key, val text);
-- create table T (s integer, value text);
-- create table U (s integer references S(id), value text);

create or replace function
   check_T_fk() returns trigger
as $$
begin
    perform * from S where s.id = NEW.s;
    if (not found) then
        raise NOTICE 'Invalid T.s value';
        if (TG_OP = 'INSERT') then
            return null;
        end if;
        if (TG_OP = 'UPDATE') then
            return old;
        end if;
    else
        return new;
    end if;
end;
$$ language plpgsql;


create trigger T_fk_check
before insert or update on T
for each row execute
procedure check_T_fk();
