@@ -340,28 +340,31 @@ impl<'conn> SynapseReader<'conn> {
340
340
///
341
341
/// - An underlying database error
342
342
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.
343
348
let users: usize = sqlx:: query_scalar :: < _ , i64 > (
344
349
"
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;
347
351
" ,
348
352
)
349
353
. fetch_one ( & mut * self . txn )
350
354
. await
351
- . into_database ( "counting Synapse users" ) ?
355
+ . into_database ( "estimating count of users" ) ?
352
356
. max ( 0 )
353
357
. try_into ( )
354
358
. unwrap_or ( usize:: MAX ) ;
355
359
356
360
let devices = sqlx:: query_scalar :: < _ , i64 > (
357
361
"
358
- SELECT COUNT(1) FROM devices
359
- WHERE NOT hidden
362
+ SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'devices'::regclass;
360
363
" ,
361
364
)
362
365
. fetch_one ( & mut * self . txn )
363
366
. await
364
- . into_database ( "counting Synapse devices" ) ?
367
+ . into_database ( "estimating count of devices" ) ?
365
368
. max ( 0 )
366
369
. try_into ( )
367
370
. unwrap_or ( usize:: MAX ) ;
0 commit comments