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

时间:2013-02-05 21:36:23

标签: ruby-on-rails ruby macos postgresql pg

我最近遇到一个问题,即Rails正在丢弃与PostgreSQL的连接,导致每个请求多次重新连接,这大大减慢了请求。我目前在Mac OS X上使用以下环境运行本地的所有内容:

  • Mac OS X 10.8(Mountain Lion)
  • 的Pow
  • NginX(ssl展开 - 即使没有启用此功能也会重新连接相同的行为)
  • PostgreSQL 9.2.2 via Homebrew
  • 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电话?他们的目的是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

答案是将PostgreSQL升级到更新版本。我刚刚从原始问题中使用的9.2.4升级到9.2.2,问题完全消失了,预期的性能特征会返回到我的开发应用程序。