pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: serovov@gmail.com
To: pgsql-bugs@postgresql.org
Cc: serovov@gmail.com
Subject: BUG #14807: Query Planner should ignore nulls last/first condition for not-null fields in btree index [order by]
Date: Fri, 08 Sep 2017 18:03:02 +0000
Message-ID: <20170908180302.25639.40695@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14807
Logged by:          Oleg Serov
Email address:      serovov@gmail.com
PostgreSQL version: 9.6.5
Operating system:   Ubuntu
Description:        

Query Planner should ignore null last/first condition for not-null fields in
btree index.

- I have a table with a field that is not null.
- I have an index on that field.
- I make a query 'ORDER BY field ASC'
- The planner does not use btree-index because default behavior for
postgresql order-by is NULLS FIRST, but for btree-indexes NULLS LAST. 
- However, PostgreSQL knows that the field is not null. But it still does
not use the index.

PostgreSQL should ignore nulls last/nulls first if the field has not null
condition.

Here is the query to reproduce:

ROLLBACK; BEGIN;

CREATE TABLE btree_bug (
    id BIGSERIAL,
    rand BIGINT NOT NULL
);

INSERT INTO btree_bug
    SELECT i, random() * 100000::BIGINT
        FROM generate_series(0, 10000) AS i;

CREATE INDEX ON btree_bug USING btree(rand);

SELECT 'Uses index:';
EXPLAIN ANALYZE SELECT id FROM btree_bug ORDER BY rand DESC NULLS FIRST
LIMIT 10;
EXPLAIN ANALYZE SELECT id FROM btree_bug ORDER BY rand ASC NULLS LAST LIMIT
10;


SELECT 'Does not use index:';
EXPLAIN ANALYZE SELECT id FROM btree_bug ORDER BY rand DESC NULLS LAST LIMIT
10;
EXPLAIN ANALYZE SELECT id FROM btree_bug ORDER BY rand ASC NULLS FIRST LIMIT
10;



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


Message-ID: <20170908180302.25639.40695@wrigleys.postgresql.org>
Permalink:  ../20170908180302.25639.40695@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170908180302.25639.40695@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: serovov@gmail.com
  Subject: Re: BUG #14807: Query Planner should ignore nulls last/first condition for not-null fields in btree index [order by]
  In-Reply-To: <20170908180302.25639.40695@wrigleys.postgresql.org>
  References: <20170908180302.25639.40695@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox