Re: BUG #13708: strange behaviour instead of syntax error - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #13708: strange behaviour instead of syntax error
Date
Msg-id 61779.1445637825@sss.pgh.pa.us
Whole thread Raw
In response to BUG #13708: strange behaviour instead of syntax error  (yozh.ne@gmail.com)
Responses Re: BUG #13708: strange behaviour instead of syntax error  (Greg Stark <stark@mit.edu>)
List pgsql-bugs
yozh.ne@gmail.com writes:
> In the code below - if I remove semicolon after "po_result := 'ERROR: No
> uncollected prize found!'" then no syntax error occurs, return statement is
> just ignored instead

> IF NOT FOUND THEN
>     po_result := 'ERROR: No uncollected prize found!'
>     RETURN;
> END IF;

Hm, yeah.  The problem is that plpgsql doesn't have any native
intelligence about what expressions can contain.  It just takes
everything between ":=" and ";" and hands that to the main parser
with a SELECT in front, ie what you have here is

    SELECT 'ERROR: No uncollected prize found!' RETURN;

which is legal syntax -- the RETURN is an AS-less column label.

I'm not sure about a reasonably low-effort way to fix this (and,
given the lack of previous complaints, I doubt it's worth a lot
of work).  I experimented with forcibly adding an AS clause,
so that what would get parsed is

    SELECT 'ERROR: No uncollected prize found!' RETURN AS collabel;

but the error message that the main parser would produce is along the
line of "ERROR: syntax error at or near "AS"", which would be quite
opaque from the user's viewpoint since there is no AS in the text.

Hmmm ... maybe just adding parens would do the trick.

regression=# SELECT ('ERROR: No uncollected prize found!' RETURN);
ERROR:  syntax error at or near "RETURN"

That looks more promising ... though I'm not sure if there are
any cases it would break.

            regards, tom lane

pgsql-bugs by date:

Previous
From: yozh.ne@gmail.com
Date:
Subject: BUG #13708: strange behaviour instead of syntax error
Next
From: Michael Paquier
Date:
Subject: Re: BUG #13657: Some kind of undetected deadlock between query and "startup process" on replica.