pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedcreating an index on a function
4+ messages / 3 participants
[nested] [flat]
* creating an index on a function
@ 2002-02-15 14:26 Nick Fankhauser <nickf@ontko.com>
0 siblings, 2 replies; 4+ messages in thread
From: Nick Fankhauser @ 2002-02-15 14:26 UTC (permalink / raw)
To: pgsql-sql
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/
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: creating an index on a function
@ 2002-02-15 15:42 Brent Verner <brent@rcfile.org>
parent: Nick Fankhauser <nickf@ontko.com>
1 sibling, 0 replies; 4+ messages in thread
From: Brent Verner @ 2002-02-15 15:42 UTC (permalink / raw)
To: Nick Fankhauser <nickf@ontko.com>; +Cc: pgsql-sql
[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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: creating an index on a function
@ 2002-02-15 16:00 Tom Lane <tgl@sss.pgh.pa.us>
parent: Nick Fankhauser <nickf@ontko.com>
1 sibling, 1 reply; 4+ messages in thread
From: Tom Lane @ 2002-02-15 16:00 UTC (permalink / raw)
To: nickf@ontko.com; +Cc: 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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: creating an index on a function
@ 2002-02-15 16:27 Nick Fankhauser <nickf@ontko.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 4+ messages in thread
From: Nick Fankhauser @ 2002-02-15 16:27 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; brent@rcfile.org; pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2002-02-15 16:27 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-02-15 14:26 creating an index on a function Nick Fankhauser <nickf@ontko.com>
2002-02-15 15:42 ` Brent Verner <brent@rcfile.org>
2002-02-15 16:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-02-15 16:27 ` Nick Fankhauser <nickf@ontko.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox