Avoid crash when rolling back within a prepared statement.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Feb 2021 00:38:29 +0000 (19:38 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Feb 2021 00:38:43 +0000 (19:38 -0500)
commit9624321ec502f4e4f4722290b358694049447f95
tree5a1df867400d60222e0b7e92845a06304b9e77d4
parent2c8726c4b0a496608919d1f78a5abc8c9b6e0868
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 1cff1b95a 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
src/backend/tcop/pquery.c