|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +DATA_DIR="$1" |
| 4 | +CONFIG_FILE="$DATA_DIR/postgresql.conf" |
| 5 | +MEMORY_CONFIG_FILE=auto_memory_config.conf |
| 6 | +MEM_STAT_FILE=/sys/fs/cgroup/memory/memory.stat |
| 7 | + |
| 8 | +TOTAL_MEMORY=$(grep hierarchical_memory_limit $MEM_STAT_FILE|cut -d ' ' -f 2) |
| 9 | + |
| 10 | +# Always clean old memory config file, because the amount of memory may change |
| 11 | +# between container starts. Old config will be preserved in case of data |
| 12 | +# directory is mounted as some kind of persistent volume. |
| 13 | +echo '' > "$DATA_DIR/$MEMORY_CONFIG_FILE" |
| 14 | + |
| 15 | +if ! [[ "$TOTAL_MEMORY" =~ ^[0-9]+$ ]] ; then |
| 16 | + echo "Failed to detect available memory size." |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | + |
| 21 | +# In case when there is no limit set in the container, TOTAL_MEMORY will |
| 22 | +# contain very large value (9223372036854771712 or something like) |
| 23 | +total_host_memory=$(free -b | awk 'NR==2{print$2}') |
| 24 | +if (( $total_host_memory < $TOTAL_MEMORY )); then |
| 25 | + echo "No memory limit set in the container. Skip auto configuration." |
| 26 | + exit 0 |
| 27 | +fi |
| 28 | + |
| 29 | +MEMORY_LIMIT_256M=$((256 * 1024 * 1024)) |
| 30 | +MEMORY_LIMIT_512M=$((512 * 1024 * 1024)) |
| 31 | +MEMORY_LIMIT_1G=$((1024 * 1024 * 1024)) |
| 32 | +MEMORY_LIMIT_2G=$((2 * 1024 * 1024 * 1024)) |
| 33 | + |
| 34 | +if (($TOTAL_MEMORY < $MEMORY_LIMIT_256M)); then |
| 35 | + max_connections=20 |
| 36 | +elif (($TOTAL_MEMORY < $MEMORY_LIMIT_512M)); then |
| 37 | + max_connections=40 |
| 38 | +elif (($TOTAL_MEMORY < $MEMORY_LIMIT_1G)); then |
| 39 | + max_connections=60 |
| 40 | +elif (($TOTAL_MEMORY < $MEMORY_LIMIT_2G)); then |
| 41 | + max_connections=150 |
| 42 | +else |
| 43 | + max_connections=300 |
| 44 | +fi |
| 45 | + |
| 46 | +shared_buffers=$(($TOTAL_MEMORY / 4)) |
| 47 | + |
| 48 | +# Assume available memory for caching (OS + database). It is recommended to set |
| 49 | +# it from 1/2 to 3/4 of available memory. |
| 50 | +effective_cache_size=$(($TOTAL_MEMORY * 3 / 4)) |
| 51 | + |
| 52 | +shared_buffers=$(($shared_buffers / (1024 * 1024)))MB |
| 53 | +effective_cache_size=$(($effective_cache_size / (1024 * 1024)))MB |
| 54 | + |
| 55 | + |
| 56 | +cat > "$DATA_DIR/$MEMORY_CONFIG_FILE" << EOF |
| 57 | +# Auto memory configuration. |
| 58 | +# TOTAL_MEMORY: $TOTAL_MEMORY |
| 59 | +max_connections = $max_connections |
| 60 | +shared_buffers = $shared_buffers |
| 61 | +effective_cache_size = $effective_cache_size |
| 62 | +EOF |
| 63 | + |
| 64 | +grep -Fq "$MEMORY_CONFIG_FILE" "$CONFIG_FILE" || echo "include = '$MEMORY_CONFIG_FILE'" >> "$CONFIG_FILE" |
0 commit comments