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

Commite64c7fe

Browse files
committed
Tweak default memory context allocation policy so that a context is not
given any malloc block until something is first allocated in it; butthereafter, MemoryContextReset won't release that first malloc block.This preserves the quick-reset property of the original policy, withoutforcing 8K to be allocated to every context whether any of it is everused or not. Also, remove some more no-longer-needed explicit freeingduring ExecEndPlan.
1 parent5bab36e commite64c7fe

File tree

11 files changed

+66
-99
lines changed

11 files changed

+66
-99
lines changed

‎src/backend/commands/prepare.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 2002, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/prepare.c,v 1.11 2002/12/1516:17:42 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/prepare.c,v 1.12 2002/12/1521:01:34 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -256,9 +256,9 @@ StoreQuery(const char *stmt_name, List *query_list, List *plan_list,
256256
/* Make a permanent memory context for the hashtable entry */
257257
entrycxt=AllocSetContextCreate(TopMemoryContext,
258258
stmt_name,
259-
1024,
260-
1024,
261-
ALLOCSET_DEFAULT_MAXSIZE);
259+
ALLOCSET_SMALL_MINSIZE,
260+
ALLOCSET_SMALL_INITSIZE,
261+
ALLOCSET_SMALL_MAXSIZE);
262262

263263
oldcxt=MemoryContextSwitchTo(entrycxt);
264264

‎src/backend/commands/trigger.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.141 2002/11/25 03:36:50 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.142 2002/12/15 21:01:34 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -2008,13 +2008,9 @@ deferredTriggerInvokeEvents(bool immediate_only)
20082008
void
20092009
DeferredTriggerInit(void)
20102010
{
2011-
/*
2012-
* Since this context will never be reset, give it a minsize of 0.
2013-
* This avoids using any memory if the session never stores anything.
2014-
*/
20152011
deftrig_gcxt=AllocSetContextCreate(TopMemoryContext,
20162012
"DeferredTriggerSession",
2017-
0,
2013+
ALLOCSET_DEFAULT_MINSIZE,
20182014
ALLOCSET_DEFAULT_INITSIZE,
20192015
ALLOCSET_DEFAULT_MAXSIZE);
20202016
}
@@ -2041,12 +2037,11 @@ DeferredTriggerBeginXact(void)
20412037

20422038
/*
20432039
* Create the per transaction memory context and copy all states from
2044-
* the per session context to here. Set the minsize to 0 to avoid
2045-
* wasting memory if there is no deferred trigger data.
2040+
* the per session context to here.
20462041
*/
20472042
deftrig_cxt=AllocSetContextCreate(TopTransactionContext,
20482043
"DeferredTriggerXact",
2049-
0,
2044+
ALLOCSET_DEFAULT_MINSIZE,
20502045
ALLOCSET_DEFAULT_INITSIZE,
20512046
ALLOCSET_DEFAULT_MAXSIZE);
20522047
oldcxt=MemoryContextSwitchTo(deftrig_cxt);

