Re: Parallel CREATE INDEX for BRIN indexes
Tomas Vondra <tomas.vondra@enterprisedb.com>
From: Tomas Vondra <tomas.vondra@enterprisedb.com>
To: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2023-11-28T17:59:15Z
Lists: pgsql-hackers
Attachments
- v20231128-0001-parallel-CREATE-INDEX-for-BRIN-v3.patch (text/x-patch) patch v20231128-0001
- v20231128-0002-add-docs-for-amcanbuildparallel.patch (text/x-patch) patch v20231128-0002
- v20231128-0003-use-per-range-memory-context-for-merging-i.patch (text/x-patch) patch v20231128-0003
- v20231128-0004-pgindent.patch (text/x-patch) patch v20231128-0004
On 11/28/23 16:39, Matthias van de Meent wrote: > On Thu, 23 Nov 2023 at 14:35, Tomas Vondra > <tomas.vondra@enterprisedb.com> wrote: >> On 11/23/23 13:33, Matthias van de Meent wrote: >>> The union operator may leak (lots of) memory, so I think it makes >>> sense to keep a context around that can be reset after we've extracted >>> the merge result. >>> >> >> But does the current code actually achieve that? It does create a "brin >> union" context, but then it only does this: >> >> /* Use our own memory context to avoid retail pfree */ >> cxt = AllocSetContextCreate(CurrentMemoryContext, >> "brin union", >> ALLOCSET_DEFAULT_SIZES); >> oldcxt = MemoryContextSwitchTo(cxt); >> db = brin_deform_tuple(bdesc, b, NULL); >> MemoryContextSwitchTo(oldcxt); >> >> Surely that does not limit the amount of memory used by the actual union >> functions in any way? > > Oh, yes, of course. For some reason I thought that covered the calls > to the union operator function too, but it indeed only covers > deserialization. I do think it is still worthwhile to not do the > create/delete cycle, but won't hold the patch back for that. > I think the union_tuples() changes are better left for a separate patch. >>>> However, I don't think the number of union_tuples calls is likely to be >>>> very high, especially for large tables. Because we split the table into >>>> 2048 chunks, and then cap the chunk size by 8192. For large tables >>>> (where this matters) we're likely close to 8192. >>> >>> I agree that the merging part of the index creation is the last part, >>> and usually has no high impact on the total performance of the reindex >>> operation, but in memory-constrained environments releasing and then >>> requesting the same chunk of memory over and over again just isn't >>> great. >> >> OK, I'll take a look at the scratch context you suggested. >> >> My point however was we won't actually do that very often, because on >> large tables the BRIN ranges are likely smaller than the parallel scan >> chunk size, so few overlaps. OTOH if the table is small, or if the BRIN >> ranges are large, there'll be few of them. > > That's true, so maybe I'm concerned about something that amounts to > only marginal gains. > However, after thinking about this a bit more, I think we actually do need to do something about the memory management when merging tuples. AFAIK the general assumption was that union_tuple() only runs for a single range, and then the whole context gets freed. But the way the merging was implemented, it all runs in a single context. And while a single union_tuple() may not need a lot memory, in total it may be annoying. I just added a palloc(1MB) into union_tuples and ended up with ~160MB allocated in the PortalContext on just 2GB table. In practice the memory will grow more slowly, but not great :-/ The attached 0003 patch adds a memory context that's reset after producing a merged BRIN tuple for each page range. > I noticed that the v4 patch doesn't yet update the documentation in > indexam.sgml with am->amcanbuildparallel. Should be fixed by 0002. I decided to add a simple note to ambuild(), not sure if something more is needed. > Once that is included and reviewed I think this will be ready, unless > you want to address any of my comments upthread (that I marked with > 'not in this patch') in this patch. > Thanks. I believe the attached version addresses it. There's also 0004 with some indentation tweaks per pgindent. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company
Commits
-
Remove incidental md5() function use from test
- 47b47a5617fa 17.0 landed
- 95b856de23c7 18.0 landed
-
Cleanup parallel BRIN index build code
- 0c2f5552d5d4 17.0 landed
-
Stabilize test of BRIN parallel create
- 0bd4b0689ba1 17.0 landed
- 4d916dd876c3 17.0 landed
-
Revert "Stabilize test of BRIN parallel create"
- a89cd7bfcdbb 17.0 landed
-
Add regression test for BRIN parallel builds
- 8225c2fd40cd 17.0 landed
-
Use the correct PG_DETOAST_DATUM macro in BRIN
- 8cea358b128f 16.3 landed
- bb616ed3e6a2 17.0 landed
-
Update nbits_set in brin_bloom_union
- ad23af83dad3 14.12 landed
- 3cd4135119be 15.7 landed
- ccd8f0fa1e54 16.3 landed
- 2f20ced1eb53 17.0 landed
-
Fix parallel BRIN builds with synchronized scans
- cb44a8345e7c 17.0 landed
-
Allow parallel CREATE INDEX for BRIN indexes
- b43757171470 17.0 landed
-
Add empty BRIN ranges during CREATE INDEX
- dae761a87eda 17.0 landed