I need to index a Postgres column that consists of mostly NULL values. I don’t want the NULL values to be stored in the index (to make the index smaller, and to speed up row insertion). However, adding a partial index on IS NOT NULL
only lets me search efficiently for all non-null values, not for a specific non-null value.
How can I define an index on the value in this column that excludes only NULL values from the index?
2
Answers
If most of the values are NULL, the most efficient way to search for NULL is a sequential scan of the table. So an index is not going to help you for such a search. Consequently, you might as well go with the partial index; you won’t lose anything.
Since PostgreSQL v13, B-tree indexes "deduplicate" repeated values, and columns with lots of NULL values can benefit from that. So even if you don’t create a partial index, the index will be fairly small (but you will pay the price for index modifications whenever you insert a NULL).
That sounds like one of these:
But none of the above is a partial index. That would be
And it does let you "search efficiently for specific non-null values" while being exactly "an index on the value in this column that excludes only NULL values from the index".