PostgreSQL Source Code  git master
reloptions.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * reloptions.h
4  * Core support for relation and tablespace options (pg_class.reloptions
5  * and pg_tablespace.spcoptions)
6  *
7  * Note: the functions dealing with text-array reloptions values declare
8  * them as Datum, not ArrayType *, to avoid needing to include array.h
9  * into a lot of low-level code.
10  *
11  *
12  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * src/include/access/reloptions.h
16  *
17  *-------------------------------------------------------------------------
18  */
19 #ifndef RELOPTIONS_H
20 #define RELOPTIONS_H
21 
22 #include "access/amapi.h"
23 #include "access/htup.h"
24 #include "access/tupdesc.h"
25 #include "nodes/pg_list.h"
26 #include "storage/lock.h"
27 
28 /* types supported by reloptions */
29 typedef enum relopt_type
30 {
36 } relopt_type;
37 
38 /* kinds supported by reloptions */
39 typedef enum relopt_kind
40 {
41  RELOPT_KIND_HEAP = (1 << 0),
42  RELOPT_KIND_TOAST = (1 << 1),
43  RELOPT_KIND_BTREE = (1 << 2),
44  RELOPT_KIND_HASH = (1 << 3),
45  RELOPT_KIND_GIN = (1 << 4),
46  RELOPT_KIND_GIST = (1 << 5),
49  RELOPT_KIND_SPGIST = (1 << 8),
50  RELOPT_KIND_VIEW = (1 << 9),
51  RELOPT_KIND_BRIN = (1 << 10),
53  /* if you add a new kind, make sure you update "last_default" too */
55  /* some compilers treat enums as signed ints, so we can't use 1 << 31 */
56  RELOPT_KIND_MAX = (1 << 30)
57 } relopt_kind;
58 
59 /* reloption namespaces allowed for heaps -- currently only TOAST */
60 #define HEAP_RELOPT_NAMESPACES { "toast", NULL }
61 
62 /* generic struct to hold shared data */
63 typedef struct relopt_gen
64 {
65  const char *name; /* must be first (used as list termination
66  * marker) */
67  const char *desc;
70  int namelen;
72 } relopt_gen;
73 
74 /* holds a parsed value */
75 typedef struct relopt_value
76 {
78  bool isset;
79  union
80  {
81  bool bool_val;
82  int int_val;
83  double real_val;
84  int enum_val;
85  char *string_val; /* allocated separately */
86  } values;
87 } relopt_value;
88 
89 /* reloptions records for specific variable types */
90 typedef struct relopt_bool
91 {
94 } relopt_bool;
95 
96 typedef struct relopt_int
97 {
100  int min;
101  int max;
102 } relopt_int;
103 
104 typedef struct relopt_real
105 {
107  double default_val;
108  double min;
109  double max;
110 } relopt_real;
111 
112 /*
113  * relopt_enum_elt_def -- One member of the array of acceptable values
114  * of an enum reloption.
115  */
116 typedef struct relopt_enum_elt_def
117 {
118  const char *string_val;
121 
122 typedef struct relopt_enum
123 {
127  const char *detailmsg;
128  /* null-terminated array of members */
129 } relopt_enum;
130 
131 /* validation routines for strings */
132 typedef void (*validate_string_relopt) (const char *value);
133 
134 typedef struct relopt_string
135 {
140  char *default_val;
141 } relopt_string;
142 
143 /* This is the table datatype for fillRelOptions */
144 typedef struct
145 {
146  const char *optname; /* option's name */
147  relopt_type opttype; /* option's datatype */
148  int offset; /* offset of field in result struct */
150 
151 
152 /*
153  * These macros exist for the convenience of amoptions writers (but consider
154  * using fillRelOptions, which is a lot simpler). Beware of multiple
155  * evaluation of arguments!
156  *
157  * The last argument in the HANDLE_*_RELOPTION macros allows the caller to
158  * determine whether the option was set (true), or its value acquired from
159  * defaults (false); it can be passed as (char *) NULL if the caller does not
160  * need this information.
161  *
162  * optname is the option name (a string), var is the variable
163  * on which the value should be stored (e.g. StdRdOptions->fillfactor), and
164  * option is a relopt_value pointer.
165  *
166  * The normal way to use this is to loop on the relopt_value array returned by
167  * parseRelOptions:
168  * for (i = 0; options[i].gen->name; i++)
169  * {
170  * if (HAVE_RELOPTION("fillfactor", options[i])
171  * {
172  * HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
173  * continue;
174  * }
175  * if (HAVE_RELOPTION("default_row_acl", options[i])
176  * {
177  * ...
178  * }
179  * ...
180  * if (validate)
181  * ereport(ERROR,
182  * (errmsg("unknown option")));
183  * }
184  *
185  * Note that this is more or less the same that fillRelOptions does, so only
186  * use this if you need to do something non-standard within some option's
187  * code block.
188  */
189 #define HAVE_RELOPTION(optname, option) \
190  (strncmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
191 
192 #define HANDLE_INT_RELOPTION(optname, var, option, wasset) \
193  do { \
194  if (option.isset) \
195  var = option.values.int_val; \
196  else \
197  var = ((relopt_int *) option.gen)->default_val; \
198  (wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
199  } while (0)
200 
201 #define HANDLE_BOOL_RELOPTION(optname, var, option, wasset) \
202  do { \
203  if (option.isset) \
204  var = option.values.bool_val; \
205  else \
206  var = ((relopt_bool *) option.gen)->default_val; \
207  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
208  } while (0)
209 
210 #define HANDLE_REAL_RELOPTION(optname, var, option, wasset) \
211  do { \
212  if (option.isset) \
213  var = option.values.real_val; \
214  else \
215  var = ((relopt_real *) option.gen)->default_val; \
216  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
217  } while (0)
218 
219 /*
220  * Note that this assumes that the variable is already allocated at the tail of
221  * reloptions structure (StdRdOptions or equivalent).
222  *
223  * "base" is a pointer to the reloptions structure, and "offset" is an integer
224  * variable that must be initialized to sizeof(reloptions structure). This
225  * struct must have been allocated with enough space to hold any string option
226  * present, including terminating \0 for every option. SET_VARSIZE() must be
227  * called on the struct with this offset as the second argument, after all the
228  * string options have been processed.
229  */
230 #define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset) \
231  do { \
232  relopt_string *optstring = (relopt_string *) option.gen;\
233  char *string_val; \
234  if (option.isset) \
235  string_val = option.values.string_val; \
236  else if (!optstring->default_isnull) \
237  string_val = optstring->default_val; \
238  else \
239  string_val = NULL; \
240  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
241  if (string_val == NULL) \
242  var = 0; \
243  else \
244  { \
245  strcpy(((char *)(base)) + (offset), string_val); \
246  var = (offset); \
247  (offset) += strlen(string_val) + 1; \
248  } \
249  } while (0)
250 
251 /*
252  * For use during amoptions: get the strlen of a string option
253  * (either default or the user defined value)
254  */
255 #define GET_STRING_RELOPTION_LEN(option) \
256  ((option).isset ? strlen((option).values.string_val) : \
257  ((relopt_string *) (option).gen)->default_len)
258 
259 /*
260  * For use by code reading options already parsed: get a pointer to the string
261  * value itself. "optstruct" is the StdRdOptions struct or equivalent, "member"
262  * is the struct member corresponding to the string option
263  */
264 #define GET_STRING_RELOPTION(optstruct, member) \
265  ((optstruct)->member == 0 ? NULL : \
266  (char *)(optstruct) + (optstruct)->member)
267 
268 
269 extern relopt_kind add_reloption_kind(void);
270 extern void add_bool_reloption(bits32 kinds, const char *name, const char *desc,
271  bool default_val, LOCKMODE lockmode);
272 extern void add_int_reloption(bits32 kinds, const char *name, const char *desc,
273  int default_val, int min_val, int max_val,
275 extern void add_real_reloption(bits32 kinds, const char *name, const char *desc,
276  double default_val, double min_val, double max_val,
278 extern void add_enum_reloption(bits32 kinds, const char *name, const char *desc,
279  relopt_enum_elt_def *members, int default_val,
280  const char *detailmsg, LOCKMODE lockmode);
281 extern void add_string_reloption(bits32 kinds, const char *name, const char *desc,
282  const char *default_val, validate_string_relopt validator,
284 
285 extern Datum transformRelOptions(Datum oldOptions, List *defList,
286  const char *namspace, char *validnsps[],
287  bool acceptOidsOff, bool isReset);
289 extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
290  amoptions_function amoptions);
291 extern relopt_value *parseRelOptions(Datum options, bool validate,
292  relopt_kind kind, int *numrelopts);
293 extern void *allocateReloptStruct(Size base, relopt_value *options,
294  int numoptions);
295 extern void fillRelOptions(void *rdopts, Size basesize,
296  relopt_value *options, int numoptions,
297  bool validate,
298  const relopt_parse_elt *elems, int nelems);
299 
300 extern bytea *default_reloptions(Datum reloptions, bool validate,
301  relopt_kind kind);
302 extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
303 extern bytea *view_reloptions(Datum reloptions, bool validate);
304 extern bytea *index_reloptions(amoptions_function amoptions, Datum reloptions,
305  bool validate);
306 extern bytea *attribute_reloptions(Datum reloptions, bool validate);
307 extern bytea *tablespace_reloptions(Datum reloptions, bool validate);
309 
310 #endif /* RELOPTIONS_H */
bool bool_val
Definition: reloptions.h:81
relopt_type opttype
Definition: reloptions.h:147
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1030
relopt_gen gen
Definition: reloptions.h:124
char * string_val
Definition: reloptions.h:85
struct relopt_string relopt_string
bytea * view_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1544
const char * desc
Definition: reloptions.h:67
int LOCKMODE
Definition: lockdefs.h:26
struct relopt_enum relopt_enum
struct relopt_value relopt_value
void add_int_reloption(bits32 kinds, const char *name, const char *desc, int default_val, int min_val, int max_val, LOCKMODE lockmode)
Definition: reloptions.c:730
LOCKMODE lockmode
Definition: reloptions.h:69
void add_real_reloption(bits32 kinds, const char *name, const char *desc, double default_val, double min_val, double max_val, LOCKMODE lockmode)
Definition: reloptions.c:749
static struct @145 value
bool default_isnull
Definition: reloptions.h:138
bool default_val
Definition: reloptions.h:93
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int nelems)
Definition: reloptions.c:1393
relopt_gen gen
Definition: reloptions.h:136
relopt_gen * gen
Definition: reloptions.h:77
bytea * index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
Definition: reloptions.c:1614
struct relopt_bool relopt_bool
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:1144
char relkind
Definition: pg_class.h:81
struct relopt_real relopt_real
double default_val
Definition: reloptions.h:107
const char * detailmsg
Definition: reloptions.h:127
relopt_gen gen
Definition: reloptions.h:92
int namelen
Definition: reloptions.h:70
relopt_enum_elt_def * members
Definition: reloptions.h:125
struct relopt_gen relopt_gen
void add_bool_reloption(bits32 kinds, const char *name, const char *desc, bool default_val, LOCKMODE lockmode)
Definition: reloptions.c:713
double real_val
Definition: reloptions.h:83
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1629
validate_string_relopt validate_cb
Definition: reloptions.h:139
bits32 kinds
Definition: reloptions.h:68
int default_val
Definition: reloptions.h:126
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1369
uintptr_t Datum
Definition: postgres.h:367
relopt_gen gen
Definition: reloptions.h:98
const char * optname
Definition: reloptions.h:146
LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList)
Definition: reloptions.c:1695
Datum transformRelOptions(Datum oldOptions, List *defList, const char *namspace, char *validnsps[], bool acceptOidsOff, bool isReset)
Definition: reloptions.c:854
void add_string_reloption(bits32 kinds, const char *name, const char *desc, const char *default_val, validate_string_relopt validator, LOCKMODE lockmode)
Definition: reloptions.c:801
uint32 bits32
Definition: c.h:367
bytea * extractRelOptions(HeapTuple tuple, TupleDesc tupdesc, amoptions_function amoptions)
Definition: reloptions.c:1079
void(* validate_string_relopt)(const char *value)
Definition: reloptions.h:132
relopt_type type
Definition: reloptions.h:71
relopt_type
Definition: reloptions.h:29
struct relopt_int relopt_int
struct relopt_enum_elt_def relopt_enum_elt_def
size_t Size
Definition: c.h:466
const char * string_val
Definition: reloptions.h:118
relopt_gen gen
Definition: reloptions.h:106
void add_enum_reloption(bits32 kinds, const char *name, const char *desc, relopt_enum_elt_def *members, int default_val, const char *detailmsg, LOCKMODE lockmode)
Definition: reloptions.c:776
int default_val
Definition: reloptions.h:99
static Datum values[MAXATTR]
Definition: bootstrap.c:167
const char * name
Definition: reloptions.h:65
relopt_kind
Definition: reloptions.h:39
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1475
Definition: c.h:549
bytea *(* amoptions_function)(Datum reloptions, bool validate)
Definition: amapi.h:103
char * default_val
Definition: reloptions.h:140
double max
Definition: reloptions.h:109
Definition: pg_list.h:50
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:609
double min
Definition: reloptions.h:108
bytea * heap_reloptions(char relkind, Datum reloptions, bool validate)
Definition: reloptions.c:1576
bytea * tablespace_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1660