Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit30c66e7

Browse files
committed
Fix SPI error cleanup and memory leak
Since the SPI stack has been moved from TopTransactionContext toTopMemoryContext, setting _SPI_stack to NULL in AtEOXact_SPI() leaksmemory. In fact, we don't need to do that anymore: We just leave theallocated stack around for the next SPI use.Also, refactor the SPI cleanup so that it is run both at transaction endand when returning to the main loop on an exception. The latter isnecessary when a procedure calls a COMMIT or ROLLBACK command thatitself causes an error.
1 parenta365f52 commit30c66e7

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

‎src/backend/executor/spi.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,35 +260,37 @@ SPI_rollback(void)
260260
_SPI_current->internal_xact= false;
261261
}
262262

263+
/*
264+
* Clean up SPI state. Called on transaction end (of non-SPI-internal
265+
* transactions) and when returning to the main loop on error.
266+
*/
267+
void
268+
SPICleanup(void)
269+
{
270+
_SPI_current=NULL;
271+
_SPI_connected=-1;
272+
SPI_processed=0;
273+
SPI_lastoid=InvalidOid;
274+
SPI_tuptable=NULL;
275+
}
276+
263277
/*
264278
* Clean up SPI state at transaction commit or abort.
265279
*/
266280
void
267281
AtEOXact_SPI(boolisCommit)
268282
{
269-
/*
270-
* Do nothing if the transaction end was initiated by SPI.
271-
*/
283+
/* Do nothing if the transaction end was initiated by SPI. */
272284
if (_SPI_current&&_SPI_current->internal_xact)
273285
return;
274286

275-
/*
276-
* Note that memory contexts belonging to SPI stack entries will be freed
277-
* automatically, so we can ignore them here. We just need to restore our
278-
* static variables to initial state.
279-
*/
280287
if (isCommit&&_SPI_connected!=-1)
281288
ereport(WARNING,
282289
(errcode(ERRCODE_WARNING),
283290
errmsg("transaction left non-empty SPI stack"),
284291
errhint("Check for missing \"SPI_finish\" calls.")));
285292

286-
_SPI_current=_SPI_stack=NULL;
287-
_SPI_stack_depth=0;
288-
_SPI_connected=-1;
289-
SPI_processed=0;
290-
SPI_lastoid=InvalidOid;
291-
SPI_tuptable=NULL;
293+
SPICleanup();
292294
}
293295

294296
/*

‎src/backend/tcop/postgres.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include"catalog/pg_type.h"
4343
#include"commands/async.h"
4444
#include"commands/prepare.h"
45+
#include"executor/spi.h"
4546
#include"jit/jit.h"
4647
#include"libpq/libpq.h"
4748
#include"libpq/pqformat.h"
@@ -3941,6 +3942,7 @@ PostgresMain(int argc, char *argv[],
39413942
WalSndErrorCleanup();
39423943

39433944
PortalErrorCleanup();
3945+
SPICleanup();
39443946

39453947
/*
39463948
* We can't release replication slots inside AbortTransaction() as we

‎src/include/executor/spi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ extern void SPI_start_transaction(void);
163163
externvoidSPI_commit(void);
164164
externvoidSPI_rollback(void);
165165

166+
externvoidSPICleanup(void);
166167
externvoidAtEOXact_SPI(boolisCommit);
167168
externvoidAtEOSubXact_SPI(boolisCommit,SubTransactionIdmySubid);
168169

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp