Search code examples
postgresqlforeign-data-wrappermulticorn

foreign table, foreign sever and foreign data wrapper: how they communicate


When the postgreSQL database server gets the query, how does it find the related foreign data wrapper to resolve the query? What does exactly foreign server do?

I want to know the sequence of messages that happens when database server gets a query on foreign table?

enter image description here


Solution

  • Let's clarify the terminology.

    • A foreign data wrapper is an object that wraps code for communication with an external data source.

    • A foreign server is an object that specifies how to locate a certain external data source. It belongs to a foreign data wrapper.

    • A user mapping is an object that contains credentials to authenticate with an external data source. It belongs to a foreign server and a database user.

    • A foreign table is an object that describes an object containing data in an external data source. It belongs to a foreign server.

    So when a user tries to access a foreign table, PostgreSQL knows how to reach the data source (via the foreign server that belongs to the table), how to authenticate (via the user mapping) and what functions to use to perform this connection and exchange data (via the foreign data wrapper).

    Most foreign data wrappers don't require a connection to the remote data souce for query planning, but when data are fetched or sent, a connection will be established.

    Depending on the foreign data wrapper, such a connection is closed when the query is done or cached for the lifetime of the database session.