- Notifications
You must be signed in to change notification settings - Fork4.9k
Commit0f21db3
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: 131 parente33969a commit0f21db3
2 files changed
+12
-5
lines changedLines changed: 6 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
558 | 558 |
| |
559 | 559 |
| |
560 | 560 |
| |
| 561 | + | |
| 562 | + | |
561 | 563 |
| |
562 | 564 |
| |
563 | 565 |
| |
564 |
| - | |
565 |
| - | |
566 |
| - | |
567 |
| - | |
| 566 | + | |
568 | 567 |
| |
569 | 568 |
| |
570 | 569 |
| |
| 570 | + | |
| 571 | + | |
| 572 | + | |
571 | 573 |
| |
572 | 574 |
| |
573 | 575 |
| |
|
Lines changed: 6 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
68 | 68 |
| |
69 | 69 |
| |
70 | 70 |
| |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
71 | 76 |
| |
72 |
| - | |
| 77 | + | |
73 | 78 |
| |
74 | 79 |
| |
75 | 80 |
| |
|
0 commit comments
Comments
(0)