Malformed Array Literal in PL/pgSQL Exception Block

From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Malformed Array Literal in PL/pgSQL Exception Block
Date: 2017-04-10 04:22:46
Message-ID: F66306DC-B544-4177-9357-6B41017753E6@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/pgSQL function 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 array doesn’t work in an exception-handling block.

Best,

David

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2017-04-10 04:34:23 Re: SCRAM authentication, take three
Previous Message Tom Lane 2017-04-10 04:19:38 Re: recent deadlock regression test failures