Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/pgSQL "Malformed array literal" error within for loop

I have the following pl/pgsql function. (Obviously, this is not the full function, it's just the minimal amount of code needed to reproduce the problem)

CREATE OR REPLACE FUNCTION test_func(infos TEXT[][])
RETURNS void AS
$$
DECLARE
    info TEXT[];
    type TEXT[];
    name TEXT;
BEGIN
    FOREACH info SLICE 1 IN ARRAY infos LOOP
        RAISE NOTICE 'Stuff: %', info;
        type := info[1];
        name := info[2];
        RAISE NOTICE 'Done with stuff';
    END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;

When I run the function using SELECT test_func('{{something, things},{other, data}}'::TEXT[][]);, I get the following output:

NOTICE:  Stuff: {something,things}
ERROR:  malformed array literal: "something"
DETAIL:  Array value must start with "{" or dimension information.
CONTEXT:  PL/pgSQL function test_func(text[]) line 10 at assignment

I don't understand how this error is happening. When the value of info is printed, it shows {something,things}, which seems to me to be a proper array literal.

I am using PostgreSQL version 9.4.7, in case it matters.

like image 468
ItsTimmy Avatar asked Apr 16 '26 06:04

ItsTimmy


1 Answers

The variable type should be text (not text[]):

CREATE OR REPLACE FUNCTION test_func(infos TEXT[][])
RETURNS void AS
$$
DECLARE
    info TEXT[];
    type TEXT;
    name TEXT;
...
like image 93
klin Avatar answered Apr 18 '26 02:04

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!