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 1rUQZh-00ApJ1-5h for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jan 2024 12:17:17 +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 1rUQZf-00DsxZ-QL for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jan 2024 12:17:15 +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 1rUQZf-00DsxR-GO for pgsql-hackers@lists.postgresql.org; Mon, 29 Jan 2024 12:17:15 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rUQZc-004KqN-G1 for pgsql-hackers@lists.postgresql.org; Mon, 29 Jan 2024 12:17:14 +0000 Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-a35e0532900so65432466b.2 for ; Mon, 29 Jan 2024 04:17:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1706530631; x=1707135431; 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=T6ESZcsBz7UvOfEa7kalxmkwoddoo3iqyxOyCoqoDYc=; b=M8w9Y9Z+jedqAaj74eeEoNnZinjQzufOvHxSvuEHPSV855afaAInVm/nrUtkfC8aPL vWJHNzEX5CrdqhtFCixEtgvjq3Qeg9TXdvHCO/DI3R9lBnhdZQ9wvGihSWrWHyDUqhzi y0/P2yFfqjsPse4gZ/ASlinQ8FUhSaHt9HSbS+RQIWx9oo8m+XeYLKDdL+pbFy9tDl1F UwbJKhxof1/iQeXu9Qi+tcK0vdn74SeiEPMWzajne7FS163QK8S7q3v5sJo6LXt+4v2k HziSBLMwyuhsfSToRkCoultvOHCU9z+TaEakvK+45OBZetHwyYVsGfhfTgvyjkzc6Z42 +9Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706530631; x=1707135431; 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=T6ESZcsBz7UvOfEa7kalxmkwoddoo3iqyxOyCoqoDYc=; b=POkbkOalqiT3bG7/pMkpmNF46XaP6lXxNkEpYNQSh6//5bN2TL1s6HtTbLLlXAcgFj NKrru9MhUJqO0DO7D3lXGJ0DUQwNBCbckJY9isSf455Tohg6E6K9MVGicMGuiz7Vfqj4 mDSkq4eW26RBgAQSGZ3nswJxIaApOu257zU0FOy3L39gAoVCtg3HckhZ4/am6e73Q91c MH5sc2evSBEGT2gLpjPx9SEMBCS4Ewlr/UwjC4YsD2b9ZUdt2tMBlvcBrXakgnY0VFIl QxsleMyaqU0PXlo2vATGCgKIWuNLVX2LVO6oQ/dJTCXLfXETaYL6FnExl6tLu5NhOSel ZquA== X-Gm-Message-State: AOJu0YyAnzQqylJAOGzFsSVCEnv8Ed+Vu5bhKGurs+BE+vMwxT7e3aom EHEgGUlt8ZYq5gXYWk6rkSs5f5KP/KRKh7VWT4Kk2q+JW5wHbJIxUgifLFLuGiRa1cPYWbkrkIE = X-Google-Smtp-Source: AGHT+IHXjIssRXQgmWugosoMaQrPIYHnimlb29+OwQCaEHA91Q5w/d0kta3qgB6CoPW67CPrHuCrMg== X-Received: by 2002:a17:906:19d0:b0:a31:b59:9cec with SMTP id h16-20020a17090619d000b00a310b599cecmr4197723ejd.16.1706530629062; Mon, 29 Jan 2024 04:17:09 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id s15-20020a170906354f00b00a3186c2c254sm3845468eja.213.2024.01.29.04.17.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 29 Jan 2024 04:17:08 -0800 (PST) Message-ID: Date: Mon, 29 Jan 2024 13:17:07 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: scalability bottlenecks with (many) partitions (and more) To: Ronan Dunklau Cc: PostgreSQL Hackers References: <510b887e-c0ce-4a0c-a17a-2c6abb8d9a5c@enterprisedb.com> <4541483.LvFx2qVVIh@aivenlaptop> Content-Language: en-US From: Tomas Vondra In-Reply-To: <4541483.LvFx2qVVIh@aivenlaptop> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/29/24 09:53, Ronan Dunklau wrote: > Le dimanche 28 janvier 2024, 22:57:02 CET Tomas Vondra a écrit : > > Hi Tomas ! > > I'll comment on glibc-malloc part as I studied that part last year, and > proposed some things here: https://www.postgresql.org/message-id/ > 3424675.QJadu78ljV%40aivenlaptop > Thanks for reminding me. I'll re-read that thread. > >> FWIW where does the malloc overhead come from? For one, while we do have >> some caching of malloc-ed memory in memory contexts, that doesn't quite >> work cross-query, because we destroy the contexts at the end of the >> query. We attempt to cache the memory contexts too, but in this case >> that can't help because the allocations come from btbeginscan() where we >> do this: >> >> so = (BTScanOpaque) palloc(sizeof(BTScanOpaqueData)); >> >> and BTScanOpaqueData is ~27kB, which means it's an oversized chunk and >> thus always allocated using a separate malloc() call. Maybe we could >> break it into smaller/cacheable parts, but I haven't tried, and I doubt >>>>> it's the only such allocation. > > Did you try running an strace on the process ? That may give you some > hindsights into what malloc is doing. A more sophisticated approach would be > using stap and plugging it into the malloc probes, for example > memory_sbrk_more and memory_sbrk_less. > No, I haven't tried that. In my experience strace is pretty expensive, and if the issue is in glibc itself (before it does the syscalls), strace won't really tell us much. Not sure, ofc. > An important part of glibc's malloc behaviour in that regard comes from the > adjustment of the mmap and free threshold. By default, mmap adjusts them > dynamically and you can poke into that using the > memory_mallopt_free_dyn_thresholds probe. > Thanks, I'll take a look at that. >> >> FWIW I was wondering if this is a glibc-specific malloc bottleneck, so I >> tried running the benchmarks with LD_PRELOAD=jemalloc, and that improves >> the behavior a lot - it gets us maybe ~80% of the mempool benefits. >> Which is nice, it confirms it's glibc-specific (I wonder if there's a >> way to tweak glibc to address this), and it also means systems using >> jemalloc (e.g. FreeBSD, right?) don't have this problem. But it also >> says the mempool has ~20% benefit on top of jemalloc. > > GLIBC's malloc offers some tuning for this. In particular, setting either > M_MMAP_THRESHOLD or M_TRIM_THRESHOLD will disable the unpredictable "auto > adjustment" beheviour and allow you to control what it's doing. > > By setting a bigger M_TRIM_THRESHOLD, one can make sure memory allocated using > sbrk isn't freed as easily, and you don't run into a pattern of moving the > sbrk pointer up and down repeatedly. The automatic trade off between the mmap > and trim thresholds is supposed to prevent that, but the way it is incremented > means you can end in a bad place depending on your particular allocation > patttern. > So, what values would you recommend for these parameters? My concern is increasing those value would lead to (much) higher memory usage, with little control over it. With the mempool we keep more blocks, ofc, but we have control over freeing the memory. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company