1- <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.457 2008/11/12 13:09:27 petere Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.458 2008/11/13 15:59:50 petere Exp $ -->
22
33 <chapter id="functions">
44 <title>Functions and Operators</title>
@@ -8353,7 +8353,7 @@ SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
83538353 </para>
83548354 </sect3>
83558355
8356- <sect3>
8356+ <sect3 id="functions-xml-xmlagg" >
83578357 <title><literal>xmlagg</literal></title>
83588358
83598359 <indexterm>
@@ -8366,10 +8366,10 @@ SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
83668366
83678367 <para>
83688368 The function <function>xmlagg</function> is, unlike the other
8369- functionsbelow , an aggregate function. It concatenates the
8369+ functionsdescribed here , an aggregate function. It concatenates the
83708370 input values to the aggregate function call,
83718371 like <function>xmlconcat</function> does.
8372- See <xref linkend="functions-aggregate"> forgeneral information
8372+ See <xref linkend="functions-aggregate"> foradditional information
83738373 about aggregate functions.
83748374 </para>
83758375
@@ -8387,23 +8387,18 @@ SELECT xmlagg(x) FROM test;
83878387 </para>
83888388
83898389 <para>
8390- Note that in the current implementation, the order of the
8391- concatenation is in principle undefined. Making the input values
8392- to be sorted in some other way will usually work, however. For
8393- instance, in the above example, one could influence the order
8394- like so:
8390+ The influence the order of the concatenation, something like the
8391+ following approach to sort the input values can be used:
8392+
83958393<screen><![CDATA[
83968394SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
83978395 xmlagg
83988396----------------------
83998397 <bar/><foo>abc</foo>
84008398]]></screen>
84018399
8402- But this approach is not guaranteed to work in all situations and
8403- in all versions of PostgreSQL. A future version of PostgreSQL
8404- will probably provide an additional feature to control the order
8405- in a proper way (<literal>xmlagg(expr ORDER BY expr, expr,
8406- ...</literal>).
8400+ Again, see <xref linkend="functions-aggregate"> for additional
8401+ information.
84078402 </para>
84088403 </sect3>
84098404
@@ -9490,6 +9485,11 @@ SELECT NULLIF(value, '(none)') ...
94909485 </tbody>
94919486 </tgroup>
94929487 </table>
9488+
9489+ <para>
9490+ See also <xref linkend="functions-aggregate"> about the aggregate
9491+ function <function>array_agg</function> for use with arrays.
9492+ </para>
94939493 </sect1>
94949494
94959495 <sect1 id="functions-aggregate">
@@ -9526,6 +9526,22 @@ SELECT NULLIF(value, '(none)') ...
95269526 </thead>
95279527
95289528 <tbody>
9529+ <row>
9530+ <entry>
9531+ <indexterm>
9532+ <primary>array_agg</primary>
9533+ </indexterm>
9534+ <function>array_agg(<replaceable class="parameter">expression</replaceable>)</function>
9535+ </entry>
9536+ <entry>
9537+ any
9538+ </entry>
9539+ <entry>
9540+ array of the argument type
9541+ </entry>
9542+ <entry>input values concatenated into an array</entry>
9543+ </row>
9544+
95299545 <row>
95309546 <entry>
95319547 <indexterm>
@@ -9684,6 +9700,22 @@ SELECT NULLIF(value, '(none)') ...
96849700 </entry>
96859701 <entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
96869702 </row>
9703+
9704+ <row>
9705+ <entry>
9706+ <indexterm>
9707+ <primary>xmlagg</primary>
9708+ </indexterm>
9709+ <function>xmlagg(<replaceable class="parameter">expression</replaceable>)</function>
9710+ </entry>
9711+ <entry>
9712+ <type>xml</type>
9713+ </entry>
9714+ <entry>
9715+ <type>xml</type>
9716+ </entry>
9717+ <entry>concatenation of XML values (see also <xref linkend="functions-xml-xmlagg">)</entry>
9718+ </row>
96879719 </tbody>
96889720 </tgroup>
96899721 </table>
@@ -9692,8 +9724,10 @@ SELECT NULLIF(value, '(none)') ...
96929724 It should be noted that except for <function>count</function>,
96939725 these functions return a null value when no rows are selected. In
96949726 particular, <function>sum</function> of no rows returns null, not
9695- zero as one might expect. The <function>coalesce</function> function can be
9696- used to substitute zero for null when necessary.
9727+ zero as one might expect, and <function>array_agg</function>
9728+ returns null rather than an empty array when there are no input
9729+ rows. The <function>coalesce</function> function can be used to
9730+ substitute zero or an empty array for null when necessary.
96979731 </para>
96989732
96999733 <note>
@@ -9733,6 +9767,25 @@ SELECT count(*) FROM sometable;
97339767 </para>
97349768 </note>
97359769
9770+ <para>
9771+ The aggregate functions <function>array_agg</function>
9772+ and <function>xmlagg</function>, as well as similar user-defined
9773+ aggregate functions, produce meaningfully different result values
9774+ depending on the order of the input values. In the current
9775+ implementation, the order of the concatenation is in principle
9776+ undefined. Making the input values to be sorted in some other way
9777+ will usually work, however. For example:
9778+
9779+ <screen><![CDATA[
9780+ SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
9781+ ]]></screen>
9782+
9783+ But this approach is not guaranteed to work in all situations, and
9784+ it is not strictly SQL-conforming. A future version of PostgreSQL
9785+ might provide an additional feature to control the order in a
9786+ better-defined way (<literal>xmlagg(expr ORDER BY expr, expr,
9787+ ...</literal>).
9788+ </para>
97369789
97379790 <para>
97389791 <xref linkend="functions-aggregate-statistics-table"> shows