1 /*-------------------------------------------------------------------------
4 * Core support for relation options (pg_class.reloptions)
6 * Note: the functions dealing with text-array reloptions values declare
7 * them as Datum, not ArrayType *, to avoid needing to include array.h
8 * into a lot of low-level code.
11 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
16 *-------------------------------------------------------------------------
21 #include "access/htup.h"
22 #include "nodes/pg_list.h"
24 /* types supported by reloptions */
25 typedef enum relopt_type
33 /* kinds supported by reloptions */
34 typedef enum relopt_kind
36 RELOPT_KIND_HEAP
= (1 << 0),
37 RELOPT_KIND_TOAST
= (1 << 1),
38 RELOPT_KIND_BTREE
= (1 << 2),
39 RELOPT_KIND_HASH
= (1 << 3),
40 RELOPT_KIND_GIN
= (1 << 4),
41 RELOPT_KIND_GIST
= (1 << 5),
42 /* if you add a new kind, make sure you update "last_default" too */
43 RELOPT_KIND_LAST_DEFAULT
= RELOPT_KIND_GIST
,
44 /* some compilers treat enums as signed ints, so we can't use 1 << 31 */
45 RELOPT_KIND_MAX
= (1 << 30)
48 /* reloption namespaces allowed for heaps -- currently only TOAST */
49 #define HEAP_RELOPT_NAMESPACES { "toast", NULL }
51 /* generic struct to hold shared data */
52 typedef struct relopt_gen
54 const char *name
; /* must be first (used as list termination
62 /* holds a parsed value */
63 typedef struct relopt_value
72 char *string_val
; /* allocated separately */
76 /* reloptions records for specific variable types */
77 typedef struct relopt_bool
83 typedef struct relopt_int
91 typedef struct relopt_real
99 /* validation routines for strings */
100 typedef void (*validate_string_relopt
) (char *value
);
102 typedef struct relopt_string
107 validate_string_relopt validate_cb
;
108 char default_val
[1]; /* variable length, zero-terminated */
111 /* This is the table datatype for fillRelOptions */
114 const char *optname
; /* option's name */
115 relopt_type opttype
; /* option's datatype */
116 int offset
; /* offset of field in result struct */
121 * These macros exist for the convenience of amoptions writers (but consider
122 * using fillRelOptions, which is a lot simpler). Beware of multiple
123 * evaluation of arguments!
125 * The last argument in the HANDLE_*_RELOPTION macros allows the caller to
126 * determine whether the option was set (true), or its value acquired from
127 * defaults (false); it can be passed as (char *) NULL if the caller does not
128 * need this information.
130 * optname is the option name (a string), var is the variable
131 * on which the value should be stored (e.g. StdRdOptions->fillfactor), and
132 * option is a relopt_value pointer.
134 * The normal way to use this is to loop on the relopt_value array returned by
136 * for (i = 0; options[i].gen->name; i++)
138 * if (HAVE_RELOPTION("fillfactor", options[i])
140 * HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
143 * if (HAVE_RELOPTION("default_row_acl", options[i])
150 * (errmsg("unknown option")));
153 * Note that this is more or less the same that fillRelOptions does, so only
154 * use this if you need to do something non-standard within some option's
157 #define HAVE_RELOPTION(optname, option) \
158 (pg_strncasecmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
160 #define HANDLE_INT_RELOPTION(optname, var, option, wasset) \
163 var = option.values.int_val; \
165 var = ((relopt_int *) option.gen)->default_val; \
166 (wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
169 #define HANDLE_BOOL_RELOPTION(optname, var, option, wasset) \
172 var = option.values.bool_val; \
174 var = ((relopt_bool *) option.gen)->default_val; \
175 (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
178 #define HANDLE_REAL_RELOPTION(optname, var, option, wasset) \
181 var = option.values.real_val; \
183 var = ((relopt_real *) option.gen)->default_val; \
184 (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
188 * Note that this assumes that the variable is already allocated at the tail of
189 * reloptions structure (StdRdOptions or equivalent).
191 * "base" is a pointer to the reloptions structure, and "offset" is an integer
192 * variable that must be initialized to sizeof(reloptions structure). This
193 * struct must have been allocated with enough space to hold any string option
194 * present, including terminating \0 for every option. SET_VARSIZE() must be
195 * called on the struct with this offset as the second argument, after all the
196 * string options have been processed.
198 #define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset) \
200 relopt_string *optstring = (relopt_string *) option.gen;\
203 string_val = option.values.string_val; \
204 else if (!optstring->default_isnull) \
205 string_val = optstring->default_val; \
208 (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
209 if (string_val == NULL) \
213 strcpy(((char *)(base)) + (offset), string_val); \
215 (offset) += strlen(string_val) + 1; \
220 * For use during amoptions: get the strlen of a string option
221 * (either default or the user defined value)
223 #define GET_STRING_RELOPTION_LEN(option) \
224 ((option).isset ? strlen((option).values.string_val) : \
225 ((relopt_string *) (option).gen)->default_len)
228 * For use by code reading options already parsed: get a pointer to the string
229 * value itself. "optstruct" is the StdRdOption struct or equivalent, "member"
230 * is the struct member corresponding to the string option
232 #define GET_STRING_RELOPTION(optstruct, member) \
233 ((optstruct)->member == 0 ? NULL : \
234 (char *)(optstruct) + (optstruct)->member)
237 extern relopt_kind
add_reloption_kind(void);
238 extern void add_bool_reloption(bits32 kinds
, char *name
, char *desc
,
240 extern void add_int_reloption(bits32 kinds
, char *name
, char *desc
,
241 int default_val
, int min_val
, int max_val
);
242 extern void add_real_reloption(bits32 kinds
, char *name
, char *desc
,
243 double default_val
, double min_val
, double max_val
);
244 extern void add_string_reloption(bits32 kinds
, char *name
, char *desc
,
245 char *default_val
, validate_string_relopt validator
);
247 extern Datum
transformRelOptions(Datum oldOptions
, List
*defList
,
248 char *namspace
, char *validnsps
[],
249 bool ignoreOids
, bool isReset
);
250 extern List
*untransformRelOptions(Datum options
);
251 extern bytea
*extractRelOptions(HeapTuple tuple
, TupleDesc tupdesc
,
253 extern relopt_value
*parseRelOptions(Datum options
, bool validate
,
254 relopt_kind kind
, int *numrelopts
);
255 extern void *allocateReloptStruct(Size base
, relopt_value
*options
,
257 extern void fillRelOptions(void *rdopts
, Size basesize
,
258 relopt_value
*options
, int numoptions
,
260 const relopt_parse_elt
*elems
, int nelems
);
262 extern bytea
*default_reloptions(Datum reloptions
, bool validate
,
264 extern bytea
*heap_reloptions(char relkind
, Datum reloptions
, bool validate
);
265 extern bytea
*index_reloptions(RegProcedure amoptions
, Datum reloptions
,
268 #endif /* RELOPTIONS_H */