Skip to content

Commit fba1496

Browse files
committed
Added auto memory configuration according to container memory limit
1 parent 8e867c8 commit fba1496

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

9.5/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ENV PGDATA /var/lib/postgresql/data
5252
VOLUME /var/lib/postgresql/data
5353

5454
COPY docker-entrypoint.sh /
55+
COPY pg_auto_memory_config.sh /
5556

5657
ENTRYPOINT ["/docker-entrypoint.sh"]
5758

9.5/docker-entrypoint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ if [ "$1" = 'postgres' ]; then
9393
echo
9494
fi
9595

96+
if [ ! -z "$PG_AUTO_MEMORY_CONFIG" ]; then
97+
. /pg_auto_memory_config.sh "$PGDATA"
98+
fi
99+
100+
96101
exec gosu postgres "$@"
97102
fi
98103

9.5/pg_auto_memory_config.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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"

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
This is the fork of official image for postgres.
2+
The difference is an optional auto memory configuration feature:
3+
Define environment variable for the container *PG_AUTO_MEMORY_CONFIG=yes* and if
4+
there is a memory limit for the container, then will be created additional
5+
configuration file $PGDATA/auto_memory_config.conf.
6+
It defines *max_connections*, *shared_buffers*. *effective_cache_size*
7+
parameters according to memory limit in the container.
8+
9+
110
# About this Repo
211

312
This is the Git repo of the Docker [official image](https://docs.docker.com/docker-hub/official_repos/) for [postgres](https://registry.hub.docker.com/_/postgres/). See [the Docker Hub page](https://registry.hub.docker.com/_/postgres/) for the full readme on how to use this Docker image and for information regarding contributing and issues.

0 commit comments

Comments
 (0)