UNLOGGED表陷阱
一、问题
为了加快PostgreSQL或者GPDB数据加载速度,将数据表加上了 UNLOGGED 属性,以禁止WAL(xlog)。但是,我们发现当postgres进程非正常退出,重启服务后,UNLOGGED 表数据全部丢失,发生故障前的未操作过的UNLOGGED 表数据也全部丢失,数据从磁盘上彻底消失。
现象如同“删库跑路”。
二、验证
第1步:执行以下SQL:
- snippet.sql
[postgres@yz219 ~]$ createdb test; [postgres@yz219 ~]$ psql -d test test=# CREATE unlogged TABLE t(a VARCHAR(16), b VARCHAR(200)); test=# INSERT INTO t VALUES ('1', '2'); test=# INSERT INTO t VALUES ('3', '4'); test=# SELECT * FROM t; test=# \q
第2步,强制杀掉postgres进程。
- snippet.bash
killall -9 postgres
第3步,重新启动postgres服务。
第4步,验证数据表。数据表变成空表了:
- snippet.sql
SELECT * FROM t;
三、文档解释
UNLOGGED
If specified, the table is created as an unlogged table. Data written to unlogged tables is not written to the write-ahead log (see Chapter 29, which makes them considerably faster than ordinary tables. However, they are not crash-safe: an unlogged table is automatically truncated after a crash or unclean shutdown. The contents of an unlogged table are also not replicated to standby servers. Any indexes created on an unlogged table are automatically unlogged as well.
这份说明显然没有充分说要使用UNLOGGED属性的灾难性。
该问题经验证存在于PostgreSQL 12.3,GPDB 6.13版本,其它版本估计也难以幸免。
因此,
在生产环境中必须禁止使用UNLOGGED表!!!
打赏作者以资鼓励:
![]() | ![]() |