Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r8Lvu-009Rwt-0F for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2023 14:52:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r8Lvs-00BApx-Fb for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2023 14:52:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r8Lvs-00BApp-3v for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2023 14:52:56 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r8Lvo-009BeO-Az for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2023 14:52:54 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a013d22effcso922294266b.2 for ; Wed, 29 Nov 2023 06:52:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1701269570; x=1701874370; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=JTkmi4bqfdSc8G7P7BousPSnXv/YUm1cOz4a2rle4Qw=; b=IAc5/UEwSevzRvfQqrWAY9xb6Hw2+PV3bBHWMx+X8b6w5gHAZIvytWQ+DITAa5e2EX a0zVeaAtHuE9Gv/IZiYSVCFVbAi4JzWsf1livl0TgQnF7uCoPtr7jW9ZhqqR1NgR+Spr 2wUBJAJqCJzAnYkpVnYOsLlZHIEEcd60lkBYvuFLkzjB7o5sFfshcRXMCKSGVrABfRGT Fe1dgKBTHiBIWkWuA7XAU7f6ZnQT2wXfs3Nl+MLSJUmi0COyQpT4Bkm21x8VuP0R/uoP j6uoPyAsTz71jRf9slyiSBZRGv6XhSJNJdg7lHLJyFy77OMT7+uEL0Mjb+gvxbQ/2iS1 nIAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701269570; x=1701874370; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JTkmi4bqfdSc8G7P7BousPSnXv/YUm1cOz4a2rle4Qw=; b=qS4JL+P0tG4I4rCvI+dXwwpZOConWjkbNw6Oql5KFANcMLpYjSlrAckI9RQS5r2voI QyHHVRFmm9kLUxheLqUrDP6BTHff9LASoW0FAuffUpZqnqinuN8bCYtgA+DHT3ngSnCk Tbb30QIu3dQXsWet6UztyZ0G5Rh+qRk5zmz9rfcTArBa3bs2lUpUZUtpopn8ZcSoXzjg YjUjBM2PDpThhpXSrYxA61az1KWqtEk5AMakhWeOPm9uAEfIXMmU2kuPWgVm6g5W19CX /x77Ln8wcY2Al9iLavBTkw/sZeccxu6kd+EjJbol9Q0wAxjqcFThbPCtITxte/pGOHQM p+iw== X-Gm-Message-State: AOJu0YzsSmXMuv3AV5lYm6teSNPIQhlcYG+V8l+ommC9bSPGTTEBt12S msAfPRNF4swKeTDBYfmVxhHfSg== X-Google-Smtp-Source: AGHT+IHWONVQvkaCQwJKAXZxEGmx7phB5FSZ1+FE+GpOH5RDKaAU5UtjvdROwr6Nzndo4wiNzrJP2A== X-Received: by 2002:a17:907:d407:b0:a02:ad84:3ab3 with SMTP id vi7-20020a170907d40700b00a02ad843ab3mr13388267ejc.44.1701269570341; Wed, 29 Nov 2023 06:52:50 -0800 (PST) Received: from [10.137.0.17] (srv1.mobissw.com. [89.235.0.226]) by smtp.gmail.com with ESMTPSA id d14-20020a1709067f0e00b009fcf829d84csm7918597ejr.169.2023.11.29.06.52.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Nov 2023 06:52:50 -0800 (PST) Message-ID: Date: Wed, 29 Nov 2023 15:52:48 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: Parallel CREATE INDEX for BRIN indexes Content-Language: en-US To: Matthias van de Meent Cc: PostgreSQL Hackers References: <2d7c64de-716e-346e-b01e-03db0c2bc5ac@enterprisedb.com> <2e86059f-4004-eec1-2bc1-9e7fd119c9e2@enterprisedb.com> <5384d42e-1961-c2fd-1955-1a5a99fa1234@enterprisedb.com> <9a646c51-a4c3-ce34-cccd-99d00fd1dc6a@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/29/23 15:42, Matthias van de Meent wrote: > On Tue, 28 Nov 2023 at 18:59, Tomas Vondra > wrote: >> >> On 11/28/23 16:39, Matthias van de Meent wrote: >>> On Thu, 23 Nov 2023 at 14:35, Tomas Vondra >>> 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. > > Correct, but it is also is (or should be) assumed that union_tuple > will be called several times in the same context to fix repeat > concurrent updates. Presumably, that only happens rarely, but it's > something that should be kept in mind regardless. > In theory, yes. But union_tuples() is used only in summarize_range(), and that only processes a single page range. >> 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. > > Looks good. > > This also made me think a bit more about how we're working with the > tuples. With your latest patch, we always deserialize and re-serialize > the sorted brin tuples, just in case the next tuple will also be a > BRIN tuple of the same page range. Could we save some of that > deserialization time by optimistically expecting that we're not going > to need to merge the tuple and only store a local copy of it locally? > See attached 0002; this saves some cycles in common cases. > Good idea! > The v20231128 version of the patchset (as squashed, attached v5-0001) > looks good to me. > Cool. I'll put this through a bit more stress testing, and then I'll get it pushed. Thanks for the reviews! -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company