Re: Better shared data structure management and resizable shared data structures

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: Haoyu Huang <haoyu(dot)huang(dot)68(at)gmail(dot)com>
Cc: Palak Chaturvedi <chaturvedipalak1911(at)gmail(dot)com>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Dagfinn Ilmari Mannsåker <ilmari(at)ilmari(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Better shared data structure management and resizable shared data structures
Date: 2026-12-04 14:57:47
Message-ID: CAExHW5sVPmEUsDHPHWnesGp+JGBf+bJgyXcGj-=1Ynsz23PwCw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Dec 4, 2026 at 11:31 AM Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
wrote:

> Hi Haoyu,
>
> On Tue, Nov 24, 2026 at 11:47 PM Haoyu Huang <haoyu(dot)huang(dot)68(at)gmail(dot)com>
> wrote:
>
>> Hi Ashutosh and Palak,
>>
>> Thanks for the careful review, and sorry for the slow reply. I agree with
>> most of your comments. The attached patch contains the changes we made on
>> top of the first patch I shared in June. It is easier to share the changes
>> on top of our earlier patch.
>>
>>>
>>>
> As I have indicated earlier, resizable shared buffers work is being
> discussed at [1]. We are discussing the resizable shared memory part of
> that work in this thread. Can you please respond there. I have shared the
> latest patches which fix many TODOs there.
>
> Here is the patchset with all the TODOs fixed in the resizable shared
> memory patch. They are as follows:
>
> 1. To support resizable shared memory structures, we mmap the memory
> segment using MAP_NORESERVE so that memory is not allocated or reserved
> against the whole segment. But this means that if the pages required for
> initial sizes of the structures are not available, there will be a fault
> when initializing the structures. In order to avoid that, we create a
> layout of memory segment indicating parts of the segment that cover the
> initial sizes of the shared structures (fixed and resizable), and the parts
> which are holes. These parts are called subsegments. More on the
> subsegments can be found in the patches which document them in detail. The
> memory pages are allocated against the first set of parts before
> initializing any structure. If that allocation fails, the server fails to
> start - that is the same behaviour we have today. At run time, when
> resizing a resizable structure, if the memory is not available, the
> resizing operation fails. Added tests testing this behaviour using
> injection points.
>
> 2. Restrict resizing operations to the affected pages: Expansion populates
> only newly required pages, and shrinking releases only pages no longer
> needed. The page containing the boundary of a resizable structure remains
> allocated as it may hold the next structure.
>
> 3. Prevent postmaster access to resizable structures: This came out of
> discussion with Heikki at PGConf.EU. The postmaster does not participate in
> the barriers used to coordinate resizing, so memory protections in
> Postmaster may go stale as the structures are resized. A non-EXEC backend
> may inherit the stale protections making the resizable structures partially
> accessible to it. Postmaster does not require access to the resizable
> structures currently. Rather than introduce a separate mechanism to update
> its protections, whole pages belonging exclusively to resizable structures
> are made inaccessible in the postmaster after initialization. This way a
> non-EXEC backend won't be able to access the shared structure unless
> they adjust their protections according to the latest size of the resizable
> structure. New backends reapply protections according to the current
> structure sizes at the beginning.
>
> 4. Allow resizable structures to be created only at startup: This came out
> of discussion with Heikki at PGConf.EU. The memory used for creating shared
> structures after startup is already allocated and is small 100K. So it does
> not make sense to allow creating resizable shared structures after startup.
> Hence creating new resizable structures after startup is now prohibited.
> Attaching to existing structures after startup remains supported.
>
> Here's a brief description of the patches attached.
> 0001 - Exposes PID of backend as a member of BackgroundPsql() object. The
> PID is fetched and cached upon a successful BackgroundPsql object creation.
> The tests in this and the latest patchset on the other thread [1] use this
> facility. But there are existing TAP tests which can use this too. If this
> patch is acceptable I will change a few existing tests to use this
> infrastructure and we can commit this patch separately.
> 0002 - is being discussed in [2] and can be committed separately.
> 0003 and 0004 - are proposed in [3]
> 0005 is the main patch supporting resizable shared structures.
>
> At this stage all the large pieces of the puzzle to support resizable
> shared structures are in place, IMO. The discussion remains open to figure
> out how far we want to go supporting resizable structures for extensions
> (e.g. providing barrier mechanism to reprotect the structures). Since the
> chief consumer of this work is resizable shared structures, the current
> support suffices and can be enhanced later.
>
> Also we may discuss whether the test verifying the actual memory
> allocations against the size of resizable structure is worth the
> complexity. But it has served its purpose as the implementation has
> undergone several changes, so I would like to keep that test as far as
> possible.
>
> Next I will be self-reviewing the code in detail.
>
> [1]
> https://www.postgresql.org/message-id/cnthxg2eekacrejyeonuhiaezc7vd7o2uowlsbenxqfkjwgvwj%40qgzu6eoqrglb
> [2]
> https://www.postgresql.org/message-id/flat/CAExHW5ttQauXiYCM4OKL8xKHZrndTN2jMN4WQjm1PkPNuqX_bg(at)mail(dot)gmail(dot)com
> [3]
> https://www.postgresql.org/message-id/flat/CAExHW5vuMVLMOf28aD75YKS66gyu3d55+afk4G7igGFWwuQvHQ(at)mail(dot)gmail(dot)com
>
>
Here's patchseries fixing the CF Bot failures: a. Reorder postgres -C and
-D arguments b. fix MacOS compilation failure.

--
Best Wishes,
Ashutosh Bapat

Attachment Content-Type Size
v20261204-0004-Record-the-main-shared-memory-segment-s-pa.patch text/x-patch 6.4 KB
v20261204-0001-PID-of-the-backend-process-backing-the-Bac.patch text/x-patch 3.1 KB
v20261204-0003-pg_get_shmem_pagesize-on-Windows.patch text/x-patch 1.7 KB
v20261204-0002-Call-CalculateShmemSize-only-once-when-cre.patch text/x-patch 8.8 KB
v20261204-0005-Resizable-shared-memory-structures.patch text/x-patch 167.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Hüseyin Demir 2026-12-04 15:14:17 Re: [PATCH] Report changes discarded for relations not in the subscription
Previous Message Fujii Masao 2026-12-04 14:55:51 Re: [PATCH] Add sampling statistics to autoanalyze log output