]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/typeconv-func.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / typeconv-func.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>10.3. Functions</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="typeconv-oper.html" title="10.2. Operators" /><link rel="next" href="typeconv-query.html" title="10.4. Value Storage" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">10.3. Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="typeconv-oper.html" title="10.2. Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><th width="60%" align="center">Chapter 10. Type Conversion</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="typeconv-query.html" title="10.4. Value Storage">Next</a></td></tr></table><hr /></div><div class="sect1" id="TYPECONV-FUNC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">10.3. Functions <a href="#TYPECONV-FUNC" class="id_link">#</a></h2></div></div></div><a id="id-1.5.9.8.2" class="indexterm"></a><p>
3    The specific function that is referenced by a function call
4    is determined using the following procedure.
5   </p><div class="procedure" id="id-1.5.9.8.4"><p class="title"><strong>Function Type Resolution</strong></p><ol class="procedure" type="1"><li class="step"><p>
6 Select the functions to be considered from the
7 <code class="classname">pg_proc</code> system catalog.  If a non-schema-qualified
8 function name was used, the functions
9 considered are those with the matching name and argument count that are
10 visible in the current search path (see <a class="xref" href="ddl-schemas.html#DDL-SCHEMAS-PATH" title="5.10.3. The Schema Search Path">Section 5.10.3</a>).
11 If a qualified function name was given, only functions in the specified
12 schema are considered.
13 </p><ol type="a" class="substeps"><li class="step"><p>
14 If the search path finds multiple functions of identical argument types,
15 only the one appearing earliest in the path is considered.  Functions of
16 different argument types are considered on an equal footing regardless of
17 search path position.
18 </p></li><li class="step"><p>
19 If a function is declared with a <code class="literal">VARIADIC</code> array parameter, and
20 the call does not use the <code class="literal">VARIADIC</code> keyword, then the function
21 is treated as if the array parameter were replaced by one or more occurrences
22 of its element type, as needed to match the call.  After such expansion the
23 function might have effective argument types identical to some non-variadic
24 function.  In that case the function appearing earlier in the search path is
25 used, or if the two functions are in the same schema, the non-variadic one is
26 preferred.
27 </p><p>
28 This creates a security hazard when calling, via qualified name
29   <a href="#ftn.FUNC-QUALIFIED-SECURITY" class="footnote"><sup class="footnote" id="FUNC-QUALIFIED-SECURITY">[10]</sup></a>,
30 a variadic function found in a schema that permits untrusted users to create
31 objects.  A malicious user can take control and execute arbitrary SQL
32 functions as though you executed them.  Substitute a call bearing
33 the <code class="literal">VARIADIC</code> keyword, which bypasses this hazard.  Calls
34 populating <code class="literal">VARIADIC "any"</code> parameters often have no
35 equivalent formulation containing the <code class="literal">VARIADIC</code> keyword.  To
36 issue those calls safely, the function's schema must permit only trusted users
37 to create objects.
38 </p></li><li class="step"><p>
39 Functions that have default values for parameters are considered to match any
40 call that omits zero or more of the defaultable parameter positions.  If more
41 than one such function matches a call, the one appearing earliest in the
42 search path is used.  If there are two or more such functions in the same
43 schema with identical parameter types in the non-defaulted positions (which is
44 possible if they have different sets of defaultable parameters), the system
45 will not be able to determine which to prefer, and so an <span class="quote">“<span class="quote">ambiguous
46 function call</span>”</span> error will result if no better match to the call can be
47 found.
48 </p><p>
49 This creates an availability hazard when calling, via qualified
50 name<a href="typeconv-func.html#ftn.FUNC-QUALIFIED-SECURITY" class="footnoteref"><sup class="footnoteref">[10]</sup></a>, any function found in a
51 schema that permits untrusted users to create objects.  A malicious user can
52 create a function with the name of an existing function, replicating that
53 function's parameters and appending novel parameters having default values.
54 This precludes new calls to the original function.  To forestall this hazard,
55 place functions in schemas that permit only trusted users to create objects.
56 </p></li></ol></li><li class="step"><p>
57 Check for a function accepting exactly the input argument types.
58 If one exists (there can be only one exact match in the set of
59 functions considered), use it.  Lack of an exact match creates a security
60 hazard when calling, via qualified
61 name<a href="typeconv-func.html#ftn.FUNC-QUALIFIED-SECURITY" class="footnoteref"><sup class="footnoteref">[10]</sup></a>, a function found in a
62 schema that permits untrusted users to create objects.  In such situations,
63 cast arguments to force an exact match.  (Cases involving <code class="type">unknown</code>
64 will never find a match at this step.)
65 </p></li><li class="step"><p>
66 If no exact match is found, see if the function call appears
67 to be a special type conversion request.  This happens if the function call
68 has just one argument and the function name is the same as the (internal)
69 name of some data type.  Furthermore, the function argument must be either
70 an unknown-type literal, or a type that is binary-coercible to the named
71 data type, or a type that could be converted to the named data type by
72 applying that type's I/O functions (that is, the conversion is either to or
73 from one of the standard string types).  When these conditions are met,
74 the function call is treated as a form of <code class="literal">CAST</code> specification.
75   <a href="#ftn.id-1.5.9.8.4.4.1.2" class="footnote"><sup class="footnote" id="id-1.5.9.8.4.4.1.2">[11]</sup></a>
76 </p></li><li class="step"><p>
77 Look for the best match.
78 </p><ol type="a" class="substeps"><li class="step"><p>
79 Discard candidate functions for which the input types do not match
80 and cannot be converted (using an implicit conversion) to match.
81 <code class="type">unknown</code> literals are
82 assumed to be convertible to anything for this purpose.  If only one
83 candidate remains, use it; else continue to the next step.
84 </p></li><li class="step"><p>
85 If any input argument is of a domain type, treat it as being of the
86 domain's base type for all subsequent steps.  This ensures that domains
87 act like their base types for purposes of ambiguous-function resolution.
88 </p></li><li class="step"><p>
89 Run through all candidates and keep those with the most exact matches
90 on input types.  Keep all candidates if none have exact matches.
91 If only one candidate remains, use it; else continue to the next step.
92 </p></li><li class="step"><p>
93 Run through all candidates and keep those that accept preferred types (of the
94 input data type's type category) at the most positions where type conversion
95 will be required.
96 Keep all candidates if none accept preferred types.
97 If only one candidate remains, use it; else continue to the next step.
98 </p></li><li class="step"><p>
99 If any input arguments are <code class="type">unknown</code>, check the type categories
100 accepted
101 at those argument positions by the remaining candidates.  At each position,
102 select the <code class="type">string</code> category if any candidate accepts that category.
103 (This bias towards string
104 is appropriate since an unknown-type literal looks like a string.)
105 Otherwise, if all the remaining candidates accept the same type category,
106 select that category; otherwise fail because
107 the correct choice cannot be deduced without more clues.
108 Now discard candidates that do not accept the selected type category.
109 Furthermore, if any candidate accepts a preferred type in that category,
110 discard candidates that accept non-preferred types for that argument.
111 Keep all candidates if none survive these tests.
112 If only one candidate remains, use it; else continue to the next step.
113 </p></li><li class="step"><p>
114 If there are both <code class="type">unknown</code> and known-type arguments, and all
115 the known-type arguments have the same type, assume that the
116 <code class="type">unknown</code> arguments are also of that type, and check which
117 candidates can accept that type at the <code class="type">unknown</code>-argument
118 positions.  If exactly one candidate passes this test, use it.
119 Otherwise, fail.
120 </p></li></ol></li></ol></div><p>
121 Note that the <span class="quote">“<span class="quote">best match</span>”</span> rules are identical for operator and
122 function type resolution.
123 Some examples follow.
124 </p><div class="example" id="id-1.5.9.8.6"><p class="title"><strong>Example 10.6. Rounding Function Argument Type Resolution</strong></p><div class="example-contents"><p>
125 There is only one <code class="function">round</code> function that takes two
126 arguments; it takes a first argument of type <code class="type">numeric</code> and
127 a second argument of type <code class="type">integer</code>.
128 So the following query automatically converts
129 the first argument of type <code class="type">integer</code> to
130 <code class="type">numeric</code>:
131
132 </p><pre class="screen">
133 SELECT round(4, 4);
134
135  round
136 --------
137  4.0000
138 (1 row)
139 </pre><p>
140
141 That query is actually transformed by the parser to:
142 </p><pre class="screen">
143 SELECT round(CAST (4 AS numeric), 4);
144 </pre><p>
145 </p><p>
146 Since numeric constants with decimal points are initially assigned the
147 type <code class="type">numeric</code>, the following query will require no type
148 conversion and therefore might be slightly more efficient:
149 </p><pre class="screen">
150 SELECT round(4.0, 4);
151 </pre><p>
152 </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.8.7"><p class="title"><strong>Example 10.7. Variadic Function Resolution</strong></p><div class="example-contents"><p>
153 </p><pre class="screen">
154 CREATE FUNCTION public.variadic_example(VARIADIC numeric[]) RETURNS int
155   LANGUAGE sql AS 'SELECT 1';
156 CREATE FUNCTION
157 </pre><p>
158
159 This function accepts, but does not require, the VARIADIC keyword.  It
160 tolerates both integer and numeric arguments:
161
162 </p><pre class="screen">
163 SELECT public.variadic_example(0),
164        public.variadic_example(0.0),
165        public.variadic_example(VARIADIC array[0.0]);
166  variadic_example | variadic_example | variadic_example
167 ------------------+------------------+------------------
168                 1 |                1 |                1
169 (1 row)
170 </pre><p>
171
172 However, the first and second calls will prefer more-specific functions, if
173 available:
174
175 </p><pre class="screen">
176 CREATE FUNCTION public.variadic_example(numeric) RETURNS int
177   LANGUAGE sql AS 'SELECT 2';
178 CREATE FUNCTION
179
180 CREATE FUNCTION public.variadic_example(int) RETURNS int
181   LANGUAGE sql AS 'SELECT 3';
182 CREATE FUNCTION
183
184 SELECT public.variadic_example(0),
185        public.variadic_example(0.0),
186        public.variadic_example(VARIADIC array[0.0]);
187  variadic_example | variadic_example | variadic_example
188 ------------------+------------------+------------------
189                 3 |                2 |                1
190 (1 row)
191 </pre><p>
192
193 Given the default configuration and only the first function existing, the
194 first and second calls are insecure.  Any user could intercept them by
195 creating the second or third function.  By matching the argument type exactly
196 and using the <code class="literal">VARIADIC</code> keyword, the third call is secure.
197 </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.8.8"><p class="title"><strong>Example 10.8. Substring Function Type Resolution</strong></p><div class="example-contents"><p>
198 There are several <code class="function">substr</code> functions, one of which
199 takes types <code class="type">text</code> and <code class="type">integer</code>.  If called
200 with a string constant of unspecified type, the system chooses the
201 candidate function that accepts an argument of the preferred category
202 <code class="literal">string</code> (namely of type <code class="type">text</code>).
203
204 </p><pre class="screen">
205 SELECT substr('1234', 3);
206
207  substr
208 --------
209      34
210 (1 row)
211 </pre><p>
212 </p><p>
213 If the string is declared to be of type <code class="type">varchar</code>, as might be the case
214 if it comes from a table, then the parser will try to convert it to become <code class="type">text</code>:
215 </p><pre class="screen">
216 SELECT substr(varchar '1234', 3);
217
218  substr
219 --------
220      34
221 (1 row)
222 </pre><p>
223
224 This is transformed by the parser to effectively become:
225 </p><pre class="screen">
226 SELECT substr(CAST (varchar '1234' AS text), 3);
227 </pre><p>
228 </p><p>
229 </p><div class="note"><h3 class="title">Note</h3><p>
230 The parser learns from the <code class="structname">pg_cast</code> catalog that
231 <code class="type">text</code> and <code class="type">varchar</code>
232 are binary-compatible, meaning that one can be passed to a function that
233 accepts the other without doing any physical conversion.  Therefore, no
234 type conversion call is really inserted in this case.
235 </p></div><p>
236 </p><p>
237 And, if the function is called with an argument of type <code class="type">integer</code>,
238 the parser will try to convert that to <code class="type">text</code>:
239 </p><pre class="screen">
240 SELECT substr(1234, 3);
241 ERROR:  function substr(integer, integer) does not exist
242 HINT:  No function matches the given name and argument types. You might need
243 to add explicit type casts.
244 </pre><p>
245
246 This does not work because <code class="type">integer</code> does not have an implicit cast
247 to <code class="type">text</code>.  An explicit cast will work, however:
248 </p><pre class="screen">
249 SELECT substr(CAST (1234 AS text), 3);
250
251  substr
252 --------
253      34
254 (1 row)
255 </pre><p>
256 </p></div></div><br class="example-break" /><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.FUNC-QUALIFIED-SECURITY" class="footnote"><p><a href="#FUNC-QUALIFIED-SECURITY" class="para"><sup class="para">[10] </sup></a>
257     The hazard does not arise with a non-schema-qualified name, because a
258     search path containing schemas that permit untrusted users to create
259     objects is not a <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.10.6. Usage Patterns">secure schema usage
260     pattern</a>.
261    </p></div><div id="ftn.id-1.5.9.8.4.4.1.2" class="footnote"><p><a href="#id-1.5.9.8.4.4.1.2" class="para"><sup class="para">[11] </sup></a>
262     The reason for this step is to support function-style cast specifications
263     in cases where there is not an actual cast function.  If there is a cast
264     function, it is conventionally named after its output type, and so there
265     is no need to have a special case.  See
266     <a class="xref" href="sql-createcast.html" title="CREATE CAST"><span class="refentrytitle">CREATE CAST</span></a>
267     for additional commentary.
268    </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="typeconv-oper.html" title="10.2. Operators">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="typeconv-query.html" title="10.4. Value Storage">Next</a></td></tr><tr><td width="40%" align="left" valign="top">10.2. Operators </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"> 10.4. Value Storage</td></tr></table></div></body></html>