macos - Postgresql 和 unicode 表名 : Why can I not select the table name from the information schema when it contains unicode characters?

标签 macos postgresql unicode

我创建了一个名称中包含 unicode 字符的表(专门用 unicode 测试表名)。它可以很好地创建表,但是我检测表是否存在的方法坏了!

这里是有问题的交互:

caribou_test=# select table_name from information_schema.tables where table_schema = 'public';
 table_name  
-------------
...
 pinkpink1
(16 rows)

caribou_test=# select table_name from information_schema.tables where table_schema = 'public' and table_name = 'pinkƒpink1';
 table_name 
------------
(0 rows)

caribou_test=# select table_name from information_schema.tables where table_schema = 'public' and table_name = 'pinkpink1';
 table_name 
------------
(0 rows)

caribou_test=# select * from pinkƒpink1;
 id | position | env_id | locked |         created_at         |       updated_at        | status_id | status_position | i1l0  |  f∆   |  growth555   
----+----------+--------+--------+----------------------------+-------------------------+-----------+-----------------+-------+-------+--------------
  1 |        0 |      1 | f      | 2013-06-27 14:50:34.228136 | 2013-06-27 14:50:34.227 |         1 |               0 | YELLL | 55555 | 1.3333388822
(1 row)

表名是pinkƒpink1(测试数据)。如您所见,当我从 information_schema.tables 中选择表名时,它显示时没有 ƒ,但我无法以任何方式选择表名!但我仍然可以直接向该表发出选择。这是怎么回事?

编辑:为@craig-ringer 提供请求的信息:

caribou_test=# SELECT current_setting('server_encoding') AS server_encoding, current_setting('client_encoding') AS client_encoding, version();
 server_encoding | client_encoding |                                                                    version                                                                     
-----------------+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------
 UTF8            | UTF8            | PostgreSQL 9.2.2 on x86_64-apple-darwin12.2.1, compiled by Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn), 64-bit

caribou_test=# SELECT * FROM pg_class WHERE relname = 'pinkƑpink1';
--->  (0 rows)

caribou_test=# SELECT upper('ƒ') = 'Ƒ', lower('Ƒ') = 'ƒ';
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

caribou_test=# WITH chars(rowid, thechar) AS (VALUES (1,'ƒ'),(2,'Ƒ'),(3,upper('ƒ')),(4,lower('Ƒ'))) SELECT rowid, thechar, convert_to(thechar, 'utf-8') from chars;
 rowid | thechar | convert_to 
-------+---------+------------
     1 | ƒ       | \xc692
     2 | Ƒ       | \xc691
     3 | Ƒ       | \xc691
     4 | ƒ       | \xc692

最佳答案

它看起来像一个错误,可能在 regclass 或与之相关的东西中:

# create table pinkƒpink1 (id serial);
NOTICE:  CREATE TABLE will create implicit sequence "pink?pink1_id_seq" for serial column "pink?pink1.id"
CREATE TABLE
# select 'pinkƒpink1'::name;
    name    
------------
 pinkƒpink1
(1 row)

# select 'pinkƒpink1'::regclass;
  regclass   
-------------
 "pinkpink1"
(1 row)

# select relname from pg_class where oid = 'pinkƒpink1'::regclass;
  relname  
-----------
 pinkpink1

# select relname from pg_class where relname = 'pinkƒpink1'::name;
 relname 
---------
(0 rows)

# select relname from pg_class where relname = 'pinkpink1';
 relname 
---------
(0 rows)

(我的系统是 OSX Lion,一切都是 utf8,以防万一。)


对于解决方法,您可以将其转换为 ::regclass 就像上面所做的那样(找到表的那个)。请注意,如果表不存在,转换为 ::regclass 将产生错误,因此请相应地编写代码。


根据 Craig 的要求:

# SELECT current_setting('server_encoding') AS server_encoding, current_setting('client_encoding') AS client_encoding, version();
 server_encoding | client_encoding |                                                              version                                                              
-----------------+-----------------+-----------------------------------------------------------------------------------------------------------------------------------
 UTF8            | UTF8            | PostgreSQL 9.2.4 on x86_64-apple-darwin11.4.2, compiled by Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn), 64-bit
(1 row)

根据欧文的说法:

# SELECT name, setting FROM pg_settings WHERE  name IN ('lc_collate','lc_ctype','client_encoding','server_encoding');
      name       |   setting   
-----------------+-------------
 client_encoding | UTF8
 lc_collate      | en_US.UTF-8
 lc_ctype        | en_US.UTF-8
 server_encoding | UTF8
(4 rows)

关于macos - Postgresql 和 unicode 表名 : Why can I not select the table name from the information schema when it contains unicode characters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17353469/

相关文章:

java - Mac IntelliJ 光标样式/类型没有改变(保持默认)

macos - 通过网络设置两机内核调试

r - Shiny 应用程序在 Mac 上的 RStudio 中崩溃

php - pg_connect 失败时不返回 false

ruby-on-rails - 为 PostgreSQL 安装 ruby​​ gem 时出错

php - 默认的 mb_internal_encoding() 值来自哪里?

objective-c - 与 mac osx 10.10 不兼容的 ARC SDK

sql-server - 将数据从 MS SQL 迁移到 PostgreSQL?

java - 奇怪的 unicode 字符导致 XML 解析失败

python - Unicode解码错误: 'ascii' codec can't decode byte 0x87 in position 10: ordinal not in range(128)