Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6a1d0d4

Browse files
committed
Add test for connection limits
Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://www.postgresql.org/message-id/a102f15f-eac4-4ff2-af02-f9ff209ec66f@iki.fi
1 parent 5b7da5c commit 6a1d0d4

File tree

6 files changed

+143
-1
lines changed

6 files changed

+143
-1
lines changed

src/test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ subdir = src/test
1212
top_builddir = ../..
1313
include $(top_builddir)/src/Makefile.global
1414

15-
SUBDIRS = perl regress isolation modules authentication recovery subscription
15+
SUBDIRS = perl postmaster regress isolation modules authentication recovery subscription
1616

1717
ifeq ($(with_icu),yes)
1818
SUBDIRS += icu

src/test/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ subdir('regress')
44
subdir('isolation')
55

66
subdir('authentication')
7+
subdir('postmaster')
78
subdir('recovery')
89
subdir('subscription')
910
subdir('modules')

src/test/postmaster/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#-------------------------------------------------------------------------
2+
#
3+
# Makefile for src/test/postmaster
4+
#
5+
# Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
6+
# Portions Copyright (c) 1994, Regents of the University of California
7+
#
8+
# src/test/postmaster/Makefile
9+
#
10+
#-------------------------------------------------------------------------
11+
12+
subdir = src/test/postmaster
13+
top_builddir = ../../..
14+
include $(top_builddir)/src/Makefile.global
15+
16+
check:
17+
$(prove_check)
18+
19+
installcheck:
20+
$(prove_installcheck)
21+
22+
clean distclean:
23+
rm -rf tmp_check

src/test/postmaster/README

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
src/test/postmaster/README
2+
3+
Regression tests for postmaster
4+
===============================
5+
6+
This directory contains a test suite for postmaster's handling of
7+
connections, connection limits, and startup/shutdown sequence.
8+
9+
10+
Running the tests
11+
=================
12+
13+
NOTE: You must have given the --enable-tap-tests argument to configure.
14+
15+
Run
16+
make check
17+
or
18+
make installcheck
19+
You can use "make installcheck" if you previously did "make install".
20+
In that case, the code in the installation tree is tested. With
21+
"make check", a temporary installation tree is built from the current
22+
sources and then tested.
23+
24+
Either way, this test initializes, starts, and stops a test Postgres
25+
cluster.
26+
27+
See src/test/perl/README for more info about running these tests.

src/test/postmaster/meson.build

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2022-2024, PostgreSQL Global Development Group
2+
3+
tests += {
4+
'name': 'postmaster',
5+
'sd': meson.current_source_dir(),
6+
'bd': meson.current_build_dir(),
7+
'tap': {
8+
'tests': [
9+
't/001_connection_limits.pl',
10+
],
11+
},
12+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
# Copyright (c) 2021-2024, PostgreSQL Global Development Group
3+
4+
# Test connection limits, i.e. max_connections, reserved_connections
5+
# and superuser_reserved_connections.
6+
7+
use strict;
8+
use warnings FATAL => 'all';
9+
use PostgreSQL::Test::Cluster;
10+
use PostgreSQL::Test::Utils;
11+
use Test::More;
12+
13+
# Initialize the server with specific low connection limits
14+
my $node = PostgreSQL::Test::Cluster->new('primary');
15+
$node->init;
16+
$node->append_conf('postgresql.conf', "max_connections = 6");
17+
$node->append_conf('postgresql.conf', "reserved_connections = 2");
18+
$node->append_conf('postgresql.conf', "superuser_reserved_connections = 1");
19+
$node->append_conf('postgresql.conf', "log_connections = on");
20+
$node->start;
21+
22+
$node->safe_psql(
23+
'postgres', qq{
24+
CREATE USER regress_regular LOGIN;
25+
CREATE USER regress_reserved LOGIN;
26+
GRANT pg_use_reserved_connections TO regress_reserved;
27+
CREATE USER regress_superuser LOGIN SUPERUSER;
28+
});
29+
30+
# With the limits we set in postgresql.conf, we can establish:
31+
# - 3 connections for any user with no special privileges
32+
# - 2 more connections for users belonging to "pg_use_reserved_connections"
33+
# - 1 more connection for superuser
34+
35+
sub background_psql_as_user
36+
{
37+
my $user = shift;
38+
39+
return $node->background_psql(
40+
'postgres',
41+
on_error_die => 1,
42+
extra_params => [ '-U', $user ]);
43+
}
44+
45+
my @sessions = ();
46+
47+
push(@sessions, background_psql_as_user('regress_regular'));
48+
push(@sessions, background_psql_as_user('regress_regular'));
49+
push(@sessions, background_psql_as_user('regress_regular'));
50+
$node->connect_fails(
51+
"dbname=postgres user=regress_regular",
52+
"reserved_connections limit",
53+
expected_stderr =>
54+
qr/FATAL: remaining connection slots are reserved for roles with privileges of the "pg_use_reserved_connections" role/
55+
);
56+
57+
push(@sessions, background_psql_as_user('regress_reserved'));
58+
push(@sessions, background_psql_as_user('regress_reserved'));
59+
$node->connect_fails(
60+
"dbname=postgres user=regress_regular",
61+
"reserved_connections limit",
62+
expected_stderr =>
63+
qr/FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute/
64+
);
65+
66+
push(@sessions, background_psql_as_user('regress_superuser'));
67+
$node->connect_fails(
68+
"dbname=postgres user=regress_superuser",
69+
"superuser_reserved_connections limit",
70+
expected_stderr => qr/FATAL: sorry, too many clients already/);
71+
72+
# TODO: test that query cancellation is still possible
73+
74+
foreach my $session (@sessions)
75+
{
76+
$session->quit;
77+
}
78+
79+
done_testing();

0 commit comments

Comments
 (0)