Use an empty query instead of SELECT 1

Using an empty query skips the PG query planner so it is more efficient
This commit is contained in:
Heinrich Lee Yu 2021-06-02 20:55:54 +08:00
parent 2fd78b126c
commit 8325d07976
No known key found for this signature in database
GPG Key ID: 3B5E694767E9311B
2 changed files with 7 additions and 1 deletions

@ -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.

@ -292,7 +292,7 @@ def self.database_exists?(config)
# Is this connection alive and ready for queries?
def active?
@lock.synchronize do
@connection.query "SELECT 1"
@connection.query ";"
end
true
rescue PG::Error