简体   繁体   English

如何列出过去24小时内所有数据表的变化?

[英]How list all tables with data changes in the last 24 hours?

We had an ugly problem, by mistake, a balancer redirect some requests to a test instance with pretty similar data than production, now I know that there are data recorded in the test Postgres that belongs to production 我们有一个丑陋的问题,错误地,平衡器将一些请求重定向到具有与生产非常相似的数据的测试实例,现在我知道在测试Postgres中记录的数据属于生产

Is there a way to list all the tables with data changes in the last 24 hours in Postgres? 有没有办法列出Postgres过去24小时内所有数据变化的表格?

Postgres version is 9.3 and I have around 250 tables. Postgres版本是9.3,我有大约250个表。

First consider my comment. 首先考虑我的评论。

Postgres up to and including 9.4 does not by itself record timestamps when rows were inserted or updated. Postgres直到和包括9.4本身不会记录插入或更新行时的时间戳。

There are some system columns in the row headers that can help in the forensic work. 行标题中有一些系统列可以帮助进行取证工作。 The physical order of rows ( ctid ) can be an indicator if nothing else has happened to the table since. 如果表格中没有其他任何内容发生,则行( ctid )的物理顺序可以作为指示符。 In simple cases new rows are appended to the physical end of a table when inserted, so the ctid indicates what was inserted last - until anything changes in the table. 在简单的情况下,插入时会将新行附加到表的物理端,因此ctid指示最后插入的内容 - 直到表中的任何更改。 Postgres is free to rearrange the physical order of rows at will, for instance with VACUUM . Postgres可以随意重新排列行的物理顺序,例如使用VACUUM Any UPDATE also writes a new row version, which can change the physical position. 任何UPDATE也会写一个新的行版本,它可以改变物理位置。 The new version does not have to be at the end of the table. 新版本不必位于表格的末尾。 Postgres tries to keep new row version on the same data page if possible ( HOT update ) ... Postgres尝试在可能的情况下将新行版本保留在同一数据页面上( HOT更新 )...

That said, here is a simple query to get the physically last rows for a given table: 也就是说,这是一个简单的查询来获取给定表的物理上最后一行:

SELECT ctid, *
FROM   big
ORDER  BY ctid DESC
LIMIT  10;

Related answers on dba.SE with detailed information: 有关详细信息,请联系dba.SE:

The insert transaction id xmin can be useful: 插入事务id xmin可能很有用:

If you happen to have a backup for the test DB from right before the incident, that would be helpful. 如果您碰巧在事件发生前就有了备份测试数据库,那将会很有帮助。 Restore the old state to a separate schema of the test DB and compare tables ... 将旧状态恢复到测试数据库的单独模式并比较表...

Typically, I add one or two timestamptz columns to important tables for when the row was inserted, and / or when it was updated the last time. 通常,我会在插入行时和/或上次更新时为重要表添加一个或两个timestamptz列。 That would be tremendously useful for you right now ... 那对你来说非常有用......

What would also be great for you: the "temporal" features introduced in the SQL standard with SQL:2011 . 对您来说也很棒: SQL标准中引入“时间”功能与SQL:2011 But that's not implemented in Postgres, yet. 但是,这还没有在Postgres中实现。
There's a page in the Postgres Wiki . Postgres Wiki中有一个页面
There is also an unofficial extension on PGXN . PGXN还有一个非正式的扩展 I have not tested it and can't say how far it is. 我没有测试它,也不能说它有多远。

Postgres 9.5 introduces a feature to record commit timestamps (like @Craig commented ). Postgres 9.5引入了一个记录提交时间戳的功能(如@Craig评论 )。 Needs to be enabled manually before it starts recording. 需要在开始录制之前手动启用。 The manual: 手册:

track_commit_timestamp ( bool ) track_commit_timestampbool

Record commit time of transactions. 记录事务的提交时间。 This parameter can only be set in postgresql.conf file or on the server command line. 此参数只能在postgresql.conf文件或服务器命令行中设置。 The default value is off . 默认值为off

And some functions to work with it. 还有一些功能可以使用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM