0

I am wondering how foreign data in Postgres are updated. I understand that the FDW makes a connection to the remote database to access the tables, and the foreign tables don't take up physical memory in the database. It is the equivalent of making a select statement to the the underlying tables.

I have some tables in Database A that I have imported as foreign tables into database B. I am using the foreign Tables in Database B to create materialized views. I am running these commands in Database B:

IMPORT FOREIGN SCHEMA completed LIMIT TO (tb1,tbl2)
FROM SERVER db_data_pipeline
INTO public;

COMMIT;

CREATE MV1 AS SELECT * FROM TB1;

My tables in database A are updated through a drop and recreate process. I have two questions regarding how foreign data are synchronized:

  1. Since my tables in database A are updated through a drop and recreate, are the foreign tables in Database B also updated automatically? is there a scenario where the foreign data becomes 'stale'? Would I have to re-import foreign tables to keep them up to date?

  2. I've read that the foreign table definition does not change when the underlying table changes. For example, a column added in the underlying table will not be reflected in the foreign table. However, since my underlying table is never updated directly and dropped and recreated instead, do I have to worry about this issue?

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263

1 Answers1

0

Ad 1:

When you query tb1 in database B, PostgreSQL composes a query, connects to database A and runs the query. If the table in database A has been dropped and re-created, this query will access the new table, since it goes by the table name. There is no need to drop and re-create the foreign tables. The data in the foreign tables will never be stale, but the data in the materialized views can of course become stale.

Ad 2:

Yes, you have to worry about that. A change in the table definition on database A will not change the foreign table in database B. So if you add a bew column in database A, it will not show up in the foreign table in database B. If you drop a column in the table in database A, querying the foreign table may cause an error, since the query could access the missing column. You have to modify or re-create the foreign table to deal with metadata changes.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263