Skip to content

Commit 8f7ec66

Browse files
authored
Add experimental max_connections config (#500)
1 parent 52543e5 commit 8f7ec66

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,7 @@ options:
406406
Allowed values are: from 0 to 2000000000.
407407
type: int
408408
default: 150000000
409+
experimental_max_connections:
410+
type: int
411+
description: |
412+
[EXPERIMENTAL] Force set max_connections.

src/charm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,8 +1500,14 @@ def update_config(self, is_creating_backup: bool = False) -> bool:
15001500
logger.debug("Early exit update_config: Patroni not started yet")
15011501
return False
15021502

1503+
# Use config value if set, calculate otherwise
1504+
if self.config.experimental_max_connections:
1505+
max_connections = self.config.experimental_max_connections
1506+
else:
1507+
max_connections = max(4 * available_cpu_cores, 100)
1508+
15031509
self._patroni.bulk_update_parameters_controller_by_patroni({
1504-
"max_connections": max(4 * available_cpu_cores, 100),
1510+
"max_connections": max_connections,
15051511
"max_prepared_transactions": self.config.memory_max_prepared_transactions,
15061512
})
15071513

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
@@ -176,6 +176,7 @@ async def test_postgresql_parameters_change(ops_test: OpsTest) -> None:
176176
"memory_max_prepared_transactions": "100",
177177
"memory_shared_buffers": "128",
178178
"response_lc_monetary": "en_GB.utf8",
179+
"experimental_max_connections": "200",
179180
})
180181
await ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", idle_period=30)
181182
password = await get_password(ops_test)
@@ -188,7 +189,12 @@ async def test_postgresql_parameters_change(ops_test: OpsTest) -> None:
188189
with psycopg2.connect(
189190
f"dbname='postgres' user='operator' host='{host}' password='{password}' connect_timeout=1"
190191
) as connection, connection.cursor() as cursor:
191-
settings_names = ["max_prepared_transactions", "shared_buffers", "lc_monetary"]
192+
settings_names = [
193+
"max_prepared_transactions",
194+
"shared_buffers",
195+
"lc_monetary",
196+
"max_connections",
197+
]
192198
cursor.execute(
193199
sql.SQL("SELECT name,setting FROM pg_settings WHERE name IN ({});").format(
194200
sql.SQL(", ").join(sql.Placeholder() * len(settings_names))
@@ -202,6 +208,7 @@ async def test_postgresql_parameters_change(ops_test: OpsTest) -> None:
202208
assert settings["max_prepared_transactions"] == "100"
203209
assert settings["shared_buffers"] == "128"
204210
assert settings["lc_monetary"] == "en_GB.utf8"
211+
assert settings["max_connections"] == "200"
205212
finally:
206213
connection.close()
207214

0 commit comments

Comments
 (0)