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 1tSj90-0014xz-Nc for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Dec 2024 20:47:15 +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 1tSj90-004w8U-2N for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Dec 2024 20:47:13 +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 1tSj8z-004w7t-KW for pgsql-hackers@lists.postgresql.org; Tue, 31 Dec 2024 20:47:13 +0000 Received: from mail-oo1-xc2b.google.com ([2607:f8b0:4864:20::c2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tSj8x-001SQq-2h for pgsql-hackers@lists.postgresql.org; Tue, 31 Dec 2024 20:47:12 +0000 Received: by mail-oo1-xc2b.google.com with SMTP id 006d021491bc7-5f3610c395dso925414eaf.2 for ; Tue, 31 Dec 2024 12:47:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=upgrade.com; s=google; t=1735678031; x=1736282831; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7rblE0w4iIK6zWq6bjLmnpO+s1LzfmH5eKf2lPC/hvk=; b=X2sWzNCQ/HD+oJXcRQP716mzv0l1Uwgzgvbm/vM6Fx9XJT/Cam8/LpRnWyXy4QiVbu haAsDHJn2FB7RsqW40aXyn7890T6qLi9IUu0vLiV7jaPnIaoyG/zurE55sIKWSumxF2h BAOwDYD/H6mThvEegE/Z6+nA6JtH1yaDQxkfawi+0lEGHplSN4q1o/PowxyXUv+TYKJA hyy8YeXzHQtj7RKglygkkLTTrWrmZ+BESdv5zGz5T045r/Ek+7NFRHO9sOCsAhVyrfza S25Z4jvo3AiQ9FgijWi+KA9bkISTdLMMsVbyCFFmXW1Nr0DskucbxHlErDlNMSjUwJc/ f6Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735678031; x=1736282831; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7rblE0w4iIK6zWq6bjLmnpO+s1LzfmH5eKf2lPC/hvk=; b=nFt6r8kOPXByiVIBiID659e2xgYmE177Iufdb3gL6KDTMYbjpEmjAiNwz/fsRbYtch 6koGrmK7M/K/ijvxFaDRMmOHPWh3DzUXyDkJN6I15H+NF1UczwRMBn+UPMvroDlTj+ls hHceluPK/3yKVWvviXXHJ7EUa4CQnjj+IaCg87vu6j6WrBZvQFRzS3EAwJBupG0e/Vd1 t2ZaZrv9HFUc1hFauk9KPGd0pd7+d0U6biLM4X76e/XmegEeZyUhLjxYDAzdC6cvtiLG O5FBj44aDhrc9ADz8GSG6USLo65yQtW+jijsfTiJ286svV2V0kkT6RU6O4vPW6sCh+Ud Pv3A== X-Forwarded-Encrypted: i=1; AJvYcCV4N+SuTbzhk+/PEce/CRbjY5uVplkhwrS67F97E642jwcuEaaUvAL/VO8zNJ4S7pPNc/pmRNz7sP7b9ySm@lists.postgresql.org X-Gm-Message-State: AOJu0YzdezBMq6QWbdp4HRXo1ialRm3noo3P/Pj47SpyFSYdhEMznUdL 93g3x+f6liKlBWG5kI8Na2ELk/uOM4AM673Vwt0zup2D9LNQJ0sRSz9YPJax0dI= X-Gm-Gg: ASbGncutxogIDeNf+B3adFzyschd0+7zCFiEwJ4ZN3l4nvUb+gmOTbpdtK+bXn7+3AP RUZuoZnaa69PTA97BOQDycQw0cSQvsAHi6VycoliweCvTbNUt+GrltrsSkTxfwY6MXOkOuVaUf9 yhC6ELIpt42Os/a9GgD49G1ixtOWZMW6y3umoAf1LF2P3inH/xV0fAAWGXEy6Hm9Gg8oYbCsBHA 3VJ3AiQWrDI/OGm58hyk2iRrKtuBzXENC3ZgebGY43im6k7qpO8c3yrP3RzcDpxDOY6JkbLYGr4 mZOUnYaEO2cpNk0r7wlFmS8iGq1CFTW2HEw= X-Google-Smtp-Source: AGHT+IEJ07aH4s/urH/etX56kLMhD/2u2yNoU1vX/UzS2F/vHOamluRpGYMhSHL2Hyw07mzZfjrofA== X-Received: by 2002:a05:6870:9c8c:b0:29e:1875:222d with SMTP id 586e51a60fabf-2a7fafd01a9mr5930123fac.1.1735678031077; Tue, 31 Dec 2024 12:47:11 -0800 (PST) Received: from smtpclient.apple (syn-070-113-014-216.res.spectrum.com. [70.113.14.216]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-71fc976ba23sm6759718a34.1.2024.12.31.12.47.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Dec 2024 12:47:10 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.300.87.4.3\)) Subject: Re: Add the ability to limit the amount of memory that can be allocated to backends. From: Jim Nasby In-Reply-To: Date: Tue, 31 Dec 2024 14:46:58 -0600 Cc: Jeremy Schneider , Tomas Vondra , "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" Content-Transfer-Encoding: quoted-printable Message-Id: References: <7912c911af51d5cf28c611190bf3d463b9209343.camel@crunchydata.com> <268e0ac7-8a81-4d65-8b40-b62c4b3f1bf9@postgrespro.ru> <48548d40-634b-4943-a737-3c9d95eacf06@postgrespro.ru> <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> To: James Hunter X-Mailer: Apple Mail (2.3826.300.87.4.3) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Dec 30, 2024, at 7:05=E2=80=AFPM, James Hunter = wrote: >=20 > On Sat, Dec 28, 2024 at 11:24=E2=80=AFPM Jim Nasby = wrote: >>=20 >> IMHO none of this will be very sane until we actually have = cluster-level limits. One sudden burst in active connections and you = still OOM the instance. >=20 > Fwiw, PG does support "max_connections" GUC, so a backend/connection - > level limit, times "max_connections", yields a cluster-level limit. max_connections is useless here, for two reasons: 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 = workload *and* your data are extremely homogeneous you can=E2=80=99t = simply limit the number of connections and call it a day. A slight = change in incoming queries, OR in the data that the queries are looking = at and you go from 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. Most of what I=E2=80=99ve seen on this thread is discussing ways to = *optimize* how much memory the set of running backends can consume. = Adjusting how you slice the memory pie across backends, or even within a = single backend, is optimization. While that=E2=80=99s a great goal that = I do support, it will never fully fix the problem. At some point you = need to either throw your hands in the air and start tossing memory = errors, because you don=E2=80=99t have control over how much work is = being thrown at the engine. The only way that the engine can exert = control over that would be to hold new transactions from starting when = the system is under duress (ie, workload management). While workload = managers can be quite sophisticated (aka, complex), the nice thing about = limiting this scope to work_mem, and only as a means to prevent complete = overload, is that the problem becomes 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 think an MVP would need is a GUC = to control how long a transaction can sit waiting 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 to apportion out per-node = work_mem limits. As I said, I=E2=80=99m not opposed to optimizations, I just think = they=E2=80=99re very much cart-before-the-horse. 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 = (max_backends?) that actually controls the total number of allowed = backends for everything. The sum of max_backends + autovac workers + = background workers + whatever else I=E2=80=99m forgetting would have to = be less than that. The idea here is that you=E2=80=99d normally run with = max_connections 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 need to restart - the new limits would just apply to = new connection requests.=