Hackers,
I’ve been happily using the array-to-element concatenation operator || to append a single value to an array, e.g,
SELECT array || 'foo';
And it works great, including in PL/pgSQL functions, except in an exception block. When I run this:
BEGIN;
CREATE OR REPLACE FUNCTION foo( ) RETURNS BOOLEAN IMMUTABLE LANGUAGE PLPGSQL AS $$ DECLARE things TEXT[]
:='{}'; BEGIN things := things || 'foo'; RAISE division_by_zero; EXCEPTION WHEN OTHERS THEN
things:= things || 'bar'; END; $$;
SELECT foo();
ROLLBACK;
The output is:
psql:array.sql:15: ERROR: malformed array literal: "bar" LINE 1: SELECT things || 'bar'
^ DETAIL: Array value must start with "{" or dimension information. QUERY: SELECT things || 'bar' CONTEXT:
PL/pgSQLfunction foo() line 8 at assignment
Note that it’s fine with the use of || outside the exception block, but not inside! I’ve worked around this by using
`things|| '{bar}'` instead, but it seems like a bug or perhaps unforeseen corner case that appending a value to an
arraydoesn’t work in an exception-handling block.
Best,
David