1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

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.

Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAJTYsWU7epjUL1-xrpYeOrmkmq20M2Q22Y_SRb8PWQG8KNQyDw@mail.gmail.com
Backpatch-through: 19
This commit is contained in:
Heikki Linnakangas
2026-08-26 12:59:18 +03:00
parent 4680f32d33
commit b259ece92a
2 changed files with 30 additions and 2 deletions

View File

@@ -872,23 +872,29 @@ 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.
*/
if ((callbacks->flags & SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP) == 0)
elog(ERROR, "cannot request shared memory at this time");
Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
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");
}
/*

View File

@@ -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
###
SKIP:
{
# Skip the test on Windows, as single-user mode would fail on permission
# failure with privileged accounts.
skip 'single-user test is not supported by 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
###