From 8325d079763e058b4e685d1300889e99fc12478a Mon Sep 17 00:00:00 2001 From: Heinrich Lee Yu Date: Wed, 2 Jun 2021 20:55:54 +0800 Subject: [PATCH] Use an empty query instead of SELECT 1 Using an empty query skips the PG query planner so it is more efficient --- activerecord/CHANGELOG.md | 6 ++++++ .../active_record/connection_adapters/postgresql_adapter.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 29005f1331..637449a537 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Use an empty query to check if the PostgreSQL connection is still active + + An empty query is faster than `SELECT 1`. + + *Heinrich Lee Yu* + * Add `ActiveRecord::Base#previously_persisted?` Returns `true` if the object has been previously persisted but now it has been deleted. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 69b8d19ecd..2ef8273352 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -292,7 +292,7 @@ module ActiveRecord # Is this connection alive and ready for queries? def active? @lock.synchronize do - @connection.query "SELECT 1" + @connection.query ";" end true rescue PG::Error