#!/bin/bash # Reproduces every claim in https://www.heatware.net/postgresql/reload-config-with-pg-ctl/ # # Creates a throwaway PostgreSQL cluster in a temp directory on port 55432, # runs each demonstration, prints the output, and removes the cluster. # It does not touch any existing PostgreSQL installation or data. # # Requires: initdb, pg_ctl, psql on PATH (any PostgreSQL 12+). # Usage: bash verify-postgresql-reload-config.sh set -u PORT=55432 PGDATA_TMP="$(mktemp -d -t hwverify)" trap 'pg_ctl -D "$PGDATA_TMP" stop -m immediate >/dev/null 2>&1; rm -rf "$PGDATA_TMP"' EXIT hr(){ printf '\n\033[1m%s\033[0m\n' "$*"; } q(){ psql -h 127.0.0.1 -p $PORT -U postgres -X "$@"; } command -v initdb >/dev/null || { echo "initdb not on PATH"; exit 1; } hr "Environment" initdb --version psql --version hr "Creating a throwaway cluster in $PGDATA_TMP" initdb -D "$PGDATA_TMP" -U postgres --auth=trust >/dev/null 2>&1 || { echo "initdb failed"; exit 1; } pg_ctl -D "$PGDATA_TMP" -o "-p $PORT" -l "$PGDATA_TMP/server.log" start >/dev/null 2>&1 sleep 2 q -At -c "SELECT version();" hr "CLAIM 1: 'pg_ctl reload' with no -D and no PGDATA fails" ( unset PGDATA; pg_ctl reload ) echo "exit code: $? <-- expect 1" hr "CLAIM 2: 'pg_ctl -D reload' succeeds" pg_ctl -D "$PGDATA_TMP" reload echo "exit code: $? <-- expect 0" hr "CLAIM 3: SELECT pg_reload_conf() returns t" q -At -c "SELECT pg_reload_conf();" hr "CLAIM 4: a sighup-context setting (work_mem) DOES apply on reload" echo -n "before: "; q -At -c "SHOW work_mem;" echo "work_mem = '8MB'" >> "$PGDATA_TMP/postgresql.conf" pg_ctl -D "$PGDATA_TMP" reload >/dev/null 2>&1; sleep 1 echo -n "after: "; q -At -c "SHOW work_mem;" echo " ^ expect 4MB -> 8MB" hr "CLAIM 5: a postmaster-context setting (shared_buffers) does NOT apply on reload" echo -n "before: "; q -At -c "SHOW shared_buffers;" echo "shared_buffers = '256MB'" >> "$PGDATA_TMP/postgresql.conf" pg_ctl -D "$PGDATA_TMP" reload >/dev/null 2>&1; sleep 1 echo -n "after: "; q -At -c "SHOW shared_buffers;" echo " ^ expect NO change, and no error anywhere" hr "CLAIM 6: pg_settings.pending_restart is how you find out" q -c "SELECT name, setting, pending_restart FROM pg_settings WHERE pending_restart;" hr "CLAIM 7: the reload is visible in the server log" grep -c "received SIGHUP" "$PGDATA_TMP/server.log" | sed 's/^/SIGHUP entries in log: /' hr "How many settings require a restart on this version?" q -At -c "SELECT count(*) FROM pg_settings WHERE context='postmaster';" hr "Done — cluster removed"