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
37
/* XXX do we need a separate kind for TOAST tables? */
42
/* if you add a new kind, make sure you update "last_default" too */
43
RELOPT_KIND_LAST_DEFAULT = RELOPT_KIND_GIST,
47
/* reloption namespaces allowed for heaps -- currently only TOAST */
48
#define HEAP_RELOPT_NAMESPACES { "toast", NULL }
50
/* generic struct to hold shared data */
51
typedef struct relopt_gen
53
const char *name; /* must be first (used as list termination marker) */
60
/* holds a parsed value */
61
typedef struct relopt_value
70
char *string_val; /* allocated separately */
74
/* reloptions records for specific variable types */
75
typedef struct relopt_bool
81
typedef struct relopt_int
89
typedef struct relopt_real
97
/* validation routines for strings */
98
typedef void (*validate_string_relopt) (char *value);
100
typedef struct relopt_string
105
validate_string_relopt validate_cb;
106
char default_val[1]; /* variable length, zero-terminated */
109
/* This is the table datatype for fillRelOptions */
112
const char *optname; /* option's name */
113
relopt_type opttype; /* option's datatype */
114
int offset; /* offset of field in result struct */
119
* These macros exist for the convenience of amoptions writers (but consider
120
* using fillRelOptions, which is a lot simpler). Beware of multiple
121
* evaluation of arguments!
123
* The last argument in the HANDLE_*_RELOPTION macros allows the caller to
124
* determine whether the option was set (true), or its value acquired from
125
* defaults (false); it can be passed as (char *) NULL if the caller does not
126
* need this information.
128
* optname is the option name (a string), var is the variable
129
* on which the value should be stored (e.g. StdRdOptions->fillfactor), and
130
* option is a relopt_value pointer.
132
* The normal way to use this is to loop on the relopt_value array returned by
134
* for (i = 0; options[i].gen->name; i++)
136
* if (HAVE_RELOPTION("fillfactor", options[i])
138
* HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
141
* if (HAVE_RELOPTION("default_row_acl", options[i])
148
* (errmsg("unknown option")));
151
* Note that this is more or less the same that fillRelOptions does, so only
152
* use this if you need to do something non-standard within some option's
155
#define HAVE_RELOPTION(optname, option) \
156
(pg_strncasecmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
158
#define HANDLE_INT_RELOPTION(optname, var, option, wasset) \
161
var = option.values.int_val; \
163
var = ((relopt_int *) option.gen)->default_val; \
164
(wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
167
#define HANDLE_BOOL_RELOPTION(optname, var, option, wasset) \
170
var = option.values.bool_val; \
172
var = ((relopt_bool *) option.gen)->default_val; \
173
(wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
176
#define HANDLE_REAL_RELOPTION(optname, var, option, wasset) \
179
var = option.values.real_val; \
181
var = ((relopt_real *) option.gen)->default_val; \
182
(wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
186
* Note that this assumes that the variable is already allocated at the tail of
187
* reloptions structure (StdRdOptions or equivalent).
189
* "base" is a pointer to the reloptions structure, and "offset" is an integer
190
* variable that must be initialized to sizeof(reloptions structure). This
191
* struct must have been allocated with enough space to hold any string option
192
* present, including terminating \0 for every option. SET_VARSIZE() must be
193
* called on the struct with this offset as the second argument, after all the
194
* string options have been processed.
196
#define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset) \
198
relopt_string *optstring = (relopt_string *) option.gen;\
201
string_val = option.values.string_val; \
202
else if (!optstring->default_isnull) \
203
string_val = optstring->default_val; \
206
(wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
207
if (string_val == NULL) \
211
strcpy(((char *)(base)) + (offset), string_val); \
213
(offset) += strlen(string_val) + 1; \
218
* For use during amoptions: get the strlen of a string option
219
* (either default or the user defined value)
221
#define GET_STRING_RELOPTION_LEN(option) \
222
((option).isset ? strlen((option).values.string_val) : \
223
((relopt_string *) (option).gen)->default_len)
226
* For use by code reading options already parsed: get a pointer to the string
227
* value itself. "optstruct" is the StdRdOption struct or equivalent, "member"
228
* is the struct member corresponding to the string option
230
#define GET_STRING_RELOPTION(optstruct, member) \
231
((optstruct)->member == 0 ? NULL : \
232
(char *)(optstruct) + (optstruct)->member)
235
extern int add_reloption_kind(void);
236
extern void add_bool_reloption(int kind, char *name, char *desc,
238
extern void add_int_reloption(int kind, char *name, char *desc,
239
int default_val, int min_val, int max_val);
240
extern void add_real_reloption(int kind, char *name, char *desc,
241
double default_val, double min_val, double max_val);
242
extern void add_string_reloption(int kind, char *name, char *desc,
243
char *default_val, validate_string_relopt validator);
245
extern Datum transformRelOptions(Datum oldOptions, List *defList,
246
char *namspace, char *validnsps[],
247
bool ignoreOids, bool isReset);
248
extern List *untransformRelOptions(Datum options);
249
extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
251
extern relopt_value *parseRelOptions(Datum options, bool validate,
252
relopt_kind kind, int *numrelopts);
253
extern void *allocateReloptStruct(Size base, relopt_value *options,
255
extern void fillRelOptions(void *rdopts, Size basesize,
256
relopt_value *options, int numoptions,
258
const relopt_parse_elt *elems, int nelems);
260
extern bytea *default_reloptions(Datum reloptions, bool validate,
262
extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
263
extern bytea *index_reloptions(RegProcedure amoptions, Datum reloptions,
266
#endif /* RELOPTIONS_H */