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

Commit9c95f8c

Browse files
committed
Repair bugs discussed in pghackers thread of 15 May 1999: creation of a
relcache entry no longer leaks a small amount of memory. index_endscannow releases all the memory acquired by index_beginscan, so callers of itshould NOT pfree the scan descriptor anymore.
1 parent649ffe1 commit9c95f8c

File tree

8 files changed

+60
-69
lines changed

8 files changed

+60
-69
lines changed

‎src/backend/access/index/genam.c

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/index/genam.c,v 1.21 1999/07/17 20:16:40 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/index/genam.c,v 1.22 1999/12/30 05:04:50 tgl Exp $
1111
*
1212
* NOTES
1313
* many of the old access method routines have been turned into
@@ -39,7 +39,7 @@
3939
* All other states cannot occur.
4040
*
4141
* Note:
42-
*It would be possible to cache the status of the previous and
42+
*It would be possible to cache the status of the previous and
4343
*next item pointer using the flags.
4444
* ----------------------------------------------------------------
4545
*/
@@ -55,9 +55,19 @@
5555
*We don't know how the various AMs do locking, however, so we don't
5656
*do anything about that here.
5757
*
58-
*The intent is that an AM implementor will define a front-end routine
59-
*that calls this one, to fill in the scan, and then does whatever kind
60-
*of locking he wants.
58+
*The intent is that an AM implementor will define a beginscan routine
59+
*that calls RelationGetIndexScan, to fill in the scan, and then does
60+
*whatever kind of locking he wants.
61+
*
62+
*At the end of a scan, the AM's endscan routine undoes the locking,
63+
*but does *not* call IndexScanEnd --- the higher-level index_endscan
64+
*routine does that. (We can't do it in the AM because index_endscan
65+
*still needs to touch the IndexScanDesc after calling the AM.)
66+
*
67+
*Because of this, the AM does not have a choice whether to call
68+
*RelationGetIndexScan or not; its beginscan routine must return an
69+
*object made by RelationGetIndexScan. This is kinda ugly but not
70+
*worth cleaning up now.
6171
* ----------------------------------------------------------------
6272
*/
6373

@@ -72,16 +82,11 @@
7282
*relation -- index relation for scan.
7383
*scanFromEnd -- if true, begin scan at one of the index's
7484
* endpoints.
75-
*numberOfKeys -- count of scan keys (more than one won't
76-
*necessarily do anything useful, yet).
85+
*numberOfKeys -- count of scan keys.
7786
*key -- the ScanKey for the starting position of the scan.
7887
*
7988
*Returns:
8089
*An initialized IndexScanDesc.
81-
*
82-
*Side Effects:
83-
*Bumps the ref count on the relation to keep it in the cache.
84-
*
8590
* ----------------
8691
*/
8792
IndexScanDesc
@@ -118,6 +123,30 @@ RelationGetIndexScan(Relation relation,
118123
returnscan;
119124
}
120125

126+
/* ----------------
127+
*IndexScanEnd -- End an index scan.
128+
*
129+
*This routine just releases the storage acquired by
130+
*RelationGetIndexScan(). Any AM-level resources are
131+
*assumed to already have been released by the AM's
132+
*endscan routine.
133+
*
134+
*Returns:
135+
*None.
136+
* ----------------
137+
*/
138+
void
139+
IndexScanEnd(IndexScanDescscan)
140+
{
141+
if (!IndexScanIsValid(scan))
142+
elog(ERROR,"IndexScanEnd: invalid scan");
143+
144+
if (scan->keyData!=NULL)
145+
pfree(scan->keyData);
146+
147+
pfree(scan);
148+
}
149+
121150
#ifdefNOT_USED
122151
/* ----------------
123152
*IndexScanRestart -- Restart an index scan.
@@ -159,29 +188,6 @@ IndexScanRestart(IndexScanDesc scan,
159188
scan->numberOfKeys*sizeof(ScanKeyData));
160189
}
161190

162-
/* ----------------
163-
*IndexScanEnd -- End and index scan.
164-
*
165-
*This routine is not used by any existing access method, but is
166-
*suitable for use if you don't want to do sophisticated locking.
167-
*
168-
*Returns:
169-
*None.
170-
*
171-
*Side Effects:
172-
*None.
173-
* ----------------
174-
*/
175-
void
176-
IndexScanEnd(IndexScanDescscan)
177-
{
178-
if (!IndexScanIsValid(scan))
179-
elog(ERROR,"IndexScanEnd: invalid scan");
180-
181-
pfree(scan);
182-
}
183-
184-
185191
/* ----------------
186192
*IndexScanMarkPosition -- Mark current position in a scan.
187193
*

‎src/backend/access/index/indexam.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.37 1999/11/07 23:07:54 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/index/indexam.c,v 1.38 1999/12/30 05:04:50 tgl Exp $
1111
*
1212
* INTERFACE ROUTINES
1313
*index_open- open an index relation by relationId
@@ -298,6 +298,9 @@ index_endscan(IndexScanDesc scan)
298298
UnlockRelation(scan->relation,AccessShareLock);
299299

300300
RelationDecrementReferenceCount(scan->relation);
301+
302+
/* Release the scan data structure itself */
303+
IndexScanEnd(scan);
301304
}
302305

