Diagnostic Queries
Symptoms
The expression in a WHERE (or HAVING/CHECK/ON) clause did not evaluate to boolean. PostgreSQL is strict about boolean context and raises SQLSTATE 42804 (datatype_mismatch).
- Often from
WHERE flag = 1on a non-boolean integer, orWHERE somecolumnthat is text. - The message names the offending type, e.g.
integer. - PostgreSQL does not implicitly treat non-zero numbers as true.
What the server log shows
ERROR: argument of WHERE must be type boolean, not type integer
LINE 1: SELECT * FROM t WHERE 1;
^
Why PostgreSQL raises this — what the manual says
As Section 9.1 Logical Operators explains:
A WHERE, HAVING, or CHECK condition must evaluate to the boolean type, but the supplied expression has some other type; turn it into a comparison (for example col = value or col IS NOT NULL) or cast it explicitly to boolean.
WHERE filters rows by evaluating a boolean predicate per row. PostgreSQL does not coerce integers or strings to boolean, so a non-boolean expression in that position is a type mismatch and fails with 42804.
Common causes
- Treating an integer/text column as a truthy flag (
WHERE activewhereactiveisint). - Forgetting the comparison operator (
WHERE statusinstead ofWHERE status = 'open'). - Porting code from a database with truthy semantics.
How to fix it
- Write an explicit comparison:
WHERE active = 1orWHERE active <> 0. - Use a real boolean column/type where you want truthiness.
- Cast intentionally if needed:
WHERE (active)::boolean(note: only 0/1-style ints cast cleanly).
Related & next steps
Reference: PostgreSQL 18 Section 9.1 “Logical Operators”.
Thanks — noted. This helps keep the database accurate.