postgres@hqdc034:~$ psql -c 'with aa as ( select * from generate_series (1,1000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
INSERT 0 1000000
postgres@hqdc034:~$ psql -c 'select count(*) from test;'
count
---------
1000000
(1 row)
postgres@hqdc034:~$
postgres@hqdc034:~$ time psql -c 'with aa as ( select * from generate_series (1,1000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
INSERT 0 1000000
real 0m4.276s
user 0m0.020s
sys 0m0.012s
postgres@hqdc034:~$ psql -c 'select count(*) from test;'
count
---------
2000000
(1 row)
postgres@hqdc034:~$
postgres@hqdc034:~$ time psql -c 'with aa as ( select * from generate_series (1,8000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
INSERT 0 8000000
real 0m32.172s
user 0m0.024s
sys 0m0.008s
postgres@hqdc034:~$ psql -c 'select count(*) from test;'
count
----------
10000000
(1 row)
postgres@hqdc034:~$
postgres@hqdc034:~$ du -sh /zp/database/10/main/
3.0G /zp/database/10/main/
postgres@hqdc034:~$
還原測試
最後一次做快照的時候,demo DB 裡面有一千萬筆資料,現在來砍掉500萬筆
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
postgres@hqdc034:~$ time psql -c 'delete from test where a > 5000000;'
DELETE 3000000
real 0m7.844s
user 0m0.024s
sys 0m0.004s
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
count
---------
7000000
(1 row)
real 0m0.268s
user 0m0.024s
sys 0m0.004s
postgres@hqdc034:~$
2019-09-06 09:25:21 [changch@hqdc034 ~]$ sudo service postgresql stop
* Stopping PostgreSQL 10 database server [ OK ]
2019-09-06 10:14:12 [changch@hqdc034 ~]$ sudo zfs rollback -r zp/database@demo_db_test_table_with_10M_rows
2019-09-06 10:14:28 [changch@hqdc034 ~]$ sudo service postgresql start
* Starting PostgreSQL 10 database server [ OK ]
2019-09-06 10:14:57 [changch@hqdc034 ~]$
檢查一下
1
2
3
4
5
6
7
8
9
10
11
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
count
----------
10000000
(1 row)
real 0m5.019s
user 0m0.040s
sys 0m0.008s
postgres@hqdc034:~$
postgres@hqdc034:~$ time psql -c 'with aa as ( select * from generate_series (1,8000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
INSERT 0 8000000
real 0m35.662s
user 0m0.048s
sys 0m0.004s
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
count
----------
10000000
(1 row)
real 0m5.259s
user 0m0.024s
sys 0m0.008s
postgres@hqdc034:~$
Clones can only be created from a snapshot and a snapshot can not
be deleted until you delete the clone that is based on this snapshot.
To create a clone, use the zfs clone command.
#data_directory = '/var/lib/postgresql/10/main' # use data in another directory
#data_directory = '/zp/database/10/main'
data_directory = '/zp/database/clone_with_10M_rows/10/main'
啟動有比較久一點 而且好像沒成功啟動
1
2
3
4
2019-09-06 10:32:27 [changch@hqdc034 ~]$ sudo service postgresql restart
* Restarting PostgreSQL 10 database server [ OK ]
2019-09-06 10:33:37 [changch@hqdc034 ~]$
2019-09-06 10:33:37 [changch@hqdc034 ~]$ sudo netstat -antlp |grep 5432
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
count
----------
10000000
(1 row)
real 0m4.716s
user 0m0.028s
sys 0m0.004s
postgres@hqdc034:~$