]> granicus.if.org Git - postgresql/commitdiff
Fix subtransaction cleanup after an outer-subtransaction portal fails.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 4 Sep 2015 17:36:50 +0000 (13:36 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 4 Sep 2015 17:36:50 +0000 (13:36 -0400)
Formerly, we treated only portals created in the current subtransaction as
having failed during subtransaction abort.  However, if the error occurred
while running a portal created in an outer subtransaction (ie, a cursor
declared before the last savepoint), that has to be considered broken too.

To allow reliable detection of which ones those are, add a bookkeeping
field to struct Portal that tracks the innermost subtransaction in which
each portal has actually been executed.  (Without this, we'd end up
failing portals containing functions that had called the subtransaction,
thereby breaking plpgsql exception blocks completely.)

In addition, when we fail an outer-subtransaction Portal, transfer its
resources into the subtransaction's resource owner, so that they're
released early in cleanup of the subxact.  This fixes a problem reported by
Jim Nasby in which a function executed in an outer-subtransaction cursor
could cause an Assert failure or crash by referencing a relation created
within the inner subtransaction.

The proximate cause of the Assert failure is that AtEOSubXact_RelationCache
assumed it could blow away a relcache entry without first checking that the
entry had zero refcount.  That was a bad idea on its own terms, so add such
a check there, and to the similar coding in AtEOXact_RelationCache.  This
provides an independent safety measure in case there are still ways to
provoke the situation despite the Portal-level changes.

This has been broken since subtransactions were invented, so back-patch
to all supported branches.

Tom Lane and Michael Paquier

src/backend/access/transam/xact.c
src/backend/commands/portalcmds.c
src/backend/tcop/pquery.c
src/backend/utils/cache/relcache.c
src/backend/utils/mmgr/portalmem.c
src/include/utils/portal.h
src/test/regress/expected/transactions.out
src/test/regress/sql/transactions.sql

index c6182360b22ca76075ec0919171350153eea83df..be3155abdee6a233763fa9f0f33770d2c1345c77 100644 (file)
@@ -4101,6 +4101,7 @@ AbortSubTransaction(void)
                AfterTriggerEndSubXact(false);
                AtSubAbort_Portals(s->subTransactionId,
                                                   s->parent->subTransactionId,
+                                                  s->curTransactionOwner,
                                                   s->parent->curTransactionOwner);
                AtEOSubXact_LargeObject(false, s->subTransactionId,
                                                                s->parent->subTransactionId);
index db4b260134375da4be718d4bf715e9c533b384ea..93d7606b70c4fdd6d55eaae04ed86300f2334998 100644 (file)
@@ -329,6 +329,7 @@ PersistHoldablePortal(Portal portal)
                                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                 errmsg("portal \"%s\" cannot be run", portal->name)));
        portal->status = PORTAL_ACTIVE;
