The argument types of function calls are resolved according to the
following steps.
Example 7-4. Factorial Function Argument Type Resolution
There is only one int4fac function defined in the
pg_proc catalog.
So the following query automatically converts the int2 argument
to int4:
tgl=> SELECT int4fac(int2 '4');
int4fac
---------
24
(1 row)
and is actually transformed by the parser to
tgl=> SELECT int4fac(int4(int2 '4'));
int4fac
---------
24
(1 row)
Example 7-5. Substring Function Type Resolution
There are two substr functions declared in pg_proc. However,
only one takes two arguments, of types text and int4.
If called with a string constant of unspecified type, the type is matched up
directly with the only candidate function type:
tgl=> SELECT substr('1234', 3);
substr
--------
34
(1 row)
If the string is declared to be of type varchar, as might be the case
if it comes from a table, then the parser will try to coerce it to become text:
tgl=> SELECT substr(varchar '1234', 3);
substr
--------
34
(1 row)
which is transformed by the parser to become
tgl=> SELECT substr(text(varchar '1234'), 3);
substr
--------
34
(1 row)
Note: Actually, the parser is aware that text and varchar
are binary-compatible, meaning that one can be passed to a function that
accepts the other without doing any physical conversion. Therefore, no
explicit type conversion call is really inserted in this case.
And, if the function is called with an int4, the parser will
try to convert that to text:
tgl=> SELECT substr(1234, 3);
substr
--------
34
(1 row)
which actually executes as
tgl=> SELECT substr(text(1234), 3);
substr
--------
34
(1 row)
This succeeds because there is a conversion function text(int4) in the
system catalog.