PostgreSQL HV00B: FDW Invalid Handle Fix
In this article
PostgreSQL error HV00B means "fdw invalid handle": the connection handle a foreign data wrapper is trying to use is no longer valid, so the server aborts the query instead of handing it to the remote database. The handle is typically a cached connection or server object that was dropped, reused after a restart, or torn down mid-transaction. In practice you fix it by closing and re-establishing the connection: run ALTER SERVER ... to refresh the connection options, reconnect the session, or restart the worker process that holds the stale handle. This guide walks through diagnosis and repair in that order.
What You'll Need
- Access to a PostgreSQL client with permission to query
pg_foreign_server,pg_foreign_tableandpg_stat_activity. - Superuser or owner rights on the foreign server and user mapping if you need to alter or drop them.
- The exact query text and full error line, including the
CONTEXTandDETAILfields, which name the server and sometimes the wrapper. - Access to the PostgreSQL server log, which usually shows the same error with the originating function name.
- Your
postgresql.confand any extension configuration (for example connection cache settings for postgres_fdw or another wrapper) plus the ability to reload the server if a setting change is needed.
Step-by-Step
- Capture the full error. HV00B alone is not enough. Copy the whole message, including
DETAIL,HINTandCONTEXT. The context line usually names the foreign server and sometimes the SQL function that raised it. Note whether the error appears on every query against that server or only intermittently; intermittent errors point at connection caching, while constant errors point at a broken definition. - Identify the foreign server involved. List the servers you have:
SELECT srvname, srvtype, srvversion, srvoptions FROM pg_foreign_server;Then list the tables and their options:SELECT ftrelid::regclass, ftserver, ftoptions FROM pg_foreign_table;Confirm the server named in the error still exists. If it was dropped and recreated while a session still referenced the old OID, the handle is now dangling. - Check whether the server was altered recently. Query
SELECT * FROM pg_foreign_server WHERE srvname = 'your_server';and compare against your deployment history. A change to the host, port, database or credentials while pooled connections were live is a common cause. If options changed, existing handles built from the old options must be discarded. - Look for concurrent DDL. On a busy database, a
DROP SERVERorALTER SERVERissued inside a transaction that later rolled back, or one that ran while a long query held a handle, can leave sessions pointing at invalidated data. Checkpg_locksand your DDL logs. Run heavy schema changes on foreign servers in a maintenance window, not during peak traffic. - Close the stale session. The cheapest fix is to end the session holding the handle. Find it with
SELECT pid, application_name, state, query FROM pg_stat_activity WHERE state <> 'idle';and thenSELECT pg_terminate_backend(pid);Only target sessions you own or have permission to end; terminating another user's backend is disruptive and requires appropriate privileges. Reconnect and rerun the query. If it now works, you have confirmed a stale connection cache. - Refresh the server definition. If altering options, force new handles by disconnecting first: reduce or zero the connection cache for that wrapper, reload the configuration, and wait for existing connections to drain. For postgres_fdw this relates to the
fetch_sizeand connection parameters defined in the user mapping and server options; changing the mapping credentials itself forces a fresh connection on the next query. UseALTER USER MAPPING FOR ... SERVER ... OPTIONS (SET password '...');and the next query builds a new handle. - Restart the relevant process if the error persists. A full
pg_ctl restartclears every cached FDW handle, but it drops all connections. Prefer reloading configuration first withSELECT pg_reload_conf();. Restart only after you have captured logs and confirmed nothing else clears it. - Rebuild the object cleanly. If the foreign table definition is corrupted or the server OID is inconsistent, drop and recreate the mapping in one transaction: save your options, drop the table, drop the server, recreate it with the same options, recreate user mappings, then run
IMPORT FOREIGN SCHEMAto recreate tables. Do this inside a transaction so a failure rolls back and does not leave a half-built definition. Back up options first. - Verify in a fresh session. Open a new connection, run a simple
SELECT * FROM foreign_table LIMIT 1;and then a query that exercises joins and pushed-down filters. ConfirmEXPLAIN (VERBOSE)shows aForeign Scanwithout errors. Repeating the query a few times checks whether the handle cache is stable.
Common Mistakes
- Retrying the query without closing the session, so the same bad handle is reused and the error repeats.
- Dropping and recreating a foreign server while active sessions hold handles, instead of draining connections first.
- Editing the password or host in options but expecting the change to apply immediately; cached connections keep the old credentials until they are closed.
- Assuming HV00B is a network problem. It is a client-side handle problem in PostgreSQL itself; the remote database may be perfectly healthy.
- Restarting the whole cluster as a first step rather than last, which hides the cause and disrupts other workloads.
- Overlooking a rolled-back transaction that altered the server definition; the rollback invalidates handles for sessions that already cached it.
Tips
- Keep FDW connection caching modest if you have many concurrent sessions; very high cache limits mean stale handles persist longer after a change.
- Record the OID and definition of each foreign server before maintenance so you can compare if error HV00B appears later.
- Test any server or mapping change in a staging database before it goes to production.
- Add the full error context to your monitoring alerts so you can distinguish HV00B from other FDW error classes such as HV00N or HV00L.
- Where possible, avoid modifying foreign server objects during high-traffic periods; schedule them like other schema changes.
Troubleshooting
If the error returns immediately after reconnecting, the problem is in the definition, not the cache. Re-check that the server OID and the user mapping match the table's ftserver. If it appears only under load, suspect connection limits or a wrapper that reuses handles across transactions. Check the PostgreSQL log for connection refusals just before the error. If the error accompanies a query that uses a pushed-down filter or a join, try EXPLAIN with pushdown disabled to see whether the wrapper itself is failing. If you see HV00B alongside a message about a server or user mapping not existing, the object was dropped underneath you; recreate it and drain sessions. When in doubt, capture a full session trace, list the open foreign handles in your extension settings, then reproduce in a fresh connection so you start from a clean state.
Frequently Asked Questions
What does HV00B specifically mean?
It is the SQLSTATE for an fdw invalid handle: a foreign data wrapper operation passed a handle PostgreSQL considers invalid, usually because the object it represented was dropped, recreated or closed.
Is this a bug in my remote database?
Usually not. It is raised locally when the local server's cached handle does not match the current definition, so a healthy remote database can still produce it.
Will restarting PostgreSQL fix it?
It clears every cached handle, but it drops all sessions. Try terminating the affected backend and refreshing options first, and reserve a restart for cases where the definition is sound.
Can I prevent it in the future?
Drain connections before changing foreign server objects, keep connection caching modest, and avoid dropping or recreating servers while sessions are pooled.
Does it affect only postgres_fdw?
No. Any foreign data wrapper that caches connection handles can raise it; the class of error is generic FDW protocol, not specific to one extension.