Postgres doesn't index the referencing side of a foreign key, so every
row deleted from `blocks` scans `transactions`, `channeltxs` and
`outputs` in full. Postgres was taking 100% CPU for multiple minutes,
causing startup timeouts in systemd.
247aa13 and 50a8e32 added indices for `outputs` and `utxoset` in
2018. `channeltxs` (28feb2e) and `transactions` (0f89653) arrived
weeks later without one. `outputs` is only half-covered:
`output_height_idx` is (`confirmation_height`, `spend_height`), which
doesn't serve a lookup on `spend_height` alone.
Changelog-Fixed: db: much faster startup on postgres nodes with large `transactions` or `channeltxs` tables.
Postgres doesn't index the referencing side of a foreign key, so every row deleted from blocks scans transactions, channeltxs and outputs in full. Postgres was taking 100% CPU for multiple minutes, causing startup timeouts in systemd.
247aa13 and 50a8e32 added indices for outputs and utxoset in
2018. channeltxs (28feb2e) and transactions (0f89653) arrived weeks later without one. outputs is only half-covered: output_height_idx is (confirmation_height, spend_height), which doesn't serve a lookup on spend_height alone.
Changelog-Fixed: db: much faster startup on postgres nodes with large transactions or channeltxs tables.
Tested on a production node running into the issue described above (fixes it).