diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c index 7fe28ca15fa..1bd4c6d2fd5 100644 --- a/src/backend/utils/mmgr/portalmem.c +++ b/src/backend/utils/mmgr/portalmem.c @@ -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.106.2.3 2010/01/18 02:30:37 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.106.2.4 2010/07/05 09:27:31 heikki Exp $ * *------------------------------------------------------------------------- */ @@ -366,6 +366,28 @@ PortalCreateHoldStore(Portal portal) MemoryContextSwitchTo(oldcxt); } +/* + * PinPortal + * Protect a portal from dropping. + */ +void +PinPortal(Portal portal) +{ + if (portal->portalPinned) + elog(ERROR, "portal already pinned"); + + portal->portalPinned = true; +} + +void +UnpinPortal(Portal portal) +{ + if (!portal->portalPinned) + elog(ERROR, "portal not pinned"); + + portal->portalPinned = false; +} + /* * PortalDrop * Destroy the portal. @@ -375,9 +397,16 @@ PortalDrop(Portal portal, bool isTopCommit) { AssertArg(PortalIsValid(portal)); - /* Not sure if this case can validly happen or not... */ - if (portal->status == PORTAL_ACTIVE) - elog(ERROR, "cannot drop active portal"); + /* + * Don't allow dropping a pinned portal, it's still needed by whoever + * pinned it. Not sure if the PORTAL_ACTIVE case can validly happen or + * not... + */ + if (portal->portalPinned || + portal->status == PORTAL_ACTIVE) + ereport(ERROR, + (errcode(ERRCODE_INVALID_CURSOR_STATE), + errmsg("cannot drop active portal \"%s\"", portal->name))); /* * Remove portal from hash table. Because we do this first, we will not @@ -620,6 +649,13 @@ AtCommit_Portals(void) continue; } + /* + * There should be no pinned portals anymore. Complain if someone + * leaked one. + */ + if (portal->portalPinned) + elog(ERROR, "cannot commit while a portal is pinned"); + /* * Do nothing to cursors held over from a previous transaction * (including holdable ones just frozen by CommitHoldablePortals). @@ -718,7 +754,15 @@ AtCleanup_Portals(void) continue; } - /* Else zap it. */ + /* + * If a portal is still pinned, forcibly unpin it. PortalDrop will + * not let us drop the portal otherwise. Whoever pinned the portal + * was interrupted by the abort too and won't try to use it anymore. + */ + if (portal->portalPinned) + portal->portalPinned = false; + + /* Zap it. */ PortalDrop(portal, false); } } diff --git a/src/include/utils/portal.h b/src/include/utils/portal.h index a37c6ef1688..eecf7559768 100644 --- a/src/include/utils/portal.h +++ b/src/include/utils/portal.h @@ -39,7 +39,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/portal.h,v 1.78 2008/01/01 19:45:59 momjian Exp $ + * $PostgreSQL: pgsql/src/include/utils/portal.h,v 1.78.2.1 2010/07/05 09:27:31 heikki Exp $ * *------------------------------------------------------------------------- */ @@ -136,6 +136,7 @@ typedef struct PortalData /* Status data */ PortalStatus status; /* see above */ + bool portalPinned; /* a pinned portal can't be dropped */ /* If not NULL, Executor is active; call ExecutorEnd eventually: */ QueryDesc *queryDesc; /* info needed for executor invocation */ @@ -202,6 +203,8 @@ extern void AtSubAbort_Portals(SubTransactionId mySubid, extern void AtSubCleanup_Portals(SubTransactionId mySubid); extern Portal CreatePortal(const char *name, bool allowDup, bool dupSilent); extern Portal CreateNewPortal(void); +extern void PinPortal(Portal portal); +extern void UnpinPortal(Portal portal); extern void PortalDrop(Portal portal, bool isTopCommit); extern Portal GetPortalByName(const char *name); extern void PortalDefineQuery(Portal portal, diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 52996993fa9..648c5e64581 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.202.2.7 2010/02/12 19:37:52 tgl Exp $ + * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.202.2.8 2010/07/05 09:27:31 heikki Exp $ * *------------------------------------------------------------------------- */ @@ -1735,9 +1735,11 @@ exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt) /* * Open the implicit cursor for the statement and fetch the initial 10 - * rows. + * rows. Pin the portal to make sure it doesn't get closed by the user + * statements we execute. */ exec_run_select(estate, stmt->query, 0, &portal); + PinPortal(portal); SPI_cursor_fetch(portal, true, 10); tuptab = SPI_tuptable; @@ -1819,6 +1821,7 @@ exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt) * code should match the code after the loop.) */ SPI_freetuptable(tuptab); + UnpinPortal(portal); SPI_cursor_close(portal); exec_set_found(estate, found); @@ -1844,6 +1847,7 @@ exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt) /* * Close the implicit cursor */ + UnpinPortal(portal); SPI_cursor_close(portal); /* @@ -2890,6 +2894,12 @@ exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt) pfree(querystr); SPI_freeplan(plan); + /* + * Make sure the portal doesn't get closed by the user statements + * we execute. + */ + PinPortal(portal); + /* * Fetch the initial 10 tuples */ @@ -2973,6 +2983,7 @@ exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt) * code should match the code after the loop.) */ SPI_freetuptable(tuptab); + UnpinPortal(portal); SPI_cursor_close(portal); exec_set_found(estate, found); @@ -2998,6 +3009,7 @@ exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt) /* * Close the implicit cursor */ + UnpinPortal(portal); SPI_cursor_close(portal); /*