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 1tBb0x-004s3N-1U for pgsql-general@arkaria.postgresql.org; Thu, 14 Nov 2024 14:40:06 +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 1tBb0u-004pRW-6C for pgsql-general@arkaria.postgresql.org; Thu, 14 Nov 2024 14:40:04 +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 1tBb0t-004pRO-QT for pgsql-general@lists.postgresql.org; Thu, 14 Nov 2024 14:40:04 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tBb0r-001unj-04 for pgsql-general@lists.postgresql.org; Thu, 14 Nov 2024 14:40:03 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a9ed0ec0e92so89927166b.0 for ; Thu, 14 Nov 2024 06:40:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=cybertec.at; t=1731595201; x=1732200001; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=qZGEi4Qpdu7W6f1JNfx5vmegCI2w7DMIG664v7VEU1k=; b=GMLKkbYvD5g3yN/2EJZXSmRyGkJDCW+lseB0/6WGuYNFfnNh+DgfKqvvzahWmPxbc+ KaqnwOoTI8t/59jp+nGBtsoDKp1VcaxuZkHDpogzRgUO7n8BiLXczf2Zf4WjgdZU6/cJ 6GV7Bov3Hx11XjxWYOnfMQJAn2XzkkL1btKwA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731595201; x=1732200001; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=qZGEi4Qpdu7W6f1JNfx5vmegCI2w7DMIG664v7VEU1k=; b=rurZhnWMLP6odBW7jGBsaAp6pk+1Yf5BW31iQGT1GLBZftQLovtoVkoBBywCMDZC0H tmVNy2cjtKfpWfwzrExb3K3ztQnMGj9EY++Y8/jAh8edyybBiwablrnF4NYwmZHUkRq7 tTsslNiobR1urlycXUQfQEbaze7CKu2jLWMxEw4S57Ymh/KHgWWW3VqBolnVQJC8Rb96 l0pRuuOGG8Tt79cJLesfGpe7NoQ8VcCwFrOaNtAc2N8+n11ZUrrHpdTyAkRLf5av56of qvUN67Xr3jv2JPK8pF20Dd2Hmt71MdAOX1sP4/xNZzBb+oK8jLKMM+qBJyw9D4lg1Bio MLDA== X-Forwarded-Encrypted: i=1; AJvYcCUkXJMDgW/ZmKq3g/K3hw6I7cGRJ6ptMSxFP3lvnUK0/eFhxD3QbuGxmALx5cZW5nJk0BECtwNhOF+TnByb@lists.postgresql.org X-Gm-Message-State: AOJu0YwJXVj+2KvoRGr02n10BscGbk1s0EN3aR62EsI8Qj8HYJmMLCVY MPSSgNB6Di1tv9u6KB2SspXXWcAhIPnWDM5t6nCxlVPoGuymJpOyoP0yqDgVx5Y= X-Google-Smtp-Source: AGHT+IFZ5qwDKggUrHUa+s0Ho624xyb70LbtIqDIJgxdATRJfodjpxhztdIXsqeq71YUGFwUfF7tJQ== X-Received: by 2002:a17:906:4fd0:b0:a99:762f:b298 with SMTP id a640c23a62f3a-aa20cdcd962mr232714566b.41.1731595201043; Thu, 14 Nov 2024 06:40:01 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:5e8:7658:4e84:1e83:27b9]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa20dfffc2esm70890766b.124.2024.11.14.06.40.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2024 06:40:00 -0800 (PST) Message-ID: Subject: Re: work_mem RAM Accounting in PostgreSQL From: Laurenz Albe To: Alexandru Lazarev , pgsql-general@lists.postgresql.org Date: Thu, 14 Nov 2024 15:40:00 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-2.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2024-11-13 at 21:09 +0200, Alexandru Lazarev wrote: > I have some questions regarding how the "work_mem" parameter affects the = overall RAM > usage of PostgreSQL processes within a physical host or container. >=20 > Each backend process during SQL execution may allocate N * "work_mem" sim= ultaneously. > For example, if "work_mem" is set to 32MB and N=3D5 (i.e. 5 simultaneous = and/or sequential > ORDER and hash operations), and the initial RAM usage (RSS - Resident Set= Size) of the > backend is 10MB, I would expect the backend process to use 160MB (32MB * = 5) + 10MB, > resulting in a total RAM usage of 170MB. The limit for a hash is hash_mem_multiplier * work_mem. > My questions are as follows: >=20 > 1. What happens to the allocated "work_mem" after the execution of query = nodes? Are > these memory allocations freed? Yes. > 2. If they are freed, do they remain in the RSS of the PostgreSQL backend= ? They may, because the C library can choose not to actually free all the mem= ory, but retain some to serve future malloc() requests more efficiently. > 3. From various sources, I understand that these allocations are freed af= ter > each node execution due to memory contexts, but they might remain in s= ome sort > of backend memory pool for future reuse. Is this correct? I am not sure what you mean, but perhaps what I wrote above. > 4. If so, will this memory be accounted for as used RAM on my Linux/Conta= iner > system after the backend returns to an idle state (e.g., connection po= oling)? Certainly. > Additionally: If the above is true, and my PostgreSQL host or container i= s limited > to 16GB of RAM, what would happen if I have 100 pooled connections, each = gradually > allocating those 160MB? Will this memory be reclaimed (if I understood it= correctly > as a kind of inactive anon mem), or will the OOM Killer be triggered at s= ome point > (because it is real allocated memory)? The backends won't keep that much memory allocated, so you need not worry. 100 connections are a lot. With efficient pooling, you could have fewer co= nnections and use your resources more efficiently. Yours, Laurenz Albe