Skip to content

Fix Could not convert DuckDB type: UNKNOWN to Postgres type for parameters used in expressions - #1062

Open
GavinPizza wants to merge 1 commit into
duckdb:mainfrom
GavinPizza:fix/480-prepared-param-cast
Open

GavinPizza wants to merge 1 commit into
duckdb:mainfrom
GavinPizza:fix/480-prepared-param-cast

Conversation

@GavinPizza

@GavinPizza GavinPizza commented Jul 21, 2026

Copy link
Copy Markdown

A prepared or parameterized statement fails when a bind parameter is used inside an expression, e.g. SELECT count(*) FROM t WHERE id = $1 + 1:

ERROR:   (CreatePlan) Cache lookup failed for type 0
WARNING: (GetPostgresDuckDBType) Could not convert DuckDB type: UNKNOWN to Postgres type

pg_duckdb deparses the query and calls DuckDB Prepare() at plan time to learn the result types. DuckDB can't infer a parameter's type inside an expression, so it stays UNKNOWN and result-type resolution fails (same root cause as #396). Since Postgres already resolved the parameter's type, we now deparse each external parameter as CAST($n AS <pgtype>) instead of a bare $n, handing DuckDB the type it needs. A regression test is included.

This also changes the error for CTAS-with-a-parameter (still unsupported): the parameter now gets a type, so DuckDB fails later with "Not all parameters were bound" instead of "Could not find parameter with identifier 1" -- same clean failure, different message.

Known limitation: jsonb parameters still fail (CAST($n AS jsonb) -- DuckDB has no jsonb type); pre-existing, tracked in #516.

Fixes #480.

Parameters used inside an expression (e.g. `WHERE id = $1 + 1`) could not
have their type inferred by DuckDB at plan time, failing with "Could not
convert DuckDB type: UNKNOWN". Deparse external parameters as
CAST($n AS <type>) so DuckDB gets the type Postgres already resolved.

Fixes duckdb#480.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prepared statement error: Could not convert DuckDB type: UNKNOWN to Postgres type

1 participant