Skip to content

fix(catalog): point the rank view at the live copy after a logical restore and before a rebuild drops the idle one - #633

Open
oleedv wants to merge 1 commit into
braedonsaunders:mainfrom
oleedv:fix/rank-view-restore
Open

oleedv wants to merge 1 commit into
braedonsaunders:mainfrom
oleedv:fix/rank-view-restore

Conversation

@oleedv

@oleedv oleedv commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Problem

The popularity rank has two copies, catalog_rank_a and catalog_rank_b. catalog_meta.rank_side names the live one (the one the triggers maintain), and the view catalog_rank reads it. Each daily rebuild fills the idle copy, swaps the view and rank_side together, and empties the retired copy.

A logical restore (store.ImportLogical: a backup restore, or ether cluster init copying a SQLite server into PostgreSQL) copies base tables only. The target keeps the view its migrations created, on catalog_rank_a, while the restored catalog_meta may say rank_side=b. That happens whenever the source's last rebuild left b live, so about half the time.

  • Everything that reads catalog_rank (popularity pages, search walks, topic rows) reads the retired copy, which is empty.
  • The next rebuild treats a as idle and runs DROP TABLE catalog_rank_a. PostgreSQL refuses: cannot drop table catalog_rank_a because other objects depend on it (SQLSTATE 2BP01). Every later rebuild fails the same way, so the rank never recovers. SQLite allows the drop, so there the next rebuild repairs it, a day later.

Seen on a server moved from SQLite to PostgreSQL with ether cluster init: the copy ran 32 minutes after the last SQLite rebuild had made b live. From the next daily rebuild on, the PostgreSQL log shows the DROP failure every hour. catalog_rank_a held 0 rows, catalog_rank_b held 154,082, and the view read catalog_rank_a.

Fix

  • A restore hook (catalog_rank_view) points the view at the copy the restored rank_side names, inside the restore transaction. DROP VIEW IF EXISTS plus CREATE VIEW works on both dialects.
  • rebuildRank checks the view before it touches the idle copy. If the view reads the idle copy, it points the view back at the live one (swapRankViewPG on PostgreSQL) and logs a warning. Servers that were already restored into this state recover at their next rebuild without manual SQL.

Tests

Both tests fail without the change, on SQLite and on PostgreSQL (ETHER_TEST_POSTGRES):

  • TestLogicalRestoreKeepsTheRankViewOnTheLiveCopy: a catalogue whose live copy is b, exported and imported. Before the change, catalog_rank read 0 of 25 rows after the restore. The next rebuild must also succeed.
  • TestRankRebuildRepairsAViewOnTheIdleCopy: a view left on the retired copy. Before the change, the PostgreSQL rebuild failed with the same 2BP01 error as the field server.

gofmt, go vet and go test ./internal/catalog/ ./internal/store/ ./internal/app/ ./internal/backup/ ./internal/migrate/ ./internal/clusterconfig/ pass on SQLite and PostgreSQL 18, Go 1.27.4.

…store and before a rebuild drops the idle one

@braedonsaunders braedonsaunders left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks @oleedv, this is an excellent catch, and the write-up made it quick to verify: the restore copies base tables but not the view, so it ends up reading the retired copy about half the time, and PostgreSQL then refuses the DROP (2BP01) on every rebuild after. Both halves of the fix are right:

  • The restore hook points the view at rank_side inside the restore transaction.
  • The rebuild repairs a database that was restored before this fix, before it drops the idle copy. PostgreSQL goes through swapRankViewPG; SQLite drops and recreates the view.

I merged it into the 0.3.17 branch, and the catalog suite passes. It ships with v0.3.17; this PR will show as merged once that reaches main.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants