Skip to content

[v0.21.x-branch] Backport #11413: htlcswitch: avoid per-row queries when pruning network result store - #11418

Merged
ziggie1984 merged 1 commit into
v0.21.x-branchfrom
backport-11413-to-v0.21.x-branch
Oct 26, 2026
Merged

ziggie1984 merged 1 commit into
v0.21.x-branchfrom
backport-11413-to-v0.21.x-branch

Conversation

@github-actions

Copy link
Copy Markdown

Backport of #11413


Supersedes #11408.

The original implementation is by @ZZiigguurraatt. Its commit is preserved as the first commit in this PR with the original authorship. The second commit fixes the PostgreSQL test setup and incorporates review feedback.

Change Description

Pruning the network result store previously performed one database operation per stored result. On SQL-backed nodes, especially those using a remote PostgreSQL database, the accumulated round trips could make startup cleanup take hours.

This change:

  • reads retained results in one streamed iteration;
  • drops and recreates the bucket when entries must be removed, then restores retained results;
  • preserves the no-op path when every result is retained;
  • runs the backend-specific test against embedded PostgreSQL for the kvdb_postgres build tag; and
  • covers partial retention, removing all results, retaining all results, and an empty store with distinct payload assertions.

Fixes #11353.

Testing

  • go test ./htlcswitch -run '^TestNetworkResultStoreCleanStore -count=1
  • go test -tags=kvdb_sqlite ./htlcswitch -run '^TestNetworkResultStoreCleanStore -count=1
  • go test -tags=kvdb_postgres ./htlcswitch -run '^TestNetworkResultStoreCleanStore -count=1
  • make lint pkg=htlcswitch

The network result store is pruned at startup, keeping only results for
payments that are still in flight. Previously, cleanStore iterated over
the bucket and deleted each stale key individually.

On SQL backends, each iteration step and deletion requires separate
database queries. A store with N entries therefore incurred O(N)
sequential client/server round trips and could make startup take hours
when using remote PostgreSQL.

Read retained results with one streaming query, drop and recreate the
bucket, then restore the retained entries. PostgreSQL still processes
O(N) rows internally, but the operation no longer performs a round trip
for every stored result.

Exercise cleanup across the supported kvdb backends, including partial,
empty, and unchanged result sets. Add PostgreSQL benchmarks covering
1,000 and 10,000 entries with fully stale and partially retained stores.

Fixes #11353.

Co-authored-by: ziggie <ziggie1984@protonmail.com>
(cherry picked from commit e092b72)
@ZZiigguurraatt

Copy link
Copy Markdown
Contributor

Did a test of this backport, it seems to be working:

$ kubectl logs -n smokenet-dev2 deployments/frank | grep -E "LTND: Version Info|HTLC Switch starting|Channel Router starting|Removed [0-9]+ stale entries from network result store"
2026-10-27 11:54:45.941 [INF] LTND: Version Info rev="" version=0.21.5-beta.rc1 commit="" debuglevel=production logging=info
2026-10-27 11:55:07.643 [INF] HSWC: HTLC Switch starting
2026-10-27 11:55:28.205 [INF] CRTR: Channel Router starting
2026-10-27 11:55:30.060 [INF] HSWC: Removed 29870 stale entries from network result store
$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants