PostgreSQL Source Code  git master
backup_manifest.h File Reference
Include dependency graph for backup_manifest.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  backup_manifest_info
 

Typedefs

typedef enum manifest_option backup_manifest_option
 
typedef struct backup_manifest_info backup_manifest_info
 

Enumerations

enum  manifest_option { MANIFEST_OPTION_YES , MANIFEST_OPTION_NO , MANIFEST_OPTION_FORCE_ENCODE }
 

Functions

void InitializeBackupManifest (backup_manifest_info *manifest, backup_manifest_option want_manifest, pg_checksum_type manifest_checksum_type)
 
void AddFileToBackupManifest (backup_manifest_info *manifest, const char *spcoid, const char *pathname, size_t size, pg_time_t mtime, pg_checksum_context *checksum_ctx)
 
void AddWALInfoToBackupManifest (backup_manifest_info *manifest, XLogRecPtr startptr, TimeLineID starttli, XLogRecPtr endptr, TimeLineID endtli)
 
void SendBackupManifest (backup_manifest_info *manifest, bbsink *sink)
 
void FreeBackupManifest (backup_manifest_info *manifest)
 

Typedef Documentation

◆ backup_manifest_info

◆ backup_manifest_option

Enumeration Type Documentation

◆ manifest_option

Enumerator
MANIFEST_OPTION_YES 
MANIFEST_OPTION_NO 
MANIFEST_OPTION_FORCE_ENCODE 

Definition at line 20 of file backup_manifest.h.

21 {
@ MANIFEST_OPTION_NO
@ MANIFEST_OPTION_FORCE_ENCODE
@ MANIFEST_OPTION_YES
enum manifest_option backup_manifest_option

Function Documentation

◆ AddFileToBackupManifest()

void AddFileToBackupManifest ( backup_manifest_info manifest,
const char *  spcoid,
const char *  pathname,
size_t  size,
pg_time_t  mtime,
pg_checksum_context checksum_ctx 
)

Definition at line 100 of file backup_manifest.c.

103 {
104  char pathbuf[MAXPGPATH];
105  int pathlen;
107 
109  return;
110 
111  /*
112  * If this file is part of a tablespace, the pathname passed to this
113  * function will be relative to the tar file that contains it. We want the
114  * pathname relative to the data directory (ignoring the intermediate
115  * symlink traversal).
116  */
117  if (spcoid != NULL)
118  {
119  snprintf(pathbuf, sizeof(pathbuf), "pg_tblspc/%s/%s", spcoid,
120  pathname);
121  pathname = pathbuf;
122  }
123 
124  /*
125  * Each file's entry needs to be separated from any entry that follows by
126  * a comma, but there's no comma before the first one or after the last
127  * one. To make that work, adding a file to the manifest starts by
128  * terminating the most recently added line, with a comma if appropriate,
129  * but does not terminate the line inserted for this file.
130  */
132  if (manifest->first_file)
133  {
134  appendStringInfoChar(&buf, '\n');
135  manifest->first_file = false;
136  }
137  else
138  appendStringInfoString(&buf, ",\n");
139 
140  /*
141  * Write the relative pathname to this file out to the manifest. The
142  * manifest is always stored in UTF-8, so we have to encode paths that are
143  * not valid in that encoding.
144  */
145  pathlen = strlen(pathname);
146  if (!manifest->force_encode &&
147  pg_verify_mbstr(PG_UTF8, pathname, pathlen, true))
148  {
149  appendStringInfoString(&buf, "{ \"Path\": ");
150  escape_json(&buf, pathname);
151  appendStringInfoString(&buf, ", ");
152  }
153  else
154  {
155  appendStringInfoString(&buf, "{ \"Encoded-Path\": \"");
156  enlargeStringInfo(&buf, 2 * pathlen);
157  buf.len += hex_encode(pathname, pathlen,
158  &buf.data[buf.len]);
159  appendStringInfoString(&buf, "\", ");
160  }
161 
162  appendStringInfo(&buf, "\"Size\": %zu, ", size);
163 
164  /*
165  * Convert last modification time to a string and append it to the
166  * manifest. Since it's not clear what time zone to use and since time
167  * zone definitions can change, possibly causing confusion, use GMT
168  * always.
169  */
170  appendStringInfoString(&buf, "\"Last-Modified\": \"");
171  enlargeStringInfo(&buf, 128);
172  buf.len += pg_strftime(&buf.data[buf.len], 128, "%Y-%m-%d %H:%M:%S %Z",
173  pg_gmtime(&mtime));
174  appendStringInfoChar(&buf, '"');
175 
176  /* Add checksum information. */
177  if (checksum_ctx->type != CHECKSUM_TYPE_NONE)
178  {
179  uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
180  int checksumlen;
181 
182  checksumlen = pg_checksum_final(checksum_ctx, checksumbuf);
183  if (checksumlen < 0)
184  elog(ERROR, "could not finalize checksum of file \"%s\"",
185  pathname);
186 
188  ", \"Checksum-Algorithm\": \"%s\", \"Checksum\": \"",
189  pg_checksum_type_name(checksum_ctx->type));
190  enlargeStringInfo(&buf, 2 * checksumlen);
191  buf.len += hex_encode((char *) checksumbuf, checksumlen,
192  &buf.data[buf.len]);
193  appendStringInfoChar(&buf, '"');
194  }
195 
196  /* Close out the object. */
197  appendStringInfoString(&buf, " }");
198 
199  /* OK, add it to the manifest. */
201 
202  /* Avoid leaking memory. */
203  pfree(buf.data);
204 }
static void AppendStringToManifest(backup_manifest_info *manifest, char *s)
static bool IsManifestEnabled(backup_manifest_info *manifest)
unsigned char uint8
Definition: c.h:439
char * pg_checksum_type_name(pg_checksum_type type)
int pg_checksum_final(pg_checksum_context *context, uint8 *output)
#define PG_CHECKSUM_MAX_LENGTH
@ CHECKSUM_TYPE_NONE
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
uint64 hex_encode(const char *src, size_t len, char *dst)
Definition: encode.c:161
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1587
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1515
void pfree(void *pointer)
Definition: mcxt.c:1175
static bool manifest
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
@ PG_UTF8
Definition: pg_wchar.h:230
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:128
#define snprintf
Definition: port.h:225
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
pg_checksum_type type

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), AppendStringToManifest(), buf, CHECKSUM_TYPE_NONE, elog, enlargeStringInfo(), ERROR, escape_json(), hex_encode(), initStringInfo(), IsManifestEnabled(), manifest, MAXPGPATH, pfree(), pg_checksum_final(), PG_CHECKSUM_MAX_LENGTH, pg_checksum_type_name(), pg_gmtime(), pg_strftime(), PG_UTF8, pg_verify_mbstr(), snprintf, and pg_checksum_context::type.

Referenced by sendFile(), and sendFileWithContent().

◆ AddWALInfoToBackupManifest()

void AddWALInfoToBackupManifest ( backup_manifest_info manifest,
XLogRecPtr  startptr,
TimeLineID  starttli,
XLogRecPtr  endptr,
TimeLineID  endtli 
)

Definition at line 211 of file backup_manifest.c.

214 {
215  List *timelines;
216  ListCell *lc;
217  bool first_wal_range = true;
218  bool found_start_timeline = false;
219 
221  return;
222 
223  /* Terminate the list of files. */
224  AppendStringToManifest(manifest, "\n],\n");
225 
226  /* Read the timeline history for the ending timeline. */
227  timelines = readTimeLineHistory(endtli);
228 
229  /* Start a list of LSN ranges. */
230  AppendStringToManifest(manifest, "\"WAL-Ranges\": [\n");
231 
232  foreach(lc, timelines)
233  {
234  TimeLineHistoryEntry *entry = lfirst(lc);
235  XLogRecPtr tl_beginptr;
236 
237  /*
238  * We only care about timelines that were active during the backup.
239  * Skip any that ended before the backup started. (Note that if
240  * entry->end is InvalidXLogRecPtr, it means that the timeline has not
241  * yet ended.)
242  */
243  if (!XLogRecPtrIsInvalid(entry->end) && entry->end < startptr)
244  continue;
245 
246  /*
247  * Because the timeline history file lists newer timelines before
248  * older ones, the first timeline we encounter that is new enough to
249  * matter ought to match the ending timeline of the backup.
250  */
251  if (first_wal_range && endtli != entry->tli)
252  ereport(ERROR,
253  errmsg("expected end timeline %u but found timeline %u",
254  starttli, entry->tli));
255 
256  /*
257  * If this timeline entry matches with the timeline on which the
258  * backup started, WAL needs to be checked from the start LSN of the
259  * backup. If this entry refers to a newer timeline, WAL needs to be
260  * checked since the beginning of this timeline, so use the LSN where
261  * the timeline began.
262  */
263  if (starttli == entry->tli)
264  tl_beginptr = startptr;
265  else
266  {
267  tl_beginptr = entry->begin;
268 
269  /*
270  * If we reach a TLI that has no valid beginning LSN, there can't
271  * be any more timelines in the history after this point, so we'd
272  * better have arrived at the expected starting TLI. If not,
273  * something's gone horribly wrong.
274  */
275  if (XLogRecPtrIsInvalid(entry->begin))
276  ereport(ERROR,
277  errmsg("expected start timeline %u but found timeline %u",
278  starttli, entry->tli));
279  }
280 
282  "%s{ \"Timeline\": %u, \"Start-LSN\": \"%X/%X\", \"End-LSN\": \"%X/%X\" }",
283  first_wal_range ? "" : ",\n",
284  entry->tli,
285  LSN_FORMAT_ARGS(tl_beginptr),
286  LSN_FORMAT_ARGS(endptr));
287 
288  if (starttli == entry->tli)
289  {
290  found_start_timeline = true;
291  break;
292  }
293 
294  endptr = entry->begin;
295  first_wal_range = false;
296  }
297 
298  /*
299  * The last entry in the timeline history for the ending timeline should
300  * be the ending timeline itself. Verify that this is what we observed.
301  */
302  if (!found_start_timeline)
303  ereport(ERROR,
304  errmsg("start timeline %u not found in history of timeline %u",
305  starttli, endtli));
306 
307  /* Terminate the list of WAL ranges. */
308  AppendStringToManifest(manifest, "\n],\n");
309 }
List * readTimeLineHistory(TimeLineID targetTLI)
Definition: timeline.c:76
#define AppendToManifest(manifest,...)
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
#define lfirst(lc)
Definition: pg_list.h:169
Definition: pg_list.h:51
XLogRecPtr begin
Definition: timeline.h:28
TimeLineID tli
Definition: timeline.h:27
XLogRecPtr end
Definition: timeline.h:29
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References AppendStringToManifest(), AppendToManifest, TimeLineHistoryEntry::begin, TimeLineHistoryEntry::end, ereport, errmsg(), ERROR, IsManifestEnabled(), lfirst, LSN_FORMAT_ARGS, manifest, readTimeLineHistory(), TimeLineHistoryEntry::tli, and XLogRecPtrIsInvalid.

