Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
SQLSTATE 42P18 is indeterminate_datatype. When a statement with $1, $2 is prepared (PREPARE, or a driver such as node-postgres, JDBC or psycopg using server-side parameters), PostgreSQL works out each parameter's type from where it is used. If $1 only appears in a place that accepts any type, there is nothing to infer from, and the statement fails before it runs.
The fix is always the same: tell PostgreSQL the type, with $1::text or a typed PREPARE. 3 cases reproduced on PostgreSQL 17.
01$1 passed to json_build_object
json_build_object accepts values of any type, so 'name', $1 gives no hint about what $1 is.
Setup
CREATE TABLE users (id int PRIMARY KEY, name text);
INSERT INTO users VALUES (1, 'kim');CREATE TABLE
INSERT 0 1PREPARE user_json AS SELECT json_build_object('id', id, 'name', $1) FROM users WHERE id = 1;ERROR: 42P18: could not determine data type of parameter $1PREPARE user_json(text) AS SELECT json_build_object('id', id, 'name', $1) FROM users WHERE id = 1;
SELECT name, parameter_types FROM pg_prepared_statements;PREPARE
name | parameter_types
-----------+-----------------
user_json | {text}
(1 row)Declare the type: PREPARE user_json(text) AS ... and pg_prepared_statements shows parameter_types {text}. Or cast inside the SQL: json_build_object('id', id, 'name', $1::text); executing with 'lee' returns {"id" : 1, "name" : "lee"}.
PREPARE user_json AS SELECT json_build_object('id', id, 'name', $1::text) FROM users WHERE id = 1;
EXECUTE user_json('lee');PREPARE
json_build_object
----------------------------
{"id" : 1, "name" : "lee"}
(1 row)02$1 passed to concat() in an UPDATE
concat also accepts any type. $2 is fine because it is compared with the int column id, but $1 has no type.
Setup
CREATE TABLE tickets (id int PRIMARY KEY, memo text);
INSERT INTO tickets VALUES (1, 'opened');CREATE TABLE
INSERT 0 1PREPARE add_memo AS UPDATE tickets SET memo = concat(memo, ' / ', $1) WHERE id = $2;ERROR: 42P18: could not determine data type of parameter $1PREPARE add_memo(text, int) AS UPDATE tickets SET memo = concat(memo, ' / ', $1) WHERE id = $2;
SELECT name, parameter_types FROM pg_prepared_statements;PREPARE
name | parameter_types
----------+-----------------
add_memo | {text,integer}
(1 row)PREPARE add_memo(text, int) AS ... is accepted with parameter_types {text,integer}. Casting $1::text inside concat works the same way: the UPDATE runs and memo becomes "opened / closed".
PREPARE add_memo AS UPDATE tickets SET memo = concat(memo, ' / ', $1::text) WHERE id = $2;
EXECUTE add_memo('closed', 1);
SELECT * FROM tickets;PREPARE
UPDATE 1
id | memo
----+-----------------
1 | opened / closed
(1 row)03A parameter used only in $1 IS NULL (optional search filter)
The "skip the filter when empty" pattern ($1 IS NULL OR status = $2) uses $1 only in IS NULL, which works for any type. $2 gets text from status, but $1 gets nothing.
Setup
CREATE TABLE orders (id int PRIMARY KEY, status text);
INSERT INTO orders VALUES (1, 'new'), (2, 'paid');CREATE TABLE
INSERT 0 2PREPARE find_orders AS SELECT id, status FROM orders WHERE ($1 IS NULL OR status = $2) ORDER BY id;ERROR: 42P18: could not determine data type of parameter $1PREPARE find_orders AS SELECT id, status FROM orders WHERE ($1::text IS NULL OR status = $2) ORDER BY id;
SELECT name, parameter_types FROM pg_prepared_statements;PREPARE
name | parameter_types
-------------+-----------------
find_orders | {text,text}
(1 row)Write $1::text IS NULL; pg_prepared_statements shows {text,text}. With (NULL, NULL) it returns all rows (new, paid); with ('paid', 'paid') it returns only paid.
PREPARE find_orders AS SELECT id, status FROM orders WHERE ($1::text IS NULL OR status = $2) ORDER BY id;
EXECUTE find_orders(NULL, NULL);
EXECUTE find_orders('paid', 'paid');PREPARE
id | status
----+--------
1 | new
2 | paid
(2 rows)
id | status
----+--------
2 | paid
(1 row)FAQ
How do I fix could not determine data type of parameter $1?
Add an explicit cast where the parameter is used, for example $1::text or $1::int, or declare the types in PREPARE name(text, int).
Why does it happen only with json_build_object or concat?
Those functions accept arguments of any type, so PostgreSQL cannot infer a type for the parameter from them.
Why does ($1 IS NULL OR col = $2) fail?
IS NULL works for any type, so $1 gets no type. Cast it: $1::text IS NULL.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12