pgsql: Introduce a new mechanism for registering shared memory areas

Started by Heikki Linnakangas8 months ago3 messagescommitters
Jump to latest
#1Heikki Linnakangas
heikki.linnakangas@iki.fi

Introduce a new mechanism for registering shared memory areas

This replaces the [Subsystem]ShmemSize() and [Subsystem]ShmemInit()
functions called at postmaster startup with a new set of callbacks.
The new mechanism is designed to be more ergonomic. Notably, the size
of each shmem area is specified in the same ShmemRequestStruct() call,
together with its name. The same mechanism is used in extensions,
replacing the shmem_{request/startup}_hooks.

ShmemInitStruct() and ShmemInitHash() become backwards-compatibility
wrappers around the new functions. In future commits, I will replace
all ShmemInitStruct() and ShmemInitHash() calls with the new
functions, although we'll still need to keep them around for
extensions.

Co-authored-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: /messages/by-id/CAExHW5vM1bneLYfg0wGeAa=52UiJ3z4vKd3AJ72X8Fw6k3KKrg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/283e823f9dcb03d0be720928b261628af06d3fd4

Modified Files
--------------
doc/src/sgml/system-views.sgml | 4 +-
doc/src/sgml/xfunc.sgml | 158 ++++--
src/backend/bootstrap/bootstrap.c | 2 +
src/backend/postmaster/launch_backend.c | 4 +
src/backend/postmaster/postmaster.c | 19 +-
src/backend/storage/ipc/ipci.c | 29 +-
src/backend/storage/ipc/shmem.c | 824 ++++++++++++++++++++++++++++----
src/backend/storage/ipc/shmem_hash.c | 79 ++-
src/backend/storage/lmgr/proc.c | 3 +
src/backend/tcop/postgres.c | 10 +-
src/backend/utils/hash/dynahash.c | 4 +-
src/include/storage/shmem.h | 159 +++++-
src/include/storage/shmem_internal.h | 41 +-
src/tools/pgindent/typedefs.list | 7 +-
14 files changed, 1144 insertions(+), 199 deletions(-)

#2Aleksander Alekseev
aleksander@tigerdata.com
In reply to: Heikki Linnakangas (#1)
Re: pgsql: Introduce a new mechanism for registering shared memory areas

Hi Heikki,

Introduce a new mechanism for registering shared memory areas

[...]

This commit introduced a memory leak which Valgrind is very much upset about.

ShmemRequestStructWithOpts() allocates a copy of `options` in
TopMemoryContext and passes it to ShmemRequestInternal(). It appends
it to pending_shmem_requests as request->options. Later in
ShmemInitRequested() when the list is freed `->options` leak. There
are similar issues in ShmemAttachRequested() and
CallShmemCallbacksAfterStartup() which free pending_shmem_requests
without freeing `->options`.

I propose to fix it as attached.

--
Best regards,
Aleksander Alekseev

Attachments:

v1-0001-Fix-memory-leaks-introduced-by-commit-283e823f9dc.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Fix-memory-leaks-introduced-by-commit-283e823f9dc.patchDownload+4-2
#3Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Aleksander Alekseev (#2)
Re: pgsql: Introduce a new mechanism for registering shared memory areas

On 06/04/2026 14:55, Aleksander Alekseev wrote:

Introduce a new mechanism for registering shared memory areas

[...]

This commit introduced a memory leak which Valgrind is very much upset about.

ShmemRequestStructWithOpts() allocates a copy of `options` in
TopMemoryContext and passes it to ShmemRequestInternal(). It appends
it to pending_shmem_requests as request->options. Later in
ShmemInitRequested() when the list is freed `->options` leak. There
are similar issues in ShmemAttachRequested() and
CallShmemCallbacksAfterStartup() which free pending_shmem_requests
without freeing `->options`.

I propose to fix it as attached.

LGTM, I will push this shortly. Thanks!

- Heikki