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-2021, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/bin/pg_basebackup/walmethods.h
9  *-------------------------------------------------------------------------
10  */
11 
12 
13 typedef void *Walfile;
14 
15 typedef enum
16 {
21 
22 /*
23  * A WalWriteMethod structure represents the different methods used
24  * to write the streaming WAL as it's received.
25  *
26  * All methods that have a failure return indicator will set state
27  * allowing the getlasterror() method to return a suitable message.
28  * Commonly, errno is this state (or part of it); so callers must take
29  * care not to clobber errno between a failed method call and use of
30  * getlasterror() to retrieve the message.
31  */
34 {
35  /*
36  * Open a target file. Returns Walfile, or NULL if open failed. If a temp
37  * suffix is specified, a file with that name will be opened, and then
38  * automatically renamed in close(). If pad_to_size is specified, the file
39  * will be padded with NUL up to that size, if supported by the Walmethod.
40  */
41  Walfile (*open_for_write) (const char *pathname, const char *temp_suffix, size_t pad_to_size);
42 
43  /*
44  * Close an open Walfile, using one or more methods for handling automatic
45  * unlinking etc. Returns 0 on success, other values for error.
46  */
47  int (*close) (Walfile f, WalCloseMethod method);
48 
49  /* Check if a file exist */
50  bool (*existsfile) (const char *pathname);
51 
52  /* Return the size of a file, or -1 on failure. */
53  ssize_t (*get_file_size) (const char *pathname);
54 
55  /*
56  * Return the name of the current file to work on in pg_malloc()'d string,
57  * without the base directory. This is useful for logging.
58  */
59  char *(*get_file_name) (const char *pathname, const char *temp_suffix);
60 
61  /* Return the level of compression */
62  int (*compression) (void);
63 
64  /*
65  * Write count number of bytes to the file, and return the number of bytes
66  * actually written or -1 for error.
67  */
68  ssize_t (*write) (Walfile f, const void *buf, size_t count);
69 
70  /* Return the current position in a file or -1 on error */
71  off_t (*get_current_pos) (Walfile f);
72 
73  /*
74  * fsync the contents of the specified file. Returns 0 on success.
75  */
76  int (*sync) (Walfile f);
77 
78  /*
79  * Clean up the Walmethod, closing any shared resources. For methods like
80  * tar, this includes writing updated headers. Returns true if the
81  * close/write/sync of shared resources succeeded, otherwise returns false
82  * (but the resources are still closed).
83  */
84  bool (*finish) (void);
85 
86  /* Return a text for the last error in this Walfile */
87  const char *(*getlasterror) (void);
88 };
89 
90 /*
91  * Available WAL methods:
92  * - WalDirectoryMethod - write WAL to regular files in a standard pg_wal
93  * - WalTarMethod - write WAL to a tarfile corresponding to pg_wal
94  * (only implements the methods required for pg_basebackup,
95  * not all those required for pg_receivewal)
96  */
98  int compression, bool sync);
99 WalWriteMethod *CreateWalTarMethod(const char *tarbase, int compression, bool sync);
100 
101 /* Cleanup routines for previously-created methods */
102 void FreeWalDirectoryMethod(void);
103 void FreeWalTarMethod(void);
WalCloseMethod
Definition: walmethods.h:15
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, int compression, bool sync)
Definition: walmethods.c:376
void * Walfile
Definition: walmethods.h:13
int(* close)(Walfile f, WalCloseMethod method)
Definition: walmethods.h:47
int(* sync)(Walfile f)
Definition: walmethods.h:76
static char * basedir
off_t(* get_current_pos)(Walfile f)
Definition: walmethods.h:71
static char * buf
Definition: pg_test_fsync.c:68
void FreeWalTarMethod(void)
Definition: walmethods.c:1068
ssize_t(* write)(Walfile f, const void *buf, size_t count)
Definition: walmethods.h:68
WalWriteMethod * CreateWalTarMethod(const char *tarbase, int compression, bool sync)
Definition: walmethods.c:1035
void FreeWalDirectoryMethod(void)
Definition: walmethods.c:402
bool(* finish)(void)
Definition: walmethods.h:84
bool(* existsfile)(const char *pathname)
Definition: walmethods.h:50
int(* compression)(void)
Definition: walmethods.h:62
Walfile(* open_for_write)(const char *pathname, const char *temp_suffix, size_t pad_to_size)
Definition: walmethods.h:41
ssize_t(* get_file_size)(const char *pathname)
Definition: walmethods.h:53
unsigned char bool
Definition: c.h:391