The idea · Lab 2.7.5A foreign data wrapper is a driver that lets PostgreSQL treat an external data source as if it were an ordinary local table: postgres_fdw for another PostgreSQL server, file_fdw for a flat file, and dozens of third-party wrappers for everything from MySQL to a REST API. The query planner does not merely fetch everything and filter locally — for postgres_fdw specifically, it can push an entire WHERE clause down to execute on the remote server itself, exactly as if that server had been asked the filtered question directly.
This lab mounts a genuinely separate PostgreSQL cluster — not a schema in the same database, an actual second postmaster on port 5433, the same pattern used to build a standby back in Block 2.5 — as a foreign server, imports its schema automatically, joins it against a real local table, and then reads the exact remote SQL PostgreSQL generated to prove the filter really did execute remotely. Then it mounts a plain CSV file as a queryable table with file_fdw, no import step anywhere.
Mounting a Remote PostgreSQL Server
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER remote_pg FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '127.0.0.1', port '5433', dbname 'postgres');
CREATE USER MAPPING FOR postgres SERVER remote_pg OPTIONS (user 'postgres');
CREATE SERVER describes where the remote database lives; CREATE USER MAPPING describes which credentials this local role uses to authenticate there — the two are deliberately separate, since a real server would map several local roles to different remote credentials.
Mirroring the Remote Schema Automatically
CREATE SCHEMA remote;
IMPORT FOREIGN SCHEMA products_schema FROM SERVER remote_pg INTO remote;
This inspects the actual remote schema and creates a matching foreign table locally for every table it finds — no hand-written CREATE FOREIGN TABLE per remote table required.
Proving the Pushdown, Not Assuming It
EXPLAIN VERBOSE SELECT * FROM remote.products WHERE price > 10;
Foreign Scan on remote.products
Remote SQL: SELECT id, sku, price FROM products_schema.products WHERE ((price > 10::numeric))
The Remote SQL: line is not a guess about what postgres_fdw probably does — it is the literal query text sent to the remote server. The price > 10 filter runs there, on the remote machine, against the remote table's own data; only the matching rows ever cross the wire.
Querying a CSV File Directly
CREATE EXTENSION IF NOT EXISTS file_fdw;
CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE finance_transactions (txn_id int, amount numeric(10,2), category text)
SERVER file_server
OPTIONS (filename '/tmp/transactions.csv', format 'csv', header 'true');
SELECT * FROM finance_transactions;
No COPY, no staging table, no ETL step — the CSV file is queried exactly where it sits.
Key concepts
Foreign Data Wrapper
A driver that implements PostgreSQL's foreign-data interface for a specific kind of external source — postgres_fdw for another PostgreSQL server, file_fdw for flat files, and many third-party wrappers for other databases and services. Once configured, tables from that source can be queried with ordinary SQL, joined against local tables, and in many cases have filters and even joins pushed down to execute at the source.
IMPORT FOREIGN SCHEMA
Inspects a foreign server's actual schema and generates matching CREATE FOREIGN TABLE statements automatically, for every table found (or a specific LIMIT TO list). This avoids hand-writing a foreign table definition — column by column, type by type — for every table on the remote side, and keeps the local mirror accurate if the remote schema is re-imported later.
What to remember
- postgres_fdw mounts a genuinely separate PostgreSQL server as a set of local foreign tables, queryable and joinable with ordinary SQL
- IMPORT FOREIGN SCHEMA generates foreign table definitions automatically from the remote schema, rather than requiring each one hand-typed
- CREATE SERVER (where the remote lives) and CREATE USER MAPPING (which credentials to use there) are deliberately separate concerns
- EXPLAIN VERBOSE's "Remote SQL:" line is the literal query text sent to the remote server — the only reliable way to confirm a filter genuinely pushed down rather than assuming it did
- file_fdw queries a flat file directly with no import step, but has no query engine on the other side to push anything down to — it always reads the whole file