Fix PostgreSQL Error HV090: FDW String Length Buffer
In this article
PostgreSQL error HV090 — fdw_invalid_string_length_or_buffer_length means a foreign data wrapper (usually tds_fdw or another ODBC-based FDW connecting to SQL Server) received a value that does not fit in the buffer it allocated for it. In practice this almost always happens when a remote column contains a string longer than the declared column length, or when the FDW guesses a character column's size incorrectly during import. The fix is to correct the column type or size on the PostgreSQL side of the foreign table definition, so the FDW allocates the right buffer. This guide walks through diagnosing and fixing it.
What You'll Need
- Superuser or owner access to the PostgreSQL database with the failing foreign table
- The extension installed (e.g. tds_fdw) and its connection details already working for other queries
- Access to the remote database (SQL Server or similar) to inspect the real column definition, or someone who has it
- Able to run SQL via psql or a client like pgAdmin
Step-by-Step
- Identify the failing column. Run a query selecting one column at a time from the foreign table, or select only the primary key first and add columns until the query fails. The last column added before the HV090 error is your culprit. Alternatively, check the PostgreSQL server log — many FDWs log the column name with the error.
- Check the current foreign table definition. In psql, run
\d+ your_foreign_tableor queryinformation_schema.columnsfor that table. Note the declared type of the failing column, e.g.varchar(255)ortext. - Compare with the remote column. On the remote SQL Server, run
sp_help 'schema.table'or queryINFORMATION_SCHEMA.COLUMNSto see the true data type. Typical mismatches that cause HV090: the remote column isvarchar(max)ornvarchar(max)but the foreign table declares a fixed length; the remote column isnvarchar(double-byte) but PostgreSQL treats it as single-byte, halving the effective buffer; or the remote column was widened after the foreign table was created. - Fix the foreign table definition. The most reliable approach is to drop and recreate the foreign table with corrected types. Map long remote strings to
textand Unicode remote columns appropriately:
DROP FOREIGN TABLE IF EXISTS public.my_table;
DROP FOREIGN TABLE IF EXISTS public.my_table;
CREATE FOREIGN TABLE public.my_table (
id integer,
long_note text,
short_code varchar(50)
)
SERVER my_remote_server
OPTIONS (schema_name 'dbo', table_name 'MyTable');
Alternatively, if you imported the table with IMPORT FOREIGN SCHEMA, re-run the import — but be aware some FDWs still guess sizes for varchar(max) columns, in which case manual definition with text is the safe route.
- Set character-type options if using tds_fdw. tds_fdw supports per-column and per-table options such as
character_seton the server or table. If the remote data is UTF-8 or Unicode and you are seeing length errors on short-looking values, make sure thecharacter_setserver option matches the remote server's actual encoding (for exampleUTF-8), because a mismatch changes byte lengths and can overflow the buffer. - Check row_size_option for tds_fdw. If large rows still fail, tds_fdw has a table-level option
row_size_optionwith values likeestimateoruse_default. Try setting it toestimateso the driver sizes the row buffer from actual metadata instead of defaults. - Re-test the query. Run
SELECT the_problem_column FROM public.my_table LIMIT 50;. If it returns cleanly, run your original full query. Then re-test with a filter (WHERE clause) too, since pushdown can change the execution path. - Verify data integrity. Once the query works, spot-check a few of the longest values against the remote database to confirm nothing is truncated or garbled, especially non-ASCII characters if the column holds Unicode text.
Common Mistakes
- Widening the column just slightly (e.g. varchar(255) to varchar(512)) instead of switching to
text. If the remote column is unbounded, usetextso you never hit the problem again. - Ignoring Unicode: an
nvarchar(100)column holds 100 characters, but if the FDW computes buffer size in bytes with the wrong character set, multi-byte characters overflow it. Fix the character set, not just the length. - Editing the FDW extension configuration files (like freetds.conf) when the real problem is the foreign table definition — the error is about the buffer between PostgreSQL and the wrapper, not usually the network connection.
- Forgetting to drop dependent views. If a view references the foreign table,
DROP FOREIGN TABLEwill fail or cascade; drop the views first and recreate them after. - Testing only with a handful of short rows. Always test against the specific rows that triggered the error.
Tips
- Use
\d+in psql to see both column types and any column-level FDW options at a glance. - For remote
varchar(max)/nvarchar(max)columns, always declaretexton the PostgreSQL side rather than relying on import guessing. - Keep foreign table definitions in a versioned migration script so fixing or redeploying them is repeatable.
- If many tables are affected, script the regeneration of definitions from the remote catalog with corrected type mappings rather than editing each by hand.
Troubleshooting
Still failing after widening the column? Check the PostgreSQL server log for the full error context — it usually names the column and the buffer size expected versus received. Confirm you recreated the table (a stale definition on a different schema can be confusing) by querying information_schema.columns again.
Error only on some rows: those rows contain values at or beyond the old length limit or multi-byte characters. That confirms a length or encoding issue, not corruption.
Error with tds_fdw and correct column types: try the row_size_option 'estimate' table option, and verify character_set on the server object matches the remote server. Also make sure your tds_fdw extension version is current, since older versions had known buffer-sizing bugs for max-type columns — upgrade via your package manager and restart PostgreSQL.
Cannot change the foreign table: as a workaround, query the remote column cast or truncated server-side if the FDW pushes down expressions, but this is second-best; fixing the definition is the correct solution.
Frequently Asked Questions
What does SQLSTATE HV090 mean?
HV090 is the SQLSTATE code for fdw_invalid_string_length_or_buffer_length — a foreign data wrapper received a string that does not fit the buffer allocated for it. The HV class covers all FDW-specific errors.
Does this error mean my data is corrupted?
No. The remote data is usually fine; PostgreSQL and the FDW simply disagree about how large the column's values can be, so the buffer is too small for valid data.
Which FDWs cause HV090?
Most commonly tds_fdw (SQL Server/Sybase) and ODBC-based wrappers. It appears when the wrapper maps a remote string column to an undersized local type or mis-handles encodings.
Is editing freetds.conf the fix?
Rarely. Connection settings like TDS version matter for other errors, but HV090 is a column-definition or character-set problem. Fix the foreign table first.
Will IMPORT FOREIGN SCHEMA fix it automatically?
Not always. Some FDW versions still import varchar(max) columns with a guessed size. If re-importing doesn't help, define the table manually with text for long columns.
Does the error affect writes as well as reads?
Yes. INSERT or UPDATE statements that send long values through an undersized column definition can raise the same HV090 error, so the same definition fix applies.