Speed up purging rooms

This commit is contained in:
Till Faelligen 2024-05-31 19:23:31 +02:00
parent 46902e5766
commit b56ad8399d
No known key found for this signature in database
GPG key ID: 3DF82D8AB9211D4E
2 changed files with 18 additions and 2 deletions

View file

@ -41,6 +41,11 @@ const purgePreviousEventsSQL = "" +
" SELECT ARRAY_AGG(event_nid) FROM roomserver_events WHERE room_nid = $1" + " 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 = "" + const purgePublishedSQL = "" +
"DELETE FROM roomserver_published WHERE room_id = $1" "DELETE FROM roomserver_published WHERE room_id = $1"
@ -69,6 +74,7 @@ type purgeStatements struct {
purgeInvitesStmt *sql.Stmt purgeInvitesStmt *sql.Stmt
purgeMembershipsStmt *sql.Stmt purgeMembershipsStmt *sql.Stmt
purgePreviousEventsStmt *sql.Stmt purgePreviousEventsStmt *sql.Stmt
purgePreviousEvents2Stmt *sql.Stmt
purgePublishedStmt *sql.Stmt purgePublishedStmt *sql.Stmt
purgeRedactionStmt *sql.Stmt purgeRedactionStmt *sql.Stmt
purgeRoomAliasesStmt *sql.Stmt purgeRoomAliasesStmt *sql.Stmt
@ -87,6 +93,7 @@ func PreparePurgeStatements(db *sql.DB) (*purgeStatements, error) {
{&s.purgeMembershipsStmt, purgeMembershipsSQL}, {&s.purgeMembershipsStmt, purgeMembershipsSQL},
{&s.purgePublishedStmt, purgePublishedSQL}, {&s.purgePublishedStmt, purgePublishedSQL},
{&s.purgePreviousEventsStmt, purgePreviousEventsSQL}, {&s.purgePreviousEventsStmt, purgePreviousEventsSQL},
{&s.purgePreviousEvents2Stmt, purgePreviousEvents2SQL},
{&s.purgeRedactionStmt, purgeRedactionsSQL}, {&s.purgeRedactionStmt, purgeRedactionsSQL},
{&s.purgeRoomAliasesStmt, purgeRoomAliasesSQL}, {&s.purgeRoomAliasesStmt, purgeRoomAliasesSQL},
{&s.purgeRoomStmt, purgeRoomSQL}, {&s.purgeRoomStmt, purgeRoomSQL},
@ -117,7 +124,8 @@ func (s *purgeStatements) PurgeRoom(
s.purgeStateSnapshotEntriesStmt, s.purgeStateSnapshotEntriesStmt,
s.purgeInvitesStmt, s.purgeInvitesStmt,
s.purgeMembershipsStmt, s.purgeMembershipsStmt,
s.purgePreviousEventsStmt, s.purgePreviousEvents2Stmt, // Fast purge the majority of events
s.purgePreviousEventsStmt, // Slow purge the remaining events
s.purgeEventJSONStmt, s.purgeEventJSONStmt,
s.purgeRedactionStmt, s.purgeRedactionStmt,
s.purgeEventsStmt, s.purgeEventsStmt,

View file

@ -41,6 +41,11 @@ const purgePreviousEventsSQL = "" +
" SELECT event_nid FROM roomserver_events WHERE room_nid = $1" + " 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 = "" + const purgePublishedSQL = "" +
"DELETE FROM roomserver_published WHERE room_id = $1" "DELETE FROM roomserver_published WHERE room_id = $1"
@ -64,6 +69,7 @@ type purgeStatements struct {
purgeInvitesStmt *sql.Stmt purgeInvitesStmt *sql.Stmt
purgeMembershipsStmt *sql.Stmt purgeMembershipsStmt *sql.Stmt
purgePreviousEventsStmt *sql.Stmt purgePreviousEventsStmt *sql.Stmt
purgePreviousEvents2Stmt *sql.Stmt
purgePublishedStmt *sql.Stmt purgePublishedStmt *sql.Stmt
purgeRedactionStmt *sql.Stmt purgeRedactionStmt *sql.Stmt
purgeRoomAliasesStmt *sql.Stmt purgeRoomAliasesStmt *sql.Stmt
@ -81,6 +87,7 @@ func PreparePurgeStatements(db *sql.DB, stateSnapshot *stateSnapshotStatements)
{&s.purgeMembershipsStmt, purgeMembershipsSQL}, {&s.purgeMembershipsStmt, purgeMembershipsSQL},
{&s.purgePublishedStmt, purgePublishedSQL}, {&s.purgePublishedStmt, purgePublishedSQL},
{&s.purgePreviousEventsStmt, purgePreviousEventsSQL}, {&s.purgePreviousEventsStmt, purgePreviousEventsSQL},
{&s.purgePreviousEvents2Stmt, purgePreviousEvents2SQL},
{&s.purgeRedactionStmt, purgeRedactionsSQL}, {&s.purgeRedactionStmt, purgeRedactionsSQL},
{&s.purgeRoomAliasesStmt, purgeRoomAliasesSQL}, {&s.purgeRoomAliasesStmt, purgeRoomAliasesSQL},
{&s.purgeRoomStmt, purgeRoomSQL}, {&s.purgeRoomStmt, purgeRoomSQL},
@ -114,7 +121,8 @@ func (s *purgeStatements) PurgeRoom(
s.purgeStateSnapshotEntriesStmt, s.purgeStateSnapshotEntriesStmt,
s.purgeInvitesStmt, s.purgeInvitesStmt,
s.purgeMembershipsStmt, s.purgeMembershipsStmt,
s.purgePreviousEventsStmt, s.purgePreviousEvents2Stmt, // Fast purge the majority of events
s.purgePreviousEventsStmt, // Slow purge the remaining events
s.purgeEventJSONStmt, s.purgeEventJSONStmt,
s.purgeRedactionStmt, s.purgeRedactionStmt,
s.purgeEventsStmt, s.purgeEventsStmt,