gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

284 行
9.0KB

  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. #include "access/amapi.h"
  22. #include "access/htup.h"
  23. #include "access/tupdesc.h"
  24. #include "nodes/pg_list.h"
  25. #include "storage/lock.h"
  26. /* types supported by reloptions */
  27. typedef enum relopt_type
  28. {
  29. RELOPT_TYPE_BOOL,
  30. RELOPT_TYPE_INT,
  31. RELOPT_TYPE_REAL,
  32. RELOPT_TYPE_STRING
  33. } relopt_type;
  34. /* kinds supported by reloptions */
  35. typedef enum relopt_kind
  36. {
  37. RELOPT_KIND_HEAP = (1 << 0),
  38. RELOPT_KIND_TOAST = (1 << 1),
  39. RELOPT_KIND_BTREE = (1 << 2),
  40. RELOPT_KIND_HASH = (1 << 3),
  41. RELOPT_KIND_GIN = (1 << 4),
  42. RELOPT_KIND_GIST = (1 << 5),
  43. RELOPT_KIND_ATTRIBUTE = (1 << 6),
  44. RELOPT_KIND_TABLESPACE = (1 << 7),
  45. RELOPT_KIND_SPGIST = (1 << 8),
  46. RELOPT_KIND_VIEW = (1 << 9),
  47. RELOPT_KIND_BRIN = (1 << 10),
  48. RELOPT_KIND_PARTITIONED = (1 << 11),
  49. /* if you add a new kind, make sure you update "last_default" too */
  50. RELOPT_KIND_LAST_DEFAULT = RELOPT_KIND_PARTITIONED,
  51. /* some compilers treat enums as signed ints, so we can't use 1 << 31 */
  52. RELOPT_KIND_MAX = (1 << 30)
  53. } relopt_kind;
  54. /* reloption namespaces allowed for heaps -- currently only TOAST */
  55. #define HEAP_RELOPT_NAMESPACES { "toast", NULL }
  56. /* generic struct to hold shared data */
  57. typedef struct relopt_gen
  58. {
  59. const char *name; /* must be first (used as list termination
  60. * marker) */
  61. const char *desc;
  62. bits32 kinds;
  63. LOCKMODE lockmode;
  64. int namelen;
  65. relopt_type type;
  66. } relopt_gen;
  67. /* holds a parsed value */
  68. typedef struct relopt_value
  69. {
  70. relopt_gen *gen;
  71. bool isset;
  72. union
  73. {
  74. bool bool_val;
  75. int int_val;
  76. double real_val;
  77. char *string_val; /* allocated separately */
  78. } values;
  79. } relopt_value;
  80. /* reloptions records for specific variable types */
  81. typedef struct relopt_bool
  82. {
  83. relopt_gen gen;
  84. bool default_val;
  85. } relopt_bool;
  86. typedef struct relopt_int
  87. {
  88. relopt_gen gen;
  89. int default_val;
  90. int min;
  91. int max;
  92. } relopt_int;
  93. typedef struct relopt_real
  94. {
  95. relopt_gen gen;
  96. double default_val;
  97. double min;
  98. double max;
  99. } relopt_real;
  100. /* validation routines for strings */
  101. typedef void (*validate_string_relopt) (const char *value);
  102. typedef struct relopt_string
  103. {
  104. relopt_gen gen;
  105. int default_len;
  106. bool default_isnull;
  107. validate_string_relopt validate_cb;
  108. char *default_val;
  109. } relopt_string;
  110. /* This is the table datatype for fillRelOptions */
  111. typedef struct
  112. {
  113. const char *optname; /* option's name */
  114. relopt_type opttype; /* option's datatype */
  115. int offset; /* offset of field in result struct */
  116. } relopt_parse_elt;
  117. /*
  118. * These macros exist for the convenience of amoptions writers (but consider
  119. * using fillRelOptions, which is a lot simpler). Beware of multiple
  120. * evaluation of arguments!
  121. *
  122. * The last argument in the HANDLE_*_RELOPTION macros allows the caller to
  123. * determine whether the option was set (true), or its value acquired from
  124. * defaults (false); it can be passed as (char *) NULL if the caller does not
  125. * need this information.
  126. *
  127. * optname is the option name (a string), var is the variable
  128. * on which the value should be stored (e.g. StdRdOptions->fillfactor), and
  129. * option is a relopt_value pointer.
  130. *
  131. * The normal way to use this is to loop on the relopt_value array returned by
  132. * parseRelOptions:
  133. * for (i = 0; options[i].gen->name; i++)
  134. * {
  135. * if (HAVE_RELOPTION("fillfactor", options[i])
  136. * {
  137. * HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
  138. * continue;
  139. * }
  140. * if (HAVE_RELOPTION("default_row_acl", options[i])
  141. * {
  142. * ...
  143. * }
  144. * ...
  145. * if (validate)
  146. * ereport(ERROR,
  147. * (errmsg("unknown option")));
  148. * }
  149. *
  150. * Note that this is more or less the same that fillRelOptions does, so only
  151. * use this if you need to do something non-standard within some option's
  152. * code block.
  153. */
  154. #define HAVE_RELOPTION(optname, option) \
  155. (strncmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
  156. #define HANDLE_INT_RELOPTION(optname, var, option, wasset) \
  157. do { \
  158. if (option.isset) \
  159. var = option.values.int_val; \
  160. else \
  161. var = ((relopt_int *) option.gen)->default_val; \
  162. (wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
  163. } while (0)
  164. #define HANDLE_BOOL_RELOPTION(optname, var, option, wasset) \
  165. do { \
  166. if (option.isset) \
  167. var = option.values.bool_val; \
  168. else \
  169. var = ((relopt_bool *) option.gen)->default_val; \
  170. (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
  171. } while (0)
  172. #define HANDLE_REAL_RELOPTION(optname, var, option, wasset) \
  173. do { \
  174. if (option.isset) \
  175. var = option.values.real_val; \
  176. else \
  177. var = ((relopt_real *) option.gen)->default_val; \
  178. (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
  179. } while (0)
  180. /*
  181. * Note that this assumes that the variable is already allocated at the tail of
  182. * reloptions structure (StdRdOptions or equivalent).
  183. *
  184. * "base" is a pointer to the reloptions structure, and "offset" is an integer
  185. * variable that must be initialized to sizeof(reloptions structure). This
  186. * struct must have been allocated with enough space to hold any string option
  187. * present, including terminating \0 for every option. SET_VARSIZE() must be
  188. * called on the struct with this offset as the second argument, after all the
  189. * string options have been processed.
  190. */
  191. #define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset) \
  192. do { \
  193. relopt_string *optstring = (relopt_string *) option.gen;\
  194. char *string_val; \
  195. if (option.isset) \
  196. string_val = option.values.string_val; \
  197. else if (!optstring->default_isnull) \
  198. string_val = optstring->default_val; \
  199. else \
  200. string_val = NULL; \
  201. (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
  202. if (string_val == NULL) \
  203. var = 0; \
  204. else \
  205. { \
  206. strcpy(((char *)(base)) + (offset), string_val); \
  207. var = (offset); \
  208. (offset) += strlen(string_val) + 1; \
  209. } \
  210. } while (0)
  211. /*
  212. * For use during amoptions: get the strlen of a string option
  213. * (either default or the user defined value)
  214. */
  215. #define GET_STRING_RELOPTION_LEN(option) \
  216. ((option).isset ? strlen((option).values.string_val) : \
  217. ((relopt_string *) (option).gen)->default_len)
  218. /*
  219. * For use by code reading options already parsed: get a pointer to the string
  220. * value itself. "optstruct" is the StdRdOption struct or equivalent, "member"
  221. * is the struct member corresponding to the string option
  222. */
  223. #define GET_STRING_RELOPTION(optstruct, member) \
  224. ((optstruct)->member == 0 ? NULL : \
  225. (char *)(optstruct) + (optstruct)->member)
  226. extern relopt_kind add_reloption_kind(void);
  227. extern void add_bool_reloption(bits32 kinds, const char *name, const char *desc,
  228. bool default_val);
  229. extern void add_int_reloption(bits32 kinds, const char *name, const char *desc,
  230. int default_val, int min_val, int max_val);
  231. extern void add_real_reloption(bits32 kinds, const char *name, const char *desc,
  232. double default_val, double min_val, double max_val);
  233. extern void add_string_reloption(bits32 kinds, const char *name, const char *desc,
  234. const char *default_val, validate_string_relopt validator);
  235. extern Datum transformRelOptions(Datum oldOptions, List *defList,
  236. const char *namspace, char *validnsps[],
  237. bool acceptOidsOff, bool isReset);
  238. extern List *untransformRelOptions(Datum options);
  239. extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
  240. amoptions_function amoptions);
  241. extern relopt_value *parseRelOptions(Datum options, bool validate,
  242. relopt_kind kind, int *numrelopts);
  243. extern void *allocateReloptStruct(Size base, relopt_value *options,
  244. int numoptions);
  245. extern void fillRelOptions(void *rdopts, Size basesize,
  246. relopt_value *options, int numoptions,
  247. bool validate,
  248. const relopt_parse_elt *elems, int nelems);
  249. extern bytea *default_reloptions(Datum reloptions, bool validate,
  250. relopt_kind kind);
  251. extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
  252. extern bytea *view_reloptions(Datum reloptions, bool validate);
  253. extern bytea *index_reloptions(amoptions_function amoptions, Datum reloptions,
  254. bool validate);
  255. extern bytea *attribute_reloptions(Datum reloptions, bool validate);
  256. extern bytea *tablespace_reloptions(Datum reloptions, bool validate);
  257. extern LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList);
  258. #endif /* RELOPTIONS_H */
上海开阖软件有限公司 沪ICP备12045867号-1