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 1myDSC-0006UO-Ci for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Dec 2021 13:39:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1myDSB-0004o0-3h for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Dec 2021 13:39:19 +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 1myDSA-0004nq-D5 for pgsql-hackers@lists.postgresql.org; Fri, 17 Dec 2021 13:39:18 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1myDS7-0007K5-1U for pgsql-hackers@lists.postgresql.org; Fri, 17 Dec 2021 13:39:17 +0000 Received: by mail-ed1-x532.google.com with SMTP id z9so7979060edb.5 for ; Fri, 17 Dec 2021 05:39:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=HDBa3G+ULaPDjW/nSYoqS8MfpZa2zy2cEtme0Pe4woY=; b=Qqb7DRDwWgRJvjMCVpclG0pswvSbzPBwHwZsWdNZ7685etUpBuKZBoKntncSbuZX75 kUUgL8vOltOa8jW2V/H+AssjsIkR1AnpvPkXTJfRaQWxBzUsgi7tgrm4NR5p654Yz0N4 QZUUxM2mXg3iLAwlhdBVTyXwtbA+kNSLV8+ZPw9dkIpkgd+R6g0VMymmiG4rLpe4sSzQ AenctmW33OigDrq0DxBV6O1l6PKmpNnmwwuwOaBoQG4c6ihwEjoFPpW3T7Z1McC5UMfd aWX0avytymyyHd8DIIj1i6WO2tz9BpO5QyScjJQ6q3su+wbAZcZAFVIBg1qCJYl/KM1d RdgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=HDBa3G+ULaPDjW/nSYoqS8MfpZa2zy2cEtme0Pe4woY=; b=Jj8FruQjCDFIdrMU9isJAzWnF0vqM50/yXxBu7LwBHfoVG3lI76twDfVF1I16zAbVt GP87l/CthpxQbcOLdXxyctvGQvTvf2DUvoLnjxty6nNIw/fDYj5aGgOQn8q7J9TkoD1V /9LTYODK9/3hgmgW70op0jgkEbFVXWzFPeSrOlkn4tvAX6VIhKl2kFTBTySAJidaInWk OKe/NP9lGwN6Kv6X76E4i/oA9UZ7br0eBh/fxOM0TeLLcXD3JKub34/IwO8nlFdGhEdr JZMWibJGeSwlGSatv0lCsZTnX4uCV2M+vXF89QxShEq+4/wknrwNI4Q5XPbf05mz3U8u Iz9Q== X-Gm-Message-State: AOAM53040IfK7+BfGCTXYyEuBcndbMAwKFysJSewMhqw9Sff4j5Zb2Vd V3gvlw+ZtYyeO1kXkzvZAThyiHGJWa2a99teTzb1FueP14nY6Gx9c4eJervp7RUE+TQVSB5mXdJ Xojv4JON0noue6B1wPxmPKB6exzo/fyPYLpeONSllfBbUIjUBJ4NXQPPLFL2OgOcu7aCTUfaKfv 0dzgKQAOxND3IoYKSUqbg2vdj82HQ717fiYDL6nCjIDYey3MONRFrg X-Google-Smtp-Source: ABdhPJyq7it87y0+3UfxtOxunX8aJW7A8puxMsYNW1QDUYHX8Ibd6iBPP1d7zZbMz1h3+El1KPhVTw== X-Received: by 2002:a17:906:a1d0:: with SMTP id bx16mr2561379ejb.154.1639748352853; Fri, 17 Dec 2021 05:39:12 -0800 (PST) Received: from [10.137.0.19] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id b4sm2825323ejl.129.2021.12.17.05.39.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Dec 2021 05:39:12 -0800 (PST) Message-ID: Date: Fri, 17 Dec 2021 14:39:10 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: Use generation context to speed up tuplesorts Content-Language: en-US To: Ronan Dunklau , David Rowley , pgsql-hackers@lists.postgresql.org Cc: Andres Freund , Tomas Vondra References: <36444569.XM6RcZxFsP@aivenronan> <70b689dc-7ad4-138c-2c0b-9fcc131a1aaf@enterprisedb.com> <7285172.GXAFRqVoOG@aivenronan> From: Tomas Vondra In-Reply-To: <7285172.GXAFRqVoOG@aivenronan> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/17/21 09:08, Ronan Dunklau wrote: > Le jeudi 16 décembre 2021, 18:00:56 CET Tomas Vondra a écrit : >> On 12/16/21 17:03, Ronan Dunklau wrote: >>> Le jeudi 16 décembre 2021, 11:56:15 CET Ronan Dunklau a écrit : >>>> I will follow up with a benchmark of the test sorting a table with a >>>> width >>>> varying from 1 to 32 columns. >>> >>> So please find attached another benchmark for that case. >>> >>> The 3 different patchsets tested are: >>> - master >>> - fixed (David's original patch) >>> - adjust (Thomas growing blocks patch) >> >> Presumably Thomas is me, right? > > I'm really sorry for this typo... Please accept my apologies. > Nah, no apology needed. I was just wondering if I missed some patch from Thomas Munro ... >> >>> So it looks like tuning malloc for this would be very benificial for any >>> kind of allocation, and by doing so we reduce the problems seen with the >>> growing blocks patch to next to nothing, while keeping the ability to not >>> allocate too much memory from the get go. >> >> Thanks for running those tests and investigating the glibc behavior! I >> find those results very interesting. My conclusions from this is that >> the interaction interaction between "our" allocator and the allocator in >> malloc (e.g. glibc) can be problematic. Which makes benchmarking and >> optimization somewhat tricky because code changes may trigger behavior >> change in glibc (or whatever allocator backs malloc). >> >> I think it's worth exploring if we can tune this in a reasonable way, >> but I have a couple concerns related to that: >> >> 1) I wonder how glibc-specific this is - I'd bet it applies to other >> allocators (either on another OS or just different allocator on Linux) >> too. Tweaking glibc parameters won't affect those systems, of course, >> but maybe we should allow tweaking those systems too ... > > I agree, finding their specific problems and see if we can workaround it would > be interesting. I suppose glibc's malloc is the most commonly used allocator > in production, as it is the default for most Linux distributions. > I wasn't really suggesting to investigate those other allocators in this patch - it seems like a task requiring a pretty significant amount of work/time. My point was that we should make it reasonably easy to add tweaks for those other environments, if someone is interested enough to do the legwork. >> >> 2) In fact, I wonder if different glibc versions behave differently? >> Hopefully it's not changing that much, though. Ditto kernel versions, >> but the mmap/sbrk interface is likely more stable. We can test this. > > That could be tested, yes. As a matter of fact, a commit removing the upper > limit for MALLOC_MMAP_THRESHOLD has just been committed yesterday to glibc, > which means we can service much bigger allocation without mmap. > Yeah, I noticed that commit too. Most systems stick to one glibc version, so it'll take time to reach most systems. Let's continue with just one glibc version and then maybe test other versions. > >> >> 3) If we bump the thresholds, won't that work against reusing the >> memory? I mean, if we free a whole block (from any allocator we have), >> glibc might return it to kernel, depending on mmap threshold value. It's >> not guaranteed, but increasing the malloc thresholds will make that even >> less likely. So we might just as well increase the minimum block size, >> with about the same effect, no? > > It is my understanding that malloc will try to compact memory by moving it > around. So the memory should be actually be released to the kernel at some > point. In the meantime, malloc can reuse it for our next invocation (which can > be in a different memory context on our side). > > If we increase the minimum block size, this is memory we will actually > reserve, and it will not protect us against the ramping-up behaviour: > - the first allocation of a big block may be over mmap_threshold, and serviced > by an expensive mmap > - when it's free, the threshold is doubled > - next invocation is serviced by an sbrk call > - freeing it will be above the trim threshold, and it will be returned. > > After several "big" allocations, the thresholds will raise to their maximum > values (well, it used to, I need to check what happens with that latest patch > of glibc...) > > This will typically happen several times as malloc doubles the threshold each > time. This is probably the reason quadrupling the block sizes was more > effective. > Hmmm, OK. Can we we benchmark the case with large initial block size, at least for comparison? > >> >>> I would like to try to implement some dynamic glibc malloc tuning, if that >>> is something we don't reject on principle from the get go. >> >> +1 to that > > Ok, I'll work on a patch for this and submit a new thread. > Cool! regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company