Postgres postgres_fdw Federation Design Prompt
Design a postgres_fdw setup that actually pushes down joins, filters, and aggregates to the remote server — instead of dragging whole tables across the network — and get the security and failure modes right.
- Target user
- Platform and data engineers federating queries across multiple Postgres databases
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior PostgreSQL architect who has built and debugged cross-database federation with postgres_fdw. You know that the whole game is pushdown: if predicates and joins don't execute on the remote, the local server fetches everything and filters locally, and performance collapses. You verify pushdown from EXPLAIN, never assume it. I will describe: - The local and remote Postgres versions: [VERSIONS] - The tables I want to expose and the queries I'll run against them: [TABLES / QUERIES] - Data volume on the remote and network latency between servers: [SIZE / LATENCY] - Whether writes go through the FDW or only reads: [READ / WRITE] - Auth constraints (who owns the mapping, secrets handling): [SECURITY] Work through this in order: 1. **Design the objects**: the `CREATE SERVER` (with `use_remote_estimate`, `fetch_size`, and appropriate `updatable`/`async_capable` options), `CREATE USER MAPPING`, and either `CREATE FOREIGN TABLE` or `IMPORT FOREIGN SCHEMA`. Explain each option you set and why. 2. **Maximize pushdown.** Explain which operations postgres_fdw can push to the remote (WHERE filters, joins between two foreign tables on the SAME server, aggregates, ORDER BY, LIMIT) and which it cannot (joins across different servers, non-pushable functions, local-only operators). Stress that `use_remote_estimate = true` plus up-to-date remote stats is what lets the planner choose a remote join. 3. **Verify with EXPLAIN.** Tell me exactly what to look for: a `Foreign Scan` whose "Remote SQL" line contains the WHERE/JOIN/aggregate, versus the failure signature — a Foreign Scan that fetches the whole table and a local Filter/Join on top. Give the command and how to read it. 4. **Tune transfer**: fetch_size for round-trip efficiency, async_capable for parallel foreign scans, and when a materialized local copy or logical replication beats live federation entirely. 5. **Cover security and failure modes**: least-privilege remote role, secrets in the user mapping (never a superuser), what happens when the remote is down (queries error — no automatic failover), and transaction/consistency caveats across servers. Output: (a) the DDL for server/mapping/foreign tables; (b) a pushdown checklist for the target queries; (c) the EXPLAIN verification with the exact "Remote SQL" line to expect; (d) security and failure-mode notes; (e) when NOT to use FDW. Guardrails: use a dedicated least-privilege remote role in the user mapping, never a superuser or shared credential. Test every target query's EXPLAIN for real pushdown on a staging pair before production — a missing pushdown can move terabytes. Note that the remote being unavailable makes dependent queries fail; FDW is not high availability.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Why this prompt works
postgres_fdw looks trivial to set up and quietly catastrophic to run: a foreign table Just Works, but whether a query is fast depends entirely on whether the filter, join, and aggregate execute on the remote or drag the whole table back to be filtered locally. The prompt makes pushdown the central concern and — critically — insists on reading the “Remote SQL” line of EXPLAIN rather than trusting that federation is efficient by default.
It also encodes the two facts teams learn the hard way: cross-server joins can’t push down (each foreign table’s server matters), and use_remote_estimate = true with fresh remote statistics is what lets the planner even choose a remote join. Rounding it out with least-privilege user mappings, transfer tuning, and an explicit “when not to federate” (sometimes a materialized copy or logical replication wins) turns a fragile convenience into a deliberate architecture with known failure modes.
Related prompts
-
Postgres auto_explain Plan Regression Detection Prompt
Set up auto_explain to catch the query whose plan flipped in production — capturing the bad plan with timing and buffers only when it matters — then diagnose why the planner chose it, without logging every fast query.
-
Postgres Keyset (Cursor) Pagination Optimization Prompt
Convert a slow OFFSET/LIMIT pagination query into stable keyset (seek) pagination that stays fast on deep pages — with a matching index and a correct tuple-comparison predicate, not a broken one-column WHERE.
-
Postgres Parallel Query Worker Tuning Prompt
Work out why a big analytical query isn't going parallel — or is spawning too many workers and starving everything else — and set the parallel GUCs from evidence in the plan instead of copy-pasted blog values.
-
Postgres Read-Replica Load Balancing Design Prompt
Design read/write query routing across streaming replicas that respects replication lag and read-your-writes consistency — so you scale reads without serving stale data to the user who just wrote it.
More Postgres prompts & error guides
Browse every Postgres prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.