]> begriffs open source - ai-pg/blob - full-docs/html/functions-array.html
Include latest toc output
[ai-pg] / full-docs / html / functions-array.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.19. Array Functions and Operators</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="functions-conditional.html" title="9.18. Conditional Expressions" /><link rel="next" href="functions-range.html" title="9.20. Range/Multirange Functions and Operators" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.19. Array Functions and Operators</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-conditional.html" title="9.18. Conditional Expressions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-range.html" title="9.20. Range/Multirange Functions and Operators">Next</a></td></tr></table><hr /></div><div class="sect1" id="FUNCTIONS-ARRAY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.19. Array Functions and Operators <a href="#FUNCTIONS-ARRAY" class="id_link">#</a></h2></div></div></div><p>
3    <a class="xref" href="functions-array.html#ARRAY-OPERATORS-TABLE" title="Table 9.56. Array Operators">Table 9.56</a> shows the specialized operators
4    available for array types.
5    In addition to those, the usual comparison operators shown in <a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE" title="Table 9.1. Comparison Operators">Table 9.1</a> are available for
6    arrays.  The comparison operators compare the array contents
7    element-by-element, using the default B-tree comparison function for
8    the element data type, and sort based on the first difference.
9    In multidimensional arrays the elements are visited in row-major order
10    (last subscript varies most rapidly).
11    If the contents of two arrays are equal but the dimensionality is
12    different, the first difference in the dimensionality information
13    determines the sort order.
14   </p><div class="table" id="ARRAY-OPERATORS-TABLE"><p class="title"><strong>Table 9.56. Array Operators</strong></p><div class="table-contents"><table class="table" summary="Array Operators" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
15         Operator
16        </p>
17        <p>
18         Description
19        </p>
20        <p>
21         Example(s)
22        </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
23         <code class="type">anyarray</code> <code class="literal">@&gt;</code> <code class="type">anyarray</code>
24         → <code class="returnvalue">boolean</code>
25        </p>
26        <p>
27         Does the first array contain the second, that is, does each element
28         appearing in the second array equal some element of the first array?
29         (Duplicates are not treated specially,
30         thus <code class="literal">ARRAY[1]</code> and <code class="literal">ARRAY[1,1]</code> are
31         each considered to contain the other.)
32        </p>
33        <p>
34         <code class="literal">ARRAY[1,4,3] @&gt; ARRAY[3,1,3]</code>
35         → <code class="returnvalue">t</code>
36        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
37         <code class="type">anyarray</code> <code class="literal">&lt;@</code> <code class="type">anyarray</code>
38         → <code class="returnvalue">boolean</code>
39        </p>
40        <p>
41         Is the first array contained by the second?
42        </p>
43        <p>
44         <code class="literal">ARRAY[2,2,7] &lt;@ ARRAY[1,7,4,2,6]</code>
45         → <code class="returnvalue">t</code>
46        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
47         <code class="type">anyarray</code> <code class="literal">&amp;&amp;</code> <code class="type">anyarray</code>
48         → <code class="returnvalue">boolean</code>
49        </p>
50        <p>
51         Do the arrays overlap, that is, have any elements in common?
52        </p>
53        <p>
54         <code class="literal">ARRAY[1,4,3] &amp;&amp; ARRAY[2,1]</code>
55         → <code class="returnvalue">t</code>
56        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
57         <code class="type">anycompatiblearray</code> <code class="literal">||</code> <code class="type">anycompatiblearray</code>
58         → <code class="returnvalue">anycompatiblearray</code>
59        </p>
60        <p>
61         Concatenates the two arrays.  Concatenating a null or empty array is a
62         no-op; otherwise the arrays must have the same number of dimensions
63         (as illustrated by the first example) or differ in number of
64         dimensions by one (as illustrated by the second).
65         If the arrays are not of identical element types, they will be coerced
66         to a common type (see <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>).
67        </p>
68        <p>
69         <code class="literal">ARRAY[1,2,3] || ARRAY[4,5,6,7]</code>
70         → <code class="returnvalue">{1,2,3,4,5,6,7}</code>
71        </p>
72        <p>
73         <code class="literal">ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9.9]]</code>
74         → <code class="returnvalue">{{1,2,3},{4,5,6},{7,8,9.9}}</code>
75        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
76         <code class="type">anycompatible</code> <code class="literal">||</code> <code class="type">anycompatiblearray</code>
77         → <code class="returnvalue">anycompatiblearray</code>
78        </p>
79        <p>
80         Concatenates an element onto the front of an array (which must be
81         empty or one-dimensional).
82        </p>
83        <p>
84         <code class="literal">3 || ARRAY[4,5,6]</code>
85         → <code class="returnvalue">{3,4,5,6}</code>
86        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
87         <code class="type">anycompatiblearray</code> <code class="literal">||</code> <code class="type">anycompatible</code>
88         → <code class="returnvalue">anycompatiblearray</code>
89        </p>
90        <p>
91         Concatenates an element onto the end of an array (which must be
92         empty or one-dimensional).
93        </p>
94        <p>
95         <code class="literal">ARRAY[4,5,6] || 7</code>
96         → <code class="returnvalue">{4,5,6,7}</code>
97        </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
98    See <a class="xref" href="arrays.html" title="8.15. Arrays">Section 8.15</a> for more details about array operator
99    behavior.  See <a class="xref" href="indexes-types.html" title="11.2. Index Types">Section 11.2</a> for more details about
100    which operators support indexed operations.
101   </p><p>
102    <a class="xref" href="functions-array.html#ARRAY-FUNCTIONS-TABLE" title="Table 9.57. Array Functions">Table 9.57</a> shows the functions
103    available for use with array types. See <a class="xref" href="arrays.html" title="8.15. Arrays">Section 8.15</a>
104    for more information  and examples of the use of these functions.
105   </p><div class="table" id="ARRAY-FUNCTIONS-TABLE"><p class="title"><strong>Table 9.57. Array Functions</strong></p><div class="table-contents"><table class="table" summary="Array Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
106         Function
107        </p>
108        <p>
109         Description
110        </p>
111        <p>
112         Example(s)
113        </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
114         <a id="id-1.5.8.25.6.2.2.1.1.1.1" class="indexterm"></a>
115         <code class="function">array_append</code> ( <code class="type">anycompatiblearray</code>, <code class="type">anycompatible</code> )
116         → <code class="returnvalue">anycompatiblearray</code>
117        </p>
118        <p>
119         Appends an element to the end of an array (same as
120         the <code class="type">anycompatiblearray</code> <code class="literal">||</code> <code class="type">anycompatible</code>
121         operator).
122        </p>
123        <p>
124         <code class="literal">array_append(ARRAY[1,2], 3)</code>
125         → <code class="returnvalue">{1,2,3}</code>
126        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
127         <a id="id-1.5.8.25.6.2.2.2.1.1.1" class="indexterm"></a>
128         <code class="function">array_cat</code> ( <code class="type">anycompatiblearray</code>, <code class="type">anycompatiblearray</code> )
129         → <code class="returnvalue">anycompatiblearray</code>
130        </p>
131        <p>
132         Concatenates two arrays (same as
133         the <code class="type">anycompatiblearray</code> <code class="literal">||</code> <code class="type">anycompatiblearray</code>
134         operator).
135        </p>
136        <p>
137         <code class="literal">array_cat(ARRAY[1,2,3], ARRAY[4,5])</code>
138         → <code class="returnvalue">{1,2,3,4,5}</code>
139        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
140         <a id="id-1.5.8.25.6.2.2.3.1.1.1" class="indexterm"></a>
141         <code class="function">array_dims</code> ( <code class="type">anyarray</code> )
142         → <code class="returnvalue">text</code>
143        </p>
144        <p>
145         Returns a text representation of the array's dimensions.
146        </p>
147        <p>
148         <code class="literal">array_dims(ARRAY[[1,2,3], [4,5,6]])</code>
149         → <code class="returnvalue">[1:2][1:3]</code>
150        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
151         <a id="id-1.5.8.25.6.2.2.4.1.1.1" class="indexterm"></a>
152         <code class="function">array_fill</code> ( <code class="type">anyelement</code>, <code class="type">integer[]</code>
153           [<span class="optional">, <code class="type">integer[]</code> </span>] )
154         → <code class="returnvalue">anyarray</code>
155        </p>
156        <p>
157         Returns an array filled with copies of the given value, having
158         dimensions of the lengths specified by the second argument.
159         The optional third argument supplies lower-bound values for each
160         dimension (which default to all <code class="literal">1</code>).
161        </p>
162        <p>
163         <code class="literal">array_fill(11, ARRAY[2,3])</code>
164         → <code class="returnvalue">{{11,11,11},{11,11,11}}</code>
165        </p>
166        <p>
167         <code class="literal">array_fill(7, ARRAY[3], ARRAY[2])</code>
168         → <code class="returnvalue">[2:4]={7,7,7}</code>
169        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
170         <a id="id-1.5.8.25.6.2.2.5.1.1.1" class="indexterm"></a>
171         <code class="function">array_length</code> ( <code class="type">anyarray</code>, <code class="type">integer</code> )
172         → <code class="returnvalue">integer</code>
173        </p>
174        <p>
175         Returns the length of the requested array dimension.
176         (Produces NULL instead of 0 for empty or missing array dimensions.)
177        </p>
178        <p>
179         <code class="literal">array_length(array[1,2,3], 1)</code>
180         → <code class="returnvalue">3</code>
181        </p>
182        <p>
183         <code class="literal">array_length(array[]::int[], 1)</code>
184         → <code class="returnvalue">NULL</code>
185        </p>
186        <p>
187         <code class="literal">array_length(array['text'], 2)</code>
188         → <code class="returnvalue">NULL</code>
189        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
190         <a id="id-1.5.8.25.6.2.2.6.1.1.1" class="indexterm"></a>
191         <code class="function">array_lower</code> ( <code class="type">anyarray</code>, <code class="type">integer</code> )
192         → <code class="returnvalue">integer</code>
193        </p>
194        <p>
195         Returns the lower bound of the requested array dimension.
196        </p>
197        <p>
198         <code class="literal">array_lower('[0:2]={1,2,3}'::integer[], 1)</code>
199         → <code class="returnvalue">0</code>
200        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
201         <a id="id-1.5.8.25.6.2.2.7.1.1.1" class="indexterm"></a>
202         <code class="function">array_ndims</code> ( <code class="type">anyarray</code> )
203         → <code class="returnvalue">integer</code>
204        </p>
205        <p>
206         Returns the number of dimensions of the array.
207        </p>
208        <p>
209         <code class="literal">array_ndims(ARRAY[[1,2,3], [4,5,6]])</code>
210         → <code class="returnvalue">2</code>
211        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
212         <a id="id-1.5.8.25.6.2.2.8.1.1.1" class="indexterm"></a>
213         <code class="function">array_position</code> ( <code class="type">anycompatiblearray</code>, <code class="type">anycompatible</code> [<span class="optional">, <code class="type">integer</code> </span>] )
214         → <code class="returnvalue">integer</code>
215        </p>
216        <p>
217         Returns the subscript of the first occurrence of the second argument
218         in the array, or <code class="literal">NULL</code> if it's not present.
219         If the third argument is given, the search begins at that subscript.
220         The array must be one-dimensional.
221         Comparisons are done using <code class="literal">IS NOT DISTINCT FROM</code>
222         semantics, so it is possible to search for <code class="literal">NULL</code>.
223        </p>
224        <p>
225         <code class="literal">array_position(ARRAY['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'], 'mon')</code>
226         → <code class="returnvalue">2</code>
227        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
228         <a id="id-1.5.8.25.6.2.2.9.1.1.1" class="indexterm"></a>
229         <code class="function">array_positions</code> ( <code class="type">anycompatiblearray</code>, <code class="type">anycompatible</code> )
230         → <code class="returnvalue">integer[]</code>
231        </p>
232        <p>
233         Returns an array of the subscripts of all occurrences of the second
234         argument in the array given as first argument.
235         The array must be one-dimensional.
236         Comparisons are done using <code class="literal">IS NOT DISTINCT FROM</code>
237         semantics, so it is possible to search for <code class="literal">NULL</code>.
238         <code class="literal">NULL</code> is returned only if the array
239         is <code class="literal">NULL</code>; if the value is not found in the array, an
240         empty array is returned.
241        </p>
242        <p>
243         <code class="literal">array_positions(ARRAY['A','A','B','A'], 'A')</code>
244         → <code class="returnvalue">{1,2,4}</code>
245        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
246         <a id="id-1.5.8.25.6.2.2.10.1.1.1" class="indexterm"></a>
247         <code class="function">array_prepend</code> ( <code class="type">anycompatible</code>, <code class="type">anycompatiblearray</code> )
248         → <code class="returnvalue">anycompatiblearray</code>
249        </p>
250        <p>
251         Prepends an element to the beginning of an array (same as
252         the <code class="type">anycompatible</code> <code class="literal">||</code> <code class="type">anycompatiblearray</code>
253         operator).
254        </p>
255        <p>
256         <code class="literal">array_prepend(1, ARRAY[2,3])</code>
257         → <code class="returnvalue">{1,2,3}</code>
258        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
259         <a id="id-1.5.8.25.6.2.2.11.1.1.1" class="indexterm"></a>
260         <code class="function">array_remove</code> ( <code class="type">anycompatiblearray</code>, <code class="type">anycompatible</code> )
261         → <code class="returnvalue">anycompatiblearray</code>
262        </p>
263        <p>
264         Removes all elements equal to the given value from the array.
265         The array must be one-dimensional.
266         Comparisons are done using <code class="literal">IS NOT DISTINCT FROM</code>
267         semantics, so it is possible to remove <code class="literal">NULL</code>s.
268        </p>
269        <p>
270         <code class="literal">array_remove(ARRAY[1,2,3,2], 2)</code>
271         → <code class="returnvalue">{1,3}</code>
272        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
273         <a id="id-1.5.8.25.6.2.2.12.1.1.1" class="indexterm"></a>
274         <code class="function">array_replace</code> ( <code class="type">anycompatiblearray</code>, <code class="type">anycompatible</code>, <code class="type">anycompatible</code> )
275         → <code class="returnvalue">anycompatiblearray</code>
276        </p>
277        <p>
278         Replaces each array element equal to the second argument with the
279         third argument.
280        </p>
281        <p>
282         <code class="literal">array_replace(ARRAY[1,2,5,4], 5, 3)</code>
283         → <code class="returnvalue">{1,2,3,4}</code>
284        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
285         <a id="id-1.5.8.25.6.2.2.13.1.1.1" class="indexterm"></a>
286         <code class="function">array_reverse</code> ( <code class="type">anyarray</code> )
287         → <code class="returnvalue">anyarray</code>
288        </p>
289        <p>
290         Reverses the first dimension of the array.
291        </p>
292        <p>
293         <code class="literal">array_reverse(ARRAY[[1,2],[3,4],[5,6]])</code>
294         → <code class="returnvalue">{{5,6},{3,4},{1,2}}</code>
295        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
296         <a id="id-1.5.8.25.6.2.2.14.1.1.1" class="indexterm"></a>
297         <code class="function">array_sample</code> ( <em class="parameter"><code>array</code></em> <code class="type">anyarray</code>, <em class="parameter"><code>n</code></em> <code class="type">integer</code> )
298         → <code class="returnvalue">anyarray</code>
299        </p>
300        <p>
301         Returns an array of <em class="parameter"><code>n</code></em> items randomly selected
302         from <em class="parameter"><code>array</code></em>.  <em class="parameter"><code>n</code></em> may not
303         exceed the length of <em class="parameter"><code>array</code></em>'s first dimension.
304         If <em class="parameter"><code>array</code></em> is multi-dimensional,
305         an <span class="quote">“<span class="quote">item</span>”</span> is a slice having a given first subscript.
306        </p>
307        <p>
308         <code class="literal">array_sample(ARRAY[1,2,3,4,5,6], 3)</code>
309         → <code class="returnvalue">{2,6,1}</code>
310        </p>
311        <p>
312         <code class="literal">array_sample(ARRAY[[1,2],[3,4],[5,6]], 2)</code>
313         → <code class="returnvalue">{{5,6},{1,2}}</code>
314        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
315         <a id="id-1.5.8.25.6.2.2.15.1.1.1" class="indexterm"></a>
316         <code class="function">array_shuffle</code> ( <code class="type">anyarray</code> )
317         → <code class="returnvalue">anyarray</code>
318        </p>
319        <p>
320         Randomly shuffles the first dimension of the array.
321        </p>
322        <p>
323         <code class="literal">array_shuffle(ARRAY[[1,2],[3,4],[5,6]])</code>
324         → <code class="returnvalue">{{5,6},{1,2},{3,4}}</code>
325        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
326         <a id="id-1.5.8.25.6.2.2.16.1.1.1" class="indexterm"></a>
327         <code class="function">array_sort</code> (
328           <em class="parameter"><code>array</code></em> <code class="type">anyarray</code>
329           [<span class="optional">, <em class="parameter"><code>descending</code></em> <code class="type">boolean</code>
330           [<span class="optional">, <em class="parameter"><code>nulls_first</code></em> <code class="type">boolean</code>
331           </span>]</span>] )
332         → <code class="returnvalue">anyarray</code>
333        </p>
334        <p>
335         Sorts the first dimension of the array.
336         The sort order is determined by the default sort ordering of the
337         array's element type; however, if the element type is collatable,
338         the collation to use can be specified by adding
339         a <code class="literal">COLLATE</code> clause to
340         the <em class="parameter"><code>array</code></em> argument.
341        </p>
342        <p>
343         If <em class="parameter"><code>descending</code></em> is true then sort in
344         descending order, otherwise ascending order.  If omitted, the
345         default is ascending order.
346         If <em class="parameter"><code>nulls_first</code></em> is true then nulls appear
347         before non-null values, otherwise nulls appear after non-null
348         values.
349         If omitted, <em class="parameter"><code>nulls_first</code></em> is taken to have
350         the same value as <em class="parameter"><code>descending</code></em>.
351        </p>
352        <p>
353         <code class="literal">array_sort(ARRAY[[2,4],[2,1],[6,5]])</code>
354         → <code class="returnvalue">{{2,1},{2,4},{6,5}}</code>
355        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
356         <a id="FUNCTION-ARRAY-TO-STRING" class="indexterm"></a>
357         <code class="function">array_to_string</code> ( <em class="parameter"><code>array</code></em> <code class="type">anyarray</code>, <em class="parameter"><code>delimiter</code></em> <code class="type">text</code> [<span class="optional">, <em class="parameter"><code>null_string</code></em> <code class="type">text</code> </span>] )
358         → <code class="returnvalue">text</code>
359        </p>
360        <p>
361         Converts each array element to its text representation, and
362         concatenates those separated by
363         the <em class="parameter"><code>delimiter</code></em> string.
364         If <em class="parameter"><code>null_string</code></em> is given and is
365         not <code class="literal">NULL</code>, then <code class="literal">NULL</code> array
366         entries are represented by that string; otherwise, they are omitted.
367         See also <a class="link" href="functions-string.html#FUNCTION-STRING-TO-ARRAY"><code class="function">string_to_array</code></a>.
368        </p>
369        <p>
370         <code class="literal">array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*')</code>
371         → <code class="returnvalue">1,2,3,*,5</code>
372        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
373         <a id="id-1.5.8.25.6.2.2.18.1.1.1" class="indexterm"></a>
374         <code class="function">array_upper</code> ( <code class="type">anyarray</code>, <code class="type">integer</code> )
375         → <code class="returnvalue">integer</code>
376        </p>
377        <p>
378         Returns the upper bound of the requested array dimension.
379        </p>
380        <p>
381         <code class="literal">array_upper(ARRAY[1,8,3,7], 1)</code>
382         → <code class="returnvalue">4</code>
383        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
384         <a id="id-1.5.8.25.6.2.2.19.1.1.1" class="indexterm"></a>
385         <code class="function">cardinality</code> ( <code class="type">anyarray</code> )
386         → <code class="returnvalue">integer</code>
387        </p>
388        <p>
389         Returns the total number of elements in the array, or 0 if the array
390         is empty.
391        </p>
392        <p>
393         <code class="literal">cardinality(ARRAY[[1,2],[3,4]])</code>
394         → <code class="returnvalue">4</code>
395        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
396         <a id="id-1.5.8.25.6.2.2.20.1.1.1" class="indexterm"></a>
397         <code class="function">trim_array</code> ( <em class="parameter"><code>array</code></em> <code class="type">anyarray</code>, <em class="parameter"><code>n</code></em> <code class="type">integer</code> )
398         → <code class="returnvalue">anyarray</code>
399        </p>
400        <p>
401         Trims an array by removing the last <em class="parameter"><code>n</code></em> elements.
402         If the array is multidimensional, only the first dimension is trimmed.
403        </p>
404        <p>
405         <code class="literal">trim_array(ARRAY[1,2,3,4,5,6], 2)</code>
406         → <code class="returnvalue">{1,2,3,4}</code>
407        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
408         <a id="id-1.5.8.25.6.2.2.21.1.1.1" class="indexterm"></a>
409         <code class="function">unnest</code> ( <code class="type">anyarray</code> )
410         → <code class="returnvalue">setof anyelement</code>
411        </p>
412        <p>
413         Expands an array into a set of rows.
414         The array's elements are read out in storage order.
415        </p>
416        <p>
417         <code class="literal">unnest(ARRAY[1,2])</code>
418         → <code class="returnvalue"></code>
419 </p><pre class="programlisting">
420  1
421  2
422 </pre><p>
423        </p>
424        <p>
425         <code class="literal">unnest(ARRAY[['foo','bar'],['baz','quux']])</code>
426         → <code class="returnvalue"></code>
427 </p><pre class="programlisting">
428  foo
429  bar
430  baz
431  quux
432 </pre><p>
433        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
434         <code class="function">unnest</code> ( <code class="type">anyarray</code>, <code class="type">anyarray</code> [<span class="optional">, ... </span>] )
435         → <code class="returnvalue">setof anyelement, anyelement [, ... ]</code>
436        </p>
437        <p>
438         Expands multiple arrays (possibly of different data types) into a set of
439         rows.  If the arrays are not all the same length then the shorter ones
440         are padded with <code class="literal">NULL</code>s.  This form is only allowed
441         in a query's FROM clause; see <a class="xref" href="queries-table-expressions.html#QUERIES-TABLEFUNCTIONS" title="7.2.1.4. Table Functions">Section 7.2.1.4</a>.
442        </p>
443        <p>
444         <code class="literal">select * from unnest(ARRAY[1,2], ARRAY['foo','bar','baz']) as x(a,b)</code>
445         → <code class="returnvalue"></code>
446 </p><pre class="programlisting">
447  a |  b
448 ---+-----
449  1 | foo
450  2 | bar
451    | baz
452 </pre><p>
453        </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
454     See also <a class="xref" href="functions-aggregate.html" title="9.21. Aggregate Functions">Section 9.21</a> about the aggregate
455     function <code class="function">array_agg</code> for use with arrays.
456    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-conditional.html" title="9.18. Conditional Expressions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-range.html" title="9.20. Range/Multirange Functions and Operators">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.18. Conditional Expressions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.20. Range/Multirange Functions and Operators</td></tr></table></div></body></html>