PostgreSQL HV004: Fix Invalid Data Type in FDW
In this article
PostgreSQL error HV004 (fdw_invalid_data_type) means a foreign data wrapper (FDW) encountered a column data type it cannot handle during a query. The fix is almost always to correct the column type mapping between the foreign table definition and the actual remote source, or to cast the value explicitly in your query. You cannot "repair" this error with a restart — it points to a mismatch in how a column is declared.
What You'll Need
- Access to the PostgreSQL database as a user who can alter foreign tables (typically the owner or a superuser).
- The name of the foreign server and foreign table involved (check the error detail, which usually names the column).
- Knowledge of the actual column type on the remote side — the source database, file, or API.
- A SQL client (psql, pgAdmin, DBeaver, or similar).
- For postgres_fdw, the same or a compatible PostgreSQL major version on both ends is strongly recommended.
Step-by-Step
- Read the full error message. HV004 usually comes with a detail line naming the foreign table and column, for example
column "created_at" of foreign table "remote_events". Note the exact table and column before changing anything. - Inspect the foreign table definition. Run
\d+ foreign_table_namein psql (or view the Columns tab in pgAdmin). Look at the declared type of the offending column. - Compare with the remote source. Connect to the remote database (or open the source file/API schema) and check the real type of that column. Common culprits:
json/jsonb, arrays, enums,uuid, geometry types, or custom domains that the FDW cannot map. - Correct the column type in the foreign table. If the local declaration is wrong, alter it. For example:
ALTER FOREIGN TABLE remote_events ALTER COLUMN created_at TYPE timestamp;Use a type the FDW supports and that matches the remote data. For postgres_fdw, the built-in PostgreSQL types (integer, bigint, numeric, text, varchar, timestamp, date, boolean, and so on) map cleanly. - If the remote type is the problem, cast on the remote side. When the source column is an unsupported type, create a view on the remote database that casts it to a supported type, then point the foreign table at that view instead. For example, on the remote server:
CREATE VIEW events_safe AS SELECT id, created_at::timestamp, payload::text FROM events;Then recreate or alter the foreign table to read fromevents_safe. - Cast in the query as a stopgap. If you cannot change the table definition, select the column with an explicit cast:
SELECT created_at::text FROM remote_events;This works only when the FDW can at least transfer the raw value; it will not help if the type is rejected before transfer. - Recreate the foreign table if altering fails. Some type changes on foreign tables are restricted. In that case, drop and recreate the foreign table with the correct column types:
DROP FOREIGN TABLE remote_events;thenCREATE FOREIGN TABLE ... SERVER ... OPTIONS (...);Dropping a foreign table removes only the local definition, not the remote data — but confirm that before proceeding. - Test with a narrow query. Run
SELECT column_name FROM foreign_table LIMIT 1;to confirm the mapping now works before running a full report.
Common Mistakes
- Changing only the query, not the definition. Casting in one SELECT hides the problem but leaves every other query broken. Fix the foreign table.
- Assuming the remote type is fine because it "looks like" text. A PostgreSQL
enumorcitextcolumn is not the same astextto every FDW. - Forgetting that arrays and composite types need special handling. Many FDWs cannot map them at all; cast to text or json on the remote side.
- Altering a foreign table while other sessions hold locks on it. Check
pg_locksor run the change during a quiet window. - Mixing PostgreSQL versions across postgres_fdw. Type support and wire format can differ; match versions where possible.
Tips
- Keep a note of the remote schema next to each foreign table definition so future mismatches are obvious.
- Prefer creating a dedicated view on the remote side for any table with exotic types — it isolates the cast in one place.
- When using file_fdw or a similar wrapper, check the file's actual format; a CSV column that is sometimes empty can be read as text but not as integer.
- Use
\d+and\des+in psql to review foreign tables and servers quickly. - Test type changes on a staging database first if the foreign table feeds production reports.
Troubleshooting
The error persists after altering the column. Confirm the change actually applied with \d+; some clients cache metadata. Reconnect your session.
ALTER FOREIGN TABLE fails with a permission error. You need to be the table owner or a superuser. Ask an administrator.
The error names a column you did not define. The foreign table may be defined with OPTIONS (columns '...') or a remote view whose schema changed. Re-inspect the remote object.
It works for one query but not another. The failing query probably selects a column the working one does not. Narrow down with SELECT * on the foreign table, then add columns one at a time.
If that did not work, drop and recreate the foreign table with explicit, supported types, and if the remote type is genuinely unsupported, expose a cast view on the remote side. As a last resort, export the remote data to a supported format and load it locally.
Frequently Asked Questions
What does HV004 actually mean?
It is the SQLSTATE for fdw_invalid_data_type: the foreign data wrapper rejected a column's data type, usually because the local foreign table declaration does not match a type the wrapper can handle.
Does restarting PostgreSQL fix HV004?
No. It is a schema/mapping error, not a runtime glitch. Restarting changes nothing; you must correct the column type or cast the value.
Which types does postgres_fdw support?
It supports the standard PostgreSQL types such as integers, numeric, text, varchar, boolean, date, timestamp, and uuid. Arrays, enums, and custom types often need casting on the remote side.
Will dropping the foreign table delete remote data?
No. A foreign table is only a local definition. Dropping it removes the local object, not the data on the remote server. Still confirm before running the drop.
Can I fix HV004 without changing the foreign table?
Sometimes, by casting the column in your SELECT. That is a stopgap; other queries will still fail until the definition is corrected.
Does this affect all FDWs?
Yes, HV004 is generic. The exact supported types differ by wrapper (postgres_fdw, file_fdw, oracle_fdw, and others), so check the documentation for the one you use.