pg_backup_start()

准备服务器进行在线备份的函数

pg_backup_start() 是一个系统函数,它将服务器置于适合进行在线备份的状态。

pg_backup_start()PostgreSQL 15 中添加,取代了 pg_start_backup()

用法

pg_backup_start ( label text [, fast boolean ] ) → pg_lsn

label 参数是必须的,但可以是任何任意值。

fast 参数(默认值:false)确定是否应强制执行立即 检查点。这将导致 I/O 尖峰,但会导致函数执行速度更快。

在线备份状态将保持活动状态,直到执行 pg_backup_stop() 或会话终止,在这种情况下,备份状态将被中止。

更改历史

示例

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()?

分类

备份系统函数

另请参阅

pg_backup_stop()pg_basebackup

反馈

提交关于 "pg_backup_start()" 的任何评论、建议或更正 此处