Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Speed up purging rooms #3381

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion roomserver/storage/postgres/purge_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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},
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion roomserver/storage/sqlite3/purge_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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},
Expand Down Expand Up @@ -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,
Expand Down
Loading