473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Index on array element

How can I create an index on an array element? I seem to recall having
done this in the past but I don't recall how.

steve=# \d foo
Table "public.foo"
Column | Type | Modifiers
-----------+--------+-----------
textarray | text[] |

steve=# create index foodex on foo (textarray[3]);
ERROR: parser: parse error at or near "[" at character 38

steve=# select version();
PostgreSQL 7.3.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3
20030226 (prerelease) (SuSE Linux)

Cheers,
Steve
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #1
5 6056
Steve Crawford <sc*******@pinpointresearch.com> writes:
How can I create an index on an array element?


You need 7.4 and an extra layer of parentheses:

create index foodex on foo ((textarray[3]));

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2
Steve Crawford <sc*******@pinpointresearch.com> writes:
How can I create an index on an array element? I seem to recall having
done this in the past but I don't recall how.

steve=# \d foo
Table "public.foo"
Column | Type | Modifiers
-----------+--------+-----------
textarray | text[] |

steve=# create index foodex on foo (textarray[3]);
ERROR: parser: parse error at or near "[" at character 38

steve=# select version();
PostgreSQL 7.3.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3
20030226 (prerelease) (SuSE Linux)


In 7.3 you'll have to create a function to make this index.

create function first(text[]) returns text language sql as 'select $1 [1]' strict immutable;

then you can create an index like

create index foodex on foo (first(textarray));

In 7.4 you can do arbitrary expressions, but in 7.3 you can only do simple
function calls of a single column.

--
greg
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #3
>> How can I create an index on an array element?

You need 7.4 and an extra layer of parentheses:

create index foodex on foo ((textarray[3]));


Sorry, but this isn't obvious to me as arrays in a database are a new
concept for me -- why are the extra layer of parenthesis needed?

--
Randolf Richardson - rr@8x.ca
Vancouver, British Columbia, Canada

Please do not eMail me directly when responding
to my postings in the newsgroups.
Nov 12 '05 #4
Randolf Richardson <rr@8x.ca> writes:
How can I create an index on an array element?
You need 7.4 and an extra layer of parentheses:

create index foodex on foo ((textarray[3]));

Sorry, but this isn't obvious to me as arrays in a database are a new
concept for me -- why are the extra layer of parenthesis needed?


It's got nothing to do with arrays as such, but is purely a syntactic
restriction: if the indexed item is anything but an unadorned column
name or a function call, CREATE INDEX wants parentheses around it.

This is because the CREATE INDEX syntax also allows for an "operator
class" name in there. Consider

create index foodex on foo (bar ! ops);

Is this supposed to mean indexing the expression "bar ! ops" (infix !
operator) or indexing the expression "bar !" (postfix ! operator) using
the operator class "ops"? No way to tell, so we make you clarify your
intent with parentheses. You can say either

create index foodex on foo ((bar ! ops));
create index foodex on foo ((bar !) ops);

depending which interpretation you want.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #5
[sNip]
It's got nothing to do with arrays as such, but is purely a syntactic
restriction: if the indexed item is anything but an unadorned column
name or a function call, CREATE INDEX wants parentheses around it.

This is because the CREATE INDEX syntax also allows for an "operator
class" name in there. Consider

create index foodex on foo (bar ! ops);

Is this supposed to mean indexing the expression "bar ! ops" (infix !
operator) or indexing the expression "bar !" (postfix ! operator) using
the operator class "ops"? No way to tell, so we make you clarify your
intent with parentheses. You can say either

create index foodex on foo ((bar ! ops));
create index foodex on foo ((bar !) ops);

depending which interpretation you want.


Thanks. Your explanation is very helpful.

--
Randolf Richardson - rr@8x.ca
Vancouver, British Columbia, Canada

Please do not eMail me directly when responding
to my postings in the newsgroups.
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
2
by: deko | last post by:
Can I return the index number of an array if all I have is the element? For example, if I want to index the alphabet, I can put the letters in the array: Dim varLtr As Variant varLtr =...
4
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
30
by: Eric Lilja | last post by:
Hello, I have a function that has a static array with two elements in it. When the function is called the first time, it should access element 0, the second time it should access element 1, the...
14
by: oLgAa25 | last post by:
Hello all, I am back I have this question everything compiles fine, but I just need to check for an index that is more than the array size. Although it says out of bound, but it is still...
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
8
by: Joe Rattz | last post by:
Ok, I can't believe what I am seeing. I am sure I do this other places with no problems, but I sure can't here. I have some code that is indexing into the ItemArray in a DataSet's DataRow. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.