| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# |
| 5 |
# Tests of pg_shmem.h functions |
| 6 |
# |
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
use File::stat qw(stat); |
| 10 |
use IPC::Run 'run'; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
use Test::More; |
| 13 |
use PostgreSQL::Test::Utils; |
| 14 |
use Time::HiRes qw(usleep); |
| 15 |
|
| 16 |
# If we don't have shmem support, skip the whole thing |
| 17 |
eval { |
| 18 |
require IPC::SharedMem; |
| 19 |
IPC::SharedMem->import; |
| 20 |
require IPC::SysV; |
| 21 |
IPC::SysV->import(qw(IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR)); |
| 22 |
}; |
| 23 |
if ($@ || $windows_os) |
| 24 |
{ |
| 25 |
plan skip_all => 'SysV shared memory not supported by this platform'; |
| 26 |
} |
| 27 |
|
| 28 |
my $tempdir = PostgreSQL::Test::Utils::tempdir; |
| 29 |
|
| 30 |
# Log "ipcs" diffs on a best-effort basis, swallowing any error. |
| 31 |
my $ipcs_before = "$tempdir/ipcs_before"; |
| 32 |
eval { run_log [ 'ipcs', '-am' ], '>' => $ipcs_before; }; |
| 33 |
|
| 34 |
sub log_ipcs |
| 35 |
{ |
| 36 |
eval { run_log [ 'ipcs', '-am' ], '|' => [ 'diff', $ipcs_before, '-' ] }; |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
# Node setup. |
| 41 |
my $gnat = PostgreSQL::Test::Cluster->new('gnat'); |
| 42 |
$gnat->init; |
| 43 |
|
| 44 |
# Create a shmem segment that will conflict with gnat's first choice |
| 45 |
# of shmem key. (If we fail to create it because something else is |
| 46 |
# already using that key, that's perfectly fine, though the test will |
| 47 |
# exercise a different scenario than it usually does.) |
| 48 |
my $gnat_dir_stat = stat($gnat->data_dir); |
| 49 |
defined($gnat_dir_stat) or die('unable to stat ' . $gnat->data_dir); |
| 50 |
my $gnat_inode = $gnat_dir_stat->ino; |
| 51 |
note "gnat's datadir inode = $gnat_inode"; |
| 52 |
|
| 53 |
# Note: must reference IPC::SysV's constants as functions, or this file |
| 54 |
# fails to compile when that module is not available. |
| 55 |
my $gnat_conflict_shm = |
| 56 |
IPC::SharedMem->new($gnat_inode, 1024, |
| 57 |
IPC_CREAT() | IPC_EXCL() | S_IRUSR() | S_IWUSR()); |
| 58 |
note "could not create conflicting shmem" if !defined($gnat_conflict_shm); |
| 59 |
log_ipcs(); |
| 60 |
|
| 61 |
$gnat->start; |
| 62 |
log_ipcs(); |
| 63 |
|
| 64 |
$gnat->restart; # should keep same shmem key |
| 65 |
log_ipcs(); |
| 66 |
|
| 67 |
# Upon postmaster death, postmaster children exit automatically. |
| 68 |
$gnat->kill9; |
| 69 |
log_ipcs(); |
| 70 |
poll_start($gnat); # gnat recycles its former shm key. |
| 71 |
log_ipcs(); |
| 72 |
|
| 73 |
note "removing the conflicting shmem ..."; |
| 74 |
$gnat_conflict_shm->remove if $gnat_conflict_shm; |
| 75 |
log_ipcs(); |
| 76 |
|
| 77 |
# Upon postmaster death, postmaster children exit automatically. |
| 78 |
$gnat->kill9; |
| 79 |
log_ipcs(); |
| 80 |
|
| 81 |
# In this start, gnat will use its normal shmem key, and fail to remove |
| 82 |
# the higher-keyed segment that the previous postmaster was using. |
| 83 |
# That's not great, but key collisions should be rare enough to not |
| 84 |
# make this a big problem. |
| 85 |
poll_start($gnat); |
| 86 |
log_ipcs(); |
| 87 |
$gnat->stop; |
| 88 |
log_ipcs(); |
| 89 |
|
| 90 |
# Re-create the conflicting segment, and start/stop normally, just so |
| 91 |
# this test script doesn't leak the higher-keyed segment. |
| 92 |
note "re-creating conflicting shmem ..."; |
| 93 |
$gnat_conflict_shm = |
| 94 |
IPC::SharedMem->new($gnat_inode, 1024, |
| 95 |
IPC_CREAT() | IPC_EXCL() | S_IRUSR() | S_IWUSR()); |
| 96 |
note "could not create conflicting shmem" if !defined($gnat_conflict_shm); |
| 97 |
log_ipcs(); |
| 98 |
|
| 99 |
$gnat->start; |
| 100 |
log_ipcs(); |
| 101 |
$gnat->stop; |
| 102 |
log_ipcs(); |
| 103 |
|
| 104 |
note "removing the conflicting shmem ..."; |
| 105 |
$gnat_conflict_shm->remove if $gnat_conflict_shm; |
| 106 |
log_ipcs(); |
| 107 |
|
| 108 |
# Scenarios involving no postmaster.pid, dead postmaster, and a live backend. |
| 109 |
# Use a regress.c function to emulate the responsiveness of a backend working |
| 110 |
# through a CPU-intensive task. |
| 111 |
$gnat->start; |
| 112 |
log_ipcs(); |
| 113 |
|
| 114 |
my $regress_shlib = $ENV{REGRESS_SHLIB}; |
| 115 |
$gnat->safe_psql('postgres', <<EOSQL); |
| 116 |
CREATE FUNCTION wait_pid(int) |
| 117 |
RETURNS void |
| 118 |
AS '$regress_shlib' |
| 119 |
LANGUAGE C STRICT; |
| 120 |
EOSQL |
| 121 |
my $slow_query = 'SELECT wait_pid(pg_backend_pid())'; |
| 122 |
my ($stdout, $stderr); |
| 123 |
my $slow_client = IPC::Run::start( |
| 124 |
[ |
| 125 |
'psql', '--no-psqlrc', '--quiet', '--no-align', '--tuples-only', |
| 126 |
'--dbname' => $gnat->connstr('postgres'), |
| 127 |
'--command' => $slow_query |
| 128 |
], |
| 129 |
'<' => \undef, |
| 130 |
'>' => \$stdout, |
| 131 |
'2>' => \$stderr, |
| 132 |
IPC::Run::timeout(5 * $PostgreSQL::Test::Utils::timeout_default)); |
| 133 |
ok( $gnat->poll_query_until( |
| 134 |
'postgres', |
| 135 |
"SELECT 1 FROM pg_stat_activity WHERE query = '$slow_query'", '1'), |
| 136 |
'slow query started'); |
| 137 |
my $slow_pid = $gnat->safe_psql('postgres', |
| 138 |
"SELECT pid FROM pg_stat_activity WHERE query = '$slow_query'"); |
| 139 |
$gnat->kill9; |
| 140 |
unlink($gnat->data_dir . '/postmaster.pid'); |
| 141 |
$gnat->rotate_logfile; # on Windows, can't open old log for writing |
| 142 |
log_ipcs(); |
| 143 |
# Reject ordinary startup. Retry for the same reasons poll_start() does, |
| 144 |
# every 0.1s for at least $PostgreSQL::Test::Utils::timeout_default seconds. |
| 145 |
my $pre_existing_msg = qr/pre-existing shared memory block/; |
| 146 |
{ |
| 147 |
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default; |
| 148 |
my $attempts = 0; |
| 149 |
while ($attempts < $max_attempts) |
| 150 |
{ |
| 151 |
last |
| 152 |
if $gnat->start(fail_ok => 1) |
| 153 |
|| slurp_file($gnat->logfile) =~ $pre_existing_msg; |
| 154 |
usleep(100_000); |
| 155 |
$attempts++; |
| 156 |
} |
| 157 |
} |
| 158 |
like(slurp_file($gnat->logfile), |
| 159 |
$pre_existing_msg, 'detected live backend via shared memory'); |
| 160 |
# Reject single-user startup. |
| 161 |
command_fails_like( |
| 162 |
[ |
| 163 |
'postgres', '--single', |
| 164 |
'-D' => $gnat->data_dir, |
| 165 |
'template1' |
| 166 |
], |
| 167 |
$pre_existing_msg, |
| 168 |
'single-user mode detected live backend via shared memory'); |
| 169 |
log_ipcs(); |
| 170 |
|
| 171 |
# cleanup slow backend |
| 172 |
PostgreSQL::Test::Utils::system_log('pg_ctl', 'kill', 'QUIT', $slow_pid); |
| 173 |
$slow_client->finish; # client has detected backend termination |
| 174 |
log_ipcs(); |
| 175 |
|
| 176 |
# now startup should work |
| 177 |
poll_start($gnat); |
| 178 |
log_ipcs(); |
| 179 |
|
| 180 |
# finish testing |
| 181 |
$gnat->stop; |
| 182 |
log_ipcs(); |
| 183 |
|
| 184 |
|
| 185 |
# We may need retries to start a new postmaster. Causes: |
| 186 |
# - kernel is slow to deliver SIGKILL |
| 187 |
# - postmaster parent is slow to waitpid() |
| 188 |
# - postmaster child is slow to exit in response to SIGQUIT |
| 189 |
# - postmaster child is slow to exit after postmaster death |
| 190 |
sub poll_start |
| 191 |
{ |
| 192 |
my ($node) = @_; |
| 193 |
|
| 194 |
my $max_attempts = 10 * $PostgreSQL::Test::Utils::timeout_default; |
| 195 |
my $attempts = 0; |
| 196 |
|
| 197 |
while ($attempts < $max_attempts) |
| 198 |
{ |
| 199 |
$node->start(fail_ok => 1) && return 1; |
| 200 |
|
| 201 |
# Wait 0.1 second before retrying. |
| 202 |
usleep(100_000); |
| 203 |
|
| 204 |
# Clean up in case the start attempt just timed out or some such. |
| 205 |
$node->stop('fast', fail_ok => 1); |
| 206 |
|
| 207 |
$attempts++; |
| 208 |
} |
| 209 |
|
| 210 |
# Try one last time without fail_ok, which will BAIL_OUT unless it |
| 211 |
# succeeds. |
| 212 |
$node->start && return 1; |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
done_testing(); |