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

Commit6aa2bdf

Browse files
committed
Initialize the entryRes array between each call to triConsistent.
The shimTriConstistentFn, which calls the opclass's consistent function withall combinations of TRUE/FALSE for any MAYBE argument, modifies the entryResarray passed by the caller. Change startScanKey to re-initialize it betweeneach call to accommodate that.It's actually a bad habit by shimTriConsistentFn to modify its argument. Butthe only caller that doesn't already re-initialize the entryRes array wasstartScanKey, and it's easy for startScanKey to do so. Add a comment toshimTriConsistentFn about that.Note: this does not give a free pass to opclass-provided consistentfunctions to modify the entryRes argument; shimTriConsistent assumes thatthey don't, even though it does it itself.While at it, refactor startScanKey to allocate the requiredEntries andadditionalEntries after it knows exactly how large they need to be. Saves alittle bit of memory, and looks nicer anyway.Per complaint by Tom Lane, buildfarm and the pg_trgm regression test.
1 parentdbc649f commit6aa2bdf

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ startScanKey(GinState *ginstate, GinScanOpaque so, GinScanKey key)
445445
{
446446
MemoryContextoldCtx=CurrentMemoryContext;
447447
inti;
448+
intj;
448449
int*entryIndexes;
449450

450451
ItemPointerSetMin(&key->curItem);
@@ -472,42 +473,51 @@ startScanKey(GinState *ginstate, GinScanOpaque so, GinScanKey key)
472473
* entries can form a match, without any items from the required set. The
473474
* rest go to the additional set.
474475
*/
475-
key->requiredEntries=palloc(key->nentries*sizeof(GinScanEntry));
476-
key->additionalEntries=palloc(key->nentries*sizeof(GinScanEntry));
477-
key->nrequired=key->nadditional=0;
478-
479476
if (key->nentries>1)
480477
{
481478
MemoryContextSwitchTo(so->tempCtx);
482479

483480
entryIndexes= (int*)palloc(sizeof(int)*key->nentries);
484481
for (i=0;i<key->nentries;i++)
485482
entryIndexes[i]=i;
486-
487483
qsort_arg(entryIndexes,key->nentries,sizeof(int),
488484
entryIndexByFrequencyCmp,key);
489485

490-
for (i=0;i<key->nentries;i++)
491-
key->entryRes[i]=GIN_MAYBE;
492-
493-
for (i=0;i<key->nentries;i++)
486+
for (i=0;i<key->nentries-1;i++)
494487
{
495-
key->requiredEntries[key->nrequired++]=key->scanEntry[entryIndexes[i]];
496-
key->entryRes[entryIndexes[i]]=GIN_FALSE;
488+
/* Pass all entries <= i as FALSE, and the rest as MAYBE */
489+
for (j=0;j <=i;j++)
490+
key->entryRes[entryIndexes[j]]=GIN_FALSE;
491+
for (j=i+1;j<key->nentries;j++)
492+
key->entryRes[entryIndexes[j]]=GIN_MAYBE;
497493

498494
if (key->triConsistentFn(key)==GIN_FALSE)
499495
break;
500496
}
501-
for (i=i+1;i<key->nentries;i++)
502-
key->additionalEntries[key->nadditional++]=key->scanEntry[entryIndexes[i]];
497+
/* i is now the last required entry. */
503498

504-
/* clean up after consistentFn calls (also frees entryIndexes) */
505499
MemoryContextSwitchTo(oldCtx);
500+
501+
key->nrequired=i+1;
502+
key->nadditional=key->nentries-key->nrequired;
503+
key->requiredEntries=palloc(key->nrequired*sizeof(GinScanEntry));
504+
key->additionalEntries=palloc(key->nadditional*sizeof(GinScanEntry));
505+
506+
j=0;
507+
for (i=0;i<key->nrequired;i++)
508+
key->requiredEntries[i]=key->scanEntry[entryIndexes[j++]];
509+
for (i=0;i<key->nadditional;i++)
510+
key->additionalEntries[i]=key->scanEntry[entryIndexes[j++]];
511+
512+
/* clean up after consistentFn calls (also frees entryIndexes) */
506513
MemoryContextReset(so->tempCtx);
507514
}
508515
else
509516
{
510-
key->requiredEntries[key->nrequired++]=key->scanEntry[0];
517+
key->nrequired=1;
518+
key->nadditional=0;
519+
key->requiredEntries=palloc(1*sizeof(GinScanEntry));
520+
key->requiredEntries[0]=key->scanEntry[0];
511521
}
512522
}
513523

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ normalBoolConsistentFn(GinScanKey key)
9090
* combination, that's the overall result. Otherwise, return MAYBE. Testing
9191
* every combination is O(n^2), so this is only feasible for a small number of
9292
* MAYBE inputs.
93+
*
94+
* NB: This function modifies the key->entryRes array!
9395
*/
9496
staticGinLogicValue
9597
shimTriConsistentFn(GinScanKeykey)
@@ -98,7 +100,7 @@ shimTriConsistentFn(GinScanKey key)
98100
intmaybeEntries[MAX_MAYBE_ENTRIES];
99101
inti;
100102
boolboolResult;
101-
boolrecheck=0;
103+
boolrecheck=false;
102104
GinLogicValuecurResult;
103105

104106
/*
@@ -124,7 +126,7 @@ shimTriConsistentFn(GinScanKey key)
124126
if (nmaybe==0)
125127
returnnormalBoolConsistentFn(key);
126128

127-
/*Try the consistent function with the maybe-inputs setboth ways */
129+
/*First call consistent function withallthe maybe-inputs setFALSE */
128130
for (i=0;i<nmaybe;i++)
129131
key->entryRes[maybeEntries[i]]=GIN_FALSE;
130132
curResult=normalBoolConsistentFn(key);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp