1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * src/bin/pg_basebackup/walmethods.h
9 *-------------------------------------------------------------------------
12 #include "common/compression.h"
14 struct WalWriteMethod
;
15 typedef struct WalWriteMethod WalWriteMethod
;
19 WalWriteMethod
*wwmethod
;
24 * MORE DATA FOLLOWS AT END OF STRUCT
26 * Each WalWriteMethod is expected to embed this as the first member of a
27 * larger struct with method-specific fields following.
39 * Table of callbacks for a WalWriteMethod.
41 typedef struct WalWriteMethodOps
44 * Open a target file. Returns Walfile, or NULL if open failed. If a temp
45 * suffix is specified, a file with that name will be opened, and then
46 * automatically renamed in close(). If pad_to_size is specified, the file
47 * will be padded with NUL up to that size, if supported by the Walmethod.
49 Walfile
*(*open_for_write
) (WalWriteMethod
*wwmethod
, const char *pathname
, const char *temp_suffix
, size_t pad_to_size
);
52 * Close an open Walfile, using one or more methods for handling automatic
53 * unlinking etc. Returns 0 on success, other values for error.
55 int (*close
) (Walfile
*f
, WalCloseMethod method
);
57 /* Check if a file exist */
58 bool (*existsfile
) (WalWriteMethod
*wwmethod
, const char *pathname
);
60 /* Return the size of a file, or -1 on failure. */
61 ssize_t (*get_file_size
) (WalWriteMethod
*wwmethod
, const char *pathname
);
64 * Return the name of the current file to work on in pg_malloc()'d string,
65 * without the base directory. This is useful for logging.
67 char *(*get_file_name
) (WalWriteMethod
*wwmethod
, const char *pathname
, const char *temp_suffix
);
70 * Write count number of bytes to the file, and return the number of bytes
71 * actually written or -1 for error.
73 ssize_t (*write
) (Walfile
*f
, const void *buf
, size_t count
);
76 * fsync the contents of the specified file. Returns 0 on success.
78 int (*sync
) (Walfile
*f
);
81 * Clean up the Walmethod, closing any shared resources. For methods like
82 * tar, this includes writing updated headers. Returns true if the
83 * close/write/sync of shared resources succeeded, otherwise returns false
84 * (but the resources are still closed).
86 bool (*finish
) (WalWriteMethod
*wwmethod
);
89 * Free subsidiary data associated with the WalWriteMethod, and the
90 * WalWriteMethod itself.
92 void (*free
) (WalWriteMethod
*wwmethod
);
96 * A WalWriteMethod structure represents a way of writing streaming WAL as
99 * All methods that have a failure return indicator will set lasterrstring
100 * or lasterrno (the former takes precedence) so that the caller can signal
103 struct WalWriteMethod
105 const WalWriteMethodOps
*ops
;
106 pg_compress_algorithm compression_algorithm
;
107 int compression_level
;
109 const char *lasterrstring
; /* if set, takes precedence over lasterrno */
113 * MORE DATA FOLLOWS AT END OF STRUCT
115 * Each WalWriteMethod is expected to embed this as the first member of a
116 * larger struct with method-specific fields following.
121 * Available WAL methods:
122 * - WalDirectoryMethod - write WAL to regular files in a standard pg_wal
123 * - WalTarMethod - write WAL to a tarfile corresponding to pg_wal
124 * (only implements the methods required for pg_basebackup,
125 * not all those required for pg_receivewal)
127 WalWriteMethod
*CreateWalDirectoryMethod(const char *basedir
,
128 pg_compress_algorithm compression_algorithm
,
129 int compression_level
, bool sync
);
130 WalWriteMethod
*CreateWalTarMethod(const char *tarbase
,
131 pg_compress_algorithm compression_algorithm
,
132 int compression_level
, bool sync
);
134 const char *GetLastWalMethodError(WalWriteMethod
*wwmethod
);