columnar

列式存储 (24.x)

使用

创建扩展以及示例表

CREATE EXTENSION "columnar";

CREATE TABLE columnar_table (
    id BIGINT,
    name TEXT,
    age INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) USING columnar;

可以在数据库的命令行界面使用命令查看当前表是否为列存表

\d+ columnar_table 
                                                       Table "public.columnar_table"
   Column   |            Type             | Collation | Nullable |      Default      | Storage  | Compression | Stats target | Description 
------------+-----------------------------+-----------+----------+-------------------+----------+-------------+--------------+-------------
 id         | bigint                      |           |          |                   | plain    |             |              | 
 name       | text                        |           |          |                   | extended |             |              | 
 age        | integer                     |           |          |                   | plain    |             |              | 
 created_at | timestamp without time zone |           |          | CURRENT_TIMESTAMP | plain    |             |              | 
Access method: columnar

使用数据库软件将列存表迁移到其他库中(因其他库没有添加列存扩展所以自动转换为行存),然后查询。 可以看到同样的结构同样的数据,其中占用的空间是不同的。

radius=# SELECT 
    (SELECT COUNT(*) FROM columnar_table) AS row_count,
    pg_size_pretty(pg_total_relation_size('columnar_table')) AS total_size;
 row_count | total_size 
-----------+------------
     10000 | 1984 kB
(1 row)

unvdb=# SELECT 
    (SELECT COUNT(*) FROM columnar_table) AS row_count,
    pg_size_pretty(pg_total_relation_size('columnar_table')) AS total_size;
 row_count | total_size 
-----------+------------
     10000 | 29 MB
(1 row)

限制

• 不支持逻辑复制 • 不支持ON CONFLICT子句 • 在某些情况下,索引可能会导致查询变慢,大部分情况下索引不是必须的 • 不支持BRIN索引