Sentry Answers>Postgres>

Don't Use NOT IN Operator

Don't Use NOT IN Operator

Evan Hicks

The ProblemJump To Solution

The NOT IN operator in Postgres seems like a useful opposite to the IN operator, but it is actually recommended by Postgres to not use this operator in your queries. In fact, they recommend you don’t use any combination of NOT with IN operators because the NOT IN operator behaves strangely with NULL values., e.g. NOT (x IN (SELECT...)).

Click to Copy
SELECT * FROM foo WHERE col NOT IN (1, NULL);

This query will never return any rows, no matter what values are in the col column. This happens because of the way NULL is evaluated with an IN condition. col IN (1, NULL) returns either TRUE if col is 1, or NULL. If you then apply a NOT to that, NOT(NULL) is still NULL. Since NULL is not TRUE, this query will never have any TRUE values and so no rows will be returned.

Click to Copy
SELECT * FROM foo WHERE col NOT IN (SELECT other_col FROM bar);

This query will not return any rows if any value from bar.other_col is NULL. Even if other_col is never NULL, this query can also suffer from extreme performance issues once the dataset gets above a certain size. That means that the performance might be okay in small tests, but once your queries get above a certain size you will likely see performance degrade by orders of magnitude.

The Solution

The NOT IN operator is fine to use for comparing against static lists.

Click to Copy
SELECT * FROM foo WHERE col NOT IN (1, 2, 3);

As long as you are comparing to small lists of constant values, it might even be advisable to use.

Further Reading

If you’re looking to get a deeper understanding on this solution, or application monitoring in general, take a look at the following articles:

  • ResourcesWhat is Distributed Tracing
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski

    Listen to Syntax

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.