strange construct with RETURN within plpgsql

Поиск
Список
Период
Сортировка
От mariusz
Тема strange construct with RETURN within plpgsql
Дата
Msg-id 1518784266.10667.82.camel@mtvk.pl
обсуждение исходный текст
Ответы Re: strange construct with RETURN within plpgsql  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: strange construct with RETURN within plpgsql  (Pavel Stehule <pavel.stehule@gmail.com>)
Список pgsql-general
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




В списке pgsql-general по дате отправления:

Предыдущее
От: Thiemo Kellner
Дата:
Сообщение: Dynamic PL/pgSQL select query: value association propblem
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: strange construct with RETURN within plpgsql