Re: Date indexing

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Ian Cass" <ian(dot)cass(at)mblox(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Date indexing
Date: 2002-04-23 13:56:38
Message-ID: 11066.1019570198@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

"Ian Cass" <ian(dot)cass(at)mblox(dot)com> writes:
> I'll only ever be referencing this data to a granularity of 1 day, so is
> there a way I can use a function to index this so that the date column in
> the index is text containing just DD/MM/YYYY?

Don't use text, use a date.

regression=# create table foo (f1 timestamp without time zone);
CREATE
regression=# create index fooi on foo(date(f1));
CREATE
regression=# explain select * from foo where date(f1) = current_date;
QUERY PLAN
-----------------------------------------------------------------
Index Scan using fooi on foo (cost=0.00..17.09 rows=5 width=8)
Index Cond: (date(f1) = date('now'::text))
(2 rows)

(This is with current devel sources, as you can possibly tell from the
EXPLAIN format, but should work fine in 7.2; less sure about 7.1.)

Note that a coercion from timestamp *with* timezone to date will
not be allowed as an index function, because it's dependent on
external information --- viz, your timezone. So I had to use
timestamp without time zone in this example.

One might also wonder why you're not just storing the column as type
date rather than timestamp in the first place, if you're certain you
do not need finer resolution.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Gamache 2002-04-23 14:02:28 Suggestions for Large DB Dump/Reload
Previous Message tony 2002-04-23 13:33:13 some pl/pgsql help please

Browse pgsql-sql by date

  From Date Subject
Next Message Ian Cass 2002-04-23 14:03:44 Re: Date indexing
Previous Message Dima Tkach 2002-04-23 12:56:31 Re: Date indexing