SKY-Q504 · Quality, structure and performance
PostgreSQL filter compares a value to NULL
Medium
A filter compares an expression to NULL with an ordinary equality or inequality operator. Under standard PostgreSQL settings the comparison evaluates to unknown, even when the expression holds NULL.
- Languages
- PostgreSQL
- Quality gate
- listed, not counted by the gate (full and diff scans). This initial PostgreSQL check is report-only.
- Finding type
- review
- Catalog name
- PostgreSQL filter compares with NULL
Why it matters
WHERE keeps rows only when its condition is true. A query intended to find active accounts can quietly return zero rows.
What Skylos flags
SELECT id FROM accountsWHERE deleted_at = NULL;[tool.skylos.sql]dialect = "postgresql"What Skylos leaves alone
SELECT id FROM accountsWHERE deleted_at IS NULL;Why Skylos leaves it alone
IS NULL selects rows whose deletion timestamp is missing.
How Skylos decides
- Install the postgres extra and run
skylos . --quality. Setdialect = "postgresql"under[tool.skylos.sql]in pyproject.toml for the folder containing the queries. - Skylos looks for a literal NULL operand on either side of an equality or inequality comparison in a filter. Assigning NULL to a column has a different meaning.
How to fix it
Use IS NULL to find missing values and IS NOT NULL to find present values.
SELECT id FROM accountsRemoved: WHERE deleted_at = NULL;Added: WHERE deleted_at IS NULL;When it can be wrong
- A database configured with transform_null_equals can rewrite equality to IS NULL.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Use an explicit null test to make the query portable across PostgreSQL settings.