diff --git a/synapse/storage/state.py b/synapse/storage/state.py
index 589a4fec6e..af3ddd962d 100644
--- a/synapse/storage/state.py
+++ b/synapse/storage/state.py
@@ -674,6 +674,17 @@ class StateStore(SQLBaseStore):
return True, count
txn.execute(
+ "SELECT state_group FROM state_group_edges"
+ " WHERE state_group = ?",
+ (state_group,)
+ )
+
+ # If we reach a point where we've already started inserting
+ # edges we should stop.
+ if txn.fetchall():
+ return True, count
+
+ txn.execute(
"SELECT coalesce(max(id), 0) FROM state_groups"
" WHERE id < ? AND room_id = ?",
(state_group, room_id,)
@@ -709,6 +720,14 @@ class StateStore(SQLBaseStore):
if prev_state.get(key, None) != value
}
+ self._simple_delete_txn(
+ txn,
+ table="state_group_edges",
+ keyvalues={
+ "state_group": state_group,
+ }
+ )
+
self._simple_insert_txn(
txn,
table="state_group_edges",
|