PostgreSQL Source Code  git master
basic_archive.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include "common/int.h"
#include "miscadmin.h"
#include "postmaster/pgarch.h"
#include "storage/copydir.h"
#include "storage/fd.h"
#include "utils/guc.h"
#include "utils/memutils.h"
Include dependency graph for basic_archive.c:

Go to the source code of this file.

Macros

#define CMP_BUF_SIZE   (4096)
 

Functions

static bool basic_archive_configured (void)
 
static bool basic_archive_file (const char *file, const char *path)
 
static void basic_archive_file_internal (const char *file, const char *path)
 
static bool check_archive_directory (char **newval, void **extra, GucSource source)
 
static bool compare_files (const char *file1, const char *file2)
 
void _PG_init (void)
 
void _PG_archive_module_init (ArchiveModuleCallbacks *cb)
 

Variables

 PG_MODULE_MAGIC
 
static char * archive_directory = NULL
 
static MemoryContext basic_archive_context
 

Macro Definition Documentation

◆ CMP_BUF_SIZE

#define CMP_BUF_SIZE   (4096)

Function Documentation

◆ _PG_archive_module_init()

void _PG_archive_module_init ( ArchiveModuleCallbacks cb)

Definition at line 82 of file basic_archive.c.

83 {
85 
88 }
void _PG_archive_module_init(ArchiveModuleCallbacks *cb)
Definition: basic_archive.c:82
static bool basic_archive_file(const char *file, const char *path)
static bool basic_archive_configured(void)
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:965
void(* ArchiveModuleInit)(ArchiveModuleCallbacks *cb)
Definition: pgarch.h:64
ArchiveFileCB archive_file_cb
Definition: pgarch.h:56
ArchiveCheckConfiguredCB check_configured_cb
Definition: pgarch.h:55

References ArchiveModuleCallbacks::archive_file_cb, AssertVariableIsOfType, basic_archive_configured(), basic_archive_file(), and ArchiveModuleCallbacks::check_configured_cb.

◆ _PG_init()

void _PG_init ( void  )

Definition at line 58 of file basic_archive.c.

59 {
60  DefineCustomStringVariable("basic_archive.archive_directory",
61  gettext_noop("Archive file destination directory."),
62  NULL,
64  "",
65  PGC_SIGHUP,
66  0,
67  check_archive_directory, NULL, NULL);
68 
69  MarkGUCPrefixReserved("basic_archive");
70 
72  "basic_archive",
74 }
static MemoryContext basic_archive_context
Definition: basic_archive.c:44
static char * archive_directory
Definition: basic_archive.c:43
static bool check_archive_directory(char **newval, void **extra, GucSource source)
Definition: basic_archive.c:96
#define gettext_noop(x)
Definition: c.h:1186
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:5023
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5084
@ PGC_SIGHUP
Definition: guc.h:71
MemoryContext TopMemoryContext
Definition: mcxt.c:141
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, archive_directory, basic_archive_context, check_archive_directory(), DefineCustomStringVariable(), gettext_noop, MarkGUCPrefixReserved(), PGC_SIGHUP, and TopMemoryContext.

◆ basic_archive_configured()

static bool basic_archive_configured ( void  )
static

Definition at line 138 of file basic_archive.c.

139 {
140  return archive_directory != NULL && archive_directory[0] != '\0';
141 }

References archive_directory.

Referenced by _PG_archive_module_init().

◆ basic_archive_file()

static bool basic_archive_file ( const char *  file,
const char *  path 
)
static

Definition at line 149 of file basic_archive.c.

150 {
151  sigjmp_buf local_sigjmp_buf;
152  MemoryContext oldcontext;
153 
154  /*
155  * We run basic_archive_file_internal() in our own memory context so that
156  * we can easily reset it during error recovery (thus avoiding memory
157  * leaks).
158  */
160 
161  /*
162  * Since the archiver operates at the bottom of the exception stack,
163  * ERRORs turn into FATALs and cause the archiver process to restart.
164  * However, using ereport(ERROR, ...) when there are problems is easy to
165  * code and maintain. Therefore, we create our own exception handler to
166  * catch ERRORs and return false instead of restarting the archiver
167  * whenever there is a failure.
168  */
169  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
170  {
171  /* Since not using PG_TRY, must reset error stack by hand */
172  error_context_stack = NULL;
173 
174  /* Prevent interrupts while cleaning up */
175  HOLD_INTERRUPTS();
176 
177  /* Report the error and clear ErrorContext for next time */
178  EmitErrorReport();
179  FlushErrorState();
180 
181  /* Close any files left open by copy_file() or compare_files() */
183 
184  /* Reset our memory context and switch back to the original one */
185  MemoryContextSwitchTo(oldcontext);
187 
188  /* Remove our exception handler */
189  PG_exception_stack = NULL;
190 
191  /* Now we can allow interrupts again */
193 
194  /* Report failure so that the archiver retries this file */
195  return false;
196  }
197 
198  /* Enable our exception handler */
199  PG_exception_stack = &local_sigjmp_buf;
200 
201  /* Archive the file! */
202  basic_archive_file_internal(file, path);
203 
204  /* Remove our exception handler */
205  PG_exception_stack = NULL;
206 
207  /* Reset our memory context and switch back to the original one */
208  MemoryContextSwitchTo(oldcontext);
210 
211  return true;
212 }
static void basic_archive_file_internal(const char *file, const char *path)
#define InvalidSubTransactionId
Definition: c.h:642
void EmitErrorReport(void)
Definition: elog.c:1669
ErrorContextCallback * error_context_stack
Definition: elog.c:95
void FlushErrorState(void)
Definition: elog.c:1825
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2932
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:314
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:134
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138

