-- 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]); SELECT va_show(1); SELECT va_show(NULL::int, 'x'::text, NULL::text); -- 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); -- 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); -- Called with VARIADIC, the function gets the array as its one argument. SELECT va_show(VARIADIC ARRAY[1, 2, 3]); SELECT va_show(VARIADIC ARRAY['a', 'b']::text[]); -- 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); -- 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); -- 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; -- 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); DROP FUNCTION va_show, va_named, va_prefix, va_out, va_srf, va_win; DROP TABLE va_w;