+       portal->activeSubid = GetCurrentSubTransactionId();
 
        /*
         * Set up global portal context pointers.
index 09135370c785f6aea00d216a1e8257d9f40427af..16cd11e4d0b7cd4c18c51885c933fdfb53e04aef 100644 (file)
@@ -748,6 +748,7 @@ PortalRun(Portal portal, long count, bool isTopLevel,
                                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                 errmsg("portal \"%s\" cannot be run", portal->name)));
        portal->status = PORTAL_ACTIVE;
+       portal->activeSubid = GetCurrentSubTransactionId();
 
        /*
         * Set up global portal context pointers.
@@ -1372,6 +1373,7 @@ PortalRunFetch(Portal portal,
                                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                 errmsg("portal \"%s\" cannot be run", portal->name)));
        portal->status = PORTAL_ACTIVE;
+       portal->activeSubid = GetCurrentSubTransactionId();
 
        /*
         * Set up global portal context pointers.
index 81595092f14248504e3a4abb5aa03e60dbdf48ba..dedb24d43f62dd266163e5fa65499d706ccf09b8 100644 (file)
@@ -1855,7 +1855,9 @@ RelationClearRelation(Relation relation, bool rebuild)
 {
        /*
         * As per notes above, a rel to be rebuilt MUST have refcnt > 0; while of
-        * course it would be a bad idea to blow away one with nonzero refcnt.
+        * course it would be an equally bad idea to blow away one with nonzero
+        * refcnt, since that would leave someone somewhere with a dangling
+        * pointer.  All callers are expected to have verified that this holds.
         */
        Assert(rebuild ?
                   !RelationHasReferenceCountZero(relation) :
@@ -2345,11 +2347,25 @@ AtEOXact_RelationCache(bool isCommit)
                {
                        if (isCommit)
                                relation->rd_createSubid = InvalidSubTransactionId;
-                       else
+                       else if (RelationHasReferenceCountZero(relation))
                        {
                                RelationClearRelation(relation, false);
                                continue;
                        }
+                       else
+                       {
+                               /*
+                                * Hmm, somewhere there's a (leaked?) reference to the
+                                * relation.  We daren't remove the entry for fear of
+                                * dereferencing a dangling pointer later.  Bleat, and mark it
+                                * as not belonging to the current transaction.  Hopefully
+                                * it'll get cleaned up eventually.  This must be just a
+                                * WARNING to avoid error-during-error-recovery loops.
+                                */
+                               relation->rd_createSubid = InvalidSubTransactionId;
+                               elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
+                                        RelationGetRelationName(relation));
+                       }
                }
 
                /*
@@ -2410,11 +2426,25 @@ AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
                {
                        if (isCommit)
                                relation->rd_createSubid = parentSubid;
-                       else
+                       else if (RelationHasReferenceCountZero(relation))
                        {
                                RelationClearRelation(relation, false);
                                continue;
                        }
+                       else
+                       {
+                               /*
+                                * Hmm, somewhere there's a (leaked?) reference to the
+                                * relation.  We daren't remove the entry for fear of
+                                * dereferencing a dangling pointer later.  Bleat, and
+                                * transfer it to the parent subtransaction so we can try
+                                * again later.  This must be just a WARNING to avoid
+                                * error-during-error-recovery loops.
+                                */
+                               relation->rd_createSubid = parentSubid;
+                               elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
+                                        RelationGetRelationName(relation));
+                       }
                }
 
                /*
index 66fe97913bc0c159e03b9e240cfb0c0a211a75f5..04c42aaa27a4b1a58a2d4f0ac18b1477345c7d91 100644 (file)
@@ -231,6 +231,7 @@ CreatePortal(const char *name, bool allowDup, bool dupSilent)
        portal->status = PORTAL_NEW;
        portal->cleanup = PortalCleanup;
        portal->createSubid = GetCurrentSubTransactionId();
+       portal->activeSubid = portal->createSubid;
        portal->strategy = PORTAL_MULTI_QUERY;
        portal->cursorOptions = CURSOR_OPT_NO_SCROLL;
        portal->atStart = true;
@@ -581,6 +582,7 @@ CommitHoldablePortals(void)
                         * not belonging to this transaction.
                         */
                        portal->createSubid = InvalidSubTransactionId;
+                       portal->activeSubid = InvalidSubTransactionId;
 
                        result = true;
                }
@@ -793,8 +795,8 @@ AtCleanup_Portals(void)
 /*
  * Pre-subcommit processing for portals.
  *
- * Reassign the portals created in the current subtransaction to the parent
- * subtransaction.
+ * Reassign portals created or used in the current subtransaction to the
+ * parent subtransaction.
  */
 void
 AtSubCommit_Portals(SubTransactionId mySubid,
@@ -816,14 +818,16 @@ AtSubCommit_Portals(SubTransactionId mySubid,
                        if (portal->resowner)
                                ResourceOwnerNewParent(portal->resowner, parentXactOwner);
                }
+               if (portal->activeSubid == mySubid)
+                       portal->activeSubid = parentSubid;
        }
 }
 
 /*
  * Subtransaction abort handling for portals.
  *
- * Deactivate portals created during the failed subtransaction.
- * Note that per AtSubCommit_Portals, this will catch portals created
+ * Deactivate portals created or used during the failed subtransaction.
+ * Note that per AtSubCommit_Portals, this will catch portals created/used
  * in descendants of the subtransaction too.
  *
  * We don't destroy any portals here; that's done in AtSubCleanup_Portals.
@@ -831,6 +835,7 @@ AtSubCommit_Portals(SubTransactionId mySubid,
 void
 AtSubAbort_Portals(SubTransactionId mySubid,
                                   SubTransactionId parentSubid,
+                                  ResourceOwner myXactOwner,
                                   ResourceOwner parentXactOwner)
 {
        HASH_SEQ_STATUS status;
@@ -842,16 +847,66 @@ AtSubAbort_Portals(SubTransactionId mySubid,
        {
                Portal          portal = hentry->portal;
 
+               /* Was it created in this subtransaction? */
                if (portal->createSubid != mySubid)