Referenced by perform_base_backup().

◆ FreeBackupManifest()

void FreeBackupManifest ( backup_manifest_info manifest)

Definition at line 90 of file backup_manifest.c.

91 {
92  pg_cryptohash_free(manifest->manifest_ctx);
93  manifest->manifest_ctx = NULL;
94 }
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:243

References manifest, and pg_cryptohash_free().

Referenced by perform_base_backup().

◆ InitializeBackupManifest()

void InitializeBackupManifest ( backup_manifest_info manifest,
backup_manifest_option  want_manifest,
pg_checksum_type  manifest_checksum_type 
)

Definition at line 57 of file backup_manifest.c.

60 {
61  memset(manifest, 0, sizeof(backup_manifest_info));
62  manifest->checksum_type = manifest_checksum_type;
63 
64  if (want_manifest == MANIFEST_OPTION_NO)
65  manifest->buffile = NULL;
66  else
67  {
68  manifest->buffile = BufFileCreateTemp(false);
69  manifest->manifest_ctx = pg_cryptohash_create(PG_SHA256);
70  if (pg_cryptohash_init(manifest->manifest_ctx) < 0)
71  elog(ERROR, "failed to initialize checksum of backup manifest: %s",
72  pg_cryptohash_error(manifest->manifest_ctx));
73  }
74 
75  manifest->manifest_size = UINT64CONST(0);
76  manifest->force_encode = (want_manifest == MANIFEST_OPTION_FORCE_ENCODE);
77  manifest->first_file = true;
78  manifest->still_checksumming = true;
79 
80  if (want_manifest != MANIFEST_OPTION_NO)
82  "{ \"PostgreSQL-Backup-Manifest-Version\": 1,\n"
83  "\"Files\": [");
84 }
BufFile * BufFileCreateTemp(bool interXact)
Definition: buffile.c:188
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:101
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:259
@ PG_SHA256
Definition: cryptohash.h:24

