Hi,

While trying pg_buffercache in single-user mode, I noticed what looks
like an overly restrictive assertion in pg_get_shmem_pagesize(). With
pg_buffercache installed and the server stopped, I can reproduce this
on an assertion-enabled build:

    $ postgres --single -D "$PGDATA" postgres
    backend> SELECT pg_buffercache_os_pages(false) LIMIT 1;
    TRAP: failed Assert("IsUnderPostmaster")

AFAICS, PostgresSingleUserMain() calls
CreateSharedMemoryAndSemaphores() before processing queries, so
huge_pages_status should already be initialized at this point.
However, IsUnderPostmaster is false in a standalone backend.

Could we drop that assertion and retain the existing
Assert(huge_pages_status != HUGE_PAGES_UNKNOWN)? That seems to check
the relevant precondition without excluding single-user mode. This
also doesn't appear specific to NUMA, since the query above requests
only OS-page mappings.

The attached patch removes the process-role assertion and adjusts the
comment to refer to shared-memory creation. The query seems to work
with this change in my local testing.

Does this look reasonable, or am I overlooking another reason for
the IsUnderPostmaster check?

Regards,
Ayush
From 6de169c09ccb17e82e89ccaf09286a6c07334517 Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <[email protected]>
Date: Sun, 20 Sep 2026 14:28:35 +0530
Subject: [PATCH v1] Fix shared-memory page-size assertion in single-user mode

pg_get_shmem_pagesize() asserts IsUnderPostmaster, but a standalone
backend also creates the shared memory segment and initializes
huge_pages_status.  Querying pg_buffercache_os_pages in single-user mode
therefore aborts in an assertion-enabled build, even without NUMA support.
The NUMA inspection functions using this helper are affected too.

Remove the process-role assertion and retain the check that
huge_pages_status is initialized.  Clarify that the helper requires the
shared memory segment to have been created, not a postmaster child.
---
 src/backend/storage/ipc/shmem.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 3345735dff0..1fcab27965e 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -1378,7 +1378,7 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS)
  * If the shared segment was allocated using huge pages, returns the size of
  * a huge page. Otherwise returns the size of regular memory page.
  *
- * This should be used only after the server is started.
+ * This should be used only after the shared memory segment is created.
  */
 Size
 pg_get_shmem_pagesize(void)
@@ -1393,7 +1393,6 @@ pg_get_shmem_pagesize(void)
 	os_page_size = sysconf(_SC_PAGESIZE);
 #endif
 
-	Assert(IsUnderPostmaster);
 	Assert(huge_pages_status != HUGE_PAGES_UNKNOWN);
 
 	if (huge_pages_status == HUGE_PAGES_ON)

base-commit: bee81f7864621117f077a2694b63265378bbd7b9

Reply via email to