Database Reference
In-Depth Information
CREATE OR REPLACE FUNCTION add(VARIADIC a
int[]) RETURNS int
AS '$libdir/add_func', 'add_int32_array'
LANGUAGE C STRICT;
The previous calls to
add_arr()
can be rewritten as:
hannu=# select add(1,2,3,4,5,6,7,8,9);
-[ RECORD 1 ]
add | 45
hannu=# select add(NULL);
-[ RECORD 1 ]
add |
hannu=# select add(1,2,NULL);
-[ RECORD 1 ]
add | 3
Notice that you can't easily get the
ERROR: 1-dimensional array needed
as
VARIADIC
always constructs a one-dimensional array from the arguments.
The only thing missing is that you can't have PostgreSQL's function overloading
mechanism to distinguish between
add( a int[])
and
add(VARIADIC a
int[])
—you simply can't declare both of these at the same time because for Post-
greSQL they are the same function with only the initial argument detection done dif-
ferently. That is why the array version of the function was named
add_arr
. In case
you need to call one
VARIADIC
function from another, there is a way. You can call
the
VARIADIC
version with an argument of array type by prefixing the argument with
VARIADIC
on call side:
hannu=# select add(ARRAY[1,2,NULL]);
.
ERROR: function add(integer[]) does not exist
LINE 1: select add(ARRAY[1,2,NULL]);
^
HINT: No function matches the given name and
argument types. You might need to add explicit