简体   繁体   English

Postgres SQL 查询不一致

[英]Postgres SQL query inconsistency

How is it possible to get a different result (0 rows vs 272 rows) when running SELECT * vs COUNT(*) with the same query in Postgres 12 on Linux?在 Linux 上的 Postgres 12 中使用相同的查询运行 SELECT * vs COUNT(*) 时,如何获得不同的结果(0 行 vs 272 行)? The database is not changing.数据库没有变化。 I tried vacuuming and restarting the Postgres server.我尝试清理并重新启动 Postgres 服务器。 I am manually trying to clean up a corrupted database by removing some references to an unimportant table that didn't get properly deleted and keeps throwing errors.我正在手动尝试通过删除对未正确删除并不断抛出错误的不重要表的一些引用来清理损坏的数据库。 Yes, I backed up all the data first.是的,我先备份了所有数据。

I am mostly wondering if this is a bug, or I am misunderstanding how to use SQL.我主要想知道这是否是一个错误,或者我误解了如何使用 SQL。 Also note that adding an AND condition increases the number of results for COUNT, which does not make sense to me.另请注意,添加 AND 条件会增加 COUNT 的结果数,这对我来说没有意义。

Psql console dump: Psql 控制台转储:

postgres=# SELECT COUNT(*) FROM pg_attribute WHERE pg_attribute.attrelid >= 2128137 AND pg_attribute.attrelid <= 2132360;
 count 
-------
     0
(1 row)

postgres=# SELECT COUNT(*) FROM pg_attribute WHERE pg_attribute.attrelid >= 2128137 AND pg_attribute.attrelid <= 2132360 AND pg_attribute.attname NOT ILIKE '%pg.dropped%';
 count 
-------
   272
(1 row)

postgres=# SELECT COUNT(*) FROM pg_attribute WHERE pg_attribute.attrelid >= 2128137 AND pg_attribute.attrelid <= 2132360 AND pg_attribute.attname NOT ILIKE '%pg.dropped%';
 count 
-------
   272
(1 row)

postgres=# SELECT * FROM pg_attribute WHERE pg_attribute.attrelid >= 2128137 AND pg_attribute.attrelid <= 2132360 AND pg_attribute.attname NOT ILIKE '%pg.dropped%';
(returns 0 rows in editor)

It is “normal” for queries on a corrupted database to return wrong results.对损坏的数据库进行查询返回错误结果是“正常的”。

Rather than trying to fix the corruption (where you can never be certain that there may not be more corruption be lurking somewhere), pg_dump the database and restore it to a newly created cluster on different hardware .与其尝试修复损坏(您永远无法确定某处可能没有更多损坏),而是pg_dump数据库并将其恢复到不同硬件上新创建的集群 If you are lucky, that will work, and you are fine.如果你很幸运,那会奏效,而且你很好。

Catalog corruption is the nastiest kind.目录损坏是最严重的一种。 If the above doesn't work, seek professional help.如果上述方法不起作用,请寻求专业帮助。 If you don't know what a query plan is, you won't be able to do that yourself (no insult intended).如果您不知道查询计划是什么,您将无法自己执行此操作(无意侮辱)。

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

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