+               {
+                       /* No, but maybe it was used in this subtransaction? */
+                       if (portal->activeSubid == mySubid)
+                       {
+                               /* Maintain activeSubid until the portal is removed */
+                               portal->activeSubid = parentSubid;
+
+                               /*
+                                * Upper-level portals that failed while running in this
+                                * subtransaction must be forced into FAILED state, for the
+                                * same reasons discussed below.
+                                *
+                                * We assume we can get away without forcing upper-level READY
+                                * portals to fail, even if they were run and then suspended.
+                                * In theory a suspended upper-level portal could have
+                                * acquired some references to objects that are about to be
+                                * destroyed, but there should be sufficient defenses against
+                                * such cases: the portal's original query cannot contain such
+                                * references, and any references within, say, cached plans of
+                                * PL/pgSQL functions are not from active queries and should
+                                * be protected by revalidation logic.
+                                */
+                               if (portal->status == PORTAL_ACTIVE)
+                               {
+                                       portal->status = PORTAL_FAILED;
+                                       /* let portalcmds.c clean up the state it knows about */
+                                       if (PointerIsValid(portal->cleanup))
+                                       {
+                                               (*portal->cleanup) (portal);
+                                               portal->cleanup = NULL;
+                                       }
+                               }
+
+                               /*
+                                * Also, if we failed it during the current subtransaction
+                                * (either just above, or earlier), reattach its resource
+                                * owner to the current subtransaction's resource owner, so
+                                * that any resources it still holds will be released while
+                                * cleaning up this subtransaction.  This prevents some corner
+                                * cases wherein we might get Asserts or worse while cleaning
+                                * up objects created during the current subtransaction
+                                * (because they're still referenced within this portal).
+                                */
+                               if (portal->status == PORTAL_FAILED && portal->resowner)
+                               {
+                                       ResourceOwnerNewParent(portal->resowner, myXactOwner);
+                                       portal->resowner = NULL;
+                               }
+                       }
+                       /* Done if it wasn't created in this subtransaction */
                        continue;
+               }
 
                /*
                 * Force any live portals of my own subtransaction into FAILED state.
                 * We have to do this because they might refer to objects created or
-                * changed in the failed subtransaction, leading to crashes if
-                * execution is resumed, or even if we just try to run ExecutorEnd.
-                * (Note we do NOT do this to upper-level portals, since they cannot
-                * have such references and hence may be able to continue.)
+                * changed in the failed subtransaction, leading to crashes within
+                * ExecutorEnd when portalcmds.c tries to close down the portal.
                 */
                if (portal->status == PORTAL_READY ||
                        portal->status == PORTAL_ACTIVE)
index 42df8546d92b7b04e75cd77134ddeda0c6cbf408..f926638a8af182d1ac91cc95a46c55773cd6982e 100644 (file)
@@ -112,12 +112,15 @@ typedef struct PortalData
        MemoryContext heap;                     /* subsidiary memory for portal */
        ResourceOwner resowner;         /* resources owned by portal */
        void            (*cleanup) (Portal portal);             /* cleanup hook */
-       SubTransactionId createSubid;           /* the ID of the creating subxact */
 
        /*
-        * if createSubid is InvalidSubTransactionId, the portal is held over from
-        * a previous transaction
+        * State data for remembering which subtransaction(s) the portal was
+        * created or used in.  If the portal is held over from a previous
+        * transaction, both subxids are InvalidSubTransactionId.  Otherwise,
+        * createSubid is the creating subxact and activeSubid is the last subxact
+        * in which we ran the portal.
         */
+       SubTransactionId createSubid;           /* the creating subxact */
 
        /* The query or queries the portal will execute */
        const char *sourceText;         /* text of query (as of 8.4, never NULL) */
@@ -168,6 +171,13 @@ typedef struct PortalData
        /* Presentation data, primarily used by the pg_cursors system view */
        TimestampTz creation_time;      /* time at which this portal was defined */
        bool            visible;                /* include this portal in pg_cursors? */
