Re: creating an index on a function - Mailing list pgsql-sql

From Tom Lane
Subject Re: creating an index on a function
Date
Msg-id 14110.1013788811@sss.pgh.pa.us
Whole thread Raw
In response to creating an index on a function  ("Nick Fankhauser" <nickf@ontko.com>)
Responses Re: creating an index on a function  ("Nick Fankhauser" <nickf@ontko.com>)
List pgsql-sql
"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


pgsql-sql by date:

Previous
From: Brent Verner
Date:
Subject: Re: creating an index on a function
Next
From: Wei Weng
Date:
Subject: create index on function - why?