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 1rUSQA-00B1Sx-0e for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jan 2024 14:15: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 1rUSQ7-00EcVy-A8 for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jan 2024 14:15:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rUSQ6-00EcVq-UV for pgsql-hackers@lists.postgresql.org; Mon, 29 Jan 2024 14:15:30 +0000 Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rUSQ3-003yhn-QJ for pgsql-hackers@lists.postgresql.org; Mon, 29 Jan 2024 14:15:29 +0000 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-51030ce36fbso2342645e87.3 for ; Mon, 29 Jan 2024 06:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; t=1706537726; x=1707142526; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3zIemXiDMZpuABXooI0jr1ep4l3UsKpqNHilMEHaSrY=; b=jccZVK6Cw5wEtIBRh+GV+bmiASkwersPt4ZhH8gO2Q7CbV9MvxNYdK00uW6AzBFPpT kh2EMkwTNQPSImsT4LWFDfXxm7JpM7RS2yaUoUorYxfZHJmJFgH9WkYP6j9k/MLeVfoh CCEz1OkvWgcms5RhJu18da6B1nXhvsoRl7plo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706537726; x=1707142526; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3zIemXiDMZpuABXooI0jr1ep4l3UsKpqNHilMEHaSrY=; b=hu5yKNr/c9RH5EiFSLqWruU2hUi4P8bE1mI1mgWyBXf6o1RLVqMg6187My+IT8v7ea Xul++ssbzaSLBtBOu3maVRzgSweeJdnGW3cSv6T3EA7jmr7HkwmKWnaDJqZmF+MZeJSF /gZnGsYgIl2jBchY8NnLRA1Tr4KNfzFL4YhN4FYcDQRfJYuYDrAxc4lXOB5Iv+QP5DXR cUvzcINfj+9lhGB2chTsmEnHQdNHxhbhN0iMSXE6A0Nn9dpvew+KXbSHvYajj2nsLry+ daTNsmx0w+FbUA6ixPCU5NIO+jY6WW3/VC7xZnKtJznM9KF5wMTdDicH7Jz0sa285nqN 77Yg== X-Gm-Message-State: AOJu0Yzk8SPz2eb7q8EgYtWdOCXd+sQJGylSh2P0khAJGGOpD/EeBk3r 7y1go80+KfHPxOLrBApBOUHNytqKS/LZYbl4nRUtjLNX9gs/h1Nj/d62LirUvDgGcgRsDEDQjZY W X-Google-Smtp-Source: AGHT+IG9TPOhQVkHf9wTnJEmSIaSC+8j9325za9+ZdEzC+sFoMK5wiazr9cQRXJAJVifm2+IY56zMw== X-Received: by 2002:a19:6907:0:b0:50e:6b5d:5976 with SMTP id e7-20020a196907000000b0050e6b5d5976mr3168940lfc.40.1706537725928; Mon, 29 Jan 2024 06:15:25 -0800 (PST) Received: from aivenlaptop.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id u7-20020a05600c138700b0040d8ff79fd8sm10344025wmf.7.2024.01.29.06.15.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 06:15:25 -0800 (PST) From: Ronan Dunklau To: Tomas Vondra Cc: PostgreSQL Hackers Subject: Re: scalability bottlenecks with (many) partitions (and more) Date: Mon, 29 Jan 2024 15:15:25 +0100 Message-ID: <13440175.uLZWGnKmhe@aivenlaptop> In-Reply-To: References: <4541483.LvFx2qVVIh@aivenlaptop> 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 lundi 29 janvier 2024, 13:17:07 CET Tomas Vondra a =E9crit : > > Did you try running an strace on the process ? That may give you some > > hindsights into what malloc is doing. A more sophisticated approach wou= ld > > be using stap and plugging it into the malloc probes, for example > > memory_sbrk_more and memory_sbrk_less. >=20 > 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. It would tell you how malloc actually performs your allocations, and how of= ten=20 they end up translated into syscalls. The main issue with glibc would be th= at=20 it releases the memory too agressively to the OS, IMO. >=20 > > 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. >=20 > Thanks, I'll take a look at that. >=20 > >> FWIW I was wondering if this is a glibc-specific malloc bottleneck, so= I > >> tried running the benchmarks with LD_PRELOAD=3Djemalloc, and that impr= oves > >> 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. > >=20 > > GLIBC's malloc offers some tuning for this. In particular, setting eith= er > > M_MMAP_THRESHOLD or M_TRIM_THRESHOLD will disable the unpredictable "au= to > > adjustment" beheviour and allow you to control what it's doing. > >=20 > > 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 t= he > > way it is incremented means you can end in a bad place depending on your > > particular allocation patttern. >=20 > So, what values would you recommend for these parameters? >=20 > 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. Right now depending on your workload (especially if you use connection=20 pooling) you can end up with something like 32 or 64MB of dynamically adjus= ted=20 trim-threshold which will never be released back.=20 The first heurstic I had in mind was to set it to work_mem, up to a=20 "reasonable" limit I guess. One can argue that it is expected for a backend= to=20 use work_mem frequently, and as such it shouldn't be released back. By sett= ing=20 work_mem to a lower value, we could ask glibc at the same time to trim the= =20 excess kept memory. That could be useful when a long-lived connection is=20 pooled, and sees a spike in memory usage only once. Currently that could we= ll=20 end up with 32MB "wasted" permanently but tuning it ourselves could allow u= s=20 to releaase it back.=20 Since it was last year I worked on this, I'm a bit fuzzy on the details but= I=20 hope this helps.