pgsql: Fix plpgsql to reinitialize record variables at block re-entry. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix plpgsql to reinitialize record variables at block re-entry.
Date
Msg-id E1eNiWz-0002M7-M8@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix plpgsql to reinitialize record variables at block re-entry.

If one exits and re-enters a DECLARE ... BEGIN ... END block within a
single execution of a plpgsql function, perhaps due to a surrounding loop,
the declared variables are supposed to get re-initialized to null (or
whatever their initializer is).  But this failed to happen for variables
of type "record", because while exec_stmt_block() expected such variables
to be included in the block's initvarnos list, plpgsql_add_initdatums()
only adds DTYPE_VAR variables to that list.  This bug appears to have
been there since the aboriginal addition of plpgsql to our tree.

Fix by teaching plpgsql_add_initdatums() to include DTYPE_REC variables
as well.  (We don't need to consider other DTYPEs because they don't
represent separately-stored values.)  I failed to resist the temptation
to make some nearby cosmetic adjustments, too.

No back-patch, because there have not been field complaints, and it
seems possible that somewhere out there someone has code depending
on the incorrect behavior.  In any case this change would have no
impact on correctly-written code.

Discussion: https://postgr.es/m/22994.1512800671@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/390d58135b22bc25229b524a60f69682182201d8

Modified Files
--------------
src/pl/plpgsql/src/pl_comp.c          | 12 +++++++++---
src/pl/plpgsql/src/pl_exec.c          | 20 +++++++++-----------
src/pl/plpgsql/src/plpgsql.h          |  4 ++--
src/test/regress/expected/plpgsql.out | 27 +++++++++++++++++++++++++++
src/test/regress/sql/plpgsql.sql      | 21 +++++++++++++++++++++
5 files changed, 68 insertions(+), 16 deletions(-)


pgsql-committers by date:

Previous
From: Magnus Hagander
Date:
Subject: pgsql: Fix typo
Next
From: Tom Lane
Date:
Subject: pgsql: Stabilize output of new regression test case.