• Tom Lane's avatar
    Fix plpgsql to reinitialize record variables at block re-entry. · 390d5813
    Tom Lane authored
    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
    390d5813
plpgsql.out 176 KB