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 1shSzr-001xeR-J2 for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Aug 2024 12:02:27 +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 1shSzp-00CWYG-OQ for pgsql-hackers@arkaria.postgresql.org; Fri, 23 Aug 2024 12:02:26 +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 1shSzp-00CWTZ-D1 for pgsql-hackers@lists.postgresql.org; Fri, 23 Aug 2024 12:02:26 +0000 Received: from mail-qk1-x731.google.com ([2607:f8b0:4864:20::731]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1shSzn-0013Y5-5x for pgsql-hackers@postgresql.org; Fri, 23 Aug 2024 12:02:24 +0000 Received: by mail-qk1-x731.google.com with SMTP id af79cd13be357-7a1e31bc1efso115301085a.3 for ; Fri, 23 Aug 2024 05:02:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724414542; x=1725019342; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=TopK3fgXuLNCqnGm3d4pL2EB93I7WNErsxfvCSHTUEk=; b=bp9t+lF9VZbtjwiJRFegNuWbzBrw/rWco/sSvk4KxWe9C2hJEMb/yxQt4+a4Yuo36E rs+DguMnQMqAqsqZxdZiLVtGgiIOoEuq3dSDhjUjslS+n0gVtVfUs9Cgx4Ox1X49qchw C9UtHX7e0HUzle8YpS50GQemiaZ3apol1CWXENj3WV/xyAtZJc6MtAUOibLx70cCiXc0 nwPpZulsv68gcltK0Jc5nJfdwXvmW4NHXHNtmqFQFJmAiS2fWjU9rwYRqlVWS1Gqs684 i4HWwRHY3OfHkSwlRaIUx1GKGXeGgnGF4zyjsIul3bKOO11qRN8XeyLHZwPQFSnxub/D MURw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724414542; x=1725019342; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TopK3fgXuLNCqnGm3d4pL2EB93I7WNErsxfvCSHTUEk=; b=sD788h57OoOT7+ewww4VYyLgnCOrkFUZ7+z5dzP2vS7Cbn1oA/pvL4xHzQ5P70u6md C3RKXnUExiD/Mite4edUHyS8H2T2QYFtrHFsUWxgfGetYnt7taSKR3aMnPTo5E7gaNHH 4hiqxbqblciXYtKmkM3/YlCphQBlx3DxPOOastuZhL4pktTILRdagcnzldhVGrHktyBG 6tZQ2KZei8WA8Gx/MI2mEJltHPJix5dHgQHWcPfySH7RZPh7WhnLI+v1qBaPiDCM3BSJ m6AALGqgA6VRpgv1O/Wcg2n/oFwy3jVitI3s9vFtIyAEXJ5em+gq/69fo3SnwOK+6WBt Ll1Q== X-Gm-Message-State: AOJu0Yz3Gj8iYQ21AWSgcWLXnHUV2cG4aZXK9x84Z2Bz0FIDpjzcf1yD uHPU7xQne0QP4TdKfUjpFh8wUnGVYHP7bp2j3zQr2bfW1sbkJRBVqNGQ0DtN8Bvb1hTKQ2RZ/31 5PEzy/936uRnqFMCY/Di01O5OK7bJCH7x X-Google-Smtp-Source: AGHT+IFZAbjZjnZtyUoKWHaVdxd1YS46hYPXFG85uz1w4y/T+jFj5SzZeovLSSTWQxb0+vKc+y1q6neRV7/5FRBPlqc= X-Received: by 2002:a05:620a:4403:b0:79d:8042:cd01 with SMTP id af79cd13be357-7a6896e396bmr259689885a.7.1724414541506; Fri, 23 Aug 2024 05:02:21 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ashutosh Bapat Date: Fri, 23 Aug 2024 17:32:10 +0530 Message-ID: Subject: Re: Trim the heap free memory To: shawn wang Cc: PostgreSQL-development Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Shawn, On Fri, Aug 23, 2024 at 2:24=E2=80=AFPM shawn wang wrote: > > Hi hackers, > > Currently, all processes in PostgreSQL actually use malloc to allocate an= d free memory. In the case of long connections where business queries are e= xecuted over extended periods, the distribution of memory can become extrem= ely complex. > > Under certain circumstances, a common issue in memory usage due to the ca= ching strategy of malloc may arise: even if memory is released through the = free function, it may not be returned to the OS in a timely manner. This ca= n lead to high system memory usage, affecting performance and the operation= of other applications, and may even result in Out-Of-Memory (OOM) errors. > > To address this issue, I have developed a new function called pg_trim_bac= kend_heap_free_memory, based on the existing pg_log_backend_memory_contexts= function. This function triggers the specified process to execute the mall= oc_trim operation by sending signals, thereby releasing as much unreturned = memory to the operating system as possible. This not only helps to optimize= memory usage but can also significantly enhance system performance under m= emory pressure. > > Here is an example of using the pg_trim_backend_heap_free_memory function= to demonstrate its effect: >> >> CREATE OR REPLACE FUNCTION public.partition_create(schemaname character = varying, numberofpartition integer) >> RETURNS integer >> LANGUAGE plpgsql >> AS $function$ >> declare >> currentTableId integer; >> currentSchemaName varchar(100); >> currentTableName varchar(100); >> begin >> execute 'create schema ' || schemaname; >> execute 'create table ' || schemaname || '.' || schemaname || 'hashtable= (p1 text, p2 text, p3 text, p4 int, p5 int, p6 int, p7 int, p8 text, p9 na= me, p10 varchar, p11 text, p12 text, p13 text) PARTITION BY HASH(p1);'; >> currentTableId :=3D 1; >> loop >> currentTableName :=3D schemaname || '.' || schemaname || 'hashtable' || = ltrim(currentTableId::varchar(10)); >> execute 'create table ' || currentTableName || ' PARTITION OF ' || schem= aname || '.' || schemaname || 'hashtable' || ' FOR VALUES WITH(MODULUS ' ||= numberofpartition || ', REMAINDER ' || currentTableId - 1 || ')'; >> currentTableId :=3D currentTableId + 1; >> if (currentTableId > numberofpartition) then exit; end if; >> end loop; >> return currentTableId - 1; >> END $function$; >> >> select public.partition_create('test3', 5000); >> select public.partition_create('test4', 5000); >> select count(*) from test4.test4hashtable a, test3.test3hashtable b wher= e a.p1=3Db.p1; > > You are now about to see the memory size of the process executing the que= ry. >> >> postgres 68673 1.2 0.0 610456 124768 ? Ss 08:25 0:01 post= gres: postgres postgres [local] idle >> Size: 89600 kB >> KernelPageSize: 4 kB >> MMUPageSize: 4 kB >> Rss: 51332 kB >> Pss: 51332 kB >> >> 02b65000-082e5000 rw-p 00000000 00:00 0 = [heap] > > > > After use pg_trim_backend_heap_free_memory, you will see: >> >> postgres=3D# select pg_trim_backend_heap_free_memory(pg_backend_pid()); >> 2024-08-23 08:27:53.958 UTC [68673] LOG: trimming heap free memory of P= ID 68673 >> pg_trim_backend_heap_free_memory >> ---------------------------------- >> t >> (1 row) >> 02b65000-082e5000 rw-p 00000000 00:00 0 = [heap] >> Size: 89600 kB >> KernelPageSize: 4 kB >> MMUPageSize: 4 kB >> Rss: 4888 kB >> Pss: 4888 kB >> >> postgres 68673 1.2 0.0 610456 75244 ? Ss 08:26 0:01 postg= res: postgres postgres [local] idle > > > Looking forward to your feedback, Looks useful. How much time does malloc_trim() take to finish? Does it affect the current database activity in that backend? It may be good to see effect of this function by firing the function on random backends while the query is running through pgbench. In the patch I don't see definitions of ProcessTrimHeapFreeMemoryInterrupt() and HandleTrimHeapFreeMemoryInterrupt(). Am I missing something? -- Best Wishes, Ashutosh Bapat