‎src/backend/executor/execJunk.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/execJunk.c,v 1.33 2002/12/12 15:49:28 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/execJunk.c,v 1.34 2002/12/15 21:01:34 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -63,8 +63,6 @@ JunkFilter *
6363
ExecInitJunkFilter(List*targetList,TupleDesctupType,
6464
TupleTableSlot*slot)
6565
{
66-
MemoryContextoldContext;
67-
MemoryContextjunkContext;
6866
JunkFilter*junkfilter;
6967
List*cleanTargetList;
7068
intlen,
@@ -79,19 +77,6 @@ ExecInitJunkFilter(List *targetList, TupleDesc tupType,
7977
AttrNumber*cleanMap;
8078
Expr*expr;
8179

82-
/*
83-
* Make a memory context that will hold the JunkFilter as well as all
84-
* the subsidiary structures we are about to create. We use smaller-
85-
* than-default sizing parameters since we don't expect a very large
86-
* volume of stuff here.
87-
*/
88-
junkContext=AllocSetContextCreate(CurrentMemoryContext,
89-
"JunkFilterContext",
90-
1024,
91-
1024,
92-
ALLOCSET_DEFAULT_MAXSIZE);
93-
oldContext=MemoryContextSwitchTo(junkContext);
94-
9580
/*
9681
* First find the "clean" target list, i.e. all the entries in the
9782
* original target list which have a false 'resjunk' NOTE: make copy
@@ -174,33 +159,14 @@ ExecInitJunkFilter(List *targetList, TupleDesc tupType,
174159
junkfilter->jf_cleanLength=cleanLength;
175160
junkfilter->jf_cleanTupType=cleanTupType;
176161
junkfilter->jf_cleanMap=cleanMap;
177-
junkfilter->jf_junkContext=junkContext;
178162
junkfilter->jf_resultSlot=slot;
179163

180164
if (slot)
181165
ExecSetSlotDescriptor(slot,cleanTupType, false);
182166

183-
MemoryContextSwitchTo(oldContext);
184-
185167
returnjunkfilter;
186168
}
187169

188-
/*-------------------------------------------------------------------------
189-
* ExecFreeJunkFilter
190-
*
191-
* Release the data structures created by ExecInitJunkFilter.
192-
*-------------------------------------------------------------------------
193-
*/
194-
void
195-
ExecFreeJunkFilter(JunkFilter*junkfilter)
196-
{
197-
/*
198-
* Since the junkfilter is inside its own context, we just have to
199-
* delete the context and we're set.
200-
*/
201-
MemoryContextDelete(junkfilter->jf_junkContext);
202-
}
203-
204170
/*-------------------------------------------------------------------------
205171
* ExecGetJunkAttribute
206172
*

‎src/backend/executor/execMain.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
*
2828
* IDENTIFICATION
29-
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.193 2002/12/1516:17:45 tgl Exp $
29+
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.194 2002/12/1521:01:34 tgl Exp $
3030
*
3131
*-------------------------------------------------------------------------
3232
*/
@@ -775,6 +775,12 @@ initResultRelInfo(ResultRelInfo *resultRelInfo,
775775
*ExecEndPlan
776776
*
777777
*Cleans up the query plan -- closes files and frees up storage
778+
*
779+
* NOTE: we are no longer very worried about freeing storage per se
780+
* in this code; FreeExecutorState should be guaranteed to release all
781+
* memory that needs to be released. What we are worried about doing
782+
* is closing relations and dropping buffer pins. Thus, for example,
783+
* tuple tables must be cleared or dropped to ensure pins are released.
778784
* ----------------------------------------------------------------
779785
*/
780786
void
@@ -803,17 +809,14 @@ ExecEndPlan(PlanState *planstate, EState *estate)
803809

804810
/*
805811
* close the result relation(s) if any, but hold locks until xact
806-
* commit.Also clean up junkfilters if present.
812+
* commit.
807813
*/
808814
resultRelInfo=estate->es_result_relations;
809815
for (i=estate->es_num_result_relations;i>0;i--)
810816
{
811817
/* Close indices and then the relation itself */
812818
ExecCloseIndices(resultRelInfo);
813819
heap_close(resultRelInfo->ri_RelationDesc,NoLock);
814-
/* Delete the junkfilter if any */
815-
if (resultRelInfo->ri_junkFilter!=NULL)
816-
ExecFreeJunkFilter(resultRelInfo->ri_junkFilter);
817820
resultRelInfo++;
818821
}
819822

@@ -823,16 +826,6 @@ ExecEndPlan(PlanState *planstate, EState *estate)
823826
if (estate->es_into_relation_descriptor!=NULL)
824827
heap_close(estate->es_into_relation_descriptor,NoLock);
825828

826-
/*
827-
* There might be a junkfilter without a result relation.
828-
*/
829-
if (estate->es_num_result_relations==0&&
830-
estate->es_junkFilter!=NULL)
831-
{
832-
ExecFreeJunkFilter(estate->es_junkFilter);
833-
estate->es_junkFilter=NULL;
834-
}
835-
836829
/*
837830
* close any relations selected FOR UPDATE, again keeping locks
838831
*/

‎src/backend/executor/spi.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.80 2002/12/1516:17:46 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.81 2002/12/1521:01:34 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1489,9 +1489,9 @@ _SPI_copy_plan(_SPI_plan *plan, int location)
14891489
*/
14901490
plancxt=AllocSetContextCreate(parentcxt,
14911491
"SPI Plan",
1492-
1024,
1493-
1024,
1494-
ALLOCSET_DEFAULT_MAXSIZE);
1492+
ALLOCSET_SMALL_MINSIZE,
1493+
ALLOCSET_SMALL_INITSIZE,
1494+
ALLOCSET_SMALL_MAXSIZE);
14951495
oldcxt=MemoryContextSwitchTo(plancxt);
14961496

14971497
/* Copy the SPI plan into its own context */

‎src/backend/utils/cache/relcache.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.181 2002/11/1517:18:49 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.182 2002/12/1521:01:34 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -647,9 +647,9 @@ RelationBuildRuleLock(Relation relation)
647647
*/
648648
rulescxt=AllocSetContextCreate(CacheMemoryContext,
649649
RelationGetRelationName(relation),
650-
0,/* minsize */
651-
1024,/* initsize */
652-
1024);/* maxsize */
650+
ALLOCSET_SMALL_MINSIZE,
651+
ALLOCSET_SMALL_INITSIZE,
652+
ALLOCSET_SMALL_MAXSIZE);
653653
relation->rd_rulescxt=rulescxt;
654654

655655
/*
@@ -994,9 +994,9 @@ RelationInitIndexAccessInfo(Relation relation)
994994
*/
995995
indexcxt=AllocSetContextCreate(CacheMemoryContext,
996996
RelationGetRelationName(relation),
997-
0,/* minsize */
998-
512,/* initsize */
999-
1024);/* maxsize */
997+
ALLOCSET_SMALL_MINSIZE,
998+
ALLOCSET_SMALL_INITSIZE,
999+
ALLOCSET_SMALL_MAXSIZE);
10001000
relation->rd_indexcxt=indexcxt;
10011001

