PostgreSQL Source Code  git master
basebackup_server.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "catalog/pg_authid.h"
#include "miscadmin.h"
#include "replication/basebackup.h"
#include "replication/basebackup_sink.h"
#include "storage/fd.h"
#include "utils/acl.h"
#include "utils/timestamp.h"
#include "utils/wait_event.h"
Include dependency graph for basebackup_server.c:

Go to the source code of this file.

Data Structures

struct  bbsink_server
 

Typedefs

typedef struct bbsink_server bbsink_server
 

Functions

static void bbsink_server_begin_archive (bbsink *sink, const char *archive_name)
 
static void bbsink_server_archive_contents (bbsink *sink, size_t len)
 
static void bbsink_server_end_archive (bbsink *sink)
 
static void bbsink_server_begin_manifest (bbsink *sink)
 
static void bbsink_server_manifest_contents (bbsink *sink, size_t len)
 
static void bbsink_server_end_manifest (bbsink *sink)
 
bbsinkbbsink_server_new (bbsink *next, char *pathname)
 

Variables

static const bbsink_ops bbsink_server_ops
 

Typedef Documentation

◆ bbsink_server

typedef struct bbsink_server bbsink_server

Function Documentation

◆ bbsink_server_archive_contents()

static void bbsink_server_archive_contents ( bbsink sink,
size_t  len 
)
static

Definition at line 160 of file basebackup_server.c.

161 {
162  bbsink_server *mysink = (bbsink_server *) sink;
163  int nbytes;
164 
165  nbytes = FileWrite(mysink->file, mysink->base.bbs_buffer, len,
167 
168  if (nbytes != len)
169  {
170  if (nbytes < 0)
171  ereport(ERROR,
173  errmsg("could not write file \"%s\": %m",
174  FilePathName(mysink->file)),
175  errhint("Check free disk space.")));
176  /* short write: complain appropriately */
177  ereport(ERROR,
178  (errcode(ERRCODE_DISK_FULL),
179  errmsg("could not write file \"%s\": wrote only %d of %d bytes at offset %u",
180  FilePathName(mysink->file),
181  nbytes, (int) len, (unsigned) mysink->filepos),
182  errhint("Check free disk space.")));
183  }
184 
185  mysink->filepos += nbytes;
186 
188 }
void bbsink_forward_archive_contents(bbsink *sink, size_t len)
int errcode_for_file_access(void)
Definition: elog.c:716
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2168
char * FilePathName(File file)
Definition: fd.c:2339
const void size_t len
char * bbs_buffer
@ WAIT_EVENT_BASEBACKUP_WRITE
Definition: wait_event.h:162

References bbsink_server::base, bbsink::bbs_buffer, bbsink_forward_archive_contents(), ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, bbsink_server::file, FilePathName(), bbsink_server::filepos, FileWrite(), len, and WAIT_EVENT_BASEBACKUP_WRITE.

◆ bbsink_server_begin_archive()

static void bbsink_server_begin_archive ( bbsink sink,
const char *  archive_name 
)
static

Definition at line 134 of file basebackup_server.c.

135 {
136  bbsink_server *mysink = (bbsink_server *) sink;
137  char *filename;
138 
139  Assert(mysink->file == 0);
140  Assert(mysink->filepos == 0);
141 
142  filename = psprintf("%s/%s", mysink->pathname, archive_name);
143 
144  mysink->file = PathNameOpenFile(filename,
145  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
146  if (mysink->file <= 0)
147  ereport(ERROR,
149  errmsg("could not create file \"%s\": %m", filename)));
150 
151  pfree(filename);
152 
153  bbsink_forward_begin_archive(sink, archive_name);
154 }
void bbsink_forward_begin_archive(bbsink *sink, const char *archive_name)
#define PG_BINARY
Definition: c.h:1268
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1566
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1175
static char * filename
Definition: pg_dumpall.c:94
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References Assert(), bbsink_forward_begin_archive(), ereport, errcode_for_file_access(), errmsg(), ERROR, bbsink_server::file, filename, bbsink_server::filepos, bbsink_server::pathname, PathNameOpenFile(), pfree(), PG_BINARY, and psprintf().

◆ bbsink_server_begin_manifest()

static void bbsink_server_begin_manifest ( bbsink sink)
static

Definition at line 228 of file basebackup_server.c.

