@@ -338,24 +338,28 @@ impl<'conn> SynapseReader<'conn> {
338338 ///
339339 /// - An underlying database error
340340 pub async fn count_rows ( & mut self ) -> Result < SynapseRowCounts , Error > {
341+ // We don't get to filter out application service users by using this estimate,
342+ // which is a shame, but on a large database this is way faster.
343+ // On matrix.org, counting users and devices properly takes around 1m10s,
344+ // which is unnecessary extra downtime during the migration, just to
345+ // show a more accurate progress bar and size a hash map accurately.
341346 let users: i64 = sqlx:: query_scalar (
342347 "
343- SELECT COUNT(1) FROM users
344- WHERE appservice_id IS NULL
348+ SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'users'::regclass;
345349 " ,
346350 )
347351 . fetch_one ( & mut * self . txn )
348352 . await
349- . into_database ( "counting Synapse users" ) ?;
353+ . into_database ( "estimating count of users" ) ?;
350354
351355 let devices = sqlx:: query_scalar (
352356 "
353- SELECT COUNT(1) FROM devices
357+ SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = ' devices'::regclass;
354358 " ,
355359 )
356360 . fetch_one ( & mut * self . txn )
357361 . await
358- . into_database ( "counting Synapse devices" ) ?;
362+ . into_database ( "estimating count of devices" ) ?;
359363
360364 // For other rows, we don't particularly care about the number except for
361365 // progress bars, so retrieve a fast estimate from the statistics system
0 commit comments