#!/bin/bash # Postgres memory / performance tuning, scaled to the container's DB_MEMORY budget so # the same init serves every host (beta 8g; prod 16g on the 48 GB box) with no # hardcoding. Runs once on a fresh data volume from /docker-entrypoint-initdb.d, after # 10-timescaledb.sql enables timescaledb. Settings are written via ALTER SYSTEM # (persisted to postgresql.auto.conf, which the timescaledb image's own # timescaledb-tune postgresql.conf defers to); the container's post-init restart brings # restart-only settings (shared_buffers, …) into effect. NB: never ALTER SYSTEM SET # shared_preload_libraries here — that would land in auto.conf and override the image's # `timescaledb` preload. Init scripts do NOT re-run on an existing volume — to # re-tune later, set DB_MEMORY and run this by hand, then restart: # docker compose exec -e DB_MEMORY=16g db bash /docker-entrypoint-initdb.d/20-tuning.sh # docker compose restart db set -euo pipefail # Parse DB_MEMORY ("16g" / "8192m" / plain MB) into whole MB; default + floor at 8 GB. budget="${DB_MEMORY:-8g}" num="${budget//[!0-9]/}" num="${num:-8}" unit="$(printf '%s' "$budget" | tr -dc '[:alpha:]' | tr '[:upper:]' '[:lower:]')" case "$unit" in g | gb) mb=$((num * 1024)) ;; m | mb | "") mb="$num" ;; *) mb=8192 ;; esac if [ "$mb" -lt 1024 ]; then mb=8192; fi # Derive settings from the budget. The ratios reproduce the historical 8 GB tuning # (shared_buffers 2 GB, effective_cache_size 6 GB, work_mem 64 MB, # maintenance_work_mem 512 MB) and scale linearly on a bigger box. shared_buffers=$((mb / 4)) # 25% — the shared page cache effective_cache=$((mb * 3 / 4)) # 75% — planner's view of total cache (PG + OS) work_mem=$((mb / 128)) # ~64 MB at 8 GB (per-operation; kept modest) if [ "$work_mem" -lt 16 ]; then work_mem=16; fi maint_mem=$((mb / 16)) # 512 MB at 8 GB — index builds / VACUUM echo "[tuning] DB_MEMORY=${budget} -> ${mb}MB: shared_buffers=${shared_buffers}MB" \ "effective_cache_size=${effective_cache}MB work_mem=${work_mem}MB" \ "maintenance_work_mem=${maint_mem}MB" psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <