Filter only ordinary tables in estimated count (#8913)

* Revert "[deploy] Use max_by instead to get the highest value (#8890)"

This reverts commit b0fb0a484d.

* Only count actual ordinary tables
This commit is contained in:
rhymes 2020-06-25 20:04:13 +02:00 committed by GitHub
parent db4e37781f
commit 80f648c837
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,12 +3,15 @@ class ApplicationRecord < ActiveRecord::Base
include Purgeable
# see <https://www.postgresql.org/docs/11/catalog-pg-class.html> 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