Re: Ignoring index on (A is null), (A is not null) conditions

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Cestmir Hybl <cestmirl(at)freeside(dot)sk>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Ignoring index on (A is null), (A is not null) conditions
Date: 2003-10-30 14:04:06
Message-ID: 20031030140406.GA7870@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Thu, Oct 30, 2003 at 12:34:15 +0100,
Cestmir Hybl <cestmirl(at)freeside(dot)sk> wrote:
> Are you seeing this question as totally off-topic in this list, or there is
> really no one who knows something about indexing "is null" bits in postgres?

There was some talk about IS NULL not being able to use indexes (unless
you specifically created a partial index using that condition) a number
of months ago. You could search through the archives if you are interested
in what was said. My memory is that people thought it would be a good idea
but that it wasn't that important to get done.

>
> > Hi,
> >
> > suppose, for simplicity, there is a table with index like this:
> >
> > create table TABLE1 (
> > A integer
> > );
> > create index TABLE1_A on TABLE1 (A);
> >
> > My question is: why psql (7.3.3) does not use index when filtering by A IS
> > NULL, A IS NOT
> > NULL expressions?

That is a Postgres limitation. If there are only a few null values, but you
query for them a lot it may be worth creating a partial index.

> >
> > In fact, I need to filter by expression ((A is null) or (A > const)).

This is a whole different matter. Using an indexed search on > is not
necessarily a good idea. Unless you know only a small fraction of the
table (often 10% is quoted) is greater than the constant, a sequential
scan is probably a better plan than an index scan. If you know that
there is only a small fraction of the values above constant and you
know some large value greater than all values, you can try using a between
comparison to coax the planner into doing an index scan.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message mallah 2003-10-30 14:12:00 Re: Query puts 7.3.4 on endless loop but 7.4beta5 is fine. [ with better indenting ]
Previous Message Franco Bruno Borghesi 2003-10-30 13:56:29 Re: Ignoring index on (A is null), (A is not null)