PostgreSQL Source Code git master
Loading...
Searching...
No Matches
compression.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_compress_specification
 

Macros

#define PG_COMPRESSION_OPTION_WORKERS   (1 << 0)
 
#define PG_COMPRESSION_OPTION_LONG_DISTANCE   (1 << 1)
 

Typedefs

typedef enum pg_compress_algorithm pg_compress_algorithm
 
typedef struct pg_compress_specification pg_compress_specification
 

Enumerations

enum  pg_compress_algorithm { PG_COMPRESSION_NONE , PG_COMPRESSION_GZIP , PG_COMPRESSION_LZ4 , PG_COMPRESSION_ZSTD }
 

Functions

void parse_compress_options (const char *option, char **algorithm, char **detail)
 
bool parse_tar_compress_algorithm (const char *fname, pg_compress_algorithm *algorithm)
 
bool parse_compress_algorithm (char *name, pg_compress_algorithm *algorithm)
 
const charget_compress_algorithm_name (pg_compress_algorithm algorithm)
 
void parse_compress_specification (pg_compress_algorithm algorithm, char *specification, pg_compress_specification *result)
 
charvalidate_compress_specification (pg_compress_specification *)
 

Macro Definition Documentation

◆ PG_COMPRESSION_OPTION_LONG_DISTANCE

#define PG_COMPRESSION_OPTION_LONG_DISTANCE   (1 << 1)

Definition at line 30 of file compression.h.

◆ PG_COMPRESSION_OPTION_WORKERS

#define PG_COMPRESSION_OPTION_WORKERS   (1 << 0)

Definition at line 29 of file compression.h.

Typedef Documentation

◆ pg_compress_algorithm

◆ pg_compress_specification

Enumeration Type Documentation

◆ pg_compress_algorithm

Enumerator
PG_COMPRESSION_NONE 
PG_COMPRESSION_GZIP 
PG_COMPRESSION_LZ4 
PG_COMPRESSION_ZSTD 

Definition at line 21 of file compression.h.

