]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix portalmem.c to avoid keeping a dangling pointer to a cached plan list
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 18 Jan 2010 02:30:30 +0000 (02:30 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 18 Jan 2010 02:30:30 +0000 (02:30 +0000)
after it's released its reference count for the cached plan.  There are
code paths that might try to examine the plan list before noticing that
the portal is already in aborted state.  Report and diagnosis by Tatsuo
Ishii, though this isn't exactly his proposed patch.

src/backend/utils/mmgr/portalmem.c

index a8ee35f08a7dfbf71cce43eed60774885435728c..501ffb1635cfecff002a29fb2ceb141f4fe4b140 100644 (file)
@@ -12,7 +12,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.113.2.1 2009/12/29 17:41:09 heikki Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.113.2.2 2010/01/18 02:30:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -328,6 +328,13 @@ PortalReleaseCachedPlan(Portal portal)
        {
                ReleaseCachedPlan(portal->cplan, false);
                portal->cplan = NULL;
+
+               /*
+                * We must also clear portal->stmts which is now a dangling
+                * reference to the cached plan's plan list.  This protects any
+                * code that might try to examine the Portal later.
+                */
+               portal->stmts = NIL;
        }
 }
 
@@ -395,8 +402,7 @@ PortalDrop(Portal portal, bool isTopCommit)
                (*portal->cleanup) (portal);
 
        /* drop cached plan reference, if any */
-       if (portal->cplan)
-               PortalReleaseCachedPlan(portal);
+       PortalReleaseCachedPlan(portal);
 
        /*
         * Release any resources still attached to the portal.  There are several
@@ -529,8 +535,7 @@ CommitHoldablePortals(void)
                        PersistHoldablePortal(portal);
 
                        /* drop cached plan reference, if any */
-                       if (portal->cplan)
-                               PortalReleaseCachedPlan(portal);
+                       PortalReleaseCachedPlan(portal);
 
                        /*
                         * Any resources belonging to the portal will be released in the
@@ -680,8 +685,7 @@ AtAbort_Portals(void)
                }
 
                /* drop cached plan reference, if any */
-               if (portal->cplan)
-                       PortalReleaseCachedPlan(portal);
+               PortalReleaseCachedPlan(portal);
 
                /*
                 * Any resources belonging to the portal will be released in the
@@ -823,8 +827,7 @@ AtSubAbort_Portals(SubTransactionId mySubid,
                        }
 
                        /* drop cached plan reference, if any */
-                       if (portal->cplan)
-                               PortalReleaseCachedPlan(portal);
+                       PortalReleaseCachedPlan(portal);
 
                        /*
                         * Any resources belonging to the portal will be released in the