From: mariusz <marius@mtvk.pl>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: strange construct with RETURN within plpgsql
Date: Fri, 16 Feb 2018 13:31:06 +0100
Message-ID: <1518784266.10667.82.camel@mtvk.pl> (raw)
hello all,
i just noticed some strange thing in plpgsql, that is keyword RETURN is
allowed as noop after a valid statement.
shame on me, after so many years of using plpgsql i happened to write a
bug omitting semicolon after statement just before RETURN, and so i
found that "special"? construct.
this may be a parser oversight or something, but how could it be that i
am the only person that forgot semicolon at least once?
this may be somewhat dangerous within function with OUT parameters
allowing RETURN statement without expression, as in example below
(RETURN with expression produces syntax error when creating function).
such a bug with missing semicolon is easy to spot and fix, but
nonetheless is possible to create when parser allows something like
this.
so, if there is a reason for such a construct and it does something i
didn't notice, please let me know what is the purpose of keyword RETURN
after a valid statement.
let me show very simplified example (not the real life one i was working
on, datatype doesn't matter here). of course it could be even simpler,
but intensionally i put early return within conditional block to show
where the danger lies
version 9.6.3, if that matters
CREATE OR REPLACE FUNCTION
testret(a bool, OUT ret bool)
RETURNS bool
LANGUAGE plpgsql
AS
$$
begin
RAISE NOTICE 'testret(%)',a;
IF a IS NULL
THEN
ret := TRUE
RETURN;
RAISE NOTICE 'testret after return';
END IF;
IF TRUE RETURN
THEN
RAISE NOTICE 'return allowed here too';
END IF;
RETURN;
end;
$$;
# select testret(null);
NOTICE: testret(<NULL>)
NOTICE: testret after return
NOTICE: return allowed here too
testret
---------
t
(1 row)
as you can see, statements with RETURN are allowed, and RETURN keyword
does nothing in them, like these:
SELECT INTO ret TRUE RETURN;
ret := TRUE RETURN;
IF TRUE RETURN THEN ... END IF;
the last one seems most curious to me, but is relatively harmless (just
hurts one's eyes)
regards,
mariusz jadczak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: marius@mtvk.pl
Subject: Re: strange construct with RETURN within plpgsql
In-Reply-To: <1518784266.10667.82.camel@mtvk.pl>
References: <1518784266.10667.82.camel@mtvk.pl>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox