PostgreSQL Source Code  git master
walmethods.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * walmethods.h
4  *
5  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/bin/pg_basebackup/walmethods.h
9  *-------------------------------------------------------------------------
10  */
11 
12 #include "common/compression.h"
13 
14 struct WalWriteMethod;
15 typedef struct WalWriteMethod WalWriteMethod;
16 
17 typedef struct
18 {
20  off_t currpos;
21  char *pathname;
22  /*
23  * MORE DATA FOLLOWS AT END OF STRUCT
24  *
25  * Each WalWriteMethod is expected to embed this as the first member of
26  * a larger struct with method-specific fields following.
27  */
28 } Walfile;
29 
30 typedef enum
31 {
36 
37 /*
38  * Table of callbacks for a WalWriteMethod.
39  */
40 typedef struct WalWriteMethodOps
41 {
42  /*
43  * Open a target file. Returns Walfile, or NULL if open failed. If a temp
44  * suffix is specified, a file with that name will be opened, and then
45  * automatically renamed in close(). If pad_to_size is specified, the file
46  * will be padded with NUL up to that size, if supported by the Walmethod.
47  */
48  Walfile *(*open_for_write) (WalWriteMethod *wwmethod, const char *pathname, const char *temp_suffix, size_t pad_to_size);
49 
50  /*
51  * Close an open Walfile, using one or more methods for handling automatic
52  * unlinking etc. Returns 0 on success, other values for error.
53  */
54  int (*close) (Walfile *f, WalCloseMethod method);
55 
56  /* Check if a file exist */
57  bool (*existsfile) (WalWriteMethod *wwmethod, const char *pathname);
58 
59  /* Return the size of a file, or -1 on failure. */
60  ssize_t (*get_file_size) (WalWriteMethod *wwmethod, const char *pathname);
61 
62  /*
63  * Return the name of the current file to work on in pg_malloc()'d string,
64  * without the base directory. This is useful for logging.
65  */
66  char *(*get_file_name) (WalWriteMethod *wwmethod, const char *pathname, const char *temp_suffix);
67 
68  /*
69  * Write count number of bytes to the file, and return the number of bytes
70  * actually written or -1 for error.
71  */
72  ssize_t (*write) (Walfile *f, const void *buf, size_t count);
73 
74  /*
75  * fsync the contents of the specified file. Returns 0 on success.
76  */
77  int (*sync) (Walfile *f);
78 
79  /*
80  * Clean up the Walmethod, closing any shared resources. For methods like
81  * tar, this includes writing updated headers. Returns true if the
82  * close/write/sync of shared resources succeeded, otherwise returns false
83  * (but the resources are still closed).
84  */
85  bool (*finish) (WalWriteMethod *wwmethod);
86 
87  /*
88  * Free subsidiary data associated with the WalWriteMethod, and the
89  * WalWriteMethod itself.
90  */
91  void (*free) (WalWriteMethod *wwmethod);
93 
94 /*
95  * A WalWriteMethod structure represents a way of writing streaming WAL as
96  * it's received.
97  *
98  * All methods that have a failure return indicator will set lasterrstring
99  * or lasterrno (the former takes precedence) so that the caller can signal
100  * a suitable error.
101  */
103 {
107  bool sync;
108  const char *lasterrstring; /* if set, takes precedence over lasterrno */
110  /*
111  * MORE DATA FOLLOWS AT END OF STRUCT
112  *
113  * Each WalWriteMethod is expected to embed this as the first member of
114  * a larger struct with method-specific fields following.
115  */
116 };
117 
118 /*
119  * Available WAL methods:
120  * - WalDirectoryMethod - write WAL to regular files in a standard pg_wal
121  * - WalTarMethod - write WAL to a tarfile corresponding to pg_wal
122  * (only implements the methods required for pg_basebackup,
123  * not all those required for pg_receivewal)
124  */
127  int compression_level, bool sync);
128 WalWriteMethod *CreateWalTarMethod(const char *tarbase,
130  int compression_level, bool sync);
131 
132 const char *GetLastWalMethodError(WalWriteMethod *wwmethod);
unsigned char bool
Definition: c.h:440
pg_compress_algorithm
Definition: compression.h:18
static char * basedir
static pg_compress_algorithm compression_algorithm
Definition: pg_receivewal.c:55
static char * buf
Definition: pg_test_fsync.c:67
void(* free)(WalWriteMethod *wwmethod)
Definition: walmethods.h:91
bool(* existsfile)(WalWriteMethod *wwmethod, const char *pathname)
Definition: walmethods.h:57
ssize_t(* write)(Walfile *f, const void *buf, size_t count)
Definition: walmethods.h:72
ssize_t(* get_file_size)(WalWriteMethod *wwmethod, const char *pathname)
Definition: walmethods.h:60
int(* close)(Walfile *f, WalCloseMethod method)
Definition: walmethods.h:54
bool(* finish)(WalWriteMethod *wwmethod)
Definition: walmethods.h:85
int(* sync)(Walfile *f)
Definition: walmethods.h:77
const char * lasterrstring
Definition: walmethods.h:108
int compression_level
Definition: walmethods.h:106
const WalWriteMethodOps * ops
Definition: walmethods.h:104
pg_compress_algorithm compression_algorithm
Definition: walmethods.h:105
WalWriteMethod * wwmethod
Definition: walmethods.h:19
char * pathname
Definition: walmethods.h:21
off_t currpos
Definition: walmethods.h:20
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, pg_compress_algorithm compression_algorithm, int compression_level, bool sync)
Definition: walmethods.c:635
struct WalWriteMethodOps WalWriteMethodOps
WalWriteMethod * CreateWalTarMethod(const char *tarbase, pg_compress_algorithm compression_algorithm, int compression_level, bool sync)
Definition: walmethods.c:1350
const char * GetLastWalMethodError(WalWriteMethod *wwmethod)
Definition: walmethods.c:1378
WalCloseMethod
Definition: walmethods.h:31
@ CLOSE_UNLINK
Definition: walmethods.h:33
@ CLOSE_NO_RENAME
Definition: walmethods.h:34
@ CLOSE_NORMAL
Definition: walmethods.h:32