postgresql - Postgresql database size is lower after restore
问题描述
I just made a simple bash script to backup a postgresql database from remote server and restore it to the same server with prefix _bak. This script looks like:
export FILENAME="/var/pg-backups/$4-$(date +%s).bak"
echo "$(date) Creating dump with args - $1 $2 $3 $4. Filename = $FILENAME"
pg_dump -h $1 -p $2 -U $3 -b -F d -j 4 -v -f "$FILENAME" $4
echo "$(date) Re-create _bak database"
psql -h $1 -p $2 -U $3 -d $4 -c "drop database if exists $4_bak;"
psql -h $1 -p $2 -U $3 -d $4 -c "create database $4_bak;"
echo "$(date) Restoring hot copy with _bak postfix"
pg_restore -h $1 -p $2 -U $3 -d "$4_bak" -w -v "$FILENAME"
echo "$(date) Done"
It works well, but there's some strange thing with _bak database, which was restored - it weights lower than original!! Here's the output of pg_database_size for original (backuped) database and its restored copy:
postgres=# select pg_database_size('somedb');
pg_database_size
------------------
548152175
postgres=# select pg_database_size('somedb_bak');
pg_database_size
------------------
511648623
(1 строка)
Also, I took one table as an example to check relation size difference, with such query:
select pg_relation_size('resources.attachment', 'main') as main,
pg_relation_size('resources.attachment', 'fsm') as fsm,
pg_relation_size('resources.attachment', 'vm') as vm,
pg_relation_size('resources.attachment', 'init') as init,
pg_table_size('resources.attachment'), pg_indexes_size('resources.attachment') as indexes,
pg_total_relation_size('resources.attachment') as total;
and here's what I got for original database:
main | fsm | vm | init | pg_table_size | indexes | total
-------+-------+------+------+---------------+---------+-----------
65536 | 24576 | 8192 | 0 | 109158400 | 32768 | 109191168
and backuped version:
main | fsm | vm | init | pg_table_size | indexes | total
-------+-------+----+------+---------------+---------+-----------
65536 | 24576 | 0 | 0 | 100302848 | 32768 | 100335616
So only pg_table_size and VM differs... records count is the same btw in original and backuped table.
Can someone please try to explain the size difference between original and restored database?
Postgresql verison:
PostgreSQL 12.7 (Debian 12.7-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
P.S.
i've also tried -F c without parallelism - same result
解决方案
推荐阅读
- go - 用函数查找字符串
- reactjs - React-Redux 操作未正确分派?
- python - 无法将正确答案附加到python中的列表
- javascript - 如何使 div 成为可滚动窗口的大小,而不是屏幕
- gremlin - Gremlin - 用有意义的信息显示最短(最低成本)的路径
- python-3.x - 通过在熊猫中使用 groupby 保持行
- assembly - 试图了解 x86 上 alloca() 函数的汇编实现
- c - 如果服务器断开连接,c 中的套接字编程不会引发任何错误
- gatsby - 如何在 gatsby frontmatter 中使用单引号?
- swift - 不能在 Firebase 默认 authUI 中使用静默 APN,总是例外