PostgreSQL数据库级别,表级别,列级别的排序规则

时间:2019-07-08 11:53:57

标签: postgresql collation

我有使用归类类型'C'和UTF8字符集创建的数据库。如果我在同一数据库下创建任何表或索引,它将具有归类“ C”,或者我需要在创建表或索引时明确定义。

我们如何在Postgresql 11中提取表和索引的整理细节

使用归类为'C'的数据库创建还创建了表和idexe,而没有明确归类类型。

CREATE DATABASE testdb
    WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    LC_COLLATE = 'C'
    LC_CTYPE = 'en_US.UTF-8'
    TABLESPACE = testts
    CONNECTION LIMIT = -1
    TEMPLATE = template0;

create table test1c (id integer, content varchar(10));
create index idx_test on test1c(content);

需要有关在表和索引级别进行整理的信息。

1 个答案:

答案 0 :(得分:0)

一列的排序规则存储在该列的attcollation行的pg_attribute中。值100表示​​“默认排序规则”,它是存储在pg_database中的数据库排序规则。

这是一种查找表所有列的排序规则的方法:

WITH defcoll AS (
   SELECT datcollate AS coll
   FROM pg_database
   WHERE datname = current_database()
)
SELECT a.attname,
       CASE WHEN c.collname = 'default'
            THEN defcoll.coll
            ELSE c.collname
       END AS collation
FROM pg_attribute AS a
   CROSS JOIN defcoll
   LEFT JOIN pg_collation AS c ON a.attcollation = c.oid
WHERE a.attrelid = 'test1c'::regclass
  AND a.attnum > 0
ORDER BY attnum;

 attname | collation 
---------+-----------
 id      | 
 content | C
(2 rows)

对于索引,排序规则存储在列pg_index.indcollation中:

WITH defcoll AS (
   SELECT datcollate AS coll
   FROM pg_database
   WHERE datname = current_database()
)
SELECT icol.pos,
       CASE WHEN c.collname = 'default'
            THEN defcoll.coll
            ELSE c.collname
       END AS collation
FROM pg_index AS i
   CROSS JOIN unnest(i.indcollation) WITH ORDINALITY AS icol(coll, pos)
   CROSS JOIN defcoll
   LEFT JOIN pg_collation AS c ON c.oid = icol.coll
WHERE i.indexrelid = 'idx_test'::regclass
ORDER BY icol.pos;

 pos | collation 
-----+-----------
   1 | C
(1 row)