Postgres:在不扫描整个表的情况下确定表中更改的最佳方法

时间:2017-04-07 08:36:28

标签: postgresql etl

我正在设计一个etl来从postgres表中进行增量更改。

如何在不执行全表扫描的情况下检测最后一次运行后是否修改了表行?

1 个答案:

答案 0 :(得分:1)

我会保存统计信息并从pg_stat_all_tables进行比较,例如我只是按顺序运行:

t=# select schemaname,relname,n_tup_ins,n_tup_upd,n_tup_del from pg_stat_all_tables where relname = 'rapid_inserts';
 schemaname |      relname       | n_tup_ins | n_tup_upd | n_tup_del
------------+--------------------+-----------+-----------+-----------
 public     | rapid_inserts| 254681563 |         0 |         0
(1 row)

Time: 10.921 ms
t=# select schemaname,relname,n_tup_ins,n_tup_upd,n_tup_del from pg_stat_all_tables where relname = 'rapid_inserts';
 schemaname |      relname       | n_tup_ins | n_tup_upd | n_tup_del
------------+--------------------+-----------+-----------+-----------
 public     | rapid_inserts| 254681569 |         0 |         0
(1 row)

Time: 10.980 ms

这意味着在几秒钟内插入了6行。同样适用于更新和删除...