PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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),
51  /* if you add a new kind, make sure you update "last_default" too */
53  /* some compilers treat enums as signed ints, so we can't use 1 << 31 */
54  RELOPT_KIND_MAX = (1 << 30)
55 } relopt_kind;
56 
57 /* reloption namespaces allowed for heaps -- currently only TOAST */
58 #define HEAP_RELOPT_NAMESPACES { "toast", NULL }
59 
60 /* generic struct to hold shared data */
61 typedef struct relopt_gen
62 {
63  const char *name; /* must be first (used as list termination
64  * marker) */
65  const char *desc;
68  int namelen;
70 } relopt_gen;
71 
72 /* holds a parsed value */
73 typedef struct relopt_value
74 {
76  bool isset;
77  union
78  {
79  bool bool_val;
80  int int_val;
81  double real_val;
82  char *string_val; /* allocated separately */
83  } values;
84 } relopt_value;
85 
86 /* reloptions records for specific variable types */
87 typedef struct relopt_bool
88 {
91 } relopt_bool;
92 
93 typedef struct relopt_int
94 {
97  int min;
98  int max;
99 } relopt_int;
100 
101 typedef struct relopt_real
102 {
104  double default_val;
105  double min;
106  double max;
107 } relopt_real;
108 
109 /* validation routines for strings */
110 typedef void (*validate_string_relopt) (char *value);
111 
112 typedef struct relopt_string
113 {
118  char *default_val;
119 } relopt_string;
120 
121 /* This is the table datatype for fillRelOptions */
122 typedef struct
123 {
124  const char *optname; /* option's name */
125  relopt_type opttype; /* option's datatype */
126  int offset; /* offset of field in result struct */
128 
129 
130 /*
131  * These macros exist for the convenience of amoptions writers (but consider
132  * using fillRelOptions, which is a lot simpler). Beware of multiple
133  * evaluation of arguments!
134  *
135  * The last argument in the HANDLE_*_RELOPTION macros allows the caller to
136  * determine whether the option was set (true), or its value acquired from
137  * defaults (false); it can be passed as (char *) NULL if the caller does not
138  * need this information.
139  *
140  * optname is the option name (a string), var is the variable
141  * on which the value should be stored (e.g. StdRdOptions->fillfactor), and
142  * option is a relopt_value pointer.
143  *
144  * The normal way to use this is to loop on the relopt_value array returned by
145  * parseRelOptions:
146  * for (i = 0; options[i].gen->name; i++)
147  * {
148  * if (HAVE_RELOPTION("fillfactor", options[i])
149  * {
150  * HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
151  * continue;
152  * }
153  * if (HAVE_RELOPTION("default_row_acl", options[i])
154  * {
155  * ...
156  * }
157  * ...
158  * if (validate)
159  * ereport(ERROR,
160  * (errmsg("unknown option")));
161  * }
162  *
163  * Note that this is more or less the same that fillRelOptions does, so only
164  * use this if you need to do something non-standard within some option's
165  * code block.
166  */
167 #define HAVE_RELOPTION(optname, option) \
168  (pg_strncasecmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
169 
170 #define HANDLE_INT_RELOPTION(optname, var, option, wasset) \
171  do { \
172  if (option.isset) \
173  var = option.values.int_val; \
174  else \
175  var = ((relopt_int *) option.gen)->default_val; \
176  (wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
177  } while (0)
178 
179 #define HANDLE_BOOL_RELOPTION(optname, var, option, wasset) \
180  do { \
181  if (option.isset) \
182  var = option.values.bool_val; \
183  else \
184  var = ((relopt_bool *) option.gen)->default_val; \
185  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
186  } while (0)
187 
188 #define HANDLE_REAL_RELOPTION(optname, var, option, wasset) \
189  do { \
190  if (option.isset) \
191  var = option.values.real_val; \
192  else \
193  var = ((relopt_real *) option.gen)->default_val; \
194  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
195  } while (0)
196 
197 /*
198  * Note that this assumes that the variable is already allocated at the tail of
199  * reloptions structure (StdRdOptions or equivalent).
200  *
201  * "base" is a pointer to the reloptions structure, and "offset" is an integer
202  * variable that must be initialized to sizeof(reloptions structure). This
203  * struct must have been allocated with enough space to hold any string option
204  * present, including terminating \0 for every option. SET_VARSIZE() must be
205  * called on the struct with this offset as the second argument, after all the
206  * string options have been processed.
207  */
208 #define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset) \
209  do { \
210  relopt_string *optstring = (relopt_string *) option.gen;\
211  char *string_val; \
212  if (option.isset) \
213  string_val = option.values.string_val; \
214  else if (!optstring->default_isnull) \
215  string_val = optstring->default_val; \
216  else \
217  string_val = NULL; \
218  (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
219  if (string_val == NULL) \
220  var = 0; \
221  else \
222  { \
223  strcpy(((char *)(base)) + (offset), string_val); \
224  var = (offset); \
225  (offset) += strlen(string_val) + 1; \
226  } \
227  } while (0)
228 
229 /*
230  * For use during amoptions: get the strlen of a string option
231  * (either default or the user defined value)
232  */
233 #define GET_STRING_RELOPTION_LEN(option) \
234  ((option).isset ? strlen((option).values.string_val) : \
235  ((relopt_string *) (option).gen)->default_len)
236 
237 /*
238  * For use by code reading options already parsed: get a pointer to the string
239  * value itself. "optstruct" is the StdRdOption struct or equivalent, "member"
240  * is the struct member corresponding to the string option
241  */
242 #define GET_STRING_RELOPTION(optstruct, member) \
243  ((optstruct)->member == 0 ? NULL : \
244  (char *)(optstruct) + (optstruct)->member)
245 
246 
247 extern relopt_kind add_reloption_kind(void);
248 extern void add_bool_reloption(bits32 kinds, char *name, char *desc,
249  bool default_val);
250 extern void add_int_reloption(bits32 kinds, char *name, char *desc,
251  int default_val, int min_val, int max_val);
252 extern void add_real_reloption(bits32 kinds, char *name, char *desc,
253  double default_val, double min_val, double max_val);
254 extern void add_string_reloption(bits32 kinds, char *name, char *desc,
255  char *default_val, validate_string_relopt validator);
256 
257 extern Datum transformRelOptions(Datum oldOptions, List *defList,
258  char *namspace, char *validnsps[],
259  bool ignoreOids, bool isReset);
261 extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
262  amoptions_function amoptions);
263 extern relopt_value *parseRelOptions(Datum options, bool validate,
264  relopt_kind kind, int *numrelopts);
265 extern void *allocateReloptStruct(Size base, relopt_value *options,
266  int numoptions);
267 extern void fillRelOptions(void *rdopts, Size basesize,
268  relopt_value *options, int numoptions,
269  bool validate,
270  const relopt_parse_elt *elems, int nelems);
271 
272 extern bytea *default_reloptions(Datum reloptions, bool validate,
273  relopt_kind kind);
274 extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
275 extern bytea *view_reloptions(Datum reloptions, bool validate);
276 extern bytea *index_reloptions(amoptions_function amoptions, Datum reloptions,
277  bool validate);
278 extern bytea *attribute_reloptions(Datum reloptions, bool validate);
279 extern bytea *tablespace_reloptions(Datum reloptions, bool validate);
281 
282 #endif /* RELOPTIONS_H */
bool bool_val
Definition: reloptions.h:79
static struct @76 value
relopt_type opttype
Definition: reloptions.h:125
List * untransformRelOptions(Datum options)
Definition: reloptions.c:863
char * string_val
Definition: reloptions.h:82
struct relopt_string relopt_string
bytea * view_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1336
const char * desc
Definition: reloptions.h:65
void add_bool_reloption(bits32 kinds, char *name, char *desc, bool default_val)
Definition: reloptions.c:588
int LOCKMODE
Definition: lockdefs.h:26
struct relopt_value relopt_value
LOCKMODE lockmode
Definition: reloptions.h:67
bool default_isnull
Definition: reloptions.h:116
bool default_val
Definition: reloptions.h:90
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int nelems)
Definition: reloptions.c:1198
relopt_gen gen
Definition: reloptions.h:114
relopt_gen * gen
Definition: reloptions.h:75
bytea * index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
Definition: reloptions.c:1404
struct relopt_bool relopt_bool
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:975
struct relopt_real relopt_real
double default_val
Definition: reloptions.h:104
union relopt_value::@40 values
relopt_gen gen
Definition: reloptions.h:89
int namelen
Definition: reloptions.h:68
void add_int_reloption(bits32 kinds, char *name, char *desc, int default_val, int min_val, int max_val)
Definition: reloptions.c:604
struct relopt_gen relopt_gen
void add_real_reloption(bits32 kinds, char *name, char *desc, double default_val, double min_val, double max_val)
Definition: reloptions.c:623
double real_val
Definition: reloptions.h:81
Datum transformRelOptions(Datum oldOptions, List *defList, char *namspace, char *validnsps[], bool ignoreOids, bool isReset)
Definition: reloptions.c:700
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1419
validate_string_relopt validate_cb
Definition: reloptions.h:117
bytea *(* amoptions_function)(Datum reloptions, bool validate)
Definition: amapi.h:102
bits32 kinds
Definition: reloptions.h:66
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1174
uintptr_t Datum
Definition: postgres.h:374
relopt_gen gen
Definition: reloptions.h:95
const char * optname
Definition: reloptions.h:124
LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList)
Definition: reloptions.c:1485
uint32 bits32
Definition: c.h:274
bytea * extractRelOptions(HeapTuple tuple, TupleDesc tupdesc, amoptions_function amoptions)
Definition: reloptions.c:912
relopt_type type
Definition: reloptions.h:69
relopt_type
Definition: reloptions.h:29
struct relopt_int relopt_int
size_t Size
Definition: c.h:353
void(* validate_string_relopt)(char *value)
Definition: reloptions.h:110
relopt_gen gen
Definition: reloptions.h:103
const char * name
Definition: encode.c:521
int default_val
Definition: reloptions.h:96
const char * name
Definition: reloptions.h:63
relopt_kind
Definition: reloptions.h:38
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1275
Definition: c.h:435
char * default_val
Definition: reloptions.h:118
double max
Definition: reloptions.h:106
Definition: pg_list.h:45
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:489
void add_string_reloption(bits32 kinds, char *name, char *desc, char *default_val, validate_string_relopt validator)
Definition: reloptions.c:647
double min
Definition: reloptions.h:105
bytea * heap_reloptions(char relkind, Datum reloptions, bool validate)
Definition: reloptions.c:1368
bytea * tablespace_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1450