From 1fdb48ef02d9b102e4fd4b2d5e15b7952a5d52c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 9 Dec 2024 16:50:57 +0100 Subject: [PATCH] count metrics faster MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/gold-crabs-agree.md | 5 +++ .../catalog-backend/src/database/metrics.ts | 36 ++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .changeset/gold-crabs-agree.md diff --git a/.changeset/gold-crabs-agree.md b/.changeset/gold-crabs-agree.md new file mode 100644 index 0000000000..1f345a8c10 --- /dev/null +++ b/.changeset/gold-crabs-agree.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Use a faster count method on pg when computing some metrics diff --git a/plugins/catalog-backend/src/database/metrics.ts b/plugins/catalog-backend/src/database/metrics.ts index d8cd87558f..809405d69f 100644 --- a/plugins/catalog-backend/src/database/metrics.ts +++ b/plugins/catalog-backend/src/database/metrics.ts @@ -98,20 +98,40 @@ export function initDatabaseMetrics(knex: Knex) { description: 'Total amount of registered locations in the catalog', }) .addCallback(async gauge => { - const total = await knex('locations').count({ - count: '*', - }); - gauge.observe(Number(total[0].count)); + if (knex.client.config.client === 'pg') { + // https://stackoverflow.com/questions/7943233/fast-way-to-discover-the-row-count-of-a-table-in-postgresql + const total = await knex.raw(` + SELECT reltuples::bigint AS estimate + FROM pg_class + WHERE oid = 'locations'::regclass; + `); + gauge.observe(Number(total.rows[0].estimate)); + } else { + const total = await knex('locations').count({ + count: '*', + }); + gauge.observe(Number(total[0].count)); + } }), relations: meter .createObservableGauge('catalog_relations_count', { description: 'Total amount of relations between entities', }) .addCallback(async gauge => { - const total = await knex('relations').count({ - count: '*', - }); - gauge.observe(Number(total[0].count)); + if (knex.client.config.client === 'pg') { + // https://stackoverflow.com/questions/7943233/fast-way-to-discover-the-row-count-of-a-table-in-postgresql + const total = await knex.raw(` + SELECT reltuples::bigint AS estimate + FROM pg_class + WHERE oid = 'relations'::regclass; + `); + gauge.observe(Number(total.rows[0].estimate)); + } else { + const total = await knex('relations').count({ + count: '*', + }); + gauge.observe(Number(total[0].count)); + } }), }; }