postgres unlogged表的数据丢失问题
一环境
数据库版本Postgres 9.5.1
操作系统系统:centos 6.5 64
二问题发现结果
(1)一个unlogged表(不管是否checkpoint ),当数据库异常关机重启后,该表数据被清空。
(2) 一个unlogged表,插入数据,切换日志,之后用pg_rman备份数据库与归档日志,然后正常关闭数据库,利用备份还原恢复数据库时,会把unlogged表中的数据清空。
(3)利用pg_dump逻辑导出unlogged表数据时,如果采用文本文件方式可以发现,其定义创建语句也是unlogged方式,恢复时请注意。
注意
大家是不是要小心使用unlogged表了-能提升数据插入时的性能,但是插入完成一定记得改为logged表。
三实验
postgres版本
PostgreSQL9.5.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (RedHat 4.4.7-4), 64-bit
(1row)
3.1测试1
创建普通表和日志表,插入数据测试(checkpoint ),正常关闭数据库。
结果:普通表和日志表数据都不丢失
//建表 test=# create table test ( id integer, name text); //插入数据测试(不checkpoint ) test=# insert into test select generate_series(1,10000),'test'; INSERT 0 10000 test=# insert into test_unlogged select generate_series(1,10000),'test'; INSERT 0 10000 //正常关机重启后查数据 pg_ctl -D /pgdb/data stop; test=# select count(*) from test; count ------- 10000 (1 row) test=# select count(*) from test_unlogged; count ------- 10000 (1 row) |
3.2测试2
创建普通表和日志表
插入数据测试(不checkpoint )
异常关闭数据库pg_ctl -D /pgdb/data stop -m immediate
操作过程同上。
结果:异常关闭数据库重启后unlogged表test_unlogged数据清空;logged表test数据不丢失。
3.3测试3
创建普通表和日志表
插入数据测试(checkpoint )
异常关闭数据库pg_ctl -D /pgdb/data stop -m immediate,之后重启
操作步骤同上
结果:异常关闭数据库重启后,unlogged表test_unlogged数据无;logged表test数据不丢失。
test=# drop table test; test=# drop table test_unlogged; test=# create unlogged table test_unlogged ( id integer, name text); test=# create table test ( id integer, name text); test=# select relname,relpersistence from pg_class where relname like 'test%'; relname | relpersistence ---------------+---------------- test | p test_unlogged | u test=# insert into test_unlogged select generate_series(1,10000),'test'; INSERT 0 10000 Time: 6.687 ms //可以看出插入时间 test=# insert into test select generate_series(1,10000),'test'; INSERT 0 10000 Time: 48.511 ms //可以看出插入时间 test=# checkpoint; CHECKPOINT Time: 100.727 ms //此处异常关闭数据库pg_ctl -D /pgdb/data stop -m immediate,并重启 test=# \q [postgres@pg1 ~]$ psql -d test -U dba -p 5432 psql (9.5.1) Type "help" for help. test=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+------- public | test | table | dba public | test_unlogged | table | dba (2 rows) test=# select count(*) from test; count ------- 10000 (1 row) test=# select count(*) from test_unlogged; count ------- 0 (1 row) |
3.4测试4
创建unlogged表test_unlogged
插入数据,正常关机,之后,test_unlogged数据正常不丢失,
然后再次插入数据,异常关机,重启之后unlogged表test