diff --git a/state/event_table.go b/state/event_table.go index bc7e59e..33c5c6c 100644 --- a/state/event_table.go +++ b/state/event_table.go @@ -141,11 +141,6 @@ func NewEventTable(db *sqlx.DB) *EventTable { CREATE INDEX IF NOT EXISTS syncv3_nid_room_state_idx ON syncv3_events(room_id, event_nid, is_state); CREATE UNIQUE INDEX IF NOT EXISTS syncv3_events_room_event_nid_type_skey_idx ON syncv3_events(event_nid, event_type, state_key); - - -- Create a materialized view for event_types (used on startup to get the latest events in each room) - CREATE MATERIALIZED VIEW IF NOT EXISTS event_types as - SELECT DISTINCT event_type - FROM syncv3_events; `) return &EventTable{db} } @@ -447,13 +442,20 @@ func (t *EventTable) SelectLatestEventsBetween(txn *sqlx.Tx, roomID string, lowe func (t *EventTable) selectLatestEventByTypeInAllRooms(txn *sqlx.Tx) ([]Event, error) { result := []Event{} // What the following query does: - // 1. Gets all event types from a materialized view (updated on startup in `PrepareSnapshot`) as the `event_types` CTE + // 1. Gets all event types from a recursive CTE as the `event_types` CTE // 2. Gets all rooms as the `room_ids` CTE // 3. Gets the latest event_nid for each event_type and room as the `max_by_ev_type` CTE // 4. Queries the required data using the event_nids provided by the `max_by_ev_type` CTE rows, err := txn.Query(` WITH event_types AS ( - SELECT * FROM event_types + WITH RECURSIVE t AS ( + (SELECT event_type FROM syncv3_events ORDER BY event_type LIMIT 1) -- parentheses required + UNION ALL + SELECT (SELECT event_type FROM syncv3_events WHERE event_type > t.event_type ORDER BY event_type LIMIT 1) + FROM t + WHERE t.event_type IS NOT NULL + ) + SELECT event_type FROM t WHERE event_type IS NOT NULL ), room_ids AS ( SELECT DISTINCT room_id FROM syncv3_rooms ), max_by_ev_type AS ( diff --git a/state/storage.go b/state/storage.go index 148f76a..d8cb2e1 100644 --- a/state/storage.go +++ b/state/storage.go @@ -171,15 +171,6 @@ func (s *Storage) PrepareSnapshot(txn *sqlx.Tx) (tableName string, err error) { `SELECT UNNEST(membership_events) AS membership_nid INTO TEMP ` + tempTableName + ` FROM syncv3_snapshots JOIN syncv3_rooms ON syncv3_snapshots.snapshot_id = syncv3_rooms.current_snapshot_id`, ) - if err != nil { - return "", err - } - // Refresh the materialized view, so getting latest events by type per room - // can use a fresh view of the event_types. - _, err = txn.Exec("REFRESH MATERIALIZED VIEW event_types;") - if err != nil { - return "", err - } return tempTableName, nil }