aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2024-09-10 20:43:50 +0200
committerGitHub <noreply@github.com>2024-09-10 20:43:50 +0200
commit3a2eadcc3690204fba3c858c43977b50068e77bb (patch)
tree02b1d4ea80a5aaa8b81bd8158764372038aad544
parent7bbec19a6a792b7b3fb571853e4e2fb009e02c4c (diff)
Speed up purging rooms (#3381)
[skip CI]
-rw-r--r--roomserver/storage/postgres/purge_statements.go10
-rw-r--r--roomserver/storage/sqlite3/purge_statements.go10
2 files changed, 18 insertions, 2 deletions
diff --git a/roomserver/storage/postgres/purge_statements.go b/roomserver/storage/postgres/purge_statements.go
index efba439b..9ca8d034 100644
--- a/roomserver/storage/postgres/purge_statements.go
+++ b/roomserver/storage/postgres/purge_statements.go
@@ -41,6 +41,11 @@ const purgePreviousEventsSQL = "" +
" SELECT ARRAY_AGG(event_nid) FROM roomserver_events WHERE room_nid = $1" +
")"
+// This removes the majority of prev events and is way faster than the above.
+// The above query is still needed to delete the remaining prev events.
+const purgePreviousEvents2SQL = "" +
+ "DELETE FROM roomserver_previous_events rpe WHERE EXISTS(SELECT event_id FROM roomserver_events re WHERE room_nid = $1 AND re.event_id = rpe.previous_event_id)"
+
const purgePublishedSQL = "" +
"DELETE FROM roomserver_published WHERE room_id = $1"
@@ -69,6 +74,7 @@ type purgeStatements struct {
purgeInvitesStmt *sql.Stmt
purgeMembershipsStmt *sql.Stmt
purgePreviousEventsStmt *sql.Stmt
+ purgePreviousEvents2Stmt *sql.Stmt
purgePublishedStmt *sql.Stmt
purgeRedactionStmt *sql.Stmt
purgeRoomAliasesStmt *sql.Stmt
@@ -87,6 +93,7 @@ func PreparePurgeStatements(db *sql.DB) (*purgeStatements, error) {
{&s.purgeMembershipsStmt, purgeMembershipsSQL},
{&s.purgePublishedStmt, purgePublishedSQL},
{&s.purgePreviousEventsStmt, purgePreviousEventsSQL},
+ {&s.purgePreviousEvents2Stmt, purgePreviousEvents2SQL},
{&s.purgeRedactionStmt, purgeRedactionsSQL},
{&s.purgeRoomAliasesStmt, purgeRoomAliasesSQL},
{&s.purgeRoomStmt, purgeRoomSQL},
@@ -117,7 +124,8 @@ func (s *purgeStatements) PurgeRoom(
s.purgeStateSnapshotEntriesStmt,
s.purgeInvitesStmt,
s.purgeMembershipsStmt,
- s.purgePreviousEventsStmt,
+ s.purgePreviousEvents2Stmt, // Fast purge the majority of events
+ s.purgePreviousEventsStmt, // Slow purge the remaining events
s.purgeEventJSONStmt,
s.purgeRedactionStmt,
s.purgeEventsStmt,
diff --git a/roomserver/storage/sqlite3/purge_statements.go b/roomserver/storage/sqlite3/purge_statements.go
index c7b4d27a..cb21515b 100644
--- a/roomserver/storage/sqlite3/purge_statements.go
+++ b/roomserver/storage/sqlite3/purge_statements.go
@@ -41,6 +41,11 @@ const purgePreviousEventsSQL = "" +
" SELECT event_nid FROM roomserver_events WHERE room_nid = $1" +
")"
+// This removes the majority of prev events and is way faster than the above.
+// The above query is still needed to delete the remaining prev events.
+const purgePreviousEvents2SQL = "" +
+ "DELETE FROM roomserver_previous_events AS rpe WHERE EXISTS(SELECT event_id FROM roomserver_events AS re WHERE room_nid = $1 AND re.event_id = rpe.previous_event_id)"
+
const purgePublishedSQL = "" +
"DELETE FROM roomserver_published WHERE room_id = $1"
@@ -64,6 +69,7 @@ type purgeStatements struct {
purgeInvitesStmt *sql.Stmt
purgeMembershipsStmt *sql.Stmt
purgePreviousEventsStmt *sql.Stmt
+ purgePreviousEvents2Stmt *sql.Stmt
purgePublishedStmt *sql.Stmt
purgeRedactionStmt *sql.Stmt
purgeRoomAliasesStmt *sql.Stmt
@@ -81,6 +87,7 @@ func PreparePurgeStatements(db *sql.DB, stateSnapshot *stateSnapshotStatements)
{&s.purgeMembershipsStmt, purgeMembershipsSQL},
{&s.purgePublishedStmt, purgePublishedSQL},
{&s.purgePreviousEventsStmt, purgePreviousEventsSQL},
+ {&s.purgePreviousEvents2Stmt, purgePreviousEvents2SQL},
{&s.purgeRedactionStmt, purgeRedactionsSQL},
{&s.purgeRoomAliasesStmt, purgeRoomAliasesSQL},
{&s.purgeRoomStmt, purgeRoomSQL},
@@ -114,7 +121,8 @@ func (s *purgeStatements) PurgeRoom(
s.purgeStateSnapshotEntriesStmt,
s.purgeInvitesStmt,
s.purgeMembershipsStmt,
- s.purgePreviousEventsStmt,
+ s.purgePreviousEvents2Stmt, // Fast purge the majority of events
+ s.purgePreviousEventsStmt, // Slow purge the remaining events
s.purgeEventJSONStmt,
s.purgeRedactionStmt,
s.purgeEventsStmt,