303306
/* ----------------

‎src/backend/catalog/indexing.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.54 1999/12/16 22:19:39 wieck Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.55 1999/12/30 05:04:55 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -266,7 +266,7 @@ CatalogIndexFetchTuple(Relation heapRelation,
266266
}
267267

268268
index_endscan(sd);
269-
pfree(sd);
269+
270270
returnresult;
271271
}
272272

‎src/backend/commands/trigger.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ RelationBuildTriggers(Relation relation)
513513
NAMEDATALEN,RelationGetRelationName(relation));
514514

515515
index_endscan(sd);
516-
pfree(sd);
517516
index_close(irel);
518517
heap_close(tgrel,AccessShareLock);
519518

@@ -1562,7 +1561,6 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt)
15621561
elog(ERROR,"Constraint '%s' does not exist", (char*)lfirst(l));
15631562

15641563
index_endscan(sd);
1565-
15661564
}
15671565
index_close(irel);
15681566
heap_close(tgrel,AccessShareLock);

‎src/backend/storage/large_object/inv_api.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.63 1999/12/16 22:19:51 wieck Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.64 1999/12/30 05:05:03 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -258,7 +258,6 @@ inv_close(LargeObjectDesc *obj_desc)
258258
if (obj_desc->iscan!= (IndexScanDesc)NULL)
259259
{
260260
index_endscan(obj_desc->iscan);
261-
pfree(obj_desc->iscan);
262261
obj_desc->iscan=NULL;
263262
}
264263

@@ -583,7 +582,6 @@ inv_cleanindex(LargeObjectDesc *obj_desc)
583582
return;
584583

585584
index_endscan(obj_desc->iscan);
586-
pfree(obj_desc->iscan);
587585
obj_desc->iscan= (IndexScanDesc)NULL;
588586

589587
ItemPointerSetInvalid(&(obj_desc->htid));
@@ -1255,7 +1253,6 @@ _inv_getsize(Relation hreln, TupleDesc hdesc, Relation ireln)
12551253
if (res== (RetrieveIndexResult)NULL)
12561254
{
12571255
index_endscan(iscan);
1258-
pfree(iscan);
12591256
return0;
12601257
}
12611258

@@ -1271,7 +1268,6 @@ _inv_getsize(Relation hreln, TupleDesc hdesc, Relation ireln)
12711268

12721269
/* don't need the index scan anymore */
12731270
index_endscan(iscan);
1274-
pfree(iscan);
12751271

12761272
/* get olastbyte attribute */
12771273
d=heap_getattr(&tuple,1,hdesc,&isNull);

‎src/backend/utils/adt/regproc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.46 1999/12/16 22:19:52 wieck Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.47 1999/12/30 05:05:07 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -102,7 +102,6 @@ regprocin(char *pro_name_or_oid)
102102
}
103103

