PostgreSQL Source Code git master
Loading...
Searching...
No Matches
reloptions.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * reloptions.c
4 * Core support for relation options (pg_class.reloptions)
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/access/common/reloptions.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include <float.h>
19
20#include "access/gist_private.h"
21#include "access/hash.h"
22#include "access/heaptoast.h"
23#include "access/htup_details.h"
24#include "access/nbtree.h"
25#include "access/reloptions.h"
27#include "catalog/pg_type.h"
28#include "commands/defrem.h"
29#include "commands/tablespace.h"
30#include "nodes/makefuncs.h"
31#include "storage/lock.h"
32#include "utils/array.h"
33#include "utils/attoptcache.h"
34#include "utils/builtins.h"
35#include "utils/guc.h"
36#include "utils/memutils.h"
37#include "utils/rel.h"
38
39/*
40 * Contents of pg_class.reloptions
41 *
42 * To add an option:
43 *
44 * (i) decide on a type (bool, ternary, integer, real, enum, string), name,
45 * default value, upper and lower bounds (if applicable); for strings,
46 * consider a validation routine.
47 * (ii) add a record below (or use add_<type>_reloption).
48 * (iii) add it to the appropriate options struct (perhaps StdRdOptions)
49 * (iv) add it to the appropriate handling routine (perhaps
50 * default_reloptions)
51 * (v) make sure the lock level is set correctly for that operation
52 * (vi) don't forget to document the option
53 *
54 * From the user's point of view, a 'ternary' is exactly like a Boolean,
55 * so we don't document it separately. On the implementation side, the
56 * handling code can detect the case where the option has not been set.
57 *
58 * The default choice for any new option should be AccessExclusiveLock.
59 * In some cases the lock level can be reduced from there, but the lock
60 * level chosen should always conflict with itself to ensure that multiple
61 * changes aren't lost when we attempt concurrent changes.
62 * The choice of lock level depends completely upon how that parameter
63 * is used within the server, not upon how and when you'd like to change it.
64 * Safety first. Existing choices are documented here, and elsewhere in
65 * backend code where the parameters are used.
66 *
67 * In general, anything that affects the results obtained from a SELECT must be
68 * protected by AccessExclusiveLock.
69 *
70 * Autovacuum related parameters can be set at ShareUpdateExclusiveLock
71 * since they are only used by the AV procs and don't change anything
72 * currently executing.
73 *
74 * Fillfactor can be set at ShareUpdateExclusiveLock because it applies only to
75 * subsequent changes made to data blocks, as documented in hio.c
76 *
77 * n_distinct options can be set at ShareUpdateExclusiveLock because they
78 * are only used during ANALYZE, which uses a ShareUpdateExclusiveLock,
79 * so the ANALYZE will not be affected by in-flight changes. Changing those
80 * values has no effect until the next ANALYZE, so no need for stronger lock.
81 *
82 * Planner-related parameters can be set at ShareUpdateExclusiveLock because
83 * they only affect planning and not the correctness of the execution. Plans
84 * cannot be changed in mid-flight, so changes here could not easily result in
85 * new improved plans in any case. So we allow existing queries to continue
86 * and existing plans to survive, a small price to pay for allowing better
87 * plans to be introduced concurrently without interfering with users.
88 *
89 * Setting parallel_workers at ShareUpdateExclusiveLock is safe, since it acts
90 * the same as max_parallel_workers_per_gather which is a USERSET parameter
91 * that doesn't affect existing plans or queries.
92 *
93 * vacuum_truncate can be set at ShareUpdateExclusiveLock because it
94 * is only used during VACUUM, which uses a ShareUpdateExclusiveLock,
95 * so the VACUUM will not be affected by in-flight changes. Changing its
96 * value has no effect until the next VACUUM, so no need for stronger lock.
97 */
98
100{
101 {
102 {
103 "autosummarize",
104 "Enables automatic summarization on this BRIN index",
107 },
108 false
109 },
110 {
111 {
112 "autovacuum_enabled",
113 "Enables autovacuum in this relation",
116 },
117 true
118 },
119 {
120 {
121 "user_catalog_table",
122 "Declare a table as an additional catalog table, e.g. for the purpose of logical replication",
125 },
126 false
127 },
128 {
129 {
130 "fastupdate",
131 "Enables \"fast update\" feature for this GIN index",
134 },
135 true
136 },
137 {
138 {
139 "security_barrier",
140 "View acts as a row security barrier",
143 },
144 false
145 },
146 {
147 {
148 "security_invoker",
149 "Privileges on underlying relations are checked as the invoking user, not the view owner",
152 },
153 false
154 },
155 {
156 {
157 "deduplicate_items",
158 "Enables \"deduplicate items\" feature for this btree index",
160 ShareUpdateExclusiveLock /* since it applies only to later
161 * inserts */
162 },
163 true
164 },
165 /* list terminator */
166 {{NULL}}
167};
168
170{
171 {
172 {
173 "vacuum_truncate",
174 "Enables vacuum to truncate empty pages at the end of this table",
177 }
178 },
179 /* list terminator */
180 {
181 {
182 NULL
183 }
184 }
185};
186
188{
189 {
190 {
191 "fillfactor",
192 "Packs table pages only to this percentage",
194 ShareUpdateExclusiveLock /* since it applies only to later
195 * inserts */
196 },
198 },
199 {
200 {
201 "fillfactor",
202 "Packs btree index pages only to this percentage",
204 ShareUpdateExclusiveLock /* since it applies only to later
205 * inserts */
206 },
208 },
209 {
210 {
211 "fillfactor",
212 "Packs hash index pages only to this percentage",
214 ShareUpdateExclusiveLock /* since it applies only to later
215 * inserts */
216 },
218 },
219 {
220 {
221 "fillfactor",
222 "Packs gist index pages only to this percentage",
224 ShareUpdateExclusiveLock /* since it applies only to later
225 * inserts */
226 },
228 },
229 {
230 {
231 "fillfactor",
232 "Packs spgist index pages only to this percentage",
234 ShareUpdateExclusiveLock /* since it applies only to later
235 * inserts */
236 },
238 },
239 {
240 {
241 "autovacuum_parallel_workers",
242 "Maximum number of parallel autovacuum workers that can be used for processing this table.",
245 },
246 -1, -1, 1024
247 },
248 {
249 {
250 "autovacuum_vacuum_threshold",
251 "Minimum number of tuple updates or deletes prior to vacuum",
254 },
255 -1, 0, INT_MAX
256 },
257 {
258 {
259 "autovacuum_vacuum_max_threshold",
260 "Maximum number of tuple updates or deletes prior to vacuum",
263 },
264 -2, -1, INT_MAX
265 },
266 {
267 {
268 "autovacuum_vacuum_insert_threshold",
269 "Minimum number of tuple inserts prior to vacuum, or -1 to disable insert vacuums",
272 },
273 -2, -1, INT_MAX
274 },
275 {
276 {
277 "autovacuum_analyze_threshold",
278 "Minimum number of tuple inserts, updates or deletes prior to analyze",
281 },
282 -1, 0, INT_MAX
283 },
284 {
285 {
286 "autovacuum_vacuum_cost_limit",
287 "Vacuum cost amount available before napping, for autovacuum",
290 },
291 -1, 1, 10000
292 },
293 {
294 {
295 "autovacuum_freeze_min_age",
296 "Minimum age at which VACUUM should freeze a table row, for autovacuum",
299 },
300 -1, 0, 1000000000
301 },
302 {
303 {
304 "autovacuum_multixact_freeze_min_age",
305 "Minimum multixact age at which VACUUM should freeze a row multixact's, for autovacuum",
308 },
309 -1, 0, 1000000000
310 },
311 {
312 {
313 "autovacuum_freeze_max_age",
314 "Age at which to autovacuum a table to prevent transaction ID wraparound",
317 },
318 -1, 100000, 2000000000
319 },
320 {
321 {
322 "autovacuum_multixact_freeze_max_age",
323 "Multixact age at which to autovacuum a table to prevent multixact wraparound",
326 },
327 -1, 10000, 2000000000
328 },
329 {
330 {
331 "autovacuum_freeze_table_age",
332 "Age at which VACUUM should perform a full table sweep to freeze row versions",
335 }, -1, 0, 2000000000
336 },
337 {
338 {
339 "autovacuum_multixact_freeze_table_age",
340 "Age of multixact at which VACUUM should perform a full table sweep to freeze row versions",
343 }, -1, 0, 2000000000
344 },
345 {
346 {
347 "log_autovacuum_min_duration",
348 "Sets the minimum execution time above which vacuum actions by autovacuum will be logged",
351 },
352 -1, -1, INT_MAX
353 },
354 {
355 {
356 "log_autoanalyze_min_duration",
357 "Sets the minimum execution time above which analyze actions by autovacuum will be logged",
360 },
361 -1, -1, INT_MAX
362 },
363 {
364 {
365 "toast_tuple_target",
366 "Sets the target tuple length at which external columns will be toasted",
369 },
371 },
372 {
373 {
374 "pages_per_range",
375 "Number of pages that each page range covers in a BRIN index",
378 }, 128, 1, 131072
379 },
380 {
381 {
382 "gin_pending_list_limit",
383 "Maximum size of the pending list for this GIN index, in kilobytes.",
386 },
387 -1, 64, MAX_KILOBYTES
388 },
389 {
390 {
391 "effective_io_concurrency",
392 "Number of simultaneous requests that can be handled efficiently by the disk subsystem.",
395 },
396 -1, 0, MAX_IO_CONCURRENCY
397 },
398 {
399 {
400 "maintenance_io_concurrency",
401 "Number of simultaneous requests that can be handled efficiently by the disk subsystem for maintenance work.",
404 },
405 -1, 0, MAX_IO_CONCURRENCY
406 },
407 {
408 {
409 "parallel_workers",
410 "Number of parallel processes that can be used per executor node for this relation.",
413 },
414 -1, 0, 1024
415 },
416
417 /* list terminator */
418 {{NULL}}
419};
420
422{
423 {
424 {
425 "autovacuum_vacuum_cost_delay",
426 "Vacuum cost delay in milliseconds, for autovacuum",
429 },
430 -1, 0.0, 100.0
431 },
432 {
433 {
434 "autovacuum_vacuum_scale_factor",
435 "Number of tuple updates or deletes prior to vacuum as a fraction of reltuples",
438 },
439 -1, 0.0, 100.0
440 },
441 {
442 {
443 "autovacuum_vacuum_insert_scale_factor",
444 "Number of tuple inserts prior to vacuum as a fraction of reltuples",
447 },
448 -1, 0.0, 100.0
449 },
450 {
451 {
452 "autovacuum_analyze_scale_factor",
453 "Number of tuple inserts, updates or deletes prior to analyze as a fraction of reltuples",
456 },
457 -1, 0.0, 100.0
458 },
459 {
460 {
461 "vacuum_max_eager_freeze_failure_rate",
462 "Fraction of pages in a relation vacuum can scan and fail to freeze before disabling eager scanning.",
465 },
466 -1, 0.0, 1.0
467 },
468
469 {
470 {
471 "seq_page_cost",
472 "Sets the planner's estimate of the cost of a sequentially fetched disk page.",
475 },
476 -1, 0.0, DBL_MAX
477 },
478 {
479 {
480 "random_page_cost",
481 "Sets the planner's estimate of the cost of a nonsequentially fetched disk page.",
484 },
485 -1, 0.0, DBL_MAX
486 },
487 {
488 {
489 "n_distinct",
490 "Sets the planner's estimate of the number of distinct values appearing in a column (excluding child relations).",
493 },
494 0, -1.0, DBL_MAX
495 },
496 {
497 {
498 "n_distinct_inherited",
499 "Sets the planner's estimate of the number of distinct values appearing in a column (including child relations).",
502 },
503 0, -1.0, DBL_MAX
504 },
505 {
506 {
507 "vacuum_cleanup_index_scale_factor",
508 "Deprecated B-Tree parameter.",
511 },
512 -1, 0.0, 1e10
513 },
514 /* list terminator */
515 {{NULL}}
516};
517
518/* values from StdRdOptIndexCleanup */
532
533/* values from GistOptBufferingMode */
535{
539 {(const char *) NULL} /* list terminator */
540};
541
542/* values from ViewOptCheckOption */
544{
545 /* no value for NOT_SET */
548 {(const char *) NULL} /* list terminator */
549};
550
552{
553 {
554 {
555 "vacuum_index_cleanup",
556 "Controls index vacuuming and index cleanup",
559 },
562 gettext_noop("Valid values are \"on\", \"off\", and \"auto\".")
563 },
564 {
565 {
566 "buffering",
567 "Enables buffering build for this GiST index",
570 },
573 gettext_noop("Valid values are \"on\", \"off\", and \"auto\".")
574 },
575 {
576 {
577 "check_option",
578 "View has WITH CHECK OPTION defined (local or cascaded).",
581 },
584 gettext_noop("Valid values are \"local\" and \"cascaded\".")
585 },
586 /* list terminator */
587 {{NULL}}
588};
589
591{
592 /* list terminator */
593 {{NULL}}
594};
595
598
599static int num_custom_options = 0;
601static bool need_initialization = true;
602
603static void initialize_reloptions(void);
605 int text_len, bool validate);
606
607/*
608 * Get the length of a string reloption (either default or the user-defined
609 * value). This is used for allocation purposes when building a set of
610 * relation options.
611 */
612#define GET_STRING_RELOPTION_LEN(option) \
613 ((option).isset ? strlen((option).string_val) : \
614 ((relopt_string *) (option).gen)->default_len)
615
616/*
617 * initialize_reloptions
618 * initialization routine, must be called before parsing
619 *
620 * Initialize the relOpts array and fill each variable's type and name length.
621 */
622static void
624{
625 int i;
626 int j;
627
628 j = 0;
629 for (i = 0; boolRelOpts[i].gen.name; i++)
630 {
633 j++;
634 }
635 for (i = 0; ternaryRelOpts[i].gen.name; i++)
636 {
639 j++;
640 }
641
642 for (i = 0; intRelOpts[i].gen.name; i++)
643 {
646 j++;
647 }
648 for (i = 0; realRelOpts[i].gen.name; i++)
649 {
652 j++;
653 }
654 for (i = 0; enumRelOpts[i].gen.name; i++)
655 {
658 j++;
659 }
660 for (i = 0; stringRelOpts[i].gen.name; i++)
661 {
664 j++;
665 }
667
668 if (relOpts)
669 pfree(relOpts);
671 (j + 1) * sizeof(relopt_gen *));
672
673 j = 0;
674 for (i = 0; boolRelOpts[i].gen.name; i++)
675 {
679 j++;
680 }
681
682 for (i = 0; ternaryRelOpts[i].gen.name; i++)
683 {
687 j++;
688 }
689
690 for (i = 0; intRelOpts[i].gen.name; i++)
691 {
692 relOpts[j] = &intRelOpts[i].gen;
695 j++;
696 }
697
698 for (i = 0; realRelOpts[i].gen.name; i++)
699 {
703 j++;
704 }
705
706 for (i = 0; enumRelOpts[i].gen.name; i++)
707 {
711 j++;
712 }
713
714 for (i = 0; stringRelOpts[i].gen.name; i++)
715 {
719 j++;
720 }
721
722 for (i = 0; i < num_custom_options; i++)
723 {
725 j++;
726 }
727
728 /* add a list terminator */
729 relOpts[j] = NULL;
730
731 /* flag the work is complete */
732 need_initialization = false;
733}
734
735/*
736 * add_reloption_kind
737 * Create a new relopt_kind value, to be used in custom reloptions by
738 * user-defined AMs.
739 */
742{
743 /* don't hand out the last bit so that the enum's behavior is portable */
747 errmsg("user-defined relation parameter types limit exceeded")));
748 last_assigned_kind <<= 1;
750}
751
752/*
753 * add_reloption
754 * Add an already-created custom reloption to the list, and recompute the
755 * main parser table.
756 */
757static void
785
786/*
787 * init_local_reloptions
788 * Initialize local reloptions that will parsed into bytea structure of
789 * 'relopt_struct_size'.
790 */
791void
793{
794 relopts->options = NIL;
795 relopts->validators = NIL;
796 relopts->relopt_struct_size = relopt_struct_size;
797}
798
799/*
800 * register_reloptions_validator
801 * Register custom validation callback that will be called at the end of
802 * build_local_reloptions().
803 */
804void
809
810/*
811 * add_local_reloption
812 * Add an already-created custom reloption to the local list.
813 */
814static void
816{
818
819 Assert(offset < relopts->relopt_struct_size);
820
821 opt->option = newoption;
822 opt->offset = offset;
823
824 relopts->options = lappend(relopts->options, opt);
825}
826
827/*
828 * allocate_reloption
829 * Allocate a new reloption and initialize the type-agnostic fields
830 * (for types other than string)
831 */
832static relopt_gen *
833allocate_reloption(uint32 kinds, int type, const char *name, const char *desc,
834 LOCKMODE lockmode)
835{
837 size_t size;
839
840 if (kinds != RELOPT_KIND_LOCAL)
842 else
843 oldcxt = NULL;
844
845 switch (type)
846 {
847 case RELOPT_TYPE_BOOL:
848 size = sizeof(relopt_bool);
849 break;
851 size = sizeof(relopt_ternary);
852 break;
853 case RELOPT_TYPE_INT:
854 size = sizeof(relopt_int);
855 break;
856 case RELOPT_TYPE_REAL:
857 size = sizeof(relopt_real);
858 break;
859 case RELOPT_TYPE_ENUM:
860 size = sizeof(relopt_enum);
861 break;
863 size = sizeof(relopt_string);
864 break;
865 default:
866 elog(ERROR, "unsupported reloption type %d", type);
867 return NULL; /* keep compiler quiet */
868 }
869
870 newoption = palloc(size);
871
872 newoption->name = pstrdup(name);
873 if (desc)
874 newoption->desc = pstrdup(desc);
875 else
876 newoption->desc = NULL;
877 newoption->kinds = kinds;
878 newoption->namelen = strlen(name);
879 newoption->type = type;
880 newoption->lockmode = lockmode;
881
882 if (oldcxt != NULL)
884
885 return newoption;
886}
887
888/*
889 * init_bool_reloption
890 * Allocate and initialize a new boolean reloption
891 */
892static relopt_bool *
893init_bool_reloption(uint32 kinds, const char *name, const char *desc,
894 bool default_val, LOCKMODE lockmode)
895{
897
899 name, desc, lockmode);
900 newoption->default_val = default_val;
901
902 return newoption;
903}
904
905/*
906 * add_bool_reloption
907 * Add a new boolean reloption
908 */
909void
910add_bool_reloption(uint32 kinds, const char *name, const char *desc,
911 bool default_val, LOCKMODE lockmode)
912{
914 default_val, lockmode);
915
917}
918
919/*
920 * add_local_bool_reloption
921 * Add a new boolean local reloption
922 *
923 * 'offset' is offset of bool-typed field.
924 */
925void
927 const char *desc, bool default_val, int offset)
928{
930 name, desc,
931 default_val, 0);
932
934}
935
936/*
937 * init_ternary_reloption
938 * Allocate and initialize a new ternary reloption
939 */
940static relopt_ternary *
941init_ternary_reloption(uint32 kinds, const char *name, const char *desc,
942 LOCKMODE lockmode)
943{
945
947 allocate_reloption(kinds, RELOPT_TYPE_TERNARY, name, desc, lockmode);
948
949 return newoption;
950}
951
952/*
953 * add_ternary_reloption
954 * Add a new ternary reloption
955 */
956void
957add_ternary_reloption(uint32 kinds, const char *name, const char *desc,
958 LOCKMODE lockmode)
959{
961
962 newoption =
963 init_ternary_reloption(kinds, name, desc, lockmode);
964
966}
967
968/*
969 * add_local_ternary_reloption
970 * Add a new ternary local reloption
971 *
972 * 'offset' is offset of ternary-typed field.
973 */
974void
976 const char *desc, int offset)
977{
979
980 newoption =
982
984}
985
986/*
987 * init_real_reloption
988 * Allocate and initialize a new integer reloption
989 */
990static relopt_int *
991init_int_reloption(uint32 kinds, const char *name, const char *desc,
992 int default_val, int min_val, int max_val,
993 LOCKMODE lockmode)
994{
996
998 name, desc, lockmode);
999 newoption->default_val = default_val;
1000 newoption->min = min_val;
1001 newoption->max = max_val;
1002
1003 return newoption;
1004}
1005
1006/*
1007 * add_int_reloption
1008 * Add a new integer reloption
1009 */
1010void
1011add_int_reloption(uint32 kinds, const char *name, const char *desc, int default_val,
1012 int min_val, int max_val, LOCKMODE lockmode)
1013{
1015 default_val, min_val,
1016 max_val, lockmode);
1017
1019}
1020
1021/*
1022 * add_local_int_reloption
1023 * Add a new local integer reloption
1024 *
1025 * 'offset' is offset of int-typed field.
1026 */
1027void
1029 const char *desc, int default_val, int min_val,
1030 int max_val, int offset)
1031{
1033 name, desc, default_val,
1034 min_val, max_val, 0);
1035
1037}
1038
1039/*
1040 * init_real_reloption
1041 * Allocate and initialize a new real reloption
1042 */
1043static relopt_real *
1044init_real_reloption(uint32 kinds, const char *name, const char *desc,
1045 double default_val, double min_val, double max_val,
1046 LOCKMODE lockmode)
1047{
1049
1051 name, desc, lockmode);
1052 newoption->default_val = default_val;
1053 newoption->min = min_val;
1054 newoption->max = max_val;
1055
1056 return newoption;
1057}
1058
1059/*
1060 * add_real_reloption
1061 * Add a new float reloption
1062 */
1063void
1064add_real_reloption(uint32 kinds, const char *name, const char *desc,
1065 double default_val, double min_val, double max_val,
1066 LOCKMODE lockmode)
1067{
1069 default_val, min_val,
1070 max_val, lockmode);
1071
1073}
1074
1075/*
1076 * add_local_real_reloption
1077 * Add a new local float reloption
1078 *
1079 * 'offset' is offset of double-typed field.
1080 */
1081void
1083 const char *desc, double default_val,
1084 double min_val, double max_val, int offset)
1085{
1087 name, desc,
1088 default_val, min_val,
1089 max_val, 0);
1090
1092}
1093
1094/*
1095 * init_enum_reloption
1096 * Allocate and initialize a new enum reloption
1097 */
1098static relopt_enum *
1099init_enum_reloption(uint32 kinds, const char *name, const char *desc,
1100 relopt_enum_elt_def *members, int default_val,
1101 const char *detailmsg, LOCKMODE lockmode)
1102{
1104
1106 name, desc, lockmode);
1107 newoption->members = members;
1108 newoption->default_val = default_val;
1109 newoption->detailmsg = detailmsg;
1110
1111 return newoption;
1112}
1113
1114
1115/*
1116 * add_enum_reloption
1117 * Add a new enum reloption
1118 *
1119 * The members array must have a terminating NULL entry.
1120 *
1121 * The detailmsg is shown when unsupported values are passed, and has this
1122 * form: "Valid values are \"foo\", \"bar\", and \"bar\"."
1123 *
1124 * The members array and detailmsg are not copied -- caller must ensure that
1125 * they are valid throughout the life of the process.
1126 */
1127void
1128add_enum_reloption(uint32 kinds, const char *name, const char *desc,
1129 relopt_enum_elt_def *members, int default_val,
1130 const char *detailmsg, LOCKMODE lockmode)
1131{
1133 members, default_val,
1134 detailmsg, lockmode);
1135
1137}
1138
1139/*
1140 * add_local_enum_reloption
1141 * Add a new local enum reloption
1142 *
1143 * 'offset' is offset of int-typed field.
1144 */
1145void
1147 const char *desc, relopt_enum_elt_def *members,
1148 int default_val, const char *detailmsg, int offset)
1149{
1151 name, desc,
1152 members, default_val,
1153 detailmsg, 0);
1154
1156}
1157
1158/*
1159 * init_string_reloption
1160 * Allocate and initialize a new string reloption
1161 */
1162static relopt_string *
1163init_string_reloption(uint32 kinds, const char *name, const char *desc,
1164 const char *default_val,
1166 fill_string_relopt filler,
1167 LOCKMODE lockmode)
1168{
1170
1171 /* make sure the validator/default combination is sane */
1172 if (validator)
1173 (validator) (default_val);
1174
1176 name, desc, lockmode);
1177 newoption->validate_cb = validator;
1178 newoption->fill_cb = filler;
1179 if (default_val)
1180 {
1181 if (kinds == RELOPT_KIND_LOCAL)
1182 newoption->default_val = strdup(default_val);
1183 else
1184 newoption->default_val = MemoryContextStrdup(TopMemoryContext, default_val);
1185 newoption->default_len = strlen(default_val);
1186 newoption->default_isnull = false;
1187 }
1188 else
1189 {
1190 newoption->default_val = "";
1191 newoption->default_len = 0;
1192 newoption->default_isnull = true;
1193 }
1194
1195 return newoption;
1196}
1197
1198/*
1199 * add_string_reloption
1200 * Add a new string reloption
1201 *
1202 * "validator" is an optional function pointer that can be used to test the
1203 * validity of the values. It must elog(ERROR) when the argument string is
1204 * not acceptable for the variable. Note that the default value must pass
1205 * the validation.
1206 */
1207void
1208add_string_reloption(uint32 kinds, const char *name, const char *desc,
1209 const char *default_val, validate_string_relopt validator,
1210 LOCKMODE lockmode)
1211{
1213 default_val,
1214 validator, NULL,
1215 lockmode);
1216
1218}
1219
1220/*
1221 * add_local_string_reloption
1222 * Add a new local string reloption
1223 *
1224 * 'offset' is offset of int-typed field that will store offset of string value
1225 * in the resulting bytea structure.
1226 */
1227void
1229 const char *desc, const char *default_val,
1231 fill_string_relopt filler, int offset)
1232{
1234 name, desc,
1235 default_val,
1236 validator, filler,
1237 0);
1238
1240}
1241
1242/*
1243 * Transform a relation options list (list of DefElem) into the text array
1244 * format that is kept in pg_class.reloptions, including only those options
1245 * that are in the passed namespace. The output values do not include the
1246 * namespace.
1247 *
1248 * This is used for three cases: CREATE TABLE/INDEX, ALTER TABLE SET, and
1249 * ALTER TABLE RESET. In the ALTER cases, oldOptions is the existing
1250 * reloptions value (possibly NULL), and we replace or remove entries
1251 * as needed.
1252 *
1253 * If acceptOidsOff is true, then we allow oids = false, but throw error when
1254 * on. This is solely needed for backwards compatibility.
1255 *
1256 * Note that this is not responsible for determining whether the options
1257 * are valid, but it does check that namespaces for all the options given are
1258 * listed in validnsps. The NULL namespace is always valid and need not be
1259 * explicitly listed. Passing a NULL pointer means that only the NULL
1260 * namespace is valid.
1261 *
1262 * Both oldOptions and the result are text arrays (or NULL for "default"),
1263 * but we declare them as Datums to avoid including array.h in reloptions.h.
1264 */
1265Datum
1267 const char *const validnsps[], bool acceptOidsOff, bool isReset)
1268{
1269 Datum result;
1270 ArrayBuildState *astate;
1271 ListCell *cell;
1272
1273 /* no change if empty list */
1274 if (defList == NIL)
1275 return oldOptions;
1276
1277 /* We build new array using accumArrayResult */
1278 astate = NULL;
1279
1280 /* Copy any oldOptions that aren't to be replaced */
1282 {
1285 int noldoptions;
1286 int i;
1287
1289
1290 for (i = 0; i < noldoptions; i++)
1291 {
1294
1295 /* Search for a match in defList */
1296 foreach(cell, defList)
1297 {
1298 DefElem *def = (DefElem *) lfirst(cell);
1299 int kw_len;
1300
1301 /* ignore if not in the same namespace */
1302 if (nameSpace == NULL)
1303 {
1304 if (def->defnamespace != NULL)
1305 continue;
1306 }
1307 else if (def->defnamespace == NULL)
1308 continue;
1309 else if (strcmp(def->defnamespace, nameSpace) != 0)
1310 continue;
1311
1312 kw_len = strlen(def->defname);
1313 if (text_len > kw_len && text_str[kw_len] == '=' &&
1314 strncmp(text_str, def->defname, kw_len) == 0)
1315 break;
1316 }
1317 if (!cell)
1318 {
1319 /* No match, so keep old option */
1320 astate = accumArrayResult(astate, oldoptions[i],
1321 false, TEXTOID,
1323 }
1324 }
1325 }
1326
1327 /*
1328 * If CREATE/SET, add new options to array; if RESET, just check that the
1329 * user didn't say RESET (option=val). (Must do this because the grammar
1330 * doesn't enforce it.)
1331 */
1332 foreach(cell, defList)
1333 {
1334 DefElem *def = (DefElem *) lfirst(cell);
1335
1336 if (isReset)
1337 {
1338 if (def->arg != NULL)
1339 ereport(ERROR,
1341 errmsg("RESET must not include values for parameters")));
1342 }
1343 else
1344 {
1345 const char *name;
1346 const char *value;
1347 text *t;
1348 Size len;
1349
1350 /*
1351 * Error out if the namespace is not valid. A NULL namespace is
1352 * always valid.
1353 */
1354 if (def->defnamespace != NULL)
1355 {
1356 bool valid = false;
1357 int i;
1358
1359 if (validnsps)
1360 {
1361 for (i = 0; validnsps[i]; i++)
1362 {
1363 if (strcmp(def->defnamespace, validnsps[i]) == 0)
1364 {
1365 valid = true;
1366 break;
1367 }
1368 }
1369 }
1370
1371 if (!valid)
1372 ereport(ERROR,
1374 errmsg("unrecognized parameter namespace \"%s\"",
1375 def->defnamespace)));
1376 }
1377
1378 /* ignore if not in the same namespace */
1379 if (nameSpace == NULL)
1380 {
1381 if (def->defnamespace != NULL)
1382 continue;
1383 }
1384 else if (def->defnamespace == NULL)
1385 continue;
1386 else if (strcmp(def->defnamespace, nameSpace) != 0)
1387 continue;
1388
1389 /*
1390 * Flatten the DefElem into a text string like "name=arg". If we
1391 * have just "name", assume "name=true" is meant. Note: the
1392 * namespace is not output.
1393 */
1394 name = def->defname;
1395 if (def->arg != NULL)
1396 value = defGetString(def);
1397 else
1398 value = "true";
1399
1400 /* Insist that name not contain "=", else "a=b=c" is ambiguous */
1401 if (strchr(name, '=') != NULL)
1402 ereport(ERROR,
1404 errmsg("invalid option name \"%s\": must not contain \"=\"",
1405 name)));
1406
1407 /*
1408 * This is not a great place for this test, but there's no other
1409 * convenient place to filter the option out. As WITH (oids =
1410 * false) will be removed someday, this seems like an acceptable
1411 * amount of ugly.
1412 */
1413 if (acceptOidsOff && def->defnamespace == NULL &&
1414 strcmp(name, "oids") == 0)
1415 {
1416 if (defGetBoolean(def))
1417 ereport(ERROR,
1419 errmsg("tables declared WITH OIDS are not supported")));
1420 /* skip over option, reloptions machinery doesn't know it */
1421 continue;
1422 }
1423
1424 len = VARHDRSZ + strlen(name) + 1 + strlen(value);
1425 /* +1 leaves room for sprintf's trailing null */
1426 t = (text *) palloc(len + 1);
1427 SET_VARSIZE(t, len);
1428 sprintf(VARDATA(t), "%s=%s", name, value);
1429
1430 astate = accumArrayResult(astate, PointerGetDatum(t),
1431 false, TEXTOID,
1433 }
1434 }
1435
1436 if (astate)
1438 else
1439 result = (Datum) 0;
1440
1441 return result;
1442}
1443
1444
1445/*
1446 * Convert the text-array format of reloptions into a List of DefElem.
1447 * This is the inverse of transformRelOptions().
1448 */
1449List *
1451{
1452 List *result = NIL;
1453 ArrayType *array;
1455 int noptions;
1456 int i;
1457
1458 /* Nothing to do if no options */
1460 return result;
1461
1462 array = DatumGetArrayTypeP(options);
1463
1465
1466 for (i = 0; i < noptions; i++)
1467 {
1468 char *s;
1469 char *p;
1470 Node *val = NULL;
1471
1473 p = strchr(s, '=');
1474 if (p)
1475 {
1476 *p++ = '\0';
1477 val = (Node *) makeString(p);
1478 }
1479 result = lappend(result, makeDefElem(s, val, -1));
1480 }
1481
1482 return result;
1483}
1484
1485/*
1486 * Extract and parse reloptions from a pg_class tuple.
1487 *
1488 * This is a low-level routine, expected to be used by relcache code and
1489 * callers that do not have a table's relcache entry (e.g. autovacuum). For
1490 * other uses, consider grabbing the rd_options pointer from the relcache entry
1491 * instead.
1492 *
1493 * tupdesc is pg_class' tuple descriptor. amoptions is a pointer to the index
1494 * AM's options parser function in the case of a tuple corresponding to an
1495 * index, or NULL otherwise.
1496 */
1497bytea *
1499 amoptions_function amoptions)
1500{
1501 bytea *options;
1502 bool isnull;
1503 Datum datum;
1505
1506 datum = fastgetattr(tuple,
1508 tupdesc,
1509 &isnull);
1510 if (isnull)
1511 return NULL;
1512
1514
1515 /* Parse into appropriate format; don't error out here */
1516 switch (classForm->relkind)
1517 {
1518 case RELKIND_RELATION:
1519 case RELKIND_TOASTVALUE:
1520 case RELKIND_MATVIEW:
1521 options = heap_reloptions(classForm->relkind, datum, false);
1522 break;
1524 options = partitioned_table_reloptions(datum, false);
1525 break;
1526 case RELKIND_VIEW:
1527 options = view_reloptions(datum, false);
1528 break;
1529 case RELKIND_INDEX:
1531 options = index_reloptions(amoptions, datum, false);
1532 break;
1534 options = NULL;
1535 break;
1536 default:
1537 Assert(false); /* can't get here */
1538 options = NULL; /* keep compiler quiet */
1539 break;
1540 }
1541
1542 return options;
1543}
1544
1545static void
1547 relopt_value *reloptions, int numoptions)
1548{
1551 int noptions;
1552 int i;
1553
1555
1556 for (i = 0; i < noptions; i++)
1557 {
1560 int j;
1561
1562 /* Search for a match in reloptions */
1563 for (j = 0; j < numoptions; j++)
1564 {
1565 int kw_len = reloptions[j].gen->namelen;
1566
1567 if (text_len > kw_len && text_str[kw_len] == '=' &&
1568 strncmp(text_str, reloptions[j].gen->name, kw_len) == 0)
1569 {
1570 parse_one_reloption(&reloptions[j], text_str, text_len,
1571 validate);
1572 break;
1573 }
1574 }
1575
1576 if (j >= numoptions && validate)
1577 {
1578 char *s;
1579 char *p;
1580
1582 p = strchr(s, '=');
1583 if (p)
1584 *p = '\0';
1585 ereport(ERROR,
1587 errmsg("unrecognized parameter \"%s\"", s)));
1588 }
1589 }
1590
1591 /* It's worth avoiding memory leaks in this function */
1593
1594 if (((void *) array) != DatumGetPointer(options))
1595 pfree(array);
1596}
1597
1598/*
1599 * Interpret reloptions that are given in text-array format.
1600 *
1601 * options is a reloption text array as constructed by transformRelOptions.
1602 * kind specifies the family of options to be processed.
1603 *
1604 * The return value is a relopt_value * array on which the options actually
1605 * set in the options array are marked with isset=true. The length of this
1606 * array is returned in *numrelopts. Options not set are also present in the
1607 * array; this is so that the caller can easily locate the default values.
1608 *
1609 * If there are no options of the given kind, numrelopts is set to 0 and NULL
1610 * is returned (unless options are illegally supplied despite none being
1611 * defined, in which case an error occurs).
1612 *
1613 * Note: values of type int, bool and real are allocated as part of the
1614 * returned array. Values of type string are allocated separately and must
1615 * be freed by the caller.
1616 */
1617static relopt_value *
1619 int *numrelopts)
1620{
1621 relopt_value *reloptions = NULL;
1622 int numoptions = 0;
1623 int i;
1624 int j;
1625
1628
1629 /* Build a list of expected options, based on kind */
1630
1631 for (i = 0; relOpts[i]; i++)
1632 if (relOpts[i]->kinds & kind)
1633 numoptions++;
1634
1635 if (numoptions > 0)
1636 {
1637 reloptions = palloc(numoptions * sizeof(relopt_value));
1638
1639 for (i = 0, j = 0; relOpts[i]; i++)
1640 {
1641 if (relOpts[i]->kinds & kind)
1642 {
1643 reloptions[j].gen = relOpts[i];
1644 reloptions[j].isset = false;
1645 j++;
1646 }
1647 }
1648 }
1649
1650 /* Done if no options */
1653
1655 return reloptions;
1656}
1657
1658/* Parse local unregistered options. */
1659static relopt_value *
1661{
1662 int nopts = list_length(relopts->options);
1664 ListCell *lc;
1665 int i = 0;
1666
1667 foreach(lc, relopts->options)
1668 {
1669 local_relopt *opt = lfirst(lc);
1670
1671 values[i].gen = opt->option;
1672 values[i].isset = false;
1673
1674 i++;
1675 }
1676
1677 if (options != (Datum) 0)
1679
1680 return values;
1681}
1682
1683/*
1684 * Subroutine for parseRelOptions, to parse and validate a single option's
1685 * value
1686 */
1687static void
1689 bool validate)
1690{
1691 char *value;
1692 int value_len;
1693 bool parsed;
1694 bool nofree = false;
1695
1696 if (option->isset && validate)
1697 ereport(ERROR,
1699 errmsg("parameter \"%s\" specified more than once",
1700 option->gen->name)));
1701
1702 value_len = text_len - option->gen->namelen - 1;
1703 value = (char *) palloc(value_len + 1);
1704 memcpy(value, text_str + option->gen->namelen + 1, value_len);
1705 value[value_len] = '\0';
1706
1707 switch (option->gen->type)
1708 {
1709 case RELOPT_TYPE_BOOL:
1710 {
1711 parsed = parse_bool(value, &option->bool_val);
1712 if (validate && !parsed)
1713 ereport(ERROR,
1715 errmsg("invalid value for boolean option \"%s\": %s",
1716 option->gen->name, value)));
1717 }
1718 break;
1720 {
1721 bool b;
1722
1723 parsed = parse_bool(value, &b);
1724 option->ternary_val = b ? PG_TERNARY_TRUE :
1726 if (validate && !parsed)
1727 ereport(ERROR,
1729 errmsg("invalid value for boolean option \"%s\": %s",
1730 option->gen->name, value));
1731 }
1732 break;
1733 case RELOPT_TYPE_INT:
1734 {
1735 relopt_int *optint = (relopt_int *) option->gen;
1736
1737 parsed = parse_int(value, &option->int_val, 0, NULL);
1738 if (validate && !parsed)
1739 ereport(ERROR,
1741 errmsg("invalid value for integer option \"%s\": %s",
1742 option->gen->name, value)));
1743 if (validate && (option->int_val < optint->min ||
1744 option->int_val > optint->max))
1745 ereport(ERROR,
1747 errmsg("value %s out of bounds for option \"%s\"",
1748 value, option->gen->name),
1749 errdetail("Valid values are between \"%d\" and \"%d\".",
1750 optint->min, optint->max)));
1751 }
1752 break;
1753 case RELOPT_TYPE_REAL:
1754 {
1756
1757 parsed = parse_real(value, &option->real_val, 0, NULL);
1758 if (validate && !parsed)
1759 ereport(ERROR,
1761 errmsg("invalid value for floating point option \"%s\": %s",
1762 option->gen->name, value)));
1763 if (validate && (option->real_val < optreal->min ||
1764 option->real_val > optreal->max))
1765 ereport(ERROR,
1767 errmsg("value %s out of bounds for option \"%s\"",
1768 value, option->gen->name),
1769 errdetail("Valid values are between \"%f\" and \"%f\".",
1770 optreal->min, optreal->max)));
1771 }
1772 break;
1773 case RELOPT_TYPE_ENUM:
1774 {
1777
1778 parsed = false;
1779 for (elt = optenum->members; elt->string_val; elt++)
1780 {
1781 if (pg_strcasecmp(value, elt->string_val) == 0)
1782 {
1783 option->enum_val = elt->symbol_val;
1784 parsed = true;
1785 break;
1786 }
1787 }
1788 if (validate && !parsed)
1789 ereport(ERROR,
1791 errmsg("invalid value for enum option \"%s\": %s",
1792 option->gen->name, value),
1793 optenum->detailmsg ?
1794 errdetail_internal("%s", _(optenum->detailmsg)) : 0));
1795
1796 /*
1797 * If value is not among the allowed string values, but we are
1798 * not asked to validate, just use the default numeric value.
1799 */
1800 if (!parsed)
1801 option->enum_val = optenum->default_val;
1802 }
1803 break;
1804 case RELOPT_TYPE_STRING:
1805 {
1807
1808 option->string_val = value;
1809 nofree = true;
1810 if (validate && optstring->validate_cb)
1811 (optstring->validate_cb) (value);
1812 parsed = true;
1813 }
1814 break;
1815 default:
1816 elog(ERROR, "unsupported reloption type %d", option->gen->type);
1817 parsed = true; /* quiet compiler */
1818 break;
1819 }
1820
1821 if (parsed)
1822 option->isset = true;
1823 if (!nofree)
1824 pfree(value);
1825}
1826
1827/*
1828 * Given the result from parseRelOptions, allocate a struct that's of the
1829 * specified base size plus any extra space that's needed for string variables.
1830 *
1831 * "base" should be sizeof(struct) of the reloptions struct (StdRdOptions or
1832 * equivalent).
1833 */
1834static void *
1836{
1837 Size size = base;
1838 int i;
1839
1840 for (i = 0; i < numoptions; i++)
1841 {
1843
1844 if (optval->gen->type == RELOPT_TYPE_STRING)
1845 {
1847
1848 if (optstr->fill_cb)
1849 {
1850 const char *val = optval->isset ? optval->string_val :
1851 optstr->default_isnull ? NULL : optstr->default_val;
1852
1853 size += optstr->fill_cb(val, NULL);
1854 }
1855 else
1856 size += GET_STRING_RELOPTION_LEN(*optval) + 1;
1857 }
1858 }
1859
1860 return palloc0(size);
1861}
1862
1863/*
1864 * Given the result of parseRelOptions and a parsing table, fill in the
1865 * struct (previously allocated with allocateReloptStruct) with the parsed
1866 * values.
1867 *
1868 * rdopts is the pointer to the allocated struct to be filled.
1869 * basesize is the sizeof(struct) that was passed to allocateReloptStruct.
1870 * options, of length numoptions, is parseRelOptions' output.
1871 * elems, of length numelems, is the table describing the allowed options.
1872 * When validate is true, it is expected that all options appear in elems.
1873 */
1874static void
1877 bool validate,
1878 const relopt_parse_elt *elems, int numelems)
1879{
1880 int i;
1881 int offset = basesize;
1882
1883 for (i = 0; i < numoptions; i++)
1884 {
1885 int j;
1886 bool found = false;
1887
1888 for (j = 0; j < numelems; j++)
1889 {
1890 if (strcmp(options[i].gen->name, elems[j].optname) == 0)
1891 {
1893 char *itempos = ((char *) rdopts) + elems[j].offset;
1894 char *string_val;
1895
1896 switch (options[i].gen->type)
1897 {
1898 case RELOPT_TYPE_BOOL:
1899 *(bool *) itempos = options[i].isset ?
1900 options[i].bool_val :
1901 ((relopt_bool *) options[i].gen)->default_val;
1902 break;
1904 *(pg_ternary *) itempos = options[i].isset ?
1905 options[i].ternary_val : PG_TERNARY_UNSET;
1906 break;
1907 case RELOPT_TYPE_INT:
1908 *(int *) itempos = options[i].isset ?
1909 options[i].int_val :
1910 ((relopt_int *) options[i].gen)->default_val;
1911 break;
1912 case RELOPT_TYPE_REAL:
1913 *(double *) itempos = options[i].isset ?
1914 options[i].real_val :
1915 ((relopt_real *) options[i].gen)->default_val;
1916 break;
1917 case RELOPT_TYPE_ENUM:
1918 *(int *) itempos = options[i].isset ?
1919 options[i].enum_val :
1920 ((relopt_enum *) options[i].gen)->default_val;
1921 break;
1922 case RELOPT_TYPE_STRING:
1923 optstring = (relopt_string *) options[i].gen;
1924 if (options[i].isset)
1925 string_val = options[i].string_val;
1926 else if (!optstring->default_isnull)
1927 string_val = optstring->default_val;
1928 else
1929 string_val = NULL;
1930
1931 if (optstring->fill_cb)
1932 {
1933 Size size =
1934 optstring->fill_cb(string_val,
1935 (char *) rdopts + offset);
1936
1937 if (size)
1938 {
1939 *(int *) itempos = offset;
1940 offset += size;
1941 }
1942 else
1943 *(int *) itempos = 0;
1944 }
1945 else if (string_val == NULL)
1946 *(int *) itempos = 0;
1947 else
1948 {
1949 strcpy((char *) rdopts + offset, string_val);
1950 *(int *) itempos = offset;
1951 offset += strlen(string_val) + 1;
1952 }
1953 break;
1954 default:
1955 elog(ERROR, "unsupported reloption type %d",
1956 options[i].gen->type);
1957 break;
1958 }
1959 found = true;
1960 break;
1961 }
1962 }
1963 if (validate && !found)
1964 elog(ERROR, "reloption \"%s\" not found in parse table",
1965 options[i].gen->name);
1966 }
1967 SET_VARSIZE(rdopts, offset);
1968}
1969
1970
1971/*
1972 * Option parser for anything that uses StdRdOptions.
1973 */
1974bytea *
1976{
1977 static const relopt_parse_elt tab[] = {
1979 {"autovacuum_enabled", RELOPT_TYPE_BOOL,
1980 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, enabled)},
1981 {"autovacuum_parallel_workers", RELOPT_TYPE_INT,
1982 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, autovacuum_parallel_workers)},
1983 {"autovacuum_vacuum_threshold", RELOPT_TYPE_INT,
1984 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_threshold)},
1985 {"autovacuum_vacuum_max_threshold", RELOPT_TYPE_INT,
1986 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_max_threshold)},
1987 {"autovacuum_vacuum_insert_threshold", RELOPT_TYPE_INT,
1988 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_ins_threshold)},
1989 {"autovacuum_analyze_threshold", RELOPT_TYPE_INT,
1990 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, analyze_threshold)},
1991 {"autovacuum_vacuum_cost_limit", RELOPT_TYPE_INT,
1993 {"autovacuum_freeze_min_age", RELOPT_TYPE_INT,
1994 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_min_age)},
1995 {"autovacuum_freeze_max_age", RELOPT_TYPE_INT,
1996 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_max_age)},
1997 {"autovacuum_freeze_table_age", RELOPT_TYPE_INT,
1998 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_table_age)},
1999 {"autovacuum_multixact_freeze_min_age", RELOPT_TYPE_INT,
2000 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_min_age)},
2001 {"autovacuum_multixact_freeze_max_age", RELOPT_TYPE_INT,
2002 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_max_age)},
2003 {"autovacuum_multixact_freeze_table_age", RELOPT_TYPE_INT,
2004 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_table_age)},
2005 {"log_autovacuum_min_duration", RELOPT_TYPE_INT,
2006 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, log_vacuum_min_duration)},
2007 {"log_autoanalyze_min_duration", RELOPT_TYPE_INT,
2008 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, log_analyze_min_duration)},
2009 {"toast_tuple_target", RELOPT_TYPE_INT,
2010 offsetof(StdRdOptions, toast_tuple_target)},
2011 {"autovacuum_vacuum_cost_delay", RELOPT_TYPE_REAL,
2013 {"autovacuum_vacuum_scale_factor", RELOPT_TYPE_REAL,
2014 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_scale_factor)},
2015 {"autovacuum_vacuum_insert_scale_factor", RELOPT_TYPE_REAL,
2016 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_ins_scale_factor)},
2017 {"autovacuum_analyze_scale_factor", RELOPT_TYPE_REAL,
2018 offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, analyze_scale_factor)},
2019 {"user_catalog_table", RELOPT_TYPE_BOOL,
2020 offsetof(StdRdOptions, user_catalog_table)},
2021 {"parallel_workers", RELOPT_TYPE_INT,
2022 offsetof(StdRdOptions, parallel_workers)},
2023 {"vacuum_index_cleanup", RELOPT_TYPE_ENUM,
2024 offsetof(StdRdOptions, vacuum_index_cleanup)},
2025 {"vacuum_truncate", RELOPT_TYPE_TERNARY,
2027 {"vacuum_max_eager_freeze_failure_rate", RELOPT_TYPE_REAL,
2029 };
2030
2031 return (bytea *) build_reloptions(reloptions, validate, kind,
2032 sizeof(StdRdOptions),
2033 tab, lengthof(tab));
2034}
2035
2036/*
2037 * build_reloptions
2038 *
2039 * Parses "reloptions" provided by the caller, returning them in a
2040 * structure containing the parsed options. The parsing is done with
2041 * the help of a parsing table describing the allowed options, defined
2042 * by "relopt_elems" of length "num_relopt_elems".
2043 *
2044 * "validate" must be true if reloptions value is freshly built by
2045 * transformRelOptions(), as opposed to being read from the catalog, in which
2046 * case the values contained in it must already be valid.
2047 *
2048 * NULL is returned if the passed-in options did not match any of the options
2049 * in the parsing table, unless validate is true in which case an error would
2050 * be reported.
2051 */
2052void *
2054 relopt_kind kind,
2055 Size relopt_struct_size,
2057 int num_relopt_elems)
2058{
2059 int numoptions;
2061 void *rdopts;
2062
2063 /* parse options specific to given relation option kind */
2064 options = parseRelOptions(reloptions, validate, kind, &numoptions);
2066
2067 /* if none set, we're done */
2068 if (numoptions == 0)
2069 {
2070 Assert(options == NULL);
2071 return NULL;
2072 }
2073
2074 /* allocate and fill the structure */
2075 rdopts = allocateReloptStruct(relopt_struct_size, options, numoptions);
2076 fillRelOptions(rdopts, relopt_struct_size, options, numoptions,
2078
2079 pfree(options);
2080
2081 return rdopts;
2082}
2083
2084/*
2085 * Parse local options, allocate a bytea struct that's of the specified
2086 * 'base_size' plus any extra space that's needed for string variables,
2087 * fill its option's fields located at the given offsets and return it.
2088 */
2089void *
2091{
2092 int noptions = list_length(relopts->options);
2094 relopt_value *vals;
2095 void *opts;
2096 int i = 0;
2097 ListCell *lc;
2098
2099 foreach(lc, relopts->options)
2100 {
2101 local_relopt *opt = lfirst(lc);
2102
2103 elems[i].optname = opt->option->name;
2104 elems[i].opttype = opt->option->type;
2105 elems[i].offset = opt->offset;
2106
2107 i++;
2108 }
2109
2111 opts = allocateReloptStruct(relopts->relopt_struct_size, vals, noptions);
2112 fillRelOptions(opts, relopts->relopt_struct_size, vals, noptions, validate,
2113 elems, noptions);
2114
2115 if (validate)
2116 foreach(lc, relopts->validators)
2117 ((relopts_validator) lfirst(lc)) (opts, vals, noptions);
2118
2119 if (elems)
2120 pfree(elems);
2121
2122 return opts;
2123}
2124
2125/*
2126 * Option parser for partitioned tables
2127 */
2128bytea *
2130{
2131 if (validate && reloptions)
2132 ereport(ERROR,
2134 errmsg("cannot specify storage parameters for a partitioned table"),
2135 errhint("Specify storage parameters for its leaf partitions instead."));
2136 return NULL;
2137}
2138
2139/*
2140 * Option parser for views
2141 */
2142bytea *
2144{
2145 static const relopt_parse_elt tab[] = {
2146 {"security_barrier", RELOPT_TYPE_BOOL,
2147 offsetof(ViewOptions, security_barrier)},
2148 {"security_invoker", RELOPT_TYPE_BOOL,
2149 offsetof(ViewOptions, security_invoker)},
2150 {"check_option", RELOPT_TYPE_ENUM,
2151 offsetof(ViewOptions, check_option)}
2152 };
2153
2154 return (bytea *) build_reloptions(reloptions, validate,
2156 sizeof(ViewOptions),
2157 tab, lengthof(tab));
2158}
2159
2160/*
2161 * Parse options for heaps, views and toast tables.
2162 */
2163bytea *
2164heap_reloptions(char relkind, Datum reloptions, bool validate)
2165{
2167
2168 switch (relkind)
2169 {
2170 case RELKIND_TOASTVALUE:
2171 rdopts = (StdRdOptions *)
2173 if (rdopts != NULL)
2174 {
2175 /* adjust default-only parameters for TOAST relations */
2176 rdopts->fillfactor = 100;
2177 rdopts->autovacuum.analyze_threshold = -1;
2178 rdopts->autovacuum.analyze_scale_factor = -1;
2179 }
2180 return (bytea *) rdopts;
2181 case RELKIND_RELATION:
2182 case RELKIND_MATVIEW:
2183 return default_reloptions(reloptions, validate, RELOPT_KIND_HEAP);
2184 default:
2185 /* other relkinds are not supported */
2186 return NULL;
2187 }
2188}
2189
2190
2191/*
2192 * Parse options for indexes.
2193 *
2194 * amoptions index AM's option parser function
2195 * reloptions options as text[] datum
2196 * validate error flag
2197 */
2198bytea *
2200{
2201 Assert(amoptions != NULL);
2202
2203 /* Assume function is strict */
2204 if (DatumGetPointer(reloptions) == NULL)
2205 return NULL;
2206
2207 return amoptions(reloptions, validate);
2208}
2209
2210/*
2211 * Option parser for attribute reloptions
2212 */
2213bytea *
2215{
2216 static const relopt_parse_elt tab[] = {
2217 {"n_distinct", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct)},
2218 {"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)}
2219 };
2220
2221 return (bytea *) build_reloptions(reloptions, validate,
2223 sizeof(AttributeOpts),
2224 tab, lengthof(tab));
2225}
2226
2227/*
2228 * Option parser for tablespace reloptions
2229 */
2230bytea *
2232{
2233 static const relopt_parse_elt tab[] = {
2236 {"effective_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, effective_io_concurrency)},
2237 {"maintenance_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, maintenance_io_concurrency)}
2238 };
2239
2240 return (bytea *) build_reloptions(reloptions, validate,
2242 sizeof(TableSpaceOpts),
2243 tab, lengthof(tab));
2244}
2245
2246/*
2247 * Determine the required LOCKMODE from an option list.
2248 *
2249 * Called from AlterTableGetLockLevel(), see that function
2250 * for a longer explanation of how this works.
2251 */
2254{
2255 LOCKMODE lockmode = NoLock;
2256 ListCell *cell;
2257
2258 if (defList == NIL)
2259 return AccessExclusiveLock;
2260
2263
2264 foreach(cell, defList)
2265 {
2266 DefElem *def = (DefElem *) lfirst(cell);
2267 int i;
2268
2269 for (i = 0; relOpts[i]; i++)
2270 {
2271 if (strncmp(relOpts[i]->name,
2272 def->defname,
2273 relOpts[i]->namelen + 1) == 0)
2274 {
2275 if (lockmode < relOpts[i]->lockmode)
2276 lockmode = relOpts[i]->lockmode;
2277 }
2278 }
2279 }
2280
2281 return lockmode;
2282}
bytea *(* amoptions_function)(Datum reloptions, bool validate)
Definition amapi.h:165
#define DatumGetArrayTypeP(X)
Definition array.h:261
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
static bool validate(Port *port, const char *auth, const char **logdetail)
Definition auth-oauth.c:672
bool parse_bool(const char *value, bool *result)
Definition bool.c:31
static Datum values[MAXATTR]
Definition bootstrap.c:190
int maintenance_io_concurrency
Definition bufmgr.c:207
int effective_io_concurrency
Definition bufmgr.c:200
#define MAX_IO_CONCURRENCY
Definition bufmgr.h:197
#define TextDatumGetCString(d)
Definition builtins.h:99
#define gettext_noop(x)
Definition c.h:1285
#define VARHDRSZ
Definition c.h:781
#define Assert(condition)
Definition c.h:943
uint32_t uint32
Definition c.h:624
#define lengthof(array)
Definition c.h:873
size_t Size
Definition c.h:689
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
double random_page_cost
Definition costsize.c:132
double seq_page_cost
Definition costsize.c:131
char * defGetString(DefElem *def)
Definition define.c:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
int errcode(int sqlerrcode)
Definition elog.c:874
#define _(x)
Definition elog.c:95
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
@ GIST_OPTION_BUFFERING_OFF
@ GIST_OPTION_BUFFERING_AUTO
@ GIST_OPTION_BUFFERING_ON
#define GIST_MIN_FILLFACTOR
#define GIST_DEFAULT_FILLFACTOR
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition guc.c:2775
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition guc.c:2865
#define MAX_KILOBYTES
Definition guc.h:29
#define HASH_DEFAULT_FILLFACTOR
Definition hash.h:296
#define HASH_MIN_FILLFACTOR
Definition hash.h:295
#define TOAST_TUPLE_TARGET
Definition heaptoast.h:50
#define TOAST_TUPLE_TARGET_MAIN
Definition heaptoast.h:61
static void * GETSTRUCT(const HeapTupleData *tuple)
static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
long val
Definition informix.c:689
static struct @177 value
int b
Definition isn.c:74
int j
Definition isn.c:78
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
Definition lock.c:621
int LOCKMODE
Definition lockdefs.h:26
#define NoLock
Definition lockdefs.h:34
#define AccessExclusiveLock
Definition lockdefs.h:43
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition makefuncs.c:637
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition mcxt.c:1768
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
char * pstrdup(const char *in)
Definition mcxt.c:1781
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
MemoryContext TopMemoryContext
Definition mcxt.c:166
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define BTREE_MIN_FILLFACTOR
Definition nbtree.h:200
#define BTREE_DEFAULT_FILLFACTOR
Definition nbtree.h:201
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static AmcheckOptions opts
Definition pg_amcheck.c:112
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
const void size_t len
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
static size_t noptions
static int fillfactor
Definition pgbench.c:188
int pg_strcasecmp(const char *s1, const char *s2)
#define sprintf
Definition port.h:262
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
pg_ternary
Definition postgres.h:544
@ PG_TERNARY_FALSE
Definition postgres.h:545
@ PG_TERNARY_TRUE
Definition postgres.h:546
@ PG_TERNARY_UNSET
Definition postgres.h:547
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static int fb(int x)
tree ctl max_val
Definition radixtree.h:1859
#define HEAP_MIN_FILLFACTOR
Definition rel.h:361
@ VIEW_OPTION_CHECK_OPTION_NOT_SET
Definition rel.h:417
@ VIEW_OPTION_CHECK_OPTION_LOCAL
Definition rel.h:418
@ VIEW_OPTION_CHECK_OPTION_CASCADED
Definition rel.h:419
#define HEAP_DEFAULT_FILLFACTOR
Definition rel.h:362
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO
Definition rel.h:338
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF
Definition rel.h:339
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON
Definition rel.h:340
static int num_custom_options
Definition reloptions.c:599
void add_local_string_reloption(local_relopts *relopts, const char *name, const char *desc, const char *default_val, validate_string_relopt validator, fill_string_relopt filler, int offset)
static relopt_enum * init_enum_reloption(uint32 kinds, const char *name, const char *desc, relopt_enum_elt_def *members, int default_val, const char *detailmsg, LOCKMODE lockmode)
static relopt_real realRelOpts[]
Definition reloptions.c:421
static relopt_enum_elt_def gistBufferingOptValues[]
Definition reloptions.c:534
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
void add_ternary_reloption(uint32 kinds, const char *name, const char *desc, LOCKMODE lockmode)
Definition reloptions.c:957
static relopt_string * init_string_reloption(uint32 kinds, const char *name, const char *desc, const char *default_val, validate_string_relopt validator, fill_string_relopt filler, LOCKMODE lockmode)
static relopt_value * parseLocalRelOptions(local_relopts *relopts, Datum options, bool validate)
void add_int_reloption(uint32 kinds, const char *name, const char *desc, int default_val, int min_val, int max_val, LOCKMODE lockmode)
bytea * tablespace_reloptions(Datum reloptions, bool validate)
static relopt_enum_elt_def StdRdOptIndexCleanupValues[]
Definition reloptions.c:519
#define GET_STRING_RELOPTION_LEN(option)
Definition reloptions.c:612
List * untransformRelOptions(Datum options)
static relopt_int intRelOpts[]
Definition reloptions.c:187
void add_bool_reloption(uint32 kinds, const char *name, const char *desc, bool default_val, LOCKMODE lockmode)
Definition reloptions.c:910
static relopt_enum enumRelOpts[]
Definition reloptions.c:551
static void parse_one_reloption(relopt_value *option, char *text_str, int text_len, bool validate)
static relopt_string stringRelOpts[]
Definition reloptions.c:590
static relopt_ternary * init_ternary_reloption(uint32 kinds, const char *name, const char *desc, LOCKMODE lockmode)
Definition reloptions.c:941
void add_enum_reloption(uint32 kinds, const char *name, const char *desc, relopt_enum_elt_def *members, int default_val, const char *detailmsg, LOCKMODE lockmode)
bytea * view_reloptions(Datum reloptions, bool validate)
void add_real_reloption(uint32 kinds, const char *name, const char *desc, double default_val, double min_val, double max_val, LOCKMODE lockmode)
bytea * index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
bytea * partitioned_table_reloptions(Datum reloptions, bool validate)
void add_local_bool_reloption(local_relopts *relopts, const char *name, const char *desc, bool default_val, int offset)
Definition reloptions.c:926
static relopt_int * init_int_reloption(uint32 kinds, const char *name, const char *desc, int default_val, int min_val, int max_val, LOCKMODE lockmode)
Definition reloptions.c:991
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition reloptions.c:792
static relopt_enum_elt_def viewCheckOptValues[]
Definition reloptions.c:543
void add_local_real_reloption(local_relopts *relopts, const char *name, const char *desc, double default_val, double min_val, double max_val, int offset)
static void add_reloption(relopt_gen *newoption)
Definition reloptions.c:758
static void add_local_reloption(local_relopts *relopts, relopt_gen *newoption, int offset)
Definition reloptions.c:815
Datum transformRelOptions(Datum oldOptions, List *defList, const char *nameSpace, const char *const validnsps[], bool acceptOidsOff, bool isReset)
void add_local_enum_reloption(local_relopts *relopts, const char *name, const char *desc, relopt_enum_elt_def *members, int default_val, const char *detailmsg, int offset)
bytea * extractRelOptions(HeapTuple tuple, TupleDesc tupdesc, amoptions_function amoptions)
static void parseRelOptionsInternal(Datum options, bool validate, relopt_value *reloptions, int numoptions)
static void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int numelems)
static relopt_gen * allocate_reloption(uint32 kinds, int type, const char *name, const char *desc, LOCKMODE lockmode)
Definition reloptions.c:833
static void initialize_reloptions(void)
Definition reloptions.c:623
static relopt_bool * init_bool_reloption(uint32 kinds, const char *name, const char *desc, bool default_val, LOCKMODE lockmode)
Definition reloptions.c:893
relopt_kind add_reloption_kind(void)
Definition reloptions.c:741
void * build_local_reloptions(local_relopts *relopts, Datum options, bool validate)
void register_reloptions_validator(local_relopts *relopts, relopts_validator validator)
Definition reloptions.c:805
static bool need_initialization
Definition reloptions.c:601
static uint32 last_assigned_kind
Definition reloptions.c:597
static relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
static relopt_gen ** relOpts
Definition reloptions.c:596
static relopt_gen ** custom_options
Definition reloptions.c:600
LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList)
bytea * attribute_reloptions(Datum reloptions, bool validate)
static relopt_real * init_real_reloption(uint32 kinds, const char *name, const char *desc, double default_val, double min_val, double max_val, LOCKMODE lockmode)
void add_local_ternary_reloption(local_relopts *relopts, const char *name, const char *desc, int offset)
Definition reloptions.c:975
static relopt_bool boolRelOpts[]
Definition reloptions.c:99
void add_local_int_reloption(local_relopts *relopts, const char *name, const char *desc, int default_val, int min_val, int max_val, int offset)
void add_string_reloption(uint32 kinds, const char *name, const char *desc, const char *default_val, validate_string_relopt validator, LOCKMODE lockmode)
static void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
bytea * heap_reloptions(char relkind, Datum reloptions, bool validate)
static relopt_ternary ternaryRelOpts[]
Definition reloptions.c:169
Size(* fill_string_relopt)(const char *value, void *ptr)
Definition reloptions.h:141
relopt_kind
Definition reloptions.h:40
@ RELOPT_KIND_LAST_DEFAULT
Definition reloptions.h:55
@ RELOPT_KIND_ATTRIBUTE
Definition reloptions.h:48
@ RELOPT_KIND_TOAST
Definition reloptions.h:43
@ RELOPT_KIND_LOCAL
Definition reloptions.h:41
@ RELOPT_KIND_SPGIST
Definition reloptions.h:50
@ RELOPT_KIND_MAX
Definition reloptions.h:57
@ RELOPT_KIND_GIST
Definition reloptions.h:47
@ RELOPT_KIND_VIEW
Definition reloptions.h:51
@ RELOPT_KIND_HEAP
Definition reloptions.h:42
@ RELOPT_KIND_TABLESPACE
Definition reloptions.h:49
@ RELOPT_KIND_GIN
Definition reloptions.h:46
@ RELOPT_KIND_HASH
Definition reloptions.h:45
@ RELOPT_KIND_BRIN
Definition reloptions.h:52
@ RELOPT_KIND_BTREE
Definition reloptions.h:44
void(* validate_string_relopt)(const char *value)
Definition reloptions.h:140
void(* relopts_validator)(void *parsed_options, relopt_value *vals, int nvals)
Definition reloptions.h:144
@ RELOPT_TYPE_ENUM
Definition reloptions.h:34
@ RELOPT_TYPE_INT
Definition reloptions.h:32
@ RELOPT_TYPE_TERNARY
Definition reloptions.h:31
@ RELOPT_TYPE_BOOL
Definition reloptions.h:30
@ RELOPT_TYPE_REAL
Definition reloptions.h:33
@ RELOPT_TYPE_STRING
Definition reloptions.h:35
#define SPGIST_DEFAULT_FILLFACTOR
#define SPGIST_MIN_FILLFACTOR
char * defnamespace
Definition parsenodes.h:859
char * defname
Definition parsenodes.h:860
Node * arg
Definition parsenodes.h:861
Definition pg_list.h:54
Definition nodes.h:135
relopt_gen * option
Definition reloptions.h:167
const char * name
Definition getopt_long.h:19
relopt_gen gen
Definition reloptions.h:94
relopt_gen gen
Definition reloptions.h:132
const char * name
Definition reloptions.h:66
LOCKMODE lockmode
Definition reloptions.h:70
relopt_type type
Definition reloptions.h:72
relopt_gen gen
Definition reloptions.h:106
const char * optname
Definition reloptions.h:159
relopt_type opttype
Definition reloptions.h:160
relopt_gen gen
Definition reloptions.h:114
relopt_gen gen
Definition reloptions.h:148
relopt_gen gen
Definition reloptions.h:100
relopt_gen * gen
Definition reloptions.h:78
Definition c.h:776
double vacuum_max_eager_freeze_failure_rate
Definition vacuum.c:82
double vacuum_cost_delay
Definition vacuum.c:92
int vacuum_cost_limit
Definition vacuum.c:93
bool vacuum_truncate
Definition vacuum.c:84
String * makeString(char *str)
Definition value.c:63
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
const char * type
const char * name