我最近遇到一个问题,Rails 正在断开与我的 PostgreSQL 的连接,导致每个请求多次重新连接,从而显着减慢请求速度。我目前使用以下环境在 Mac OS X 上本地运行所有内容:
- Mac OS X 10.8(山狮)
- 战俘
- NginX(ssl 展开 - 相同的应用程序重新连接行为,即使没有启用此功能)
- 通过 Homebrew 的 PostgreSQL 9.2.2
- rails 3.2.11
这是我的 database.yml
: (已编辑数据库和用户名)
development:
adapter: postgresql
encoding: unicode
database: <database>
pool: 5
username: <user>
password:
这是 development.log
的输出在一个典型的请求期间:(特定模型和渲染编辑)
Connecting to database specified by database.yml
Started GET "/" for 127.0.0.1 at 2013-02-05 12:25:38 -0800
Processing by HomeController#index as HTML
... <app performs model loads and render calls>
Completed 200 OK in 314ms (Views: 196.0ms | ActiveRecord: 60.9ms)
Connecting to database specified by database.yml
Connecting to database specified by database.yml
我还在 Postgres 中启用了语句日志记录,以便更好地了解在给定请求期间数据库端到底发生了什么。这是 postgresql-<date>.log
的输出: (已编辑特定于我的应用的查询)
LOG: connection received: host=[local]
LOG: connection authorized: user=<user> database=<database>
LOG: statement: set client_encoding to 'UTF8'
LOG: statement: set client_encoding to 'unicode'
LOG: statement: SHOW client_min_messages
LOG: statement: SET client_min_messages TO 'panic'
LOG: statement: SET standard_conforming_strings = on
LOG: statement: SET client_min_messages TO 'notice'
LOG: statement: SET time zone 'UTC'
LOG: statement: SHOW TIME ZONE
LOG: statement: SELECT 1
LOG: statement: SELECT 1
... <app makes queries for request>
LOG: disconnection: session time: 0:00:01.346 user=<user> database=<database> host=[local]
LOG: connection received: host=[local]
LOG: connection authorized: user=<user> database=<database>
LOG: statement: set client_encoding to 'UTF8'
LOG: statement: set client_encoding to 'unicode'
LOG: statement: SHOW client_min_messages
LOG: statement: SET client_min_messages TO 'panic'
LOG: statement: SET standard_conforming_strings = on
LOG: statement: SET client_min_messages TO 'notice'
LOG: statement: SET time zone 'UTC'
LOG: statement: SHOW TIME ZONE
LOG: statement: SELECT 1
LOG: connection received: host=[local]
LOG: connection authorized: user=<user> database=<database>
LOG: statement: set client_encoding to 'UTF8'
LOG: statement: set client_encoding to 'unicode'
LOG: statement: SHOW client_min_messages
LOG: statement: SET client_min_messages TO 'panic'
LOG: statement: SET standard_conforming_strings = on
LOG: statement: SET client_min_messages TO 'notice'
LOG: statement: SET time zone 'UTC'
LOG: statement: SHOW TIME ZONE
LOG: statement: SELECT 1
LOG: statement: SELECT 1
LOG: statement: SELECT 1
LOG: disconnection: session time: 0:00:00.750 user=<user> database=<database> host=[local]
LOG: disconnection: session time: 0:00:00.733 user=<user> database=<database> host=[local]
我很乐意为命令调用更新相关配置或日志输出。另外,为什么所有 SELECT 1
电话?他们的目的是什么?
谢谢!
最佳答案
答案是将 PostgreSQL 升级到更新的版本。我刚刚升级到 9.2.4
来自 9.2.2
正如在原始问题中使用的那样,问题完全消失了,我的开发应用程序恢复了预期的性能特征。
关于ruby-on-rails - Rails with PostgreSQL 每个请求重新连接多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14717394/