229 {
230  bbsink_server *mysink = (bbsink_server *) sink;
231  char *tmp_filename;
232 
233  Assert(mysink->file == 0);
234 
235  tmp_filename = psprintf("%s/backup_manifest.tmp", mysink->pathname);
236 
237  mysink->file = PathNameOpenFile(tmp_filename,
238  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
239  if (mysink->file <= 0)
240  ereport(ERROR,
242  errmsg("could not create file \"%s\": %m", tmp_filename)));
243 
244  pfree(tmp_filename);
245 
247 }
void bbsink_forward_begin_manifest(bbsink *sink)

References Assert(), bbsink_forward_begin_manifest(), ereport, errcode_for_file_access(), errmsg(), ERROR, bbsink_server::file, bbsink_server::pathname, PathNameOpenFile(), pfree(), PG_BINARY, and psprintf().

◆ bbsink_server_end_archive()

static void bbsink_server_end_archive ( bbsink sink)
static

Definition at line 194 of file basebackup_server.c.

195 {
196  bbsink_server *mysink = (bbsink_server *) sink;
197 
198  /*
199  * We intentionally don't use data_sync_elevel here, because the server
200  * shouldn't PANIC just because we can't guarantee that the backup has
201  * been written down to disk. Running recovery won't fix anything in this
202  * case anyway.
203  */
204  if (FileSync(mysink->file, WAIT_EVENT_BASEBACKUP_SYNC) < 0)
205  ereport(ERROR,
207  errmsg("could not fsync file \"%s\": %m",
208  FilePathName(mysink->file))));
209 
210 
211  /* We're done with this file now. */
212  FileClose(mysink->file);
213  mysink->file = 0;
214  mysink->filepos = 0;
215 
217 }
void bbsink_forward_end_archive(bbsink *sink)
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2266
void FileClose(File file)
Definition: fd.c:1961
@ WAIT_EVENT_BASEBACKUP_SYNC
Definition: wait_event.h:161

References bbsink_forward_end_archive(), ereport, errcode_for_file_access(), errmsg(), ERROR, bbsink_server::file, FileClose(), FilePathName(), bbsink_server::filepos, FileSync(), and WAIT_EVENT_BASEBACKUP_SYNC.

◆ bbsink_server_end_manifest()

static void bbsink_server_end_manifest ( bbsink sink)
static

Definition at line 287 of file basebackup_server.c.

288 {
289  bbsink_server *mysink = (bbsink_server *) sink;
290  char *tmp_filename;
291  char *filename;
292 
293  /* We're done with this file now. */
294  FileClose(mysink->file);
295  mysink->file = 0;
296 
297  /*
298  * Rename it into place. This also fsyncs the temporary file, so we don't
299  * need to do that here. We don't use data_sync_elevel here for the same
300  * reasons as in bbsink_server_end_archive.
301  */
302  tmp_filename = psprintf("%s/backup_manifest.tmp", mysink->pathname);
303  filename = psprintf("%s/backup_manifest", mysink->pathname);
304  durable_rename(tmp_filename, filename, ERROR);
305  pfree(filename);
306  pfree(tmp_filename);
307 
309 }
void bbsink_forward_end_manifest(bbsink *sink)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:699

References bbsink_forward_end_manifest(), durable_rename(), ERROR, bbsink_server::file, FileClose(), filename, bbsink_server::pathname, pfree(), and psprintf().

◆ bbsink_server_manifest_contents()

static void bbsink_server_manifest_contents ( bbsink sink,
size_t  len 
)
static

Definition at line 253 of file basebackup_server.c.

254 {
255  bbsink_server *mysink = (bbsink_server *) sink;
256  int nbytes;
257 
258  nbytes = FileWrite(mysink->file, mysink->base.bbs_buffer, len,
260 
261  if (nbytes != len)
262  {
263  if (nbytes < 0)
264  ereport(ERROR,
266  errmsg("could not write file \"%s\": %m",
267  FilePathName(mysink->file)),
268  errhint("Check free disk space.")));
269  /* short write: complain appropriately */
270  ereport(ERROR,
271  (errcode(ERRCODE_DISK_FULL),
272  errmsg("could not write file \"%s\": wrote only %d of %d bytes at offset %u",
273  FilePathName(mysink->file),
274  nbytes, (int) len, (unsigned) mysink->filepos),
275  errhint("Check free disk space.")));
276  }
277 
278  mysink->filepos += nbytes;
279 
281 }
void bbsink_forward_manifest_contents(bbsink *sink, size_t len)

References bbsink_server::base, bbsink::bbs_buffer, bbsink_forward_manifest_contents(), ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, bbsink_server::file, FilePathName(), bbsink_server::filepos, FileWrite(), len, and WAIT_EVENT_BASEBACKUP_WRITE.