References AppendToManifest, BufFileCreateTemp(), elog, ERROR, manifest, MANIFEST_OPTION_FORCE_ENCODE, MANIFEST_OPTION_NO, pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_init(), and PG_SHA256.

Referenced by perform_base_backup().

◆ SendBackupManifest()

void SendBackupManifest ( backup_manifest_info manifest,
bbsink sink 
)

Definition at line 315 of file backup_manifest.c.

316 {
317  uint8 checksumbuf[PG_SHA256_DIGEST_LENGTH];
318  char checksumstringbuf[PG_SHA256_DIGEST_STRING_LENGTH];
319  size_t manifest_bytes_done = 0;
320 
322  return;
323 
324  /*
325  * Append manifest checksum, so that the problems with the manifest itself
326  * can be detected.
327  *
328  * We always use SHA-256 for this, regardless of what algorithm is chosen
329  * for checksumming the files. If we ever want to make the checksum
330  * algorithm used for the manifest file variable, the client will need a
331  * way to figure out which algorithm to use as close to the beginning of
332  * the manifest file as possible, to avoid having to read the whole thing
333  * twice.
334  */
335  manifest->still_checksumming = false;
336  if (pg_cryptohash_final(manifest->manifest_ctx, checksumbuf,
337  sizeof(checksumbuf)) < 0)
338  elog(ERROR, "failed to finalize checksum of backup manifest: %s",
339  pg_cryptohash_error(manifest->manifest_ctx));
340  AppendStringToManifest(manifest, "\"Manifest-Checksum\": \"");
341 
342  hex_encode((char *) checksumbuf, sizeof checksumbuf, checksumstringbuf);
343  checksumstringbuf[PG_SHA256_DIGEST_STRING_LENGTH - 1] = '\0';
344 
345  AppendStringToManifest(manifest, checksumstringbuf);
347 
348  /*
349  * We've written all the data to the manifest file. Rewind the file so
350  * that we can read it all back.
351  */
352  if (BufFileSeek(manifest->buffile, 0, 0L, SEEK_SET))
353  ereport(ERROR,
355  errmsg("could not rewind temporary file")));
356 
357 
358  /*
359  * Send the backup manifest.
360  */
361  bbsink_begin_manifest(sink);
362  while (manifest_bytes_done < manifest->manifest_size)
363  {
364  size_t bytes_to_read;
365  size_t rc;
366 
367  bytes_to_read = Min(sink->bbs_buffer_length,
368  manifest->manifest_size - manifest_bytes_done);
369  rc = BufFileRead(manifest->buffile, sink->bbs_buffer,
370  bytes_to_read);
371  if (rc != bytes_to_read)
372  ereport(ERROR,
374  errmsg("could not read from temporary file: %m")));
375  bbsink_manifest_contents(sink, bytes_to_read);
376  manifest_bytes_done += bytes_to_read;
377  }
378  bbsink_end_manifest(sink);
379 
380  /* Release resources */
381  BufFileClose(manifest->buffile);
382 }
static void bbsink_begin_manifest(bbsink *sink)
static void bbsink_end_manifest(bbsink *sink)
static void bbsink_manifest_contents(bbsink *sink, size_t len)
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:582
int BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
Definition: buffile.c:689
void BufFileClose(BufFile *file)
Definition: buffile.c:407
#define Min(x, y)
Definition: c.h:986
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:177
int errcode_for_file_access(void)
Definition: elog.c:716
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
#define PG_SHA256_DIGEST_STRING_LENGTH
Definition: sha2.h:24
char * bbs_buffer
size_t bbs_buffer_length

References AppendStringToManifest(), bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_begin_manifest(), bbsink_end_manifest(), bbsink_manifest_contents(), BufFileClose(), BufFileRead(), BufFileSeek(), elog, ereport, errcode_for_file_access(), errmsg(), ERROR, hex_encode(), IsManifestEnabled(), manifest, Min, pg_cryptohash_error(), pg_cryptohash_final(), PG_SHA256_DIGEST_LENGTH, and PG_SHA256_DIGEST_STRING_LENGTH.

Referenced by perform_base_backup().