Skip site navigation (1) Skip section navigation (2)

Re: index over timestamp not being used

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: arnaulist(at)andromeiberica(dot)com
Cc: PostgreSQL Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: index over timestamp not being used
Date: 2007-07-24 19:24:55
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance
Arnau <arnaulist(at)andromeiberica(dot)com> writes:
> timestamp_in          | timestamp without time zone | default now()

> SELECT ...
>   transactions t
>   LEFT OUTER JOIN statistics s ON t.transaction_id = s.transaction_id
>   t.timestamp_in >= to_timestamp('20070101', 'YYYYMMDD')
> GROUP BY date, t.type_id;

to_timestamp() produces timestamp *with* timezone, so your WHERE query
is effectively
    t.timestamp_in::timestamptz >= to_timestamp('20070101', 'YYYYMMDD')
which doesn't match the index.

The first question you should ask yourself is whether you picked the
right datatype for the column.  IMHO timestamp with tz is the more
appropriate choice in the majority of cases.

If you do want to stick with timestamp without tz, you'll need to cast
the result of to_timestamp to that.

Alternatively, do you really need to_timestamp at all?  The standard
timestamp input routine won't have any problem with that format:
    t.timestamp_in >= '20070101'

			regards, tom lane

In response to


pgsql-performance by date

Next:From: ArnauDate: 2007-07-24 19:31:07
Subject: Re: index over timestamp not being used
Previous:From: Campbell, LanceDate: 2007-07-24 18:41:22
Subject: Re: Table Statistics with pgAdmin III

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group