| 1 |
# Copyright (c) 2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings FATAL => 'all'; |
| 5 |
|
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
use List::Util qw(shuffle); |
| 10 |
|
| 11 |
|
| 12 |
my $node = PostgreSQL::Test::Cluster->new('worker'); |
| 13 |
$node->init(); |
| 14 |
$node->append_conf( |
| 15 |
'postgresql.conf', qq( |
| 16 |
io_method=worker |
| 17 |
)); |
| 18 |
|
| 19 |
$node->start(); |
| 20 |
|
| 21 |
# Test changing the number of I/O worker processes while also evaluating the |
| 22 |
# handling of their termination. |
| 23 |
test_number_of_io_workers_dynamic($node); |
| 24 |
|
| 25 |
$node->stop(); |
| 26 |
|
| 27 |
done_testing(); |
| 28 |
|
| 29 |
|
| 30 |
sub test_number_of_io_workers_dynamic |
| 31 |
{ |
| 32 |
my $node = shift; |
| 33 |
|
| 34 |
my $prev_worker_count = $node->safe_psql('postgres', 'SHOW io_workers'); |
| 35 |
|
| 36 |
# Verify that worker count can't be set to 0 |
| 37 |
change_number_of_io_workers($node, 0, $prev_worker_count, 1); |
| 38 |
|
| 39 |
# Verify that worker count can't be set to 33 (above the max) |
| 40 |
change_number_of_io_workers($node, 33, $prev_worker_count, 1); |
| 41 |
|
| 42 |
# Try changing IO workers to a random value and verify that the worker |
| 43 |
# count ends up as expected. Always test the min/max of workers. |
| 44 |
# |
| 45 |
# Valid range for io_workers is [1, 32]. 8 tests in total seems |
| 46 |
# reasonable. |
| 47 |
my @io_workers_range = shuffle(1 ... 32); |
| 48 |
foreach my $worker_count (1, 32, @io_workers_range[ 0, 6 ]) |
| 49 |
{ |
| 50 |
$prev_worker_count = |
| 51 |
change_number_of_io_workers($node, $worker_count, |
| 52 |
$prev_worker_count, 0); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
sub change_number_of_io_workers |
| 57 |
{ |
| 58 |
my $node = shift; |
| 59 |
my $worker_count = shift; |
| 60 |
my $prev_worker_count = shift; |
| 61 |
my $expect_failure = shift; |
| 62 |
my ($result, $stdout, $stderr); |
| 63 |
|
| 64 |
($result, $stdout, $stderr) = |
| 65 |
$node->psql('postgres', "ALTER SYSTEM SET io_workers = $worker_count"); |
| 66 |
$node->safe_psql('postgres', 'SELECT pg_reload_conf()'); |
| 67 |
|
| 68 |
if ($expect_failure) |
| 69 |
{ |
| 70 |
ok( $stderr =~ |
| 71 |
/$worker_count is outside the valid range for parameter "io_workers"/, |
| 72 |
"updating number of io_workers to $worker_count failed, as expected" |
| 73 |
); |
| 74 |
|
| 75 |
return $prev_worker_count; |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
is( $node->safe_psql('postgres', 'SHOW io_workers'), |
| 80 |
$worker_count, |
| 81 |
"updating number of io_workers from $prev_worker_count to $worker_count" |
| 82 |
); |
| 83 |
|
| 84 |
check_io_worker_count($node, $worker_count); |
| 85 |
terminate_io_worker($node, $worker_count); |
| 86 |
check_io_worker_count($node, $worker_count); |
| 87 |
|
| 88 |
return $worker_count; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
sub terminate_io_worker |
| 93 |
{ |
| 94 |
my $node = shift; |
| 95 |
my $worker_count = shift; |
| 96 |
my ($pid, $ret); |
| 97 |
|
| 98 |
# Select a random io worker |
| 99 |
$pid = $node->safe_psql( |
| 100 |
'postgres', |
| 101 |
qq(SELECT pid FROM pg_stat_activity WHERE |
| 102 |
backend_type = 'io worker' ORDER BY RANDOM() LIMIT 1)); |
| 103 |
|
| 104 |
# terminate IO worker with SIGINT |
| 105 |
is(PostgreSQL::Test::Utils::system_log('pg_ctl', 'kill', 'INT', $pid), |
| 106 |
0, "random io worker process signalled with INT"); |
| 107 |
|
| 108 |
# Check that worker exits |
| 109 |
ok( $node->poll_query_until( |
| 110 |
'postgres', |
| 111 |
qq(SELECT COUNT(*) FROM pg_stat_activity WHERE pid = $pid), '0'), |
| 112 |
"random io worker process exited after signal"); |
| 113 |
} |
| 114 |
|
| 115 |
sub check_io_worker_count |
| 116 |
{ |
| 117 |
my $node = shift; |
| 118 |
my $worker_count = shift; |
| 119 |
|
| 120 |
ok( $node->poll_query_until( |
| 121 |
'postgres', |
| 122 |
qq(SELECT COUNT(*) FROM pg_stat_activity WHERE backend_type = 'io worker'), |
| 123 |
$worker_count), |
| 124 |
"io worker count is $worker_count"); |
| 125 |
} |