pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
RegisterShmemCallbacks() does nothing in single-user mode
6+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-08-26 edc095a265 b259ece92a  Fix registering shmem callbacks in single-user mode
* RegisterShmemCallbacks() does nothing in single-user mode
@ 2026-08-21 05:36 Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  2026-08-25 20:00 ` Re: RegisterShmemCallbacks() does nothing in single-user mode Nathan Bossart <nathandbossart@gmail.com>
  2026-08-25 21:39 ` Re: RegisterShmemCallbacks() does nothing in single-user mode Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 2 replies; 6+ messages in thread

From: Ayush Tiwari @ 2026-08-21 05:36 UTC (permalink / raw)
  To: pgsql-hackers; +Cc: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>

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.

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

Thoughts?

Regards,
Ayush

Attachments:

v1-0001-Fix-after-startup-shmem-allocation-in-single-user.patch (application/octet-stream, 2.7K) download From 5ea6d1098c7ec37acaa09b6a1605982666187207 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari <ayushtiwari.slg01@gmail.com> 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
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: RegisterShmemCallbacks() does nothing in single-user mode
  2026-08-21 05:36 RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-08-25 20:00 ` Nathan Bossart <nathandbossart@gmail.com>
  2026-08-25 20:19   ` Re: RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Nathan Bossart @ 2026-08-25 20:00 UTC (permalink / raw)
  To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>

Does this one deserve a mention on the open items wiki [0]?

[0] https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items

-- 
nathan





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: RegisterShmemCallbacks() does nothing in single-user mode
  2026-08-21 05:36 RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  2026-08-25 20:00 ` Re: RegisterShmemCallbacks() does nothing in single-user mode Nathan Bossart <nathandbossart@gmail.com>
@ 2026-08-25 20:19   ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Ayush Tiwari @ 2026-08-25 20:19 UTC (permalink / raw)
  To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>

Hi,

On Wed, 26 Aug 2026 at 01:31, Nathan Bossart <nathandbossart@gmail.com>
wrote:

> Does this one deserve a mention on the open items wiki [0]?
>
> [0] https://wiki.postgresql.org/wiki/PostgreSQL_19_Open_Items


I was looking through your emails and wondering whether I should add
this to the open items list before you got to this one. :p Thanks for
reviewing so many patches and calling them out!

Since this fails silently and does not appear to be documented either,
I'll add an open item for it, if the commit owners feels otherwise, they
can resolve the item accordingly.

Regards,
Ayush

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: RegisterShmemCallbacks() does nothing in single-user mode
  2026-08-21 05:36 RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-08-25 21:39 ` Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-26 06:24   ` Re: RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2026-08-25 21:39 UTC (permalink / raw)
  To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; pgsql-hackers; +Cc: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

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

Attachments:

v2-0001-Fix-registering-shmem-callbacks-in-single-user-mo.patch (text/x-patch, 2.9K) download From c5f5ed2ee4c2219bad6d9079ccb09cc51f2ff7d8 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Wed, 26 Aug 2026 00:39:31 +0300 Subject: [PATCH v2 1/1] Fix registering shmem callbacks in single-user mode RegisterShmemCallbacks() should not be called from the postmaster process after postmaster startup. Add an assertion for that, and fix the check for whether it's being called for "after startup" allocations to take single-user mode into account. Reported-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com> Discussion: https://www.postgresql.org/message-id/CAJTYsWU7epjUL1-xrpYeOrmkmq20M2Q22Y_SRb8PWQG8KNQyDw@mail.gmail.com Backpatch-through: 19 --- src/backend/storage/ipc/shmem.c | 11 +++++++-- .../test_shmem/t/001_late_shmem_alloc.pl | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index c511395b1f0..0200bcda2cd 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -911,23 +911,30 @@ ShmemAddrIsValid(const void *addr) void RegisterShmemCallbacks(const ShmemCallbacks *callbacks) { - if (shmem_request_state == SRS_DONE && IsUnderPostmaster) + if (shmem_request_state == SRS_DONE) { /* * After-startup initialization or attachment. Call the appropriate * callbacks immediately. + * + * This is not allowed from the postmaster, because the postmaster + * cannot acquire locks. */ + Assert(IsUnderPostmaster || !IsPostmasterEnvironment); + if ((callbacks->flags & SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP) == 0) elog(ERROR, "cannot request shared memory at this time"); CallShmemCallbacksAfterStartup(callbacks); } - else + else if (shmem_request_state == SRS_INITIAL) { /* Remember the callbacks for later */ registered_shmem_callbacks = lappend(registered_shmem_callbacks, (void *) callbacks); } + else + elog(ERROR, "cannot request shared memory at this time"); } /* 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 b09889f4fb0..d2bf32a72fe 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 @@ -100,4 +100,28 @@ else } $node->stop; + +### +# Test allocating memory after startup in single-user mode +### +SKIP: +{ + # Skip the test on Windows, as single-user mode would fail on permission + # failure with privileged accounts. + 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"); +} + done_testing(); -- 2.47.3
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: RegisterShmemCallbacks() does nothing in single-user mode
  2026-08-21 05:36 RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  2026-08-25 21:39 ` Re: RegisterShmemCallbacks() does nothing in single-user mode Heikki Linnakangas <hlinnaka@iki.fi>
@ 2026-08-26 06:24   ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  2026-08-26 10:01     ` Re: RegisterShmemCallbacks() does nothing in single-user mode Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 6+ messages in thread

From: Ayush Tiwari @ 2026-08-26 06:24 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnaka@iki.fi>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

Hi,

On Wed, 26 Aug 2026 at 03:10, Heikki Linnakangas <hlinnaka@iki.fi> wrote:

> 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.
>

Thanks for the updated flow, it looks much better.

I think the new test may need to be moved earlier in the TAP script.  It
currently runs after shared_preload_libraries has been set to test_shmem,
so the standalone backend loads and initializes test_shmem during startup.
The later SELECT therefore uses an area that was already initialized,
rather than exercising after-startup registration.

Would it make sense to move that block just after the first $node->stop?

Regards,
Ayush

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: RegisterShmemCallbacks() does nothing in single-user mode
  2026-08-21 05:36 RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  2026-08-25 21:39 ` Re: RegisterShmemCallbacks() does nothing in single-user mode Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-26 06:24   ` Re: RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-08-26 10:01     ` Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 0 replies; 6+ messages in thread

From: Heikki Linnakangas @ 2026-08-26 10:01 UTC (permalink / raw)
  To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

On 26/08/2026 09:24, Ayush Tiwari wrote:
> Thanks for the updated flow, it looks much better.
> 
> I think the new test may need to be moved earlier in the TAP script.  It
> currently runs after shared_preload_libraries has been set to test_shmem,
> so the standalone backend loads and initializes test_shmem during startup.
> The later SELECT therefore uses an area that was already initialized,
> rather than exercising after-startup registration.
> 
> Would it make sense to move that block just after the first $node->stop?

Good catch. Moved the test to fix that and committed. Thanks!

- Heikki






^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-08-26 10:01 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-21 05:36 RegisterShmemCallbacks() does nothing in single-user mode Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-08-25 20:00 ` Nathan Bossart <nathandbossart@gmail.com>
2026-08-25 20:19   ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-08-25 21:39 ` Heikki Linnakangas <hlinnaka@iki.fi>
2026-08-26 06:24   ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-08-26 10:01     ` Heikki Linnakangas <hlinnaka@iki.fi>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox