Skip to content

[8] graph/db: use batch loading for various graph SQL methods#10129

Merged
ellemouton merged 9 commits intolightningnetwork:masterfrom
ellemouton:graphPerf9
Aug 8, 2025
Merged

[8] graph/db: use batch loading for various graph SQL methods#10129
ellemouton merged 9 commits intolightningnetwork:masterfrom
ellemouton:graphPerf9

Conversation

@ellemouton
Copy link
Collaborator

@ellemouton ellemouton commented Aug 5, 2025

Depends on #10127
Part of #9795

This PR continues with performance improvements for the native SQL graph store. This is done for methods:

  • ChanUpdatesInHorizon
  • DeleteChannelEdges
  • PruneGraph
  • DisconnectBlockAtHeight
  • FetchChanInfos

Example performance improvement of ChanUpdatesInHorizon:

The new benchmark test is used to measure the performance improvement of the ChanUpdatesInHorizon call:

name                                                      old time/op  new time/op  delta
GraphReadMethods/ChanUpdatesInHorizon-kvdb-sqlite-10       4.18s ±11%   4.07s ± 3%     ~     (p=0.077 n=9+9)
GraphReadMethods/ChanUpdatesInHorizon-native-sqlite-10     18.5s ± 3%    2.0s ± 5%  -89.11%  (p=0.000 n=9+9)
GraphReadMethods/ChanUpdatesInHorizon-native-postgres-10   59.0s ± 3%    0.8s ±10%  -98.65%  (p=0.000 n=10+9)

@ellemouton ellemouton self-assigned this Aug 5, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @ellemouton, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on optimizing the native SQL graph store by implementing batch loading for several critical graph database operations. The primary goal is to reduce the number of individual database queries, thereby significantly improving the performance of methods responsible for fetching, deleting, and pruning channel and node information within the graph.

Highlights

  • Performance Improvement: This pull request significantly improves the performance of several graph SQL methods by introducing batch loading. For example, benchmarks show ChanUpdatesInHorizon performance improving by 89.11% for native SQLite and 98.65% for native PostgreSQL.
  • Batch Loading Implementation: Introduced generic batch loading functions (batchBuildChannelEdges, batchBuildChannelInfo) to efficiently retrieve channel, policy, and node data from the SQL database. These functions minimize individual database queries by fetching related data in bulk.
  • Refactored Graph Store Methods: The following SQLStore methods have been refactored to leverage the new batch loading capabilities: ChanUpdatesInHorizon, DeleteChannelEdges, PruneGraph, DisconnectBlockAtHeight, and FetchChanInfos. This replaces previous individual data fetching loops with more optimized batch operations.
  • New Interfaces for Genericity: Two new interfaces, ChannelAndNodes and ChannelAndNodeIDs, have been introduced in sqldb/sqlc/db_custom.go. These interfaces provide a common contract for different sqlc generated row types, enabling the new batch loading functions to operate generically across various query results.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces batch loading for several graph SQL methods, improving performance. The changes include modifications to benchmark tests, KV store, SQL migration, and SQL store files. The code adheres to the style guide, but some line lengths exceed 80 characters and could be wrapped for better readability. Additionally, some comments could be improved for clarity.

@ellemouton ellemouton requested review from bhandras and guggero August 5, 2025 14:52
@saubyk saubyk added this to lnd v0.20 Aug 5, 2025
@saubyk saubyk moved this to In review in lnd v0.20 Aug 5, 2025
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-98.65%, that's some epic gainz!!!
Great stuff, LGTM 🎉

Let the helper method only take the params it needs so that we dont need
to construct an entire models.ChannelEdgeInfo object to pass to it. This
will be useful later on.
Since the type returned from the DB is not a pointer. This will be
useful later on.
@ellemouton ellemouton changed the base branch from elle-graphPerf8 to master August 7, 2025 06:04
The following performance gains were measured using the new benchmark
test.

```
name                                      old time/op  new time/op  delta
ChanUpdatesInHorizon-native-sqlite-10     18.5s ± 3%    2.0s ± 5%  -89.11%  (p=0.000 n=9+9)
ChanUpdatesInHorizon-native-postgres-10   59.0s ± 3%    0.8s ±10%  -98.65%  (p=0.000 n=10+9)
```
Copy link
Collaborator

@bhandras bhandras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ⚡

@ellemouton ellemouton merged commit a3793b2 into lightningnetwork:master Aug 8, 2025
36 of 39 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in lnd v0.20 Aug 8, 2025
@ellemouton ellemouton deleted the graphPerf9 branch August 8, 2025 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants