带有PostgreSQL的Rails每次请求重新连接多次



我最近遇到一个问题,Rails断开了与PostgreSQL的连接,导致每个请求多次重新连接,大大降低了请求速度。我目前正在以下环境下运行Mac OS X上的所有本地程序:

  • Mac OS X 10.8(Mountain Lion)
  • Pow
  • NginX(ssl展开-即使没有启用此功能,也会有相同的应用程序重新连接行为)
  • PostgreSQL 9.2.2通过Homebrew
  • 轨道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.2升级到9.2.4,问题就完全消失了,预期的性能特性又回到了我的开发应用程序中。

最新更新