From 80f648c8379436caef66059e1ffb2fb80eafcbbe Mon Sep 17 00:00:00 2001 From: rhymes Date: Thu, 25 Jun 2020 20:04:13 +0200 Subject: [PATCH] Filter only ordinary tables in estimated count (#8913) * Revert "[deploy] Use max_by instead to get the highest value (#8890)" This reverts commit b0fb0a484dc36f26e4f726e0c73334a741a03b04. * Only count actual ordinary tables --- app/models/application_record.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index bfdad82f9..fb38ec9a9 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -3,12 +3,15 @@ class ApplicationRecord < ActiveRecord::Base include Purgeable + # see for details + # on the `pg_class` table QUERY_ESTIMATED_COUNT = <<~SQL.squish.freeze SELECT ( (reltuples / GREATEST(relpages, 1)) * (pg_relation_size(?) / (GREATEST(current_setting('block_size')::integer, 1))) )::bigint AS count - FROM pg_class WHERE relname = ?; + FROM pg_class + WHERE relname = ? AND relkind = 'r'; SQL # Computes an estimated count of the number of rows using stats collected by VACUUM @@ -18,7 +21,7 @@ class ApplicationRecord < ActiveRecord::Base query = sanitize_sql_array([QUERY_ESTIMATED_COUNT, table_name, table_name]) result = connection.execute(query) - count = result.max_by(&:values)["count"] # sometimes is an array of hashes, for ex. [{"count" => 0}, [{"count" => 1000}] + count = result.first["count"] result.clear # PG::Result is manually managed in memory, we need to release its resources count end