codekingpro/portable-devtools
115k
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 16.3 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.53. Array Operators">Table 9.53</a> shows the specialized operators4 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 for6 arrays. The comparison operators compare the array contents7 element-by-element, using the default B-tree comparison function for8 the element data type, and sort based on the first difference.9 In multidimensional arrays the elements are visited in row-major order10 (last subscript varies most rapidly).11 If the contents of two arrays are equal but the dimensionality is12 different, the first difference in the dimensionality information13 determines the sort order.14 </p><div class="table" id="ARRAY-OPERATORS-TABLE"><p class="title"><strong>Table 9.53. 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 Operator16 </p>17 <p>18 Description19 </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">@></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 element28 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> are31 each considered to contain the other.)32 </p>33 <p>34 <code class="literal">ARRAY[1,4,3] @> 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"><@</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] <@ 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">&&</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] && 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 a62 no-op; otherwise the arrays must have the same number of dimensions63 (as illustrated by the first example) or differ in number of64 dimensions by one (as illustrated by the second).65 If the arrays are not of identical element types, they will be coerced66 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 be81 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 be92 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 operator99 behavior. See <a class="xref" href="indexes-types.html" title="11.2. Index Types">Section 11.2</a> for more details about100 which operators support indexed operations.101 </p><p>102 <a class="xref" href="functions-array.html#ARRAY-FUNCTIONS-TABLE" title="Table 9.54. Array Functions">Table 9.54</a> shows the functions103 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.54. 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 Function107 </p>108 <p>109 Description110 </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 as120 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 as133 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, having158 dimensions of the lengths specified by the second argument.159 The optional third argument supplies lower-bound values for each160 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 argument218 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 second234 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 array239 is <code class="literal">NULL</code>; if the value is not found in the array, an240 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 as252 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 the279 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_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> )287 → <code class="returnvalue">anyarray</code>288 </p>289 <p>290 Returns an array of <em class="parameter"><code>n</code></em> items randomly selected291 from <em class="parameter"><code>array</code></em>. <em class="parameter"><code>n</code></em> may not292 exceed the length of <em class="parameter"><code>array</code></em>'s first dimension.293 If <em class="parameter"><code>array</code></em> is multi-dimensional,294 an <span class="quote">“<span class="quote">item</span>”</span> is a slice having a given first subscript.295 </p>296 <p>297 <code class="literal">array_sample(ARRAY[1,2,3,4,5,6], 3)</code>298 → <code class="returnvalue">{2,6,1}</code>299 </p>300 <p>301 <code class="literal">array_sample(ARRAY[[1,2],[3,4],[5,6]], 2)</code>302 → <code class="returnvalue">{{5,6},{1,2}}</code>303 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">304 <a id="id-1.5.8.25.6.2.2.14.1.1.1" class="indexterm"></a>305 <code class="function">array_shuffle</code> ( <code class="type">anyarray</code> )306 → <code class="returnvalue">anyarray</code>307 </p>308 <p>309 Randomly shuffles the first dimension of the array.310 </p>311 <p>312 <code class="literal">array_shuffle(ARRAY[[1,2],[3,4],[5,6]])</code>313 → <code class="returnvalue">{{5,6},{1,2},{3,4}}</code>314 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">315 <a id="FUNCTION-ARRAY-TO-STRING" class="indexterm"></a>316 <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>] )317 → <code class="returnvalue">text</code>318 </p>319 <p>320 Converts each array element to its text representation, and321 concatenates those separated by322 the <em class="parameter"><code>delimiter</code></em> string.323 If <em class="parameter"><code>null_string</code></em> is given and is324 not <code class="literal">NULL</code>, then <code class="literal">NULL</code> array325 entries are represented by that string; otherwise, they are omitted.326 See also <a class="link" href="functions-string.html#FUNCTION-STRING-TO-ARRAY"><code class="function">string_to_array</code></a>.327 </p>328 <p>329 <code class="literal">array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*')</code>330 → <code class="returnvalue">1,2,3,*,5</code>331 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">332 <a id="id-1.5.8.25.6.2.2.16.1.1.1" class="indexterm"></a>333 <code class="function">array_upper</code> ( <code class="type">anyarray</code>, <code class="type">integer</code> )334 → <code class="returnvalue">integer</code>335 </p>336 <p>337 Returns the upper bound of the requested array dimension.338 </p>339 <p>340 <code class="literal">array_upper(ARRAY[1,8,3,7], 1)</code>341 → <code class="returnvalue">4</code>342 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">343 <a id="id-1.5.8.25.6.2.2.17.1.1.1" class="indexterm"></a>344 <code class="function">cardinality</code> ( <code class="type">anyarray</code> )345 → <code class="returnvalue">integer</code>346 </p>347 <p>348 Returns the total number of elements in the array, or 0 if the array349 is empty.350 </p>351 <p>352 <code class="literal">cardinality(ARRAY[[1,2],[3,4]])</code>353 → <code class="returnvalue">4</code>354 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">355 <a id="id-1.5.8.25.6.2.2.18.1.1.1" class="indexterm"></a>356 <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> )357 → <code class="returnvalue">anyarray</code>358 </p>359 <p>360 Trims an array by removing the last <em class="parameter"><code>n</code></em> elements.361 If the array is multidimensional, only the first dimension is trimmed.362 </p>363 <p>364 <code class="literal">trim_array(ARRAY[1,2,3,4,5,6], 2)</code>365 → <code class="returnvalue">{1,2,3,4}</code>366 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">367 <a id="id-1.5.8.25.6.2.2.19.1.1.1" class="indexterm"></a>368 <code class="function">unnest</code> ( <code class="type">anyarray</code> )369 → <code class="returnvalue">setof anyelement</code>370 </p>371 <p>372 Expands an array into a set of rows.373 The array's elements are read out in storage order.374 </p>375 <p>376 <code class="literal">unnest(ARRAY[1,2])</code>377 → <code class="returnvalue"></code>378</p><pre class="programlisting">379 1380 2381</pre><p>382 </p>383 <p>384 <code class="literal">unnest(ARRAY[['foo','bar'],['baz','quux']])</code>385 → <code class="returnvalue"></code>386</p><pre class="programlisting">387 foo388 bar389 baz390 quux391</pre><p>392 </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">393 <code class="function">unnest</code> ( <code class="type">anyarray</code>, <code class="type">anyarray</code> [<span class="optional">, ... </span>] )394 → <code class="returnvalue">setof anyelement, anyelement [, ... ]</code>395 </p>396 <p>397 Expands multiple arrays (possibly of different data types) into a set of398 rows. If the arrays are not all the same length then the shorter ones399 are padded with <code class="literal">NULL</code>s. This form is only allowed400 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>.401 </p>402 <p>403 <code class="literal">select * from unnest(ARRAY[1,2], ARRAY['foo','bar','baz']) as x(a,b)</code>404 → <code class="returnvalue"></code>405</p><pre class="programlisting">406 a | b407---+-----408 1 | foo409 2 | bar410 | baz411</pre><p>412 </p></td></tr></tbody></table></div></div><br class="table-break" /><p>413 See also <a class="xref" href="functions-aggregate.html" title="9.21. Aggregate Functions">Section 9.21</a> about the aggregate414 function <code class="function">array_agg</code> for use with arrays.415 </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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.20. Range/Multirange Functions and Operators</td></tr></table></div></body></html>