22 typedef struct bbsink_gzip
37 static void bbsink_gzip_begin_backup(
bbsink *sink);
38 static void bbsink_gzip_begin_archive(
bbsink *sink,
const char *archive_name);
39 static void bbsink_gzip_archive_contents(
bbsink *sink,
size_t len);
40 static void bbsink_gzip_manifest_contents(
bbsink *sink,
size_t len);
41 static void bbsink_gzip_end_archive(
bbsink *sink);
42 static void *gzip_palloc(
void *opaque,
unsigned items,
unsigned size);
43 static void gzip_pfree(
void *opaque,
void *address);
47 .begin_archive = bbsink_gzip_begin_archive,
48 .archive_contents = bbsink_gzip_archive_contents,
49 .end_archive = bbsink_gzip_end_archive,
51 .manifest_contents = bbsink_gzip_manifest_contents,
66 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
67 errmsg(
"gzip compression is not supported by this build")));
79 sink =
palloc0(
sizeof(bbsink_gzip));
80 *((
const bbsink_ops **) &sink->base.bbs_ops) = &bbsink_gzip_ops;
81 sink->base.bbs_next =
next;
94 bbsink_gzip_begin_backup(
bbsink *sink)
114 bbsink_gzip_begin_archive(
bbsink *sink,
const char *archive_name)
116 bbsink_gzip *mysink = (bbsink_gzip *) sink;
117 char *gz_archive_name;
118 z_stream *zs = &mysink->zstream;
121 memset(zs, 0,
sizeof(z_stream));
122 zs->zalloc = gzip_palloc;
123 zs->zfree = gzip_pfree;
139 if (deflateInit2(zs, mysink->compresslevel, Z_DEFLATED, 15 + 16, 8,
140 Z_DEFAULT_STRATEGY) != Z_OK)
142 errcode(ERRCODE_INTERNAL_ERROR),
143 errmsg(
"could not initialize compression library"));
149 gz_archive_name =
psprintf(
"%s.gz", archive_name);
152 pfree(gz_archive_name);
167 bbsink_gzip_archive_contents(
bbsink *sink,
size_t len)
169 bbsink_gzip *mysink = (bbsink_gzip *) sink;
170 z_stream *zs = &mysink->zstream;
173 zs->next_in = (
uint8 *) mysink->base.bbs_buffer;
176 while (zs->avail_in > 0)
181 Assert(mysink->bytes_written < mysink->base.bbs_next->bbs_buffer_length);
182 zs->next_out = (
uint8 *)
183 mysink->base.bbs_next->bbs_buffer + mysink->bytes_written;
185 mysink->base.bbs_next->bbs_buffer_length - mysink->bytes_written;
198 res = deflate(zs, Z_NO_FLUSH);
199 if (
res == Z_STREAM_ERROR)
200 elog(
ERROR,
"could not compress data: %s", zs->msg);
203 mysink->bytes_written =
204 mysink->base.bbs_next->bbs_buffer_length - zs->avail_out;
210 if (mysink->bytes_written >= mysink->base.bbs_next->bbs_buffer_length)
213 mysink->bytes_written = 0;
225 bbsink_gzip_end_archive(
bbsink *sink)
227 bbsink_gzip *mysink = (bbsink_gzip *) sink;
228 z_stream *zs = &mysink->zstream;
231 zs->next_in = (
uint8 *) mysink->base.bbs_buffer;
239 Assert(mysink->bytes_written < mysink->base.bbs_next->bbs_buffer_length);
240 zs->next_out = (
uint8 *)
241 mysink->base.bbs_next->bbs_buffer + mysink->bytes_written;
243 mysink->base.bbs_next->bbs_buffer_length - mysink->bytes_written;
249 res = deflate(zs, Z_FINISH);
250 if (
res == Z_STREAM_ERROR)
251 elog(
ERROR,
"could not compress data: %s", zs->msg);
254 mysink->bytes_written =
255 mysink->base.bbs_next->bbs_buffer_length - zs->avail_out;
261 if (mysink->bytes_written == 0)
266 mysink->bytes_written = 0;
278 bbsink_gzip_manifest_contents(
bbsink *sink,
size_t len)
289 gzip_palloc(
void *opaque,
unsigned items,
unsigned size)
299 gzip_pfree(
void *opaque,
void *address)
bbsink * bbsink_gzip_new(bbsink *next, pg_compress_specification *compress)
void bbsink_forward_begin_manifest(bbsink *sink)
void bbsink_forward_end_backup(bbsink *sink, XLogRecPtr endptr, TimeLineID endtli)
void bbsink_forward_cleanup(bbsink *sink)
void bbsink_forward_end_archive(bbsink *sink)
void bbsink_forward_end_manifest(bbsink *sink)
static void bbsink_begin_backup(bbsink *sink, bbsink_state *state, int buffer_length)
static void bbsink_begin_archive(bbsink *sink, const char *archive_name)
static void bbsink_archive_contents(bbsink *sink, size_t len)
static void bbsink_manifest_contents(bbsink *sink, size_t len)
#define Assert(condition)
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
if(TABLE==NULL||TABLE_index==NULL)
void pfree(void *pointer)
void * palloc0(Size size)
char * psprintf(const char *fmt,...)
static pg_noinline void Size size
void(* begin_backup)(bbsink *sink)