I have two databases (postgresql 14): let's call them master
and slave
.
Let's create table in slave
db:
create table sample_slave(
id int,
name varchar,
location varchar
);
Then in master
db:
CREATE TABLE sample(
id serial primary key,
name varchar,
location varchar
);
CREATE TABLE sample_master(check(location='master')) inherits(sample);
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER slave_server FOREIGN DATA WRAPPER postgres_fdw options (dbname '...', host '...', port '5432', sslcert '...', sslkey '...', sslrootcert '...');
CREATE USER mapping for test_user SERVER slave_server options (user '...', password '...');
CREATE FOREIGN TABLE sample_slave (check(location='slave')) inherits(sample) SERVER slave_server;
create or replace function sample_trigger_fn() returns trigger as
$$
begin
if new.location = 'master' then
insert into public."sample_master" values(new.*);
elsif new.location = 'slave' then
insert into public."sample_slave" values(new.*);
end if;
return null;
end
$$
language plpgsql;
create trigger sample_trigger before insert on public."sample" for each row execute procedure public.sample_trigger_fn();
So, we have simple partitioned table sample
. If we execute query in 'master':
SELECT * FROM sample;
we have all data from 'master' and 'slave' db. Its all works fine if all connections are stable, but if connection with 'slave' db is lost, 'select' will return error.
I want to do, that when the connection is lost, the 'master' db does not throw an error, but returns all available records (except for the part that is unavailable).
Is it possible to do that in Postgres?
Thanks