92 Commits

Author SHA1 Message Date
Elle Mouton
f2ed5564ef graph/db+sqldb: improve performance of chan update sql migration
This commit simplifies insertChanEdgePolicyMig. Much of the logic can be
removed given that this method is only used in the context of the graph
SQL migration.

This should improve the performance of the migration quite a lot since
it removes the extra GetChannelAndNodesBySCID call.
2025-09-01 08:07:57 +02:00
Elle Mouton
22bf88e900 graph/db+sqldb: make policy migration idempotent
Finally, we make the channel-policy part of the SQL migration idempotent
by adding a migration-only policy insert query which will not error out
if the policy already exists and does not have a timestamp that is newer
than the existing records timestamp. To keep the commit simple, a
insertChanEdgePolicyMig function is added which is basically identical
to the updateChanEdgePolicy function except for the fact that it uses
the newly added query. In the next commit, it will be simplified even
more.
2025-09-01 08:06:12 +02:00
Elle Mouton
8736fcafa8 graph/db+sqldb: make channel SQL mig retry-safe
In this commit, we make the channel part of the graph SQL migration
idempotent (retry-safe!). We do this by adding a migration-only channel
insert query that will not error out if a the query is called and a
chanenl with the given scid&version already exists. We also ensure that
errors are not thrown if existing channel features & extra types are
re-added.
2025-09-01 08:05:21 +02:00
Elle Mouton
a291d6f1a6 graph/db+sqldb: improve efficiency of node migration
There is no need to use the "collect-then-update" pattern for node
insertion during the SQL migration since if we do have any previously
persisted data for the node and happen to re-run the insertion for that
node, the data will be exactly the same. So we can make use of "On
conflict, no nothing" here too.
2025-09-01 08:02:38 +02:00
Elle Mouton
ddea6d59ce graph/db+sqldb: make node migration idempotent
In this commit, the graph SQL migration is updated so that the node
migration step is retry-safe. This is done by using migration specific
logic & queries that do not use the same node-update-constraint as the
normal node upsert logic. For normal "run-time" logic, we always expect
a node update to have a newer timestamp than any previously stored one.
But for the migration, we will only ever be dealing with a single
announcement for a given node & to make things retry-safe, we dont want
the query to error if we re-insert the exact same node.
2025-09-01 07:56:34 +02:00
Oliver Gugger
31fc556507 Merge pull request #10155 from ziggie1984/add-missing-invoice-settle-index
Add missing invoice index for native sql
2025-08-14 09:23:37 -06:00
ziggie
ea9fb80eff sqldb: use the new schema
We put this new schema update into the main line and change the
versions of the schema updates which are currently only available
in dev builds. The schemas need to be chronological therefore we
also need to rename the file numbers.
2025-08-14 11:59:09 +02:00
ziggie
506d226eb3 sqldb: add missing index for settled invoices 2025-08-14 08:17:15 +02:00
ziggie
22fbbee837 sqldb: delete wrong index names and add missing one 2025-08-14 08:17:15 +02:00
Elle Mouton
5b06474744 graph/db+sqldb: batch validation for zombie index migration
Finally, we update the migrateZombieIndex function to use batch
validation just like was done in the previous commits. Here, we
additionally make sure to validate the entire zombie index entry and not
just the SCID.
2025-08-14 08:00:07 +02:00
Elle Mouton
a490e03479 graph/db+sqldb: use batch validation for closed SCID migration
As was done in the previous commits for nodes & channels, we update the
migrateClosedSCIDIndex function here so that it validates migrated
entries in batches rather than one-by-one.
2025-08-14 08:00:06 +02:00
Elle Mouton
8554f17b3f graph/db+sqldb: validate prune log migration using batching
As was done in the previous commits for nodes & channels, we update the
migratePruneLog function here so that it validates migrated entries in
batches rather than one-by-one.
2025-08-14 08:00:06 +02:00
Elle Mouton
81c54611c1 graph/db+sqldb: use batch fetching during channel&policy migration
Restructue the `migrateChannelsAndPolicies` function so that it does the
validation of migrated channels and policies in batches. So instead of
fetching channel and its policies individually after migrating it, we
wait for a minimum batch size to be reached and then validate a batch of
them together. This lets us make way fewer DB round trips.
2025-08-14 08:00:06 +02:00
Elle Mouton
03ef2740a6 graph/db+sqldb: use batch validation for node migration
Restructue the `migrateNodes` function so that it does the validation of
migrated nodes in batches. So instead of fetching each node individually
after migrating it, we wait for a minimum batch size to be reached and
then validate a batch of nodes together. This lets us make way fewer DB
round trips.
2025-08-14 08:00:06 +02:00
Elle Mouton
8de33fa601 graph/db: batch fetching for FetchChanInfos 2025-08-07 08:12:40 +02:00
Elle Mouton
594c842aeb graph/db: batch loading for DisconnectBlockAtHeight 2025-08-07 08:12:40 +02:00
Elle Mouton
69bcf47dca graph/db: use batch loading for PruneGraph 2025-08-07 08:12:40 +02:00
Elle Mouton
556af8e221 graph/db: use batch fetching for DeleteChannelEdges 2025-08-07 08:12:40 +02:00
Elle Mouton
ebe6a8af9f graph/db: use batch loading for ChanUpdatesInHorizon
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)
```
2025-08-07 08:12:40 +02:00
Elle Mouton
3b60d33ac8 sqlc: add ListChannelsForNodeIDs batch query
Add a ListChannelsForNodeIDs query which will let us fetch all channels
belonging to a set of nodes.
2025-08-05 08:00:19 +02:00
Elle Mouton
f39edae6e3 graph/db: use new batch helpers for edge loading
Use the new batch helpers to replace the existing logic for loading an
edge.
2025-07-31 07:19:14 +02:00
Elle Mouton
8ad5f633bc sqldb: add channel data batch queries
Also add the calling logic for these queries. This logic is not yet
used.
2025-07-31 07:19:14 +02:00
Elle Mouton
0dc0d320f8 graph/db+sqldb: queries and logic for batch fetching node data
In this commit, we add the queries that will be needed to batch-fetch
the data of a set of nodes. The logic for using these new queries is
also added but not used yet.
2025-07-30 19:29:21 +02:00
Elle Mouton
5a1184c664 graph/db+sqldb: remove LEFT JOIN for fetching node addresses
In this commit, we remove the LEFT JOIN query that was used for fetching
a nodes addresses. The reason it was used before was to ensure that we'd
get an empty address list if the node did exist but had no addresses.
This was for the purposes of the `AddrsForNode` method since it needs to
return false/true to indicate if the given node exists.
2025-07-30 18:11:01 +02:00
Elle Mouton
43db6683d2 graph/db+sqldb: only fetch what is needed for graph cache
Add a ListChannelsWithPoliciesForCachePaginated query that only fetches
the channel/policy fields that the cache requires.
2025-07-30 18:11:01 +02:00
Elle Mouton
3eed2f94c1 sqldb/sqlc: add index on graph_channels(version, id)
This new index greatly improves the speed of the
ListChannelsWithPoliciesPaginated query.
2025-07-30 18:11:01 +02:00
Elle Mouton
ddc0e95eda graph/db+sqldb: delete channels in batches
Use the new `SLICES` directive to add a DeleteChannels query which takes
a set of DB channel IDs. Then replace all our calls to DeleteChannel
with a paginated call to DeleteChannels.
2025-07-22 17:17:23 +02:00
Elle Mouton
e269d57ffa sqldb+graph/db: use pagination for FetchChanInfos 2025-07-22 17:17:11 +02:00
Elle Mouton
88e9a21d63 sqldb+graph/db: update FilterKnownChanIDs to use pagination
Remove a TODO by making use of the new sqldb.ExecutePagedQuery to fetch
channels in batches rather than one by one.
2025-07-22 17:16:59 +02:00
Elle Mouton
f72c48b283 graph/db+sqldb: pass set of outpoints to SQL
This commit adds a new GetChannelsByOutpoints query which takes a slice
of outpoint strings. This lets us then update PruneGraph to use
paginated calls to GetChannelsByOutpoints instead of making one DB call
per outpoint.
2025-07-22 17:16:49 +02:00
Elle Mouton
f0d2d1fd0a sqldb: demonstrate the use of ExecutePagedQuery
Here, a new query (GetChannelsByOutpoints) is added which makes use of
the /*SLICE:outpoints*/ directive & added workaround. This is then used
in a test to demonstrate how the ExecutePagedQuery helper can be used to
wrap a query like this such that calls are done in pages.

The query that has been added will also be used by live code paths in an
upcoming commit.
2025-07-22 17:16:38 +02:00
Elle Mouton
5afd9a5678 scripts: add sql slices workaround to sqlc gen script
This copies the workaround introduced in the taproot-assets code base
and will allow us to use `WHERE x in <list>` type queries.
2025-07-22 17:14:55 +02:00
Elle Mouton
74b70a5108 sqldb+graph/db: prefix graph SQL objects with "graph_"
This makes it more clear what each table is for especially when viewed
with other invoice/payments tables.
2025-07-15 18:40:16 +02:00
Elle Mouton
03ede9ccef graph/db: migrate prune log
This commit expands the `MigrateGraphToSQL` to include migration of the
prune log.
2025-07-11 09:15:47 +02:00
Oliver Gugger
500808fadd Merge pull request #10010 from ellemouton/sqlGraphUpdates
graph/db: various misc updates
2025-07-01 12:40:50 +02:00
Elle Mouton
4a05e5a226 sqldb/sqlc: add message and channe flags to channel_policies table
We need to explicitly store the entire bitfield types since we may have
channel_updates with bitfields containing bits we just dont need or
understand but we still need to store the entire bitfield so that the
reconstructed announcement remains valid.

This commit only adds the new columns but does not use them yet. NOTE:
this is ok since the migration adding this schema is not available in
the production build yet.
2025-07-01 10:12:54 +02:00
Elle Mouton
f1b7ccc6b2 sqldb+graph/db: prune graph nodes in a single query
Update the pruneGraphNodes routine to prune the graph nodes in a single
query instead of two.
2025-06-30 18:41:28 +02:00
Elle Mouton
933ab3c6b7 graph/db+sqldb: impl PutClosedScid and IsClosedScid 2025-06-25 13:26:52 +02:00
Elle Mouton
5effa96766 sqldb: closed_scids table 2025-06-25 13:26:52 +02:00
Elle Mouton
d46552f5ad graph/db+sqldb: implement AddEdgeProof
And run `TestAddEdgeProof` against the SQL backends.
2025-06-25 13:26:51 +02:00
Elle Mouton
e875183c4f sqldb+graph/db: impl DisconnectBlockAtHeight
Which lets us run `TestDisconnectBlockAtHeight` and
`TestStressTestChannelGraphAPI` against our SQL backends.
2025-06-25 11:22:03 +02:00
Elle Mouton
9dd0361ed0 graph/db+sqldb: impl PruneGraph, PruneTip, ChannelView
Which lets us run `TestGraphPruning` and `TestBatchedAddChannelEdge`
against our SQL backends.
2025-06-25 11:22:00 +02:00
Elle Mouton
2da701cc4f graph/db+sqldb: impl PruneGraphNodes
Which lets us run `TestChannelEdgePruningUpdateIndexDeletion` against
our SQL backends.
2025-06-25 10:48:35 +02:00
Elle Mouton
102c04daaf sqldb/sqlc: prune_log schema
Define the schema for the prune log.
2025-06-25 10:48:35 +02:00
Elle Mouton
eec89362a5 graph/db+sqldb: impl DisabledChannelIDs
Which lets us run `TestDisabledChannelIDs` against our SQL DB backends.
2025-06-24 21:04:36 +02:00
Elle Mouton
f1da3812de graph/db+sqldb: impl IsPublicNode
Which lets us run `TestNodeIsPublic` against our SQL DB backends.
Note that we need to tweak the tests a little bit so that
`AddLightningNode` for the same node is always called with a newer
LastUpdate time else it will fail the SQL constraint that only allows
the upsert if the update is newer than the persisted one.
2025-06-24 21:04:36 +02:00
Elle Mouton
13bf6a549f graph/db+sqldb: implement HasChannelEdge and ChannelID
And run `TestEdgeInfoUpdates` against our SQL backends.
2025-06-24 21:04:35 +02:00
Elle Mouton
4fad4a7023 graph/db+sqldb: implement FetchChannelEdgesByOutpoint/SCID
And run `TestEdgeInsertionDeletion` against our SQL backends.
2025-06-24 21:04:35 +02:00
Elle Mouton
2a6e6683eb graph/db+sqldb: implement DeleteChannelEdges
This lets us run TestGraphZombieIndex against the SQL backends.
2025-06-24 19:02:45 +02:00
Elle Mouton
00b6e0204c graph/db+sqldb: implement various zombie index methods
Here we implement the SQLStore methods:
- MarkEdgeZombie
- MarkEdgeLive
- IsZombieEdge
- NumZombies

These will be tested in the next commit as one more method
implementation is required.
2025-06-24 19:02:14 +02:00