Skip to content

Commit c71ea15

Browse files
reivilibresandhose
authored andcommitted
For performance, switch to a row count estimate for users and devices
1 parent 8d738ed commit c71ea15

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

crates/syn2mas/src/migration.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ pub async fn migrate(
155155

156156
let state = MigrationState {
157157
server_name,
158-
users: HashMap::with_capacity(counts.users),
159-
devices_to_compat_sessions: HashMap::with_capacity(counts.devices),
158+
// We oversize the hashmaps, as the estimates are innaccurate, and we would like to avoid
159+
// reallocations.
160+
users: HashMap::with_capacity(counts.users * 9 / 8),
161+
devices_to_compat_sessions: HashMap::with_capacity(counts.devices * 9 / 8),
160162
provider_id_mapping,
161163
};
162164

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,28 +340,31 @@ impl<'conn> SynapseReader<'conn> {
340340
///
341341
/// - An underlying database error
342342
pub async fn count_rows(&mut self) -> Result<SynapseRowCounts, Error> {
343+
// We don't get to filter out application service users by using this estimate,
344+
// which is a shame, but on a large database this is way faster.
345+
// On matrix.org, counting users and devices properly takes around 1m10s,
346+
// which is unnecessary extra downtime during the migration, just to
347+
// show a more accurate progress bar and size a hash map accurately.
343348
let users: usize = sqlx::query_scalar::<_, i64>(
344349
"
345-
SELECT COUNT(1) FROM users
346-
WHERE appservice_id IS NULL
350+
SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'users'::regclass;
347351
",
348352
)
349353
.fetch_one(&mut *self.txn)
350354
.await
351-
.into_database("counting Synapse users")?
355+
.into_database("estimating count of users")?
352356
.max(0)
353357
.try_into()
354358
.unwrap_or(usize::MAX);
355359

356360
let devices = sqlx::query_scalar::<_, i64>(
357361
"
358-
SELECT COUNT(1) FROM devices
359-
WHERE NOT hidden
362+
SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'devices'::regclass;
360363
",
361364
)
362365
.fetch_one(&mut *self.txn)
363366
.await
364-
.into_database("counting Synapse devices")?
367+
.into_database("estimating count of devices")?
365368
.max(0)
366369
.try_into()
367370
.unwrap_or(usize::MAX);

0 commit comments

Comments
 (0)