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-2018, 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 {
35 } relopt_type;
36 
37 /* kinds supported by reloptions */
38 typedef enum relopt_kind
39 {
40  RELOPT_KIND_HEAP = (1 << 0),
41  RELOPT_KIND_TOAST = (1 << 1),
42  RELOPT_KIND_BTREE = (1 << 2),
43  RELOPT_KIND_HASH = (1 << 3),
44  RELOPT_KIND_GIN = (1 << 4),
45  RELOPT_KIND_GIST = (1 << 5),
48  RELOPT_KIND_SPGIST = (1 << 8),
49  RELOPT_KIND_VIEW = (1 << 9),
50  RELOPT_KIND_BRIN = (1 << 10),
52  /* 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  char *string_val; /* allocated separately */
85  } values;
86 } relopt_value;
87 
88 /* reloptions records for specific variable types */
89 typedef struct relopt_bool
90 {
93 } relopt_bool;
94 
95 typedef struct relopt_int
96 {
99  int min;
100  int max;
101 } relopt_int;
102 
103 typedef struct relopt_real
104 {
106  double default_val;
107  double min;
108  double max;
109 } relopt_real;
110 
111 /* validation routines for strings */
112 typedef void (*validate_string_relopt) (const char *value);
113 
114 typedef struct relopt_string
115 {
120  char *default_val;
121 } relopt_string;
122 
123 /* This is the table datatype for fillRelOptions */
124 typedef struct
125 {
126  const char *optname; /* option's name */
127  relopt_type opttype; /* option's datatype */
128  int offset; /* offset of field in result struct */
130 
131 
132 /*
133  * These macros exist for the convenience of amoptions writers (but consider
134  * using fillRelOptions, which is a lot simpler). Beware of multiple
135  * evaluation of arguments!
136  *
137  * The last argument in the HANDLE_*_RELOPTION macros allows the caller to
138  * determine whether the option was set (true), or its value acquired from
139  * defaults (false); it can be passed as (char *) NULL if the caller does not
140  * need this information.
141  *
142  * optname is the option name (a string), var is the variable
143  * on which the value should be stored (e.g. StdRdOptions->fillfactor), and
144  * option is a relopt_value pointer.
145  *
146  * The normal way to use this is to loop on the relopt_value array returned by
147  * parseRelOptions:
148  * for (i = 0; options[i].gen->name; i++)
149  * {
150  * if (HAVE_RELOPTION("fillfactor", options[i])
151  * {
152  * HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
153  * continue;
154  * }
155  * if (HAVE_RELOPTION("default_row_acl", options[i])
156  * {
157  * ...
158  * }
159  * ...
160  * if (validate)
161  * ereport(ERROR,
162  * (errmsg("unknown option")));
163  * }
164  *
165  * Note that this is more or less the same that fillRelOptions does, so only
166  * use this if you need to do something non-standard within some option's
167  * code block.
168  */
169 #define HAVE_RELOPTION(optname, option) \
170  (strncmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
171 
172 #define HANDLE_INT_RELOPTION(optname, var, option, wasset) \
173  do { \
174  if (option.isset) \
175  var = option.values.int_val; \
176  else \
177  var = ((relopt_int *) option.gen)->default_val; \
178  (wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
179  } while (0)
180 
181 #define HANDLE_BOOL_RELOPTION(optname, var, option, wasset) \
182  do { \
183  if (option.isset) \
184  var = option.values.bool_val; \
185  else \
186  var = ((relopt_bool *) option.gen)->default_val; \
187  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
188  } while (0)
189 
190 #define HANDLE_REAL_RELOPTION(optname, var, option, wasset) \
191  do { \
192  if (option.isset) \
193  var = option.values.real_val; \
194  else \
195  var = ((relopt_real *) option.gen)->default_val; \
196  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
197  } while (0)
198 
199 /*
200  * Note that this assumes that the variable is already allocated at the tail of
201  * reloptions structure (StdRdOptions or equivalent).
202  *
203  * "base" is a pointer to the reloptions structure, and "offset" is an integer
204  * variable that must be initialized to sizeof(reloptions structure). This
205  * struct must have been allocated with enough space to hold any string option
206  * present, including terminating \0 for every option. SET_VARSIZE() must be
207  * called on the struct with this offset as the second argument, after all the
208  * string options have been processed.
209  */
210 #define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset) \
211  do { \
212  relopt_string *optstring = (relopt_string *) option.gen;\
213  char *string_val; \
214  if (option.isset) \
215  string_val = option.values.string_val; \
216  else if (!optstring->default_isnull) \
217  string_val = optstring->default_val; \
218  else \
219  string_val = NULL; \
220  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
221  if (string_val == NULL) \
222  var = 0; \
223  else \
224  { \
225  strcpy(((char *)(base)) + (offset), string_val); \
226  var = (offset); \
227  (offset) += strlen(string_val) + 1; \
228  } \
229  } while (0)
230 
231 /*
232  * For use during amoptions: get the strlen of a string option
233  * (either default or the user defined value)
234  */
235 #define GET_STRING_RELOPTION_LEN(option) \
236  ((option).isset ? strlen((option).values.string_val) : \
237  ((relopt_string *) (option).gen)->default_len)
238 
239 /*
240  * For use by code reading options already parsed: get a pointer to the string
241  * value itself. "optstruct" is the StdRdOption struct or equivalent, "member"
242  * is the struct member corresponding to the string option
243  */
244 #define GET_STRING_RELOPTION(optstruct, member) \
245  ((optstruct)->member == 0 ? NULL : \
246  (char *)(optstruct) + (optstruct)->member)
247 
248 
249 extern relopt_kind add_reloption_kind(void);
250 extern void add_bool_reloption(bits32 kinds, const char *name, const char *desc,
251  bool default_val);
252 extern void add_int_reloption(bits32 kinds, const char *name, const char *desc,
253  int default_val, int min_val, int max_val);
254 extern void add_real_reloption(bits32 kinds, const char *name, const char *desc,
255  double default_val, double min_val, double max_val);
256 extern void add_string_reloption(bits32 kinds, const char *name, const char *desc,
257  const char *default_val, validate_string_relopt validator);
258 
259 extern Datum transformRelOptions(Datum oldOptions, List *defList,
260  const char *namspace, char *validnsps[],
261  bool ignoreOids, bool isReset);
263 extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
264  amoptions_function amoptions);
265 extern relopt_value *parseRelOptions(Datum options, bool validate,
266  relopt_kind kind, int *numrelopts);
267 extern void *allocateReloptStruct(Size base, relopt_value *options,
268  int numoptions);
269 extern void fillRelOptions(void *rdopts, Size basesize,
270  relopt_value *options, int numoptions,
271  bool validate,
272  const relopt_parse_elt *elems, int nelems);
273 
274 extern bytea *default_reloptions(Datum reloptions, bool validate,
275  relopt_kind kind);
276 extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
277 extern bytea *view_reloptions(Datum reloptions, bool validate);
278 extern bytea *index_reloptions(amoptions_function amoptions, Datum reloptions,
279  bool validate);
280 extern bytea *index_generic_reloptions(Datum reloptions, bool validate);
281 extern bytea *attribute_reloptions(Datum reloptions, bool validate);
282 extern bytea *tablespace_reloptions(Datum reloptions, bool validate);
284 
285 #endif /* RELOPTIONS_H */
bool bool_val
Definition: reloptions.h:81
relopt_type opttype
Definition: reloptions.h:127
List * untransformRelOptions(Datum options)
Definition: reloptions.c:934
char * string_val
Definition: reloptions.h:84
struct relopt_string relopt_string
Datum transformRelOptions(Datum oldOptions, List *defList, const char *namspace, char *validnsps[], bool ignoreOids, bool isReset)
Definition: reloptions.c:773
bytea * view_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1408
const char * desc
Definition: reloptions.h:67
int LOCKMODE
Definition: lockdefs.h:26
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)
Definition: reloptions.c:677
LOCKMODE lockmode
Definition: reloptions.h:69
bool default_isnull
Definition: reloptions.h:118
bool default_val
Definition: reloptions.h:92
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int nelems)
Definition: reloptions.c:1266
relopt_gen gen
Definition: reloptions.h:116
void add_real_reloption(bits32 kinds, const char *name, const char *desc, double default_val, double min_val, double max_val)
Definition: reloptions.c:696
relopt_gen * gen
Definition: reloptions.h:77
bytea * index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
Definition: reloptions.c:1478
struct relopt_bool relopt_bool
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:1048
char relkind
Definition: pg_class.h:51
struct relopt_real relopt_real
void add_string_reloption(bits32 kinds, const char *name, const char *desc, const char *default_val, validate_string_relopt validator)
Definition: reloptions.c:720
double default_val
Definition: reloptions.h:106
relopt_gen gen
Definition: reloptions.h:91
int namelen
Definition: reloptions.h:70
struct relopt_gen relopt_gen
double real_val
Definition: reloptions.h:83
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1527
validate_string_relopt validate_cb
Definition: reloptions.h:119
bits32 kinds
Definition: reloptions.h:68
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1242
uintptr_t Datum
Definition: postgres.h:365
relopt_gen gen
Definition: reloptions.h:97
const char * optname
Definition: reloptions.h:126
LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList)
Definition: reloptions.c:1593
static struct @131 value
uint32 bits32
Definition: c.h:334
bytea * extractRelOptions(HeapTuple tuple, TupleDesc tupdesc, amoptions_function amoptions)
Definition: reloptions.c:983
void(* validate_string_relopt)(const char *value)
Definition: reloptions.h:112
relopt_type type
Definition: reloptions.h:71
relopt_type
Definition: reloptions.h:29
struct relopt_int relopt_int
size_t Size
Definition: c.h:433
relopt_gen gen
Definition: reloptions.h:105
void add_bool_reloption(bits32 kinds, const char *name, const char *desc, bool default_val)
Definition: reloptions.c:661
int default_val
Definition: reloptions.h:98
static Datum values[MAXATTR]
Definition: bootstrap.c:164
const char * name
Definition: reloptions.h:65
relopt_kind
Definition: reloptions.h:38
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1343
Definition: c.h:516
bytea *(* amoptions_function)(Datum reloptions, bool validate)
Definition: amapi.h:103
char * default_val
Definition: reloptions.h:120
double max
Definition: reloptions.h:108
Definition: pg_list.h:45
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:562
bytea * index_generic_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1496
double min
Definition: reloptions.h:107
bytea * heap_reloptions(char relkind, Datum reloptions, bool validate)
Definition: reloptions.c:1440
bytea * tablespace_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1558