Re: [SQL] Index on date_trunc

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
Cc: Christophe Labouisse <labouiss(at)cybercable(dot)fr>, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] Index on date_trunc
Date: 1999-05-03 13:52:54
Message-ID: 19798.925739574@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il> writes:
> At 08:19 +0300 on 30/04/1999, Christophe Labouisse wrote:
>> create index ns_dt1_idx on netstats (date_trunc('day',NS_DATE) datetime_ops);

> Seems as if the syntax requires that all the arguments for the function
> should be attributes.

Yes, I believe that's the case. It's not only the parser that's
limited, either: the physical representation of indices doesn't
currently have room for anything more than a function OID to define
what the index sort function is. So there's noplace to put a constant
value.

> Solution: create a function that hides the 'day' in it:

Good thought, but it doesn't actually work, as you'll find as soon
as there are any entries in the table:

insert into test values ('1999-08-01 10:15');
ERROR: SQL-language function not supported in this context.

(6.4.x gives a misleading error message, but it's the same restriction.)

It'd be nice to allow SQL functions to be used for indexes, but I'm not
sure what it'd take to make it happen. At the very least there'd have
to be some drastic restrictions on what the function could do (imagine
the carnage if the function tries to modify the table the index is
being built for...)

Bottom line is there's no easy way to do this right now :-(. What I'd
suggest is keeping a separate column that is the day part of the date
and indexing that. You could use a rule to update that column
automatically whenever the main timestamp column is set. Alternatively,
try to restructure your queries so that you don't actually need an index
on the day part of the date...

regards, tom lane

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message pierre 1999-05-03 14:06:53 Re: [SQL] Slow Inserts Again
Previous Message Jan Wieck 1999-05-03 13:52:11 Re: [SQL] Slow Inserts Again