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 1r8Rbm-009t6q-A7 for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2023 20:56:34 +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 1r8Rbi-00D3E4-8X for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2023 20:56:30 +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 1r8Rbh-00D3Dv-SD for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2023 20:56:29 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r8Rba-009EHC-LE for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2023 20:56:29 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40b2fa4ec5eso1477145e9.2 for ; Wed, 29 Nov 2023 12:56:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1701291381; x=1701896181; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=XwmMNbhgc097e4zthOKtOTstlrTft0ms0Ektai3emAc=; b=AM3tLstJj3uFo06OaemqE9HG3XlwvlJA/jMyJ2pOpqFkytjPD/RurhWyBdJ1NEe0hz Izzh8kbm2om5mFqA+QMYbS+GlPBBqAeb7DXV2QfsGWqpjNKB7fAXgoKRiKMK93VgDbb5 0geFPZbWBOG1cU5/aXty6A2FVwVGzz4jrj7c36Rq8ZQvsAEvt2BcpqkYt4s1iFTo6HRC /zAL+Bit/4ELddIXh8rgr3ogQe5Zwz0l7jEuxbXXwUts3lA4iUzsD6kTbiX6jKOQRQNY kSgGm4Y3CSmlRL9Qo57Tgjk6HIIhTSAFbF+eae1DLek7PV5TpBLacIwXdkP+vkoUQeEt OMZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701291381; x=1701896181; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XwmMNbhgc097e4zthOKtOTstlrTft0ms0Ektai3emAc=; b=P60580IDiClKGR3+Y5J4NZQGr0jxIRaniZ7Q4v1acqa5lP8VqpRdv/bFPfTcUNPW1s 2EJ1xs0NKr7W6+RuuNhM4HVUgE3dZtp2+ikb+hSehPxEC+VkXo9KsY/S2qx9guVb74mB 90snwRgxxdJ9+gY+88CbDUCwDTpFwfpt9aU7cFuqtoxEUXUhrFuA3+dflmh6iA7rykqW 02j1Sfjrt/w/Iq9uggl+lZmCIVvZiIjJUWfNLJASVZsZkTdl8YCTEo9q04ZrRTMsB0ue 3bGA+cPLO7TIDwlOPnAzdai9eXdj08oLHN6Wru5B+Zb9YQu96XXbwcqYm30hCKs+EFj3 guPw== X-Gm-Message-State: AOJu0YzSJRWuO3ZwVlkZPwGKKlAXsbAz0YzSIpVHoiL4NHSeNfdgavWg l4MKCUDwRDPJpEDGCFtkLOHqjQ== X-Google-Smtp-Source: AGHT+IHWPI/lHvDCWJbL+wXPlVk1LulfDZcsISZEhfqFp0YRfuPBratLtWStjZQ/frwf/IlYXQVV2g== X-Received: by 2002:adf:ea0d:0:b0:333:1449:cce0 with SMTP id q13-20020adfea0d000000b003331449cce0mr2835596wrm.20.1701291380977; Wed, 29 Nov 2023 12:56:20 -0800 (PST) Received: from [10.137.0.17] (ip-86-49-225-170.bb.vodafone.cz. [86.49.225.170]) by smtp.gmail.com with ESMTPSA id s11-20020adfeccb000000b00332c6c5ce82sm18929930wro.94.2023.11.29.12.56.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Nov 2023 12:56:20 -0800 (PST) Message-ID: Date: Wed, 29 Nov 2023 21:56:18 +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 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> <64adb9cb-3474-e0ff-7a38-9bed4f5170e6@enterprisedb.com> Content-Language: en-US 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 21:30, Matthias van de Meent wrote: > On Wed, 29 Nov 2023 at 18:55, Tomas Vondra > wrote: >> >> On 11/29/23 15:52, Tomas Vondra wrote: >>>> ... >>>> >>>> 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! >>> >> >> FWIW there's a bug, in this part of the optimization: >> >> ------------------ >> + if (memtuple == NULL) >> + memtuple = brin_deform_tuple(state->bs_bdesc, btup, >> + memtup_holder); >> + >> union_tuples(state->bs_bdesc, memtuple, btup); >> continue; >> ------------------ >> >> The deforming should use prevbtup, otherwise union_tuples() jut combines >> two copies of the same tuple. > > Good point. There were some more issues as well, fixes are attached. > >> Which however brings me to the bigger issue with this - my stress test >> found this issue pretty quickly, but then I spent quite a bit of time >> trying to find what went wrong. I find this reworked code pretty hard to >> understand, and not necessarily because of how it's written. The problem >> is it the same loop tries to juggle multiple pieces of information with >> different lifespans, and so on. I find it really hard to reason about >> how it behaves ... > > Yeah, it'd be nice if we had a peek option for sortsupport, that'd > improve context handling. > >> I did try to measure how much it actually saves, but none of the tests I >> did actually found measurable improvement. So I'm tempted to just not >> include this part, and accept that we may deserialize some of the tuples >> unnecessarily. >> >> Did you actually observe measurable improvements in some cases? > > The improvements would mostly stem from brin indexes with multiple > (potentially compressed) by-ref types, as they go through more complex > and expensive code to deserialize, requiring separate palloc() and > memcpy() calls each. > For single-column and by-value types the improvements are expected to > be negligible, because there is no meaningful difference between > copying a single by-ref value and copying its container; the > additional work done for each tuple is marginal for those. > > For an 8-column BRIN index ((sha256((id)::text::bytea)::text), > (sha256((id+1)::text::bytea)::text), > (sha256((id+2)::text::bytea)::text), ...) instrumented with 0003 I > measured a difference of 10x less time spent in the main loop of > _brin_end_parallel, from ~30ms to 3ms when dealing with 55k 1-block > ranges. It's not a lot, but worth at least something, I guess? > It is something, but I can't really convince myself it's worth the extra code complexity. It's a somewhat extreme example, and the parallelism certainly saves much more than this. > The attached patch fixes the issue that you called out . > It also further updates _brin_end_parallel: the final 'write empty > tuples' loop is never hit and is thus removed, because if there were > any tuples in the spool we'd have filled the empty ranges at the end > of the main loop, and if there were no tuples in the spool then the > memtuple would still be at its original initialized value of 0 thus > resulting in a constant false condition. I also updated some comments. > Ah, right. I'll take a look tomorrow, but I guess I didn't realize we insert the empty ranges in the main loop, because we're already looking at the *next* summary. But I think the idea was to insert empty ranges if there's a chunk of empty ranges at the end of the table, after the last tuple the index build reads. But I'm not sure that can actually happen ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company