fix: batch proof witness queries in check_state to prevent pool exhaustion #1514

Merged
thesimplekid merged 2 commits from debug_state_check into main 2026-01-10 20:36:51 +00:00
thesimplekid commented 2026-01-10 10:58:57 +00:00 (Migrated from github.com)

Description


The check_state function was making N individual database queries (one per
proof) inside a try_join_all loop. With many proofs, this spawned concurrent
queries that exhausted PostgreSQL's connection pool, causing timeouts.

This worked fine with SQLite (in-process, serialized) but failed with
PostgreSQL (network connections, fixed pool size).

Changes:

  • Replace N individual get_proofs_by_ys calls with single batched query
  • Use HashMap for O(1) witness lookup while preserving input order
  • Add empty slice guard to prevent invalid "WHERE y IN ()" SQL
  • Remove futures::try_join_all dependency

Notes to the reviewers


Suggested CHANGELOG Updates

CHANGED

ADDED

REMOVED

FIXED


Checklist

### Description <!-- Describe the purpose of this PR, what's being adding and/or fixed --> ----- The check_state function was making N individual database queries (one per proof) inside a try_join_all loop. With many proofs, this spawned concurrent queries that exhausted PostgreSQL's connection pool, causing timeouts. This worked fine with SQLite (in-process, serialized) but failed with PostgreSQL (network connections, fixed pool size). Changes: - Replace N individual get_proofs_by_ys calls with single batched query - Use HashMap for O(1) witness lookup while preserving input order - Add empty slice guard to prevent invalid "WHERE y IN ()" SQL - Remove futures::try_join_all dependency ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ----- ### Suggested [CHANGELOG](https://github.com/cashubtc/cdk/blob/main/CHANGELOG.md) Updates <!-- Please do not edit the actual changelog but note what you changed here. --> #### CHANGED #### ADDED #### REMOVED #### FIXED ---- ### Checklist * [ ] I followed the [code style guidelines](https://github.com/cashubtc/cdk/blob/main/CODE_STYLE.md) * [ ] I ran `just final-check` before committing
crodas (Migrated from github.com) reviewed 2026-01-10 13:30:08 +00:00
crodas (Migrated from github.com) left a comment

LGTM.

LGTM.
crodas (Migrated from github.com) approved these changes 2026-01-10 13:52:52 +00:00
cdk-bot commented 2026-01-10 20:37:01 +00:00 (Migrated from github.com)

Backport failed for v0.14.x, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin v0.14.x
git worktree add -d .worktree/backport-1514-to-v0.14.x origin/v0.14.x
cd .worktree/backport-1514-to-v0.14.x
git switch --create backport-1514-to-v0.14.x
git cherry-pick -x 2e004114479836a04887d6dddc8699755622d62c
Backport failed for `v0.14.x`, because it was unable to cherry-pick the commit(s). Please cherry-pick the changes locally and resolve any conflicts. ```bash git fetch origin v0.14.x git worktree add -d .worktree/backport-1514-to-v0.14.x origin/v0.14.x cd .worktree/backport-1514-to-v0.14.x git switch --create backport-1514-to-v0.14.x git cherry-pick -x 2e004114479836a04887d6dddc8699755622d62c ```
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cashubtc/cdk!1514
No description provided.