Re: Ignoring index on (A is null), (A is not null) conditions - Mailing list pgsql-performance

From Bruno Wolff III
Subject Re: Ignoring index on (A is null), (A is not null) conditions
Date
Msg-id 20031030140406.GA7870@wolff.to
Whole thread Raw
In response to Re: Ignoring index on (A is null), (A is not null) conditions  ("Cestmir Hybl" <cestmirl@freeside.sk>)
List pgsql-performance
On Thu, Oct 30, 2003 at 12:34:15 +0100,
  Cestmir Hybl <cestmirl@freeside.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.

pgsql-performance by date:

Previous
From: Franco Bruno Borghesi
Date:
Subject: Re: Ignoring index on (A is null), (A is not null)
Next
From:
Date:
Subject: Re: Query puts 7.3.4 on endless loop but 7.4beta5 is fine. [ with better indenting ]