postgresql - Select 返回不一致的数据

标签 postgresql postgresql-9.3

这怎么可能?

insight=> select id from analysis_analysis where status != 'finished' and status != 'archived' and begin_at < '2014-07-28 17:23:27' limit 1;
  id   
-------
 46632
(1 row)

insight=> select id from analysis_analysis where id = 46632 ;
 id 
----
(0 rows)

我在同一台机器/主机/服务器上同时执行这些查询。我在 2 小时内得到了相同的结果。

编辑: 根据要求,以下是解释查询:

insight=> explain select id from analysis_analysis where status != 'finished' and status != 'archived' and begin_at < '2014-07-28 17:23:27' limit 1;
                                                                             QUERY PLAN                                                                              
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..4.61 rows=1 width=4)
   ->  Seq Scan on analysis_analysis  (cost=0.00..15286.67 rows=3317 width=4)
         Filter: (((status)::text <> 'finished'::text) AND ((status)::text <> 'archived'::text) AND (begin_at < '2014-07-28 17:23:27'::timestamp without time zone))
(3 rows)

insight=> explain select id from analysis_analysis where id = 46632 ;
                                             QUERY PLAN                                              
-----------------------------------------------------------------------------------------------------
 Index Only Scan using analysis_analysis_pkey on analysis_analysis  (cost=0.41..8.43 rows=1 width=4)
   Index Cond: (id = 46632)
(2 rows)

编辑:

我尝试重建索引,但现在出现另一个错误:

insight=> select id from analysis_analysis where id = 46632 ;
ERROR:  tuple offset out of range: 0
insight=> select id from analysis_analysis where id = 46633 ;
  id   
-------
 46633
(1 row)

编辑:

我(再次)重建索引,现在,查询结果为空(就像最初一样)

编辑:

另一个查询:

insight=> select id from analysis_analysis where id >= 46630 and id <= 46635;
  id   
-------
 46630
 46631
 46633
 46634
 46635
(5 rows)

最佳答案

analysis_analysis(id) 上可能有一个索引,不知何故它已损坏,您的第一个查询没有使用它,但第二个查询使用了它,但由于数据损坏而失败。

尝试第二个查询:

SET enable_indexscan TO off;

如果不一致来自于索引,这次结果将是正确的。

索引会如何损坏?

  • 早期版本的 postgresql 9.3.x 有一些可能导致此问题的错误,请检查 release notes .

  • 硬件问题。

在 SQL 中,您可以使用

REINDEX INDEX index_name;

从头开始重建它。

关于postgresql - Select 返回不一致的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25000532/

相关文章:

postgresql - 在 golang 的 sql 中使用 row_to_json 从我的 pgx 查询中获取字符串时遇到问题

postgresql - 根据另一个查询返回的条件在 postgres 中触发不同的查询

python - Django DateRangeField 问题

sql - 存储过程中不寻常的 UPDATE 语法

sql - Postgres数据库设计——仪表盘功能

postgresql - 使用 H2 数据库在 play slick 中运行测试时出现未知数据类型 "JSONB"

postgresql - 9.6 升级后 postgresql IN 查询出现奇怪的性能问题

postgresql - Postgres 在运行 Select 查询时不返回任何数据

postgresql - 插入后同步两个表

python - 提交后保持对数据库对象的锁定