where子句中的lower()与模式不匹配

时间:2017-05-23 12:10:44

标签: postgresql where-clause postgresql-9.3

select version();
                                                   version
--------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.3.14 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
(1 row)

查询:

select foreign_text
, lower(t.foreign_text) = 'спорт' c1
, convert_to(lower(t.foreign_text),'utf8') = convert_to(lower('спорт'),'utf8') c2
from translation_keywords_from_ru_to_en t
where
  lower(t.foreign_text) = 'спорт'
;
 foreign_text | c1 | c2
--------------+----+----
(0 rows)

删除lower中的where,留在列列表中:

select foreign_text
, lower(t.foreign_text) = 'спорт' c1
, convert_to(lower(t.foreign_text),'utf8') = convert_to(lower('спорт'),'utf8') c2
from translation_keywords_from_ru_to_en t
where
  t.foreign_text = 'спорт'
;
 foreign_text | c1 | c2
--------------+----+----
 спорт        | t  | t
 спорт        | t  | t
(2 rows)

请注意 c1 lower(t.foreign_text) = 'спорт')是true,而它显然不在WHERE子句中的先前选择中。

同样如此:

where lower(t.foreign_text collate "fr_FR") = 'спорт'
where initcap(t.foreign_text) = initcap('спорт')
where convert_to(lower(t.foreign_text),'utf8') = convert_to(lower('спорт'),'utf8')
where upper(t.foreign_text) = upper('спорт')

但是错误:

where lower(t.foreign_text) = 'спорт'
where lower(t.foreign_text) = lower('спорт')

我为lower()做了JLS。如果我将lower(t.foreign_text)转换为“utf bytea”,现象就会消失。

我的情况:一旦查询insert into ... where not exists (... where lower(t.foreign_text) = lower('спорт'))无法找到行,它就会尝试插入重复项。我看到如何重写查询以避免它。但我很困惑。

问题:我错过了什么,而lower(t.foreign_text) = 'спорт'where中可能是假的,但列列表中的true或者我遇到了某种错误?< / p>

foreign_text属于text类型

\l+ db
                                               List of databases
  Name  | Owner | Encoding |   Collate   |    Ctype    | Access privileges |  Size  | Tablespace | Description
--------+-------+----------+-------------+-------------+-------------------+--------+------------+-------------
      db|     un| UTF8     | en_US.UTF-8 | en_US.UTF-8 |                   | 406 GB | pg_default 

|

1 个答案:

答案 0 :(得分:3)

我能想到的唯一解释是你有一个索引ON translation_keywords_from_ru_to_en (lower(foreign_text)),但该索引已损坏。

如果在查询之前运行以下命令,是否会得到相同的结果:

SET enable_indexscan = off;
SET enable_indexonlyscan = off;
SET enable_bitmapscan = off;

这将避免在查询中使用索引。