~ubuntu-branches/debian/sid/postgresql-9.3/sid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.3beta1 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Type Conversion"
HREF="typeconv.html"><LINK
REL="PREVIOUS"
TITLE="Operators"
HREF="typeconv-oper.html"><LINK
REL="NEXT"
TITLE="Value Storage"
HREF="typeconv-query.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2013-05-06T21:00:50"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.3beta1 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Operators"
HREF="typeconv-oper.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="typeconv.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 10. Type Conversion</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Value Storage"
HREF="typeconv-query.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TYPECONV-FUNC"
>10.3. Functions</A
></H1
><P
>   The specific function that is referenced by a function call
   is determined using the following procedure.
  </P
><DIV
CLASS="PROCEDURE"
><P
><B
>Function Type Resolution</B
></P
><OL
TYPE="1"
><LI
CLASS="STEP"
><P
>Select the functions to be considered from the
<CODE
CLASS="CLASSNAME"
>pg_proc</CODE
> system catalog.  If a non-schema-qualified
function name was used, the functions
considered are those with the matching name and argument count that are
visible in the current search path (see <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PATH"
>Section 5.7.3</A
>).
If a qualified function name was given, only functions in the specified
schema are considered.</P
><OL
CLASS="SUBSTEPS"
TYPE="a"
><LI
CLASS="STEP"
><P
>If the search path finds multiple functions of identical argument types,
only the one appearing earliest in the path is considered.  Functions of
different argument types are considered on an equal footing regardless of
search path position.</P
></LI
><LI
CLASS="STEP"
><P
>If a function is declared with a <TT
CLASS="LITERAL"
>VARIADIC</TT
> array parameter, and
the call does not use the <TT
CLASS="LITERAL"
>VARIADIC</TT
> keyword, then the function
is treated as if the array parameter were replaced by one or more occurrences
of its element type, as needed to match the call.  After such expansion the
function might have effective argument types identical to some non-variadic
function.  In that case the function appearing earlier in the search path is
used, or if the two functions are in the same schema, the non-variadic one is
preferred.</P
></LI
><LI
CLASS="STEP"
><P
>Functions that have default values for parameters are considered to match any
call that omits zero or more of the defaultable parameter positions.  If more
than one such function matches a call, the one appearing earliest in the
search path is used.  If there are two or more such functions in the same
schema with identical parameter types in the non-defaulted positions (which is
possible if they have different sets of defaultable parameters), the system
will not be able to determine which to prefer, and so an <SPAN
CLASS="QUOTE"
>"ambiguous
function call"</SPAN
> error will result if no better match to the call can be
found.</P
></LI
></OL
></LI
><LI
CLASS="STEP"
><P
>Check for a function accepting exactly the input argument types.
If one exists (there can be only one exact match in the set of
functions considered), use it.
(Cases involving <TT
CLASS="TYPE"
>unknown</TT
> will never find a match at
this step.)</P
></LI
><LI
CLASS="STEP"
><P
>If no exact match is found, see if the function call appears
to be a special type conversion request.  This happens if the function call
has just one argument and the function name is the same as the (internal)
name of some data type.  Furthermore, the function argument must be either
an unknown-type literal, or a type that is binary-coercible to the named
data type, or a type that could be converted to the named data type by
applying that type's I/O functions (that is, the conversion is either to or
from one of the standard string types).  When these conditions are met,
the function call is treated as a form of <TT
CLASS="LITERAL"
>CAST</TT
> specification.
  <A
NAME="AEN21298"
HREF="#FTN.AEN21298"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></P
></LI
><LI
CLASS="STEP"
><P
>Look for the best match.</P
><OL
CLASS="SUBSTEPS"
TYPE="a"
><LI
CLASS="STEP"
><P
>Discard candidate functions for which the input types do not match
and cannot be converted (using an implicit conversion) to match.
<TT
CLASS="TYPE"
>unknown</TT
> literals are
assumed to be convertible to anything for this purpose.  If only one
candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><P
>Run through all candidates and keep those with the most exact matches
on input types.  (Domains are considered the same as their base type
for this purpose.)  Keep all candidates if none have exact matches.
If only one candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><P
>Run through all candidates and keep those that accept preferred types (of the
input data type's type category) at the most positions where type conversion
will be required.
Keep all candidates if none accept preferred types.
If only one candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><P
>If any input arguments are <TT
CLASS="TYPE"
>unknown</TT
>, check the type categories
accepted
at those argument positions by the remaining candidates.  At each position,
select the <TT
CLASS="TYPE"
>string</TT
> category if any candidate accepts that category.
(This bias towards string
is appropriate since an unknown-type literal looks like a string.)
Otherwise, if all the remaining candidates accept the same type category,
select that category; otherwise fail because
the correct choice cannot be deduced without more clues.
Now discard candidates that do not accept the selected type category.
Furthermore, if any candidate accepts a preferred type in that category,
discard candidates that accept non-preferred types for that argument.
Keep all candidates if none survive these tests.
If only one candidate remains, use it; else continue to the next step.</P
></LI
><LI
CLASS="STEP"
><P
>If there are both <TT
CLASS="TYPE"
>unknown</TT
> and known-type arguments, and all
the known-type arguments have the same type, assume that the
<TT
CLASS="TYPE"
>unknown</TT
> arguments are also of that type, and check which
candidates can accept that type at the <TT
CLASS="TYPE"
>unknown</TT
>-argument
positions.  If exactly one candidate passes this test, use it.
Otherwise, fail.</P
></LI
></OL
></LI
></OL
></DIV
><P
>Note that the <SPAN
CLASS="QUOTE"
>"best match"</SPAN
> rules are identical for operator and
function type resolution.
Some examples follow.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21322"
></A
><P
><B
>Example 10-5. Rounding Function Argument Type Resolution</B
></P
><P
>There is only one <CODE
CLASS="FUNCTION"
>round</CODE
> function that takes two
arguments; it takes a first argument of type <TT
CLASS="TYPE"
>numeric</TT
> and
a second argument of type <TT
CLASS="TYPE"
>integer</TT
>.
So the following query automatically converts
the first argument of type <TT
CLASS="TYPE"
>integer</TT
> to
<TT
CLASS="TYPE"
>numeric</TT
>:

</P><PRE
CLASS="SCREEN"
>SELECT round(4, 4);

 round
--------
 4.0000
(1 row)</PRE
><P>

That query is actually transformed by the parser to:
</P><PRE
CLASS="SCREEN"
>SELECT round(CAST (4 AS numeric), 4);</PRE
><P></P
><P
>Since numeric constants with decimal points are initially assigned the
type <TT
CLASS="TYPE"
>numeric</TT
>, the following query will require no type
conversion and therefore might be slightly more efficient:
</P><PRE
CLASS="SCREEN"
>SELECT round(4.0, 4);</PRE
><P></P
></DIV
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN21335"
></A
><P
><B
>Example 10-6. Substring Function Type Resolution</B
></P
><P
>There are several <CODE
CLASS="FUNCTION"
>substr</CODE
> functions, one of which
takes types <TT
CLASS="TYPE"
>text</TT
> and <TT
CLASS="TYPE"
>integer</TT
>.  If called
with a string constant of unspecified type, the system chooses the
candidate function that accepts an argument of the preferred category
<TT
CLASS="LITERAL"
>string</TT
> (namely of type <TT
CLASS="TYPE"
>text</TT
>).

</P><PRE
CLASS="SCREEN"
>SELECT substr('1234', 3);

 substr
--------
     34
(1 row)</PRE
><P></P
><P
>If the string is declared to be of type <TT
CLASS="TYPE"
>varchar</TT
>, as might be the case
if it comes from a table, then the parser will try to convert it to become <TT
CLASS="TYPE"
>text</TT
>:
</P><PRE
CLASS="SCREEN"
>SELECT substr(varchar '1234', 3);

 substr
--------
     34
(1 row)</PRE
><P>

This is transformed by the parser to effectively become:
</P><PRE
CLASS="SCREEN"
>SELECT substr(CAST (varchar '1234' AS text), 3);</PRE
><P></P
><P
></P><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>The parser learns from the <TT
CLASS="STRUCTNAME"
>pg_cast</TT
> catalog that
<TT
CLASS="TYPE"
>text</TT
> and <TT
CLASS="TYPE"
>varchar</TT
>
are binary-compatible, meaning that one can be passed to a function that
accepts the other without doing any physical conversion.  Therefore, no
type conversion call is really inserted in this case.</P
></BLOCKQUOTE
></DIV
><P></P
><P
>And, if the function is called with an argument of type <TT
CLASS="TYPE"
>integer</TT
>,
the parser will try to convert that to <TT
CLASS="TYPE"
>text</TT
>:
</P><PRE
CLASS="SCREEN"
>SELECT substr(1234, 3);
ERROR:  function substr(integer, integer) does not exist
HINT:  No function matches the given name and argument types. You might need
to add explicit type casts.</PRE
><P>

This does not work because <TT
CLASS="TYPE"
>integer</TT
> does not have an implicit cast
to <TT
CLASS="TYPE"
>text</TT
>.  An explicit cast will work, however:
</P><PRE
CLASS="SCREEN"
>SELECT substr(CAST (1234 AS text), 3);

 substr
--------
     34
(1 row)</PRE
><P></P
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN21298"
HREF="typeconv-func.html#AEN21298"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>    The reason for this step is to support function-style cast specifications
    in cases where there is not an actual cast function.  If there is a cast
    function, it is conventionally named after its output type, and so there
    is no need to have a special case.  See
    <A
HREF="sql-createcast.html"
>CREATE CAST</A
>
    for additional commentary.
   </P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="typeconv-oper.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="typeconv-query.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Operators</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="typeconv.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Value Storage</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>