-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpgtune.sh
550 lines (486 loc) · 13.4 KB
/
pgtune.sh
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/env bash
function _version()
{
cat ./VERSION
echo ""
exit
}
function _usage()
{
echo -e "Build project folders\n"
echo -e "Usage:"
echo -e " bash $0 [OPTIONS...]\n"
echo -e "Options:"
echo -e " -o, --output=file\t\t\tStore data in file (If not set show in stdout)"
echo -e " -v, --version\t\t\t\tShow version information and exit"
echo -e " -h, --help\t\t\t\tShow help"
echo ""
echo -e " --db-version=version\t\tPostgres database version [REQUIRE]"
echo -e " --db-type=type\t\t\tPostgres database type (web|oltp|dw|desktop|mixed) [REQUIRE]"
echo -e " --os-type=type\t\t\tOperation system type (linux|windows) [REQUIRE]"
echo -e " --max-connection=connection\tNumber of connections"
echo -e " --memory=memory\t\t\tTotal memory (RAM) [REQUIRE]"
echo -e " --cpu=cpu\t\t\t\tNumber of CPUs"
echo -e " --hd-type=cpu\t\t\tData storage (ssd|san|hdd) [REQUIRE]"
echo ""
exit
}
if [[ $# -eq 0 ]]; then
_usage
fi
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case ${key} in
# Output file
#############
-o|--output)
declare -r I_OUTPUT="$2"
shift
shift
;;
-o=*|--output=*)
declare -r I_OUTPUT="${key#*=}"
shift
;;
# Output file
#############
-r|--replace)
declare -r I_REPLACE="$2"
shift
shift
;;
-r=*|--replace=*)
declare -r I_REPLACE="${key#*=}"
shift
;;
# Input database version
########################
--db-version)
declare -r I_DB_VERSION="$2"
shift
shift
;;
--db-version=*)
declare -r I_DB_VERSION="${key#*=}"
shift
;;
# Input database type
#####################
--db-type)
declare -r I_DB_TYPE="$2"
shift
shift
;;
--db-type=*)
declare -r I_DB_TYPE="${key#*=}"
shift
;;
# Input os type
###############
--os-type)
declare -r I_OS_TYPE="$2"
shift
shift
;;
--os-type=*)
declare -r I_OS_TYPE="${key#*=}"
shift
;;
# Input max connection
######################
--max-connection)
declare -r I_MAX_CONNECTION="$2"
shift
shift
;;
--max-connection=*)
declare -r I_MAX_CONNECTION="${key#*=}"
shift
;;
# Input total memory
####################
--memory)
declare -r I_MEMORY="$2"
shift
shift
;;
--memory=*)
declare -r I_MEMORY="${key#*=}"
shift
;;
# Input hd type
###############
--cpu)
declare -r I_CPU="$2"
shift
shift
;;
--cpu=*)
declare -r I_CPU="${key#*=}"
shift
;;
# Input hd type
###############
--hd-type)
declare -r I_HD_TYPE="$2"
shift
shift
;;
--hd-type=*)
declare -r I_HD_TYPE="${key#*=}"
shift
;;
-v|--version)
_version
shift
;;
-h|--help)
_usage
shift
;;
*)
_usage
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
if [[ -z ${I_DB_VERSION} ]]; then
echo "[ERR] Please add database version"
exit 1
fi
if [[ -z ${I_DB_TYPE} ]]; then
echo "[ERR] Please add database type (web|oltp|dw|desktop|mixed)"
exit 1
elif [[ ! ${I_DB_TYPE} =~ ^(web|oltp|dw|desktop|mixed)$ ]]; then
echo "[ERR] Database type not match with one of type (web|oltp|dw|desktop|mixed)"
exit 1
fi
if [[ -z ${I_OS_TYPE} ]]; then
echo "[ERR] Please add operation system type (linux|windows)"
exit 1
elif [[ ! ${I_OS_TYPE} =~ ^(linux|windows)$ ]]; then
echo "[ERR] Operation system type not match with one of type (linux|windows)"
exit 1
fi
if [[ -z ${I_MEMORY} ]]; then
echo "[ERR] Please add total memory"
exit 1
elif [[ ! ${I_MEMORY} =~ ^([0-9]+)(MB|GB)$ ]]; then
echo "[ERR] Memory type not match with one of type (MB|GB)"
exit 1
fi
if [[ -z ${I_HD_TYPE} ]]; then
echo "[ERR] Please add data storage type (ssd|san|hdd)"
exit 1
elif [[ ! ${I_HD_TYPE} =~ ^(ssd|san|hdd)$ ]]; then
echo "[ERR] Data storage type not match with one of type (ssd|san|hdd)"
exit 1
fi
if [[ -z ${I_CPU} ]]; then
cpu_num=0
else
cpu_num=${I_CPU}
fi
declare -r SIZE_KB=1024
declare -r SIZE_MB=1048576
declare -r SIZE_GB=1073741824
declare -r SIZE_TB=1099511627776
declare -r KB_UNIT_MAP_MB=1024
declare -r KB_UNIT_MAP_GB=1048576
declare -r GET_MEMORY_SIZE="SIZE_${I_MEMORY: -2}"
declare -r MEMORY_IN_BYTES=$(( ${I_MEMORY:0:-2} * !GET_MEMORY_SIZE ))
declare -r MEMORY_IN_KB=$(( MEMORY_IN_BYTES / SIZE_KB ))
output_list=("max_connection" "shared_buffers" "effective_cache_size" "maintenance_work_mem" "checkpoint_completion_target" "wal_buffers" "default_statistics_target" "random_page_cost" "effective_io_concurrency" "work_mem" "checkpoint_segments" "min_wal_size" "max_wal_size" "max_worker_processes" "max_parallel_workers_per_gather" "max_parallel_workers")
for out in "${output_list[@]}"; do
read "o_$out" <<< "0"
done
function formatValue()
{
local output;
if [[ $(awk 'BEGIN { print ('"${1}"' < 999) ? "y" : "n" }') = "y" ]]; then
output="${1}"
elif [[ $(( ${1} % KB_UNIT_MAP_GB )) -eq 0 ]]; then
output="$(( ${1} / KB_UNIT_MAP_GB ))GB"
elif [[ $(( ${1} % KB_UNIT_MAP_MB )) -eq 0 ]]; then
output="$(( ${1} / KB_UNIT_MAP_MB ))MB"
else
output="${1}kB"
fi
echo "${output}"
}
function getMaxConnection ()
{
local max_connection=0
if [[ -z ${I_MAX_CONNECTION} ]]; then
case ${I_DB_TYPE} in
web)
max_connection=200
;;
oltp)
max_connection=300
;;
dw)
max_connection=20
;;
desktop)
max_connection=10
;;
mixed)
max_connection=100
;;
esac
else
max_connection=${I_MAX_CONNECTION}
fi
o_max_connection=${max_connection}
}
function getSharedBuffers()
{
local shared_buffers=0
case ${I_DB_TYPE} in
web|oltp|dw|mixed)
shared_buffers=$(( MEMORY_IN_KB / 4 ))
;;
desktop)
shared_buffers=$(( MEMORY_IN_KB / 16 ))
;;
esac
local shared_buffers_win=$(( 512 * SIZE_MB / SIZE_KB ))
if [[ ${I_OS_TYPE} = "windows" ]] && [[ ${shared_buffers} -gt ${shared_buffers_win} ]]; then
shared_buffers=${shared_buffers_win}
fi
o_shared_buffers=${shared_buffers}
}
function getEffectiveCacheSize()
{
local effective_cache_size=0
case ${I_DB_TYPE} in
web|oltp|dw|mixed)
effective_cache_size=$(( MEMORY_IN_KB * 3 / 4 ))
;;
desktop)
effective_cache_size=$(( MEMORY_IN_KB / 4 ))
;;
esac
o_effective_cache_size=${effective_cache_size}
}
function getMaintenanceWorkMem()
{
local maintenance_work_mem=0
case ${I_DB_TYPE} in
web|oltp|desktop|mixed)
maintenance_work_mem=$(( MEMORY_IN_KB / 16 ))
;;
dw)
maintenance_work_mem=$(( MEMORY_IN_KB / 8 ))
;;
esac
local memory_limit=$(( 2 * SIZE_GB / SIZE_KB ))
if [[ ${maintenance_work_mem} -gt ${memory_limit} ]]; then
maintenance_work_mem=${memory_limit}
fi
o_maintenance_work_mem=${maintenance_work_mem}
}
function getCheckpointCompletionTarget()
{
case ${I_DB_TYPE} in
web)
o_checkpoint_completion_target=0.7
;;
oltp)
o_checkpoint_completion_target=0.9
;;
dw)
o_checkpoint_completion_target=0.9
;;
desktop)
o_checkpoint_completion_target=0.5
;;
mixed)
o_checkpoint_completion_target=0.9
;;
esac
}
function getWalBuffers()
{
local wal_buffers=$(( 3 * o_shared_buffers / 100 ))
local max_wal_buffer=$(( 16 * SIZE_MB / SIZE_KB ))
if [[ ${wal_buffers} -gt ${max_wal_buffer} ]]; then
wal_buffers=${max_wal_buffer}
fi
local wal_buffer_near=$(( 14 * SIZE_MB / SIZE_KB ))
if [[ ${wal_buffers} -gt ${wal_buffer_near} ]] && [[ ${wal_buffers} -lt ${max_wal_buffer} ]]; then
wal_buffers=${max_wal_buffer}
fi
if [[ ${wal_buffers} -lt 32 ]]; then
wal_buffers=32
fi
o_wal_buffers=${wal_buffers}
}
function getDefaultStatisticsTarget()
{
case ${I_DB_TYPE} in
web)
o_default_statistics_target=100
;;
oltp)
o_default_statistics_target=100
;;
dw)
o_default_statistics_target=500
;;
desktop)
o_default_statistics_target=100
;;
mixed)
o_default_statistics_target=100
;;
esac
}
function getRandomPageCost()
{
case ${I_HD_TYPE} in
ssd)
o_random_page_cost=1.1
;;
san)
o_random_page_cost=1.1
;;
hdd)
o_random_page_cost=4
;;
esac
}
function getEffectiveIoConcurrency()
{
if [[ ${I_OS_TYPE} = "windows" ]]; then
return
else
case ${I_HD_TYPE} in
ssd)
o_effective_io_concurrency=200
;;
san)
o_effective_io_concurrency=300
;;
hdd)
o_effective_io_concurrency=2
;;
esac
fi
}
function getParallelSettings()
{
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' < 9.5) ? "y" : "n" }') = "y" ]] || [[ ${cpu_num} -lt 2 ]]; then
return
fi
o_max_worker_processes=${cpu_num}
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' >= 9.6) ? "y" : "n" }') = "y" ]]; then
o_max_parallel_workers_per_gather=$(( cpu_num / 2 ))
fi
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' >= 10) ? "y" : "n" }') = "y" ]]; then
o_max_parallel_workers=${cpu_num}
fi
}
function getWorkMem()
{
getParallelSettings
local parallel_for_work_mem=1;
if [[ ${o_max_worker_processes} -gt 0 ]]; then
parallel_for_work_mem=$(( o_max_worker_processes / 2 ))
fi
local work_mem=$(( $(( MEMORY_IN_KB - o_shared_buffers )) / $(( o_max_connection * 3 )) / parallel_for_work_mem ))
case ${I_DB_TYPE} in
web)
o_work_mem=${work_mem}
;;
oltp)
o_work_mem=${work_mem}
;;
dw)
o_work_mem=$(( work_mem / 2 ))
;;
desktop)
o_work_mem=$(( work_mem / 6 ))
;;
mixed)
o_work_mem=$(( work_mem / 2 ))
;;
esac
if [[ ${o_work_mem} -lt 64 ]]; then
o_work_mem=64
fi
}
function getCheckpointSegments()
{
case ${I_DB_TYPE} in
web)
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' < 9.5) ? "y" : "n" }') = "y" ]]; then
o_checkpoint_segments=32
else
o_min_wal_size=$(( 1024 * SIZE_MB / SIZE_KB ))
o_max_wal_size=$(( 2048 * SIZE_MB / SIZE_KB ))
fi
;;
oltp)
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' < 9.5) ? "y" : "n" }') = "y" ]]; then
o_checkpoint_segments=64
else
o_min_wal_size=$(( 2048 * SIZE_MB / SIZE_KB ))
o_max_wal_size=$(( 4096 * SIZE_MB / SIZE_KB ))
fi
;;
dw)
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' < 9.5) ? "y" : "n" }') = "y" ]]; then
o_checkpoint_segments=128
else
o_min_wal_size=$(( 4096 * SIZE_MB / SIZE_KB ))
o_max_wal_size=$(( 8192 * SIZE_MB / SIZE_KB ))
fi
;;
desktop)
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' < 9.5) ? "y" : "n" }') = "y" ]]; then
o_checkpoint_segments=3
else
o_min_wal_size=$(( 100 * SIZE_MB / SIZE_KB ))
o_max_wal_size=$(( 1024 * SIZE_MB / SIZE_KB ))
fi
;;
mixed)
if [[ $(awk 'BEGIN { print ('"${I_DB_VERSION}"' < 9.5) ? "y" : "n" }') = "y" ]]; then
o_checkpoint_segments=32
else
o_min_wal_size=$(( 1024 * SIZE_MB / SIZE_KB ))
o_max_wal_size=$(( 2048 * SIZE_MB / SIZE_KB ))
fi
;;
esac
}
getMaxConnection
getSharedBuffers
getEffectiveCacheSize
getMaintenanceWorkMem
getCheckpointCompletionTarget
getWalBuffers
getDefaultStatisticsTarget
getRandomPageCost
getEffectiveIoConcurrency
getWorkMem
getCheckpointSegments
if [[ -n ${I_OUTPUT} ]] && [[ -z ${I_REPLACE} ]]; then
truncate -s 0 "${I_OUTPUT}"
fi
for out in "${output_list[@]}"; do
tmp="o_${out}"
if [[ $(awk 'BEGIN { print ('"${!tmp}"' > 0) ? "y" : "n" }') = "y" ]]; then
if [[ -n ${I_OUTPUT} ]] && [[ -z ${I_REPLACE} ]]; then
echo "${out} = $(formatValue "${!tmp}")" >> "${I_OUTPUT}"
# elif [[ -n ${I_OUTPUT} ]] && [[ -n ${I_REPLACE} ]]; then
# echo 1
else
echo "${out} = $(formatValue "${!tmp}")"
fi
fi
done