On Tue, Nov 11, 2014 at 5:26 PM, Marti Raudsepp <ma...@juffo.org> wrote:
> Perhaps should be:
> <command>SELECT</> and <literal>HAVING</> clauses are evaluated.
>
> Other than that, looks good to me.
Let's do so then and mark that as ready for committer.
Thanks,
-- 
Michael
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index b58cfa5..2d5e0d3 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -11254,6 +11254,25 @@ SELECT NULLIF(value, '(none)') ...
     all are NULL.
    </para>
   </sect2>
+
+  <sect2 id="functions-conditional-implementation">
+   <title>Implementation Notes</title>
+   <para>
+    When a conditional expression depends on the results of aggregate
+    functions in a <command>SELECT</> or <literal>HAVING</> clause, then all
+    the aggregate functions are always evaluated prior to determining which
+    branch the conditional should take.  For example, the following query can
+    cause a division-by-zero error despite only using division in a branch
+    that requires positive values:
+
+<programlisting>
+SELECT CASE WHEN min(employees) > 0
+            THEN average(expenses / employees)
+       END
+    FROM departments;
+</programlisting>
+   </para>
+  </sect2>
  </sect1>
 
  <sect1 id="functions-array">
diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml
index 473939a..7001ea6 100644
--- a/doc/src/sgml/ref/select.sgml
+++ b/doc/src/sgml/ref/select.sgml
@@ -111,9 +111,11 @@ TABLE [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ]
      <para>
       If the <literal>GROUP BY</literal> clause is specified, the
       output is combined into groups of rows that match on one or more
-      values.  If the <literal>HAVING</literal> clause is present, it
-      eliminates groups that do not satisfy the given condition.  (See
-      <xref linkend="sql-groupby" endterm="sql-groupby-title"> and
+      values.  All aggregate functions and their argument expressions
+      in <command>SELECT</literal> and <literal>HAVING</literal> clauses
+      are evaluated.  If the <literal>HAVING</literal> clause is present,
+      it eliminates groups that do not satisfy the given condition.  (See
+      <xref linkend="sql-groupby" endterm="sql-groupby-title">, and
       <xref linkend="sql-having" endterm="sql-having-title"> below.)
      </para>
     </listitem>
-- 
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs

Reply via email to