Use .execute to avoid any possibility of prepared statements (#5509) [deploy]
This commit is contained in:
parent
8f87f02978
commit
760d5ef325
1 changed files with 7 additions and 11 deletions
|
|
@ -4,24 +4,20 @@ class ApplicationRecord < ActiveRecord::Base
|
|||
QUERY_ESTIMATED_COUNT = <<~SQL.squish.freeze
|
||||
SELECT (
|
||||
(reltuples / GREATEST(relpages, 1)) *
|
||||
(pg_relation_size($1) / (GREATEST(current_setting('block_size')::integer, 1)))
|
||||
(pg_relation_size(?) / (GREATEST(current_setting('block_size')::integer, 1)))
|
||||
)::bigint AS count
|
||||
FROM pg_class WHERE relname = $2;
|
||||
FROM pg_class WHERE relname = ?;
|
||||
SQL
|
||||
|
||||
# Computes an estimated count of the number of rows using stats collected by VACUUM
|
||||
# inspired by <https://www.citusdata.com/blog/2016/10/12/count-performance/#dup_counts_estimated_full>
|
||||
# and <https://stackoverflow.com/a/48391562/4186181>
|
||||
def self.estimated_count
|
||||
query_name = "SQL COUNT ESTIMATE: #{table_name}"
|
||||
table_name_attr = ActiveRecord::Relation::QueryAttribute.new(
|
||||
"relname", table_name, ActiveRecord::Type::String.new
|
||||
)
|
||||
query = sanitize_sql_array([QUERY_ESTIMATED_COUNT, table_name, table_name])
|
||||
result = connection.execute(query)
|
||||
|
||||
result = connection.exec_query(
|
||||
QUERY_ESTIMATED_COUNT, query_name, [table_name_attr, table_name_attr]
|
||||
)
|
||||
|
||||
result.first["count"]
|
||||
count = result.first["count"]
|
||||
result.clear # PG::Result is manually managed in memory, we need to release its resources
|
||||
count
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue