From 5ea6d1098c7ec37acaa09b6a1605982666187207 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Thu, 20 Aug 2026 22:34:03 +0530 Subject: [PATCH v1] Fix after-startup shmem allocation in single-user mode RegisterShmemCallbacks() acts on a request immediately when it is called after startup, but only if IsUnderPostmaster is set. That is presumably meant to exclude the postmaster, but it is also false in a standalone backend, which therefore adds the callbacks to a list that startup has already consumed. An extension loaded after startup in single-user mode, e.g. with LOAD or CREATE EXTENSION, never gets its callbacks called: no memory is reserved, and the caller's pointer variable is left NULL without any error being raised. Also check !IsPostmasterEnvironment, which distinguishes a standalone backend from the postmaster. Add a single-user case to the test_shmem TAP test, which previously only exercised postmaster-managed startup. --- src/backend/storage/ipc/shmem.c | 4 +++- .../test_shmem/t/001_late_shmem_alloc.pl | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index 228871d2525..3039d6d8d6e 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -872,7 +872,9 @@ ShmemAddrIsValid(const void *addr) void RegisterShmemCallbacks(const ShmemCallbacks *callbacks) { - if (shmem_request_state == SRS_DONE && IsUnderPostmaster) + /* a standalone backend has no postmaster, but can still allocate here */ + if (shmem_request_state == SRS_DONE && + (IsUnderPostmaster || !IsPostmasterEnvironment)) { /* * After-startup initialization or attachment. Call the appropriate diff --git a/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl b/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl index 5cf07d071ec..32bbfb634be 100644 --- a/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl +++ b/src/test/modules/test_shmem/t/001_late_shmem_alloc.pl @@ -27,6 +27,28 @@ cmp_ok($attach_count2, '>', $attach_count1, "attach callback is called in each backend"); $node->stop; +### +# Test allocating memory after startup in single-user mode, where there is +# no postmaster +### +SKIP: +{ + skip 'single-user mode is not supported on this platform', 1 + if $windows_os; + + my $query = "SELECT get_test_shmem_attach_count();\n"; + my $result = run_log( + [ + 'postgres', '--single', '-F', + '-c' => 'exit_on_error=true', + '-D' => $node->data_dir, + 'postgres' + ], + '<' => \$query); + + ok($result, "shmem area is initialized in single-user mode"); +} + ### # Test that loading via shared_preload_libraries also works ### -- 2.34.1