• Tom Lane's avatar
    Detoast plpgsql variables if they might live across a transaction boundary. · 2efc9241
    Tom Lane authored
    Up to now, it's been safe for plpgsql to store TOAST pointers in its
    variables because the ActiveSnapshot for whatever query called the plpgsql
    function will surely protect such TOAST values from being vacuumed away,
    even if the owning table rows are committed dead.  With the introduction of
    procedures, that assumption is no longer good in "non atomic" executions
    of plpgsql code.  We adopt the slightly brute-force solution of detoasting
    all TOAST pointers at the time they are stored into variables, if we're in
    a non-atomic context, just in case the owning row goes away.
    
    Some care is needed to avoid long-term memory leaks, since plpgsql tends
    to run with CurrentMemoryContext pointing to its call-lifespan context,
    but we shouldn't assume that no memory is leaked by heap_tuple_fetch_attr.
    In plpgsql proper, we can do the detoasting work in the "eval_mcontext".
    
    Most of the code thrashing here is due to the need to add this capability
    to expandedrecord.c as well as plpgsql proper.  In expandedrecord.c,
    we can't assume that the caller's context is short-lived, so make use of
    the short-term sub-context that was already invented for checking domain
    constraints.  In view of this repurposing, it seems good to rename that
    variable and associated code from "domain_check_cxt" to "short_term_cxt".
    
    Peter Eisentraut and Tom Lane
    
    Discussion: https://postgr.es/m/5AC06865.9050005@anastigmatix.net
    2efc9241
plpgsql-toast.out 2.87 KB