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

Re: Date Index

From: "Ryan Hansen" <ryan(dot)hansen(at)brightbuilders(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Date Index
Date: 2008-11-03 22:57:25
Message-ID: 000b01c93e07$8a2c44e0$9e84cea0$ (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
Incidentally,  extract(date from ts) doesn't work on my install of 8.3
(standard Ubuntu Hardy apt install).  I get a "timestamp units "date" not
recognized" error when I try it.  The field I'm trying to create it on is
"timestamp without time zone".

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org]
On Behalf Of A. Kretschmer
Sent: Friday, October 31, 2008 1:49 AM
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Date Index

am  Thu, dem 30.10.2008, um 14:49:16 -0600 mailte Ryan Hansen folgendes:
> Hey all,
> I?m apparently too lazy to figure this out on my own so maybe one of you
> just make it easy on me. J 
> I want to index a timestamp field but I only want the index to include the
> yyyy-mm-dd portion of the date, not the time.  I figure this would be
where the
> ?expression? portion of the CREATE INDEX syntax would come in, but I?m not
> I understand what the syntax would be for this.
> Any suggestions?


You can create an index based on a function, but only if the function is

test=# create table foo (ts timestamptz);
test=*# create index idx_foo on foo(extract(date from ts));
ERROR:  functions in index expression must be marked IMMUTABLE

To solve this problem specify the timezone:

For the same table as above:
test=*# create index idx_foo on foo(extract(date from ts at time zone

If you have a timestamp whitout timezone it is much easier:
test=# create table foo (ts timestamp);
test=*# create index idx_foo on foo(extract(date from ts));

Hope thats help, Andreas
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA

In response to


pgsql-sql by date

Next:From: Tom LaneDate: 2008-11-03 23:59:17
Subject: Re: Date Index
Previous:From: Tom LaneDate: 2008-11-03 21:20:47
Subject: Re: [SQL] reliable lock inside stored procedure (SOLVED)

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