basebackup_sink.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*-------------------------------------------------------------------------
  2. *
  3. * basebackup_sink.h
  4. * API for filtering or sending to a final destination the archives
  5. * produced by the base backup process
  6. *
  7. * Taking a base backup produces one archive per tablespace directory,
  8. * plus a backup manifest unless that feature has been disabled. The
  9. * goal of the backup process is to put those archives and that manifest
  10. * someplace, possibly after postprocessing them in some way. A 'bbsink'
  11. * is an object to which those archives, and the manifest if present,
  12. * can be sent.
  13. *
  14. * In practice, there will be a chain of 'bbsink' objects rather than
  15. * just one, with callbacks being forwarded from one to the next,
  16. * possibly with modification. Each object is responsible for a
  17. * single task e.g. command progress reporting, throttling, or
  18. * communication with the client.
  19. *
  20. * Portions Copyright (c) 2010-2022, PostgreSQL Global Development Group
  21. *
  22. * src/include/backup/basebackup_sink.h
  23. *
  24. *-------------------------------------------------------------------------
  25. */
  26. #ifndef BASEBACKUP_SINK_H
  27. #define BASEBACKUP_SINK_H
  28. #include "access/xlog_internal.h"
  29. #include "common/compression.h"
  30. #include "nodes/pg_list.h"
  31. /* Forward declarations. */
  32. struct bbsink;
  33. struct bbsink_ops;
  34. typedef struct bbsink bbsink;
  35. typedef struct bbsink_ops bbsink_ops;
  36. /*
  37. * Overall backup state shared by all bbsink objects for a backup.
  38. *
  39. * Before calling bbstate_begin_backup, caller must initiate a bbsink_state
  40. * object which will last for the lifetime of the backup, and must thereafter
  41. * update it as required before each new call to a bbsink method. The bbsink
  42. * will retain a pointer to the state object and will consult it to understand
  43. * the progress of the backup.
  44. *
  45. * 'tablespaces' is a list of tablespaceinfo objects. It must be set before
  46. * calling bbstate_begin_backup() and must not be modified thereafter.
  47. *
  48. * 'tablespace_num' is the index of the current tablespace within the list
  49. * stored in 'tablespaces'.
  50. *
  51. * 'bytes_done' is the number of bytes read so far from $PGDATA.
  52. *
  53. * 'bytes_total' is the total number of bytes estimated to be present in
  54. * $PGDATA, if we have estimated this.
  55. *
  56. * 'bytes_total_is_valid' is true if and only if a proper estimate has been
  57. * stored into 'bytes_total'.
  58. *
  59. * 'startptr' and 'starttli' identify the point in the WAL stream at which
  60. * the backup began. They must be set before calling bbstate_begin_backup()
  61. * and must not be modified thereafter.
  62. */
  63. typedef struct bbsink_state
  64. {
  65. List *tablespaces;
  66. int tablespace_num;
  67. uint64 bytes_done;
  68. uint64 bytes_total;
  69. bool bytes_total_is_valid;
  70. XLogRecPtr startptr;
  71. TimeLineID starttli;
  72. } bbsink_state;
  73. /*
  74. * Common data for any type of basebackup sink.
  75. *
  76. * 'bbs_ops' is the relevant callback table.
  77. *
  78. * 'bbs_buffer' is the buffer into which data destined for the bbsink
  79. * should be stored. It must be a multiple of BLCKSZ.
  80. *
  81. * 'bbs_buffer_length' is the allocated length of the buffer.
  82. *
  83. * 'bbs_next' is a pointer to another bbsink to which this bbsink is
  84. * forwarding some or all operations.
  85. *
  86. * 'bbs_state' is a pointer to the bbsink_state object for this backup.
  87. * Every bbsink associated with this backup should point to the same
  88. * underlying state object.
  89. *
  90. * In general it is expected that the values of these fields are set when
  91. * a bbsink is created and that they do not change thereafter. It's OK
  92. * to modify the data to which bbs_buffer or bbs_state point, but no changes
  93. * should be made to the contents of this struct.
  94. */
  95. struct bbsink
  96. {
  97. const bbsink_ops *bbs_ops;
  98. char *bbs_buffer;
  99. size_t bbs_buffer_length;
  100. bbsink *bbs_next;
  101. bbsink_state *bbs_state;
  102. };
  103. /*
  104. * Callbacks for a base backup sink.
  105. *
  106. * All of these callbacks are required. If a particular callback just needs to
  107. * forward the call to sink->bbs_next, use bbsink_forward_<callback_name> as
  108. * the callback.
  109. *
  110. * Callers should always invoke these callbacks via the bbsink_* inline
  111. * functions rather than calling them directly.
  112. */
  113. struct bbsink_ops
  114. {
  115. /*
  116. * This callback is invoked just once, at the very start of the backup. It
  117. * must set bbs_buffer to point to a chunk of storage where at least
  118. * bbs_buffer_length bytes of data can be written.
  119. */
  120. void (*begin_backup) (bbsink *sink);
  121. /*
  122. * For each archive transmitted to a bbsink, there will be one call to the
  123. * begin_archive() callback, some number of calls to the
  124. * archive_contents() callback, and then one call to the end_archive()
  125. * callback.
  126. *
  127. * Before invoking the archive_contents() callback, the caller should copy
  128. * a number of bytes equal to what will be passed as len into bbs_buffer,
  129. * but not more than bbs_buffer_length.
  130. *
  131. * It's generally good if the buffer is as full as possible before the
  132. * archive_contents() callback is invoked, but it's not worth expending
  133. * extra cycles to make sure it's absolutely 100% full.
  134. */
  135. void (*begin_archive) (bbsink *sink, const char *archive_name);
  136. void (*archive_contents) (bbsink *sink, size_t len);
  137. void (*end_archive) (bbsink *sink);
  138. /*
  139. * If a backup manifest is to be transmitted to a bbsink, there will be
  140. * one call to the begin_manifest() callback, some number of calls to the
  141. * manifest_contents() callback, and then one call to the end_manifest()
  142. * callback. These calls will occur after all archives are transmitted.
  143. *
  144. * The rules for invoking the manifest_contents() callback are the same as
  145. * for the archive_contents() callback above.
  146. */
  147. void (*begin_manifest) (bbsink *sink);
  148. void (*manifest_contents) (bbsink *sink, size_t len);
  149. void (*end_manifest) (bbsink *sink);
  150. /*
  151. * This callback is invoked just once, after all archives and the manifest
  152. * have been sent.
  153. */
  154. void (*end_backup) (bbsink *sink, XLogRecPtr endptr, TimeLineID endtli);
  155. /*
  156. * If a backup is aborted by an error, this callback is invoked before the
  157. * bbsink object is destroyed, so that it can release any resources that
  158. * would not be released automatically. If no error occurs, this callback
  159. * is invoked after the end_backup callback.
  160. */
  161. void (*cleanup) (bbsink *sink);
  162. };
  163. /* Begin a backup. */
  164. static inline void
  165. bbsink_begin_backup(bbsink *sink, bbsink_state *state, int buffer_length)
  166. {
  167. Assert(sink != NULL);
  168. Assert(buffer_length > 0);
  169. sink->bbs_state = state;
  170. sink->bbs_buffer_length = buffer_length;
  171. sink->bbs_ops->begin_backup(sink);
  172. Assert(sink->bbs_buffer != NULL);
  173. Assert((sink->bbs_buffer_length % BLCKSZ) == 0);
  174. }
  175. /* Begin an archive. */
  176. static inline void
  177. bbsink_begin_archive(bbsink *sink, const char *archive_name)
  178. {
  179. Assert(sink != NULL);
  180. sink->bbs_ops->begin_archive(sink, archive_name);
  181. }
  182. /* Process some of the contents of an archive. */
  183. static inline void
  184. bbsink_archive_contents(bbsink *sink, size_t len)
  185. {
  186. Assert(sink != NULL);
  187. /*
  188. * The caller should make a reasonable attempt to fill the buffer before
  189. * calling this function, so it shouldn't be completely empty. Nor should
  190. * it be filled beyond capacity.
  191. */
  192. Assert(len > 0 && len <= sink->bbs_buffer_length);
  193. sink->bbs_ops->archive_contents(sink, len);
  194. }
  195. /* Finish an archive. */
  196. static inline void
  197. bbsink_end_archive(bbsink *sink)
  198. {
  199. Assert(sink != NULL);
  200. sink->bbs_ops->end_archive(sink);
  201. }
  202. /* Begin the backup manifest. */
  203. static inline void
  204. bbsink_begin_manifest(bbsink *sink)
  205. {
  206. Assert(sink != NULL);
  207. sink->bbs_ops->begin_manifest(sink);
  208. }
  209. /* Process some of the manifest contents. */
  210. static inline void
  211. bbsink_manifest_contents(bbsink *sink, size_t len)
  212. {
  213. Assert(sink != NULL);
  214. /* See comments in bbsink_archive_contents. */
  215. Assert(len > 0 && len <= sink->bbs_buffer_length);
  216. sink->bbs_ops->manifest_contents(sink, len);
  217. }
  218. /* Finish the backup manifest. */
  219. static inline void
  220. bbsink_end_manifest(bbsink *sink)
  221. {
  222. Assert(sink != NULL);
  223. sink->bbs_ops->end_manifest(sink);
  224. }
  225. /* Finish a backup. */
  226. static inline void
  227. bbsink_end_backup(bbsink *sink, XLogRecPtr endptr, TimeLineID endtli)
  228. {
  229. Assert(sink != NULL);
  230. Assert(sink->bbs_state->tablespace_num == list_length(sink->bbs_state->tablespaces));
  231. sink->bbs_ops->end_backup(sink, endptr, endtli);
  232. }
  233. /* Release resources before destruction. */
  234. static inline void
  235. bbsink_cleanup(bbsink *sink)
  236. {
  237. Assert(sink != NULL);
  238. sink->bbs_ops->cleanup(sink);
  239. }
  240. /* Forwarding callbacks. Use these to pass operations through to next sink. */
  241. extern void bbsink_forward_begin_backup(bbsink *sink);
  242. extern void bbsink_forward_begin_archive(bbsink *sink,
  243. const char *archive_name);
  244. extern void bbsink_forward_archive_contents(bbsink *sink, size_t len);
  245. extern void bbsink_forward_end_archive(bbsink *sink);
  246. extern void bbsink_forward_begin_manifest(bbsink *sink);
  247. extern void bbsink_forward_manifest_contents(bbsink *sink, size_t len);
  248. extern void bbsink_forward_end_manifest(bbsink *sink);
  249. extern void bbsink_forward_end_backup(bbsink *sink, XLogRecPtr endptr,
  250. TimeLineID endtli);
  251. extern void bbsink_forward_cleanup(bbsink *sink);
  252. /* Constructors for various types of sinks. */
  253. extern bbsink *bbsink_copystream_new(bool send_to_client);
  254. extern bbsink *bbsink_gzip_new(bbsink *next, pg_compress_specification *);
  255. extern bbsink *bbsink_lz4_new(bbsink *next, pg_compress_specification *);
  256. extern bbsink *bbsink_zstd_new(bbsink *next, pg_compress_specification *);
  257. extern bbsink *bbsink_progress_new(bbsink *next, bool estimate_backup_size);
  258. extern bbsink *bbsink_server_new(bbsink *next, char *pathname);
  259. extern bbsink *bbsink_throttle_new(bbsink *next, uint32 maxrate);
  260. /* Extra interface functions for progress reporting. */
  261. extern void basebackup_progress_wait_checkpoint(void);
  262. extern void basebackup_progress_estimate_backup_size(void);
  263. extern void basebackup_progress_wait_wal_archive(bbsink_state *);
  264. extern void basebackup_progress_transfer_wal(void);
  265. extern void basebackup_progress_done(void);
  266. #endif