The transform_null_equals configuration in Postgres
How to make null = null evaluate to true in Postgres
It is a well-known fact that you should always use IS NULL
to test if some value is null. That is because NULL = NULL
does not evaluate to true in SQL, instead it evaluates to NULL
.
However, if you insist on violating SQL standards and making NULL = NULL
be true, you can. There's a runtime configuration option for that: transform_null_equals
(docs).
SELECT NULL = NULL;-- Returns NULLSET transform_null_equals TO true;SELECT NULL = NULL;-- Returns TRUE
Should you use this option? No.
This only works for specifically = NULL
constructions. In particular, NULL != NULL
will still return NULL
instead of FALSE
, and NULL IN (1, NULL, 2)
will not return TRUE
, even with the option enabled. But it's there if you need it.
transform_null_equals
is off by default, and is available in all versions of Postgres.
Like this article? Follow me on Bluesky or subscribe to the RSS feed to get notified about new articles.