Tweak default memory context allocation policy so that a context is not

given any malloc block until something is first allocated in it; but
thereafter, MemoryContextReset won't release that first malloc block.
This preserves the quick-reset property of the original policy, without
forcing 8K to be allocated to every context whether any of it is ever
used or not.  Also, remove some more no-longer-needed explicit freeing
during ExecEndPlan.
This commit is contained in:
Tom Lane 2002-12-15 21:01:34 +00:00
parent 5bab36e9f6
commit e64c7feb2f
11 changed files with 66 additions and 99 deletions

View File

@ -6,7 +6,7 @@
* Copyright (c) 2002, PostgreSQL Global Development Group * Copyright (c) 2002, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/prepare.c,v 1.11 2002/12/15 16:17:42 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/prepare.c,v 1.12 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -256,9 +256,9 @@ StoreQuery(const char *stmt_name, List *query_list, List *plan_list,
/* Make a permanent memory context for the hashtable entry */ /* Make a permanent memory context for the hashtable entry */
entrycxt = AllocSetContextCreate(TopMemoryContext, entrycxt = AllocSetContextCreate(TopMemoryContext,
stmt_name, stmt_name,
1024, ALLOCSET_SMALL_MINSIZE,
1024, ALLOCSET_SMALL_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_SMALL_MAXSIZE);
oldcxt = MemoryContextSwitchTo(entrycxt); oldcxt = MemoryContextSwitchTo(entrycxt);

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.141 2002/11/25 03:36:50 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.142 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -2008,13 +2008,9 @@ deferredTriggerInvokeEvents(bool immediate_only)
void void
DeferredTriggerInit(void) DeferredTriggerInit(void)
{ {
/*
* Since this context will never be reset, give it a minsize of 0.
* This avoids using any memory if the session never stores anything.
*/
deftrig_gcxt = AllocSetContextCreate(TopMemoryContext, deftrig_gcxt = AllocSetContextCreate(TopMemoryContext,
"DeferredTriggerSession", "DeferredTriggerSession",
0, ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_DEFAULT_MAXSIZE);
} }
@ -2041,12 +2037,11 @@ DeferredTriggerBeginXact(void)
/* /*
* Create the per transaction memory context and copy all states from * Create the per transaction memory context and copy all states from
* the per session context to here. Set the minsize to 0 to avoid * the per session context to here.
* wasting memory if there is no deferred trigger data.
*/ */
deftrig_cxt = AllocSetContextCreate(TopTransactionContext, deftrig_cxt = AllocSetContextCreate(TopTransactionContext,
"DeferredTriggerXact", "DeferredTriggerXact",
0, ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_DEFAULT_MAXSIZE);
oldcxt = MemoryContextSwitchTo(deftrig_cxt); oldcxt = MemoryContextSwitchTo(deftrig_cxt);

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execJunk.c,v 1.33 2002/12/12 15:49:28 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/executor/execJunk.c,v 1.34 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -63,8 +63,6 @@ JunkFilter *
ExecInitJunkFilter(List *targetList, TupleDesc tupType, ExecInitJunkFilter(List *targetList, TupleDesc tupType,
TupleTableSlot *slot) TupleTableSlot *slot)
{ {
MemoryContext oldContext;
MemoryContext junkContext;
JunkFilter *junkfilter; JunkFilter *junkfilter;
List *cleanTargetList; List *cleanTargetList;
int len, int len,
@ -79,19 +77,6 @@ ExecInitJunkFilter(List *targetList, TupleDesc tupType,
AttrNumber *cleanMap; AttrNumber *cleanMap;
Expr *expr; Expr *expr;
/*
* Make a memory context that will hold the JunkFilter as well as all
* the subsidiary structures we are about to create. We use smaller-
* than-default sizing parameters since we don't expect a very large
* volume of stuff here.
*/
junkContext = AllocSetContextCreate(CurrentMemoryContext,
"JunkFilterContext",
1024,
1024,
ALLOCSET_DEFAULT_MAXSIZE);
oldContext = MemoryContextSwitchTo(junkContext);
/* /*
* First find the "clean" target list, i.e. all the entries in the * First find the "clean" target list, i.e. all the entries in the
* original target list which have a false 'resjunk' NOTE: make copy * original target list which have a false 'resjunk' NOTE: make copy
@ -174,33 +159,14 @@ ExecInitJunkFilter(List *targetList, TupleDesc tupType,
junkfilter->jf_cleanLength = cleanLength; junkfilter->jf_cleanLength = cleanLength;
junkfilter->jf_cleanTupType = cleanTupType; junkfilter->jf_cleanTupType = cleanTupType;
junkfilter->jf_cleanMap = cleanMap; junkfilter->jf_cleanMap = cleanMap;
junkfilter->jf_junkContext = junkContext;
junkfilter->jf_resultSlot = slot; junkfilter->jf_resultSlot = slot;
if (slot) if (slot)
ExecSetSlotDescriptor(slot, cleanTupType, false); ExecSetSlotDescriptor(slot, cleanTupType, false);
MemoryContextSwitchTo(oldContext);
return junkfilter; return junkfilter;
} }
/*-------------------------------------------------------------------------
* ExecFreeJunkFilter
*
* Release the data structures created by ExecInitJunkFilter.
*-------------------------------------------------------------------------
*/
void
ExecFreeJunkFilter(JunkFilter *junkfilter)
{
/*
* Since the junkfilter is inside its own context, we just have to
* delete the context and we're set.
*/
MemoryContextDelete(junkfilter->jf_junkContext);
}
/*------------------------------------------------------------------------- /*-------------------------------------------------------------------------
* ExecGetJunkAttribute * ExecGetJunkAttribute
* *

View File

@ -26,7 +26,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.193 2002/12/15 16:17:45 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.194 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -775,6 +775,12 @@ initResultRelInfo(ResultRelInfo *resultRelInfo,
* ExecEndPlan * ExecEndPlan
* *
* Cleans up the query plan -- closes files and frees up storage * Cleans up the query plan -- closes files and frees up storage
*
* NOTE: we are no longer very worried about freeing storage per se
* in this code; FreeExecutorState should be guaranteed to release all
* memory that needs to be released. What we are worried about doing
* is closing relations and dropping buffer pins. Thus, for example,
* tuple tables must be cleared or dropped to ensure pins are released.
* ---------------------------------------------------------------- * ----------------------------------------------------------------
*/ */
void void
@ -803,7 +809,7 @@ ExecEndPlan(PlanState *planstate, EState *estate)
/* /*
* close the result relation(s) if any, but hold locks until xact * close the result relation(s) if any, but hold locks until xact
* commit. Also clean up junkfilters if present. * commit.
*/ */
resultRelInfo = estate->es_result_relations; resultRelInfo = estate->es_result_relations;
for (i = estate->es_num_result_relations; i > 0; i--) for (i = estate->es_num_result_relations; i > 0; i--)
@ -811,9 +817,6 @@ ExecEndPlan(PlanState *planstate, EState *estate)
/* Close indices and then the relation itself */ /* Close indices and then the relation itself */
ExecCloseIndices(resultRelInfo); ExecCloseIndices(resultRelInfo);
heap_close(resultRelInfo->ri_RelationDesc, NoLock); heap_close(resultRelInfo->ri_RelationDesc, NoLock);
/* Delete the junkfilter if any */
if (resultRelInfo->ri_junkFilter != NULL)
ExecFreeJunkFilter(resultRelInfo->ri_junkFilter);
resultRelInfo++; resultRelInfo++;
} }
@ -823,16 +826,6 @@ ExecEndPlan(PlanState *planstate, EState *estate)
if (estate->es_into_relation_descriptor != NULL) if (estate->es_into_relation_descriptor != NULL)
heap_close(estate->es_into_relation_descriptor, NoLock); heap_close(estate->es_into_relation_descriptor, NoLock);
/*
* There might be a junkfilter without a result relation.
*/
if (estate->es_num_result_relations == 0 &&
estate->es_junkFilter != NULL)
{
ExecFreeJunkFilter(estate->es_junkFilter);
estate->es_junkFilter = NULL;
}
/* /*
* close any relations selected FOR UPDATE, again keeping locks * close any relations selected FOR UPDATE, again keeping locks
*/ */

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.80 2002/12/15 16:17:46 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.81 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1489,9 +1489,9 @@ _SPI_copy_plan(_SPI_plan *plan, int location)
*/ */
plancxt = AllocSetContextCreate(parentcxt, plancxt = AllocSetContextCreate(parentcxt,
"SPI Plan", "SPI Plan",
1024, ALLOCSET_SMALL_MINSIZE,
1024, ALLOCSET_SMALL_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE); ALLOCSET_SMALL_MAXSIZE);
oldcxt = MemoryContextSwitchTo(plancxt); oldcxt = MemoryContextSwitchTo(plancxt);
/* Copy the SPI plan into its own context */ /* Copy the SPI plan into its own context */

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.181 2002/11/15 17:18:49 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.182 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -647,9 +647,9 @@ RelationBuildRuleLock(Relation relation)
*/ */
rulescxt = AllocSetContextCreate(CacheMemoryContext, rulescxt = AllocSetContextCreate(CacheMemoryContext,
RelationGetRelationName(relation), RelationGetRelationName(relation),
0, /* minsize */ ALLOCSET_SMALL_MINSIZE,
1024, /* initsize */ ALLOCSET_SMALL_INITSIZE,
1024); /* maxsize */ ALLOCSET_SMALL_MAXSIZE);
relation->rd_rulescxt = rulescxt; relation->rd_rulescxt = rulescxt;
/* /*
@ -994,9 +994,9 @@ RelationInitIndexAccessInfo(Relation relation)
*/ */
indexcxt = AllocSetContextCreate(CacheMemoryContext, indexcxt = AllocSetContextCreate(CacheMemoryContext,
RelationGetRelationName(relation), RelationGetRelationName(relation),
0, /* minsize */ ALLOCSET_SMALL_MINSIZE,
512, /* initsize */ ALLOCSET_SMALL_INITSIZE,
1024); /* maxsize */ ALLOCSET_SMALL_MAXSIZE);
relation->rd_indexcxt = indexcxt; relation->rd_indexcxt = indexcxt;
/* /*
@ -2851,9 +2851,9 @@ load_relcache_init_file(void)
*/ */
indexcxt = AllocSetContextCreate(CacheMemoryContext, indexcxt = AllocSetContextCreate(CacheMemoryContext,
RelationGetRelationName(rel), RelationGetRelationName(rel),
0, /* minsize */ ALLOCSET_SMALL_MINSIZE,
512, /* initsize */ ALLOCSET_SMALL_INITSIZE,
1024); /* maxsize */ ALLOCSET_SMALL_MAXSIZE);
rel->rd_indexcxt = indexcxt; rel->rd_indexcxt = indexcxt;
/* next, read the index strategy map */ /* next, read the index strategy map */

View File

@ -11,7 +11,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.48 2002/09/04 20:31:33 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.49 2002/12/15 21:01:34 tgl Exp $
* *
* NOTE: * NOTE:
* This is a new (Feb. 05, 1999) implementation of the allocation set * This is a new (Feb. 05, 1999) implementation of the allocation set
@ -371,10 +371,11 @@ AllocSetInit(MemoryContext context)
* Frees all memory which is allocated in the given set. * Frees all memory which is allocated in the given set.
* *
* Actually, this routine has some discretion about what to do. * Actually, this routine has some discretion about what to do.
* It should mark all allocated chunks freed, but it need not * It should mark all allocated chunks freed, but it need not necessarily
* necessarily give back all the resources the set owns. Our * give back all the resources the set owns. Our actual implementation is
* actual implementation is that we hang on to any "keeper" * that we hang onto any "keeper" block specified for the set. In this way,
* block specified for the set. * we don't thrash malloc() when a context is repeatedly reset after small
* allocations, which is typical behavior for per-tuple contexts.
*/ */
static void static void
AllocSetReset(MemoryContext context) AllocSetReset(MemoryContext context)
@ -697,6 +698,21 @@ AllocSetAlloc(MemoryContext context, Size size)
block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ; block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
block->endptr = ((char *) block) + blksize; block->endptr = ((char *) block) + blksize;
/*
* If this is the first block of the set, make it the "keeper" block.
* Formerly, a keeper block could only be created during context
* creation, but allowing it to happen here lets us have fast reset
* cycling even for contexts created with minContextSize = 0; that
* way we don't have to force space to be allocated in contexts that
* might never need any space. Don't mark an oversize block as
* a keeper, however.
*/
if (set->blocks == NULL && blksize == set->initBlockSize)
{
Assert(set->keeper == NULL);
set->keeper = block;
}
block->next = set->blocks; block->next = set->blocks;
set->blocks = block; set->blocks = block;
} }

View File

@ -14,7 +14,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.36 2002/11/13 00:37:06 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.37 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -80,7 +80,7 @@ MemoryContextInit(void)
*/ */
TopMemoryContext = AllocSetContextCreate((MemoryContext) NULL, TopMemoryContext = AllocSetContextCreate((MemoryContext) NULL,
"TopMemoryContext", "TopMemoryContext",
8 * 1024, 0,
8 * 1024, 8 * 1024,
8 * 1024); 8 * 1024);

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: executor.h,v 1.84 2002/12/15 16:17:54 tgl Exp $ * $Id: executor.h,v 1.85 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -41,7 +41,6 @@ extern bool ExecSupportsMarkRestore(NodeTag plantype);
*/ */
extern JunkFilter *ExecInitJunkFilter(List *targetList, TupleDesc tupType, extern JunkFilter *ExecInitJunkFilter(List *targetList, TupleDesc tupType,
TupleTableSlot *slot); TupleTableSlot *slot);
extern void ExecFreeJunkFilter(JunkFilter *junkfilter);
extern bool ExecGetJunkAttribute(JunkFilter *junkfilter, TupleTableSlot *slot, extern bool ExecGetJunkAttribute(JunkFilter *junkfilter, TupleTableSlot *slot,
char *attrName, Datum *value, bool *isNull); char *attrName, Datum *value, bool *isNull);
extern HeapTuple ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot); extern HeapTuple ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot);

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: execnodes.h,v 1.86 2002/12/15 16:17:56 tgl Exp $ * $Id: execnodes.h,v 1.87 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -218,16 +218,7 @@ typedef struct ProjectionInfo
* cleanMap: A map with the correspondence between the non-junk * cleanMap: A map with the correspondence between the non-junk
* attribute numbers of the "original" tuple and the * attribute numbers of the "original" tuple and the
* attribute numbers of the "clean" tuple. * attribute numbers of the "clean" tuple.
* junkContext: memory context holding the JunkFilter node and all
* its subsidiary data structures.
* resultSlot: tuple slot that can be used to hold cleaned tuple. * resultSlot: tuple slot that can be used to hold cleaned tuple.
*
* NOTE: the original targetList and tupType are passed to ExecInitJunkFilter,
* as is the resultSlot. These items do not belong to the JunkFilter. All
* the other subsidiary structures are created during ExecInitJunkFilter,
* and all of them can be freed by deleting the memory context junkContext.
* This would not be needed if we had a cleaner approach to managing
* query-lifetime data structures...
* ---------------- * ----------------
*/ */
typedef struct JunkFilter typedef struct JunkFilter
@ -240,7 +231,6 @@ typedef struct JunkFilter
int jf_cleanLength; int jf_cleanLength;
TupleDesc jf_cleanTupType; TupleDesc jf_cleanTupType;
AttrNumber *jf_cleanMap; AttrNumber *jf_cleanMap;
MemoryContext jf_junkContext;
TupleTableSlot *jf_resultSlot; TupleTableSlot *jf_resultSlot;
} JunkFilter; } JunkFilter;

View File

@ -10,7 +10,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: memutils.h,v 1.48 2002/09/04 20:31:45 momjian Exp $ * $Id: memutils.h,v 1.49 2002/12/15 21:01:34 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -116,8 +116,16 @@ extern MemoryContext AllocSetContextCreate(MemoryContext parent,
* Recommended default alloc parameters, suitable for "ordinary" contexts * Recommended default alloc parameters, suitable for "ordinary" contexts
* that might hold quite a lot of data. * that might hold quite a lot of data.
*/ */
#define ALLOCSET_DEFAULT_MINSIZE (8 * 1024) #define ALLOCSET_DEFAULT_MINSIZE 0
#define ALLOCSET_DEFAULT_INITSIZE (8 * 1024) #define ALLOCSET_DEFAULT_INITSIZE (8 * 1024)
#define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024) #define ALLOCSET_DEFAULT_MAXSIZE (8 * 1024 * 1024)
/*
* Recommended alloc parameters for "small" contexts that are not expected
* to contain much data (for example, a context to contain a query plan).
*/
#define ALLOCSET_SMALL_MINSIZE 0
#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
#endif /* MEMUTILS_H */ #endif /* MEMUTILS_H */