Skip to content

Commit 4feeaee

Browse files
authored
[MISC] Add experimental max_connections config (#472)
* Add experimental max_connections config * Add max_connections to restart param test * Typo
1 parent ce38466 commit 4feeaee

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,7 @@ options:
402402
Allowed values are: from 0 to 2000000000.
403403
type: int
404404
default: 150000000
405+
experimental_max_connections:
406+
type: int
407+
description: |
408+
[EXPERIMENTAL] Force set max_connections.

src/charm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ def _restart(self, event: RunWithLock) -> None:
14681468
return
14691469

14701470
try:
1471-
for attempt in Retrying(wait=wait_fixed(3), stop_after_delay=stop_after_delay(300)):
1471+
for attempt in Retrying(wait=wait_fixed(3), stop=stop_after_delay(300)):
14721472
with attempt:
14731473
if not self._can_connect_to_postgresql:
14741474
assert False
@@ -1547,8 +1547,14 @@ def update_config(self, is_creating_backup: bool = False) -> bool:
15471547
logger.warning("Early exit update_config: Cannot connect to Postgresql")
15481548
return False
15491549

1550+
# Use config value if set, calculate otherwise
1551+
if self.config.experimental_max_connections:
1552+
max_connections = self.config.experimental_max_connections
1553+
else:
1554+
max_connections = max(4 * os.cpu_count(), 100)
1555+
15501556
self._patroni.bulk_update_parameters_controller_by_patroni({
1551-
"max_connections": max(4 * os.cpu_count(), 100),
1557+
"max_connections": max_connections,
15521558
"max_prepared_transactions": self.config.memory_max_prepared_transactions,
15531559
})
15541560

src/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class CharmConfig(BaseConfigModel):
9898
vacuum_autovacuum_vacuum_cost_delay: Optional[float]
9999
vacuum_autovacuum_vacuum_scale_factor: Optional[float]
100100
vacuum_vacuum_freeze_table_age: Optional[int]
101+
experimental_max_connections: Optional[int]
101102

102103
@classmethod
103104
def keys(cls) -> list[str]:

tests/integration/test_charm.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ async def test_postgresql_parameters_change(ops_test: OpsTest) -> None:
173173
"memory_max_prepared_transactions": "100",
174174
"memory_shared_buffers": "128",
175175
"response_lc_monetary": "en_GB.utf8",
176+
"experimental_max_connections": "200",
176177
})
177178
await ops_test.model.wait_for_idle(apps=[DATABASE_APP_NAME], status="active", idle_period=30)
178179
any_unit_name = ops_test.model.applications[DATABASE_APP_NAME].units[0].name
@@ -186,7 +187,12 @@ async def test_postgresql_parameters_change(ops_test: OpsTest) -> None:
186187
with psycopg2.connect(
187188
f"dbname='postgres' user='operator' host='{host}' password='{password}' connect_timeout=1"
188189
) as connection, connection.cursor() as cursor:
189-
settings_names = ["max_prepared_transactions", "shared_buffers", "lc_monetary"]
190+
settings_names = [
191+
"max_prepared_transactions",
192+
"shared_buffers",
193+
"lc_monetary",
194+
"max_connections",
195+
]
190196
cursor.execute(
191197
sql.SQL("SELECT name,setting FROM pg_settings WHERE name IN ({});").format(
192198
sql.SQL(", ").join(sql.Placeholder() * len(settings_names))
@@ -200,6 +206,7 @@ async def test_postgresql_parameters_change(ops_test: OpsTest) -> None:
200206
assert settings["max_prepared_transactions"] == "100"
201207
assert settings["shared_buffers"] == "128"
202208
assert settings["lc_monetary"] == "en_GB.utf8"
209+
assert settings["max_connections"] == "200"
203210
finally:
204211
connection.close()
205212

tests/unit/test_charm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ def test_restart(harness):
11351135
with (
11361136
patch("charm.Patroni.restart_postgresql") as _restart_postgresql,
11371137
patch("charm.Patroni.are_all_members_ready") as _are_all_members_ready,
1138+
patch("charm.PostgresqlOperatorCharm._can_connect_to_postgresql", return_value=True),
11381139
):
11391140
_are_all_members_ready.side_effect = [False, True, True]
11401141

0 commit comments

Comments
 (0)