Documentation by PostgreSQL 17
The postgres_fdw
module provides the foreign-data wrapper postgres_fdw
, which can be used to access data stored in external PostgreSQL servers.
The functionality provided by this module overlaps substantially with the functionality of the older dblink module. But postgres_fdw
provides more transparent and standards-compliant syntax for accessing remote tables, and can give better performance in many cases.
To prepare for remote access using postgres_fdw
:
- Install the
postgres_fdw
extension using CREATE EXTENSION. - Create a foreign server object, using CREATE SERVER, to represent each remote database you want to connect to. Specify connection information, except
user
andpassword
, as options of the server object. - Create a user mapping, using CREATE USER MAPPING, for each database user you want to allow to access each foreign server. Specify the remote user name and password to use as
user
andpassword
options of the user mapping. - Create a foreign table, using CREATE FOREIGN TABLE or IMPORT FOREIGN SCHEMA, for each remote table you want to access. The columns of the foreign table must match the referenced remote table. You can, however, use table and/or column names different from the remote table’s, if you specify the correct remote names as options of the foreign table object.
Now you need only SELECT
from a foreign table to access the data stored in its underlying remote table. You can also modify the remote table using INSERT
, UPDATE
, DELETE
, COPY
, or TRUNCATE
. (Of course, the remote user you have specified in your user mapping must have privileges to do these things.)
Note that the ONLY
option specified in SELECT
, UPDATE
, DELETE
or TRUNCATE
has no effect when accessing or modifying the remote table.
Note that postgres_fdw
currently lacks support for INSERT
statements with an ON CONFLICT DO UPDATE
clause. However, the ON CONFLICT DO NOTHING
clause is supported, provided a unique index inference specification is omitted. Note also that postgres_fdw
supports row movement invoked by UPDATE
statements executed on partitioned tables, but it currently does not handle the case where a remote partition chosen to insert a moved row into is also an UPDATE
target partition that will be updated elsewhere in the same command.
It is generally recommended that the columns of a foreign table be declared with exactly the same data types, and collations if applicable, as the referenced columns of the remote table. Although postgres_fdw
is currently rather forgiving about performing data type conversions at need, surprising semantic anomalies may arise when types or collations do not match, due to the remote server interpreting query conditions differently from the local server.
Note that a foreign table can be declared with fewer columns, or with a different column order, than its underlying remote table has. Matching of columns to the remote table is by name, not position.
FDW Options of postgres_fdw
Connection Options
A foreign server using the postgres_fdw
foreign data wrapper can have the same options that libpq accepts in connection strings, except that these options are not allowed or have special handling:
user
,password
andsslpassword
(specify these in a user mapping instead, or use a service file)client_encoding
(this is automatically set from the local server encoding)application_name
– this may appear in either or both a connection and postgres_fdw.application_name. If both are present,postgres_fdw.application_name
overrides the connection setting. Unlike libpq,postgres_fdw
allowsapplication_name
to include “escape sequences”. See postgres_fdw.application_name for details.fallback_application_name
(always set topostgres_fdw
)sslkey
andsslcert
– these may appear in either or both a connection and a user mapping. If both are present, the user mapping setting overrides the connection setting.
Only superusers may create or modify user mappings with the sslcert
or sslkey
settings.
Non-superusers may connect to foreign servers using password authentication or with GSSAPI delegated credentials, so specify the password
option for user mappings belonging to non-superusers where password authentication is required.
A superuser may override this check on a per-user-mapping basis by setting the user mapping option password_required 'false'
, e.g.,
ALTER USER MAPPING FOR some_non_superuser SERVER loopback_nopw
OPTIONS (ADD password_required ‘false’);
To prevent unprivileged users from exploiting the authentication rights of the unix user the postgres server is running as to escalate to superuser rights, only the superuser may set this option on a user mapping.
Care is required to ensure that this does not allow the mapped user the ability to connect as superuser to the mapped database per CVE-2007-3278 and CVE-2007-6601. Don’t set password_required=false
on the public
role. Keep in mind that the mapped user can potentially use any client certificates, .pgpass
, .pg_service.conf
etc. in the unix home directory of the system user the postgres server runs as. They can also use any trust relationship granted by authentication modes like peer
or ident
authentication.
Connection Management
postgres_fdw
establishes a connection to a foreign server during the first query that uses a foreign table associated with the foreign server. By default this connection is kept and re-used for subsequent queries in the same session. This behavior can be controlled using keep_connections
option for a foreign server. If multiple user identities (user mappings) are used to access the foreign server, a connection is established for each user mapping.
When changing the definition of or removing a foreign server or a user mapping, the associated connections are closed. But note that if any connections are in use in the current local transaction, they are kept until the end of the transaction. Closed connections will be re-established when they are necessary by future queries using a foreign table.
Once a connection to a foreign server has been established, it’s by default kept until the local or corresponding remote session exits. To disconnect a connection explicitly, keep_connections
option for a foreign server may be disabled, or postgres_fdw_disconnect
and postgres_fdw_disconnect_all
functions may be used. For example, these are useful to close connections that are no longer necessary, thereby releasing connections on the foreign server.
Transaction Management
During a query that references any remote tables on a foreign server, postgres_fdw
opens a transaction on the remote server if one is not already open corresponding to the current local transaction. The remote transaction is committed or aborted when the local transaction commits or aborts. Savepoints are similarly managed by creating corresponding remote savepoints.
The remote transaction uses SERIALIZABLE
isolation level when the local transaction has SERIALIZABLE
isolation level; otherwise it uses REPEATABLE READ
isolation level. This choice ensures that if a query performs multiple table scans on the remote server, it will get snapshot-consistent results for all the scans. A consequence is that successive queries within a single transaction will see the same data from the remote server, even if concurrent updates are occurring on the remote server due to other activities. That behavior would be expected anyway if the local transaction uses SERIALIZABLE
or REPEATABLE READ
isolation level, but it might be surprising for a READ COMMITTED
local transaction. A future PostgreSQL release might modify these rules.
Note that it is currently not supported by postgres_fdw
to prepare the remote transaction for two-phase commit.
Remote Query Optimization
postgres_fdw
attempts to optimize remote queries to reduce the amount of data transferred from foreign servers. This is done by sending query WHERE
clauses to the remote server for execution, and by not retrieving table columns that are not needed for the current query. To reduce the risk of misexecution of queries, WHERE
clauses are not sent to the remote server unless they use only data types, operators, and functions that are built-in or belong to an extension that’s listed in the foreign server’s extensions
option. Operators and functions in such clauses must be IMMUTABLE
as well. For an UPDATE
or DELETE
query, postgres_fdw
attempts to optimize the query execution by sending the whole query to the remote server if there are no query WHERE
clauses that cannot be sent to the remote server, no local joins for the query, no row-level local BEFORE
or AFTER
triggers or stored generated columns on the target table, and no CHECK OPTION
constraints from parent views. In UPDATE
, expressions to assign to target columns must use only built-in data types, IMMUTABLE
operators, or IMMUTABLE
functions, to reduce the risk of misexecution of the query.
When postgres_fdw
encounters a join between foreign tables on the same foreign server, it sends the entire join to the foreign server, unless for some reason it believes that it will be more efficient to fetch rows from each table individually, or unless the table references involved are subject to different user mappings. While sending the JOIN
clauses, it takes the same precautions as mentioned above for the WHERE
clauses.
The query that is actually sent to the remote server for execution can be examined using EXPLAIN VERBOSE
.