summary refs log tree commit diff
path: root/synapse/storage/state.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-09-14 10:03:48 +0100
committerErik Johnston <erik@matrix.org>2016-09-14 10:03:48 +0100
commitd5ae1f129143d6436a238fd7882e39168f944846 (patch)
tree495ff980ee00b730b46a29cc5a0608f4d8734250 /synapse/storage/state.py
parentMerge pull request #1111 from matrix-org/matthew/device-ids (diff)
downloadsynapse-d5ae1f129143d6436a238fd7882e39168f944846.tar.xz
Ensure we don't mutate state cache entries
Diffstat (limited to 'synapse/storage/state.py')
-rw-r--r--synapse/storage/state.py45
1 files changed, 25 insertions, 20 deletions
diff --git a/synapse/storage/state.py b/synapse/storage/state.py
index fdbdade536..ec8c62b653 100644
--- a/synapse/storage/state.py
+++ b/synapse/storage/state.py
@@ -817,27 +817,32 @@ class StateStore(SQLBaseStore):
 
     @defer.inlineCallbacks
     def _background_index_state(self, progress, batch_size):
-        def reindex_txn(txn):
-            if isinstance(self.database_engine, PostgresEngine):
-                txn.execute(
-                    "CREATE INDEX CONCURRENTLY state_groups_state_type_idx"
-                    " ON state_groups_state(state_group, type, state_key)"
-                )
-                txn.execute(
-                    "DROP INDEX IF EXISTS state_groups_state_id"
-                )
-            else:
-                txn.execute(
-                    "CREATE INDEX state_groups_state_type_idx"
-                    " ON state_groups_state(state_group, type, state_key)"
-                )
-                txn.execute(
-                    "DROP INDEX IF EXISTS state_groups_state_id"
-                )
+        def reindex_txn(conn):
+            conn.rollback()
+            # postgres insists on autocommit for the index
+            conn.set_session(autocommit=True)
+            try:
+                txn = conn.cursor()
+                if isinstance(self.database_engine, PostgresEngine):
+                    txn.execute(
+                        "CREATE INDEX CONCURRENTLY state_groups_state_type_idx"
+                        " ON state_groups_state(state_group, type, state_key)"
+                    )
+                    txn.execute(
+                        "DROP INDEX IF EXISTS state_groups_state_id"
+                    )
+                else:
+                    txn.execute(
+                        "CREATE INDEX state_groups_state_type_idx"
+                        " ON state_groups_state(state_group, type, state_key)"
+                    )
+                    txn.execute(
+                        "DROP INDEX IF EXISTS state_groups_state_id"
+                    )
+            finally:
+                conn.set_session(autocommit=False)
 
-        yield self.runInteraction(
-            self.STATE_GROUP_INDEX_UPDATE_NAME, reindex_txn
-        )
+        yield self.runWithConnection(reindex_txn)
 
         yield self._end_background_update(self.STATE_GROUP_INDEX_UPDATE_NAME)