Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postgres: using 9999-12-31 vs NULL as expire date

In a Postgres table, I’m storing some events with their expire_date. I’m wondering how to model non-expiring events. Two alternatives:

  1. expire_date = 9999-12-31
  2. expire_date = NULL

The column expire_date will be indexed. From the performance perspective, are there any differences between the two approaches?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If that column is used for range queries, storing NULL is a problem because you always need an OR condition which can be bad for performance.

But luckily, Postgres provides a DATE value that is bigger then all others: infinity so you can store that for the expire_date.

e.g.

insert into events (id, ..., expire_date)
values (1, ..., 'infinity');

Note that the opposite -infinity also exists. It also works for timestamp columns.


A range query on a NULL value could be indexed, if you use coalesce() instead of an OR condition:

create index on events (coalesce(expire_date, 'infinity'))

But you need to use that exact expression in your queries in order to make use of that index, e.g:

select *
from events
where coalesce(expire_date, 'infinity') > date '2022-08-01'

When storing infinity instead of NULL, you don’t need to remember that in your queries.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading