Skip to content

A platform for community discussion. Free, open, simple.

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

Alan Guo Xiang Tan19a92fbadc88
diff --git a/lib/freedom_patches/active_record_postgresql_adapter.rb b/lib/freedom_patches/active_record_postgresql_adapter.rb
new file mode 100644
index 00000000000..bf64f9233c5
--- /dev/null
+++ b/lib/freedom_patches/active_record_postgresql_adapter.rb
@@ -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