| PostgreSQL Reference Manual - Volume 1 - SQL Language Reference by The PostgreSQL Global Development Group Paperback (6"x9"), 716 pages ISBN 0954612027 RRP £32.00 ($49.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
6.10.6 Array Input and Output Syntax
The external text representation of an array value consists of items that
are interpreted according to the I/O conversion rules for the array's
element type, plus decoration that indicates the array structure.
The decoration consists of curly braces ({ and })
around the array value plus delimiter characters between adjacent items.
The delimiter character is usually a comma (,) but can be
something else: it is determined by the typdelim setting
for the array's element type. (Among the standard data types provided
in the PostgreSQL distribution, type
box uses a semicolon (;) but all the others
use comma.) In a multidimensional array, each dimension (row, plane,
cube, etc.) gets its own level of curly braces, and delimiters
must be written between adjacent curly-braced entities of the same level.
The array output routine will put double quotes around element values
if they are empty strings, contain curly braces, delimiter characters,
double quotes, backslashes, or white space, or match the word
NULL. Double quotes and backslashes
embedded in element values will be backslash-escaped. For numeric
data types it is safe to assume that double quotes will never appear, but
for textual data types one should be prepared to cope with either presence
or absence of quotes.
By default, the lower bound index value of an array's dimensions is
set to one. To represent arrays with other lower bounds, the array
subscript ranges can be specified explicitly before writing the
array contents.
This decoration consists of square brackets ([])
around each array dimension's lower and upper bounds, with
a colon (:) delimiter character in between. The
array dimension decoration is followed by an equal sign (=).
For example:
SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[]
AS f1) AS ss;
e1 | e2
----+----
1 | 6
(1 row)
The array output routine will include explicit dimensions in its result only when there are one or more lower bounds different from one.
If the value written for an element is NULL (in any case
variant), the element is taken to be NULL. The presence of any quotes
or backslashes disables this and allows the literal string value
“NULL” to be entered. Also, for backwards compatibility with
pre-8.2 versions of PostgreSQL, the array_nulls configuration parameter may be turned
off to suppress recognition of NULL as a NULL.
As shown previously, when writing an array value you can write double
quotes around any individual array element. You must do so
if the element value would otherwise confuse the array-value parser.
For example, elements containing curly braces, commas (or whatever the
delimiter character is), double quotes, backslashes, or leading or trailing
whitespace must be double-quoted. Empty strings and strings matching the
word NULL must be quoted, too. To put a double quote or
backslash in a quoted array element value, use escape string syntax
and precede it with a backslash. Alternatively, you can use
backslash-escaping to protect all data characters that would otherwise
be taken as array syntax.
You may write whitespace before a left brace or after a right brace. You may also write whitespace before or after any individual item string. In all of these cases the whitespace will be ignored. However, whitespace within double-quoted elements, or surrounded on both sides by non-whitespace characters of an element, is not ignored.
Note: Remember that what you write in an SQL command will first be interpreted as a string literal, and then as an array. This doubles the number of backslashes you need. For example, to insert a
textarray value containing a backslash and a double quote, you'd need to writeINSERT ... VALUES (E'{"\\\\","\\""}');The escape string processor removes one level of backslashes, so that what arrives at the array-value parser looks like
{"\\","\""}. In turn, the strings fed to thetextdata type's input routine become\and"respectively. (If we were working with a data type whose input routine also treated backslashes specially,byteafor example, we might need as many as eight backslashes in the command to get one backslash into the stored array element.) Dollar quoting (see section 2.1.2.2 Dollar-Quoted String Constants) can be used to avoid the need to double backslashes.
Tip: The
ARRAYconstructor syntax (see section 2.2.10 Array Constructors) is often easier to work with than the array-literal syntax when writing array values in SQL commands. InARRAY, individual element values are written the same way they would be written when not members of an array.
| ISBN 0954612027 | PostgreSQL Reference Manual - Volume 1 - SQL Language Reference | See the print edition |