-- A VARIADIC "any" function is declared with one argument for all of its -- variadic ones, but PostgreSQL passes each of them separately, each with its -- own type. Only the declared argument was converted, so `arguments` held the -- first value and the rest were silently dropped. CREATE FUNCTION va_show(VARIADIC "any") RETURNS text AS $$ return Array.from(arguments).map(a => typeof a + ':' + JSON.stringify(a)).join(' '); $$ LANGUAGE pljs; SELECT va_show(1, 'two'::text, 3.5::float8, true, '{"k":[4]}'::jsonb, ARRAY[5, 6]); va_show ----------------------------------------------------------------------------- number:1 string:"two" number:3.5 boolean:true object:{"k":[4]} object:[5,6] (1 row) SELECT va_show(1); va_show ---------- number:1 (1 row) SELECT va_show(NULL::int, 'x'::text, NULL::text); va_show ------------------------------------ object:null string:"x" object:null (1 row) -- The named parameter is still the first value. CREATE FUNCTION va_named(VARIADIC args "any") RETURNS text AS $$ return typeof args + ':' + args + ' of ' + arguments.length; $$ LANGUAGE pljs; SELECT va_named(7, 'eight'::text, 9); va_named --------------- number:7 of 3 (1 row) -- Declared arguments before the variadic ones. CREATE FUNCTION va_prefix(fmt text, VARIADIC "any") RETURNS text AS $$ var rest = Array.from(arguments).slice(1); return fmt + ': ' + rest.map(a => typeof a + ':' + a).join(', '); $$ LANGUAGE pljs; SELECT va_prefix('vals', 1, 'b'::text, 2.5::numeric); va_prefix -------------------------------------- vals: number:1, string:b, number:2.5 (1 row) -- Called with VARIADIC, the function gets the array as its one argument. SELECT va_show(VARIADIC ARRAY[1, 2, 3]); va_show ---------------- object:[1,2,3] (1 row) SELECT va_show(VARIADIC ARRAY['a', 'b']::text[]); va_show ------------------ object:["a","b"] (1 row) -- An OUT argument after the variadic one. CREATE FUNCTION va_out(VARIADIC "any", OUT n int, OUT last text) AS $$ return {n: arguments.length, last: String(arguments[arguments.length - 1])}; $$ LANGUAGE pljs; SELECT * FROM va_out(1, 'b'::text, 3); n | last ---+------ 3 | 3 (1 row) -- A set-returning function. CREATE FUNCTION va_srf(VARIADIC "any") RETURNS SETOF text AS $$ return Array.from(arguments).map(a => typeof a + ':' + a); $$ LANGUAGE pljs; SELECT * FROM va_srf(1, 'two'::text, false); va_srf --------------- number:1 string:two boolean:false (3 rows) -- A window function, through `arguments` and through the window object. CREATE TABLE va_w (n int, t text); INSERT INTO va_w VALUES (1, 'alpha'), (2, 'beta'); CREATE FUNCTION va_win(VARIADIC "any") RETURNS text AS $$ var w = pljs.get_window_object(); var args = Array.from(arguments).map(a => typeof a + ':' + a).join(','); return args + ' / ' + w.get_func_arg_current(2); $$ LANGUAGE pljs WINDOW; SELECT n, va_win(n, t, n * 10) OVER (ORDER BY n) FROM va_w ORDER BY n; n | va_win ---+-------------------------------------- 1 | number:1,string:alpha,number:10 / 10 2 | number:2,string:beta,number:20 / 20 (2 rows) -- Many arguments. SELECT va_named(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); va_named ---------------- number:1 of 40 (1 row) DROP FUNCTION va_show, va_named, va_prefix, va_out, va_srf, va_win; DROP TABLE va_w;