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 1tUv3W-003pjh-Gn for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jan 2025 21:54:38 +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 1tUv3V-00Dkjk-Id for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jan 2025 21:54:37 +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 1tUv3V-00DkjV-8t for pgsql-hackers@lists.postgresql.org; Mon, 06 Jan 2025 21:54:36 +0000 Received: from relay6-d.mail.gandi.net ([2001:4b98:dc4:8::226]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tUv3S-000DoB-0Q for pgsql-hackers@lists.postgresql.org; Mon, 06 Jan 2025 21:54:35 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id B3E4BC0004; Mon, 6 Jan 2025 21:54:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vondra.me; s=gm1; t=1736200470; 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=GKxlKBs+6dpLnRUL4BqqK8TGcVzJCzDkjTnB6MtDgUY=; b=ixiE0qHtEcsW2BMvHmaddBGvntXww2/cs34w4Er+CgeZ7AUZKeWbZC1MJWbR9NwQnThtfZ eZO/8tK1ssZqpFv67K5FYJQBrLpzBfgeSedK6GxspLr8+dGptUbSC1mStz2rVjty+ZpQyf xUx2LcoCL1fc8z8O7nhq364bUAcUQDtxXw+v9YchlR+SzJ70qQPY2KQbDk5RB2GUzpceTy OI0R8mtmfqtY2CIMFF6fo8Bv6YXi46XTJRf603+MG/1GFs0YYg0UE1xKbSD4w22aaYxrvP x3TJOE9nOkM8FpdBli8eUk9ZLjwoq1CeTOjgBnegonWdPlL7NUJ5/iPt/f936A== Message-ID: Date: Mon, 6 Jan 2025 22:54:26 +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 Cc: James Hunter , Jeremy Schneider , "Anton A. Melnikov" , 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> <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> <967BCFAB-D931-46DE-933B-5BD32064E49D@upgrade.com> <6be20166-734d-432f-a2d2-40f405517cd1@vondra.me> <7EF54040-C95E-425C-A3FA-A5A0BD60D122@upgrade.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: <7EF54040-C95E-425C-A3FA-A5A0BD60D122@upgrade.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-GND-Sasl: tomas@vondra.me List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/6/25 22:07, Jim Nasby wrote: > On Jan 2, 2025, at 9:21 PM, Tomas Vondra wrote: >> >>> That said, I do think a workload manager would be more effective than >>> trying to limit total connections. >> >> The "workload management" concept is so abstract I find it very >> difficult to discuss without much more detail about how would it >> actually work / be implemented. >> >> I believe implementing some rudimentary "global" memory accounting would >> not be *that* hard (possibly along the lines of the patches early in >> this thread), and adding some sort of dynamic connection limit would not >> be much harder I think. But then comes the hard part of actually doing >> the "workload management" part, which seems pretty comparable to what a >> QoS / scheduler needs to do. With all the weird corner cases. > > I’ve been saying “workload management” for lack of a better term, but my > initial suggestion upthread was to simply stop allowing new transactions > to start if global work_mem consumption exceeded some threshold. That’s > simplistic enough that I wouldn’t really consider it “workload > management”. Maybe “deferred execution” would be a better name. The only > other thing it’d need is a timeout on how long a new transaction could > sit in limbo. > How would that not be a huge DoS vector? If a low-priority task can allocate a chunk of memory, and block/delay execution of everything else, that seems potentially disastrous. I believe there is a reason why workload management systems tend to be fairly complex systems, both to implement and manage, requiring stuff like resource pools, workload classes, etc. > I agree that no matter what everything being proposed would rely on > having metrics on actual work_mem consumption. That would definitely be > a good feature on its own. I’m thinking adding “work_mem_bytes” and > “work_mem_operations” to pg_stat_activity (where “work_mem_operations” > would tell you how many different things were using work_mem in the backend. > > Incidentally, something related to this that I’ve seen is backend memory > consumption slowly growing over time. Unbounded growth of relcache and > friends was presumably the biggest contributor. There’s an argument to > be made for a view dedicated to tracking per-backend memory stats, with > additional info about things contributing to idle memory consumption. True, but I don't quite see how would a global memory limit help with any of that. In fact, relcache seems exactly like the thing to limit at the backend level. regards -- Tomas Vondra