Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 77e760d

Browse files
committed
Avoid crash when rolling back within a prepared statement.
If a portal is used to run a prepared CALL or DO statement that contains a ROLLBACK, PortalRunMulti fails because the portal's statement list gets cleared by the rollback. (Since the grammar doesn't allow CALL/DO in PREPARE, the only easy way to get to this is via extended query protocol, which treats all inputs as prepared statements.) It's difficult to avoid resetting the portal early because of resource-management issues, so work around this by teaching PortalRunMulti to be wary of portal->stmts having suddenly become NIL. The crash has only been seen to occur in v13 and HEAD (as a consequence of commit 1cff1b9 having added an extra touch of portal->stmts). But even before that, the code involved touching a List that the portal no longer has any claim on. In the test case at hand, the List will still exist because of another refcount on the cached plan; but I'm far from convinced that it's impossible for the cached plan to have been dropped by the time control gets back to PortalRunMulti. Hence, backpatch to v11 where nested transactions were added. Thomas Munro and Tom Lane, per bug #16811 from James Inform Discussion: https://postgr.es/m/16811-c1b599b2c6c2d622@postgresql.org
1 parent 1dd6baf commit 77e760d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/backend/tcop/pquery.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,19 +1330,30 @@ PortalRunMulti(Portal portal,
13301330
}
13311331
}
13321332

1333-
/*
1334-
* Increment command counter between queries, but not after the last
1335-
* one.
1336-
*/
1337-
if (lnext(stmtlist_item) != NULL)
1338-
CommandCounterIncrement();
1339-
13401333
/*
13411334
* Clear subsidiary contexts to recover temporary memory.
13421335
*/
13431336
Assert(portal->portalContext == CurrentMemoryContext);
13441337

13451338
MemoryContextDeleteChildren(portal->portalContext);
1339+
1340+
/*
1341+
* Avoid crashing if portal->stmts has been reset. This can only
1342+
* occur if a CALL or DO utility statement executed an internal
1343+
* COMMIT/ROLLBACK (cf PortalReleaseCachedPlan). The CALL or DO must
1344+
* have been the only statement in the portal, so there's nothing left
1345+
* for us to do; but we don't want to dereference a now-dangling list
1346+
* pointer.
1347+
*/
1348+
if (portal->stmts == NIL)
1349+
break;
1350+
1351+
/*
1352+
* Increment command counter between queries, but not after the last
1353+
* one.
1354+
*/
1355+
if (lnext(stmtlist_item) != NULL)
1356+
CommandCounterIncrement();
13461357
}
13471358

13481359
/* Pop the snapshot if we pushed one. */

0 commit comments

Comments
 (0)