#!/usr/bin/env bash # 012-tune-cpu - Cap query parallelism for the shared-core 2 vCPU e2-micro # (C-tier of the 2026-08 tuning review). # # max_parallel_workers_per_gather = 1 one query must not take every core; # other sessions still get CPU # max_parallel_workers = 2 there are only 2 cores to begin with # jit = off JIT compile cost outweighs gains for # short queries / low TPS / shared CPU # # All three are reloadable GUCs - no restart, no downtime. # up: write drop-in; reload # down: remove drop-in; reload set -euo pipefail PGBIN=/etc/postgresql/18/main CONF=$PGBIN/conf.d/012-tune-cpu.conf CLUSTER=$(pg_lsclusters --no-header | awk 'NR==1{print $1"/"$2}') # desired state as "guc value" pairs; SHOW renders exactly these strings WANT=( 'max_parallel_workers_per_gather 1' 'max_parallel_workers 2' 'jit off' ) pg() { runuser -u postgres -- psql -d postgres -v ON_ERROR_STOP=1 -tAc "$1"; } write_conf() { install -d -m 755 "$PGBIN/conf.d" cat > "$CONF" <<'EOF' # managed by sysmig 012-tune-cpu - parallelism caps for shared-core 2 vCPU max_parallel_workers_per_gather = 1 max_parallel_workers = 2 jit = off EOF chown root:postgres "$CONF" chmod 644 "$CONF" } case "${1:-}" in up) write_conf pg_ctlcluster "$CLUSTER" reload echo " cluster reloaded" for kv in "${WANT[@]}"; do read -r guc want <<<"$kv" got=$(pg "SHOW $guc") [[ $got == "$want" ]] || { echo "FATAL: $guc is '$got', expected '$want'" >&2; exit 1; } done echo " ${#WANT[@]} cpu/parallel settings active" ;; status) if [[ $EUID -eq 0 ]]; then q() { runuser -u postgres -- psql -d postgres -v ON_ERROR_STOP=1 -tAc "$1"; } elif psql -d postgres -tAc 'SELECT 1' >/dev/null 2>&1; then q() { psql -d postgres -v ON_ERROR_STOP=1 -tAc "$1"; } else echo "? needs root or peer DB access to verify" exit 2 fi [[ -f $CONF ]] || { echo "DRIFT drop-in $CONF missing"; exit 1; } bad=() for kv in "${WANT[@]}"; do read -r guc want <<<"$kv" got=$(q "SHOW $guc" 2>/dev/null) [[ $got == "$want" ]] || bad+=("$guc=$got(want $want)") done if ((${#bad[@]})); then echo "DRIFT ${bad[*]}"; exit 1; fi echo "OK parallel capped (per_gather=1, workers=2), jit off" ;; down) rm -f "$CONF" pg_ctlcluster "$CLUSTER" reload echo " drop-in removed, cluster reloaded (defaults restored)" ;; esac