This is the strongest recovery story in the programme, and the reason is simple: it is PostgreSQL's. Write-ahead logging, continuous archiving, point-in-time recovery to any moment within your archive, streaming replication, synchronous commit choices, and a mature tooling ecosystem - pgBackRest, barman, WAL-G - all of it works on a hypertable because a hypertable is tables. After five courses of "replay from your event log because the database has no backup", the ability to restore to 14:32:07 last Tuesday is worth appreciating explicitly.
Physical backup with WAL archiving is the right default. A base backup plus archived WAL gives point-in-time recovery, the restore is a file copy plus replay rather than a logical reload, and it scales to databases where pg_dump would take days. Use a tool rather than hand-rolling it: pgBackRest and friends handle retention, parallelism, compression, encryption and - critically - verification, which hand-rolled scripts invariably omit.
Logical backup with pg_dump has two TimescaleDB caveats and both bite. First, extension-aware restore: a dump contains the hypertable's catalogue entries, and restoring it requires the extension to cooperate - which is what timescaledb_pre_restore() and timescaledb_post_restore() are for. Call them around the restore, in that order, every time. Second, the restore target's extension version should match the dump's, because the internal catalogue format changes across versions. Neither is hard; both are easy to discover during an incident, which is the wrong time.
Point-in-time recovery deserves practice rather than reading. The mechanism: a base backup, archived WAL from then on, and a recovery_target_time in the restore configuration. The honest points are about time: the restore duration is a file copy plus WAL replay, and the replay portion is proportional to how much WAL accumulated since the base backup - so a weekly base backup means up to a week of WAL to replay, which may be hours. That relationship is your RTO, and the lever is base backup frequency.
Streaming replication gives you a hot standby for failover and a read replica for queries. Asynchronous by default - the lag is your RPO, and pg_stat_replication reports it. Synchronous replication removes that lag and makes every commit wait for the standby, which is a real latency cost on an ingest-heavy workload. For telemetry, asynchronous with a measured lag is almost always the right trade, and the point is to measure it rather than assume it.
Two TimescaleDB-specific points on replicas. Continuous aggregate refresh policies and compression policies are writes, so they run only on the primary - a replica's aggregates are maintained by replication, not by its own jobs, which is correct and worth knowing when a failover makes the standby primary. And a long-running analytical query on a hot standby can conflict with replay; hot_standby_feedback and max_standby_streaming_delay are the knobs, and the trade is bloat on the primary against query cancellations on the standby.
Verification. A restored database that starts is not a verified restore. The checks: row counts per chunk against the source; continuous aggregates recomputed and compared against raw data, which is the one people miss and the one that catches a partially-replayed restore; foreign key integrity; and amcheck on indexes if you are being thorough. And the drill must be timed, because an RTO you have not measured is a guess.
Laboratory exercise 5 - the drill, timed.
Configure WAL archiving and pgBackRest, take a base backup, and perform point-in-time recovery to a timestamp you choose, verifying the data is exactly as of that moment. Time every phase.
Do a destructive drill properly: with the system under live ingest, drop a continuous aggregate's underlying chunks, then recover to one minute before. Record RPO and RTO as measured numbers.
Demonstrate the extension-aware restore caveat: restore a pg_dump without timescaledb_pre_restore() and record exactly what fails. Then do it correctly.
Measure the base-backup-frequency lever: perform PITR from base backups taken 1 hour, 1 day and 7 days before the target, recording the WAL replay time for each. That table is your RTO-against-storage trade and it is the most useful artefact here.
Configure streaming replication, measure the lag under load, and perform a controlled failover, timing it. Then demonstrate the policy point: show that the standby's continuous aggregate jobs are not running, and that after promotion they are.
Demonstrate the standby conflict: run a long analytical query on the hot standby under heavy primary write load, and show the cancellation. Then fix it with each knob and state the cost of each.
Finally, write the verification suite: per-chunk row counts, continuous aggregates recomputed against raw, foreign key integrity, and the timing of each.
Deliverable: the archiving and backup configuration, the PITR with every phase timed, the destructive drill with measured RPO and RTO, the extension-aware restore failure and fix, the base-backup-frequency table, the replication lag and timed failover, the policy demonstration before and after promotion, the standby conflict with both knobs costed, and the verification suite.
-- ==================================================================
-- *** THE STRONGEST RECOVERY STORY IN THE PROGRAMME, BECAUSE IT IS
-- *** POSTGRESQL'S. ***
-- ==================================================================
-- WAL, continuous archiving, PITR to ANY MOMENT in the archive,
-- streaming replication, and a MATURE TOOLING ECOSYSTEM - all of it
-- works on a hypertable BECAUSE A HYPERTABLE IS TABLES.
--
-- *** After five courses of "replay from your event log because the
-- *** database has no backup", THE ABILITY TO RESTORE TO 14:32:07
-- *** LAST TUESDAY IS WORTH APPRECIATING EXPLICITLY. ***
-- postgresql.conf:
-- wal_level = replica
-- archive_mode = on
-- archive_command = 'pgbackrest --stanza=telemetry archive-push %p'
-- max_wal_size = '16GB'
-- checkpoint_timeout = '15min'
SELECT pg_current_wal_lsn(), pg_walfile_name(pg_current_wal_lsn());
SELECT * FROM pg_stat_archiver; -- failed_count MUST stay 0
-- *** PHYSICAL BACKUP + WAL ARCHIVING IS THE RIGHT DEFAULT: ***
-- pgbackrest --stanza=telemetry --type=full backup
-- pgbackrest --stanza=telemetry --type=incr backup
-- pgbackrest --stanza=telemetry info
-- *** pgbackrest --stanza=telemetry check *** <- VERIFICATION,
-- which hand-rolled scripts INVARIABLY OMIT. Use a tool.
-- ==================================================================
-- *** POINT-IN-TIME RECOVERY. Practise it; do not read about it. ***
-- ==================================================================
-- pgbackrest --stanza=telemetry --type=time \
-- --target="2026-11-25 14:32:07+00" --delta restore
--
-- postgresql.auto.conf gets:
-- restore_command = 'pgbackrest --stanza=telemetry archive-get %f "%p"'
-- recovery_target_time = '2026-11-25 14:32:07+00'
-- recovery_target_action = 'promote'
SELECT pg_is_in_recovery(), pg_last_wal_replay_lsn();
-- *** THE HONEST POINT IS ABOUT TIME: the restore is A FILE COPY
-- *** PLUS WAL REPLAY, and the replay portion is PROPORTIONAL TO HOW
-- *** MUCH WAL ACCUMULATED SINCE THE BASE BACKUP. ***
--
-- *** THE BASE-BACKUP-FREQUENCY LEVER - the most useful artefact
-- *** in this module: ***
-- base backup age WAL to replay copy replay TOTAL RTO
-- 1 hour 14 GB 11 min 4 min 15 min
-- 1 day 340 GB 11 min 47 min 58 min
-- *** 7 days 2,400 GB 11 min 5h 20m 5h 31m ***
--
-- *** A WEEKLY BASE BACKUP MEANS UP TO A WEEK OF WAL TO REPLAY. ***
-- That relationship IS YOUR RTO, and the lever is BASE BACKUP
-- FREQUENCY - which costs storage. Pick with the table in front of
-- you.
-- ==================================================================
-- *** LOGICAL BACKUP: TWO TIMESCALEDB CAVEATS, AND BOTH BITE. ***
-- ==================================================================
-- pg_dump -Fc -f telemetry.dump telemetry
--
-- *** (1) EXTENSION-AWARE RESTORE. A dump contains the hypertable's
-- *** CATALOGUE ENTRIES, and restoring needs the extension to
-- *** COOPERATE: ***
SELECT timescaledb_pre_restore(); -- *** BEFORE ***
-- \! pg_restore -d telemetry_restored telemetry.dump
SELECT timescaledb_post_restore(); -- *** AFTER ***
-- *** CALL THEM AROUND THE RESTORE, IN THAT ORDER, EVERY TIME. ***
-- Skip pre_restore and you get catalogue errors and a hypertable
-- that is not a hypertable. *** DEMONSTRATE THE FAILURE ONCE, in
-- the lab, SO YOU RECOGNISE IT. ***
--
-- *** (2) THE RESTORE TARGET'S EXTENSION VERSION SHOULD MATCH THE
-- *** DUMP'S *** - the internal catalogue format changes across
-- versions.
--
-- *** NEITHER IS HARD. BOTH ARE EASY TO DISCOVER DURING AN INCIDENT,
-- *** WHICH IS THE WRONG TIME. ***
-- ==================================================================
-- STREAMING REPLICATION
-- ==================================================================
-- primary: max_wal_senders = 10, wal_keep_size = '8GB'
-- standby: primary_conninfo = 'host=primary ...'
-- hot_standby = on
SELECT client_addr, state, sent_lsn, replay_lsn,
pg_wal_lsn_diff(sent_lsn, replay_lsn) AS replay_lag_bytes,
replay_lag, sync_state
FROM pg_stat_replication;
-- *** ASYNCHRONOUS BY DEFAULT: THE LAG IS YOUR RPO. MEASURE IT,
-- *** DO NOT ASSUME IT. ***
-- under load: replay_lag p50 180 ms p95 2.4 s p99 11 s
--
-- SYNCHRONOUS removes the lag and makes EVERY COMMIT WAIT:
-- synchronous_standby_names = 'ANY 1 (standby1, standby2)'
-- ingest 318,000 -> 94,000 rows/s -> *** a 70% cost ***
-- *** FOR TELEMETRY, ASYNCHRONOUS WITH A MEASURED LAG IS ALMOST
-- *** ALWAYS THE RIGHT TRADE. *** State it as a choice.
-- *** TWO TIMESCALEDB-SPECIFIC POINTS ON REPLICAS: ***
--
-- (1) *** CONTINUOUS AGGREGATE REFRESH AND COMPRESSION POLICIES ARE
-- *** WRITES, SO THEY RUN ONLY ON THE PRIMARY. *** A replica's
-- aggregates are maintained BY REPLICATION, not by its own jobs.
-- Correct - and worth knowing when a FAILOVER makes the standby
-- primary and its jobs START:
SELECT job_id, proc_name, scheduled, next_start
FROM timescaledb_information.jobs; -- on each node, before and
-- after promotion
--
-- (2) A LONG ANALYTICAL QUERY ON A HOT STANDBY *** CONFLICTS WITH
-- REPLAY: ***
-- ERROR: canceling statement due to conflict with recovery
-- The knobs, and the trade:
-- hot_standby_feedback = on -> *** BLOAT ON THE PRIMARY ***
-- max_standby_streaming_delay=300s -> *** REPLICATION LAG ***
-- There is no free option; pick and write down which.
-- ==================================================================
-- *** VERIFICATION. A restored database THAT STARTS is not a
-- *** verified restore. ***
-- ==================================================================
-- (1) ROW COUNTS PER CHUNK, against the source:
SELECT c.chunk_name, c.range_start, c.range_end,
(SELECT COUNT(*) FROM vehicle_telemetry t
WHERE t.time >= c.range_start AND t.time < c.range_end) AS rows
FROM timescaledb_information.chunks c
WHERE c.hypertable_name = 'vehicle_telemetry'
ORDER BY c.range_start;
-- (2) *** CONTINUOUS AGGREGATES RECOMPUTED AND COMPARED AGAINST RAW
-- *** - THE ONE PEOPLE MISS, AND THE ONE THAT CATCHES A
-- *** PARTIALLY-REPLAYED RESTORE: ***
SELECT c.bucket, c.vehicle_id, c.avg_speed AS stored, x.recomputed
FROM telemetry_1m c
JOIN (SELECT time_bucket(INTERVAL '1 minute', time) AS bucket,
vehicle_id, AVG(speed_kph) AS recomputed
FROM vehicle_telemetry
WHERE time >= now() - INTERVAL '3 days'
GROUP BY 1, 2) x
ON x.bucket = c.bucket AND x.vehicle_id = c.vehicle_id
WHERE ABS(c.avg_speed - x.recomputed) > 0.0001;
-- Expected: no rows.
-- (3) FOREIGN KEY INTEGRITY:
SELECT COUNT(*) FROM vehicle_telemetry t
LEFT JOIN vehicles v ON v.vehicle_id = t.vehicle_id
WHERE v.vehicle_id IS NULL;
-- Expected: 0.
-- (4) INDEXES, if you are being thorough:
-- CREATE EXTENSION amcheck;
-- SELECT bt_index_check(c.oid) FROM pg_class c
-- JOIN pg_index i ON i.indexrelid = c.oid WHERE c.relkind = 'i';
-- *** AND THE DRILL MUST BE TIMED, BECAUSE AN RTO YOU HAVE NOT
-- *** MEASURED IS A GUESS: ***
-- detect ___ min
-- decide ___ min
-- restore (copy) ___ min
-- WAL replay ___ min <- the base-backup-frequency lever
-- verify ___ min
-- *** TOTAL RTO ___ min *** RPO: ___ (the replication lag,
-- or the archive interval)
Discussion
No questions on this topic yet. Sign in to ask the first one.