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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: jiangshan(dot)liu(at)tju(dot)edu(dot)cn
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18403: PL/pgSQL is reporting unexpected errors when processing DECLARE blocks with <<label>>
Date: 2024-03-20 21:02:06
Message-ID: 103434.1710968526@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Thomas Munro 2024-03-20 22:26:26 Re: Regression tests fail with musl libc because libpq.so can't be loaded
Previous Message Pavel Stehule 2024-03-20 20:57:06 Re: BUG #18403: PL/pgSQL is reporting unexpected errors when processing DECLARE blocks with <<label>>