dbTalk Databases Forums  

Indexes on arrays

comp.databases.postgresql comp.databases.postgresql


Discuss Indexes on arrays in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mladen Gogala
 
Posts: n/a

Default Indexes on arrays - 11-25-2009 , 01:59 PM






I have a table defined like this:

scott=> \d t
Table "public.t"
Column | Type | Modifiers
--------+----------------------+-----------
col1 | character varying(3) |
col2 | integer[] |
Indexes:
"ind_t_proj" btree (proj_elem_1(col2))

I would like to create an index on col2[1] but it's syntactically
supported. I found a way to circumvent the limitation by doing the
following:

scott=> create function proj_elem_1(integer[]) returns integer
scott-> as $$
scott$> begin
scott$> return($1[1]);
scott$> end;
scott$> $$ language plpgsql immutable;
CREATE FUNCTION
scott=> create index ind_t_proj on t(proj_elem_1(col2));
CREATE INDEX

Is there anything else, more elegant than creating a specialized function?


--
http://mgogala.byethost5.com

Reply With Quote
  #2  
Old   
Jasen Betts
 
Posts: n/a

Default Re: Indexes on arrays - 11-26-2009 , 03:07 AM






On 2009-11-25, Mladen Gogala <no (AT) email (DOT) here.invalid> wrote:
Quote:
I have a table defined like this:

scott=> \d t
Table "public.t"
Column | Type | Modifiers
--------+----------------------+-----------
col1 | character varying(3) |
col2 | integer[] |

try this:

create index ind_t_proj on t(( col2[2] ));

Reply With Quote
  #3  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Indexes on arrays - 11-26-2009 , 04:08 AM



Mladen Gogala wrote:
Quote:
I have a table defined like this:

scott=> \d t
Table "public.t"
Column | Type | Modifiers
--------+----------------------+-----------
col1 | character varying(3) |
col2 | integer[] |
Indexes:
"ind_t_proj" btree (proj_elem_1(col2))

I would like to create an index on col2[1] but it's syntactically
supported. I found a way to circumvent the limitation by doing the
following:

[...]

Is there anything else, more elegant than creating a specialized function?
It *is* syntactically supported. That was a Freudian lapse.

CREATE INDEX ind_t_proj ON t ((col2[1]));

Look at the syntax diagram for CREATE INDEX, there are extra
parentheses around the "expression".

The documentation says:
The key field(s) for the index are specified as column names,
or alternatively as expressions written in parentheses.

I realize that this can easily be misinterpreted, but note
the lack of comma after "expressions".

Yours,
Laurenz Albe

Reply With Quote
  #4  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Indexes on arrays - 11-26-2009 , 09:00 PM



On Thu, 26 Nov 2009 11:08:16 +0100, Laurenz Albe wrote:

Quote:
Mladen Gogala wrote:
I have a table defined like this:

scott=> \d t
Table "public.t"
Column | Type | Modifiers
--------+----------------------+----------- col1 | character
varying(3) |
col2 | integer[] |
Indexes:
"ind_t_proj" btree (proj_elem_1(col2))

I would like to create an index on col2[1] but it's syntactically
supported. I found a way to circumvent the limitation by doing the
following:

[...]

Is there anything else, more elegant than creating a specialized
function?

It *is* syntactically supported. That was a Freudian lapse.

CREATE INDEX ind_t_proj ON t ((col2[1]));

Look at the syntax diagram for CREATE INDEX, there are extra parentheses
around the "expression".

The documentation says:
The key field(s) for the index are specified as column names, or
alternatively as expressions written in parentheses.

I realize that this can easily be misinterpreted, but note the lack of
comma after "expressions".

Yours,
Laurenz Albe
Laurenz, I owe you a beer, if you ever come over to NYC:

scott=> CREATE INDEX ind_t_proj ON t ((col2[1]));
CREATE INDEX
scott=> \d t
Table "scott.t"
Column | Type | Modifiers
--------+-----------------------+-----------
col1 | character varying(10) |
col2 | integer[] |
Indexes:
"ind_t_proj" btree ((col2[1]))


Thanks again! I am getting more thrilled by PostgreSQL, the more I study
it.


--
http://mgogala.byethost5.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.