References AtEOSubXact_Files(), basic_archive_context, basic_archive_file_internal(), EmitErrorReport(), error_context_stack, FlushErrorState(), HOLD_INTERRUPTS, InvalidSubTransactionId, MemoryContextReset(), MemoryContextSwitchTo(), PG_exception_stack, and RESUME_INTERRUPTS.

Referenced by _PG_archive_module_init().

◆ basic_archive_file_internal()

static void basic_archive_file_internal ( const char *  file,
const char *  path 
)
static

Definition at line 215 of file basic_archive.c.

216 {
217  char destination[MAXPGPATH];
218  char temp[MAXPGPATH + 256];
219  struct stat st;
220  struct timeval tv;
221  uint64 epoch; /* milliseconds */
222 
223  ereport(DEBUG3,
224  (errmsg("archiving \"%s\" via basic_archive", file)));
225 
226  snprintf(destination, MAXPGPATH, "%s/%s", archive_directory, file);
227 
228  /*
229  * First, check if the file has already been archived. If it already
230  * exists and has the same contents as the file we're trying to archive,
231  * we can return success (after ensuring the file is persisted to disk).
232  * This scenario is possible if the server crashed after archiving the
233  * file but before renaming its .ready file to .done.
234  *
235  * If the archive file already exists but has different contents,
236  * something might be wrong, so we just fail.
237  */
238  if (stat(destination, &st) == 0)
239  {
240  if (compare_files(path, destination))
241  {
242  ereport(DEBUG3,
243  (errmsg("archive file \"%s\" already exists with identical contents",
244  destination)));
245 
246  fsync_fname(destination, false);
248 
249  return;
250  }
251 
252  ereport(ERROR,
253  (errmsg("archive file \"%s\" already exists", destination)));
254  }
255  else if (errno != ENOENT)
256  ereport(ERROR,
258  errmsg("could not stat file \"%s\": %m", destination)));
259 
260  /*
261  * Pick a sufficiently unique name for the temporary file so that a
262  * collision is unlikely. This helps avoid problems in case a temporary
263  * file was left around after a crash or another server happens to be
264  * archiving to the same directory.
265  */
266  gettimeofday(&tv, NULL);
267  if (pg_mul_u64_overflow((uint64) 1000, (uint64) tv.tv_sec, &epoch) ||
268  pg_add_u64_overflow(epoch, (uint64) (tv.tv_usec / 1000), &epoch))
269  elog(ERROR, "could not generate temporary file name for archiving");
270 
271  snprintf(temp, sizeof(temp), "%s/%s.%s.%d." UINT64_FORMAT,
272  archive_directory, "archtemp", file, MyProcPid, epoch);
273 
274  /*
275  * Copy the file to its temporary destination. Note that this will fail
276  * if temp already exists.
277  */
278  copy_file(path, temp);
279 
280  /*
281  * Sync the temporary file to disk and move it to its final destination.
282  * Note that this will overwrite any existing file, but this is only
283  * possible if someone else created the file since the stat() above.
284  */
285  (void) durable_rename(temp, destination, ERROR);
286 
287  ereport(DEBUG1,
288  (errmsg("archived \"%s\" via basic_archive", file)));
289 }
static bool compare_files(const char *file1, const char *file2)
#define UINT64_FORMAT
Definition: c.h:533
void copy_file(const char *fromfile, const char *tofile)
Definition: copydir.c:117
int errcode_for_file_access(void)
Definition: elog.c:881
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define DEBUG3
Definition: elog.h:28
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:688
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:662
int MyProcPid
Definition: globals.c:44
static bool pg_add_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:376
static bool pg_mul_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:410
#define MAXPGPATH
#define snprintf
Definition: port.h:238
#define stat
Definition: win32_port.h:286
static const unsigned __int64 epoch
int gettimeofday(struct timeval *tp, void *tzp)

References archive_directory, compare_files(), copy_file(), DEBUG1, DEBUG3, durable_rename(), elog(), epoch, ereport, errcode_for_file_access(), errmsg(), ERROR, fsync_fname(), gettimeofday(), MAXPGPATH, MyProcPid, pg_add_u64_overflow(), pg_mul_u64_overflow(), snprintf, stat, and UINT64_FORMAT.

