This repository was archived by the owner on Jun 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautotune.py
More file actions
335 lines (275 loc) · 10.9 KB
/
Copy pathautotune.py
File metadata and controls
335 lines (275 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# This script is based on pgtune, and generates a configuration based on
# Aptible's APTIBLE_CONTAINER_SIZE environment variable (size in MB).
# ... with some fixes from yours truly (mpasternak)
import os
import sys
ONE_MB_IN_KB = 1024
ONE_GB_IN_KB = 1024 * ONE_MB_IN_KB
how_much_ram_for_postgres = float(os.environ.get("POSTGRESQL_RAM_PERCENT", "0.95"))
force_ram_for_postgres = os.environ.get("POSTGRESQL_RAM_THIS_MUCH_GB", None)
default_ram_for_postgres = int(os.environ.get("POSTGRESQL_DEFAULT_RAM", 4096))
# Tryb "szybki ale niebezpieczny" - wyłącza synchronizację zapisu na dysk
# Używać TYLKO do testów/developmentu, NIGDY w produkcji!
unsafe_but_fast = os.environ.get("POSTGRESQL_UNSAFE_BUT_FAST", "").lower() in (
"1",
"true",
"yes",
)
# Ręczne nadpisanie limitów tabeli locków — gdy równoległe workery (xdist, celery)
# dotykają wielu tabel/indeksów w jednej transakcji, domyślne 64 bywa za małe
# ("brak pamięci współdzielonej / max_locks_per_transaction"). Puste = PG default.
max_locks_per_transaction = os.environ.get("POSTGRESQL_MAX_LOCKS_PER_TRANSACTION")
max_pred_locks_per_transaction = os.environ.get(
"POSTGRESQL_MAX_PRED_LOCKS_PER_TRANSACTION"
)
def cgroup_limit_kb():
# /proc/meminfo zawsze pokazuje RAM hosta, nie limit kontenera — limit
# siedzi w cgroup. Zwracamy go w kB, albo None gdy brak skończonego limitu.
try:
with open("/sys/fs/cgroup/memory.max") as f:
raw = f.read().strip()
if raw != "max":
return int(raw) // 1024
except FileNotFoundError:
pass
try:
with open("/sys/fs/cgroup/memory/memory.limit_in_bytes") as f:
v = int(f.read().strip())
# cgroup v1 zwraca ogromną wartość-wartownik gdy limit nie jest ustawiony
if v < (1 << 62):
return v // 1024
except FileNotFoundError:
pass
return None
def host_ram_kb():
import re
if not os.path.exists("/proc/meminfo"):
return None
with open("/proc/meminfo") as f:
meminfo = f.read()
matched = re.search(r"^MemTotal:\s+(\d+)", meminfo)
if not matched:
return None
return int(matched.groups()[0])
def total_ram_size_mb():
if force_ram_for_postgres:
print(
f"# autotune.py: RAM size for Postgres is {force_ram_for_postgres} MB, because of "
f"env var POSTGRESQL_RAM_THIS_MUCH_DB setting. Change the env var to tweak it or"
f"remove it to use automatic RAM size detection."
)
return int(force_ram_for_postgres)
host_kb = host_ram_kb()
cgroup_kb = cgroup_limit_kb()
if cgroup_kb is not None and (host_kb is None or cgroup_kb < host_kb):
print(
f"# autotune.py: detected cgroup memory limit {cgroup_kb} kB "
f"(host RAM: {host_kb} kB); will use {int(how_much_ram_for_postgres * 100)} % of the cgroup limit"
)
return how_much_ram_for_postgres * cgroup_kb / 1024
if host_kb is not None:
print(
f"# autotune.py: detected {host_kb} kB RAM; will use {int(how_much_ram_for_postgres * 100)} % of it"
)
return how_much_ram_for_postgres * host_kb / 1024
print(
f"# autotune.py: unable to detect RAM size, returning default {default_ram_for_postgres} MB; "
f"change environment variable POSTGRESQL_DEFAULT_RAM if you need to change this"
)
return default_ram_for_postgres
def get_nproc():
import multiprocessing
return multiprocessing.cpu_count()
def normalize_size(v):
if v % ONE_GB_IN_KB == 0:
return f"{int(v / ONE_GB_IN_KB)}GB"
if v % ONE_MB_IN_KB == 0:
return f"{int(v / ONE_MB_IN_KB)}MB"
return f"{int(v)}kB"
def to_config_value(v, n=None):
if n == "max_connections":
return int(v)
if isinstance(v, str):
return v
if isinstance(v, int):
return normalize_size(v)
if isinstance(v, float):
return to_config_value(int(v))
raise TypeError(f"Unexpected type: {type(v)}")
def generate_config(ram_kb):
config = {}
nproc = get_nproc()
max_parallel = 1
if nproc >= 4:
max_parallel = 2
if nproc in [5, 6]:
max_parallel = 3
if nproc >= 7:
max_parallel = 4
# Directly form pgtune
config["shared_buffers"] = ram_kb / 4
config["effective_cache_size"] = ram_kb * 3 / 4
config["maintenance_work_mem"] = min(ram_kb / 16, 2 * ONE_GB_IN_KB)
# 100 połaczeń na 1 GB RAM, maksymalnie 250
conns = min(100 * ram_kb / ONE_GB_IN_KB, 250)
config["max_connections"] = conns
config["work_mem"] = (ram_kb * 3 / 4) / (conns * 3) / max_parallel
config["min_wal_size"] = ONE_GB_IN_KB
config["max_wal_size"] = 4 * ONE_GB_IN_KB
config["wal_buffers"] = min(ram_kb * 3 / 4 / 100, 16 * ONE_MB_IN_KB)
config["checkpoint_completion_target"] = "0.7"
config["default_statistics_target"] = "100"
config["max_worker_processes"] = str(nproc)
config["max_parallel_workers_per_gather"] = str(max_parallel)
config["max_parallel_workers"] = str(nproc)
config["max_parallel_maintenance_workers"] = str(max_parallel)
if max_locks_per_transaction:
config["max_locks_per_transaction"] = str(int(max_locks_per_transaction))
if max_pred_locks_per_transaction:
config["max_pred_locks_per_transaction"] = str(
int(max_pred_locks_per_transaction)
)
# Tryb "szybki ale niebezpieczny" - maksymalna wydajność kosztem bezpieczeństwa
if unsafe_but_fast:
# Wyłącz synchronizację zapisu na dysk
config["fsync"] = "off"
config["full_page_writes"] = "off"
config["synchronous_commit"] = "off"
# Minimalny poziom WAL (wymaga max_wal_senders=0)
config["wal_level"] = "minimal"
config["max_wal_senders"] = "0"
config["archive_mode"] = "off"
# Opóźnienia zapisu WAL
config["wal_writer_delay"] = "10000ms"
config["commit_delay"] = "100000"
# Optymalizacje I/O (zakładamy SSD)
config["random_page_cost"] = "1.1"
config["effective_io_concurrency"] = "200"
return {x: to_config_value(y, x) for x, y in sorted(config.items())}
def main():
ram_mb = total_ram_size_mb()
config = generate_config(ram_mb * ONE_MB_IN_KB)
print("# Automatically added by autotune.py")
if unsafe_but_fast:
print("#")
print("# *** UWAGA! TRYB POSTGRESQL_UNSAFE_BUT_FAST JEST WŁĄCZONY! ***")
print("# *** fsync, full_page_writes, synchronous_commit WYŁĄCZONE ***")
print("# *** wal_level=minimal, max_wal_senders=0, archive_mode=off ***")
print("# *** DANE MOGĄ ZOSTAĆ UTRACONE! NIE UŻYWAJ W PRODUKCJI! ***")
print("#")
for k, v in sorted(config.items()):
print(f"{k} = {v}")
def test():
# Wartości zależne od RAM (deterministyczne)
# Uwaga: work_mem zależy też od max_connections i max_parallel, więc go nie testujemy
# Wartości max_worker_processes, max_parallel_* zależą od CPU - testujemy tylko obecność
test_cases = [
[
0.5 * ONE_GB_IN_KB,
{
"shared_buffers": "128MB",
"effective_cache_size": "384MB",
"maintenance_work_mem": "32MB",
"min_wal_size": "1GB",
"max_wal_size": "4GB",
"checkpoint_completion_target": "0.7",
"wal_buffers": "3932kB",
"default_statistics_target": "100",
"max_connections": 50,
},
],
[
ONE_GB_IN_KB,
{
"shared_buffers": "256MB",
"effective_cache_size": "768MB",
"maintenance_work_mem": "64MB",
"min_wal_size": "1GB",
"max_wal_size": "4GB",
"checkpoint_completion_target": "0.7",
"wal_buffers": "7864kB",
"default_statistics_target": "100",
"max_connections": 100,
},
],
[
2 * ONE_GB_IN_KB,
{
"shared_buffers": "512MB",
"effective_cache_size": "1536MB",
"maintenance_work_mem": "128MB",
"min_wal_size": "1GB",
"max_wal_size": "4GB",
"checkpoint_completion_target": "0.7",
"wal_buffers": "15728kB",
"default_statistics_target": "100",
"max_connections": 200,
},
],
[
4 * ONE_GB_IN_KB,
{
"shared_buffers": "1GB",
"effective_cache_size": "3GB",
"maintenance_work_mem": "256MB",
"min_wal_size": "1GB",
"max_wal_size": "4GB",
"checkpoint_completion_target": "0.7",
"wal_buffers": "16MB",
"default_statistics_target": "100",
"max_connections": 250,
},
],
]
# Klucze które muszą być obecne (zależne od CPU, nie sprawdzamy wartości)
cpu_dependent_keys = {
"max_worker_processes",
"max_parallel_workers_per_gather",
"max_parallel_workers",
"max_parallel_maintenance_workers",
"work_mem",
}
for size, expected_config in test_cases:
prefix = f"Postgres at {size / ONE_GB_IN_KB}GB"
real_config = generate_config(size)
# Sprawdź wartości deterministyczne
for key, expected in expected_config.items():
assert key in real_config, f"{prefix}: missing key {key}"
real = real_config[key]
m = f"{prefix}: {key} differs:\n Got: {real}\n Expected: {expected}"
assert real == expected, m
# Sprawdź obecność kluczy zależnych od CPU
for key in cpu_dependent_keys:
assert key in real_config, f"{prefix}: missing CPU-dependent key {key}"
# Test trybu unsafe_but_fast
if unsafe_but_fast:
config = generate_config(ONE_GB_IN_KB)
# Klucze które powinny być "off"
off_keys = {
"fsync",
"full_page_writes",
"synchronous_commit",
"archive_mode",
}
for key in off_keys:
assert key in config, f"unsafe mode: missing key {key}"
assert config[key] == "off", f"unsafe mode: {key} should be 'off'"
# Klucze WAL
assert config.get("wal_level") == "minimal"
assert config.get("max_wal_senders") == "0"
# Klucze I/O
assert "wal_writer_delay" in config
assert "commit_delay" in config
assert config.get("random_page_cost") == "1.1"
assert config.get("effective_io_concurrency") == "200"
sys.stderr.write("OK\n")
def usage(program):
sys.stderr.write(f"Usage: {program} [--test]\n")
if __name__ == "__main__":
if len(sys.argv) == 1:
main()
elif len(sys.argv) == 2 and sys.argv[1] == "--test":
test()
else:
usage(sys.argv[0])