Skip to content

Commit 25ee160

Browse files
committed
More paranoia in AtEOSubXact_SPI: don't assume we can safely use SPI_finish
for cleaning up. It seems possible that the memory contexts SPI_finish would try to touch are already gone; and there's no need for SPI itself to delete them, since the containing contexts will surely be going away anyway at transaction end.
1 parent 1732cb0 commit 25ee160

File tree

1 file changed

+16
-5
lines changed
  • src/backend/executor

1 file changed

+16
-5
lines changed

src/backend/executor/spi.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.119 2004/07/01 00:50:26 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.120 2004/07/01 21:17:13 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -206,16 +206,27 @@ AtEOSubXact_SPI(bool isCommit, TransactionId childXid)
206206
while (_SPI_connected >= 0)
207207
{
208208
_SPI_connection *connection = &(_SPI_stack[_SPI_connected]);
209-
int res;
210209

211210
if (connection->connectXid != childXid)
212211
break; /* couldn't be any underneath it either */
213212

214213
found = true;
215214

216-
_SPI_curid = _SPI_connected - 1; /* avoid begin_call error */
217-
res = SPI_finish();
218-
Assert(res == SPI_OK_FINISH);
215+
/*
216+
* Pop the stack entry and reset global variables. Unlike
217+
* SPI_finish(), we don't risk switching to memory contexts that
218+
* might be already gone, or deleting memory contexts that have been
219+
* or will be thrown away anyway.
220+
*/
221+
_SPI_connected--;
222+
_SPI_curid = _SPI_connected;
223+
if (_SPI_connected == -1)
224+
_SPI_current = NULL;
225+
else
226+
_SPI_current = &(_SPI_stack[_SPI_connected]);
227+
SPI_processed = 0;
228+
SPI_lastoid = InvalidOid;
229+
SPI_tuptable = NULL;
219230
}
220231

221232
if (found && isCommit)

0 commit comments

Comments
 (0)