Thread: creating an index on a function

creating an index on a function

From
"Nick Fankhauser"
Date:
Good Morning everyone!

I just tried to create an index on a function with no success. My syntax
seems to match the syntax shown in the docs for Create Index, but I get an
error. Any thoughts would be appreciated. Here is my attempt-

First, I did a select to make sure I had the function syntax correct, and
then tried to create the index:


staging=# select count(*) from event where
date_trunc('day',event_date_time)='07/27/2001';count
-------   27
(1 row)

staging=#
staging=# create index event_day on
event(date_trunc('day',event_date_time));
ERROR:  parser: parse error at or near "'"

Thanks!

-Nick

--------------------------------------------------------------------------
Nick Fankhauser  nickf@ontko.com  Phone 1.765.935.4283  Fax 1.765.962.9788
Ray Ontko & Co.     Software Consulting Services     http://www.ontko.com/



Re: creating an index on a function

From
Brent Verner
Date:
[2002-02-15 09:26] Nick Fankhauser said:
| Good Morning everyone!
| 
| I just tried to create an index on a function with no success. My syntax
| seems to match the syntax shown in the docs for Create Index, but I get an
| error. Any thoughts would be appreciated. Here is my attempt-
| 
| First, I did a select to make sure I had the function syntax correct, and
| then tried to create the index:
| 
| 
| staging=# select count(*) from event where
| date_trunc('day',event_date_time)='07/27/2001';
|  count
| -------
|     27
| (1 row)
| 
| staging=#
| staging=# create index event_day on
| event(date_trunc('day',event_date_time));

You cannot create an index on a function with constant parameters.
This limitation may be removed in a future version, but for now
you have to create a wrapper function that doesn't require the
constant parameter
 CREATE FUNCTION date_trunc_day(TIMESTAMP) RETURNS TIMESTAMP AS '    SELECT date_trunc(''day'',$1); ' LANGUAGE 'sql';

then use this to create your function index.   create index event_day on    event(date_trunc_day(event_date_time));

hth. brent

-- 
"Develop your talent, man, and leave the world something. Records are 
really gifts from people. To think that an artist would love you enough
to share his music with anyone is a beautiful thing."  -- Duane Allman


Re: creating an index on a function

From
Tom Lane
Date:
"Nick Fankhauser" <nickf@ontko.com> writes:
> staging=# create index event_day on
> event(date_trunc('day',event_date_time));
> ERROR:  parser: parse error at or near "'"

You missed the fine print that says the function must be applied to
table column name(s) only.  No constants, no expressions.

You can get around this limitation by defining a custom function that
fills in whatever extra baggage you need.

My own first thought was that you could just use conversion to type
date, but that falls down.  Not for syntax reasons though:

regression=# create table foo (event_date_time timestamp);
CREATE
regression=# create index event_day on foo (date(event_date_time));
ERROR:  DefineIndex: index function must be marked iscachable

This raises a subtle point that you'd better think about before you go
too far in this direction: truncating a timestamp to date is not a very
well-defined operation, because it depends on the timezone setting.
Indexes on functions whose values might vary depend on who's executing
them are a recipe for disaster --- the index is almost certainly going
to wind up corrupted (out of order).
        regards, tom lane


Re: creating an index on a function

From
"Nick Fankhauser"
Date:
Tom, Brent-

Thanks for the thoughtful responses. I now understand why it didn't work and
what I need to change about my approach.

> This raises a subtle point that you'd better think about before you go
> too far in this direction: truncating a timestamp to date is not a very
> well-defined operation, because it depends on the timezone setting.

That's a good point. Fortunately, in this specific application the events
are selected by location as a part of the same operation, so I *think* I'll
be OK- However, there are some other application functions I'm contemplating
for the future which would really get mangled if I lost my TZ information.

Regards,

-Nick