Skip to main content

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

query.sql · SQLReported on line 2
SELECT id FROM accountsWHERE deleted_at = NULL;
pyproject.toml · TOML
[tool.skylos.sql]dialect = "postgresql"

What Skylos leaves alone

query.sql · SQL
SELECT id FROM accountsWHERE deleted_at IS NULL;

pyproject.toml: same as in the flagged example.

Why Skylos leaves it alone

IS NULL selects rows whose deletion timestamp is missing.

How Skylos decides

  1. Install the postgres extra and run skylos . --quality. Set dialect = "postgresql" under [tool.skylos.sql] in pyproject.toml for the folder containing the queries.
  2. 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.

query.sql · SQL · the fix, against the flagged file
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.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.