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 1tSlrR-001OBo-HL for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Dec 2024 23:41:18 +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 1tSlrQ-005yWN-Md for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Dec 2024 23:41:16 +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 1tSlrQ-005yVU-7t for pgsql-hackers@lists.postgresql.org; Tue, 31 Dec 2024 23:41:16 +0000 Received: from relay7-d.mail.gandi.net ([217.70.183.200]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tSlrO-001TbG-0M for pgsql-hackers@lists.postgresql.org; Tue, 31 Dec 2024 23:41:14 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id CD1C320003; Tue, 31 Dec 2024 23:41:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vondra.me; s=gm1; t=1735688470; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sylZDNIY/3KGbr8G5m/prreYasmnf4tH7IJaFZy9ZGc=; b=ns3WO35b0h0HJjBohVKk4SL4TEsB3+mRpIp3XMVDl8sg5gFeDeHE+Vhvk1acQyPCfPZ8Sx 7RJDKP6iTKYCNbdGrMX1L1Cyt7EigxzLPW4LCxrG0pX+S8X7LM7/OF7WduW6GxI2ESkAA/ nwWwNEYHiOoOTIY+XHfv0qvL92IPiqCqB41V10CPGFvsNFqSnsYDYAbqQcQvnjrx6DZVE2 ihQlsetPM7p0JamJ1AgNrriWg9URJ3f+hwxIbUurTlx82ijjfP7JL0HPjcIXfdAGSbmAgU SpjKbGiR7hDKmjX1KZiGKBpzn005CnGuwoguK2pX+U3JmCYxzWJRH95QV68Asw== Message-ID: Date: Wed, 1 Jan 2025 00:41:05 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Add the ability to limit the amount of memory that can be allocated to backends. To: Jim Nasby , James Hunter Cc: Jeremy Schneider , "Anton A. Melnikov" , Tomas Vondra , Andres Freund , Andrei Lepikhov , Stephen Frost , reid.thompson@crunchydata.com, Arne Roland , "pgsql-hackers@lists.postgresql.org" , vignesh C , Justin Pryzby , Ibrar Ahmed , "stephen.frost" References: <7912c911af51d5cf28c611190bf3d463b9209343.camel@crunchydata.com> <8fcb4406-49f5-4069-b8e9-197a38004ddd@postgrespro.ru> <20231024024435.yaqrajcchcliwhjl@awork3.anarazel.de> <98646b96-6dcf-8d8a-3daf-837f25f8b1e3@enterprisedb.com> <1c5f1856-817d-45e5-8e1a-acd95c6dd335@enterprisedb.com> <25e68736-00ff-4346-b432-4cda836743f3@vondra.me> <600384a7-09bc-41f8-a38c-2f3d2195054b@postgrespro.ru> <4806d917-c019-49c7-9182-1203129cd295@vondra.me> <20241228102645.228f544d@jeremy-ThinkPad-T430s> <8B21EC3F-B8A3-4FCE-94B1-0B5AA14BD2C8@upgrade.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: tomas@vondra.me List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/31/24 21:46, Jim Nasby wrote: > On Dec 30, 2024, at 7:05=E2=80=AFPM, James Hunter wrote: >> >> On Sat, Dec 28, 2024 at 11:24=E2=80=AFPM Jim Nasby wrote: >>> >>> IMHO none of this will be very sane until we actually have cluster-le= vel limits. One sudden burst in active connections and you still OOM the = instance. >> >> Fwiw, PG does support "max_connections" GUC, so a backend/connection -= >> level limit, times "max_connections", yields a cluster-level limit. >=20 > max_connections is useless here, for two reasons: >=20 > 1. Changing it requires a restart. That=E2=80=99s at *best* a real PITA= in production. [1] > 2. It still doesn=E2=80=99t solve the actual problem. Unless your workl= oad *and* your data are extremely homogeneous you can=E2=80=99t simply li= mit the number of connections and call it a day. A slight change in incom= ing queries, OR in the data that the queries are looking at and you go fr= om running fine to meltdown. You don=E2=80=99t even need a plan flip for = this to happen, just the same plan run at the same rate but now accessing= more data than before. >=20 I really don't follow your argument ... Yes, changing max_connections requires a restart - so what? AFAIK the point James was making is that if you multiply max_connections by the per-backend limit, you get a cluster-wide limit. And presumably the per-backend limit would be a GUC not requiring a restart. Yes, high values of max_connections are problematic. I don't see how a global limit would fundamentally change that. In fact, it could introduce yet more weird failures because some unrelated backend did something weird. FWIW I'm not opposed to having some global memory limit, but as I explained earlier, I don't see a way to do that sensibly without having a per-backend limit first. Because if you have a global limit, a single backend consuming memory could cause all kinds of weird failures in random other backends. > Most of what I=E2=80=99ve seen on this thread is discussing ways to *op= timize* how much memory the set of running backends can consume. Adjustin= g how you slice the memory pie across backends, or even within a single b= ackend, is optimization. While that=E2=80=99s a great goal that I do supp= ort, it will never fully fix the problem. At some point you need to eithe= r throw your hands in the air and start tossing memory errors, because yo= u don=E2=80=99t have control over how much work is being thrown at the en= gine. The only way that the engine can exert control over that would be t= o hold new transactions from starting when the system is under duress (ie= , workload management). While workload managers can be quite sophisticate= d (aka, complex), the nice thing about limiting this scope to work_mem, a= nd only as a means to prevent complete overload, is that the problem beco= mes a lot simpler since you=E2=80=99re only looking at one metric and not= trying to support any kind of priority system. The only fanciness I thin= k an MVP would need is a GUC to control how long a transaction can sit wa= iting before it throws an error. Frankly, that sounds a lot less complex = and much easier for DBAs to adjust than trying to teach the planner how t= o apportion out per-node work_mem limits. >=20 > As I said, I=E2=80=99m not opposed to optimizations, I just think they=E2= =80=99re very much cart-before-the-horse. >=20 What optimization? I didn't notice anything like that. I don't see how "adjusting how you slice the memory pie across backends" counts as an optimization. I mean, that's exactly what a memory limit is meant to do. Similarly, there was a proposal to do planning with work_mem, and then go back and adjust the per-node limits to impose a global limit. That does not seem like an optimization either ... (more an opposite of it). > 1: While it=E2=80=99d be a lot of work to make max_connections dynamic = one thing we could do fairly easily would be to introduce another GUC (ma= x_backends?) that actually controls the total number of allowed backends = for everything. The sum of max_backends + autovac workers + background wo= rkers + whatever else I=E2=80=99m forgetting would have to be less than t= hat. The idea here is that you=E2=80=99d normally run with max_connection= s set significantly lower than max_backends. That means that if you need = to adjust any of these GUCs (other than max_backends) you don=E2=80=99t n= eed to restart - the new limits would just apply to new connection reques= ts. I don't quite understad how max_backends helps with anything except allowing to change the limit of connections without a restart, or why would it be needed for introducing a memory limit. To me those seem very much like two separate features. regards --=20 Tomas Vondra