summaryrefslogtreecommitdiffstats
path: root/src/fe_utils/archive.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:17:33 +0000
commit5e45211a64149b3c659b90ff2de6fa982a5a93ed (patch)
tree739caf8c461053357daa9f162bef34516c7bf452 /src/fe_utils/archive.c
parentInitial commit. (diff)
downloadpostgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.tar.xz
postgresql-15-5e45211a64149b3c659b90ff2de6fa982a5a93ed.zip
Adding upstream version 15.5.upstream/15.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/fe_utils/archive.c')
-rw-r--r--src/fe_utils/archive.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/fe_utils/archive.c b/src/fe_utils/archive.c
new file mode 100644
index 0000000..53d42c2
--- /dev/null
+++ b/src/fe_utils/archive.c
@@ -0,0 +1,109 @@
+/*-------------------------------------------------------------------------
+ *
+ * archive.c
+ * Routines to access WAL archives from frontend
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/fe_utils/archive.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres_fe.h"
+
+#include <unistd.h>
+#include <sys/stat.h>
+
+#include "access/xlog_internal.h"
+#include "common/archive.h"
+#include "common/logging.h"
+#include "fe_utils/archive.h"
+
+
+/*
+ * RestoreArchivedFile
+ *
+ * Attempt to retrieve the specified file from off-line archival storage.
+ * If successful, return a file descriptor of the restored file, else
+ * return -1.
+ *
+ * For fixed-size files, the caller may pass the expected size as an
+ * additional crosscheck on successful recovery. If the file size is not
+ * known, set expectedSize = 0.
+ */
+int
+RestoreArchivedFile(const char *path, const char *xlogfname,
+ off_t expectedSize, const char *restoreCommand)
+{
+ char xlogpath[MAXPGPATH];
+ char *xlogRestoreCmd;
+ int rc;
+ struct stat stat_buf;
+
+ snprintf(xlogpath, MAXPGPATH, "%s/" XLOGDIR "/%s", path, xlogfname);
+
+ xlogRestoreCmd = BuildRestoreCommand(restoreCommand, xlogpath,
+ xlogfname, NULL);
+ if (xlogRestoreCmd == NULL)
+ pg_fatal("cannot use restore_command with %%r placeholder");
+
+ /*
+ * Execute restore_command, which should copy the missing file from
+ * archival storage.
+ */
+ rc = system(xlogRestoreCmd);
+ pfree(xlogRestoreCmd);
+
+ if (rc == 0)
+ {
+ /*
+ * Command apparently succeeded, but let's make sure the file is
+ * really there now and has the correct size.
+ */
+ if (stat(xlogpath, &stat_buf) == 0)
+ {
+ if (expectedSize > 0 && stat_buf.st_size != expectedSize)
+ pg_fatal("unexpected file size for \"%s\": %lld instead of %lld",
+ xlogfname, (long long int) stat_buf.st_size,
+ (long long int) expectedSize);
+ else
+ {
+ int xlogfd = open(xlogpath, O_RDONLY | PG_BINARY, 0);
+
+ if (xlogfd < 0)
+ pg_fatal("could not open file \"%s\" restored from archive: %m",
+ xlogpath);
+ else
+ return xlogfd;
+ }
+ }
+ else
+ {
+ if (errno != ENOENT)
+ pg_fatal("could not stat file \"%s\": %m",
+ xlogpath);
+ }
+ }
+
+ /*
+ * If the failure was due to a signal, then it would be misleading to
+ * return with a failure at restoring the file. So just bail out and
+ * exit. Hard shell errors such as "command not found" are treated as
+ * fatal too.
+ */
+ if (wait_result_is_any_signal(rc, true))
+ pg_fatal("restore_command failed: %s",
+ wait_result_to_str(rc));
+
+ /*
+ * The file is not available, so just let the caller decide what to do
+ * next.
+ */
+ pg_log_error("could not restore file \"%s\" from archive",
+ xlogfname);
+ return -1;
+}