summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2023-05-03 14:42:43 +0100
committerGitHub <noreply@github.com>2023-05-03 13:42:43 +0000
commit28ac1a1a91c972c19649e21a6e8d92bb786d8a57 (patch)
treee426fa952df06f82aa09119f64879d6bf1c54b66
parentSpeed up rebuilding of the user directory for local users (#15529) (diff)
downloadsynapse-28ac1a1a91c972c19649e21a6e8d92bb786d8a57.tar.xz
Speed up deleting of old rows in `event_push_actions` (#15531)
Enforce that we use index scans (rather than seq scans), which we also do for state queries. The reason to enforce this is that we can't correctly get PostgreSQL to understand the distribution of `stream_ordering` depends on `highlight`, and so it always defaults (on matrix.org) to sequential scans. 
-rw-r--r--changelog.d/15531.misc1
-rw-r--r--synapse/storage/databases/main/event_push_actions.py9
2 files changed, 10 insertions, 0 deletions
diff --git a/changelog.d/15531.misc b/changelog.d/15531.misc
new file mode 100644
index 0000000000..6d4da961b5
--- /dev/null
+++ b/changelog.d/15531.misc
@@ -0,0 +1 @@
+Speed up deleting of old rows in `event_push_actions`.
diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py
index ab8f354dc1..2e98a29fef 100644
--- a/synapse/storage/databases/main/event_push_actions.py
+++ b/synapse/storage/databases/main/event_push_actions.py
@@ -1612,6 +1612,15 @@ class EventPushActionsWorkerStore(ReceiptsWorkerStore, StreamWorkerStore, SQLBas
             # deletes.
             batch_size = self._rotate_count
 
+            if isinstance(self.database_engine, PostgresEngine):
+                # Temporarily disable sequential scans in this transaction. We
+                # need to do this as the postgres statistics don't take into
+                # account the `highlight = 0` part when estimating the
+                # distribution of `stream_ordering`. I.e. since we keep old
+                # highlight rows the query planner thinks there are way more old
+                # rows to delete than there actually are.
+                txn.execute("SET LOCAL enable_seqscan=off")
+
             txn.execute(
                 """
                 SELECT stream_ordering FROM event_push_actions