[PG] PreparedQuery results in `ambiguous_parameter` error (42P08)
See original GitHub issueVersion
4.2.1
Context
I encountered this error while refactoring a query to avoid unnecessary updates (similar to this StackOverflow post):
-- Before (works)
UPDATE repository
SET name = $2, description = $3
WHERE id = $1;
-- After (doesn't work)
UPDATE repository
SET name = $2, description = $3
WHERE id = $1 AND (repository.*) IS DISTINCT FROM ($1, $2, $3);
Running this query with PgPool.preparedQuery(...).execute(Tuple.of(...))
results in the following error:
2021-12-09 10:39:14,228 ERROR [com.example] (main @coroutine#2) CON: io.vertx.pgclient.PgException: ERROR: could not determine data type of parameter $2 (42P08)
at io.vertx.pgclient.impl.codec.ErrorResponse.toException(ErrorResponse.java:31)
at io.vertx.pgclient.impl.codec.PrepareStatementCommandCodec.handleErrorResponse(PrepareStatementCommandCodec.java:90)
at io.vertx.pgclient.impl.codec.PgDecoder.decodeError(PgDecoder.java:246)
at io.vertx.pgclient.impl.codec.PgDecoder.decodeMessage(PgDecoder.java:132)
at io.vertx.pgclient.impl.codec.PgDecoder.channelRead(PgDecoder.java:112)
This is presumably because the client creates a prepared statement without explicit data types and Postgres tries to infer the type “…from the context in which the parameter is first referenced”, which I believe is actually in WHERE
(ambiguous) and not in SET
.
Expected
Ideally it would be possible to either create a prepared statement with explicit data types from a tuple (PrepareStatementCommand
and PrepareStatementCommandCodec
) so the statement is created as:
-PREPARE foo AS
+PREPARE foo (bigint, text, text) AS
UPDATE repository
SET name = $2, description = $3
WHERE id = $1 AND (repository.*) IS DISTINCT FROM ($1, $2, $3);
Perhaps 42P08
should be added to the list of indeterminate error codes as well.
Extra
As discussed in previous issues (#699 and #851), not defining explicit data types is an explicit choice:
We do not provide the parameters because some queries might use a different parameter types at execution time and that would result in a cast error in PG (e.g using a function that take any parameter as input, if you bind it with integer, then it will fail for a string).
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
@rgmz 4.2.2 will be released this week as soon as Netty 4.1.72.Final is released
it seems also one solution is to force the type in the query, e.g