Skip to content

Support UUID columns in pgsql queries with string parameters - #829

Open
devfrey wants to merge 1 commit into
staabm:mainfrom
devfrey:fix-600-pgsql-uuid-params
Open

devfrey wants to merge 1 commit into
staabm:mainfrom
devfrey:fix-600-pgsql-uuid-params

Conversation

@devfrey

@devfrey devfrey commented Nov 13, 2026 •

Copy link
Copy Markdown

Fixes #600

This makes queries that compare a uuid column with a string parameter work on PostgreSQL.

Before, a query like this got no result type, and phpstan-dba reported an error for it:

$conn->fetchAssociative('SELECT name FROM users WHERE id = :id', ['id' => $id]); // string $id
Query error: SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for type uuid: "1970-01-01"

phpstan-dba checks a query by running it with a stand-in value for each parameter. For strings that stand-in was a date, '1970-01-01'. MySQL accepts that for most columns, but PostgreSQL rejects it for a uuid column because it isn't a valid uuid. jsonb, enum and inet columns had the same problem.

On PostgreSQL, string parameters are now filled in as NULL instead. PostgreSQL accepts NULL for every column type, so the query runs and the result type is inferred again:

-SELECT name FROM users WHERE id = '1970-01-01' LIMIT 0
+SELECT name FROM users WHERE id = NULL LIMIT 0

phpstan-dba only looks at the result columns, so it doesn't matter that no rows match.

I tried a range of query shapes on Postgres 14 and 18 (IN, ANY, coalesce, CASE, function calls, INSERT and UPDATE). NULL gives the same result types as the old date value. The one exception is the DATE :x syntax, which doesn't work with real prepared statements either.

What stays the same:

  • MySQL and MariaDB behave exactly as before. The recorded test caches didn't need updating.
  • Other parameters (constant strings, numbers, booleans, dates) keep their current stand-in values.

Tests: I added a uuid table to the PostgreSQL test schema, with type inference and rule tests for uuid, jsonb and enum columns. Four existing PostgreSQL error expectations now show NULL where they used to show the date.

One thing to be aware of: PostgreSQL is detected through the database connection, and ReplayQueryReflector and ChainedReflector don't have one. In those setups the old date value is still used. In replay mode, the queries then wouldn't match the recorded ones. Record and replay isn't supported on PostgreSQL yet (#353), so nothing breaks today, but it will need handling when that is added.

PostgreSQL parses a quoted literal with the input function of the type it
is compared to. The '1970-01-01' stand-in for a non-constant string
parameter is therefore rejected by uuid, jsonb, enum, inet and integer
columns (SQLSTATE 22P02). The query got no result type, and the rules
reported a false error.

On PostgreSQL, replace such parameters with an untyped NULL instead.
PostgreSQL resolves it from context like a bind parameter, never runs a
type input function on it, and picks the same function overloads and
result column types as for the quoted literal.

Only non-constant, non-numeric strings change, including nullable strings
and lists of strings. Constant, numeric, int, bool, float and DateTime
parameters keep their simulated values, and MySQL/MariaDB is unaffected.

PostgreSQL is detected through the reflector's datasource, so wrapped
reflectors such as RecordingQueryReflector are recognised as well.
@devfrey
devfrey force-pushed the fix-600-pgsql-uuid-params branch from 7c511d1 to 55e6d96 Compare November 13, 2026 15:52
@devfrey devfrey changed the title Simulate non-constant string parameters as NULL on PostgreSQL Support UUID columns in pgsql queries with string parameters Nov 13, 2026
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.

pgsql support for uuid type

1 participant