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

Commitff428cd

Browse files
author
Neil Conway
committed
Fix several memory leaks when rescanning SRFs. Arrange for an SRF's
"multi_call_ctx" to be a distinct sub-context of the EState's per-querycontext, and delete the multi_call_ctx as soon as the SRF finishesexecution. This avoids leaking SRF memory until the end of the currentquery, which is particularly egregious when the SRF is scannedmultiple times. This change also fixes a leak of the fields of theAttInMetadata struct in shutdown_MultiFuncCall().Also fix a leak of the SRF result TupleDesc when rescanning aFunctionScan node. The TupleDesc is allocated in the per-query contextfor every call to ExecMakeTableFunctionResult(), so we should free itafter calling that function. Since the SRF might choose to returna non-expendable TupleDesc, we only free the TupleDesc if it isnot being reference-counted.Backpatch to 8.3 and 8.2 stable branches.
1 parentb13635c commitff428cd

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

‎src/backend/executor/nodeFunctionscan.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/nodeFunctionscan.c,v 1.45 2008/01/01 19:45:49 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/nodeFunctionscan.c,v 1.46 2008/02/29 02:49:39 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -77,7 +77,17 @@ FunctionNext(FunctionScanState *node)
7777
* do it always.
7878
*/
7979
if (funcTupdesc)
80+
{
8081
tupledesc_match(node->tupdesc,funcTupdesc);
82+
83+
/*
84+
* If it is a dynamically-allocated TupleDesc, free it: it is
85+
* typically allocated in the EState's per-query context, so we
86+
* must avoid leaking it on rescan.
87+
*/
88+
if (funcTupdesc->tdrefcount==-1)
89+
FreeTupleDesc(funcTupdesc);
90+
}
8191
}
8292

8393
/*

‎src/backend/utils/fmgr/funcapi.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/fmgr/funcapi.c,v 1.37 2008/01/01 19:45:53 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/fmgr/funcapi.c,v 1.38 2008/02/29 02:49:39 neilc Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,6 +23,7 @@
2323
#include"utils/array.h"
2424
#include"utils/builtins.h"
2525
#include"utils/lsyscache.h"
26+
#include"utils/memutils.h"
2627
#include"utils/syscache.h"
2728
#include"utils/typcache.h"
2829

@@ -63,13 +64,23 @@ init_MultiFuncCall(PG_FUNCTION_ARGS)
6364
/*
6465
* First call
6566
*/
66-
ReturnSetInfo*rsi= (ReturnSetInfo*)fcinfo->resultinfo;
67+
ReturnSetInfo*rsi= (ReturnSetInfo*)fcinfo->resultinfo;
68+
MemoryContextmulti_call_ctx;
69+
70+
/*
71+
* Create a suitably long-lived context to hold cross-call data
72+
*/
73+
multi_call_ctx=AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
74+
"SRF multi-call context",
75+
ALLOCSET_SMALL_MINSIZE,
76+
ALLOCSET_SMALL_INITSIZE,
77+
ALLOCSET_SMALL_MAXSIZE);
6778

6879
/*
6980
* Allocate suitably long-lived space and zero it
7081
*/
7182
retval= (FuncCallContext*)
72-
MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
83+
MemoryContextAllocZero(multi_call_ctx,
7384
sizeof(FuncCallContext));
7485

7586
/*
@@ -81,7 +92,7 @@ init_MultiFuncCall(PG_FUNCTION_ARGS)
8192
retval->user_fctx=NULL;
8293
retval->attinmeta=NULL;
8394
retval->tuple_desc=NULL;
84-
retval->multi_call_memory_ctx=fcinfo->flinfo->fn_mcxt;
95+
retval->multi_call_memory_ctx=multi_call_ctx;
8596

8697
/*
8798
* save the pointer for cross-call use
@@ -168,13 +179,11 @@ shutdown_MultiFuncCall(Datum arg)
168179
flinfo->fn_extra=NULL;
169180

170181
/*
171-
*Caller is responsible to free up memory for individual struct elements
172-
*other than att_in_funcinfo and elements.
182+
*Delete context that holds all multi-call data, including the
183+
*FuncCallContext itself
173184
*/
174-
if (funcctx->attinmeta!=NULL)
175-
pfree(funcctx->attinmeta);
176-
177-
pfree(funcctx);
185+
MemoryContextSwitchTo(flinfo->fn_mcxt);
186+
MemoryContextDelete(funcctx->multi_call_memory_ctx);
178187
}
179188

180189

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp