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