ruby-on-rails - 在事务期间保持数据可用 (postgresql)

标签 ruby-on-rails postgresql transactions truncate pg

为什么我无法在使用截断语句的事务期间访问数据?

我虽然可以在同一个表上运行事务时读取表的数据。 只要您不执行截断语句,就有可能。 我没有在文档中发现任何反对它的内容。

这里有一个示例项目来说明这种行为,自述文件试图解释如何重现:https://github.com/Haelle/pg_transaction_tests

如果它不应该发生,它可能是一个问题? :

  • 我的代码...
  • 事件记录
  • 珍宝
  • Postgresql 中的一个选项

最佳答案

这是 postgresql 特有的东西,来自 TRUNCATE documentation :

TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which blocks all other concurrent operations on the table. When RESTART IDENTITY is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the DELETE command should be used instead.

这是您遇到的一个非常具体的用例。不确定为什么在截断事务期间需要能够访问即将被截断的表。但是,正如注释所说:改用 delete 。在 rails 中,这意味着:.destroy_all(检查 rails 验证)或 .delete_all(不检查 rails 验证)

关于ruby-on-rails - 在事务期间保持数据可用 (postgresql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956860/

相关文章:

ruby-on-rails - 多态 Ruby SimpleDelegator

java - Android 应用程序如何使用 Java 前端和 Rails 4 后端?

ruby-on-rails - Rails 3.2 重定向/images/* 到/assets/*

sql - 具有 session 变量的 postgresql 行级安全性

sql - 升级到 PostgreSQL 11 : set-returning functions are not allowed in CASE

php - 如果查询使用多个连接,mysql 事务会起作用吗?

testing - Grails 测试和@Transactional

mysql - 比较日期以找出 mysql 中的 max(date) 时出错

ruby-on-rails - 依赖于 Ruby on Rails 关联类型的急切加载

ruby-on-rails - 其中(:sku => 'sku123' ) vs where ('sku =