]> begriffs open source - ai-pg/blob - full-docs/html/xfunc-overload.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / xfunc-overload.html
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>36.6. Function Overloading</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="xfunc-sql.html" title="36.5. Query Language (SQL) Functions" /><link rel="next" href="xfunc-volatility.html" title="36.7. Function Volatility Categories" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">36.6. Function Overloading</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="xfunc-sql.html" title="36.5. Query Language (SQL) Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="extend.html" title="Chapter 36. Extending SQL">Up</a></td><th width="60%" align="center">Chapter 36. Extending <acronym class="acronym">SQL</acronym></th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="xfunc-volatility.html" title="36.7. Function Volatility Categories">Next</a></td></tr></table><hr /></div><div class="sect1" id="XFUNC-OVERLOAD"><div class="titlepage"><div><div><h2 class="title" style="clear: both">36.6. Function Overloading <a href="#XFUNC-OVERLOAD" class="id_link">#</a></h2></div></div></div><a id="id-1.8.3.9.2" class="indexterm"></a><p>
3     More than one function can be defined with the same SQL name, so long
4     as the arguments they take are different.  In other words,
5     function names can be <em class="firstterm">overloaded</em>.  Whether or not
6     you use it, this capability entails security precautions when calling
7     functions in databases where some users mistrust other users; see
8     <a class="xref" href="typeconv-func.html" title="10.3. Functions">Section 10.3</a>.  When a query is executed, the server
9     will determine which function to call from the data types and the number
10     of the provided arguments.  Overloading can also be used to simulate
11     functions with a variable number of arguments, up to a finite maximum
12     number.
13    </p><p>
14     When creating a family of overloaded functions, one should be
15     careful not to create ambiguities.  For instance, given the
16     functions:
17 </p><pre class="programlisting">
18 CREATE FUNCTION test(int, real) RETURNS ...
19 CREATE FUNCTION test(smallint, double precision) RETURNS ...
20 </pre><p>
21     it is not immediately clear which function would be called with
22     some trivial input like <code class="literal">test(1, 1.5)</code>.  The
23     currently implemented resolution rules are described in
24     <a class="xref" href="typeconv.html" title="Chapter 10. Type Conversion">Chapter 10</a>, but it is unwise to design a system that subtly
25     relies on this behavior.
26    </p><p>
27     A function that takes a single argument of a composite type should
28     generally not have the same name as any attribute (field) of that type.
29     Recall that <code class="literal"><em class="replaceable"><code>attribute</code></em>(<em class="replaceable"><code>table</code></em>)</code>
30     is considered equivalent
31     to <code class="literal"><em class="replaceable"><code>table</code></em>.<em class="replaceable"><code>attribute</code></em></code>.
32     In the case that there is an
33     ambiguity between a function on a composite type and an attribute of
34     the composite type, the attribute will always be used.  It is possible
35     to override that choice by schema-qualifying the function name
36     (that is, <code class="literal"><em class="replaceable"><code>schema</code></em>.<em class="replaceable"><code>func</code></em>(<em class="replaceable"><code>table</code></em>)
37     </code>) but it's better to
38     avoid the problem by not choosing conflicting names.
39    </p><p>
40     Another possible conflict is between variadic and non-variadic functions.
41     For instance, it is possible to create both <code class="literal">foo(numeric)</code> and
42     <code class="literal">foo(VARIADIC numeric[])</code>.  In this case it is unclear which one
43     should be matched to a call providing a single numeric argument, such as
44     <code class="literal">foo(10.1)</code>.  The rule is that the function appearing
45     earlier in the search path is used, or if the two functions are in the
46     same schema, the non-variadic one is preferred.
47    </p><p>
48     When overloading C-language functions, there is an additional
49     constraint: The C name of each function in the family of
50     overloaded functions must be different from the C names of all
51     other functions, either internal or dynamically loaded.  If this
52     rule is violated, the behavior is not portable.  You might get a
53     run-time linker error, or one of the functions will get called
54     (usually the internal one).  The alternative form of the
55     <code class="literal">AS</code> clause for the SQL <code class="command">CREATE
56     FUNCTION</code> command decouples the SQL function name from
57     the function name in the C source code.  For instance:
58 </p><pre class="programlisting">
59 CREATE FUNCTION test(int) RETURNS int
60     AS '<em class="replaceable"><code>filename</code></em>', 'test_1arg'
61     LANGUAGE C;
62 CREATE FUNCTION test(int, int) RETURNS int
63     AS '<em class="replaceable"><code>filename</code></em>', 'test_2arg'
64     LANGUAGE C;
65 </pre><p>
66     The names of the C functions here reflect one of many possible conventions.
67    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="xfunc-sql.html" title="36.5. Query Language (SQL) Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extend.html" title="Chapter 36. Extending SQL">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="xfunc-volatility.html" title="36.7. Function Volatility Categories">Next</a></td></tr><tr><td width="40%" align="left" valign="top">36.5. Query Language (<acronym class="acronym">SQL</acronym>) Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 36.7. Function Volatility Categories</td></tr></table></div></body></html>