Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nAS1Q-0005kv-6k for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Jan 2022 07:38:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nAS1P-0006vO-1M for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Jan 2022 07:38:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nAS1O-0006vE-MY for pgsql-hackers@lists.postgresql.org; Thu, 20 Jan 2022 07:38:14 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nAS1I-0005JU-76 for pgsql-hackers@lists.postgresql.org; Thu, 20 Jan 2022 07:38:13 +0000 Received: by mail-wm1-x32d.google.com with SMTP id w26so10190022wmi.0 for ; Wed, 19 Jan 2022 23:38:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=from:to:cc:subject:date:message-id:organization:in-reply-to :references:mime-version:content-transfer-encoding; bh=mc+GEh46ok2luXzFAXRjwZ108fngDaJaEcPQ5UhJBio=; b=K2X3WiJd41/LiiKf1c/EnAJDI342EiH7oWS5l38LaenKqSeMOO5nOMTZnvxPmIRyL+ lSwaK1invvhwfGdmfO3UYKgre83BBH9RJ5guI1wWv7/IWKL4zYwHQCLCYLIcBWHS90KM H9EOLLC7uOI5uU+5ufsRDIMX07WbdpF36YK5E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:organization :in-reply-to:references:mime-version:content-transfer-encoding; bh=mc+GEh46ok2luXzFAXRjwZ108fngDaJaEcPQ5UhJBio=; b=K36iVnULyzccqL/omxHp5nvp3lYgr1ncjm7kFDemF1BEJDaLM6JTFqjo1Oqwv+0USo Zg6xr3zao8rN00vrM8uZYGmko0AGFxmq4L8R1jsb9BPjAfkxajUsrnvhmkDcw2wpPGqj +jsMNPjjfsYvj/7CPePBF3aEUrSUvdxnwYHf6tRlH4A0d6rFGNZ1HSrkZ03qKLIgZgQY TK/um0hzdpBLf27PoqG4O7A5M40dQJbe8DhjSpBCOBdNV8CfQpKVP7iPd9OcU81VWTe9 bHsas/I0SwcxYGrcrMP7BFe4WcK3QYXvGHH7+Xk1hRqG1pKAS/mEFCDa1c8nMJ0CJnv5 ubLA== X-Gm-Message-State: AOAM533Hi+xB5ut9SuditF8Oc4DSg8arCPFM63bM45I2q/rsMuZMObEi El801Sjd/rSaE2xgkT3AqU03BQ== X-Google-Smtp-Source: ABdhPJyqsEQO19lzKCrsXjjqbDpt1xwyZAewOWz2g3YXpWhNL2c25Zb0h/A0iSd+TBfkCrKyFAkVGQ== X-Received: by 2002:adf:e5cc:: with SMTP id a12mr4371677wrn.121.1642664285232; Wed, 19 Jan 2022 23:38:05 -0800 (PST) Received: from aivenronan.localnet (static-176-158-121-96.ftth.abo.bbox.fr. [176.158.121.96]) by smtp.gmail.com with ESMTPSA id u7sm1540964wmc.11.2022.01.19.23.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jan 2022 23:38:04 -0800 (PST) From: Ronan Dunklau To: Julien Rouhaud , David Rowley Cc: Tomas Vondra , PostgreSQL Developers , Andres Freund , Tomas Vondra Subject: Re: Use generation context to speed up tuplesorts Date: Thu, 20 Jan 2022 08:36:46 +0100 Message-ID: <4377612.LvFx2qVVIh@aivenronan> Organization: aiven In-Reply-To: References: <20220118064539.o3gc257z2vavb66e@jrouhaud> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Le jeudi 20 janvier 2022, 02:31:15 CET David Rowley a =E9crit : > On Tue, 18 Jan 2022 at 19:45, Julien Rouhaud wrote: > > I'm also a bit confused about which patch(es) should actually be review= ed > > in that thread. My understanding is that the original patch might not = be > > relevant anymore but I might be wrong. The CF entry should probably be > > updated to clarify that, with an annotation/ title change / author upda= te > > or something. > >=20 > > In the meantime I will switch the entry to Waiting on Author. >=20 > I think what's going on here is a bit confusing. There's quite a few > patches on here that aim to do very different things. >=20 > The patch I originally proposed was just to make tuplesort use > generation memory contexts. This helps speed up tuplesort because > generation contexts allow palloc() to be faster than the standard > allocator. Additionally, there's no power-of-2 wastage with generation > contexts like there is with the standard allocator. This helps fit > more tuples on fewer CPU cache lines. >=20 > I believe Andres then mentioned that the fixed block size for > generation contexts might become a problem. With the standard > allocator the actual size of the underlying malloc() grows up until a > maximum size. With generation contexts this is always the same, so we > could end up with a very large number of blocks which means lots of > small mallocs which could end up slow. Tomas then posted a series of > patches to address this. >=20 > I then posted another patch that has the planner make a choice on the > size of the blocks to use for the generation context based on the > tuple width and number of tuple estimates. My hope there was to > improve performance further by making a better choice for how large to > make the blocks in the first place. This reduces the need for Tomas' > patch, but does not eliminate the need for it. >=20 > As of now, I still believe we'll need Tomas' patches to allow the > block size to grow up to a maximum size. I think those patches are > likely needed before we think about making tuplesort use generation > contexts. The reason I believe this is that we don't always have good > estimates for the number of tuples we're going to sort. nodeSort.c is > fairly easy as it just fetches tuples once and then sorts them. The > use of tuplesort for nodeIncrementalSort.c is much more complex as > we'll likely be performing a series of smaller sorts which are much > harder to get size estimates for. This means there's likely no magic > block size that we can use for the generation context that's used to > store the tuples in tuplesort. This is also the case for order by > aggregates and probably most other usages of tuplesort. >=20 You left out the discussion I started about glibc's malloc specific behavio= ur. I tried to demonstrate that a big part of the performance gain you were see= ing=20 were not in fact due to our allocator by itself, but by the way different b= lock=20 sizes allocations interact with this malloc implementation and how it handl= es=20 releasing memory back to the system. I also tried to show that we can give= =20 DBAs more control over how much memory to "waste" as the price for faster=20 allocations. I agree that the generation context memory manager is useful in the tupleso= rt=20 context, if only for the fact that we fall back to disk less quickly due to= =20 the reduced wastage of memory, but I would be wary of drawing conclusions t= oo=20 quickly based on a specific malloc implementation which shows threshold eff= ects,=20 which are compounded by our growing block sizes code in general. I have on my TODO list to run the same kind of benchmarks using jemalloc, t= o=20 better isolate the performance gains expected from the generation allocator= =20 itself from the side effect of avoiding glibc's pattern of releasing memory= =20 back to the kernel too quickly.=20 Julien, sorry for the confusion of adding that discussion and those patche= s=20 to the same thread, but I don't really see a way of dissociating those two= =20 aspects. =2D-=20 Ronan Dunklau