1 /*-------------------------------------------------------------------------
4 * Routines to access WAL archives from frontend
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/fe_utils/archive.c
13 *-------------------------------------------------------------------------
16 #include "postgres_fe.h"
21 #include "access/xlog_internal.h"
22 #include "common/archive.h"
23 #include "common/logging.h"
24 #include "fe_utils/archive.h"
30 * Attempt to retrieve the specified file from off-line archival storage.
31 * If successful, return a file descriptor of the restored file, else
34 * For fixed-size files, the caller may pass the expected size as an
35 * additional crosscheck on successful recovery. If the file size is not
36 * known, set expectedSize = 0.
39 RestoreArchivedFile(const char *path
, const char *xlogfname
,
40 off_t expectedSize
, const char *restoreCommand
)
42 char xlogpath
[MAXPGPATH
];
47 snprintf(xlogpath
, MAXPGPATH
, "%s/" XLOGDIR
"/%s", path
, xlogfname
);
49 xlogRestoreCmd
= BuildRestoreCommand(restoreCommand
, xlogpath
,
51 if (xlogRestoreCmd
== NULL
)
53 pg_log_fatal("cannot use restore_command with %%r placeholder");
58 * Execute restore_command, which should copy the missing file from
61 rc
= system(xlogRestoreCmd
);
62 pfree(xlogRestoreCmd
);
67 * Command apparently succeeded, but let's make sure the file is
68 * really there now and has the correct size.
70 if (stat(xlogpath
, &stat_buf
) == 0)
72 if (expectedSize
> 0 && stat_buf
.st_size
!= expectedSize
)
74 pg_log_fatal("unexpected file size for \"%s\": %lld instead of %lld",
75 xlogfname
, (long long int) stat_buf
.st_size
,
76 (long long int) expectedSize
);
81 int xlogfd
= open(xlogpath
, O_RDONLY
| PG_BINARY
, 0);
85 pg_log_fatal("could not open file \"%s\" restored from archive: %m",
97 pg_log_fatal("could not stat file \"%s\": %m",
105 * If the failure was due to a signal, then it would be misleading to
106 * return with a failure at restoring the file. So just bail out and
107 * exit. Hard shell errors such as "command not found" are treated as
110 if (wait_result_is_any_signal(rc
, true))
112 pg_log_fatal("restore_command failed: %s",
113 wait_result_to_str(rc
));
118 * The file is not available, so just let the caller decide what to do
121 pg_log_error("could not restore file \"%s\" from archive",