+
+       /*
+        * This field belongs with createSubid, but in pre-9.5 branches, add it
+        * at the end to avoid creating an ABI break for extensions that examine
+        * Portal structs.
+        */
+       SubTransactionId activeSubid;           /* the last subxact with activity */
 } PortalData;
 
 /*
@@ -196,6 +206,7 @@ extern void AtSubCommit_Portals(SubTransactionId mySubid,
                                        ResourceOwner parentXactOwner);
 extern void AtSubAbort_Portals(SubTransactionId mySubid,
                                   SubTransactionId parentSubid,
+                                  ResourceOwner myXactOwner,
                                   ResourceOwner parentXactOwner);
 extern void AtSubCleanup_Portals(SubTransactionId mySubid);
 extern Portal CreatePortal(const char *name, bool allowDup, bool dupSilent);
index c4f8965fd1e27b07af41a11371cf1981268374ca..90fd2a5decb9375db2b8b94d7e68a6cc7a91237e 100644 (file)
@@ -527,6 +527,52 @@ fetch from foo;
 (1 row)
 
 abort;
+-- Test for proper cleanup after a failure in a cursor portal
+-- that was created in an outer subtransaction
+CREATE FUNCTION invert(x float8) RETURNS float8 LANGUAGE plpgsql AS
+$$ begin return 1/x; end $$;
+CREATE FUNCTION create_temp_tab() RETURNS text
+LANGUAGE plpgsql AS $$
+BEGIN
+  CREATE TEMP TABLE new_table (f1 float8);
+  -- case of interest is that we fail while holding an open
+  -- relcache reference to new_table
+  INSERT INTO new_table SELECT invert(0.0);
+  RETURN 'foo';
+END $$;
+BEGIN;
+DECLARE ok CURSOR FOR SELECT * FROM int8_tbl;
+DECLARE ctt CURSOR FOR SELECT create_temp_tab();
+FETCH ok;
+ q1  | q2  
+-----+-----
+ 123 | 456
+(1 row)
+
+SAVEPOINT s1;
+FETCH ok;  -- should work
+ q1  |        q2        
+-----+------------------
+ 123 | 4567890123456789
+(1 row)
+
+FETCH ctt; -- error occurs here
+ERROR:  division by zero
+CONTEXT:  PL/pgSQL function "invert" line 1 at RETURN
+SQL statement "INSERT INTO new_table SELECT invert(0.0)"
+PL/pgSQL function "create_temp_tab" line 5 at SQL statement
+ROLLBACK TO s1;
+FETCH ok;  -- should work
+        q1        | q2  
+------------------+-----
+ 4567890123456789 | 123
+(1 row)
+
+FETCH ctt; -- must be rejected
+ERROR:  portal "ctt" cannot be run
+COMMIT;
+DROP FUNCTION create_temp_tab();
+DROP FUNCTION invert(x float8);
 -- tests for the "tid" type
 SELECT '(3, 3)'::tid = '(3, 4)'::tid;
  ?column? 
index c670ae18d0cc9dc1b85f95a47838e69117c74545..12aa69065930214afa4af84f0ac822ef23db5b37 100644 (file)
@@ -326,6 +326,38 @@ fetch from foo;
 
 abort;
 
+
+-- Test for proper cleanup after a failure in a cursor portal
+-- that was created in an outer subtransaction
+CREATE FUNCTION invert(x float8) RETURNS float8 LANGUAGE plpgsql AS
+$$ begin return 1/x; end $$;
+
+CREATE FUNCTION create_temp_tab() RETURNS text
+LANGUAGE plpgsql AS $$
+BEGIN
+  CREATE TEMP TABLE new_table (f1 float8);
+  -- case of interest is that we fail while holding an open
+  -- relcache reference to new_table
+  INSERT INTO new_table SELECT invert(0.0);
+  RETURN 'foo';
+END $$;
+
+BEGIN;
+DECLARE ok CURSOR FOR SELECT * FROM int8_tbl;
+DECLARE ctt CURSOR FOR SELECT create_temp_tab();
+FETCH ok;
+SAVEPOINT s1;
+FETCH ok;  -- should work
+FETCH ctt; -- error occurs here
+ROLLBACK TO s1;
+FETCH ok;  -- should work
+FETCH ctt; -- must be rejected
+COMMIT;
+
+DROP FUNCTION create_temp_tab();
+DROP FUNCTION invert(x float8);
+
+
 -- tests for the "tid" type
 SELECT '(3, 3)'::tid = '(3, 4)'::tid;
 SELECT '(3, 3)'::tid = '(3, 3)'::tid;