10021002
/*
@@ -2851,9 +2851,9 @@ load_relcache_init_file(void)
28512851
*/
28522852
indexcxt=AllocSetContextCreate(CacheMemoryContext,
28532853
RelationGetRelationName(rel),
2854-
0,/* minsize */
2855-
512,/* initsize */
2856-
1024);/* maxsize */
2854+
ALLOCSET_SMALL_MINSIZE,
2855+
ALLOCSET_SMALL_INITSIZE,
2856+
ALLOCSET_SMALL_MAXSIZE);
28572857
rel->rd_indexcxt=indexcxt;
28582858

28592859
/* next, read the index strategy map */

‎src/backend/utils/mmgr/aset.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.48 2002/09/04 20:31:33 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.49 2002/12/15 21:01:34 tgl Exp $
1515
*
1616
* NOTE:
1717
*This is a new (Feb. 05, 1999) implementation of the allocation set
@@ -371,10 +371,11 @@ AllocSetInit(MemoryContext context)
371371
*Frees all memory which is allocated in the given set.
372372
*
373373
* Actually, this routine has some discretion about what to do.
374-
* It should mark all allocated chunks freed, but it need not
375-
* necessarily give back all the resources the set owns. Our
376-
* actual implementation is that we hang on to any "keeper"
377-
* block specified for the set.
374+
* It should mark all allocated chunks freed, but it need not necessarily
375+
* give back all the resources the set owns. Our actual implementation is
376+
* that we hang onto any "keeper" block specified for the set. In this way,
377+
* we don't thrash malloc() when a context is repeatedly reset after small
378+
* allocations, which is typical behavior for per-tuple contexts.
378379
*/
379380
staticvoid
380381
AllocSetReset(MemoryContextcontext)
@@ -697,6 +698,21 @@ AllocSetAlloc(MemoryContext context, Size size)
697698
block->freeptr= ((char*)block)+ALLOC_BLOCKHDRSZ;
698699
block->endptr= ((char*)block)+blksize;
699700

