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 1tD6w9-00Fzeh-Tx for pgsql-general@arkaria.postgresql.org; Mon, 18 Nov 2024 18:57:25 +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 1tD6w7-006f23-Uf for pgsql-general@arkaria.postgresql.org; Mon, 18 Nov 2024 18:57:24 +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 1tD6w7-006f1v-Il for pgsql-general@lists.postgresql.org; Mon, 18 Nov 2024 18:57:24 +0000 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tD6w5-002cQb-K3 for pgsql-general@lists.postgresql.org; Mon, 18 Nov 2024 18:57:23 +0000 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-3824709ee03so938118f8f.2 for ; Mon, 18 Nov 2024 10:57:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=cybertec.at; t=1731956239; x=1732561039; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=0ZMk+f+RgneD2ba0Qy6JcpjT8nJzFh6t0K/dEfUQ8jg=; b=LBBEciExjiy3NB13v3TuHk/z4fgjvQOUP4ctxYttvugUdmV8PEXVZvF6+SAiF7uaKe Ijd9KnYNTI+w/lwxw39x+BbnzIYYAWbT+Hl1Ulf4zHxGZ0Y0QMTvcD5/TOzReBVYkUwP pEHScDwOJX2NHWhQ9uDtbCbSQlM8rsdUGuMA0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731956239; x=1732561039; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=0ZMk+f+RgneD2ba0Qy6JcpjT8nJzFh6t0K/dEfUQ8jg=; b=oAx0tifnHvCqBUjMs3jJfaA+egKSOK1fJtkKZHIF8yJJNwkQXMf7vpwIn94GLWMviM XNoZtKelF3OQzC0S+2wS6Yd6PHIw5VqnbfPjoWiuVHx+L3nFvllpmiIn5g3rPMf7tfIk ViYDDjgiKPmNn+04Yak5ALXqdskUJPf8QL/TyySMxiZeJ8yRWibPKV9ppXY+ODOcGVXJ nUMu9ib41iDibRvwj0YVbIxWnP8slXeUrRIEcTJMqTIrGuRPFyuMnOZgn25lI69dKB5h I0lkoW8hRRlUF0q1lHg4JcGdJcW8seozYlVh+fFXYtELISMk/EpkC2OkJD0t9QtOhNXy QytQ== X-Gm-Message-State: AOJu0YzJ3xCQmBJAJLY7wmlsNTUqMqfx7PRuPNBGz9JUSI6VE90s50/w Q9X/Lcc2OJ8b3N7V2JomeFu8rw2f3ac/HxMQvU2isHn6rEtluzACojfBK5jBon8= X-Google-Smtp-Source: AGHT+IEM1MzObXILf0wL06IragW9lPDcw6sAd0UypWYdm7uGHUujX0SXOfcuppFl0HFQy1nto4z4+A== X-Received: by 2002:a5d:5987:0:b0:382:4a0d:5ce5 with SMTP id ffacd0b85a97d-3824a0d5ea4mr2182549f8f.12.1731956239484; Mon, 18 Nov 2024 10:57:19 -0800 (PST) Received: from localhost.localdomain ([91.26.40.154]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38243e09ff9sm5044022f8f.68.2024.11.18.10.57.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 10:57:19 -0800 (PST) Message-ID: Subject: Re: work_mem RAM Accounting in PostgreSQL From: Laurenz Albe To: Alexandru Lazarev Cc: pgsql-general@lists.postgresql.org Date: Mon, 18 Nov 2024 19:57:18 +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 Mon, 2024-11-18 at 18:11 +0200, Alexandru Lazarev wrote: > Highly appreciate=C2=A0your response. But I have some additional question= s (inline) >=20 > On Thu, Nov 14, 2024 at 4:40=E2=80=AFPM Laurenz Albe wrote: > > On Wed, 2024-11-13 at 21:09 +0200, Alexandru Lazarev wrote: > >=20 > > > My questions are as follows: > > >=20 > > > 1. What happens to the allocated "work_mem" after the execution of qu= ery nodes? Are > > > =C2=A0 =C2=A0 these memory allocations freed? > >=20 > > Yes. > >=20 > > > 2. If they are freed, do they remain in the RSS of the PostgreSQL bac= kend? > >=20 > > They may, because the C library can choose not to actually free all the= memory, > > but retain some to serve future malloc() requests more efficiently. >=20 > This part is important for me to understand (my apologies, I am not a C p= rogrammer > and for me it is difficult reading PG sources :)): I wanted to understand= if in this part > there isn't some kind of allocated memory pooling in postgres. So, since = some memory is > freed then it is up to C Library and underlying OS if it will be returned= back to OS or > will stay somehow reserved - did I get it correctly? If so, then most pro= bably this > "reserved" memory=C2=A0should be reclaimed under memory pressure conditio= ns (other backends > processes try greedely allocate big chunks of memory)? Idle PostgreSQL backends won't keep moch memory allocated under normal cond= itions: the catalog cache, prepared statements, private memory to buffer temporary = tables, potentially a materialized WITH HOLD cursor. Yes, it is up to the C library to keep some free'd memory allocated for reu= se. This memory won't be reclaimed when there is memory pressure, since it belo= ngs to the process. > >=20 > >=20 > > The backends won't keep that much memory allocated, so you need not wor= ry. >=20 > Let me reformulate the use-case a bit differently - I would highly apprec= iate=C2=A0any community inputs:=C2=A0 > 1) Let say 60 connections did some intensive memory consuming operations,= each one > allocating up to 200MB of work_mem, then they finished and returned to= 'idle' state. The lion's share of that memory will be freed when processing is done. > 2) After that the rest of 40 connections starting doing "work_mem" consum= ing operations > in parallel, each one allocating up to 300MB, then will N*"work_mem" a= llocated RAM > from step#1 be reclaimed at point of step#2 when multiple backend need= to allocate > aggressively=C2=A0K*"wor_mem" memory? Most of the memory is already reclaimed when the first cconnections are don= e, so you shouldn't be in trouble. All this is somehow vague, since it depends on the C library implementation= . I recommend that you start tuning with a value for work_mem that is low eno= ugh to be very certain that you won't go out of memory. Then monitor temporary files. If too many get created, increase work_mem, = all the while watching your memory consumption to avoid going OOM. As soon as only few temporary files are created, you are probably good. If you cannot get there, try to tune your queries. The king's way to avoid problems in this area is to use a moderately sized connection pool. Then connections don't sit idle for long and cannot hog m= uch memory. Yours, Laurenz Albe