TT Lab
Blog Get started
Learn Learning paths Courses

PostgreSQL Replication and Promotion

Stand Up Replication and Promote It

Continue in TT Lab

Goal

Build a primary and a standby yourself inside a single Pod, attach replication, and go as far as promoting the standby. After seeing the timeline fork with your own eyes, at the end you fix an incident in which three standbys someone else set up fail to attach, each for a different reason.

Why it matters

"We built HA" usually means we have never once done a failover. The configuration is right, but when you actually promote, the standby has not caught up, or the promotion works but the application is still looking at the old address, or you try to reattach the old primary and ruin the data.

This lab walks through that whole loop ahead of time.

Environment

This Pod runs as the postgres account (lab Pods have no capabilities, so you cannot switch users with su). So you can type commands directly, without su or sudo.

psql                      주 서버(5432)에 바로 붙습니다
psql -h 127.0.0.1 -p 5433 대기 서버

(The first line connects straight to the primary on 5432; the second is the standby.)

Be sure to add -h 127.0.0.1 for the standby. This environment moves the Unix socket directory under the home directory (the container runtime overlays /run with a tmpfs, so the default path disappears).

Steps

  1. Check the primary's replication settings and add to them (wal_log_hints, wal_keep_size)
  2. Replication role rep and slot s1
  3. Base backup → /var/lib/postgresql/standby
  4. Start the standby on 5433
  5. Check replication → /root/db/05-repl.txt
  6. Check read-only → /root/db/06-readonly.txt
  7. Promote
  8. Compare timelines → /root/db/08-timeline.txt
  9. Incident: three newly attached standbys do not follow the primary

Notes

Check the primary's replication settings and add to them

Check with pg_settings that wal_level, max_wal_senders, and hot_standby are already at defaults suitable for replication, and change two settings that are not defaults — wal_log_hints = on, wal_keep_size = 256MB. Both must be actually in effect now (if a setting needs a restart, go as far as restarting).

Direction: in PostgreSQL 16 the values replication needs are already on by default. Check first, and change only what is not a default. Whether a changed setting takes effect with a reload or needs a restart differs per setting. Tools: the setting, source, context, and pending_restart columns of pg_settings, alter system set, restart and reload of pg_ctl. Specifics: settings whose context is postmaster change only after a restart, and for sighup a reload is enough. The primary's data directory is /var/lib/postgresql/data, and the log is /var/log/postgresql.log.

Replication role and slot

On the primary, create a replication-only login role rep (password rep) and a physical replication slot s1. The base backup in the next step uses these two names.

Direction: on the primary, create the role the standby will connect as, and a marker (the slot) saying not to delete WAL until the standby has taken it. Without a slot, if the standby disconnects briefly and comes back, the WAL it needs is already gone and replication breaks. Tools: the replication and login attributes of create role, pg_create_physical_replication_slot, and pg_roles and pg_replication_slots to check. Specifics: the replication privilege is an attribute granted separately from ordinary privileges (it does not need to be a superuser). The slot type is physical — logical slots are for logical decoding, not streaming replication.

Base backup

Base backup → /var/lib/postgresql/standby

Direction: the standby starts from a copy of the primary's files. To get a consistent copy, you must also receive the WAL generated while the copy was being taken. Tools: pg_basebackup's -X stream (WAL along with it), -S (the slot from the previous step), -R (writes the standby marker and connection info), and the role rep. Specifics: the destination is /var/lib/postgresql/standby, and it must be empty. With -R, a standby.signal file and primary_conninfo in postgresql.auto.conf are created — that one file is the marker that says "you are a standby".

Standby on 5433

Start the standby on 5433

Direction: the copy brought over even the primary's settings (port 5432). To run both in the same Pod, you only need to change the port. Tools: postgresql.auto.conf (read after postgresql.conf, so it wins), -D and -l of pg_ctl start, pg_is_in_recovery(). Specifics: the port is 5433 and the log is /var/log/labhub/standby.log. When connecting to check, be sure to add -h 127.0.0.1 — this environment has moved the Unix socket directory, so if you change only the port, the socket path cannot be found. On a standby, pg_is_in_recovery() is t.

