Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Ensure that plpgsql cleans up cleanly during parallel-worker exit.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Mar 2020 22:06:55 +0000 (18:06 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Mar 2020 22:06:55 +0000 (18:06 -0400)
plpgsql_xact_cb ought to treat events XACT_EVENT_PARALLEL_COMMIT and
XACT_EVENT_PARALLEL_ABORT like XACT_EVENT_COMMIT and XACT_EVENT_ABORT
respectively, since its goal is to do process-local cleanup.  This
oversight caused plpgsql's end-of-transaction cleanup to not get done
in parallel workers.  Since a parallel worker will exit just after the
transaction cleanup, the effects of this are limited.  I couldn't find
any case in the core code with user-visible effects, but perhaps there
are some in extensions.  In any case it's wrong, so let's fix it before
it bites us not after.

In passing, add some comments around the handling of expression
evaluation resources in DO blocks.  There's no live bug there, but it's
quite unobvious what's happening; at least I thought so.  This isn't
related to the other issue, except that I found both things while poking
at expression-evaluation performance.

Back-patch the plpgsql_xact_cb fix to 9.5 where those event types
were introduced, and the DO-block commentary to v11 where DO blocks
gained the ability to issue COMMIT/ROLLBACK.

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

src/pl/plpgsql/src/pl_exec.c

index db994a6cdf67f29834aa9f658c2fb838d3f14844..86ec26afa5473450acb1f2e06d5dac3306c0ba6e 100644 (file)
@@ -6918,7 +6918,9 @@ plpgsql_xact_cb(XactEvent event, void *arg)
     * expect the regular abort recovery procedures to release everything of
     * interest.
     */
-   if (event == XACT_EVENT_COMMIT || event == XACT_EVENT_PREPARE)
+   if (event == XACT_EVENT_COMMIT ||
+       event == XACT_EVENT_PARALLEL_COMMIT ||
+       event == XACT_EVENT_PREPARE)
    {
        /* Shouldn't be any econtext stack entries left at commit */
        Assert(simple_econtext_stack == NULL);
@@ -6927,7 +6929,8 @@ plpgsql_xact_cb(XactEvent event, void *arg)
            FreeExecutorState(shared_simple_eval_estate);
        shared_simple_eval_estate = NULL;
    }
-   else if (event == XACT_EVENT_ABORT)
+   else if (event == XACT_EVENT_ABORT ||
+            event == XACT_EVENT_PARALLEL_ABORT)
    {
        simple_econtext_stack = NULL;
        shared_simple_eval_estate = NULL;