Received: from sss.sss.pgh.pa.us (sss.pgh.pa.us [206.210.65.6]) by hub.org (8.9.3/8.9.3) with ESMTP id JAA89142 for ; Mon, 3 May 1999 09:56:36 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.sss.pgh.pa.us (8.9.1/8.9.1) with ESMTP id JAA19800; Mon, 3 May 1999 09:52:54 -0400 (EDT) To: Herouth Maoz cc: Christophe Labouisse , pgsql-sql@postgreSQL.org Subject: Re: [SQL] Index on date_trunc In-reply-to: Your message of Mon, 3 May 1999 12:42:05 +0300 Date: Mon, 03 May 1999 09:52:54 -0400 Message-ID: <19798.925739574@sss.pgh.pa.us> From: Tom Lane Herouth Maoz 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