3 Watch a database under load become plain SQL backups in real-time. Three terminals recommended.
8 # Install the extension
9 cd wal2sql && make && make install
11 # Configure PostgreSQL for logical replication
12 echo "wal_level = logical" >> ~/.pgenv/pgsql/data/postgresql.conf
13 echo "max_replication_slots = 10" >> ~/.pgenv/pgsql/data/postgresql.conf
14 echo "max_wal_senders = 10" >> ~/.pgenv/pgsql/data/postgresql.conf
18 ## Phase 1: Initialize with pgbench workload
20 **Terminal 1** - Initialize database and backup system:
23 # Create database and populate with pgbench tables
24 createdb -U postgres demo
25 pgbench -i -s 10 -U postgres demo # Creates 1M rows in pgbench_accounts
27 # Try to initialize pg_scribe
28 pg_scribe --init -d demo -f /tmp/demo_backup -U postgres
31 This will fail with a validation error:
34 ERROR: CRITICAL: The following tables lack adequate replica identity:
35 ERROR: - public.pgbench_history
36 ERROR: Fix: Add a primary key or set replica identity:
37 ERROR: ALTER TABLE <table> ADD PRIMARY KEY (id);
39 ERROR: ALTER TABLE <table> REPLICA IDENTITY FULL;
42 **Why?** The `pgbench_history` table is append-only (no primary key) and needs replica identity for logical replication. pg_scribe validates this upfront to prevent silent data loss.
47 # Set replica identity for the append-only history table
48 psql -U postgres demo -c "ALTER TABLE pgbench_history REPLICA IDENTITY FULL;"
50 # Now initialize pg_scribe successfully
51 pg_scribe --init -d demo -f /tmp/demo_backup -U postgres
53 # Start streaming changes to active.sql
54 pg_scribe --start -d demo -f /tmp/demo_backup -U postgres
57 Leave Terminal 1 streaming. Every database change now becomes SQL.
59 **Terminal 2** - Generate realistic load:
62 # Run pgbench workload in background: 5 clients, 10 minutes
63 # This will run continuously throughout the tutorial
64 pgbench -c 5 -T 600 -U postgres demo &
67 Leave this running in the background. It will generate continuous load for the rest of the tutorial.
69 **Terminal 3** - Watch the backup grow:
72 # Check replication status (run this a few times while pgbench is running)
73 pg_scribe --status -d demo -f /tmp/demo_backup -U postgres
76 You'll see actual replication metrics updating in real-time: LSN positions, lag bytes, transaction counts, and file sizes growing!
79 # Peek at the SQL being captured
80 tail -n 30 /tmp/demo_backup/chain-*/active.sql
83 You'll see thousands of INSERT/UPDATE transactions accumulating as plain SQL!
85 ## Phase 2: Rotate differentials under load
87 (pgbench is still running in the background from Phase 1)
89 **Terminal 3** - Rotate differential while load continues:
92 pg_scribe --rotate-diff -f /tmp/demo_backup
95 **Terminal 3** - Verify the rotation:
98 # Check status - you'll see the new active.sql starting fresh
99 pg_scribe --status -d demo -f /tmp/demo_backup -U postgres
101 # Or list the files to see the sealed differential with timestamp
102 ls -lht /tmp/demo_backup/chain-*/
105 **What happened:** The old `active.sql` was sealed with a timestamp (e.g., `diff-YYYYMMDD-HHMMSS.sql`). New transactions now write to a fresh `active.sql`. The rotation happened instantly with zero transaction loss. Replication never stopped.
107 ## Phase 3: Chain transfer under load (zero-downtime operation)
109 Chains let you periodically create fresh compressed base backups (e.g., weekly). This keeps old chains archive-ready while new data streams to the new chain.
111 (pgbench is still running in the background from Phase 1)
113 **Terminal 3** - Create new chain and transfer streaming to it:
116 # Stops old streaming, creates new compressed base backup, starts streaming to new chain
117 pg_scribe --new-chain --start -d demo -f /tmp/demo_backup -Z gzip:6 -U postgres
120 This automatically stops the old stream in Terminal 1. Terminal 3 now holds the new streaming process.
122 **Terminal 1** - Verify new chain is active (Terminal 1 is now free):
125 pg_scribe --status -d demo -f /tmp/demo_backup -U postgres
128 The status output will show which chain is active and confirm streaming is working.
131 # You can also list directory to see both chains
132 ls -lh /tmp/demo_backup/
133 # chain-YYYYMMDD-HHMMSS/ (old chain, sealed)
134 # chain-YYYYMMDD-HHMMSS/ (new chain, active)
137 The old chain's final differential was sealed. The new chain has a fresh compressed base backup. No transactions were lost during the transfer.
139 ## Phase 4: Catching up after pause (shows resilience)
141 **Terminal 1** - Stop streaming (this stops Terminal 3's stream):
144 pg_scribe --stop -f /tmp/demo_backup
147 **Terminal 2** - Stop background pgbench (if still running) and generate load while streaming is stopped:
150 # Stop the background pgbench if it's still running
153 # Generate new load while streaming is stopped
154 pgbench -c 5 -t 1000 -U postgres demo
157 **Terminal 1** - Check status (streaming stopped, WAL accumulating):
160 pg_scribe --status -d demo -f /tmp/demo_backup -U postgres
163 You'll see something like: "Replication slot is 45 MB behind current WAL position"
165 The replication slot preserved the WAL even though streaming stopped!
167 **Terminal 1** - Restart streaming and watch it catch up:
170 pg_scribe --start -d demo -f /tmp/demo_backup -U postgres
173 Leave Terminal 1 streaming.
175 **Terminal 3** - Monitor catch-up progress:
178 watch -n 1 'pg_scribe --status -d demo -f /tmp/demo_backup -U postgres'
181 Watch the lag decrease as it replays accumulated WAL. Ctrl+C when caught up.
183 ## Phase 5: Restore and verify
185 **Terminal 1** - Seal final differential, then stop streaming:
188 pg_scribe --rotate-diff -f /tmp/demo_backup
189 pg_scribe --stop -f /tmp/demo_backup
192 **Terminal 1** - Restore to new database:
195 pg_scribe --restore -d demo_restored -f /tmp/demo_backup -C -U postgres
198 **Terminal 1** - Verify data matches:
202 psql -U postgres demo -c "SELECT count(*) FROM pgbench_accounts;"
203 psql -U postgres demo_restored -c "SELECT count(*) FROM pgbench_accounts;"
205 # Compare transaction history (shows all transactions were captured)
206 psql -U postgres demo -c "SELECT count(*) FROM pgbench_history;"
207 psql -U postgres demo_restored -c "SELECT count(*) FROM pgbench_history;"
209 # Compare balances (proves data integrity)
210 psql -U postgres demo -c "SELECT sum(abalance) FROM pgbench_accounts;"
211 psql -U postgres demo_restored -c "SELECT sum(abalance) FROM pgbench_accounts;"
219 # Stop pgbench if still running
222 # Clean up databases and backup files
223 dropdb -U postgres demo_restored
224 dropdb -U postgres demo
225 rm -rf /tmp/demo_backup
228 ## What just happened
230 - `pgbench -i` created realistic TPC-B-like tables with 1M rows
231 - `pg_scribe --init` created the wal2sql extension, replication slot, and base backup
232 - `pg_scribe --start` streamed thousands of transactions as plain SQL
233 - Differential rotation worked safely during active writes
234 - Chain transfer happened with zero transaction loss
235 - Replication slot preserved WAL during streaming pause
236 - System caught up gracefully from lag
237 - `--restore` perfectly reconstructed the database
238 - All backups are plain SQL readable with `less`
244 pg_scribe --init -d mydb -f /backups/mydb -U postgres
246 # Run continuously (use systemd or supervisor)
247 pg_scribe --start -d mydb -f /backups/mydb -U postgres
249 # Rotate differentials daily (cron job)
250 0 0 * * * pg_scribe --rotate-diff -f /backups/mydb
252 # Create new chain weekly with compressed base backup
253 # This automatically stops old streaming and starts streaming to new chain
254 0 0 * * 0 pg_scribe --new-chain --start -d mydb -f /backups/mydb -Z gzip:6 -U postgres
256 # Monitor replication lag (nagios/prometheus)
257 pg_scribe --status -d mydb -f /backups/mydb -U postgres