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 1uC9q0-00BI5c-L5 for pgsql-hackers@arkaria.postgresql.org; Tue, 06 May 2025 04:23: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 1uC9pz-006go5-FX for pgsql-hackers@arkaria.postgresql.org; Tue, 06 May 2025 04:23:23 +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 1uC9pz-006gnx-5X for pgsql-hackers@lists.postgresql.org; Tue, 06 May 2025 04:23:23 +0000 Received: from bramsgout01.huawei.com ([119.8.89.135]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uC9ps-000NyW-0Y for pgsql-hackers@postgresql.org; Tue, 06 May 2025 04:23:19 +0000 Received: from mail.maildlp.com (unknown [172.18.210.252]) by bramsgout01.huawei.com (SkyGuard) with ESMTPS id 4Zs4xd5Zy1z3NNyr for ; Tue, 6 May 2025 12:22:45 +0800 (CST) Received: from ytopeml100002.china.huawei.com (unknown [7.184.16.70]) by mail.maildlp.com (Postfix) with ESMTPS id 69A3B1402CD for ; Tue, 6 May 2025 12:23:09 +0800 (CST) Received: from ytopeml500001.china.huawei.com (7.184.16.223) by ytopeml100002.china.huawei.com (7.184.16.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Tue, 6 May 2025 00:23:07 -0400 Received: from ytopeml500001.china.huawei.com ([7.184.16.223]) by ytopeml500001.china.huawei.com ([7.184.16.223]) with mapi id 15.01.2507.039; Tue, 6 May 2025 00:23:07 -0400 From: Jack Ng To: Dmitry Dolgov <9erthalion6@gmail.com> CC: Ashutosh Bapat , "pgsql-hackers@postgresql.org" , Robert Haas , Ni Ku Subject: RE: Changing shared_buffers without restart Thread-Topic: Changing shared_buffers without restart Thread-Index: AQHbl7DrFzUaSyTydEunWI983IaxOrNc1X6AgGMtZVaABTbmwA== Date: Tue, 6 May 2025 04:23:07 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-CA, zh-CN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.48.240.81] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks Dmitry. Right, the coordination mechanism in v4-0006 works as expect= ed in various tests (sorry, I misunderstood some details initially). I also want to report a couple of minor issues found during testing (which = you may be aware of already): 1. For memory segments other the first one ('main'), the start address pass= ed to mmap may not be aligned to 4KB or huge page size (since reserved_offs= et may not be aligned) and cause mmap to fail. 2. Since the ratio for main/desc/iocv/checkpt/strategy in SHMEM_RESIZE_RATI= O are relatively small, I think we need to guard against the case where 'm= ax_available_memory' is too small for the required sizes of these segments = (from CalculateShmemSize). Like when max_available_memory=3Ddefault and shared_numbers=3D128kB, 'main'= still needs ~109MB, but since only 10% of max_available_memory is reserved= for it (~102MB) and start address of the next segment is calculated based = on reserved_offset, this would cause the mappings to overlap and memory pro= blems later (I hit this after fixing 1.) I suppose we can change the minimum value of max_available_memory to be lar= ge enough, and may also adjust the ratios in SHMEM_RESIZE_RATIO to ensure t= he reserved space of those segments are sufficient. Regards, Jack Ng -----Original Message----- From: Dmitry Dolgov <9erthalion6@gmail.com>=20 Sent: Monday, April 21, 2025 5:33 AM To: Ni Ku Cc: Ashutosh Bapat ; pgsql-hackers@postgresql= .org; Robert Haas Subject: Re: Changing shared_buffers without restart > On Thu, Apr 17, 2025 at 07:05:36PM GMT, Ni Ku wrote: > I also have a related question about how ftruncate() is used in the patch= . > In my testing I also see that when using ftruncate to shrink a shared=20 > segment, the memory is freed immediately after the call, even if other=20 > processes still have that memory mapped, and they will hit SIGBUS if=20 > they try to access that memory again as the manpage says. > > So am I correct to think that, to support the bufferpool shrinking=20 > case, it would not be safe to call ftruncate in AnonymousShmemResize=20 > as-is, since at that point other processes may still be using pages=20 > that belong to the truncated memory? > It appears that for shrinking we should only call ftruncate when we're=20 > sure no process will access those pages again (eg, all processes have=20 > handled the resize interrupt signal barrier). I suppose this can be=20 > done by the resize coordinator after synchronizing with all the other pro= cesses. > But in that case it seems we cannot use the postmaster as the=20 > coordinator then? b/c I see some code comments saying the postmaster=20 > does not have waiting infrastructure... (maybe even if the postmaster=20 > has waiting infra we don't want to use it anyway since it can be=20 > blocked for a long time and won't be able to serve other requests). There is already a coordination infrastructure, implemented in the patch 00= 06, which will take care of this and prevent access to the shared memory un= til everything is resized.