Foreign Tables
Foreign Tables (SQL/MED)
A Foreign Table is a database object that acts as a virtual pointer to a table in an external data source. It allows you to query data from other systems—such as different databases, flat files, or cloud storage—using your current database's SQL without physically moving the data.
How it Works
Foreign Data Wrapper (FDW): A library or "driver" that translates SQL commands from your local engine into a language the external system understands.
Foreign Server: An object that defines the connection details (host, port, credentials) for the external source.
Foreign Table Definition: A schema created in your local database that mirrors the structure of the remote data, linking it to the foreign server.
Key Benefits
Zero ETL: Avoids complex data migration, storage duplication, and the need for a "Load" step.
Real-time Access: Queries always fetch the most current data directly from the source.
Unified Querying: You can perform
JOINsbetween a local table and an external S3 file using standard SQL.Cost-Effective: Reduces storage costs because data isn't duplicated across systems.
Implementation and Syntax
The syntax follows the SQL/MED (Management of External Data) standard. While the concept is universal, the keywords vary by provider.
Example: PostgreSQL (The Standard Syntax)
To query a local CSV file as if it were a SQL table:
Provider Variations
Database
Feature Name
Common Use Case
PostgreSQL
FOREIGN TABLE
Connecting to Postgres, MySQL, or CSVs.
Snowflake
EXTERNAL TABLE
Querying Parquet/JSON files in S3 or Azure.
BigQuery
EXTERNAL TABLE
Querying data in Google Cloud Storage or Drive.
SQL Server
PolyBase
Querying Hadoop, Azure Blob, or MongoDB.
Use Cases and Trade-offs
Common Scenarios
Data Lakehouse: Querying massive files in cloud storage (S3/GCS) without importing them.
Data Federation: Integrating a central data warehouse with various operational databases.
Legacy Migration: Accessing data incrementally from old systems during a transition.
The "Catch" (Important for DEs)
Performance: Foreign tables are almost always slower than local tables because data must travel over a network or be parsed from a file on the fly.
Dependency: If the external file is moved or the remote server goes down, your local query will fail immediately.
Optimization: Look for Pushdown support. A "smart" foreign table will send your
WHEREfilters to the external source so it only transfers the rows you actually need.
Last updated