pg_backup_start() 是一个系统函数,它将服务器置于适合进行在线备份的状态。
pg_backup_start() 在 PostgreSQL 15 中被添加,取代了 pg_start_backup()。
用法
pg_backup_start (labeltext[,fastboolean] ) →pg_lsn
label 参数是必需的,但可以是任何任意值。
fast 参数(默认值:false)决定了是否强制立即执行 检查点。这将导致 I/O 峰值,但会加快函数的执行速度。
在线备份状态将一直保持活跃,直到执行 pg_backup_stop() 或会话终止,在这种情况下,备份状态将被中止。
变更历史
- PostgreSQL 15
- 添加于 (commit 58c41712)
示例
pg_backup_start() 的基本执行示例
postgres=# SELECT pg_backup_start('foo');
pg_backup_start
-----------------
0/2000028
(1 row)
Time: 7974.096 ms (00:07.974)
将 fast 选项设置为 true 的执行将(假设最近没有完成检查点)导致更快的执行。
postgres=# SELECT pg_backup_start('foo', fast := true);
pg_backup_start
-----------------
0/2000028
(1 row)
Time: 50.964 ms
执行第一个示例时 PostgreSQL 的日志输出
[2022-05-13 15:55:32 UTC] psql postgres postgres LOG: 00000: statement: SELECT pg_backup_start('foo');
[2022-05-13 15:55:32 UTC] LOG: 00000: checkpoint starting: force wait
[2022-05-13 15:55:40 UTC] LOG: 00000: checkpoint complete: wrote 83 buffers (0.1%); 0 WAL file(s) added, 0 removed, 1 recycled; write=7.941 s, sync=0.001 s, total=7.956 s; sync files=0, longest=0.000 s, average=0.000 s; distance=11672 kB, estimate=11672 kB
与第二个示例相比
[2022-05-13 15:57:01 UTC] psql postgres postgres LOG: 00000: statement: SELECT pg_backup_start('foo', fast := true);
[2022-05-13 15:57:01 UTC] LOG: 00000: checkpoint starting: immediate force wait
[2022-05-13 15:57:01 UTC] LOG: 00000: checkpoint complete: wrote 83 buffers (0.1%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.008 s, sync=0.001 s, total=0.021 s; sync files=0, longest=0.000 s, average=0.000 s; distance=11672 kB, estimate=11672 kB
在同一个会话中不允许启动多个备份。
postgres=# SELECT pg_backup_start('foo', fast := true);
pg_backup_start
-----------------
0/3000028
(1 row)
postgres=# SELECT pg_backup_start('foo');
ERROR: a backup is already in progress in this session
postgres=# SELECT pg_backup_start('bar');
ERROR: a backup is already in progress in this session
如果在未调用 pg_backup_stop() 的情况下终止会话,备份将被中止。
[2022-05-13 16:05:25 UTC] psql postgres postgres WARNING: 01000: aborting backup due to backend exiting before pg_backup_stop was called [2022-05-13 16:05:25 UTC] psql postgres postgres LOG: 00000: disconnection: session time: 0:01:41.427 user=postgres database=postgres host=127.0.0.1 port=58848
如果提供的 label 名称为 NULL,则返回 NULL,服务器不会被置于适合进行在线备份的状态。
postgres=# SELECT pg_backup_start(NULL); pg_backup_start ----------------- (1 row) postgres=# SELECT pg_backup_stop(); ERROR: backup is not in progress HINT: Did you call pg_backup_start()?
参考资料
- PostgreSQL 文档: 备份控制函数
