reloptions.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-2022, 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_ENUM,
  33. RELOPT_TYPE_STRING
  34. } relopt_type;
  35. /* kinds supported by reloptions */
  36. typedef enum relopt_kind
  37. {
  38. RELOPT_KIND_LOCAL = 0,
  39. RELOPT_KIND_HEAP = (1 << 0),
  40. RELOPT_KIND_TOAST = (1 << 1),
  41. RELOPT_KIND_BTREE = (1 << 2),
  42. RELOPT_KIND_HASH = (1 << 3),
  43. RELOPT_KIND_GIN = (1 << 4),
  44. RELOPT_KIND_GIST = (1 << 5),
  45. RELOPT_KIND_ATTRIBUTE = (1 << 6),
  46. RELOPT_KIND_TABLESPACE = (1 << 7),
  47. RELOPT_KIND_SPGIST = (1 << 8),
  48. RELOPT_KIND_VIEW = (1 << 9),
  49. RELOPT_KIND_BRIN = (1 << 10),
  50. RELOPT_KIND_PARTITIONED = (1 << 11),
  51. /* if you add a new kind, make sure you update "last_default" too */
  52. RELOPT_KIND_LAST_DEFAULT = RELOPT_KIND_PARTITIONED,
  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. /* reloption namespaces allowed for heaps -- currently only TOAST */
  57. #define HEAP_RELOPT_NAMESPACES { "toast", NULL }
  58. /* generic struct to hold shared data */
  59. typedef struct relopt_gen
  60. {
  61. const char *name; /* must be first (used as list termination
  62. * marker) */
  63. const char *desc;
  64. bits32 kinds;
  65. LOCKMODE lockmode;
  66. int namelen;
  67. relopt_type type;
  68. } relopt_gen;
  69. /* holds a parsed value */
  70. typedef struct relopt_value
  71. {
  72. relopt_gen *gen;
  73. bool isset;
  74. union
  75. {
  76. bool bool_val;
  77. int int_val;
  78. double real_val;
  79. int enum_val;
  80. char *string_val; /* allocated separately */
  81. } values;
  82. } relopt_value;
  83. /* reloptions records for specific variable types */
  84. typedef struct relopt_bool
  85. {
  86. relopt_gen gen;
  87. bool default_val;
  88. } relopt_bool;
  89. typedef struct relopt_int
  90. {
  91. relopt_gen gen;
  92. int default_val;
  93. int min;
  94. int max;
  95. } relopt_int;
  96. typedef struct relopt_real
  97. {
  98. relopt_gen gen;
  99. double default_val;
  100. double min;
  101. double max;
  102. } relopt_real;
  103. /*
  104. * relopt_enum_elt_def -- One member of the array of acceptable values
  105. * of an enum reloption.
  106. */
  107. typedef struct relopt_enum_elt_def
  108. {
  109. const char *string_val;
  110. int symbol_val;
  111. } relopt_enum_elt_def;
  112. typedef struct relopt_enum
  113. {
  114. relopt_gen gen;
  115. relopt_enum_elt_def *members;
  116. int default_val;
  117. const char *detailmsg;
  118. /* null-terminated array of members */
  119. } relopt_enum;
  120. /* validation routines for strings */
  121. typedef void (*validate_string_relopt) (const char *value);
  122. typedef Size (*fill_string_relopt) (const char *value, void *ptr);
  123. /* validation routine for the whole option set */
  124. typedef void (*relopts_validator) (void *parsed_options, relopt_value *vals, int nvals);
  125. typedef struct relopt_string
  126. {
  127. relopt_gen gen;
  128. int default_len;
  129. bool default_isnull;
  130. validate_string_relopt validate_cb;
  131. fill_string_relopt fill_cb;
  132. char *default_val;
  133. } relopt_string;
  134. /* This is the table datatype for build_reloptions() */
  135. typedef struct
  136. {
  137. const char *optname; /* option's name */
  138. relopt_type opttype; /* option's datatype */
  139. int offset; /* offset of field in result struct */
  140. } relopt_parse_elt;
  141. /* Local reloption definition */
  142. typedef struct local_relopt
  143. {
  144. relopt_gen *option; /* option definition */
  145. int offset; /* offset of parsed value in bytea structure */
  146. } local_relopt;
  147. /* Structure to hold local reloption data for build_local_reloptions() */
  148. typedef struct local_relopts
  149. {
  150. List *options; /* list of local_relopt definitions */
  151. List *validators; /* list of relopts_validator callbacks */
  152. Size relopt_struct_size; /* size of parsed bytea structure */
  153. } local_relopts;
  154. /*
  155. * Utility macro to get a value for a string reloption once the options
  156. * are parsed. This gets a pointer to the string value itself. "optstruct"
  157. * is the StdRdOptions struct or equivalent, "member" is the struct member
  158. * corresponding to the string option.
  159. */
  160. #define GET_STRING_RELOPTION(optstruct, member) \
  161. ((optstruct)->member == 0 ? NULL : \
  162. (char *)(optstruct) + (optstruct)->member)
  163. extern relopt_kind add_reloption_kind(void);
  164. extern void add_bool_reloption(bits32 kinds, const char *name, const char *desc,
  165. bool default_val, LOCKMODE lockmode);
  166. extern void add_int_reloption(bits32 kinds, const char *name, const char *desc,
  167. int default_val, int min_val, int max_val,
  168. LOCKMODE lockmode);
  169. extern void add_real_reloption(bits32 kinds, const char *name, const char *desc,
  170. double default_val, double min_val, double max_val,
  171. LOCKMODE lockmode);
  172. extern void add_enum_reloption(bits32 kinds, const char *name, const char *desc,
  173. relopt_enum_elt_def *members, int default_val,
  174. const char *detailmsg, LOCKMODE lockmode);
  175. extern void add_string_reloption(bits32 kinds, const char *name, const char *desc,
  176. const char *default_val, validate_string_relopt validator,
  177. LOCKMODE lockmode);
  178. extern void init_local_reloptions(local_relopts *opts, Size relopt_struct_size);
  179. extern void register_reloptions_validator(local_relopts *opts,
  180. relopts_validator validator);
  181. extern void add_local_bool_reloption(local_relopts *opts, const char *name,
  182. const char *desc, bool default_val,
  183. int offset);
  184. extern void add_local_int_reloption(local_relopts *opts, const char *name,
  185. const char *desc, int default_val,
  186. int min_val, int max_val, int offset);
  187. extern void add_local_real_reloption(local_relopts *opts, const char *name,
  188. const char *desc, double default_val,
  189. double min_val, double max_val,
  190. int offset);
  191. extern void add_local_enum_reloption(local_relopts *relopts,
  192. const char *name, const char *desc,
  193. relopt_enum_elt_def *members,
  194. int default_val, const char *detailmsg,
  195. int offset);
  196. extern void add_local_string_reloption(local_relopts *opts, const char *name,
  197. const char *desc,
  198. const char *default_val,
  199. validate_string_relopt validator,
  200. fill_string_relopt filler, int offset);
  201. extern Datum transformRelOptions(Datum oldOptions, List *defList,
  202. const char *namspace, char *validnsps[],
  203. bool acceptOidsOff, bool isReset);
  204. extern List *untransformRelOptions(Datum options);
  205. extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
  206. amoptions_function amoptions);
  207. extern void *build_reloptions(Datum reloptions, bool validate,
  208. relopt_kind kind,
  209. Size relopt_struct_size,
  210. const relopt_parse_elt *relopt_elems,
  211. int num_relopt_elems);
  212. extern void *build_local_reloptions(local_relopts *relopts, Datum options,
  213. bool validate);
  214. extern bytea *default_reloptions(Datum reloptions, bool validate,
  215. relopt_kind kind);
  216. extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
  217. extern bytea *view_reloptions(Datum reloptions, bool validate);
  218. extern bytea *partitioned_table_reloptions(Datum reloptions, bool validate);
  219. extern bytea *index_reloptions(amoptions_function amoptions, Datum reloptions,
  220. bool validate);
  221. extern bytea *attribute_reloptions(Datum reloptions, bool validate);
  222. extern bytea *tablespace_reloptions(Datum reloptions, bool validate);
  223. extern LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList);
  224. #endif /* RELOPTIONS_H */