ruby-on-rails - Rails with PostgreSQL 每个请求重新连接多次

标签 ruby-on-rails ruby macos postgresql pg

我最近遇到一个问题,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/

相关文章:

ruby-on-rails - bundle 程序:找不到命令:在示例项目上使用 docker-compose 的 Rails

java - 执行非 native MacOS 终端命令 - 截断以创建新文件 - 在 Java 代码中

macos - 显示 NSWindow 下面的内容的最佳方式是什么?

ruby-on-rails - 如何显示谁访问了您的个人资料?

ruby-on-rails - 通过命令行升级 RailsInstaller 安装?

ruby-on-rails - 如何在Rails 4 上显示Redis 的Hash 值?

ruby - 在 2 个循环内将字符串插入字符串

ruby-on-rails - 如何在没有模板的情况下使用 Rails 发送邮件?

ruby-on-rails - 图像的 Rails 3.1 绝对 URL

python - 当我尝试在 Mac mojave 10.14 中安装 MySQL-python 包时出现错误 : command 'cc' failed with exit status 1.