| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Tests for connection behavior prior to authentication. |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
|
| 9 |
use PostgreSQL::Test::Cluster; |
| 10 |
use PostgreSQL::Test::Utils; |
| 11 |
use Time::HiRes qw(usleep); |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
if ($ENV{enable_injection_points} ne 'yes') |
| 15 |
{ |
| 16 |
plan skip_all => 'Injection points not supported by this build'; |
| 17 |
} |
| 18 |
|
| 19 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 20 |
$node->init; |
| 21 |
$node->append_conf( |
| 22 |
'postgresql.conf', q[ |
| 23 |
log_connections = 'receipt,authentication' |
| 24 |
]); |
| 25 |
|
| 26 |
$node->start; |
| 27 |
|
| 28 |
# Check if the extension injection_points is available, as it may be |
| 29 |
# possible that this script is run with installcheck, where the module |
| 30 |
# would not be installed by default. |
| 31 |
if (!$node->check_extension('injection_points')) |
| 32 |
{ |
| 33 |
plan skip_all => 'Extension injection_points not installed'; |
| 34 |
} |
| 35 |
|
| 36 |
$node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); |
| 37 |
|
| 38 |
# Connect to the server and inject a waitpoint. |
| 39 |
my $psql = $node->background_psql('postgres'); |
| 40 |
$psql->query_safe("SELECT injection_points_attach('init-pre-auth', 'wait')"); |
| 41 |
|
| 42 |
# From this point on, all new connections will hang during startup, just before |
| 43 |
# authentication. Use the $psql connection handle for server interaction. |
| 44 |
my $conn = $node->background_psql('postgres', wait => 0); |
| 45 |
|
| 46 |
# Wait for the connection to show up in pg_stat_activity, with the wait_event |
| 47 |
# of the injection point. |
| 48 |
my $pid; |
| 49 |
while (1) |
| 50 |
{ |
| 51 |
$pid = $psql->query( |
| 52 |
qq{SELECT pid FROM pg_stat_activity |
| 53 |
WHERE backend_type = 'client backend' |
| 54 |
AND state = 'starting' |
| 55 |
AND wait_event = 'init-pre-auth';}); |
| 56 |
last if $pid ne ""; |
| 57 |
|
| 58 |
usleep(100_000); |
| 59 |
} |
| 60 |
|
| 61 |
note "backend $pid is authenticating"; |
| 62 |
ok(1, 'authenticating connections are recorded in pg_stat_activity'); |
| 63 |
|
| 64 |
# Detach the waitpoint and wait for the connection to complete. |
| 65 |
$psql->query_safe("SELECT injection_points_wakeup('init-pre-auth');"); |
| 66 |
$conn->wait_connect(); |
| 67 |
|
| 68 |
# Make sure the pgstat entry is updated eventually. |
| 69 |
while (1) |
| 70 |
{ |
| 71 |
my $state = |
| 72 |
$psql->query("SELECT state FROM pg_stat_activity WHERE pid = $pid;"); |
| 73 |
last if $state eq "idle"; |
| 74 |
|
| 75 |
note "state for backend $pid is '$state'; waiting for 'idle'..."; |
| 76 |
usleep(100_000); |
| 77 |
} |
| 78 |
|
| 79 |
ok(1, 'authenticated connections reach idle state in pg_stat_activity'); |
| 80 |
|
| 81 |
$psql->query_safe("SELECT injection_points_detach('init-pre-auth');"); |
| 82 |
$psql->quit(); |
| 83 |
$conn->quit(); |
| 84 |
|
| 85 |
done_testing(); |