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 1n5ny5-0006qX-V6 for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Jan 2022 12:03:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n5ny4-0007WC-Ov for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Jan 2022 12:03:36 +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 1n5ny3-0007W3-Vx for pgsql-hackers@lists.postgresql.org; Fri, 07 Jan 2022 12:03:36 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n5ny0-0005Ff-JF for pgsql-hackers@lists.postgresql.org; Fri, 07 Jan 2022 12:03:34 +0000 Received: by mail-ed1-x535.google.com with SMTP id k15so21120007edk.13 for ; Fri, 07 Jan 2022 04:03:32 -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=dC5VC493Gb5nYZI7R9Akk8xjALnrFAjUxoZYx2VD5gE=; b=iWWdXWf0TqedyI0T+W7BmCLvYzp8LYJPUgWJY9lZTLaUEOZClZb8l0YqIbbTObiqT2 S+3fAccZDXAlrfM7XgnojgetwUcZ5t5IPGxBsa9lP3283vBMfT8fyYLPQQdgCEeUIRQv A2xili9uyD4iFmjQ29VuwQWfle8Dd8zPpmmcvZjFX0xGOY5G+nFHGbD2JV8UbHU2PRak A7cW87UlZXY1b1SXGr7nHsYTvSC8MmVojBLDDgeKx+VXL9MqeFJMwJJBwh9dpYlpRZ4t xdh8D0akpoga2Uf9QQvHlmdc3rsPQ5iv+Q8hf9akDDZIoddAVPhzAR7JXy78lB7PFnAO xfVw== 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=dC5VC493Gb5nYZI7R9Akk8xjALnrFAjUxoZYx2VD5gE=; b=g6V3V8kZ5/JNb78ZV7ydvQw7Zg1c1VJv80zkacyjksAarfnuNINupZlVeaPN8/UYNM CROLo0iWrU/wbBzTjsbcETLiEKRml/HvyVNvBbZYuNZr0fxaedvnadZhO/hM4O4P51Y1 v42ucLGO4zdCvEflPY855FIF5qvMvEGTaJFQ03t4kMwNIYmDDMm9H+9yhf1yMnjEpGW/ 7WxflF9FYlxI5D+th3ciA4O0PlUyEi7alrju/HIfDOe3xEWVL2HJ9goVW9evoew60szQ Xw6cZNSA/EPpnAr/A4tp7NMZq1aB3CsWjBLCG+A5sgwy9I1ByavEELC9VfFlj491HRy+ Su0A== X-Gm-Message-State: AOAM533av0B0UHpalxr/lyOszfmbPyb8IwD0BFy2s8F9d4YlBiqyUjU5 U3mrqJSx+t/z1hlmxfbL99MQ2JtqzrIDkNHhgnFmS7EQTjBKkyzYdTkU49npLhCFr6ZcuZzMQol psr3lQaBHZ6xtLSJhi2rIPAHnltcaFsMOP1ZR9kHRNys08IWTR0Q0uqVnE7xYOQcAIxPRLNY9TI RfDcY4Dmpbj+1wsEw0uzKbKAFKbLqC9puJa1/Ey9QrOMMVkxyB0kLK X-Google-Smtp-Source: ABdhPJwrurrpRiUgCO2uwS8TLmI8NETQMWc1HY05wWLlM6ysKdxphrp2RNQ+KPn98Ik2GoAUzYPYVg== X-Received: by 2002:aa7:c641:: with SMTP id z1mr62672533edr.84.1641557010244; Fri, 07 Jan 2022 04:03:30 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id ga10sm406601ejc.12.2022.01.07.04.03.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 07 Jan 2022 04:03:29 -0800 (PST) Message-ID: <94e34870-9e3b-8c49-6617-016253129f06@enterprisedb.com> Date: Fri, 7 Jan 2022 13:03:28 +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 , pgsql-hackers@lists.postgresql.org Cc: Andres Freund , Tomas Vondra , David Rowley References: <3082578.5fSG56mABF@aivenronan> From: Tomas Vondra In-Reply-To: <3082578.5fSG56mABF@aivenronan> Content-Type: text/plain; charset=UTF-8; format=flowed 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 1/7/22 12:03, Ronan Dunklau wrote: > Le vendredi 31 décembre 2021, 22:26:37 CET David Rowley a écrit : >> I've attached some benchmark results that I took recently. The >> spreadsheet contains results from 3 versions. master, master + 0001 - >> 0002, then master + 0001 - 0003. The 0003 patch makes the code a bit >> more conservative about the chunk sizes it allocates and also tries to >> allocate the tuple array according to the number of tuples we expect >> to be able to sort in a single batch for when the sort is not >> estimated to fit inside work_mem. > > (Sorry for trying to merge back the discussion on the two sides of the thread) > > In https://www.postgresql.org/message-id/4776839.iZASKD2KPV%40aivenronan, I > expressed the idea of being able to tune glibc's malloc behaviour. > > I implemented that (patch 0001) to provide a new hook which is called on > backend startup, and anytime we set work_mem. This hook is # defined depending > on the malloc implementation: currently a default, no-op implementation is > provided as well as a glibc's malloc implementation. > Not sure I'd call this a hook - that usually means a way to plug-in custom code through a callback, and this is simply ifdefing a block of code to pick the right implementation. Which may be a good way to do that, just let's not call that a hook. There's a commented-out MallocTuneHook() call, probably not needed. I wonder if #ifdefing is sufficient solution, because it happens at compile time, so what if someone overrides the allocator in LD_PRELOAD? That was a fairly common way to use a custom allocator in an existing application. But I don't know how many people do that with Postgres (I'm not aware of anyone doing that) or if we support that (it'd probably apply to other stuff too, not just malloc). So maybe it's OK, and I can't think of a better way anyway. > The glibc's malloc implementation relies on a new GUC, > glibc_malloc_max_trim_threshold. When set to it's default value of -1, we > don't tune malloc at all, exactly as in HEAD. If a different value is provided, > we set M_MMAP_THRESHOLD to half this value, and M_TRIM_TRESHOLD to this value, > capped by work_mem / 2 and work_mem respectively. > > The net result is that we can then allow to keep more unused memory at the top > of the heap, and to use mmap less frequently, if the DBA chooses too. A > possible other use case would be to on the contrary, limit the allocated > memory in idle backends to a minimum. > > The reasoning behind this is that glibc's malloc default way of handling those > two thresholds is to adapt to the size of the last freed mmaped block. > > I've run the same "up to 32 columns" benchmark as you did, with this new patch > applied on top of both HEAD and your v2 patchset incorporating planner > estimates for the block sizez. Those are called "aset" and "generation" in the > attached spreadsheet. For each, I've run it with > glibc_malloc_max_trim_threshold set to -1, 1MB, 4MB and 64MB. In each case > I've measured two things: > - query latency, as reported by pgbench > - total memory allocated by malloc at backend ext after running each query > three times. This represents the "idle" memory consumption, and thus what we > waste in malloc inside of releasing back to the system. This measurement has > been performed using the very small module presented in patch 0002. Please > note that I in no way propose that we include this module, it was just a > convenient way for me to measure memory footprint. > > My conclusion is that the impressive gains you see from using the generation > context with bigger blocks mostly comes from the fact that we allocate bigger > blocks, and that this moves the mmap thresholds accordingly. I wonder how much > of a difference it would make on other malloc implementation: I'm afraid the > optimisation presented here would in fact be specific to glibc's malloc, since > we have almost the same gains with both allocators when tuning malloc to keep > more memory. I still think both approaches are useful, and would be necessary. > Interesting measurements. It's intriguing that for generation contexts, the default "-1" often outperforms "1MB" (but not the other options), while for aset it's pretty much "the higher value the better". > Since this affects all memory allocations, I need to come up with other > meaningful scenarios to benchmarks. > OK. Are you thinking about a different microbenchmark, or something closer to real workload? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company