暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

[翻译] Postgres 14 亮点-创建表压缩

原创 Michael Paquier 2021-05-17
3990

以下是有关以下commit的简短故事 ,该功能将在即将发布的PostgreSQL 14版本中提供:

commit: bbe0a81db69bd10bd166907c3701492a29aca294
author: Robert Haas <rhaas@postgresql.org>
date: Fri, 19 Mar 2021 15:10:38 -0400
Allow configurable LZ4 TOAST compression

There is now a per-column COMPRESSION option which can be set to pglz
(the default, and the only option in up until now) or lz4. Or, if you
like, you can set the new default_toast_compression GUC to lz4, and
then that will be the default for new table columns for which no value
is specified. We don't have lz4 support in the PostgreSQL code, so
to use lz4 compression, PostgreSQL must be built --with-lz4.

[...]

Dilip Kumar. The original patches on which this work was based were
written by Ildus Kurbangaliev, and those were patches were based on
even earlier work by Nikita Glukhov, but the design has since changed
very substantially, since allow a potentially large number of
compression methods that could be added and dropped on a running
system proved too problematic given some of the architectural issues
mentioned above; the choice of which specific compression method to
add first is now different; and a lot of the code has been heavily
refactored.  More recently, Justin Przyby helped quite a bit with
testing and reviewing and this version also includes some code
contributions from him. Other design input and review from Tomas
Vondra, Álvaro Herrera, Andres Freund, Oleg Bartunov, Alexander
Korotkov, and me.

Discussion: http://postgr.es/m/20170907194236.4cefce96%40wp.localdomain
Discussion: http://postgr.es/m/CAFiTN-uUpX3ck%3DK0mLEk-G_kUQY%3DSNOTeqdaNRR9FMdQrHKebw%40mail.gmail.com

OAST的概念解决了在Postgres中存储大值的问题,该概念 基本上是使用适合于PostgreSQL的,称为pglz的压缩算法(从其API的代码树中的src / common / pg_lzcompress.c)压缩数据值。这种压缩方法已经可以投票,尽管它取决于您自己的国家/地区(来自 Jan Wieck于1999年提交的commit 79c3b71c),但随着pglz成为CPU的巨大消耗者,它很容易被最新的压缩算法所取代。上面提到的提交为用户提供了一种可用于TOAST压缩的压缩方法的替代方法,已知LZ4在压缩和速度方面可以提供很好的折衷。

为了启用此选项,首先请注意,必须使用configure选项–with-lz4来构建代码,这意味着编译将需要一个额外的开发包,例如liblz4-dev。

此选项仅适用于将应用Toast压缩的数据类型,并且CREATE TABLE 获得了名为COMPRESSION的新列级子句,可以将其设置为两个值:“ pglz”或“ lz4”。例如(请注意,\ d +显示每列使用的压缩方法):

=# CREATE TABLE tab_compression (
     a text COMPRESSION pglz,
     b text COMPRESSION lz4);
CREATE TABLE
=# \d+ tab_compression
                                    Table "public.tab_compression"
 Column | Type | Collation | Nullable | Default | Storage  | Compression | Stats target | Description
--------+------+-----------+----------+---------+----------+-------------+--------------+-------------
 a      | text |           |          |         | extended | pglz        |              |
 b      | text |           |          |         | extended | lz4         |              |
Access method: heap

值得注意的是,psql具有一个称为HIDE_TOAST_COMPRESSION的选项,能够隐藏使用的压缩方法,类似于HIDE_TABLEAM的访问方法。这对旨在进行便携式测试的扩展开发人员很有用。

此功能附带一个 可以由用户设置的名为default_toast_compression的GUC 。它的默认值是“ pglz”,任何未指定COMPRESSION的属性都将其用作默认压缩方法。

ALTER TABLE 也带有其自己的方法来强制使用属性使用的压缩方法,但是请注意,这不会更改已经以特定方式压缩的任何值,并且仅对新值有效(可以在此处使用VACUUM FULL) 。新函数pg_column_compression()提供给定值所使用的压缩方法,在这里非常有用:

=# CREATE TABLE tab_compression_2 (id int, data text COMPRESSION pglz);
CREATE TABLE
=# INSERT INTO tab_compression_2 VALUES(1, repeat('1234567890', 1000));
INSERT 0 1
=# ALTER TABLE tab_compression_2 ALTER COLUMN data SET COMPRESSION lz4;
ALTER TABLE
=# INSERT INTO tab_compression_2 VALUES(2, repeat('1234567890', 1000));
INSERT 0 1
=# SELECT id, pg_column_compression(data) FROM tab_compression_2;
 id | pg_column_compression
----+-----------------------
  1 | pglz
  2 | lz4
(2 rows)

相同的概念适用于CREATE TABLE AS或SELECT INSERT,其中,由于性能原因,已压缩的值存储在关系中,而无需重新压缩值。因此,即使关系的属性使用给定的压缩方法,也可能会混合使用多种压缩方法:

=# CREATE TABLE tab_compression_3 AS SELECT * FROM tab_compression_2;
SELECT 2
=# SELECT id, pg_column_compression(data) FROM tab_compression_3;
 id | pg_column_compression
----+-----------------------
  1 | pglz
  2 | lz4
(2 rows)
=# SHOW default_toast_compression;
 default_toast_compression
---------------------------
 pglz
(1 row)
=# \d+ tab_compression_3
                                    Table "public.tab_compression_3"
 Column |  Type   | Collation | Nullable | Default | Storage  | Compression | Stats target | Description
--------+---------+-----------+----------+---------+----------+-------------+--------------+-------------
 id     | integer |           |          |         | plain    |             |              |
 data   | text    |           |          |         | extended | pglz        |              |
Access method: heap

由CREATE TABLE AS 或SELECT INTO创建的表的属性所使用的压缩方法 不能在语法级别上强制执行。因此,唯一的解决方案是使用default_toast_compression,它将为需要TOAST的所有属性设置相同的压缩方法。之后可以使用ALTER TABLE查询来强制执行此操作,这对于逻辑转储很有用,以确保还原将数据压缩为新的压缩方法,因为转储包括原始的未压缩数据。

要注意的另一件事是pg_dump支持称为–no-toast-compression的选项,以不转储关系所使用的压缩方法。与default_toast_compression结合使用时,这对于清理在新群集上使用的压缩方法非常有帮助。

ALTER MATERIALIZED VIEW 支持SET COMPRESSION子句,尽管影响有限,但与CTAS的规则相同,并且REFRESH MATERIALIZED VIEW会将任何已压缩的值复制到实例化视图使用的物理文件中,而无需进行任何更改。

说到复制,即使备用数据库的PostgreSQL二进制文件不支持LZ4 ,也完全有可能在备用数据库的WAL记录上通过物理,流传输,复制在主数据库上重放用LZ4压缩的数据。尝试在表上读取此类值只会导致错误。通过应用更改的逻辑复制不受此影响,并且将使用其配置的压缩方法压缩值以将其应用于关系。

这确实是一个很酷的功能,很高兴看到PostgreSQL 14的这一部分为用户提供了更多选项。

文章来源:https://paquier.xyz/postgresql-2/postgres-14-table-compression/

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论