22{
pg_compress_algorithm
Definition compression.h:22
@ PG_COMPRESSION_GZIP
Definition compression.h:24
@ PG_COMPRESSION_LZ4
Definition compression.h:25
@ PG_COMPRESSION_NONE
Definition compression.h:23
@ PG_COMPRESSION_ZSTD
Definition compression.h:26

Function Documentation

◆ get_compress_algorithm_name()

const char * get_compress_algorithm_name ( pg_compress_algorithm  algorithm)
extern

Definition at line 99 of file compression.c.

100{
101 switch (algorithm)
102 {
104 return "none";
106 return "gzip";
108 return "lz4";
110 return "zstd";
111 /* no default, to provoke compiler warnings if values are added */
112 }
113 Assert(false);
114 return "???"; /* placate compiler */
115}
#define Assert(condition)
Definition c.h:945

References Assert, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, and PG_COMPRESSION_ZSTD.

Referenced by PrintTOCSummary(), supports_compression(), and validate_compress_specification().

◆ parse_compress_algorithm()

bool parse_compress_algorithm ( char name,
pg_compress_algorithm algorithm 
)
extern

Definition at line 79 of file compression.c.

80{
81 if (strcmp(name, "none") == 0)
82 *algorithm = PG_COMPRESSION_NONE;
83 else if (strcmp(name, "gzip") == 0)
84 *algorithm = PG_COMPRESSION_GZIP;
85 else if (strcmp(name, "lz4") == 0)
86 *algorithm = PG_COMPRESSION_LZ4;
87 else if (strcmp(name, "zstd") == 0)
88 *algorithm = PG_COMPRESSION_ZSTD;
89 else
90 return false;
91 return true;
92}
static int fb(int x)
const char * name

References fb(), name, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, and PG_COMPRESSION_ZSTD.

Referenced by main(), and parse_basebackup_options().

◆ parse_compress_options()

void parse_compress_options ( const char option,
char **  algorithm,
char **  detail 
)
extern

◆ parse_compress_specification()

void parse_compress_specification ( pg_compress_algorithm  algorithm,
char specification,
pg_compress_specification result 
)
extern

Definition at line 137 of file compression.c.

139{
140 int bare_level;
141 char *bare_level_endp;
142
143 /* Initial setup of result object. */
144 result->algorithm = algorithm;
145 result->options = 0;
146 result->parse_error = NULL;
147
148 /*
149 * Assign a default level depending on the compression method. This may
150 * be enforced later.
151 */
152 switch (result->algorithm)
153 {
155 result->level = 0;
156 break;
158#ifdef USE_LZ4
159 result->level = 0; /* fast compression mode */
160#else
161 result->parse_error =
162 psprintf(_("this build does not support compression with %s"),
163 "LZ4");
164#endif
165 break;
167#ifdef USE_ZSTD
168 result->level = ZSTD_CLEVEL_DEFAULT;
169#else
170 result->parse_error =
171 psprintf(_("this build does not support compression with %s"),
172 "ZSTD");
173#endif
174 break;
176#ifdef HAVE_LIBZ
178#else
179 result->parse_error =
180 psprintf(_("this build does not support compression with %s"),
181 "gzip");
182#endif
183 break;
184 }
185
186 /* If there is no specification, we're done already. */
187 if (specification == NULL)
188 return;
189
190 /* As a special case, the specification can be a bare integer. */
193 {
194 result->level = bare_level;
195 return;
196 }
197
198 /* Look for comma-separated keyword or keyword=value entries. */
199 while (1)
200 {
201 char *kwstart;
202 char *kwend;
203 char *vstart;
204 char *vend;
205 int kwlen;
206 int vlen;
207 bool has_value;
208 char *keyword;
209 char *value;
210
211 /* Figure start, end, and length of next keyword and any value. */
213 while (*kwend != '\0' && *kwend != ',' && *kwend != '=')
214 ++kwend;
215 kwlen = kwend - kwstart;
216 if (*kwend != '=')
217 {
218 vstart = vend = NULL;
219 vlen = 0;
220 has_value = false;
221 }
222 else
223 {
224 vstart = vend = kwend + 1;
225 while (*vend != '\0' && *vend != ',')
226 ++vend;
227 vlen = vend - vstart;
228 has_value = true;
229 }
230
231 /* Reject empty keyword. */
232 if (kwlen == 0)
233 {
234 result->parse_error =
235 pstrdup(_("found empty string where a compression option was expected"));
236 break;
237 }
238
239 /* Extract keyword and value as separate C strings. */
240 keyword = palloc(kwlen + 1);
241 memcpy(keyword, kwstart, kwlen);
242 keyword[kwlen] = '\0';
243 if (!has_value)
244 value = NULL;
245 else
246 {
247 value = palloc(vlen + 1);
249 value[vlen] = '\0';
250 }
251
252 /* Handle whatever keyword we found. */
253 if (strcmp(keyword, "level") == 0)
254 {
255 result->level = expect_integer_value(keyword, value, result);
256
257 /*
258 * No need to set a flag in "options", there is a default level
259 * set at least thanks to the logic above.
260 */
261 }
262 else if (strcmp(keyword, "workers") == 0)
263 {
264 result->workers = expect_integer_value(keyword, value, result);
266 }
267 else if (strcmp(keyword, "long") == 0)
268 {
269 result->long_distance = expect_boolean_value(keyword, value, result);
271 }
272 else
273 result->parse_error =
274 psprintf(_("unrecognized compression option: \"%s\""), keyword);
275
276 /* Release memory, just to be tidy. */
277 pfree(keyword);
278 if (value != NULL)
279 pfree(value);
280
281 /*
282 * If we got an error or have reached the end of the string, stop.
283 *
284 * If there is no value, then the end of the keyword might have been
285 * the end of the string. If there is a value, then the end of the
286 * keyword cannot have been the end of the string, but the end of the
287 * value might have been.
288 */
289 if (result->parse_error != NULL ||
290 (vend == NULL ? *kwend == '\0' : *vend == '\0'))
291 break;
292
293 /* Advance to next entry and loop around. */
294 specification = vend == NULL ? kwend + 1 : vend + 1;
295 }
296}
static bool expect_boolean_value(char *keyword, char *value, pg_compress_specification *result)
static int expect_integer_value(char *keyword, char *value, pg_compress_specification *result)
#define PG_COMPRESSION_OPTION_WORKERS
Definition compression.h:29
#define PG_COMPRESSION_OPTION_LONG_DISTANCE
Definition compression.h:30
#define _(x)
Definition elog.c:95
static struct @174 value
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
pg_compress_algorithm algorithm
Definition compression.h:34

References _, pg_compress_specification::algorithm, expect_boolean_value(), expect_integer_value(), fb(), pg_compress_specification::level, pg_compress_specification::long_distance, pg_compress_specification::options, palloc(), pg_compress_specification::parse_error, pfree(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_OPTION_LONG_DISTANCE, PG_COMPRESSION_OPTION_WORKERS, PG_COMPRESSION_ZSTD, psprintf(), pstrdup(), value, and pg_compress_specification::workers.

Referenced by main(), and parse_basebackup_options().

◆ parse_tar_compress_algorithm()

bool parse_tar_compress_algorithm ( const char fname,
pg_compress_algorithm algorithm 
)
extern

Definition at line 49 of file compression.c.

50{
51 size_t fname_len = strlen(fname);
52
53 if (fname_len >= 4 &&
54 strcmp(fname + fname_len - 4, ".tar") == 0)
55 *algorithm = PG_COMPRESSION_NONE;
56 else if (fname_len >= 4 &&
57 strcmp(fname + fname_len - 4, ".tgz") == 0)
58 *algorithm = PG_COMPRESSION_GZIP;
59 else if (fname_len >= 7 &&
60 strcmp(fname + fname_len - 7, ".tar.gz") == 0)
61 *algorithm = PG_COMPRESSION_GZIP;
62 else if (fname_len >= 8 &&
63 strcmp(fname + fname_len - 8, ".tar.lz4") == 0)
64 *algorithm = PG_COMPRESSION_LZ4;
65 else if (fname_len >= 8 &&
66 strcmp(fname + fname_len - 8, ".tar.zst") == 0)
67 *algorithm = PG_COMPRESSION_ZSTD;
68 else
69 return false;
70
71 return true;
72}

References fb(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, and PG_COMPRESSION_ZSTD.

Referenced by CreateBackupStreamer(), main(), and precheck_tar_backup_file().

◆ validate_compress_specification()

char * validate_compress_specification ( pg_compress_specification spec)
extern

Definition at line 374 of file compression.c.

375{
376 int min_level = 1;
377 int max_level = 1;
378 int default_level = 0;
379
380 /* If it didn't even parse OK, it's definitely no good. */
381 if (spec->parse_error != NULL)
382 return spec->parse_error;
383
384 /*
385 * Check that the algorithm expects a compression level and it is within
386 * the legal range for the algorithm.
387 */
388 switch (spec->algorithm)
389 {
391 max_level = 9;
392#ifdef HAVE_LIBZ
394#endif
395 break;
397 max_level = 12;
398 default_level = 0; /* fast mode */
399 break;
401#ifdef USE_ZSTD
405#endif
406 break;
408 if (spec->level != 0)
409 return psprintf(_("compression algorithm \"%s\" does not accept a compression level"),
411 break;
412 }
413
414 if ((spec->level < min_level || spec->level > max_level) &&
415 spec->level != default_level)
416 return psprintf(_("compression algorithm \"%s\" expects a compression level between %d and %d (default at %d)"),
419
420 /*
421 * Of the compression algorithms that we currently support, only zstd
422 * allows parallel workers.
423 */
424 if ((spec->options & PG_COMPRESSION_OPTION_WORKERS) != 0 &&
425 (spec->algorithm != PG_COMPRESSION_ZSTD))
426 {
427 return psprintf(_("compression algorithm \"%s\" does not accept a worker count"),
429 }
430
431 /*
432 * Of the compression algorithms that we currently support, only zstd
433 * supports long-distance mode.
434 */
435 if ((spec->options & PG_COMPRESSION_OPTION_LONG_DISTANCE) != 0 &&
436 (spec->algorithm != PG_COMPRESSION_ZSTD))
437 {
438 return psprintf(_("compression algorithm \"%s\" does not support long-distance mode"),
440 }
441
442 return NULL;
443}
const char * get_compress_algorithm_name(pg_compress_algorithm algorithm)
Definition compression.c:99

References _, fb(), get_compress_algorithm_name(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_OPTION_LONG_DISTANCE, PG_COMPRESSION_OPTION_WORKERS, PG_COMPRESSION_ZSTD, and psprintf().

Referenced by main(), and parse_basebackup_options().