I found a process that let you keep the remote schema in-sync, It only solves the views problem but the concept could be applied to other objects (I guess)
Once you've got your schema imported and you have created several local views that use the remote tables, the process to refresh the schema would be like:
First of all, create a new schema other_schema
(It's only temporary)
create schema IF NOT EXISTS other_schema;
IMPORT FOREIGN SCHEMA public
FROM SERVER remote_server INTO other_schema;
Now we should change all our views code to use the tables in the new schema, other_schema
, replacing the references to the original remote_schema
, from psql
we can execute:
SELECT 'CREATE OR REPLACE view '|| viewname || ' AS ' ||
replace(definition, 'remote_schema.', 'other_schema.') view_stmt
from pg_views where definition like '%remote_schema.%' \gexec
Now we already have our schema and local views updated, so we can remove the obsoleted schema and rename the new one to keep all system as It was before the refresh process.
DROP schema remote_schema cascade;
ALTER SCHEMA other_schema RENAME TO remote_schema;
That's all. Now, all content in the remote schema is updated and your views are using it.
This approach has got some caveats, for a short period of time you stored 2 similar schemas in local (you'll need enough disk to support this), the name of the schema should be different to any existing table or view and finally this only solves the problem with Views, to apply to other objects you'll need to read the proper catalog table/view and generate the SQL command to update it.
ERROR: relation xyz already exists
when I try to re-import tables from a foreign schema. I'm using pg 14. – Boise