summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorreivilibre <oliverw@matrix.org>2023-03-24 15:31:12 +0000
committerGitHub <noreply@github.com>2023-03-24 15:31:12 +0000
commit5f7c9082805846cc07bfef2d48c6f6cfc9f723e9 (patch)
tree78dae0204444dc26f361418675f09c9fafcf3184 /synapse
parentMake cleaning up pushers depend on the device_id instead of the token_id (#15... (diff)
downloadsynapse-5f7c9082805846cc07bfef2d48c6f6cfc9f723e9.tar.xz
As an optimisation, use `TRUNCATE` on Postgres when clearing the user directory tables. (#15316)
Diffstat (limited to 'synapse')
-rw-r--r--synapse/storage/databases/main/user_directory.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/synapse/storage/databases/main/user_directory.py b/synapse/storage/databases/main/user_directory.py
index 97f09b73dd..9fced4b997 100644
--- a/synapse/storage/databases/main/user_directory.py
+++ b/synapse/storage/databases/main/user_directory.py
@@ -698,10 +698,17 @@ class UserDirectoryBackgroundUpdateStore(StateDeltasStore):
         """Delete the entire user directory"""
 
         def _delete_all_from_user_dir_txn(txn: LoggingTransaction) -> None:
-            txn.execute("DELETE FROM user_directory")
-            txn.execute("DELETE FROM user_directory_search")
-            txn.execute("DELETE FROM users_in_public_rooms")
-            txn.execute("DELETE FROM users_who_share_private_rooms")
+            # SQLite doesn't support TRUNCATE.
+            # On Postgres, DELETE FROM does a table scan but TRUNCATE is more efficient.
+            truncate = (
+                "DELETE FROM"
+                if isinstance(self.database_engine, Sqlite3Engine)
+                else "TRUNCATE"
+            )
+            txn.execute(f"{truncate} user_directory")
+            txn.execute(f"{truncate} user_directory_search")
+            txn.execute(f"{truncate} users_in_public_rooms")
+            txn.execute(f"{truncate} users_who_share_private_rooms")
             txn.call_after(self.get_user_in_directory.invalidate_all)
 
         await self.db_pool.runInteraction(