pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Boes <jboes@nexcerpt.com>
To: pgsql-admin@postgresql.org
Subject: Timestamp indexes aren't used for ">="
Date: Fri, 09 Nov 2001 16:28:46 -0500
Message-ID: <9shhnf$23ks$1@news.tht.net> (raw)

We have a table which has approximately 400,000 rows.
It has 17 columns, and 4 indexes.  The primary key
is a int4 (filled by a sequence), additionally we
have two more int4 indexes and a timestamp index.

The documentation for create index
(http://www.postgresql.org/idocs/index.php?sql-createindex.html)
gives a nice query to display the list of known operators
for each of the index types.  Running this yields the
following useful data....

  acc_name |   ops_name    | ops_comp
----------+---------------+----------
  btree    | timestamp_ops | <
  btree    | timestamp_ops | <=
  btree    | timestamp_ops | =
  btree    | timestamp_ops | =
  btree    | timestamp_ops | >
  btree    | timestamp_ops | >=

Now, if this is true.... how can this result be consistant?

(selecting by equality with timestamp value)

explain select fetch_status, count(*)
from stat_fetch where fetch_date = '2001-11-08 00:00:00-05'
group by fetch_status;

NOTICE:  QUERY PLAN:

Aggregate  (cost=8.05..8.06 rows=1 width=12)
   ->  Group  (cost=8.05..8.05 rows=1 width=12)
         ->  Sort  (cost=8.05..8.05 rows=1 width=12)
               ->  Index Scan using ix_stat_fetch_3 on stat_fetch 
(cost=0.00..8.04 rows=1 width=12)

EXPLAIN

.....BUT.....

(selecting by comparison ">=" to timestamp value)

explain select fetch_status, count(*)
from stat_fetch where fetch_date >= '2001-11-08 00:00:00-05'
group by fetch_status;

NOTICE:  QUERY PLAN:

Aggregate  (cost=12322.64..12522.06 rows=3988 width=12)
   ->  Group  (cost=12322.64..12422.35 rows=39884 width=12)
         ->  Sort  (cost=12322.64..12322.64 rows=39884 width=12)
               ->  Seq Scan on stat_fetch  (cost=0.00..8917.33 
rows=39884 width=12)

EXPLAIN

.....AND YET.....

set enable_seqscan to off;
explain select fetch_status, count(*)
from stat_fetch where fetch_date >= '2001-11-08 00:00:00-05'
group by fetch_status;
NOTICE:  QUERY PLAN:

Aggregate  (cost=38193.97..38393.39 rows=3988 width=12)
   ->  Group  (cost=38193.97..38293.68 rows=39884 width=12)
         ->  Sort  (cost=38193.97..38193.97 rows=39884 width=12)
               ->  Index Scan using ix_stat_fetch_3 on stat_fetch 
(cost=0.00..34788.66 rows=39884 width=12)

EXPLAIN

Note the cost of the "Index" scan is actually a higher estimate
than the sequential scan.

This leads me to the conclusion that either postgres has a
bug that is preventing it from actually using the operator
that is defined on the index (thus falling back to the non-
indexed comparison), or explain is broken, or my understanding
of indexes is broken.


-- 
Jeff Boes                                             vox 616.226.9550
Database Engineer                                     fax 616.349.9076
Nexcerpt, Inc.                                      jboes@nexcerpt.com



view thread (5+ messages)  latest in thread

Message-ID: <9shhnf$23ks$1@news.tht.net>
Permalink:  ../9shhnf$23ks$1@news.tht.net/
Also on:    postgresql.org/message-id/9shhnf$23ks$1@news.tht.net

 · 

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-admin@postgresql.org
  Cc: jboes@nexcerpt.com
  Subject: Re: Timestamp indexes aren't used for ">="
  In-Reply-To: <9shhnf$23ks$1@news.tht.net>
  References: <9shhnf$23ks$1@news.tht.net>

* 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