Home Explore Blog CI



postgresql

34th chunk of `doc/src/sgml/syntax.sgml`
5a1c15f8fe57de05333613fafd28c2a12f080c3f3ff34b470000000100000d59
 <structfield>employees</structfield> equal to zero, the division-by-zero error
    will occur before there is any opportunity to test the result of
    <function>min()</function>.  Instead, use a <literal>WHERE</literal>
    or <literal>FILTER</literal> clause to prevent problematic input rows from
    reaching an aggregate function in the first place.
   </para>
  </sect2>
 </sect1>

 <sect1 id="sql-syntax-calling-funcs">
  <title>Calling Functions</title>

   <indexterm zone="sql-syntax-calling-funcs">
    <primary>notation</primary>
    <secondary>functions</secondary>
   </indexterm>

   <para>
    <productname>PostgreSQL</productname> allows functions that have named
    parameters to be called using either <firstterm>positional</firstterm> or
    <firstterm>named</firstterm> notation.  Named notation is especially
    useful for functions that have a large number of parameters, since it
    makes the associations between parameters and actual arguments more
    explicit and reliable.
    In positional notation, a function call is written with
    its argument values in the same order as they are defined in the function
    declaration.  In named notation, the arguments are matched to the
    function parameters by name and can be written in any order.
    For each notation, also consider the effect of function argument types,
    documented in <xref linkend="typeconv-func"/>.
   </para>

   <para>
    In either notation, parameters that have default values given in the
    function declaration need not be written in the call at all.  But this
    is particularly useful in named notation, since any combination of
    parameters can be omitted; while in positional notation parameters can
    only be omitted from right to left.
   </para>

   <para>
    <productname>PostgreSQL</productname> also supports
    <firstterm>mixed</firstterm> notation, which combines positional and
    named notation.  In this case, positional parameters are written first
    and named parameters appear after them.
   </para>

   <para>
    The following examples will illustrate the usage of all three
    notations, using the following function definition:
<programlisting>
CREATE FUNCTION concat_lower_or_upper(a text, b text, uppercase boolean DEFAULT false)
RETURNS text
AS
$$
 SELECT CASE
        WHEN $3 THEN UPPER($1 || ' ' || $2)
        ELSE LOWER($1 || ' ' || $2)
        END;
$$
LANGUAGE SQL IMMUTABLE STRICT;
</programlisting>
    Function <function>concat_lower_or_upper</function> has two mandatory
    parameters, <literal>a</literal> and <literal>b</literal>.  Additionally
    there is one optional parameter <literal>uppercase</literal> which defaults
    to <literal>false</literal>.  The <literal>a</literal> and
    <literal>b</literal> inputs will be concatenated, and forced to either
    upper or lower case depending on the <literal>uppercase</literal>
    parameter.  The remaining details of this function
    definition are not important here (see <xref linkend="extend"/> for
    more information).
   </para>

   <sect2 id="sql-syntax-calling-funcs-positional">
    <title>Using Positional Notation</title>

    <indexterm>
     <primary>function</primary>
     <secondary>positional notation</secondary>
    </indexterm>

    <para>
     Positional notation is the traditional mechanism for passing arguments
     to functions in <productname>PostgreSQL</productname>.

Title: Calling Functions in PostgreSQL: Positional, Named, and Mixed Notation
Summary
This section discusses how to call functions in PostgreSQL using positional, named, and mixed notation. Positional notation is the traditional method, where arguments are passed in the order defined in the function. Named notation allows arguments to be matched by name, offering more clarity, especially for functions with many parameters. Mixed notation combines both, with positional arguments first, followed by named arguments. The section uses an example function, `concat_lower_or_upper`, to illustrate these notations, highlighting the flexibility of omitting parameters with default values, especially in named notation.