Skip to content

Commit b50cbbd

Browse files
committed
Fix up memory leakage created by recent changes.
1 parent 62d4526 commit b50cbbd

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/backend/commands/analyze.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.36 2002/06/13 19:52:02 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.37 2002/06/15 22:25:40 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -59,7 +59,7 @@ typedef enum
5959

6060
/*
6161
* We build one of these structs for each attribute (column) that is to be
62-
* analyzed. The struct and subsidiary data are in TransactionCommandContext,
62+
* analyzed. The struct and subsidiary data are in anl_context,
6363
* so they live until the end of the ANALYZE operation.
6464
*/
6565
typedef struct
@@ -109,6 +109,8 @@ typedef struct
109109

110110
static int elevel = -1;
111111

112+
static MemoryContext anl_context = NULL;
113+
112114
/* context information for compare_scalars() */
113115
static FmgrInfo *datumCmpFn;
114116
static SortFunctionKind datumCmpFnKind;
@@ -155,6 +157,13 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
155157
else
156158
elevel = DEBUG1;
157159

160+
/*
161+
* Use the current context for storing analysis info. vacuum.c ensures
162+
* that this context will be cleared when I return, thus releasing the
163+
* memory allocated here.
164+
*/
165+
anl_context = CurrentMemoryContext;
166+
158167
/*
159168
* Check for user-requested abort. Note we want this to be inside a
160169
* transaction, so xact.c doesn't issue useless WARNING.
@@ -306,14 +315,14 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
306315
* Compute the statistics. Temporary results during the calculations
307316
* for each column are stored in a child context. The calc routines
308317
* are responsible to make sure that whatever they store into the
309-
* VacAttrStats structure is allocated in TransactionCommandContext.
318+
* VacAttrStats structure is allocated in anl_context.
310319
*/
311320
if (numrows > 0)
312321
{
313322
MemoryContext col_context,
314323
old_context;
315324

316-
col_context = AllocSetContextCreate(CurrentMemoryContext,
325+
col_context = AllocSetContextCreate(anl_context,
317326
"Analyze Column",
318327
ALLOCSET_DEFAULT_MINSIZE,
319328
ALLOCSET_DEFAULT_INITSIZE,
@@ -1094,8 +1103,8 @@ compute_minimal_stats(VacAttrStats *stats,
10941103
Datum *mcv_values;
10951104
float4 *mcv_freqs;
10961105

1097-
/* Must copy the target values into TransactionCommandContext */
1098-
old_context = MemoryContextSwitchTo(TransactionCommandContext);
1106+
/* Must copy the target values into anl_context */
1107+
old_context = MemoryContextSwitchTo(anl_context);
10991108
mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
11001109
mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
11011110
for (i = 0; i < num_mcv; i++)
@@ -1415,8 +1424,8 @@ compute_scalar_stats(VacAttrStats *stats,
14151424
Datum *mcv_values;
14161425
float4 *mcv_freqs;
14171426

1418-
/* Must copy the target values into TransactionCommandContext */
1419-
old_context = MemoryContextSwitchTo(TransactionCommandContext);
1427+
/* Must copy the target values into anl_context */
1428+
old_context = MemoryContextSwitchTo(anl_context);
14201429
mcv_values = (Datum *) palloc(num_mcv * sizeof(Datum));
14211430
mcv_freqs = (float4 *) palloc(num_mcv * sizeof(float4));
14221431
for (i = 0; i < num_mcv; i++)
@@ -1501,8 +1510,8 @@ compute_scalar_stats(VacAttrStats *stats,
15011510
nvals = values_cnt;
15021511
Assert(nvals >= num_hist);
15031512

1504-
/* Must copy the target values into TransactionCommandContext */
1505-
old_context = MemoryContextSwitchTo(TransactionCommandContext);
1513+
/* Must copy the target values into anl_context */
1514+
old_context = MemoryContextSwitchTo(anl_context);
15061515
hist_values = (Datum *) palloc(num_hist * sizeof(Datum));
15071516
for (i = 0; i < num_hist; i++)
15081517
{
@@ -1530,8 +1539,8 @@ compute_scalar_stats(VacAttrStats *stats,
15301539
double corr_xsum,
15311540
corr_x2sum;
15321541

1533-
/* Must copy the target values into TransactionCommandContext */
1534-
old_context = MemoryContextSwitchTo(TransactionCommandContext);
1542+
/* Must copy the target values into anl_context */
1543+
old_context = MemoryContextSwitchTo(anl_context);
15351544
corrs = (float4 *) palloc(sizeof(float4));
15361545
MemoryContextSwitchTo(old_context);
15371546

0 commit comments

Comments
 (0)