Skip to content

Commit cf0f08e

Browse files
committed
Only install a portal's ResourceOwner if it actually has one.
In most scenarios a portal without a ResourceOwner is dead and not subject to any further execution, but a portal for a cursor WITH HOLD remains in existence with no ResourceOwner after the creating transaction is over. In this situation, if we attempt to "execute" the portal directly to fetch data from it, we were setting CurrentResourceOwner to NULL, leading to a segfault if the datatype output code did anything that required a resource owner (such as trying to fetch system catalog entries that weren't already cached). The case appears to be impossible to provoke with stock libpq, but psqlODBC at least is able to cause it when working with held cursors. Simplest fix is to just skip the assignment to CurrentResourceOwner, so that any resources used by the data output operations will be managed by the transaction-level resource owner instead. For consistency I changed all the places that install a portal's resowner as current, even though some of them are probably not reachable with a held cursor's portal. Per report from Joshua Berry (with thanks to Hiroshi Inoue for developing a self-contained test case). Back-patch to all supported versions.
1 parent e19c932 commit cf0f08e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/backend/commands/portalcmds.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ PortalCleanup(Portal portal)
274274
saveResourceOwner = CurrentResourceOwner;
275275
PG_TRY();
276276
{
277-
CurrentResourceOwner = portal->resowner;
277+
if (portal->resowner)
278+
CurrentResourceOwner = portal->resowner;
278279
ExecutorFinish(queryDesc);
279280
ExecutorEnd(queryDesc);
280281
FreeQueryDesc(queryDesc);
@@ -349,7 +350,8 @@ PersistHoldablePortal(Portal portal)
349350
PG_TRY();
350351
{
351352
ActivePortal = portal;
352-
CurrentResourceOwner = portal->resowner;
353+
if (portal->resowner)
354+
CurrentResourceOwner = portal->resowner;
353355
PortalContext = PortalGetHeapMemory(portal);
354356

355357
MemoryContextSwitchTo(PortalContext);

src/backend/tcop/pquery.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
478478
PG_TRY();
479479
{
480480
ActivePortal = portal;
481-
CurrentResourceOwner = portal->resowner;
481+
if (portal->resowner)
482+
CurrentResourceOwner = portal->resowner;
482483
PortalContext = PortalGetHeapMemory(portal);
483484

484485
oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
@@ -761,7 +762,8 @@ PortalRun(Portal portal, long count, bool isTopLevel,
761762
PG_TRY();
762763
{
763764
ActivePortal = portal;
764-
CurrentResourceOwner = portal->resowner;
765+
if (portal->resowner)
766+
CurrentResourceOwner = portal->resowner;
765767
PortalContext = PortalGetHeapMemory(portal);
766768

767769
MemoryContextSwitchTo(PortalContext);
@@ -1410,7 +1412,8 @@ PortalRunFetch(Portal portal,
14101412
PG_TRY();
14111413
{
14121414
ActivePortal = portal;
1413-
CurrentResourceOwner = portal->resowner;
1415+
if (portal->resowner)
1416+
CurrentResourceOwner = portal->resowner;
14141417
PortalContext = PortalGetHeapMemory(portal);
14151418

14161419
oldContext = MemoryContextSwitchTo(PortalContext);

0 commit comments

Comments
 (0)