Re: Indexes on NULL's and order by ... limit N queries

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Maxim Boguk <mboguk(at)masterhost(dot)ru>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Indexes on NULL's and order by ... limit N queries
Date: 2008-12-01 21:28:54
Message-ID: 1390.1228166934@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Maxim Boguk <mboguk(at)masterhost(dot)ru> writes:
> But why? NULL's have some special representation in index which don't work same as normal values?

In general, NULLs don't work the same as normal values, no.

The reason this particular query isn't working as you are expecting is
that "foo IS NULL" isn't seen as an ordering constraint by the planner's
pathkey machinery, and so the query doesn't appear to match the index
order. You could work around it by explicitly specifying a matching
ordering:

SELECT * from cluster_weight where cluster_weight.rubric_id IS NULL
ORDER BY rubric_id, pos LIMIT 5;
^^^^^^^^^^

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message justin 2008-12-01 22:11:10 Re: Monty on MySQL 5.1: "Oops, we did it again"
Previous Message Tom Lane 2008-12-01 21:09:40 Re: Nested Loop Left Join always shows rows=1