Skip to content

Add standby_clean_stop.sh: stop a crashed standby in standby mode - #75

Merged
souravbiswassanto merged 4 commits into
masterfrom
standby-clean-stop
Nov 9, 2026
Merged

souravbiswassanto merged 4 commits into
masterfrom
standby-clean-stop

Conversation

@souravbiswassanto

@souravbiswassanto souravbiswassanto commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Problem

pg-coordinator used to crash-recover a hard-killed standby ("in archive recovery") in single-user mode:

  • That mode removes standby.signal and recovers the node as a primary on the timeline it shares with the leader.
  • It writes its own WAL there, and the leader's commits in the segment tail can be silently skipped.
  • The fix is in kubedb/pg-coordinator#293.

Change

Add scripts/standby_clean_stop.sh. The init container copies it to /scripts, next to the other scripts the coordinator runs. It:

  • starts the server in standby mode with no upstream and hot_standby=off;
  • waits for "consistent recovery state reached";
  • stops it with -m fast.

The control file then says "shut down in recovery", and no WAL is written.

  • POSIX sh: busybox ash (alpine), dash (debian), bash (CentOS Stream 9).
  • For an arbitrary uid, looks for libnss_wrapper.so under /usr/lib and /usr/lib64.
  • Arguments <pgdata> <pg-major> <timeout> default to $PGDATA, PG_VERSION and 300 s.

pg-coordinator#293 runs this copy when it exists, and its embedded copy otherwise.

Testing

Tested on alpine 17.9, bookworm 17.9 and Postgres Enterprise 17.11 on CentOS Stream 9:

  • In each image: a primary and a streaming standby with archive_mode=always. The standby is hard-killed while the leader writes and changes a role password, then the script runs. Each ended "shut down in recovery" on the same timeline, and after a restart it was byte-identical to the primary, pg_authid included.

  • On KubeDB, with pg-coordinator#293: the crashed-standby step runs this script from /scripts on all three distros. The standby rejoins and every pod has the same data.

  • Stops only the server it started (second commit):

    • pg_ctl stop -w waits for the pid file to disappear, re-reading it every 100 ms. If another server starts the moment ours exits (for example the container start loop), it waits on that server for the full timeout and then immediate-stops it. If another server is already running, the old script fast-stopped it and reported success.
    • Both were reproduced deterministically on all three images.
    • The script now tags its server with cluster_name=pgc-standby-clean-stop, identifies it by PID and command line, and stops that PID only: SIGINT, with SIGQUIT as a last resort. It waits on that process, not the pid file. Any other server is left alone and reported as OTHER_RUNNING=1, and the coordinator retries.
    • Results with the PR images, on 5-replica KubeDB DBs on alpine, bookworm and stream9:
      • 18 rounds of a standby postmaster hard-kill plus the step, while the pod's own start loop raced it.
      • 6 clean stops and 12 races left alone, each run at most 2 s.
      • No immediate stops, and every pod byte-identical afterwards.

souravbiswassanto and others added 4 commits November 5, 2026 12:17
pg-coordinator runs /scripts/standby_clean_stop.sh on a standby that was not
shut down cleanly ("in archive recovery"), instead of single-user crash
recovery, which recovers the standby as a primary on the leader's timeline and
can silently skip the leader's WAL. The script starts the server in standby mode
with no upstream and hot_standby=off, waits for a consistent state, and stops it
with -m fast, so the control file says "shut down in recovery" and no WAL is
written.

It is POSIX sh (busybox ash, dash and bash), finds nss_wrapper under /usr/lib
and /usr/lib64 for an arbitrary uid, and defaults its arguments from $PGDATA and
PG_VERSION.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: souravbiswassanto <saurov@appscode.com>
pg_ctl stop -w waits for the pid file to go away, re-reading it every 100 ms. When
the container's start loop started a standby the moment our server exited, pg_ctl
waited on that server for the full timeout and then immediate-stopped it. When
another server was already running, pg_ctl start failed and the script fast-stopped
that server and reported success. Both were reproduced on alpine, bookworm and
CentOS Stream 9.

The server is now tagged with cluster_name=pgc-standby-clean-stop and identified by
its PID and command line. It is stopped with SIGINT (fast) to that PID, the wait is
for that process, and SIGQUIT (immediate) is a last resort for that PID only. A
server this step did not start is never signalled: it is reported as
OTHER_RUNNING=1 and the caller retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: souravbiswassanto <saurov@appscode.com>
Signed-off-by: souravbiswassanto <saurov@appscode.com>
…'s copy)

Signed-off-by: souravbiswassanto <saurov@appscode.com>
@souravbiswassanto
souravbiswassanto merged commit 3855f0a into master Nov 9, 2026
4 checks passed
@souravbiswassanto
souravbiswassanto deleted the standby-clean-stop branch November 9, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant