Fix PostgreSQL Error HV014: FDW Too Many Handles
In this article
PostgreSQL error HV014, "fdw_too_many_handles", means a foreign data wrapper (FDW) such as postgres_fdw or oracle_fdw has run out of connection "handles" to the remote server. Each open foreign scan, and each cached connection, holds a handle, and the wrapper has a fixed limit. The fix is almost always to close idle connections and keep concurrent foreign-table scans below that limit: run DISCARD ALL; or reconnect to release cached connections, reduce parallelism on queries touching foreign tables, and raise the FDW's own connection limit where the wrapper allows it.
What You'll Need
- Superuser or owner access to the local PostgreSQL database that raises the error
- Access to run SQL via
psqlor another client - The ability to change user mappings or server objects (for the limit changes)
- Knowledge of which foreign tables and queries trigger the error
Step-by-Step
- Identify the foreign data wrapper involved. Run
\desinpsql, or querySELECT srvname, srvhandler::regproc FROM pg_foreign_server;to see which FDWs are in use. HV014 is most commonly raised bypostgres_fdw. - Release cached connections in the current session.
postgres_fdwcaches one connection per foreign server per session. RunDISCARD ALL;or simply disconnect and reconnect your session. This drops cached connections and frees handles immediately for that session. - Check for long-lived sessions holding connections. Query
pg_stat_activityto find idle sessions that have touched foreign tables:SELECT pid, state, query FROM pg_stat_activity WHERE state = 'idle';Idle sessions keep their cached FDW connections open. Considerpg_terminate_backend(pid)for truly stuck sessions (warn users first). - Reduce concurrent scans of foreign tables. HV014 typically appears when one query opens too many foreign scans at once, for example a join of many foreign tables with parallel workers, or many partitions that are all foreign tables. Test by running the failing query with
SET max_parallel_workers_per_gather = 0;in the session to rule out parallel scans multiplying connections. - Split up the query. If a single query joins many foreign tables, break it into several smaller queries, materializing intermediate results into local temp tables with
CREATE TEMP TABLE ... AS SELECT ..., so fewer connections are open simultaneously. - Use
use_remote_estimateandfetch_sizewisely, not excessively. Thesepostgres_fdwoptions do not consume handles, but oversized plans pulling many foreign tables in one plan do. ReviewEXPLAINoutput for the failing query to see how many foreign scans appear. - For
oracle_fdwand other wrappers, check their connection limits. Some FDWs expose a limit setting (for exampleoracle_fdwhas a maximum number of connections defined at build time, commonly 64 per process). If you are hitting it, the remedies are the same: fewer concurrent scans, shorter transactions, and closing idle sessions. - Keep transactions short. Foreign-table scans inside long-running transactions hold their connection for the whole transaction. Commit or roll back promptly after reading foreign data so handles return to the pool.
- Restart the application pool if needed. If the error comes from an application with a connection pooler, recycle idle pooled connections (most poolers have a max-lifetime or idle-timeout setting) so stale sessions release their FDW connections.
Common Mistakes
- Restarting PostgreSQL entirely when a simple
DISCARD ALL;or session reconnect would free the handles. - Increasing
max_connectionsthinking it helps; HV014 is about the FDW's internal handle limit, not the server's connection cap. - Ignoring parallel query: parallel workers can each open their own foreign connection, silently doubling or tripling handle usage.
- Leaving idle-in-transaction sessions open for hours; these pin cached FDW connections indefinitely.
- Opening many partitions that are all foreign tables in one query and assuming only one connection is used.
Tips
- Pull frequently used foreign data into local tables on a schedule instead of querying the remote server live; this removes the handle problem entirely for reporting workloads.
- Aggregate joins between foreign tables on the remote side where possible:
postgres_fdwcan push down entire joins when both tables share one foreign server, using a single connection. - Keep foreign tables from different servers out of the same query plan when you can; each distinct server needs its own connection.
- After any change, reproduce the original failing query to confirm the error is gone before closing the incident.
Troubleshooting
If the error persists after freeing connections, check the server log around the failure time for the exact query and process; the HV014 line includes context about which operation exhausted handles. Confirm which FDW raised it: postgres_fdw and oracle_fdw have different limits and behaviors. If a specific query always fails, reduce it to the smallest form that still fails and inspect its plan with EXPLAIN (VERBOSE) to count foreign scans. If idle sessions keep accumulating, look at the application layer: missing connection cleanup or an overly aggressive pool size is usually the root cause. As a last resort, restarting the PostgreSQL server clears all sessions and handles, but schedule it during a maintenance window since it drops every active connection.
Frequently Asked Questions
What does error code HV014 mean?
It is the SQLSTATE code fdw_too_many_handles: a foreign data wrapper has exhausted its pool of connections to a remote server and cannot open another one.
Does raising max_connections fix it?
No. The limit is internal to the foreign data wrapper, not the server's client connection limit. You must reduce concurrent foreign scans or free cached connections.
Why does a single query trigger it?
A query can open multiple foreign scans at once: joins of several foreign tables, many foreign-table partitions, or parallel workers each needing their own connection.
Does DISCARD ALL really release FDW connections?
Yes. It closes the cached connections that postgres_fdw keeps for your session, returning their handles to the pool.
Is this error specific to postgres_fdw?
No. Any FDW can raise HV014, but postgres_fdw and oracle_fdw are the most common sources because of their connection caching and per-process limits.