Referenced by basic_archive_file().

◆ check_archive_directory()

static bool check_archive_directory ( char **  newval,
void **  extra,
GucSource  source 
)
static

Definition at line 96 of file basic_archive.c.

97 {
98  struct stat st;
99 
100  /*
101  * The default value is an empty string, so we have to accept that value.
102  * Our check_configured callback also checks for this and prevents
103  * archiving from proceeding if it is still empty.
104  */
105  if (*newval == NULL || *newval[0] == '\0')
106  return true;
107 
108  /*
109  * Make sure the file paths won't be too long. The docs indicate that the
110  * file names to be archived can be up to 64 characters long.
111  */
112  if (strlen(*newval) + 64 + 2 >= MAXPGPATH)
113  {
114  GUC_check_errdetail("Archive directory too long.");
115  return false;
116  }
117 
118  /*
119  * Do a basic sanity check that the specified archive directory exists. It
120  * could be removed at some point in the future, so we still need to be
121  * prepared for it not to exist in the actual archiving logic.
122  */
123  if (stat(*newval, &st) != 0 || !S_ISDIR(st.st_mode))
124  {
125  GUC_check_errdetail("Specified archive directory does not exist.");
126  return false;
127  }
128 
129  return true;
130 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:437
#define S_ISDIR(m)
Definition: win32_port.h:327

References GUC_check_errdetail, MAXPGPATH, newval, S_ISDIR, stat::st_mode, and stat.

Referenced by _PG_init().

◆ compare_files()

static bool compare_files ( const char *  file1,
const char *  file2 
)
static

Definition at line 297 of file basic_archive.c.

298 {
299 #define CMP_BUF_SIZE (4096)
300  char buf1[CMP_BUF_SIZE];
301  char buf2[CMP_BUF_SIZE];
302  int fd1;
303  int fd2;
304  bool ret = true;
305 
306  fd1 = OpenTransientFile(file1, O_RDONLY | PG_BINARY);
307  if (fd1 < 0)
308  ereport(ERROR,
310  errmsg("could not open file \"%s\": %m", file1)));
311 
312  fd2 = OpenTransientFile(file2, O_RDONLY | PG_BINARY);
313  if (fd2 < 0)
314  ereport(ERROR,
316  errmsg("could not open file \"%s\": %m", file2)));
317 
318  for (;;)
319  {
320  int nbytes = 0;
321  int buf1_len = 0;
322  int buf2_len = 0;
323 
324  while (buf1_len < CMP_BUF_SIZE)
325  {
326  nbytes = read(fd1, buf1 + buf1_len, CMP_BUF_SIZE - buf1_len);
327  if (nbytes < 0)
328  ereport(ERROR,
330  errmsg("could not read file \"%s\": %m", file1)));
331  else if (nbytes == 0)
332  break;
333 
334  buf1_len += nbytes;
335  }
336 
337  while (buf2_len < CMP_BUF_SIZE)
338  {
339  nbytes = read(fd2, buf2 + buf2_len, CMP_BUF_SIZE - buf2_len);
340  if (nbytes < 0)
341  ereport(ERROR,
343  errmsg("could not read file \"%s\": %m", file2)));
344  else if (nbytes == 0)
345  break;
346 
347  buf2_len += nbytes;
348  }
349 
350  if (buf1_len != buf2_len || memcmp(buf1, buf2, buf1_len) != 0)
351  {
352  ret = false;
353  break;
354  }
355  else if (buf1_len == 0)
356  break;
357  }
358 
359  if (CloseTransientFile(fd1) != 0)
360  ereport(ERROR,
362  errmsg("could not close file \"%s\": %m", file1)));
363 
364  if (CloseTransientFile(fd2) != 0)
365  ereport(ERROR,
367  errmsg("could not close file \"%s\": %m", file2)));
368 
369  return ret;
370 }
#define CMP_BUF_SIZE
#define PG_BINARY
Definition: c.h:1260
int CloseTransientFile(int fd)
Definition: fd.c:2609
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2433
#define read(a, b, c)
Definition: win32.h:13

References CloseTransientFile(), CMP_BUF_SIZE, ereport, errcode_for_file_access(), errmsg(), ERROR, OpenTransientFile(), PG_BINARY, and read.

Referenced by basic_archive_file_internal().

Variable Documentation

◆ archive_directory

char* archive_directory = NULL
static

Definition at line 43 of file basic_archive.c.

Referenced by _PG_init(), basic_archive_configured(), and basic_archive_file_internal().

◆ basic_archive_context

MemoryContext basic_archive_context
static

Definition at line 44 of file basic_archive.c.

Referenced by _PG_init(), and basic_archive_file().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 41 of file basic_archive.c.