'postgresql function with list of arrays as input

I want a postgresql function that will take a list of arrays, and loop through them.

I have written a "hello world" type function that takes a single array and prints it:

CREATE OR REPLACE FUNCTION print_array(_array varchar[]) RETURNS VOID 
AS $$
BEGIN
    RAISE NOTICE '%', _array;
END;
$$ LANGUAGE plpgsql;

which I can call: SELECT print_array('{a,b,c}');

output:

{a,b,c}

How would I extend this function to take a list of n arrays?

Expected input: SELECT print_arrays('{a,b,c}', '{foo,bar,baz}');

Expected output:

{a,b,c}

{foo,bar,baz}

I tried following the examples listed in the docs: https://www.postgresql.org/docs/9.3/plpgsql-control-structures.html#:~:text=40.6.5.%20Looping%20Through%20Arrays But I don't know how to define a function with a list of arrays as the input.



Solution 1:[1]

This worked using VARIADIC:

CREATE OR REPLACE FUNCTION print_arrays(VARIADIC _arrays varchar[]) RETURNS VOID 
AS $$
DECLARE
    arr text;
BEGIN
    FOREACH arr IN ARRAY _arrays
    LOOP
        RAISE NOTICE '%', arr;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan