AI-assisted development · Verification ·

The slow tests were waiting for database cleanup

TickerTrac's release test run kept approaching or exceeding its time limit. I used Claude Code to investigate under explicit test-isolation and completion requirements. The investigation found repeated database storage work, discarded a candidate that passed its tests but failed cleanup, and reduced the backend stage from 677.7 to 152.2 seconds, about 78%.

Recorded September 30–October 1, 2026. These measurements concern local development infrastructure, not customer response time or production database capacity.

The last test was paying for cleanup

Reports blamed whichever test finished last on each of eight parallel workers. JUnit includes setup and teardown in a test's duration, so a slow database drop looked like a slow test. Phase-level timings and PostgreSQL activity revealed where the time went.

The fixture truncated all 230 test-owned tables before every test. In this schema, each reset replaced 1,515 storage files across tables, indexes, overflow storage and sequences. Eight workers created replacement files faster than PostgreSQL could finish syncing and removing them. Database drops then waited for that checkpoint work.

All 5,474 tests passed. The run still failed.

The first candidate skipped database drops and left cleanup to container removal. Pytest finished in 528.6 seconds, but 6.6 million files and 5.6 GB of write-ahead log accumulated. The container reached its 2,048 MiB memory ceiling, and removal exceeded its timeout. There was no valid completion verdict for the full run.

The agent discarded that candidate. Skipping the drops had also removed occasions when checkpoints were forced to finish. Moving cleanup to the end hid the cost until the system could no longer complete it within the budget.

Reset what changed, preserve the clean start

The accepted fix selects tables with allocated storage or an owned sequence that has been used, then applies the existing TRUNCATE … RESTART IDENTITY CASCADE. Untouched tables keep their files. Test-owned tables still start empty, and their sequences restart; explicitly preserved corpus tables retain their separate policy.

Checking only for rows would miss important cases. A failed insert can consume a sequence number without leaving a row. A rolled-back insert can leave allocated storage. Three regression tests cover those cases and confirm that an untouched table keeps its file.

Faster scheduling also exposed a pre-existing assertion that counted another test's retained filing rows. It was corrected to count its own company. The successful run passed 5,477 backend tests.

Measured improvement, with a defined scope

Recorded local runs. Backend-only and full-release rows are separate comparisons.
MeasurementBeforeAfter
Backend stage, run alone677.7 s152.2 s
Checkpoint files synced in that comparison7.58 million1.02 million
Complete release verification, all lanes931.0 s247.1 s

The isolated backend comparison used the same application base with the proposed fixture and test changes. The separate full-release runs included the other verification lanes. These are individual recorded runs, not a latency distribution or a promise that every run improves by the same amount.

The work was AI-assisted throughout: I initiated the investigation and set the requirements; Claude Code performed diagnosis, experiments and implementation. Evaluating the entire run kept a passing test suite from being mistaken for a successful optimization.

The result does not establish hosted-CI performance, customer latency, production scale or dollar savings. Foreign-key cascades still reset related tables, and database drops still wait for checkpoints. The selector also does not detect an unused sequence changed with setval(…, false); the inspected application and suite did not use that operation. The remaining work was documented rather than expanded into another optimization campaign.