{
/* Use a random nonconflicting name */
portal = CreateNewPortal();
-
- /*
- * Make sure the portal doesn't get closed by the user statements we
- * execute.
- */
- PinPortal(portal);
}
else
{
if (!PortalIsValid(portal))
elog(ERROR, "invalid portal in SPI cursor operation");
- if (portal->portalPinned)
- UnpinPortal(portal);
-
PortalDrop(portal, false);
}
/* Fetch loop variable's datum entry */
var = (PLpgSQL_variable *) estate->datums[stmt->var->dno];
+ /*
+ * Make sure the portal doesn't get closed by the user statements we
+ * execute.
+ */
+ PinPortal(portal);
+
/*
* Fetch the initial tuple(s). If prefetching is allowed then we grab a
* few more rows to avoid multiple trips through executor startup
*/
SPI_freetuptable(tuptab);
+ UnpinPortal(portal);
+
/*
* Set the FOUND variable to indicate the result of executing the loop
* (namely, whether we looped one or more times). This must be set last so