701+
/*
702+
* If this is the first block of the set, make it the "keeper" block.
703+
* Formerly, a keeper block could only be created during context
704+
* creation, but allowing it to happen here lets us have fast reset
705+
* cycling even for contexts created with minContextSize = 0; that
706+
* way we don't have to force space to be allocated in contexts that
707+
* might never need any space. Don't mark an oversize block as
708+
* a keeper, however.
709+
*/
710+
if (set->blocks==NULL&&blksize==set->initBlockSize)
711+
{
712+
Assert(set->keeper==NULL);
713+
set->keeper=block;
714+
}
715+
700716
block->next=set->blocks;
701717
set->blocks=block;
702718
}

‎src/backend/utils/mmgr/mcxt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.36 2002/11/13 00:37:06 momjian Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.37 2002/12/15 21:01:34 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -80,7 +80,7 @@ MemoryContextInit(void)
8080
*/
8181
TopMemoryContext=AllocSetContextCreate((MemoryContext)NULL,
8282
"TopMemoryContext",
83-
8*1024,
83+
0,
8484
8*1024,
8585
8*1024);
8686

‎src/include/executor/executor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: executor.h,v 1.84 2002/12/1516:17:54 tgl Exp $
10+
* $Id: executor.h,v 1.85 2002/12/1521:01:34 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -41,7 +41,6 @@ extern bool ExecSupportsMarkRestore(NodeTag plantype);
4141
*/
4242
externJunkFilter*ExecInitJunkFilter(List*targetList,TupleDesctupType,
4343
TupleTableSlot*slot);
44-
externvoidExecFreeJunkFilter(JunkFilter*junkfilter);
4544
externboolExecGetJunkAttribute(JunkFilter*junkfilter,TupleTableSlot*slot,
4645
char*attrName,Datum*value,bool*isNull);
4746
externHeapTupleExecRemoveJunk(JunkFilter*junkfilter,TupleTableSlot*slot);

‎src/include/nodes/execnodes.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: execnodes.h,v 1.86 2002/12/1516:17:56 tgl Exp $
10+
* $Id: execnodes.h,v 1.87 2002/12/1521:01:34 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -218,16 +218,7 @@ typedef struct ProjectionInfo
218218
* cleanMap:A map with the correspondence between the non-junk
219219
*attribute numbers of the "original" tuple and the
220220
*attribute numbers of the "clean" tuple.
221-
* junkContext:memory context holding the JunkFilter node and all
222-
*its subsidiary data structures.
223221
* resultSlot:tuple slot that can be used to hold cleaned tuple.
224-
*
225-
* NOTE: the original targetList and tupType are passed to ExecInitJunkFilter,
226-
* as is the resultSlot. These items do not belong to the JunkFilter.All
227-
* the other subsidiary structures are created during ExecInitJunkFilter,
228-
* and all of them can be freed by deleting the memory context junkContext.
229-
* This would not be needed if we had a cleaner approach to managing
230-
* query-lifetime data structures...
231222
* ----------------
232223
*/
233224
typedefstructJunkFilter
@@ -240,7 +231,6 @@ typedef struct JunkFilter
240231
intjf_cleanLength;
241232
TupleDescjf_cleanTupType;
242233
AttrNumber*jf_cleanMap;
243-
MemoryContextjf_junkContext;
244234
TupleTableSlot*jf_resultSlot;
245235
}JunkFilter;
246236

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp