Re: BUG #18403: PL/pgSQL is reporting unexpected errors when processing DECLARE blocks with <

From Tom Lane
Subject Re: BUG #18403: PL/pgSQL is reporting unexpected errors when processing DECLARE blocks with <
Date
Msg-id 103434.1710968526@sss.pgh.pa.us
Whole thread Raw
In response to BUG #18403: PL/pgSQL is reporting unexpected errors when processing DECLARE blocks with <  (PG Bug reporting form <noreply@postgresql.org>)
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> I have approximated the program to a clean situation. When I execute this
> program, the PL/pgSQL engine does not report errors to me:

> DO $$
> DECLARE
> DECLARE
>     var1 INT = 1;
> BEGIN
>     RAISE NOTICE '%', var1;
> END;
> $$;

That is not valid code according to the documentation, which clearly
says that every DECLARE must be followed by BEGIN and then END [1].
You happen to get away with it because of an undocumented "feature":
one of the options for a declaration statement is

                | K_DECLARE
                    {
                        /* We allow useless extra DECLAREs */
                    }

So the first DECLARE begins the block, and the second is an
ignored noise word.

> DO $$
> DECLARE
> <<label>>
> DECLARE
>     var1 INT = 1;
> BEGIN
>     RAISE NOTICE '%', var1;
> END;
> $$;

This, however, is flat wrong, and the error message seems
perfectly on-point to me:

> ERROR:  block label must be placed before DECLARE, not after
> LINE 3: <<label>>
>         ^

            regards, tom lane

[1] https://www.postgresql.org/docs/current/plpgsql-structure.html



pgsql-bugs by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: BUG #18403: PL/pgSQL is reporting unexpected errors when processing DECLARE blocks with <
Next
From: Thomas Munro
Date:
Subject: Re: Regression tests fail with musl libc because libpq.so can't be loaded