Is it really replicating

Check replication → /root/db/05-repl.txt

Direction: check from both sides whether what you write on the primary is visible on the standby, and how the primary sees the standby. Tools: the primary's pg_stat_replication (state, sent_lsn, replay_lsn, replay_lag), and the same select on both sides. Specifics: create a table ha(id int) on the primary and insert rows, then wait a moment and count on 5433. pg_stat_replication is visible only on the primary; save its output to /root/db/05-repl.txt. Separately from the file, the grader counts the rows on both sides itself.

Why the standby refuses writes

Check read-only → /root/db/06-readonly.txt

Direction: the standby cannot produce WAL on its own, so it cannot accept writes — it is structure, not configuration. Confirm it with the error message. Tools: an insert into ha on 5433, and a redirection that also captures standard error. Specifics: the error goes to standard error, so you must capture it together with 2>&1 for it to land in /root/db/06-readonly.txt.

Promote

Promote the standby (5433), and insert one row into ha on the promoted 5433.

Direction: promotion tells the standby "you are the primary now". Once promoted, it leaves recovery and accepts writes. Tools: pg_ctl promote (or select pg_promote()), pg_is_in_recovery(). Specifics: promotion may not finish immediately — wait a few seconds, check that pg_is_in_recovery() is f, and then insert on 5433. The grader checks that ha on the new primary has more rows than on the old primary.

The timeline forks

Compare timelines → /root/db/08-timeline.txt

Direction: a promoted server starts writing its own history on a new timeline. The WAL file name reveals that fact. Tools: pg_walfile_name(pg_current_wal_lsn()) on each of the two servers. Specifics: the first 8 digits of the file name are the timeline (00000001 / 00000002). Write the two names to /root/db/08-timeline.txt as two lines, old-primary: and promoted:. This is why you cannot simply reattach the old primary — to reattach it, you have to rewind it with pg_rewind to the point where they forked.

Incident — three newly attached standbys do not follow the primary

Running incident rp-standby-join starts the incident (read the ticket first). Symptom: on the three standbys stby1, stby2, and stby3 (5441, 5442, 5443, /var/lib/postgresql/rp-join/<name>) newly attached to the primary rp-main (127.0.0.1:5440, /var/lib/postgresql/rp-join/main), not a single row written on the primary is visible. Fix it so that all three follow the primary in real time. Standby stbyN must connect with its own replication role rep_stbyN, over TCP to 127.0.0.1:5440, using its own replication slot. Leave the primary's data as it is, do not add an access rule that opens replication to many roles at once (replication all), and do not leave behind slots nobody uses. In /root/incident/rp-standby-join/report.txt, write for each standby one line stby1: <code> and one line evidence-stby1: <observed line> (the same form for stby2 and stby3). The code for each standby is one of the following: conninfo-port (the port in the connection info is wrong) · hba-missing (the primary's pg_hba.conf has no line for that replication role) · no-signal (it started without standby.signal and became a server accepting writes on its own) · slot-missing (the requested slot does not exist on the primary).

Direction: "does not follow" splits into three — it started as something other than a standby, it is rejected when trying to connect, or it is trying to connect to the wrong place. For each standby, first sort out which one it is. Tools: pg_is_in_recovery(), the FATAL lines in the standby log (/var/log/labhub/rp-join/.log), pg_stat_wal_receiver, the primary's pg_stat_replication, pg_replication_slots, and pg_hba_file_rules, and postgresql.auto.conf and standby.signal in the data directory. Specifics: when it fails, the WAL receiver reconnects every few seconds and leaves the same FATAL — that sentence tells you who rejected it (the primary's access rules, the slot, or the connection itself). pg_hba.conf, primary_conninfo, and primary_slot_name take effect with a reload. A server that started up accepting writes is already a copy that has diverged from the primary, so placing a single marker file cannot bring it back.