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

Commit0f21db3

Browse files
committed
Fix some performance issues in GIN query startup.
If a GIN index search had a lot of search keys (for example,"jsonbcol ?| array[]" with tens of thousands of array elements),both ginFillScanKey() and startScanKey() took O(N^2) time.Worse, those loops were uncancelable for lack of CHECK_FOR_INTERRUPTS.The problem in ginFillScanKey() is the brute-force search keyde-duplication done in ginFillScanEntry(). The most expedientsolution seems to be to just stop trying to de-duplicate oncethere are "too many" search keys. We could imagine working harder,say by using a sort-and-unique algorithm instead of brute forcecompare-all-the-keys. But it seems unlikely to be worth the trouble.There is no correctness issue here, since the code already allowedduplicate keys if any extra_data is present.The problem in startScanKey() is the loop that attempts to identifythe first non-required search key. In the submitted test case, thatvainly tests all the key positions, and each iteration takes O(N)time. One part of that is that it's reinitializing the entryRes[]array from scratch each time, which is entirely unnecessary giventhat the triConsistentFn isn't supposed to scribble on its input.We can easily adjust the array contents incrementally instead.The other part of it is that the triConsistentFn may itself takeO(N) time (and does in this test case). This is all extremelybrute force: in simple cases with AND or OR semantics, we couldknow without any looping whatever that all or none of the keysare required. But GIN opclasses don't have any API for exposingthat knowledge, so at least in the short run there is little tobe done about that. Put in a CHECK_FOR_INTERRUPTS so that atleast the loop is cancelable.These two changes together resolve the primary complaint thatthe test query doesn't respond promptly to cancel interrupts.Also, while they don't completely eliminate the O(N^2) behavior,they do provide quite a nice speedup for mid-sized examples.Bug: #18831Reported-by: Niek <niek.brasa@hitachienergy.com>Author: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://postgr.es/m/18831-e845ac44ebc5dd36@postgresql.orgBackpatch-through: 13
1 parente33969a commit0f21db3

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,16 +558,18 @@ startScanKey(GinState *ginstate, GinScanOpaque so, GinScanKey key)
558558
qsort_arg(entryIndexes,key->nentries,sizeof(int),
559559
entryIndexByFrequencyCmp,key);
560560

561+
for (i=1;i<key->nentries;i++)
562+
key->entryRes[entryIndexes[i]]=GIN_MAYBE;
561563
for (i=0;i<key->nentries-1;i++)
562564
{
563565
/* Pass all entries <= i as FALSE, and the rest as MAYBE */
564-
for (j=0;j <=i;j++)
565-
key->entryRes[entryIndexes[j]]=GIN_FALSE;
566-
for (j=i+1;j<key->nentries;j++)
567-
key->entryRes[entryIndexes[j]]=GIN_MAYBE;
566+
key->entryRes[entryIndexes[i]]=GIN_FALSE;
568567

569568
if (key->triConsistentFn(key)==GIN_FALSE)
570569
break;
570+
571+
/* Make this loop interruptible in case there are many keys */
572+
CHECK_FOR_INTERRUPTS();
571573
}
572574
/* i is now the last required entry. */
573575

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ ginFillScanEntry(GinScanOpaque so, OffsetNumber attnum,
6868
*
6969
* Entries with non-null extra_data are never considered identical, since
7070
* we can't know exactly what the opclass might be doing with that.
71+
*
72+
* Also, give up de-duplication once we have 100 entries. That avoids
73+
* spending O(N^2) time on probably-fruitless de-duplication of large
74+
* search-key sets. The threshold of 100 is arbitrary but matches
75+
* predtest.c's threshold for what's a large array.
7176
*/
72-
if (extra_data==NULL)
77+
if (extra_data==NULL&&so->totalentries<100)
7378
{
7479
for (i=0;i<so->totalentries;i++)
7580
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp