Introduction
Modern self-hosted architectures rarely rely on a single database engine. A typical deployment might use PostgreSQL for transactional data, MySQL for a legacy application, and Microsoft SQL Server for a vendor system. PostgreSQL’s Foreign Data Wrapper (FDW) framework enables transparent cross-database queries — you can JOIN a local PostgreSQL table with a remote MySQL table in a single SQL statement without ETL pipelines.
This guide compares three production-grade FDWs: the built-in postgres_fdw for PostgreSQL-to-PostgreSQL federation, mysql_fdw for MySQL/MariaDB access, and tds_fdw for Microsoft SQL Server and Sybase connectivity. We cover installation, performance tuning, security, and self-hosted deployment patterns.
Comparison Table
| Feature | postgres_fdw | mysql_fdw | tds_fdw |
|---|---|---|---|
| Source Database | PostgreSQL 9.6+ | MySQL 5.5+ / MariaDB | MSSQL 2008+ / Sybase |
| Stars | Bundled with PG (21K+) | 596 | 427 |
| Last Update | Continuous | May 2026 | May 2026 |
| Pushdown (WHERE) | Full | Partial | Partial |
| Pushdown (JOIN) | Yes (PG 11+) | No | No |
| Pushdown (Aggregates) | Yes (PG 14+) | No | No |
| Write Support | INSERT/UPDATE/DELETE | INSERT/UPDATE/DELETE | INSERT/UPDATE/DELETE |
| Column Mapping | Automatic | Manual via options | Manual via options |
| SSL/TLS | Native | Via MySQL SSL | Via FreeTDS |
| Import Schema | IMPORT FOREIGN SCHEMA | IMPORT FOREIGN SCHEMA | IMPORT FOREIGN SCHEMA |
| Transaction Support | Yes (2PC optional) | Limited | Limited |
postgres_fdw: PostgreSQL-to-PostgreSQL Federation
postgres_fdw is included in the PostgreSQL core distribution since 9.3 and provides the deepest query pushdown capabilities among all FDWs. It can push down WHERE clauses, JOINs, ORDER BY, LIMIT, and aggregate functions to the remote server, minimizing data transfer.
Docker Compose Setup
| |
Creating and Using postgres_fdw
| |
Performance Tuning
| |
mysql_fdw: Bridging MySQL and PostgreSQL
mysql_fdw enables PostgreSQL to query MySQL and MariaDB databases. It supports read/write operations and partial query pushdown for WHERE clauses.
Installation
| |
Configuration & Usage
| |
SSL/TLS Configuration
| |
tds_fdw: SQL Server Integration
tds_fdw connects PostgreSQL to Microsoft SQL Server and Sybase databases via the FreeTDS library. It’s essential for organizations migrating from MSSQL to PostgreSQL or maintaining hybrid architectures.
Installation
| |
FreeTDS Configuration
| |
Usage
| |
Deployment Architecture
For production deployments, consider this architecture pattern:
| |
Security Best Practices
| |
Why Self-Host Your Data Federation Layer?
Running your own data federation layer eliminates vendor lock-in for cross-database queries. Cloud providers charge premiums for cross-region or cross-engine query capabilities — AWS Athena Federated Queries costs $5 per TB scanned, and Azure Synapse Link charges per vCore-hour. Self-hosted FDWs run on your existing PostgreSQL instance at zero additional licensing cost.
Data sovereignty is equally important. When your PostgreSQL server queries a remote database, the data flows directly between the two without intermediate cloud services that can log, inspect, or throttle your queries. This is critical for GDPR compliance when personal data spans multiple database systems.
The FDW approach also simplifies migrations. Organizations moving from MSSQL to PostgreSQL can use tds_fdw to keep the old system accessible during a phased migration — applications gradually switch to native PostgreSQL tables while still querying legacy data through foreign tables. For more on database migration strategies, see our PostgreSQL Backup comparison guide. Our PostgreSQL vs MySQL comparison covers when each engine is the right choice, and our PostgreSQL Log Analysis guide covers query performance diagnostics.
FAQ
What’s the performance overhead of FDW queries?
FDWs add network latency overhead. For a remote row count query, expect 2-5ms added latency on local networks. Query pushdown dramatically reduces this — with postgres_fdw pushing down a WHERE clause, only matching rows transfer over the network. Without pushdown, the entire table scans locally. Always run EXPLAIN (ANALYZE, VERBOSE) to verify which operations are pushed to the remote server.
Can I use FDWs in high-availability setups?
Yes. Use connection pooling (PgBouncer or Odyssey) in front of the PostgreSQL instance running FDWs. For the remote servers, specify multiple hosts using DNS load balancing or a TCP proxy like HAProxy. postgres_fdw supports application_name for connection identification in monitoring tools.
How do FDWs handle data type differences?
Each FDW maps remote types to PostgreSQL types. postgres_fdw preserves types exactly since both ends are PostgreSQL. mysql_fdw maps MySQL’s DATETIME to TIMESTAMP, TINYINT to SMALLINT, and TEXT to TEXT. tds_fdw maps MSSQL’s NVARCHAR to VARCHAR and MONEY to NUMERIC(19,4). Check the specific mapping in each FDW’s documentation for edge cases.
Can I combine multiple FDWs in a single query?
Yes. You can JOIN a local table, a postgres_fdw foreign table, and a mysql_fdw foreign table in one query. However, cross-FDW JOINs execute on the local PostgreSQL instance — neither foreign server can push down a JOIN involving a different FDW. For read-heavy cross-FDW queries, consider creating materialized views.
What about JSON and NoSQL FDWs?
Beyond relational databases, PostgreSQL offers FDWs for MongoDB (mongo_fdw), Redis (redis_fdw), S3 files (s3_fdw), and even CSV files (file_fdw). The same access patterns apply — you define a foreign server, user mapping, and foreign tables, then query non-relational data with SQL.
Are FDWs suitable for real-time applications?
For OLTP workloads requiring sub-millisecond latency, FDWs are not ideal. Each query to a foreign table involves network round trips. Use FDWs for reporting, analytics, ETL, and phased migrations. For real-time cross-database transactions, consider CDC (Change Data Capture) with tools like Debezium instead.
💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com