Question

I've just found an interesting partial index in my db:

CREATE INDEX orders_idx
  ON orders
  USING btree
  (status)
  WHERE status IS NULL;

As you see it's completely non-selective and imho completely not usefull, or am I missing something?

Was it helpful?

Solution

If you have queries where you filter by status is null, (as mentioned in the comments,) or need to run maintenance on the entries where the status is null, this index may be useful.

For example from a project I used to work on (that didn't have this type of index,) I had an import process that keeps a log/queue. It sets status to queued when starting, running while in progress, and sets status to complete when finished - but sets it to null if there is an error. That will need to be cleaned up, because items in other tables will not have been correctly set to the values needed. I needed to routinely query on status is null to diagnose specific problems, or run cleanup scripts so the database wasn't full of partial imports.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top