pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedRe: Error trying to create a functional index.
5+ messages / 3 participants
[nested] [flat]
* Re: Error trying to create a functional index.
@ 2002-01-07 11:01 CoL <col@mportal.hu>
0 siblings, 2 replies; 5+ messages in thread
From: CoL @ 2002-01-07 11:01 UTC (permalink / raw)
To: pgsql-sql
Steve: of course you can make functional indices:
So why this not workin???? (not workin for me 2 too, pg 7.1.3)
"CREATE INDEX eventdateindex ON event (date_trunc('day', eventstamp));"
----------------
7.5. Functional Indices
For a functional index, an index is defined on the result of a function
applied to one or more columns of a single table. Functional indices can
be used to obtain fast access to data based on the result of function calls.
For example, a common way to do case-insensitive comparisons is to use
the lower:
SELECT * FROM test1 WHERE lower(col1) = 'value';
In order for that query to be able to use an index, it has to be defined
on the result of the lower(column) operation:
CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));
The function in the index definition can take more than one argument,
but they must be table columns, not constants. Functional indices are
always single-column (namely, the function result) even if the function
uses more than one input field; there cannot be multi-column indices
that contain function calls.
Tip: The restrictions mentioned in the previous paragraph can easily be
worked around by defining custom functions to use in the index
definition that call the desired function(s) internally.
----------------
Steve Brett wrote:
> i think you need to do this
>
> CREATE INDEX eventdateindex ON event (eventstamp);
>
> as i'm 99% certain that you can only create an index on a 'whole' attribute
> and not the result of a function.
>
> Steve
>
> "Neal Lindsay" <chox65@yahoo.com> wrote in message
> news:b01eaea0.0201030725.51661db3@posting.google.com...
>
>>I have a table called "event":
>> Attribute | Type |
>>Modifier
>>-------------+--------------------------+---------------------------------
>>
> ----------------------
>
>> eventid | integer | not null default
>>nextval('"event_eventid_seq"'::text)
>> femployeeid | integer | not null
>> ftaskid | integer | not null
>> flocaleid | integer | not null
>> eventstamp | timestamp with time zone | not null
>> duration | smallint | not null
>>Index: event_pkey
>>
>>When I try to create an index:
>>CREATE INDEX eventdateindex ON event (date_trunc('day', eventstamp));
>>
>>It gives me this error:
>>ERROR: parser: parse error at or near "'"
>>
>>I can "SELECT date_trunc('day', eventstamp) FROM event;", but not
>>create an index on that function. What am I doing wrong?
>>
>>Thanks
>>-Neal Lindsay
>>
>
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Error trying to create a functional index.
@ 2002-01-08 09:33 Steve Brett <steve.brett@e-mis.com>
parent: CoL <col@mportal.hu>
1 sibling, 0 replies; 5+ messages in thread
From: Steve Brett @ 2002-01-08 09:33 UTC (permalink / raw)
To: pgsql-sql
i stand totally corrected.
:-)
Steve
"CoL" <col@mportal.hu> wrote in message news:3C397FF1.20706@mportal.hu...
> Steve: of course you can make functional indices:
>
> So why this not workin???? (not workin for me 2 too, pg 7.1.3)
> "CREATE INDEX eventdateindex ON event (date_trunc('day', eventstamp));"
>
> ----------------
> 7.5. Functional Indices
> For a functional index, an index is defined on the result of a function
> applied to one or more columns of a single table. Functional indices can
> be used to obtain fast access to data based on the result of function
calls.
> For example, a common way to do case-insensitive comparisons is to use
> the lower:
> SELECT * FROM test1 WHERE lower(col1) = 'value';
> In order for that query to be able to use an index, it has to be defined
> on the result of the lower(column) operation:
> CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));
> The function in the index definition can take more than one argument,
> but they must be table columns, not constants. Functional indices are
> always single-column (namely, the function result) even if the function
> uses more than one input field; there cannot be multi-column indices
> that contain function calls.
> Tip: The restrictions mentioned in the previous paragraph can easily be
> worked around by defining custom functions to use in the index
> definition that call the desired function(s) internally.
> ----------------
>
> Steve Brett wrote:
>
> > i think you need to do this
> >
> > CREATE INDEX eventdateindex ON event (eventstamp);
> >
> > as i'm 99% certain that you can only create an index on a 'whole'
attribute
> > and not the result of a function.
> >
> > Steve
> >
> > "Neal Lindsay" <chox65@yahoo.com> wrote in message
> > news:b01eaea0.0201030725.51661db3@posting.google.com...
> >
> >>I have a table called "event":
> >> Attribute | Type |
> >>Modifier
>
>>-------------+--------------------------+---------------------------------
> >>
> > ----------------------
> >
> >> eventid | integer | not null default
> >>nextval('"event_eventid_seq"'::text)
> >> femployeeid | integer | not null
> >> ftaskid | integer | not null
> >> flocaleid | integer | not null
> >> eventstamp | timestamp with time zone | not null
> >> duration | smallint | not null
> >>Index: event_pkey
> >>
> >>When I try to create an index:
> >>CREATE INDEX eventdateindex ON event (date_trunc('day', eventstamp));
> >>
> >>It gives me this error:
> >>ERROR: parser: parse error at or near "'"
> >>
> >>I can "SELECT date_trunc('day', eventstamp) FROM event;", but not
> >>create an index on that function. What am I doing wrong?
> >>
> >>Thanks
> >>-Neal Lindsay
> >>
> >
> >
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Error trying to create a functional index.
@ 2002-01-08 11:27 Jochem van Dieten <nomail@dev.null>
parent: CoL <col@mportal.hu>
1 sibling, 1 reply; 5+ messages in thread
From: Jochem van Dieten @ 2002-01-08 11:27 UTC (permalink / raw)
To: pgsql-sql
CoL wrote:
> Steve: of course you can make functional indices:
>
> So why this not workin???? (not workin for me 2 too, pg 7.1.3)
> "CREATE INDEX eventdateindex ON event (date_trunc('day', eventstamp));"
>
> ----------------
> 7.5. Functional Indices
> For a functional index, an index is defined on the result of a function
> applied to one or more columns of a single table. Functional indices can
> be used to obtain fast access to data based on the result of function
> calls.
> For example, a common way to do case-insensitive comparisons is to use
> the lower:
> SELECT * FROM test1 WHERE lower(col1) = 'value';
> In order for that query to be able to use an index, it has to be defined
> on the result of the lower(column) operation:
> CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));
> The function in the index definition can take more than one argument,
> but they must be table columns, not constants.
Wouldn't your problem be that 'day' is a constant?
Jochem
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Error trying to create a functional index.
@ 2002-01-08 11:57 CoL <col@mportal.hu>
parent: Jochem van Dieten <nomail@dev.null>
0 siblings, 1 reply; 5+ messages in thread
From: CoL @ 2002-01-08 11:57 UTC (permalink / raw)
To: pgsql-sql
Hi,
Jochem van Dieten wrote:
> CoL wrote:
>
>> Steve: of course you can make functional indices:
>>
>> So why this not workin???? (not workin for me 2 too, pg 7.1.3)
>> "CREATE INDEX eventdateindex ON event (date_trunc('day', eventstamp));"
>>
>
> Wouldn't your problem be that 'day' is a constant?
> Jochem
sure it is, but date_trunc takes this, so what is the way to create a date_trunc part or any
date function index on a timestamp?
Or it's not possible?
CoL
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Error trying to create a functional index.
@ 2002-01-08 12:10 Jochem van Dieten <nomail@dev.null>
parent: CoL <col@mportal.hu>
0 siblings, 0 replies; 5+ messages in thread
From: Jochem van Dieten @ 2002-01-08 12:10 UTC (permalink / raw)
To: pgsql-sql
CoL wrote:
>
> sure it is, but date_trunc takes this, so what is the way to create a
> date_trunc part or any date function index on a timestamp?
>
> Or it's not possible?
I would tend to believe it is not directly possible. But maybe with a
custom function:
(pseudocode, check whether Day is a reserved word)
CREATE FUNCTION Day(date) RETURNS varchar AS '
BEGIN
RETURN Cast(date_trunc('day', $1) AS varchar);
END;
' LANGUAGE 'SQL';
CREATE INDEX eventdateindex ON event (Day(eventstamp));
SELECT Day(eventstamp) FROM event;
Jochem
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2002-01-08 12:10 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-01-07 11:01 Re: Error trying to create a functional index. CoL <col@mportal.hu>
2002-01-08 09:33 ` Steve Brett <steve.brett@e-mis.com>
2002-01-08 11:27 ` Jochem van Dieten <nomail@dev.null>
2002-01-08 11:57 ` CoL <col@mportal.hu>
2002-01-08 12:10 ` Jochem van Dieten <nomail@dev.null>
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