Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/plpgsql.sgml`
2e23c6ed004eab96f6beab2b05660f55c8dac5666b53f00a0000000100000fa0
 the return value if desired,
      though that is not required.  <literal>$0</literal> can also be
      given an alias.  For example, this function works on any data type
      that has a <literal>+</literal> operator:

<programlisting>
CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement)
RETURNS anyelement AS $$
DECLARE
    result ALIAS FOR $0;
BEGIN
    result := v1 + v2 + v3;
    RETURN result;
END;
$$ LANGUAGE plpgsql;
</programlisting>
     </para>

     <para>
      The same effect can be obtained by declaring one or more output parameters as
      polymorphic types.  In this case the
      special <literal>$0</literal> parameter is not used; the output
      parameters themselves serve the same purpose.  For example:

<programlisting>
CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement,
                                 OUT sum anyelement)
AS $$
BEGIN
    sum := v1 + v2 + v3;
END;
$$ LANGUAGE plpgsql;
</programlisting>
     </para>

     <para>
      In practice it might be more useful to declare a polymorphic function
      using the <type>anycompatible</type> family of types, so that automatic
      promotion of the input arguments to a common type will occur.
      For example:

<programlisting>
CREATE FUNCTION add_three_values(v1 anycompatible, v2 anycompatible, v3 anycompatible)
RETURNS anycompatible AS $$
BEGIN
    RETURN v1 + v2 + v3;
END;
$$ LANGUAGE plpgsql;
</programlisting>

      With this example, a call such as

<programlisting>
SELECT add_three_values(1, 2, 4.7);
</programlisting>

      will work, automatically promoting the integer inputs to numeric.
      The function using <type>anyelement</type> would require you to
      cast the three inputs to the same type manually.
     </para>
    </sect2>

  <sect2 id="plpgsql-declaration-alias">
   <title><literal>ALIAS</literal></title>

<synopsis>
<replaceable>newname</replaceable> ALIAS FOR <replaceable>oldname</replaceable>;
</synopsis>

   <para>
    The <literal>ALIAS</literal> syntax is more general than is suggested in the
    previous section: you can declare an alias for any variable, not just
    function parameters.  The main practical use for this is to assign
    a different name for variables with predetermined names, such as
    <varname>NEW</varname> or <varname>OLD</varname> within
    a trigger function.
   </para>

   <para>
    Examples:
<programlisting>
DECLARE
  prior ALIAS FOR old;
  updated ALIAS FOR new;
</programlisting>
   </para>

   <para>
    Since <literal>ALIAS</literal> creates two different ways to name the same
    object, unrestricted use can be confusing.  It's best to use it only
    for the purpose of overriding predetermined names.
   </para>
   </sect2>

  <sect2 id="plpgsql-declaration-type">
   <title>Copying Types</title>

<synopsis>
<replaceable>name</replaceable> <replaceable>table</replaceable>.<replaceable>column</replaceable>%TYPE
<replaceable>name</replaceable> <replaceable>variable</replaceable>%TYPE
</synopsis>

   <para>
    <literal>%TYPE</literal> provides the data type of a table column
    or a previously-declared <application>PL/pgSQL</application>
    variable. You can use this to declare variables that will hold
    database values. For example, let's say you have a column named
    <literal>user_id</literal> in your <literal>users</literal>
    table. To declare a variable with the same data type as
    <literal>users.user_id</literal> you write:
<programlisting>
user_id users.user_id%TYPE;
</programlisting>
   </para>

   <para>
    It is also possible to write array decoration
    after <literal>%TYPE</literal>, thereby creating a variable that holds
    an array of the referenced type:
<programlisting>
user_ids users.user_id%TYPE[];
user_ids users.user_id%TYPE ARRAY[4];  -- equivalent to the above
</programlisting>
    Just as when declaring table columns that are arrays, it doesn't
    matter whether you write multiple bracket pairs or specific

Title: PL/pgSQL: Polymorphic Types, anycompatible Type Family, and ALIAS
Summary
Polymorphic functions can also be declared with output parameters. It may be more practical to use the anycompatible family of types for automatic input argument promotion. The ALIAS syntax allows assigning different names to variables, useful for predetermined names like NEW or OLD in triggers. Restricting its use to overriding such names is recommended for clarity. Additionally, %TYPE allows declaring variables with the same data type as a table column or another variable, and it can be used with array decoration to create arrays of that type.