We Believed Our Own Timeout, Twice
The restore worked. The logs said so plainly: archive recovery complete, database system is ready to accept connections. Our tool reported that it had timed out after ten minutes, and we believed our tool. It is a reasonable thing to do. We are reasonable people. We had just experienced some very unreasonable things, and we did it again a few hours later.
The rehearsal#
The job was a disaster-recovery rehearsal: prove that a production database on Kubernetes can be restored to a point in time, into an empty namespace, while a mistake is still free. Seed a row, take a base backup, record a target time, restore to it, and check two things. The row from before the target is there. A row from after the target is not.
The restore leg failed on its first live run:
recovery cluster not Ready within 600s
The recovery template had genuinely never been exercised. So suspicion landed there, naturally and reasonably. We spent a while on it.
Bug one: the question that could not be asked#
The readiness check was assembled inside a bash -c string. The predicate it
wanted to ask was a jsonpath filter on the cluster object:
{.status.conditions[?(@.type=="Ready")].status}
That filter has double quotes in it. Escaped through the string they reached
kubectl as \"Ready\", and kubectl does not accept that. We can show exactly
what arrived, with no cluster at all, because kubectl patch --local
evaluates jsonpath against a file:
#!/bin/sh
IMG=cr.example.com/kubectl:1.33.5
k() { podman run --rm --network none -v "$PWD/cluster.yaml:/cluster.yaml:ro,Z" "$IMG" kubectl "$@"; }
printf 'as written: '
k patch --local -f /cluster.yaml --type merge -p '{}' \
-o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'; echo
printf 'as it arrived: '
k patch --local -f /cluster.yaml --type merge -p '{}' \
-o jsonpath='{.status.conditions[?(@.type==\"Ready\")].status}'; echo
as written: True
as it arrived: error: error parsing jsonpath {.status.conditions[?(@.type==\"Ready\")].status}, unterminated filter
So the predicate returned empty, every five seconds, forever. It would have timed out against any cluster, including a perfectly healthy one. Which is what it did. The restored database had come up and said so in its own log:
LOG recovery stopping before commit of transaction 771
LOG selected new timeline ID: 2
LOG archive recovery complete
LOG database system is ready to accept connections
while the poll sat there, asking a question with a syntax error in it, and reporting the silence as the database’s fault.
Establishing that takes about thirty seconds: run the predicate by hand against a cluster you know is Ready and see whether it matches. We did not establish it for ten minutes, because the instrument’s answer agreed with the suspect we already had.
Bug two: the target that moved#
Under the quoting bug there was a real defect, and it was ours too.
A point-in-time restore needs a transaction record at or after the target time in order to stop cleanly. PostgreSQL’s documentation is direct about what happens otherwise: “if a recovery target is configured but the archive recovery ends before the target is reached, the server will shut down with a fatal error.” Our target was six seconds after the last archived transaction on a quiet database, so:
FATAL: recovery ended before configured recovery target was reached
LOG: last completed transaction was at log time 2026-08-19 17:49:34.927661+00
A longer sleep does not fix that. The problem is not latency; nothing happened after the target. So the fix writes a transaction after the target, forces a segment switch, and waits for the archiver to catch up before starting the restore.
The wait we wrote compared the archiver’s last archived segment against the segment the server was currently writing:
last_archived_wal >= pg_walfile_name(pg_current_wal_lsn())
Look, that reads fine. It is also false forever. The segment being written is not archived until it is itself closed, so the archiver is permanently one behind the right-hand side, and every time it catches up the right-hand side has moved. On a real PostgreSQL, immediately after a switch:
#!/bin/sh
C=rig-pg-$$
podman run -d --rm --name "$C" --network none -e POSTGRES_PASSWORD=x \
cr.example.com/postgres:17 \
-c archive_mode=on -c "archive_command=cp %p /tmp/%f" >/dev/null
until podman exec "$C" pg_isready -q 2>/dev/null; do sleep 1; done
q() { podman exec "$C" psql -U postgres -Atc "$1"; }
q "create table t(x int)" >/dev/null
q "insert into t values (1)" >/dev/null
switched=$(q "select pg_walfile_name(pg_switch_wal())")
sleep 2
echo "segment closed by pg_switch_wal(): $switched"
echo "segment being written now: $(q "select pg_walfile_name(pg_current_wal_lsn())")"
echo "pg_stat_archiver.last_archived_wal: $(q "select last_archived_wal from pg_stat_archiver")"
podman stop -t 2 "$C" >/dev/null
segment closed by pg_switch_wal(): 000000010000000000000002
segment being written now: 000000010000000000000003
pg_stat_archiver.last_archived_wal: 000000010000000000000002
The archiver is exactly where it should be. It has archived the segment that
was closed. It has not archived the one being written, and it never will until
that one closes too, at which point there is a new one being written. Wait for
the fixed target instead: the segment name that pg_switch_wal() itself
returns. That converges in about a second.
Two bugs, one face#
The first bug was a typo. The second was a category error, a comparison against a moving target. They have nothing in common except how they looked from outside, and from outside they were identical: a timeout, which made the system under test look guilty while the fault was in the instrument. The recovery template took the blame twice in one afternoon for defects in the thing watching it.
That is the part worth keeping. A timeout is not an observation. It is a claim about the world, made by an instrument, and when the instrument is broken the claim is indistinguishable from the thing genuinely never happening. Worse, it confirms whatever you already suspected, because the failure you expected is a timeout too.
The fix felt good, both times. It felt kind of invincible. That is the feeling to distrust.
The third mistake was temperament#
On failure the tool cleaned up after itself. It tore down the recovery cluster it had just failed to verify, deleting the evidence that would have shown the database was fine, and the diagnosis had to be reconstructed afterwards from shipped logs.
Cleanup-on-failure is the default nobody chooses deliberately, and it is exactly backwards. Preserve on failure. Tidy on success. The tool now captures the recovery cluster’s conditions, pods and last forty log lines, prints the most likely cause, and leaves the cluster standing with the teardown command written out for whoever comes to look.
What we always say at a time like this#
Had we checked the check? Yes sir. It was in the mail.
Neither bug was caught by code review, by a passing self-test suite, or by a linter, because none of those ask the only question that matters about a poll: could this ever say yes? Both predicates were valid shell and valid-looking jsonpath. They rendered. Rendering was read as executing, and the gap between those two rungs is exactly where a syntactically fine, completely broken command lives.
So, the rules, the way we say them now:
- A wait gets a regression test asserting that its predicate matches a fixture known to be in the desired state. Negative-control it: the old broken form must miss against the same fixture, or the test proves nothing.
- Never build a kubectl invocation inside a
bash -cstring. Make it a shell function. Escaping through a string is where jsonpath filters go to die. - A poll’s target must be fixed, not moving. If the right-hand side advances whenever the left-hand side does, it does not converge.
- When a wait expires, print both sides of the comparison and any counter that
separates “broken” from “slow”. For an archiver that is
failed_count: a non-zero value means archiving is broken, which is a different problem with a different fix. “Did not catch up” alone throws away the evidence the tool was already holding. - And before believing a timeout, ask whether the predicate could ever have been true. Thirty seconds. Both bugs die.
The common thread under assertions that are vacuous, tests that pass by skipping and polls that wait for the impossible is a check whose own correctness was never checked. The tell is always the same, and we had it both times: the result was too consistent with what we already believed.