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

Commitc6e394c

Browse files
committed
Small improvements for allocation logic in ginHeapTupleFastCollect().
Avoid repetitive calls to repalloc() when the required size of thecollector array grows more than 2x in one call. Also ensure that thearray size is a power of 2 (since palloc will probably consume a powerof 2 anyway) and doesn't start out very small (which'd likely just leadto extra repallocs).David Rowley, tweaked a bit by meDiscussion:https://postgr.es/m/CAKJS1f8vn-iSBE8PKeVHrnhvyjRNYCxguPFFY08QLYmjWG9hPQ@mail.gmail.com
1 parent6399242 commitc6e394c

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

‎src/backend/access/gin/ginfast.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,18 +486,41 @@ ginHeapTupleFastCollect(GinState *ginstate,
486486
entries=ginExtractEntries(ginstate,attnum,value,isNull,
487487
&nentries,&categories);
488488

489+
/*
490+
* Protect against integer overflow in allocation calculations
491+
*/
492+
if (nentries<0||
493+
collector->ntuples+nentries>MaxAllocSize /sizeof(IndexTuple))
494+
elog(ERROR,"too many entries for GIN index");
495+
489496
/*
490497
* Allocate/reallocate memory for storing collected tuples
491498
*/
492499
if (collector->tuples==NULL)
493500
{
494-
collector->lentuples=nentries*ginstate->origTupdesc->natts;
501+
/*
502+
* Determine the number of elements to allocate in the tuples array
503+
* initially. Make it a power of 2 to avoid wasting memory when
504+
* resizing (since palloc likes powers of 2).
505+
*/
506+
collector->lentuples=16;
507+
while (collector->lentuples<nentries)
508+
collector->lentuples *=2;
509+
495510
collector->tuples= (IndexTuple*)palloc(sizeof(IndexTuple)*collector->lentuples);
496511
}
497-
498-
while (collector->ntuples+nentries>collector->lentuples)
512+
elseif (collector->lentuples<collector->ntuples+nentries)
499513
{
500-
collector->lentuples *=2;
514+
/*
515+
* Advance lentuples to the next suitable power of 2. This won't
516+
* overflow, though we could get to a value that exceeds
517+
* MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc.
518+
*/
519+
do
520+
{
521+
collector->lentuples *=2;
522+
}while (collector->lentuples<collector->ntuples+nentries);
523+
501524
collector->tuples= (IndexTuple*)repalloc(collector->tuples,
502525
sizeof(IndexTuple)*collector->lentuples);
503526
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp