Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL HV021: Fix FDW Inconsistent Descriptor

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Reproduce and capture the full error. Run the failing query in psql with \set VERBOSITY verbose so 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: a SELECT * may fail while a query listing only unchanged columns succeeds.
  2. Identify the foreign table and server. Query information_schema.foreign_tables or run \d+ foreign_table_name in psql. This shows the foreign server, the FDW options, and the local column list with types. Save this output; it is your baseline.
  3. Compare with the remote schema. Connect to the remote database and inspect the same table (for example with \d+ table_name on 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.
  4. Check FDW options that affect column mapping. Some wrappers let you map columns by name or position through options such as column_name on the foreign table column, or a schema_name/table_name option 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. Use ALTER FOREIGN TABLE ... OPTIONS (SET table_name 'new_name') or the equivalent for your wrapper.
  5. 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 with ALTER 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.
  6. 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 run DROP FOREIGN TABLE foreign_table_name; and recreate it with CREATE 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.
  7. 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.
  8. 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_name or schema_name option 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 SCHEMA where 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.