【问题标题】:Why postgres 9.3 table keeps losing the primary key and indexes?为什么 postgres 9.3 表不断丢失主键和索引?
【发布时间】:2014-08-12 21:06:51
【问题描述】:

rails 3.2 应用程序的 pg 9.3 数据库中,表 engine_configs 不断丢失主键 (id) 及其索引。这是表格:

对于上面显示的表格,我们只是再次添加了主键,它在列 ID 中显示整数而不是序列。上周,我们在表上手动重新创建了键和索引,并验证了这些键和索引确实存在。表上只有几个pg_dumppg_restore,才发现主键和索引又不见了。没有数据库崩溃或类似情况。是否有任何解释为什么会发生这种情况以及如何防止它再次发生?这个问题真的让我们的 Rails 应用程序崩溃了。

更新:

这是创建的 rails db:

class CreateAuthentifyEngineConfigs < ActiveRecord::Migration
  def change
    create_table :authentify_engine_configs do |t|
      #t.integer     :client_id
      t.string      :engine_name
      t.string      :engine_version
      t.string      :argument_name
      t.text        :argument_value
      t.integer     :last_updated_by_id
      t.timestamps
      t.string      :brief_note
      t.boolean :global, :default => false

    end

    add_index :authentify_engine_configs, :engine_name
    add_index :authentify_engine_configs, :argument_name
    add_index :authentify_engine_configs, [:engine_name, :argument_name], :name => :authentify_engine_configs_names
  end
end

这个迁移文件在 sqlite3 上已经用过很多次了,从来没有出现过问题。

更新1: 在pg_dump -Fc --table=authentify_engine_configs mydb &gt; mydb_ec.backup之后,再用:

恢复
pg_restore --clean --dbname=mydevdb --table=authentify_engine_configs --verbose c:\d\code\rails_proj\cis\db\mydb_ec.backup

恢复的本地副本丢失了索引。但是,当 pg_dump 整个数据库时,恢复的副本就可以了。

【问题讨论】:

  • 在 5 年多的 postgres 使用中,我从未经历过这样的行为。一定是你的轨道出轨了。
  • 在任何情况下:您可以检查 postgres 日志文件(如果您仍然拥有它,并且记录了“ddl”)
  • 这确实很奇怪。这是第一次迁移到 postgres。在与 sqlite3 一起使用之前从未出现过问题。
  • 嗨。与 pg_dump 和 pg_restore 相反,用户和执行数据库更新是否会破坏数据库?如果问题过于愚蠢,我们深表歉意。皮埃尔
  • 该应用仍由开发者开发和测试。配置表需要不断更新,这就是为什么有很多 pg_restore 和 pg_dump。当 pg_restore 时,使用 --clean。不确定这 --clean 是否会引入模式损坏的风险。 pg_restore 似乎无法仅进行数据更新(清空表并替换数据)。 pg_restore 的 --data-only 只是将记录添加到表的末尾。感谢您的提问。

标签: ruby-on-rails database postgresql indexing


【解决方案1】:

我最近遇到了同样的问题,用pg:reset解决了。我从 Getting "Unknown primary key for table" while the ID is there 找到了解决方案。我使用的是heroku pgbackups:restore,它正在删除主键,直到我使用pg:reset 删除了数据库。

【讨论】:

  • 我不在 Heroku,但删除数据库 (dropdb xxx) 并重新导入备份工作
猜你喜欢
  • 2021-02-28
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
  • 2014-03-17
相关资源
最近更新 更多