简体   繁体   English

添加新列时如何避免PG :: InFailedSqlTransaction?

[英]How can I avoid PG::InFailedSqlTransaction when adding new columns?

We are running two rails applications against the same database. 我们针对同一个数据库运行两个rails应用程序。 When we deploy, we typically deploy to App A, then App B, restarting all rails processes during the deploy. 部署时,我们通常部署到App A,然后部署到App B,在部署期间重新启动所有rails进程。 App A runs on 7 servers with at least 20 processes connections to the database. App A在7台服务器上运行,至少有20个进程连接到数据库。 App B runs on 4 servers with at least 8 connections to the database. App B在4台服务器上运行,至少有8个与数据库的连接。

Today, when we deployed App A, we added a column to an existing table: 今天,当我们部署App A时,我们在现有表中添加了一列:

change_table :organizations do |t|
  t.integer :users_count, default: 0
end

We expected this to be fine: its new column on an existing table and it has a default. 我们希望这很好:它在现有表上的新列,它有一个默认值。 Shortly after the migration ran, a number of errors showed up, from both App A (before it was restarted) and App B (before it was deployed to). 迁移运行后不久,App A(重启之前)和App B(部署之前)出现了一些错误。

These errors were: 这些错误是:

FATAL ActiveRecord::StatementInvalid error: PG::InFailedSqlTransaction:
ERROR:  current transaction is aborted, commands ignored until end of 
transaction block

In the postgres log, I have 58 errors like this: 在postgres日志中,我有58个错误,如下所示:

postgres[12283]: ERROR:  cached plan must not change result type
postgres[12283]: STATEMENT:  SELECT  "organizations".* FROM 
  "organizations" WHERE "organizations"."id" = $1 LIMIT $2

This repeats a number of times and goes away after all deploys have finished and all processes restarted. 这会重复多次,并在所有部署完成并重新启动所有进程后消失。

It appeared that Rails bug #12330 and Rails PR 22170 addressed this in Rails 5.0, but I have that commit and am still seeing this error. 似乎Rails错误#12330Rails PR 22170在Rails 5.0中解决了这个问题,但我有这个提交,我仍然看到这个错误。

Relevant software versions 相关软件版本

  • Rails 5.0.2 Rails 5.0.2
  • PG 0.19.0 PG 0.19.0
  • Postgres 9.5 Postgres 9.5

One comment on Rails bug #12330 suggests that I have to add the columns with null defaults. 关于Rails bug#12330的一条评论表明我必须添加具有null默认值的列。 Another suggests performing multiple deploys, one to disable prepare statements, then another to perform the migration and and re-enable prepared statements. 另一个建议执行多个部署,一个用于禁用prepare语句,另一个用于执行迁移并重新启用预准备语句。

Is there away to avoid this? 有没有避免这种情况? It clears up when we restart the servers, but I feel like I'm missing something - like only using nullable columns perhaps that would avoid these errors all together. 当我们重新启动服务器时它会清除,但我觉得我错过了一些东西 - 比如只使用可空列可能会一起避免这些错误。 This doesn't happen on every deploy and I don't know how to reproduce it - but this wasn't the first time it has happened. 这不会在每次部署时发生,我不知道如何重现它 - 但这不是第一次发生。

You have changed table structure and thus what your prepared SELECT returns, because you use "organizations".* , with is returning all columns. 您已经更改了表结构,因此更改了准备好的SELECT返回的内容,因为您使用"organizations".* ,并返回所有列。 PostgreSQL apparently doesn't support updating of prepared statements, so you need to either create new session (reconnect) or use DEALLOCATE to remove that prepared statement. PostgreSQL显然不支持更新预准备语句,因此您需要创建新会话(重新连接)或使用DEALLOCATE删除准备好的语句。

EDIT: You could also stop using SELECT * . 编辑:你也可以停止使用SELECT *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 4:使用 RSpec 测试 rake 任务时的 PG::InFailedSqlTransaction - Rails 4: PG::InFailedSqlTransaction when testing a rake task with RSpec 调试 PG::InFailedSqlTransaction - Debug PG::InFailedSqlTransaction ActiveRecord::StatementInvalid: PG InFailedSqlTransaction - ActiveRecord::StatementInvalid: PG InFailedSqlTransaction PG :: InFailedSqlTransaction创建初始租户 - PG::InFailedSqlTransaction on creating initial tenant 后挂钩错误,来自Rspec的PG :: InFailedSqlTransaction - Error in an after hook, PG::InFailedSqlTransaction from Rspec Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: - Rails Rspec - ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: 向数据库添加新记录时如何避免包含 OUTPUT INSERTED - How to avoid includeing OUTPUT INSERTED when adding a new record to database 使用时如何避免开始换行 <div class=> Ajax刷新加载? - How can I avoid starting new line when using <div class=> for ajax refresh loading? 使用sum函数时如何避免PG :: NumericValueOutOfRange - How to avoid PG::NumericValueOutOfRange when using sum function 为什么在向表中添加新列时不再能耙db:migrate了? 我可以在迁移文件中手动添加列还是应该生成一个? - Why can I no longer rake db:migrate when adding new columns to my table? Can I add columns manually in the migration file or should I generate one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM