Re: Should pg 11 use a lot more memory building an spgist index?
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alvaro Herrera <alvherre@2ndquadrant.com>
Cc: Alexander Korotkov <akorotkov@postgresql.org>,
Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>,
Bruno Wolff III <bruno@wolff.to>, pgsql-hackers@lists.postgresql.org
Date: 2018-10-29T19:48:37Z
Lists: pgsql-hackers
Attachments
- fix-spgist-memory-leaks-1.patch (text/x-diff) patch
I wrote: > Alvaro Herrera <alvherre@2ndquadrant.com> writes: >> How about modifying SysScanDescData to have a memory context member, >> which is created by systable_beginscan and destroyed by endscan? > I think it would still have problems, in that it would affect in which > context index_getnext delivers its output. We could reasonably make > these sorts of changes in places where the entire index_beginscan/ > index_getnext/index_endscan call structure is in one place, but that > doesn't apply for the systable functions. Actually, after looking more closely, index_getnext doesn't return a palloc'd object anyway, or at least not one that the caller is responsible for managing. So maybe that could work. I was confused about why the memory leak in Bruno's example is so much larger in HEAD than v11; spgbeginscan does allocate more stuff than before, but only if numberOfOrderBys > 0, which surely doesn't apply for the exclusion-check code path. Eventually I realized that the difference is because commit 2a6368343 made SpGistScanOpaqueData a good deal larger than it had been (10080 vs 6264 bytes, on my x86_64 box), so it was just the failure to pfree that struct that accounted for the bigger leak. There's another issue with 2a6368343, though: it added a couple of fmgr_info_copy calls to spgbeginscan. I don't understand why it'd be a good idea to do that rather than using the FmgrInfos in the index's relcache entry directly. Making a copy every time risks a memory leak, because spgendscan has no way to free any fn_extra data that gets allocated by the called function; and by the same token it's inefficient, because the function has no way to cache fn_extra data across queries. If we consider only the leak aspect, this coding presents a reason why we should try to change things as Alvaro suggests. However, because of the poor-caching aspect, I think this code is pretty broken anyway, and once we fix it the issue is much less clear-cut. (Looking around, it seems like we're very schizophrenic about whether to copy index relcache support function FmgrInfos or just use them directly. But nbtree and hash seem to always do the latter, so I think it's probably reasonable to standardize on that.) Anyway, the attached proposed patch for HEAD makes Bruno's problem go away, and it also fixes an unrelated leak introduced by 2a6368343 because it reallocates so->orderByTypes each time through spgrescan. I think we should apply this, and suitable subsets in the back branches, to fix the immediate problem. Then we can work at leisure on a more invasive HEAD-only patch, if anyone is excited about that. regards, tom lane
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix memory leak in repeated SPGIST index scans.
- 696b0c5fd0a8 12.0 landed
- 95015b1f8e8e 9.4.20 landed
- 92e371f9b143 10.6 landed
- 82dd1c271492 9.3.25 landed
- 558571afc7c4 9.6.11 landed
- 156a737a6b73 9.5.15 landed
- 2493e2c2d167 11.1 landed
-
Add support for nearest-neighbor (KNN) searches to SP-GiST
- 2a6368343ff4 12.0 cited