Re: RegisterShmemCallbacks() does nothing in single-user mode

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Subject: Re: RegisterShmemCallbacks() does nothing in single-user mode
Date: 2026-08-25 21:39:55
Message-ID: 2b9fca08-ce59-411a-9ff7-a51764b7dd0b@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 21/08/2026 08:36, Ayush Tiwari wrote:
> Looking at the after-startup path from 283e823f9dc, I noticed that
> RegisterShmemCallbacks() does not do anything useful in single-user mode.
> The branch that does the work immediately is guarded by:
>
>     if (shmem_request_state == SRS_DONE && IsUnderPostmaster)
>
> IsUnderPostmaster is false in a standalone backend as well as in the
> postmaster, so a standalone backend always falls through to the "remember
> the callbacks for later" branch, and startup has already consumed that
> list.  An extension loaded at runtime there, e.g. with LOAD or CREATE
> EXTENSION, therefore never has its callbacks called, and
> RegisterShmemCallbacks() reports nothing:
>
>     LOG:  test_shmem module's _PG_init called
>     ERROR:  shmem area not attached or initialized in this process
>
> The LOG line is the only trace of the registration.  The ERROR is
> test_shmem's own guard when the area is later used, not the shmem code;
> an extension without such a guard would dereference a NULL pointer?
>
> I am unsure whether this was left out intentionally or simply missed, but
> it seems worth having: a standalone backend has shared memory and the
> same after-startup reserve, and a silent no-op is easy to mistake for
> success.

That was an oversight, thanks for the testing!

> The attached patch also checks !IsPostmasterEnvironment and adds a
> single-user case to the test_shmem TAP test.

Hmm, I think the check should be just "if (shmem_request_state ==
SRS_DONE)", with an assert that it's not called from the postmaster. The
reason it cannot be called from the postmaster after startup is that
postmaster cannot acquire lwlocks.

> skip 'single-user mode is not supported on this platform', 1
> if $windows_os;

Huh, we don't support single-user mode on Windows? /me looks around. We
do, but apparently there are some issues with it in the CI. All other
single-user tests are also skipped on Windows. Per commit 1f2e51e3c7:

> These tests are skipped on Windows, as direct calls of
> postgres --single would fail on permission failures. There is no
> platform-specific behavior that needs to be checked, so living with this
> restriction should be fine. The CI is OK with that, now let's see what
> the buildfarm tells.

Ok then I guess, but I'll copy the comment from that commit to explain
why it's not run on Windows.

See attached.

- Heikki

Attachment Content-Type Size
v2-0001-Fix-registering-shmem-callbacks-in-single-user-mo.patch text/x-patch 2.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2026-08-25 22:24:00 Re: Assertion failure in GetSubscriptionRelations() with concurrent DROP TABLE
Previous Message Daniel Gustafsson 2026-08-25 21:36:07 Re: Changing client-side behavior regarding Certificate Revocation Lists (CRL)