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

Commit696b0c5

Browse files
committed
Fix memory leak in repeated SPGIST index scans.
spgendscan neglected to pfree all the memory allocated by spgbeginscan.It's possible to get away with that in most normal queries, since thememory is allocated in the executor's per-query context which is aboutto get deleted anyway; but it causes severe memory leakage duringcreation or filling of large exclusion-constraint indexes.Also, document that amendscan is supposed to free what ambeginscanallocates. The docs' lack of clarity on that point probably caused thisbug to begin with. (There is discussion of changing that API spec goingforward, but I don't think it'd be appropriate for the back branches.)Per report from Bruno Wolff. It's been like this since the beginning,so back-patch to all active branches.In HEAD, also fix an independent leak caused by commit2a63683(allocating memory during spgrescan instead of spgbeginscan, whichmight be all right if it got cleaned up, but it didn't). And do a bitof code beautification on that commit, too.Discussion:https://postgr.es/m/20181024012314.GA27428@wolff.to
1 parentc4ab62f commit696b0c5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

‎doc/src/sgml/indexam.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ amendscan (IndexScanDesc scan);
614614
</programlisting>
615615
End a scan and release resources. The <literal>scan</literal> struct itself
616616
should not be freed, but any locks or pins taken internally by the
617-
access method must be released.
617+
access method must be released, as well as any other memory allocated
618+
by <function>ambeginscan</function> and other scan-related functions.
618619
</para>
619620

620621
<para>

‎src/backend/access/spgist/spgscan.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,31 @@ spgbeginscan(Relation rel, int keysz, int orderbysz)
294294
/* Set up indexTupDesc and xs_hitupdesc in case it's an index-only scan */
295295
so->indexTupDesc=scan->xs_hitupdesc=RelationGetDescr(rel);
296296

297+
/* Allocate various arrays needed for order-by scans */
297298
if (scan->numberOfOrderBys>0)
298299
{
299-
so->zeroDistances=palloc(sizeof(double)*scan->numberOfOrderBys);
300-
so->infDistances=palloc(sizeof(double)*scan->numberOfOrderBys);
300+
/* This will be filled in spgrescan, but allocate the space here */
301+
so->orderByTypes= (Oid*)
302+
palloc(sizeof(Oid)*scan->numberOfOrderBys);
303+
304+
/* These arrays have constant contents, so we can fill them now */
305+
so->zeroDistances= (double*)
306+
palloc(sizeof(double)*scan->numberOfOrderBys);
307+
so->infDistances= (double*)
308+
palloc(sizeof(double)*scan->numberOfOrderBys);
301309

302310
for (i=0;i<scan->numberOfOrderBys;i++)
303311
{
304312
so->zeroDistances[i]=0.0;
305313
so->infDistances[i]=get_float8_infinity();
306314
}
307315

308-
scan->xs_orderbyvals=palloc0(sizeof(Datum)*scan->numberOfOrderBys);
309-
scan->xs_orderbynulls=palloc(sizeof(bool)*scan->numberOfOrderBys);
310-
memset(scan->xs_orderbynulls, true,sizeof(bool)*scan->numberOfOrderBys);
316+
scan->xs_orderbyvals= (Datum*)
317+
palloc0(sizeof(Datum)*scan->numberOfOrderBys);
318+
scan->xs_orderbynulls= (bool*)
319+
palloc(sizeof(bool)*scan->numberOfOrderBys);
320+
memset(scan->xs_orderbynulls, true,
321+
sizeof(bool)*scan->numberOfOrderBys);
311322
}
312323

313324
fmgr_info_copy(&so->innerConsistentFn,
@@ -336,15 +347,14 @@ spgrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
336347
memmove(scan->keyData,scankey,
337348
scan->numberOfKeys*sizeof(ScanKeyData));
338349

350+
/* initialize order-by data if needed */
339351
if (orderbys&&scan->numberOfOrderBys>0)
340352
{
341353
inti;
342354

343355
memmove(scan->orderByData,orderbys,
344356
scan->numberOfOrderBys*sizeof(ScanKeyData));
345357

346-
so->orderByTypes= (Oid*)palloc(sizeof(Oid)*scan->numberOfOrderBys);
347-
348358
for (i=0;i<scan->numberOfOrderBys;i++)
349359
{
350360
ScanKeyskey=&scan->orderByData[i];
@@ -380,11 +390,22 @@ spgendscan(IndexScanDesc scan)
380390
MemoryContextDelete(so->tempCxt);
381391
MemoryContextDelete(so->traversalCxt);
382392

393+
if (so->keyData)
394+
pfree(so->keyData);
395+
396+
if (so->state.deadTupleStorage)
397+
pfree(so->state.deadTupleStorage);
398+
383399
if (scan->numberOfOrderBys>0)
384400
{
401+
pfree(so->orderByTypes);
385402
pfree(so->zeroDistances);
386403
pfree(so->infDistances);
404+
pfree(scan->xs_orderbyvals);
405+
pfree(scan->xs_orderbynulls);
387406
}
407+
408+
pfree(so);
388409
}
389410

390411
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp