PERF: Switch ActiveRecord PG connection active check to use empty query. (#13323)

See https://github.com/rails/rails/pull/42368

The impact is not quantifiable at the time of this writing but
prelimary investigation shows that `SELECT 1` accounts for 0.09 of CPU
time on a database. Note that Discourse runs thousands of databases so
the small impact may be amplified by the large number of databases that
we run.
This commit is contained in:
Alan Guo Xiang Tan 2021-06-08 11:15:07 +08:00 committed by GitHub
parent 0815b4cc2e
commit 19a92fbadc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
# Pulls in https://github.com/rails/rails/pull/42368 early since the query is
# definitely more efficient as it does not involved the PG planner.
# Remove once Rails 7 has been released.
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
def active?
@lock.synchronize do
@connection.query ";"
end
true
rescue PG::Error
false
end
end
end
end