PostgreSQL Source Code  git master
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)
 

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_compress_algorithm (char *name, pg_compress_algorithm *algorithm)
 
const char * get_compress_algorithm_name (pg_compress_algorithm algorithm)
 
void parse_compress_specification (pg_compress_algorithm algorithm, char *specification, pg_compress_specification *result)
 
char * validate_compress_specification (pg_compress_specification *)
 

Macro Definition Documentation

◆ PG_COMPRESSION_OPTION_WORKERS

#define PG_COMPRESSION_OPTION_WORKERS   (1 << 0)

Definition at line 25 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 17 of file compression.h.

18 {
pg_compress_algorithm
Definition: compression.h:18
@ PG_COMPRESSION_GZIP
Definition: compression.h:20
@ PG_COMPRESSION_LZ4
Definition: compression.h:21
@ PG_COMPRESSION_NONE
Definition: compression.h:19
@ PG_COMPRESSION_ZSTD
Definition: compression.h:22

Function Documentation

◆ get_compress_algorithm_name()

const char* get_compress_algorithm_name ( pg_compress_algorithm  algorithm)

Definition at line 67 of file compression.c.

68 {
69  switch (algorithm)
70  {
72  return "none";
74  return "gzip";
75  case PG_COMPRESSION_LZ4:
76  return "lz4";
78  return "zstd";
79  /* no default, to provoke compiler warnings if values are added */
80  }
81  Assert(false);
82  return "???"; /* placate compiler */
83 }
Assert(fmt[strlen(fmt) - 1] !='\n')

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

Referenced by validate_compress_specification().

◆ parse_compress_algorithm()

bool parse_compress_algorithm ( char *  name,
pg_compress_algorithm algorithm 
)

Definition at line 47 of file compression.c.

48 {
49  if (strcmp(name, "none") == 0)
50  *algorithm = PG_COMPRESSION_NONE;
51  else if (strcmp(name, "gzip") == 0)
52  *algorithm = PG_COMPRESSION_GZIP;
53  else if (strcmp(name, "lz4") == 0)
54  *algorithm = PG_COMPRESSION_LZ4;
55  else if (strcmp(name, "zstd") == 0)
56  *algorithm = PG_COMPRESSION_ZSTD;
57  else
58  return false;
59  return true;
60 }
const char * name
Definition: encode.c:571

References 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 
)

◆ parse_compress_specification()

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

Definition at line 105 of file compression.c.

107 {
108  int bare_level;
109  char *bare_level_endp;
110 
111  /* Initial setup of result object. */
112  result->algorithm = algorithm;
113  result->options = 0;
114  result->parse_error = NULL;
115 
116  /*
117  * Assign a default level depending on the compression method. This may
118  * be enforced later.
119  */
120  switch (result->algorithm)
121  {
122  case PG_COMPRESSION_NONE:
123  result->level = 0;
124  break;
125  case PG_COMPRESSION_LZ4:
126 #ifdef USE_LZ4
127  result->level = 0; /* fast compression mode */
128 #else
129  result->parse_error =
130  psprintf(_("this build does not support compression with %s"),
131  "LZ4");
132 #endif
133  break;
134  case PG_COMPRESSION_ZSTD:
135 #ifdef USE_ZSTD
136  result->level = ZSTD_CLEVEL_DEFAULT;
137 #else
138  result->parse_error =
139  psprintf(_("this build does not support compression with %s"),
140  "ZSTD");
141 #endif
142  break;
143  case PG_COMPRESSION_GZIP:
144 #ifdef HAVE_LIBZ
145  result->level = Z_DEFAULT_COMPRESSION;
146 #else
147  result->parse_error =
148  psprintf(_("this build does not support compression with %s"),
149  "gzip");
150 #endif
151  break;
152  }
153 
154  /* If there is no specification, we're done already. */
155  if (specification == NULL)
156  return;
157 
158  /* As a special case, the specification can be a bare integer. */
159  bare_level = strtol(specification, &bare_level_endp, 10);
160  if (specification != bare_level_endp && *bare_level_endp == '\0')
161  {
162  result->level = bare_level;
163  return;
164  }
165 
166  /* Look for comma-separated keyword or keyword=value entries. */
167  while (1)
168  {
169  char *kwstart;
170  char *kwend;
171  char *vstart;
172  char *vend;
173  int kwlen;
174  int vlen;
175  bool has_value;
176  char *keyword;
177  char *value;
178 
179  /* Figure start, end, and length of next keyword and any value. */
180  kwstart = kwend = specification;
181  while (*kwend != '\0' && *kwend != ',' && *kwend != '=')
182  ++kwend;
183  kwlen = kwend - kwstart;
184  if (*kwend != '=')
185  {
186  vstart = vend = NULL;
187  vlen = 0;
188  has_value = false;
189  }
190  else
191  {
192  vstart = vend = kwend + 1;
193  while (*vend != '\0' && *vend != ',')
194  ++vend;
195  vlen = vend - vstart;
196  has_value = true;
197  }
198 
199  /* Reject empty keyword. */
200  if (kwlen == 0)
201  {
202  result->parse_error =
203  pstrdup(_("found empty string where a compression option was expected"));
204  break;
205  }
206 
207  /* Extract keyword and value as separate C strings. */
208  keyword = palloc(kwlen + 1);
209  memcpy(keyword, kwstart, kwlen);
210  keyword[kwlen] = '\0';
211  if (!has_value)
212  value = NULL;
213  else
214  {
215  value = palloc(vlen + 1);
216  memcpy(value, vstart, vlen);
217  value[vlen] = '\0';
218  }
219 
220  /* Handle whatever keyword we found. */
221  if (strcmp(keyword, "level") == 0)
222  {
223  result->level = expect_integer_value(keyword, value, result);
224 
225  /*
226  * No need to set a flag in "options", there is a default level
227  * set at least thanks to the logic above.
228  */
229  }
230  else if (strcmp(keyword, "workers") == 0)
231  {
232  result->workers = expect_integer_value(keyword, value, result);
234  }
235  else
236  result->parse_error =
237  psprintf(_("unrecognized compression option: \"%s\""), keyword);
238 
239  /* Release memory, just to be tidy. */
240  pfree(keyword);
241  if (value != NULL)
242  pfree(value);
243 
244  /*
245  * If we got an error or have reached the end of the string, stop.
246  *
247  * If there is no value, then the end of the keyword might have been
248  * the end of the string. If there is a value, then the end of the
249  * keyword cannot have been the end of the string, but the end of the
250  * value might have been.
251  */
252  if (result->parse_error != NULL ||
253  (vend == NULL ? *kwend == '\0' : *vend == '\0'))
254  break;
255 
256  /* Advance to next entry and loop around. */
257  specification = vend == NULL ? kwend + 1 : vend + 1;
258  }
259 }
static int expect_integer_value(char *keyword, char *value, pg_compress_specification *result)
Definition: compression.c:268
#define PG_COMPRESSION_OPTION_WORKERS
Definition: compression.h:25
#define _(x)
Definition: elog.c:91
static struct @143 value
char * pstrdup(const char *in)
Definition: mcxt.c:1624
void pfree(void *pointer)
Definition: mcxt.c:1436
void * palloc(Size size)
Definition: mcxt.c:1210
#define Z_DEFAULT_COMPRESSION
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
pg_compress_algorithm algorithm
Definition: compression.h:29

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

Referenced by main(), and parse_basebackup_options().

◆ validate_compress_specification()

char* validate_compress_specification ( pg_compress_specification spec)

Definition at line 300 of file compression.c.

301 {
302  int min_level = 1;
303  int max_level = 1;
304  int default_level = 0;
305 
306  /* If it didn't even parse OK, it's definitely no good. */
307  if (spec->parse_error != NULL)
308  return spec->parse_error;
309 
310  /*
311  * Check that the algorithm expects a compression level and it is within
312  * the legal range for the algorithm.
313  */
314  switch (spec->algorithm)
315  {
316  case PG_COMPRESSION_GZIP:
317  max_level = 9;
318 #ifdef HAVE_LIBZ
319  default_level = Z_DEFAULT_COMPRESSION;
320 #endif
321  break;
322  case PG_COMPRESSION_LZ4:
323  max_level = 12;
324  default_level = 0; /* fast mode */
325  break;
326  case PG_COMPRESSION_ZSTD:
327 #ifdef USE_ZSTD
328  max_level = ZSTD_maxCLevel();
329  min_level = ZSTD_minCLevel();
330  default_level = ZSTD_CLEVEL_DEFAULT;
331 #endif
332  break;
333  case PG_COMPRESSION_NONE:
334  if (spec->level != 0)
335  return psprintf(_("compression algorithm \"%s\" does not accept a compression level"),
337  break;
338  }
339 
340  if ((spec->level < min_level || spec->level > max_level) &&
341  spec->level != default_level)
342  return psprintf(_("compression algorithm \"%s\" expects a compression level between %d and %d (default at %d)"),
344  min_level, max_level, default_level);
345 
346  /*
347  * Of the compression algorithms that we currently support, only zstd
348  * allows parallel workers.
349  */
350  if ((spec->options & PG_COMPRESSION_OPTION_WORKERS) != 0 &&
351  (spec->algorithm != PG_COMPRESSION_ZSTD))
352  {
353  return psprintf(_("compression algorithm \"%s\" does not accept a worker count"),
355  }
356 
357  return NULL;
358 }
const char * get_compress_algorithm_name(pg_compress_algorithm algorithm)
Definition: compression.c:67

References _, pg_compress_specification::algorithm, get_compress_algorithm_name(), pg_compress_specification::level, pg_compress_specification::options, pg_compress_specification::parse_error, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_OPTION_WORKERS, PG_COMPRESSION_ZSTD, psprintf(), and Z_DEFAULT_COMPRESSION.

Referenced by main(), and parse_basebackup_options().