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