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-2022, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/bin/pg_basebackup/walmethods.h
9  *-------------------------------------------------------------------------
10  */
11 
12 #include "common/compression.h"
13 
14 typedef void *Walfile;
15 
16 typedef enum
17 {
22 
23 /*
24  * A WalWriteMethod structure represents the different methods used
25  * to write the streaming WAL as it's received.
26  *
27  * All methods that have a failure return indicator will set state
28  * allowing the getlasterror() method to return a suitable message.
29  * Commonly, errno is this state (or part of it); so callers must take
30  * care not to clobber errno between a failed method call and use of
31  * getlasterror() to retrieve the message.
32  */
33 typedef struct WalWriteMethod WalWriteMethod;
35 {
36  /*
37  * Open a target file. Returns Walfile, or NULL if open failed. If a temp
38  * suffix is specified, a file with that name will be opened, and then
39  * automatically renamed in close(). If pad_to_size is specified, the file
40  * will be padded with NUL up to that size, if supported by the Walmethod.
41  */
42  Walfile (*open_for_write) (const char *pathname, const char *temp_suffix, size_t pad_to_size);
43 
44  /*
45  * Close an open Walfile, using one or more methods for handling automatic
46  * unlinking etc. Returns 0 on success, other values for error.
47  */
48  int (*close) (Walfile f, WalCloseMethod method);
49 
50  /* Check if a file exist */
51  bool (*existsfile) (const char *pathname);
52 
53  /* Return the size of a file, or -1 on failure. */
54  ssize_t (*get_file_size) (const char *pathname);
55 
56  /*
57  * Return the name of the current file to work on in pg_malloc()'d string,
58  * without the base directory. This is useful for logging.
59  */
60  char *(*get_file_name) (const char *pathname, const char *temp_suffix);
61 
62  /* Returns the compression method */
64 
65  /*
66  * Write count number of bytes to the file, and return the number of bytes
67  * actually written or -1 for error.
68  */
69  ssize_t (*write) (Walfile f, const void *buf, size_t count);
70 
71  /* Return the current position in a file or -1 on error */
72  off_t (*get_current_pos) (Walfile f);
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) (void);
86 
87  /* Return a text for the last error in this Walfile */
88  const char *(*getlasterror) (void);
89 };
90 
91 /*
92  * Available WAL methods:
93  * - WalDirectoryMethod - write WAL to regular files in a standard pg_wal
94  * - WalTarMethod - write WAL to a tarfile corresponding to pg_wal
95  * (only implements the methods required for pg_basebackup,
96  * not all those required for pg_receivewal)
97  */
99  pg_compress_algorithm compression_algo,
100  int compression, bool sync);
101 WalWriteMethod *CreateWalTarMethod(const char *tarbase,
102  pg_compress_algorithm compression_algo,
103  int compression, bool sync);
104 
105 /* Cleanup routines for previously-created methods */
106 void FreeWalDirectoryMethod(void);
107 void FreeWalTarMethod(void);
unsigned char bool
Definition: c.h:391
pg_compress_algorithm
Definition: compression.h:18
static char * basedir
static char * buf
Definition: pg_test_fsync.c:67
off_t(* get_current_pos)(Walfile f)
Definition: walmethods.h:72
ssize_t(* get_file_size)(const char *pathname)
Definition: walmethods.h:54
Walfile(* open_for_write)(const char *pathname, const char *temp_suffix, size_t pad_to_size)
Definition: walmethods.h:42
bool(* existsfile)(const char *pathname)
Definition: walmethods.h:51
int(* close)(Walfile f, WalCloseMethod method)
Definition: walmethods.h:48
int(* sync)(Walfile f)
Definition: walmethods.h:77
ssize_t(* write)(Walfile f, const void *buf, size_t count)
Definition: walmethods.h:69
pg_compress_algorithm(* compression_algorithm)(void)
Definition: walmethods.h:63
bool(* finish)(void)
Definition: walmethods.h:85
void FreeWalTarMethod(void)
Definition: walmethods.c:1375
WalWriteMethod * CreateWalTarMethod(const char *tarbase, pg_compress_algorithm compression_algo, int compression, bool sync)
Definition: walmethods.c:1338
void FreeWalDirectoryMethod(void)
Definition: walmethods.c:654
WalCloseMethod
Definition: walmethods.h:17
@ CLOSE_UNLINK
Definition: walmethods.h:19
@ CLOSE_NO_RENAME
Definition: walmethods.h:20
@ CLOSE_NORMAL
Definition: walmethods.h:18
void * Walfile
Definition: walmethods.h:14
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, pg_compress_algorithm compression_algo, int compression, bool sync)
Definition: walmethods.c:625