question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[PG] PreparedQuery results in `ambiguous_parameter` error (42P08)

See original GitHub issue

Version

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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vietjcommented, Dec 13, 2021

@rgmz 4.2.2 will be released this week as soon as Netty 4.1.72.Final is released

0reactions
vietjcommented, Dec 13, 2021

it seems also one solution is to force the type in the query, e.g

UPDATE repository
SET name = $2::varchar, description = $3::varchar
WHERE id = $1::varchar AND (repository.*) IS DISTINCT FROM ($1::varchar, $2::varchar, $3::varchar);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres Sql `could not determine data type of parameter` by ...
The PostgreSQL driver tries to figure out the type of the parameters to tell those parameters directly to the PostgreSQL Server.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found