Skip to content

Commit 4228bde

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 6803921 commit 4228bde

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
@@ -263,7 +263,8 @@ PortalCleanup(Portal portal)
263263
saveResourceOwner = CurrentResourceOwner;
264264
PG_TRY();
265265
{
266-
CurrentResourceOwner = portal->resowner;
266+
if (portal->resowner)
267+
CurrentResourceOwner = portal->resowner;
267268
/* we do not need AfterTriggerEndQuery() here */
268269
ExecutorEnd(queryDesc);
269270
FreeQueryDesc(queryDesc);
@@ -338,7 +339,8 @@ PersistHoldablePortal(Portal portal)
338339
PG_TRY();
339340
{
340341
ActivePortal = portal;
341-
CurrentResourceOwner = portal->resowner;
342+
if (portal->resowner)
343+
CurrentResourceOwner = portal->resowner;
342344
PortalContext = PortalGetHeapMemory(portal);
343345

344346
MemoryContextSwitchTo(PortalContext);

src/backend/tcop/pquery.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
481481
PG_TRY();
482482
{
483483
ActivePortal = portal;
484-
CurrentResourceOwner = portal->resowner;
484+
if (portal->resowner)
485+
CurrentResourceOwner = portal->resowner;
485486
PortalContext = PortalGetHeapMemory(portal);
486487

487488
oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
@@ -771,7 +772,8 @@ PortalRun(Portal portal, long count, bool isTopLevel,
771772
PG_TRY();
772773
{
773774
ActivePortal = portal;
774-
CurrentResourceOwner = portal->resowner;
775+
if (portal->resowner)
776+
CurrentResourceOwner = portal->resowner;
775777
PortalContext = PortalGetHeapMemory(portal);
776778

777779
MemoryContextSwitchTo(PortalContext);
@@ -1380,7 +1382,8 @@ PortalRunFetch(Portal portal,
13801382
PG_TRY();
13811383
{
13821384
ActivePortal = portal;
1383-
CurrentResourceOwner = portal->resowner;
1385+
if (portal->resowner)
1386+
CurrentResourceOwner = portal->resowner;
13841387
PortalContext = PortalGetHeapMemory(portal);
13851388

13861389
oldContext = MemoryContextSwitchTo(PortalContext);

0 commit comments

Comments
 (0)