Postgresql Arch process

  1. 归档信息文件 .backup
  2. archive_status文件夹
  3. Postgresql arch process 进程解析

归档文件

000000010000000000000002.000000D0.backup

do_pg_stop_backup() 产生backup 信息文件

1566549837206

1
2
3
4
#define BackupHistoryFilePath(path, tli, logSegNo, offset)	\
snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
(uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)

Start WAL Location: offset;

1566783010245

archive_status作用

用于标记文件拷贝情况.

1566804346654

postgres: archiver process

postmaster.c:PostmasterMain()
1
pqsignal_no_restart(SIGCHLD, reaper);
postmaster.c:reaper()
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* reaper -- signal handler to cleanup after a child process dies;
*/
static void reaper(SIGNAL_ARGS);

//内部pgarch实现;
if (pid == PgArchPID)
{
PgArchPID = 0;
if (PgArchStartupAllowed())
PgArchPID = pgarch_start();
continue;
}
pgarch.c:pgarch_ArchiverCopyLoop()

使用.ready的archive_status循环遍历所有xlog并归档它们… 大多数情况下我们希望这是一个单独文件, 后端会将这些文件添加到需要归档的列表中. 而我们仍在复制早期的归档文件.

pgarch.c:pgarch_archiveXlog()

Invokes system(3) to copy one archive file to wherever it should go

Returns true if successful

1
rc = system(xlogarchcmd);

利用postgresql.conf中archive_command的参数. 进行系统拷贝;

pgarch.c:pgarch_archiveDone()

Emit notification that an xlog file has been successfully archived. We do this by renaming the status file from XXXX.ready to XXXX.done. Eventually, a checkpoint process will notice this and delete both the XXXX.done file and the xlog file itself. – 检查点进程将会清理xxx.done 和 xlog本身文件.

实现:

1
2
3
4
5
6
7
8
9
10
11
static void
pgarch_archiveDone(char *xlog)
{
char rlogready[MAXPGPATH];
char rlogdone[MAXPGPATH];

StatusFilePath(rlogready, xlog, ".ready");
StatusFilePath(rlogdone, xlog, ".done");

(void) durable_rename(rlogready, rlogdone, WARNING);
}
xlogarchve.c
  • XLogArchiveNotify
    • Create an archive notification file
  • XLogArchiveNotifySeg
    • Convenience routine to notify using segment number representation of filename
  • XLogArchiveForceDone
    • Emit notification forcibly that an XLOG segment file has been successfully archived, bu creating \.done regardless of whether \.ready exists or not.
  • XLogArchiveCheckDone
  • XLogArchiveIsBusy
    • Check to see if an XLOG segment file is still unarchived
  • XLogArchiveIsReadyOrDone
  • XLogArchiveIsReady
    • Check to see if an XLOG segment file has an archive notification (.ready) file.
  • XLogArchiveCleanup
    • Cleanup archive notification file(s) for a particular xlog segment
欣赏此文? 求鼓励,求支持!