Skip to content

Commit 376c620

Browse files
committed
Fix portalmem.c to avoid keeping a dangling pointer to a cached plan list
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.
1 parent 04ef404 commit 376c620

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/backend/utils/mmgr/portalmem.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.115 2010/01/02 16:57:58 momjian Exp $
15+
* $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.116 2010/01/18 02:30:25 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -328,6 +328,13 @@ PortalReleaseCachedPlan(Portal portal)
328328
{
329329
ReleaseCachedPlan(portal->cplan, false);
330330
portal->cplan = NULL;
331+
332+
/*
333+
* We must also clear portal->stmts which is now a dangling
334+
* reference to the cached plan's plan list. This protects any
335+
* code that might try to examine the Portal later.
336+
*/
337+
portal->stmts = NIL;
331338
}
332339
}
333340

@@ -395,8 +402,7 @@ PortalDrop(Portal portal, bool isTopCommit)
395402
(*portal->cleanup) (portal);
396403

397404
/* drop cached plan reference, if any */
398-
if (portal->cplan)
399-
PortalReleaseCachedPlan(portal);
405+
PortalReleaseCachedPlan(portal);
400406

401407
/*
402408
* Release any resources still attached to the portal. There are several
@@ -529,8 +535,7 @@ CommitHoldablePortals(void)
529535
PersistHoldablePortal(portal);
530536

531537
/* drop cached plan reference, if any */
532-
if (portal->cplan)
533-
PortalReleaseCachedPlan(portal);
538+
PortalReleaseCachedPlan(portal);
534539

535540
/*
536541
* Any resources belonging to the portal will be released in the
@@ -680,8 +685,7 @@ AtAbort_Portals(void)
680685
}
681686

682687
/* drop cached plan reference, if any */
683-
if (portal->cplan)
684-
PortalReleaseCachedPlan(portal);
688+
PortalReleaseCachedPlan(portal);
685689

686690
/*
687691
* Any resources belonging to the portal will be released in the
@@ -823,8 +827,7 @@ AtSubAbort_Portals(SubTransactionId mySubid,
823827
}
824828

825829
/* drop cached plan reference, if any */
826-
if (portal->cplan)
827-
PortalReleaseCachedPlan(portal);
830+
PortalReleaseCachedPlan(portal);
828831

829832
/*
830833
* Any resources belonging to the portal will be released in the

0 commit comments

Comments
 (0)