◆ bbsink_server_new()

bbsink* bbsink_server_new ( bbsink next,
char *  pathname 
)

Definition at line 62 of file basebackup_server.c.

63 {
64  bbsink_server *sink = palloc0(sizeof(bbsink_server));
65 
66  *((const bbsink_ops **) &sink->base.bbs_ops) = &bbsink_server_ops;
67  sink->pathname = pathname;
68  sink->base.bbs_next = next;
69 
70  /* Replication permission is not sufficient in this case. */
72  if (!has_privs_of_role(GetUserId(), ROLE_PG_WRITE_SERVER_FILES))
73  ereport(ERROR,
74  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
75  errmsg("must be superuser or a role with privileges of the pg_write_server_files role to create server backup")));
77 
78  /*
79  * It's not a good idea to store your backups in the same directory that
80  * you're backing up. If we allowed a relative path here, that could
81  * easily happen accidentally, so we don't. The user could still
82  * accomplish the same thing by including the absolute path to $PGDATA in
83  * the pathname, but that's likely an intentional bad decision rather than
84  * an accident.
85  */
86  if (!is_absolute_path(pathname))
87  ereport(ERROR,
88  (errcode(ERRCODE_INVALID_NAME),
89  errmsg("relative path not allowed for server backup")));
90 
91  switch (pg_check_dir(pathname))
92  {
93  case 0:
94 
95  /*
96  * Does not exist, so create it using the same permissions we'd
97  * use for a new subdirectory of the data directory itself.
98  */
99  if (MakePGDirectory(pathname) < 0)
100  ereport(ERROR,
102  errmsg("could not create directory \"%s\": %m", pathname)));
103  break;
104 
105  case 1:
106  /* Exists, empty. */
107  break;
108 
109  case 2:
110  case 3:
111  case 4:
112  /* Exists, not empty. */
113  ereport(ERROR,
114  (errcode(ERRCODE_DUPLICATE_FILE),
115  errmsg("directory \"%s\" exists but is not empty",
116  pathname)));
117  break;
118 
119  default:
120  /* Access problem. */
121  ereport(ERROR,
123  errmsg("could not access directory \"%s\": %m",
124  pathname)));
125  }
126 
127  return &sink->base;
128 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4951
static const bbsink_ops bbsink_server_ops
static int32 next
Definition: blutils.c:219
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3803
void * palloc0(Size size)
Definition: mcxt.c:1099
Oid GetUserId(void)
Definition: miscinit.c:492
#define is_absolute_path(filename)
Definition: port.h:89
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:33
bbsink * bbs_next
const bbsink_ops * bbs_ops
void StartTransactionCommand(void)
Definition: xact.c:2925
void CommitTransactionCommand(void)
Definition: xact.c:3022

References bbsink_server::base, bbsink::bbs_next, bbsink::bbs_ops, bbsink_server_ops, CommitTransactionCommand(), ereport, errcode(), errcode_for_file_access(), errmsg(), ERROR, GetUserId(), has_privs_of_role(), is_absolute_path, MakePGDirectory(), next, palloc0(), bbsink_server::pathname, pg_check_dir(), and StartTransactionCommand().

Referenced by server_get_sink().

Variable Documentation

◆ bbsink_server_ops

const bbsink_ops bbsink_server_ops
static
Initial value:
= {
.begin_backup = bbsink_forward_begin_backup,
.begin_archive = bbsink_server_begin_archive,
.archive_contents = bbsink_server_archive_contents,
.end_archive = bbsink_server_end_archive,
.begin_manifest = bbsink_server_begin_manifest,
.manifest_contents = bbsink_server_manifest_contents,
.end_manifest = bbsink_server_end_manifest,
}
static void bbsink_server_end_manifest(bbsink *sink)
static void bbsink_server_archive_contents(bbsink *sink, size_t len)
static void bbsink_server_end_archive(bbsink *sink)
static void bbsink_server_manifest_contents(bbsink *sink, size_t len)
static void bbsink_server_begin_manifest(bbsink *sink)
static void bbsink_server_begin_archive(bbsink *sink, const char *archive_name)
void bbsink_forward_begin_backup(bbsink *sink)
void bbsink_forward_end_backup(bbsink *sink, XLogRecPtr endptr, TimeLineID endtli)
void bbsink_forward_cleanup(bbsink *sink)

Definition at line 46 of file basebackup_server.c.

Referenced by bbsink_server_new().