PostgreSQL: Features new in 7.4: SQL99 Array handling

  • Arrays pre 7.4
template1=# create table foo(bar int[2]);
CREATE TABLE
template1=# insert into foo values('{1,2}');
INSERT 17147 1
template1=# select * from foo where bar[1] = '1' OR bar[2] = '1';
  bar
-------
 {1,2}
(1 row)
  • 7.4: Array as sub query
template1=# select * from foo where 1 = any(bar);
  bar
-------
 {1,2}
(1 row)
  • Also: ALL(), SOME()
  • Better array typing: ARRAY[]
template1=# select ARRAY[1,2] as foo;
  foo
-------
 {1,2}
(1 row)

template1=# select (ARRAY[1,2] || ARRAY[3,4]) as foo;
    foo
-----------
 {1,2,3,4}
(1 row)
  • Polymorphic data types:
    • anyarray - any array data type
    • anyelement - any data type

  • Operator support:
    • < - less than - ARRAY[1,2] < ARRAY[3,4] = true
    • > - greater than - ARRAY[3,4,5] > ARRAY[4,3,1] = false (evaluate comparison on each element left to right)
    • = - equals - ARRAY[1,2,3] = ARRAY[1,2,3] = true
    • <> - not equal - ARRAY[1,2] <> ARRAY[3] = true
    • || - concatenation - ARRAY[1,2] || 3 = '{1,2,3}'; ARRAY[1] || ARRAY[2] = '{1,2}; arrays must be of the same dimension.

  • New array functions:
    • array_append(anyarray,anyelement) - append second arg to first
    • array_prepend(anyarray,anyelement) - prepend second arg to first
    • array_cat(anyarray,anyarray) - concatenate args into one array
    • array_lower(anyarray,integer) - return lower bound of given dimension
    • array_upper(anyarray,integer) - return upper bound of given dimension
    • array_dims(anyarray) - return textual representation of dimensions
    • array_to_string(anyarray, text) - flatten first arg with seperator in second
    • string_to_array(text,text) - convert string given as first arg delimited by second arg to array

  • Index support on array columns (not supported on a single dimension of an array)
Prev

Next

Page 10