Skip to content

tapdb: fix postgres sequence desyncs#2047

Open
jtobin wants to merge 4 commits into
lightninglabs:mainfrom
jtobin:mig31-fix
Open

tapdb: fix postgres sequence desyncs#2047
jtobin wants to merge 4 commits into
lightninglabs:mainfrom
jtobin:mig31-fix

Conversation

@jtobin
Copy link
Copy Markdown
Member

@jtobin jtobin commented Apr 2, 2026

Fixes Postgres sequence desyncs caused by a couple of legacy migrations. One is material (in universe_leaves, via migration 31) and the other two are benign (supply commitment-related enum values, via migration 40). Each desync'd sequence is simply set to its respective table's max id.

Also adds a Postgres-only unit test that should catch future desyncs caused by migrations like this in CI; it simply checks that every BIGSERIAL sequence is consistent with its table's max id after all migrations have run.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 addresses issues where Postgres sequences become desynchronized from their underlying table data due to legacy migrations that inserted rows with explicit IDs. By explicitly resetting these sequences and implementing a robust automated test to monitor sequence consistency in CI, the changes ensure database integrity for future migrations.

Highlights

  • Postgres Sequence Correction: Added a new migration (55) to reset BIGSERIAL sequences for 'universe_leaves', 'supply_commit_states', and 'supply_commit_update_types' to match their table's maximum ID, resolving desyncs caused by legacy migrations.
  • Regression Testing: Introduced a new Postgres-only unit test, 'TestSequenceConsistency', which verifies that all auto-increment sequences remain consistent with their table's maximum ID after migrations are applied.
  • SQLite Compatibility: Updated 'sqliteSchemaReplacements' to ensure the new Postgres-specific sequence reset commands are treated as no-ops when running on SQLite.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

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
Copy Markdown

@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 adds migration 55 to synchronize Postgres sequences that were desynchronized by previous migrations and introduces a new test to verify sequence consistency. Feedback includes a critical fix for the migration script to prevent failures on empty tables, a reminder to update SQLite replacements if the SQL changes, and suggestions to improve the test logic and SQL safety.

Comment thread tapdb/sqlc/migrations/000055_fix_universe_leaves_seq.up.sql Outdated
Comment thread tapdb/sqlite.go Outdated
Comment thread tapdb/migrations_test.go
@jtobin jtobin marked this pull request as draft April 2, 2026 08:44
@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 24383514050

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage decreased (-0.005%) to 34.278%

Details

  • Coverage decreased (-0.005%) from the base build.
  • Patch coverage: 5 uncovered changes across 1 file (0 of 5 lines covered, 0.0%).
  • 64 coverage regressions across 9 files.

Uncovered Changes

File Changed Covered %
cmd/merge-sql-schemas/main.go 5 0 0.0%

Coverage Regressions

64 previously-covered lines in 9 files lost coverage.

File Lines Losing Coverage Coverage
tappsbt/create.go 30 23.67%
tapgarden/caretaker.go 11 68.42%
lndservices/block_header_cache.go 7 94.41%
authmailbox/receive_subscription.go 4 74.84%
asset/mock.go 3 65.89%
authmailbox/server.go 3 75.8%
asset/asset.go 2 46.74%
tapchannel/aux_leaf_signer.go 2 43.18%
tapgarden/planter.go 2 63.32%

Coverage Stats

Coverage Status
Relevant Lines: 99976
Covered Lines: 34270
Line Coverage: 34.28%
Coverage Strength: 0.37 hits per line

💛 - Coveralls

jtobin added 4 commits April 15, 2026 11:18
Migration 31 copied universe_leaves rows with explicit ids, leaving
the BIGSERIAL sequence behind the actual data. New inserts then hit
duplicate-key errors.

Add migration 55 to reset the universe_leaves sequence via setval().
Also reset supply_commit_states and supply_commit_update_types
(migration 40 inserted explicit ids into these enum tables); these
are benign since no code path auto-increments into them, but fixing
them keeps sequence state consistent. On SQLite the statements are
replaced with no-ops.
Add TestSequenceConsistency, a Postgres-only test that verifies every
BIGSERIAL sequence is consistent with its table's max ID after all
migrations have run. This catches migrations that recreate tables and
copy rows with explicit IDs without advancing the sequence.
Move the Postgres-to-SQLite replacement map into
tapdb/sqlc/replacements.go so both tapdb/sqlite.go and
cmd/merge-sql-schemas share a single source of truth.
@jtobin jtobin marked this pull request as ready for review April 15, 2026 04:20
Copy link
Copy Markdown
Contributor

@darioAnongba darioAnongba left a comment

Choose a reason for hiding this comment

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

TBH I don't think we need a migration for this? Seems a bit over-engineered for a fix most likely no-one needs? Or do you think some users are suffering from this bug?

We can always keep the PR open and release in a patch if someone needs it someday

@jtobin
Copy link
Copy Markdown
Member Author

jtobin commented Apr 16, 2026

TBH I don't think we need a migration for this? Seems a bit over-engineered for a fix most likely no-one needs? Or do you think some users are suffering from this bug?

We can always keep the PR open and release in a patch if someone needs it someday

Yeah I think it's unlikely that there are other parties suffering from this bug, as it requires that they were 1) running prior to v0.6.0, and upgraded, 2) had substantial proof data, and 3) were running Postgres. But: anyone who does meet the criteria is probably fairly serious, and the bug means that their universe_leaves sequences are definitely jacked up, such that their InsertProof success rate is likely affected.

My intent with this is purely a just-in-case auto-fix, with the generic preventative test thrown in for good measure, but I think it's fine not to merge it. I definitely don't think we need it; if anyone needs the fix, we can just tell them to run the setval query manually, after all.

@darioAnongba
Copy link
Copy Markdown
Contributor

darioAnongba commented Apr 16, 2026

Yeah I think it's unlikely that there are other parties suffering from this bug, as it requires that they were 1) running prior to v0.6.0, and upgraded, 2) had substantial proof data, and 3) were running Postgres. But: anyone who does meet the criteria is probably fairly serious, and the bug means that their universe_leaves sequences are definitely jacked up, such that their InsertProof success rate is likely affected.

My intent with this is purely a just-in-case auto-fix, with the generic preventative test thrown in for good measure, but I think it's fine not to merge it. I definitely don't think we need it; if anyone needs the fix, we can just tell them to run the setval query manually, after all.

Agreed. Good to have a proper fix and ready to be added in any patch release. Thanks!

@jtobin jtobin removed this from the v0.8 milestone May 4, 2026
@lightninglabs-deploy
Copy link
Copy Markdown

@GeorgeTsagk: review reminder
@jtobin, remember to re-request review from reviewers when ready

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

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

4 participants