104104
index_endscan(sd);
105-
pfree(sd);
106105
index_close(idesc);
107106
heap_close(hdesc,AccessShareLock);
108107

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

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.83 1999/12/28 13:40:49 wieck Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.84 1999/12/30 05:05:11 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -378,31 +378,27 @@ static Relation
378378
AllocateRelationDesc(Relationrelation,u_intnatts,
379379
Form_pg_classrelp)
380380
{
381-
Sizelen;
382381
Form_pg_classrelationForm;
383382

384383
/* ----------------
385384
*allocate space for the relation tuple form
386385
* ----------------
387386
*/
388-
relationForm= (Form_pg_class)
389-
palloc((Size) (sizeof(FormData_pg_class)));
387+
relationForm= (Form_pg_class)palloc(sizeof(FormData_pg_class));
390388

391-
memmove((char*)relationForm, (char*)relp,CLASS_TUPLE_SIZE);
389+
memcpy((char*)relationForm, (char*)relp,CLASS_TUPLE_SIZE);
392390

393391
/* ----------------
394392
*allocate space for new relation descriptor, if needed
395393
*/
396-
len=sizeof(RelationData);
397-
398394
if (relation==NULL)
399-
relation= (Relation)palloc(len);
395+
relation= (Relation)palloc(sizeof(RelationData));
400396

401397
/* ----------------
402398
*clear new reldesc
403399
* ----------------
404400
*/
405-
MemSet((char*)relation,0,len);
401+
MemSet((char*)relation,0,sizeof(RelationData));
406402

407403
/* make sure relation is marked as having no open file yet */
408404
relation->rd_fd=-1;
@@ -745,14 +741,10 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo,
745741
u_intnatts;
746742
Oidrelid;
747743
Oidrelam;
744+
HeapTuplepg_class_tuple;
748745
Form_pg_classrelp;
749-
750746
MemoryContextoldcxt;
751747

752-
HeapTuplepg_class_tuple;
753-
754-
oldcxt=MemoryContextSwitchTo((MemoryContext)CacheCxt);
755-
756748
/* ----------------
757749
*find the tuple in pg_class corresponding to the given relation id
758750
* ----------------
@@ -764,11 +756,7 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo,
764756
* ----------------
765757
*/
766758
if (!HeapTupleIsValid(pg_class_tuple))
767-
{
768-
MemoryContextSwitchTo(oldcxt);
769-
770759
returnNULL;
771-
}
772760

773761
/* ----------------
774762
*get information from the pg_class_tuple
@@ -781,8 +769,10 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo,
781769
/* ----------------
782770
*allocate storage for the relation descriptor,
783771
*initialize relation->rd_rel and get the access method id.
772+
*The storage is allocated in memory context CacheCxt.
784773
* ----------------
785774
*/
775+
oldcxt=MemoryContextSwitchTo((MemoryContext)CacheCxt);
786776
relation=AllocateRelationDesc(oldrelation,natts,relp);
787777
relam=relation->rd_rel->relam;
788778

@@ -866,15 +856,15 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo,
866856
*/
867857
RelationCacheInsert(relation);
868858

859+
MemoryContextSwitchTo(oldcxt);
860+
869861
/* -------------------
870862
*free the memory allocated for pg_class_tuple
871863
*and for lock data pointed to by pg_class_tuple
872864
* -------------------
873865
*/
874866
heap_freetuple(pg_class_tuple);
875867

876-
MemoryContextSwitchTo(oldcxt);
877-
878868
returnrelation;
879869
}
880870

@@ -1764,7 +1754,6 @@ AttrDefaultFetch(Relation relation)
17641754
ndef-found,RelationGetRelationName(relation));
17651755

17661756
index_endscan(sd);
1767-
pfree(sd);
17681757
index_close(irel);
17691758
heap_close(adrel,AccessShareLock);
17701759
}
@@ -1837,7 +1826,6 @@ RelCheckFetch(Relation relation)
18371826
ncheck-found,RelationGetRelationName(relation));
18381827

18391828
index_endscan(sd);
1840-
pfree(sd);
18411829
index_close(irel);
18421830
heap_close(rcrel,AccessShareLock);
18431831
}

‎src/include/access/genam.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: genam.h,v 1.20 1999/07/16 17:07:25 momjian Exp $
9+
* $Id: genam.h,v 1.21 1999/12/30 05:05:13 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -47,5 +47,6 @@ extern Datum GetIndexValue(HeapTuple tuple, TupleDesc hTupDesc,
4747
/* in genam.c */
4848
externIndexScanDescRelationGetIndexScan(Relationrelation,boolscanFromEnd,
4949
uint16numberOfKeys,ScanKeykey);
50+
externvoidIndexScanEnd(IndexScanDescscan);
5051

5152
#endif/* GENAM_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp