[PATCH] Assertion in pg_get_shmem_pagesize() in single-user mode

Started by Ayush Tiwari2 months ago2 messageshackers
Jump to latest
#1Ayush Tiwari
ayushtiwari.slg01@gmail.com

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

Attachments:

t253872_1
v1-0001-Fix-shared-memory-page-size-assertion-in-single-user.patchapplication/x-patch; name=v1-0001-Fix-shared-memory-page-size-assertion-in-single-user.patchDownload+1-2
#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Ayush Tiwari (#1)
Re: [PATCH] Assertion in pg_get_shmem_pagesize() in single-user mode

On 21/09/2026 02:51, Ayush Tiwari wrote:

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.

Agreed.

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?

+1. Committed, thanks!

- Heikki