PostgreSQL HV021: Fix FDW Inconsistent Descriptor
In this article
PostgreSQL error HV021 with the message "inconsistent descriptor information" appears when a foreign data wrapper (FDW) returns column metadata that does not match what the query planner expected. In practice, this almost always means the foreign table definition in PostgreSQL no longer matches the remote table: columns were added, dropped, renamed, reordered, or their data types changed on the remote side. The fix is to reconcile the local foreign table definition with the remote schema, then re-run the query. The steps below walk through diagnosis, the safe fix, and what to do if the error persists.
What You'll Need
- Access to the PostgreSQL database as a user who owns (or can alter) the foreign table and the foreign server.
- Access to the remote database or service, or its current schema documentation, so you can compare column names, order, and types.
- The name of the FDW in use (for example
postgres_fdw,mysql_fdw,oracle_fdw, or a file-based wrapper) and the foreign server and foreign table names. - A maintenance window if the foreign table is used by production queries; some fixes require dropping and recreating the table.
Step-by-Step
- Reproduce and capture the full error. Run the failing query in
psqlwith\set VERBOSITY verboseso you see the SQLSTATE (HV021), the message, and the detail line. The detail often names the foreign table or the column that triggered the mismatch. Note the exact query, because the error can be query-specific: aSELECT *may fail while a query listing only unchanged columns succeeds. - Identify the foreign table and server. Query
information_schema.foreign_tablesor run\d+ foreign_table_nameinpsql. This shows the foreign server, the FDW options, and the local column list with types. Save this output; it is your baseline. - Compare with the remote schema. Connect to the remote database and inspect the same table (for example with
\d+ table_nameon a remote PostgreSQL, or the equivalent schema view on MySQL, Oracle, or another source). Compare three things: the set of columns, the column order, and the data types. Any difference in these can produce HV021, because the FDW builds a descriptor from the remote result and PostgreSQL checks it against the local foreign table definition. - Check FDW options that affect column mapping. Some wrappers let you map columns by name or position through options such as
column_nameon the foreign table column, or aschema_name/table_nameoption on the foreign table. If the remote table was renamed or moved to another schema, update the foreign table option rather than the column list. UseALTER FOREIGN TABLE ... OPTIONS (SET table_name 'new_name')or the equivalent for your wrapper. - Fix the local definition. If columns were added remotely, add them locally with
ALTER FOREIGN TABLE ... ADD COLUMN, matching the remote name and type. If columns were dropped remotely, drop them locally withALTER FOREIGN TABLE ... DROP COLUMN. If types changed, alter the local column type to match. Keep the column order consistent with the remote table where the wrapper is sensitive to position; when in doubt, recreate the foreign table. - Recreate the foreign table if alterations are not enough. For wrappers that cache or rely on positional mapping, the cleanest fix is to drop and recreate the foreign table. First save the definition with
\d+ foreign_table_name, then runDROP FOREIGN TABLE foreign_table_name;and recreate it withCREATE FOREIGN TABLE ... SERVER ... OPTIONS (...);. Dropping a foreign table removes only the local definition, not the remote data, but any local views, grants, or dependent objects that reference it must be recreated, so check dependencies first. - Refresh statistics and reconnect. Run
ANALYZE foreign_table_name;so the planner has current statistics. If the FDW uses connection pooling or a persistent connection, disconnect and reconnect the session, or restart the application connection pool, so stale descriptor caches are discarded. - Re-run the original query. Execute the exact query that failed. If it succeeds, run a broader check such as
SELECT * FROM foreign_table_name LIMIT 1;to confirm every column maps correctly.
Common Mistakes
- Changing only the local column list while leaving a stale
table_nameorschema_nameoption in place. The FDW then reads a different remote table and the descriptor mismatch returns. - Assuming the error is a PostgreSQL bug. HV021 is a class 0H FDW error and nearly always reflects a real schema difference, not a server defect.
- Dropping the foreign table without checking dependencies. Views, materialized views, and grants that reference the foreign table will block the drop or be lost.
- Ignoring column order. Some wrappers map by position, so adding a column at the end locally when it was inserted in the middle remotely can still trigger HV021.
- Testing only with a narrow column list. A query that selects unchanged columns may pass while the application's
SELECT *still fails.
Tips
- Keep the foreign table definition in version control alongside the remote schema migration, so both change together.
- After any remote schema change, run a quick
SELECT * FROM foreign_table_name LIMIT 1;as a smoke test before deploying application changes. - Use
IMPORT FOREIGN SCHEMAwhere the wrapper supports it to generate foreign table definitions from the remote schema, reducing manual drift. - Prefer explicit column lists in application queries over
SELECT *on foreign tables; it limits the blast radius of remote schema changes.
Troubleshooting
If the error persists after reconciling the schema, work through these checks in order. First, confirm you altered the correct foreign table: a same-named table in another schema can be the one the query actually hits. Second, inspect the FDW options on both the server and the table with \des+ and \det+ in psql; a wrong dbname, host, or schema_name option can point the wrapper at a different table with a similar name. Third, check whether the remote object is a view rather than a table: a view whose underlying columns changed will return a different descriptor even though the view name is unchanged. Fourth, if the wrapper supports it, enable FDW logging or raise client_min_messages to debug1 for the session to see the descriptor the wrapper received. Fifth, test with a minimal query selecting one column at a time to isolate which column causes the mismatch. If none of this resolves it, recreate the foreign table from a freshly generated definition and reconnect; stale cached descriptors are cleared by a new backend session.
Frequently Asked Questions
What does SQLSTATE HV021 mean exactly?
It is the FDW error class code for inconsistent descriptor information. PostgreSQL received column metadata from the foreign data wrapper that did not match the foreign table definition it expected.
Does HV021 mean data was corrupted?
No. It is a metadata mismatch between the local foreign table definition and the remote result, not data corruption. Fixing the definition resolves it.
Can HV021 appear only for some queries?
Yes. Queries that touch only unchanged columns may succeed while queries selecting the changed columns, or using SELECT *, fail.
Will dropping the foreign table delete remote data?
No. DROP FOREIGN TABLE removes only the local definition. The remote table and its rows are untouched, but dependent local objects must be recreated.
Do I need to restart PostgreSQL?
Usually not. Reconnecting the session or restarting the application connection pool is enough to clear cached descriptors; a full server restart is rarely necessary.
Does this affect all FDWs?
The error class is generic to foreign data wrappers, but the exact cause and the options available for fixing it vary by wrapper. Check the documentation for the FDW you use.