Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix port script fails when DB has no backfilled events. #8729

Merged
merged 3 commits into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8729.bugfix
@@ -0,0 +1 @@
Fix port script fails when DB has no backfilled events. Broke in v1.21.0.
12 changes: 5 additions & 7 deletions scripts/synapse_port_db
Expand Up @@ -820,14 +820,12 @@ class Porter(object):
"ALTER SEQUENCE events_stream_seq RESTART WITH %s", (next_id,)
)

txn.execute("SELECT -MIN(stream_ordering) FROM events")
txn.execute("SELECT GREATEST(-MIN(stream_ordering), 1) FROM events")
Copy link
Member

Choose a reason for hiding this comment

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

I assume this does fix things, but what is the purpose of negating the minimum stream_ordering here? And wouldn't using GREATEST and - here mean that we almost always end up with 1 as it's greater?

Copy link
Member Author

Choose a reason for hiding this comment

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

stream_ordering is negative for backfilled events, so if e.g. the min is -52, then GREATEST(- -52 1) is 52, while if there are no backfilled events we may end up with a positive min so we get GREATEST(-1, 1) which is 1.

curr_id = txn.fetchone()[0]
if curr_id:
next_id = curr_id + 1
txn.execute(
"ALTER SEQUENCE events_backfill_stream_seq RESTART WITH %s",
(next_id,),
)
next_id = curr_id + 1
Copy link
Member

Choose a reason for hiding this comment

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

sooo.. if there are no backfilled events, the SELECT will return 1, so we'll set the RESTART value to 2. I mean, it's not the end of the world, but it doesn't sound right.

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean, it really doesn't matter? I can change it if you want, but we do the exact same thing from a fresh DB too

Copy link
Member

Choose a reason for hiding this comment

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

we do? fair enough. Thought you might change the GREATEST to 0. I don't feel strongly.

txn.execute(
"ALTER SEQUENCE events_backfill_stream_seq RESTART WITH %s", (next_id,),
)

return self.postgres_store.db_pool.runInteraction(
"_setup_events_stream_seqs", r
Expand Down