pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: lucas@lucas-nussbaum.net
To: pgsql-bugs@postgresql.org
Subject: BUG #14608: no index scan with NOT IN and ENUM
Date: Fri, 31 Mar 2017 18:53:01 +0000
Message-ID: <20170331185301.2690.64421@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:      14608
Logged by:          Lucas Nussbaum
Email address:      lucas@lucas-nussbaum.net
PostgreSQL version: 9.6.2
Operating system:   Linux (Debian stretch)
Description:        

Hi,

I augment pgbench's pgbench_accounts table with an additional column that
only contains '0', '1', '2', '3' or '4'.
I set the type of this column to enum('0', '1', '2', '3', '4').
I forbid null values.
I set an index on that column.

When I do:
explain analyze select * from tt where d = '0';
the index is used, as expected.

When I do:
explain analyze select * from tt where d not in ('1', '2', '3', '4');
(which should always produce the same result as the previous query)
the index is not used and a seq scan + filter is used instead.
I would expect postgresql to figure out that "not in ('1', '2', '3', '4')"
is equivalent to "d = '0'" given that the column is NOT NULL. And use the
index.

The following code demonstrates this:

# initialize database
pgbench -i -s 100 bench

# create table from pgbench data
create table tt as select *, (aid%5)::text as d from pgbench_accounts ;
# remove lots of '0' rows (this isn't relevant, in the end, but I thought it
might)
delete from tt where d='0' and aid % 100000 <> 0;
# change column type and set NOT NULL
create type dt as enum('0', '1', '2', '3', '4');
alter table tt alter column d type dt using d::dt;
alter table tt alter column d set not null;
# add index
create index tt_d ON tt(d);

# check values and frequency:
bench=# select d, count(*) from tt group by d;
 d |  count  
---+---------
 0 |     100
 3 | 2000000
 4 | 2000000
 2 | 2000000
 1 | 2000000
(5 rows)


First case: explain analyze select * from tt where d = '0';
=> index scan, OK

Second case: explain analyze select * from tt where d not in ('1', '2', '3',
'4');
=> seq scan :-(


Thanks!

Lucas


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


view thread (5+ messages)  latest in thread

Message-ID: <20170331185301.2690.64421@wrigleys.postgresql.org>
Permalink:  ../20170331185301.2690.64421@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170331185301.2690.64421@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: lucas@lucas-nussbaum.net
  Subject: Re: BUG #14608: no index scan with NOT IN and ENUM
  In-Reply-To: <20170331185301.2690.64421@wrigleys.postgresql.org>
  References: <20170331185301.2690.64421@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