Age Owner Branch data TLA Line data Source code
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"
26 : : #include "access/spgist_private.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 : :
99 : : static relopt_bool boolRelOpts[] =
100 : : {
101 : : {
102 : : {
103 : : "autosummarize",
104 : : "Enables automatic summarization on this BRIN index",
105 : : RELOPT_KIND_BRIN,
106 : : AccessExclusiveLock
107 : : },
108 : : false
109 : : },
110 : : {
111 : : {
112 : : "user_catalog_table",
113 : : "Declare a table as an additional catalog table, e.g. for the purpose of logical replication",
114 : : RELOPT_KIND_HEAP,
115 : : AccessExclusiveLock
116 : : },
117 : : false
118 : : },
119 : : {
120 : : {
121 : : "fastupdate",
122 : : "Enables \"fast update\" feature for this GIN index",
123 : : RELOPT_KIND_GIN,
124 : : AccessExclusiveLock
125 : : },
126 : : true
127 : : },
128 : : {
129 : : {
130 : : "security_barrier",
131 : : "View acts as a row security barrier",
132 : : RELOPT_KIND_VIEW,
133 : : AccessExclusiveLock
134 : : },
135 : : false
136 : : },
137 : : {
138 : : {
139 : : "security_invoker",
140 : : "Privileges on underlying relations are checked as the invoking user, not the view owner",
141 : : RELOPT_KIND_VIEW,
142 : : AccessExclusiveLock
143 : : },
144 : : false
145 : : },
146 : : {
147 : : {
148 : : "deduplicate_items",
149 : : "Enables \"deduplicate items\" feature for this btree index",
150 : : RELOPT_KIND_BTREE,
151 : : ShareUpdateExclusiveLock /* since it applies only to later
152 : : * inserts */
153 : : },
154 : : true
155 : : },
156 : : /* list terminator */
157 : : {{NULL}}
158 : : };
159 : :
160 : : static relopt_ternary ternaryRelOpts[] =
161 : : {
162 : : {
163 : : {
164 : : "autovacuum_enabled",
165 : : "Enables autovacuum in this relation",
166 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
167 : : ShareUpdateExclusiveLock
168 : : }
169 : : },
170 : : {
171 : : {
172 : : "vacuum_truncate",
173 : : "Enables vacuum to truncate empty pages at the end of this table",
174 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
175 : : ShareUpdateExclusiveLock
176 : : }
177 : : },
178 : : /* list terminator */
179 : : {
180 : : {
181 : : NULL
182 : : }
183 : : }
184 : : };
185 : :
186 : : static relopt_int intRelOpts[] =
187 : : {
188 : : {
189 : : {
190 : : "fillfactor",
191 : : "Packs table pages only to this percentage",
192 : : RELOPT_KIND_HEAP,
193 : : ShareUpdateExclusiveLock /* since it applies only to later
194 : : * inserts */
195 : : },
196 : : HEAP_DEFAULT_FILLFACTOR, HEAP_MIN_FILLFACTOR, 100
197 : : },
198 : : {
199 : : {
200 : : "fillfactor",
201 : : "Packs btree index pages only to this percentage",
202 : : RELOPT_KIND_BTREE,
203 : : ShareUpdateExclusiveLock /* since it applies only to later
204 : : * inserts */
205 : : },
206 : : BTREE_DEFAULT_FILLFACTOR, BTREE_MIN_FILLFACTOR, 100
207 : : },
208 : : {
209 : : {
210 : : "fillfactor",
211 : : "Packs hash index pages only to this percentage",
212 : : RELOPT_KIND_HASH,
213 : : ShareUpdateExclusiveLock /* since it applies only to later
214 : : * inserts */
215 : : },
216 : : HASH_DEFAULT_FILLFACTOR, HASH_MIN_FILLFACTOR, 100
217 : : },
218 : : {
219 : : {
220 : : "fillfactor",
221 : : "Packs gist index pages only to this percentage",
222 : : RELOPT_KIND_GIST,
223 : : ShareUpdateExclusiveLock /* since it applies only to later
224 : : * inserts */
225 : : },
226 : : GIST_DEFAULT_FILLFACTOR, GIST_MIN_FILLFACTOR, 100
227 : : },
228 : : {
229 : : {
230 : : "fillfactor",
231 : : "Packs spgist index pages only to this percentage",
232 : : RELOPT_KIND_SPGIST,
233 : : ShareUpdateExclusiveLock /* since it applies only to later
234 : : * inserts */
235 : : },
236 : : SPGIST_DEFAULT_FILLFACTOR, SPGIST_MIN_FILLFACTOR, 100
237 : : },
238 : : {
239 : : {
240 : : "autovacuum_parallel_workers",
241 : : "Maximum number of parallel autovacuum workers that can be used for processing this table.",
242 : : RELOPT_KIND_HEAP,
243 : : ShareUpdateExclusiveLock
244 : : },
245 : : -1, -1, 1024
246 : : },
247 : : {
248 : : {
249 : : "autovacuum_vacuum_threshold",
250 : : "Minimum number of tuple updates or deletes prior to vacuum",
251 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
252 : : ShareUpdateExclusiveLock
253 : : },
254 : : -1, 0, INT_MAX
255 : : },
256 : : {
257 : : {
258 : : "autovacuum_vacuum_max_threshold",
259 : : "Maximum number of tuple updates or deletes prior to vacuum",
260 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
261 : : ShareUpdateExclusiveLock
262 : : },
263 : : -2, -1, INT_MAX
264 : : },
265 : : {
266 : : {
267 : : "autovacuum_vacuum_insert_threshold",
268 : : "Minimum number of tuple inserts prior to vacuum, or -1 to disable insert vacuums",
269 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
270 : : ShareUpdateExclusiveLock
271 : : },
272 : : -2, -1, INT_MAX
273 : : },
274 : : {
275 : : {
276 : : "autovacuum_analyze_threshold",
277 : : "Minimum number of tuple inserts, updates or deletes prior to analyze",
278 : : RELOPT_KIND_HEAP,
279 : : ShareUpdateExclusiveLock
280 : : },
281 : : -1, 0, INT_MAX
282 : : },
283 : : {
284 : : {
285 : : "autovacuum_vacuum_cost_limit",
286 : : "Vacuum cost amount available before napping, for autovacuum",
287 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
288 : : ShareUpdateExclusiveLock
289 : : },
290 : : -1, 1, 10000
291 : : },
292 : : {
293 : : {
294 : : "autovacuum_freeze_min_age",
295 : : "Minimum age at which VACUUM should freeze a table row, for autovacuum",
296 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
297 : : ShareUpdateExclusiveLock
298 : : },
299 : : -1, 0, 1000000000
300 : : },
301 : : {
302 : : {
303 : : "autovacuum_multixact_freeze_min_age",
304 : : "Minimum multixact age at which VACUUM should freeze a row multixact's, for autovacuum",
305 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
306 : : ShareUpdateExclusiveLock
307 : : },
308 : : -1, 0, 1000000000
309 : : },
310 : : {
311 : : {
312 : : "autovacuum_freeze_max_age",
313 : : "Age at which to autovacuum a table to prevent transaction ID wraparound",
314 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
315 : : ShareUpdateExclusiveLock
316 : : },
317 : : -1, 100000, 2000000000
318 : : },
319 : : {
320 : : {
321 : : "autovacuum_multixact_freeze_max_age",
322 : : "Multixact age at which to autovacuum a table to prevent multixact wraparound",
323 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
324 : : ShareUpdateExclusiveLock
325 : : },
326 : : -1, 10000, 2000000000
327 : : },
328 : : {
329 : : {
330 : : "autovacuum_freeze_table_age",
331 : : "Age at which VACUUM should perform a full table sweep to freeze row versions",
332 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
333 : : ShareUpdateExclusiveLock
334 : : }, -1, 0, 2000000000
335 : : },
336 : : {
337 : : {
338 : : "autovacuum_multixact_freeze_table_age",
339 : : "Age of multixact at which VACUUM should perform a full table sweep to freeze row versions",
340 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
341 : : ShareUpdateExclusiveLock
342 : : }, -1, 0, 2000000000
343 : : },
344 : : {
345 : : {
346 : : "log_autovacuum_min_duration",
347 : : "Sets the minimum execution time above which vacuum actions by autovacuum will be logged",
348 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
349 : : ShareUpdateExclusiveLock
350 : : },
351 : : -2, -1, INT_MAX
352 : : },
353 : : {
354 : : {
355 : : "log_autoanalyze_min_duration",
356 : : "Sets the minimum execution time above which analyze actions by autovacuum will be logged",
357 : : RELOPT_KIND_HEAP,
358 : : ShareUpdateExclusiveLock
359 : : },
360 : : -1, -1, INT_MAX
361 : : },
362 : : {
363 : : {
364 : : "toast_tuple_target",
365 : : "Sets the target tuple length at which external columns will be toasted",
366 : : RELOPT_KIND_HEAP,
367 : : ShareUpdateExclusiveLock
368 : : },
369 : : TOAST_TUPLE_TARGET, 128, TOAST_TUPLE_TARGET_MAIN
370 : : },
371 : : {
372 : : {
373 : : "pages_per_range",
374 : : "Number of pages that each page range covers in a BRIN index",
375 : : RELOPT_KIND_BRIN,
376 : : AccessExclusiveLock
377 : : }, 128, 1, 131072
378 : : },
379 : : {
380 : : {
381 : : "gin_pending_list_limit",
382 : : "Maximum size of the pending list for this GIN index, in kilobytes.",
383 : : RELOPT_KIND_GIN,
384 : : AccessExclusiveLock
385 : : },
386 : : -1, 64, MAX_KILOBYTES
387 : : },
388 : : {
389 : : {
390 : : "effective_io_concurrency",
391 : : "Number of simultaneous requests that can be handled efficiently by the disk subsystem.",
392 : : RELOPT_KIND_TABLESPACE,
393 : : ShareUpdateExclusiveLock
394 : : },
395 : : -1, 0, MAX_IO_CONCURRENCY
396 : : },
397 : : {
398 : : {
399 : : "maintenance_io_concurrency",
400 : : "Number of simultaneous requests that can be handled efficiently by the disk subsystem for maintenance work.",
401 : : RELOPT_KIND_TABLESPACE,
402 : : ShareUpdateExclusiveLock
403 : : },
404 : : -1, 0, MAX_IO_CONCURRENCY
405 : : },
406 : : {
407 : : {
408 : : "parallel_workers",
409 : : "Number of parallel processes that can be used per executor node for this relation.",
410 : : RELOPT_KIND_HEAP,
411 : : ShareUpdateExclusiveLock
412 : : },
413 : : -1, 0, 1024
414 : : },
415 : :
416 : : /* list terminator */
417 : : {{NULL}}
418 : : };
419 : :
420 : : static relopt_real realRelOpts[] =
421 : : {
422 : : {
423 : : {
424 : : "autovacuum_vacuum_cost_delay",
425 : : "Vacuum cost delay in milliseconds, for autovacuum",
426 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
427 : : ShareUpdateExclusiveLock
428 : : },
429 : : -1, 0.0, 100.0
430 : : },
431 : : {
432 : : {
433 : : "autovacuum_vacuum_scale_factor",
434 : : "Number of tuple updates or deletes prior to vacuum as a fraction of reltuples",
435 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
436 : : ShareUpdateExclusiveLock
437 : : },
438 : : -1, 0.0, 100.0
439 : : },
440 : : {
441 : : {
442 : : "autovacuum_vacuum_insert_scale_factor",
443 : : "Number of tuple inserts prior to vacuum as a fraction of reltuples",
444 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
445 : : ShareUpdateExclusiveLock
446 : : },
447 : : -1, 0.0, 100.0
448 : : },
449 : : {
450 : : {
451 : : "autovacuum_analyze_scale_factor",
452 : : "Number of tuple inserts, updates or deletes prior to analyze as a fraction of reltuples",
453 : : RELOPT_KIND_HEAP,
454 : : ShareUpdateExclusiveLock
455 : : },
456 : : -1, 0.0, 100.0
457 : : },
458 : : {
459 : : {
460 : : "vacuum_max_eager_freeze_failure_rate",
461 : : "Fraction of pages in a relation vacuum can scan and fail to freeze before disabling eager scanning.",
462 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
463 : : ShareUpdateExclusiveLock
464 : : },
465 : : -1, 0.0, 1.0
466 : : },
467 : :
468 : : {
469 : : {
470 : : "seq_page_cost",
471 : : "Sets the planner's estimate of the cost of a sequentially fetched disk page.",
472 : : RELOPT_KIND_TABLESPACE,
473 : : ShareUpdateExclusiveLock
474 : : },
475 : : -1, 0.0, DBL_MAX
476 : : },
477 : : {
478 : : {
479 : : "random_page_cost",
480 : : "Sets the planner's estimate of the cost of a nonsequentially fetched disk page.",
481 : : RELOPT_KIND_TABLESPACE,
482 : : ShareUpdateExclusiveLock
483 : : },
484 : : -1, 0.0, DBL_MAX
485 : : },
486 : : {
487 : : {
488 : : "n_distinct",
489 : : "Sets the planner's estimate of the number of distinct values appearing in a column (excluding child relations).",
490 : : RELOPT_KIND_ATTRIBUTE,
491 : : ShareUpdateExclusiveLock
492 : : },
493 : : 0, -1.0, DBL_MAX
494 : : },
495 : : {
496 : : {
497 : : "n_distinct_inherited",
498 : : "Sets the planner's estimate of the number of distinct values appearing in a column (including child relations).",
499 : : RELOPT_KIND_ATTRIBUTE,
500 : : ShareUpdateExclusiveLock
501 : : },
502 : : 0, -1.0, DBL_MAX
503 : : },
504 : : {
505 : : {
506 : : "vacuum_cleanup_index_scale_factor",
507 : : "Deprecated B-Tree parameter.",
508 : : RELOPT_KIND_BTREE,
509 : : ShareUpdateExclusiveLock
510 : : },
511 : : -1, 0.0, 1e10
512 : : },
513 : : /* list terminator */
514 : : {{NULL}}
515 : : };
516 : :
517 : : /* values from StdRdOptIndexCleanup */
518 : : static relopt_enum_elt_def StdRdOptIndexCleanupValues[] =
519 : : {
520 : : /* no value for NOT_SET */
521 : : {"auto", STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO},
522 : : {"on", STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON},
523 : : {"off", STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF},
524 : : {"true", STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON},
525 : : {"false", STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF},
526 : : {"yes", STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON},
527 : : {"no", STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF},
528 : : {"1", STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON},
529 : : {"0", STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF},
530 : : {(const char *) NULL} /* list terminator */
531 : : };
532 : :
533 : : /* values from GistOptBufferingMode */
534 : : static relopt_enum_elt_def gistBufferingOptValues[] =
535 : : {
536 : : {"auto", GIST_OPTION_BUFFERING_AUTO},
537 : : {"on", GIST_OPTION_BUFFERING_ON},
538 : : {"off", GIST_OPTION_BUFFERING_OFF},
539 : : {(const char *) NULL} /* list terminator */
540 : : };
541 : :
542 : : /* values from ViewOptCheckOption */
543 : : static relopt_enum_elt_def viewCheckOptValues[] =
544 : : {
545 : : /* no value for NOT_SET */
546 : : {"local", VIEW_OPTION_CHECK_OPTION_LOCAL},
547 : : {"cascaded", VIEW_OPTION_CHECK_OPTION_CASCADED},
548 : : {(const char *) NULL} /* list terminator */
549 : : };
550 : :
551 : : static relopt_enum enumRelOpts[] =
552 : : {
553 : : {
554 : : {
555 : : "vacuum_index_cleanup",
556 : : "Controls index vacuuming and index cleanup",
557 : : RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
558 : : ShareUpdateExclusiveLock
559 : : },
560 : : StdRdOptIndexCleanupValues,
561 : : STDRD_OPTION_VACUUM_INDEX_CLEANUP_NOT_SET,
562 : : gettext_noop("Valid values are \"on\", \"off\", and \"auto\".")
563 : : },
564 : : {
565 : : {
566 : : "buffering",
567 : : "Enables buffering build for this GiST index",
568 : : RELOPT_KIND_GIST,
569 : : AccessExclusiveLock
570 : : },
571 : : gistBufferingOptValues,
572 : : GIST_OPTION_BUFFERING_AUTO,
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).",
579 : : RELOPT_KIND_VIEW,
580 : : AccessExclusiveLock
581 : : },
582 : : viewCheckOptValues,
583 : : VIEW_OPTION_CHECK_OPTION_NOT_SET,
584 : : gettext_noop("Valid values are \"local\" and \"cascaded\".")
585 : : },
586 : : /* list terminator */
587 : : {{NULL}}
588 : : };
589 : :
590 : : static relopt_string stringRelOpts[] =
591 : : {
592 : : /* list terminator */
593 : : {{NULL}}
594 : : };
595 : :
596 : : static relopt_gen **relOpts = NULL;
597 : : static uint32 last_assigned_kind = RELOPT_KIND_LAST_DEFAULT;
598 : :
599 : : static int num_custom_options = 0;
600 : : static relopt_gen **custom_options = NULL;
601 : : static bool need_initialization = true;
602 : :
603 : : static void initialize_reloptions(void);
604 : : static void parse_one_reloption(relopt_value *option, char *text_str,
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 : : #ifdef USE_ASSERT_CHECKING
617 : : /*
618 : : * Verify that every option a TOAST table accepts defaults to a value the user
619 : : * cannot set. That way, we can tell whether a reloption is set on the TOAST
620 : : * table or if we should pull the value from its main table.
621 : : */
622 : : static void
6 nathan@postgresql.or 623 :GNC 4239 : assert_toast_defaults_unsettable(void)
624 : : {
625 [ + + ]: 202342 : for (int i = 0; relOpts[i]; i++)
626 : : {
627 : 198103 : relopt_gen *gen = relOpts[i];
628 : :
629 [ + + ]: 198103 : if ((gen->kinds & RELOPT_KIND_TOAST) == 0)
630 : 121801 : continue;
631 : :
632 : : /*
633 : : * A TOAST table's value is filled in from its main table's at the
634 : : * same offset in the same struct, so the option must be settable on a
635 : : * heap too.
636 : : */
637 [ - + ]: 76302 : Assert((gen->kinds & RELOPT_KIND_HEAP) != 0);
638 : :
639 [ + + + + : 76302 : switch (gen->type)
- ]
640 : : {
641 : 8478 : case RELOPT_TYPE_TERNARY:
642 : :
643 : : /*
644 : : * Ternaries carry no default, and parse_one_reloption() can
645 : : * only produce true or false, so PG_TERNARY_UNSET is already
646 : : * beyond a user's reach.
647 : : */
648 : 8478 : break;
649 : :
650 : 46629 : case RELOPT_TYPE_INT:
651 : : {
652 : 46629 : relopt_int *optint = (relopt_int *) gen;
653 : :
654 [ - + - - ]: 46629 : Assert(optint->default_val < optint->min ||
655 : : optint->default_val > optint->max);
656 : 46629 : break;
657 : : }
658 : :
659 : 16956 : case RELOPT_TYPE_REAL:
660 : : {
661 : 16956 : relopt_real *optreal = (relopt_real *) gen;
662 : :
663 [ - + - - ]: 16956 : Assert(optreal->default_val < optreal->min ||
664 : : optreal->default_val > optreal->max);
665 : 16956 : break;
666 : : }
667 : :
668 : 4239 : case RELOPT_TYPE_ENUM:
669 : : {
670 : 4239 : relopt_enum *optenum = (relopt_enum *) gen;
671 : :
672 : 4239 : for (relopt_enum_elt_def *elt = optenum->members;
673 [ + + ]: 42390 : elt->string_val; elt++)
674 [ - + ]: 38151 : Assert(elt->symbol_val != optenum->default_val);
675 : 4239 : break;
676 : : }
677 : :
6 nathan@postgresql.or 678 :UNC 0 : default:
679 : : /* Neither bools nor strings can express "unset". */
680 : 0 : Assert(false);
681 : : }
682 : : }
6 nathan@postgresql.or 683 :GNC 4239 : }
684 : : #endif /* USE_ASSERT_CHECKING */
685 : :
686 : : /*
687 : : * initialize_reloptions
688 : : * initialization routine, must be called before parsing
689 : : *
690 : : * Initialize the relOpts array and fill each variable's type and name length.
691 : : */
692 : : static void
6443 alvherre@alvh.no-ip. 693 :CBC 4239 : initialize_reloptions(void)
694 : : {
695 : : int i;
696 : : int j;
697 : :
6013 tgl@sss.pgh.pa.us 698 : 4239 : j = 0;
6443 alvherre@alvh.no-ip. 699 [ + + ]: 29673 : for (i = 0; boolRelOpts[i].gen.name; i++)
700 : : {
4031 simon@2ndQuadrant.co 701 [ - + ]: 25434 : Assert(DoLockModesConflict(boolRelOpts[i].gen.lockmode,
702 : : boolRelOpts[i].gen.lockmode));
6443 alvherre@alvh.no-ip. 703 : 25434 : j++;
704 : : }
218 alvherre@kurilemu.de 705 [ + + ]: 12717 : for (i = 0; ternaryRelOpts[i].gen.name; i++)
706 : : {
707 [ - + ]: 8478 : Assert(DoLockModesConflict(ternaryRelOpts[i].gen.lockmode,
708 : : ternaryRelOpts[i].gen.lockmode));
709 : 8478 : j++;
710 : : }
711 : :
6443 alvherre@alvh.no-ip. 712 [ + + ]: 110214 : for (i = 0; intRelOpts[i].gen.name; i++)
713 : : {
4031 simon@2ndQuadrant.co 714 [ - + ]: 105975 : Assert(DoLockModesConflict(intRelOpts[i].gen.lockmode,
715 : : intRelOpts[i].gen.lockmode));
6443 alvherre@alvh.no-ip. 716 : 105975 : j++;
717 : : }
718 [ + + ]: 46629 : for (i = 0; realRelOpts[i].gen.name; i++)
719 : : {
4031 simon@2ndQuadrant.co 720 [ - + ]: 42390 : Assert(DoLockModesConflict(realRelOpts[i].gen.lockmode,
721 : : realRelOpts[i].gen.lockmode));
6443 alvherre@alvh.no-ip. 722 : 42390 : j++;
723 : : }
2528 724 [ + + ]: 16956 : for (i = 0; enumRelOpts[i].gen.name; i++)
725 : : {
726 [ - + ]: 12717 : Assert(DoLockModesConflict(enumRelOpts[i].gen.lockmode,
727 : : enumRelOpts[i].gen.lockmode));
728 : 12717 : j++;
729 : : }
6443 730 [ - + ]: 4239 : for (i = 0; stringRelOpts[i].gen.name; i++)
731 : : {
4031 simon@2ndQuadrant.co 732 [ # # ]:UBC 0 : Assert(DoLockModesConflict(stringRelOpts[i].gen.lockmode,
733 : : stringRelOpts[i].gen.lockmode));
6443 alvherre@alvh.no-ip. 734 : 0 : j++;
735 : : }
6443 alvherre@alvh.no-ip. 736 :CBC 4239 : j += num_custom_options;
737 : :
738 [ - + ]: 4239 : if (relOpts)
6443 alvherre@alvh.no-ip. 739 :UBC 0 : pfree(relOpts);
6443 alvherre@alvh.no-ip. 740 :CBC 8478 : relOpts = MemoryContextAlloc(TopMemoryContext,
741 : 4239 : (j + 1) * sizeof(relopt_gen *));
742 : :
743 : 4239 : j = 0;
744 [ + + ]: 29673 : for (i = 0; boolRelOpts[i].gen.name; i++)
745 : : {
746 : 25434 : relOpts[j] = &boolRelOpts[i].gen;
747 : 25434 : relOpts[j]->type = RELOPT_TYPE_BOOL;
748 : 25434 : relOpts[j]->namelen = strlen(relOpts[j]->name);
749 : 25434 : j++;
750 : : }
751 : :
218 alvherre@kurilemu.de 752 [ + + ]: 12717 : for (i = 0; ternaryRelOpts[i].gen.name; i++)
753 : : {
754 : 8478 : relOpts[j] = &ternaryRelOpts[i].gen;
755 : 8478 : relOpts[j]->type = RELOPT_TYPE_TERNARY;
756 : 8478 : relOpts[j]->namelen = strlen(relOpts[j]->name);
757 : 8478 : j++;
758 : : }
759 : :
6443 alvherre@alvh.no-ip. 760 [ + + ]: 110214 : for (i = 0; intRelOpts[i].gen.name; i++)
761 : : {
762 : 105975 : relOpts[j] = &intRelOpts[i].gen;
763 : 105975 : relOpts[j]->type = RELOPT_TYPE_INT;
764 : 105975 : relOpts[j]->namelen = strlen(relOpts[j]->name);
765 : 105975 : j++;
766 : : }
767 : :
768 [ + + ]: 46629 : for (i = 0; realRelOpts[i].gen.name; i++)
769 : : {
770 : 42390 : relOpts[j] = &realRelOpts[i].gen;
771 : 42390 : relOpts[j]->type = RELOPT_TYPE_REAL;
772 : 42390 : relOpts[j]->namelen = strlen(relOpts[j]->name);
773 : 42390 : j++;
774 : : }
775 : :
2528 776 [ + + ]: 16956 : for (i = 0; enumRelOpts[i].gen.name; i++)
777 : : {
778 : 12717 : relOpts[j] = &enumRelOpts[i].gen;
779 : 12717 : relOpts[j]->type = RELOPT_TYPE_ENUM;
780 : 12717 : relOpts[j]->namelen = strlen(relOpts[j]->name);
781 : 12717 : j++;
782 : : }
783 : :
6443 784 [ - + ]: 4239 : for (i = 0; stringRelOpts[i].gen.name; i++)
785 : : {
6443 alvherre@alvh.no-ip. 786 :UBC 0 : relOpts[j] = &stringRelOpts[i].gen;
787 : 0 : relOpts[j]->type = RELOPT_TYPE_STRING;
788 : 0 : relOpts[j]->namelen = strlen(relOpts[j]->name);
789 : 0 : j++;
790 : : }
791 : :
6443 alvherre@alvh.no-ip. 792 [ + + ]:CBC 7348 : for (i = 0; i < num_custom_options; i++)
793 : : {
794 : 3109 : relOpts[j] = custom_options[i];
795 : 3109 : j++;
796 : : }
797 : :
798 : : /* add a list terminator */
799 : 4239 : relOpts[j] = NULL;
800 : :
801 : : /* flag the work is complete */
6013 tgl@sss.pgh.pa.us 802 : 4239 : need_initialization = false;
803 : :
804 : : #ifdef USE_ASSERT_CHECKING
6 nathan@postgresql.or 805 :GNC 4239 : assert_toast_defaults_unsettable();
806 : : #endif
6443 alvherre@alvh.no-ip. 807 :CBC 4239 : }
808 : :
809 : : /*
810 : : * add_reloption_kind
811 : : * Create a new relopt_kind value, to be used in custom reloptions by
812 : : * user-defined AMs.
813 : : */
814 : : relopt_kind
815 : 96 : add_reloption_kind(void)
816 : : {
817 : : /* don't hand out the last bit so that the enum's behavior is portable */
818 [ - + ]: 96 : if (last_assigned_kind >= RELOPT_KIND_MAX)
6443 alvherre@alvh.no-ip. 819 [ # # ]:UBC 0 : ereport(ERROR,
820 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
821 : : errmsg("user-defined relation parameter types limit exceeded")));
6354 alvherre@alvh.no-ip. 822 :CBC 96 : last_assigned_kind <<= 1;
6304 tgl@sss.pgh.pa.us 823 : 96 : return (relopt_kind) last_assigned_kind;
824 : : }
825 : :
826 : : /*
827 : : * add_reloption
828 : : * Add an already-created custom reloption to the list, and recompute the
829 : : * main parser table.
830 : : */
831 : : static void
6443 alvherre@alvh.no-ip. 832 : 3142 : add_reloption(relopt_gen *newoption)
833 : : {
834 : : static int max_custom_options = 0;
835 : :
836 [ + + ]: 3142 : if (num_custom_options >= max_custom_options)
837 : : {
838 : : MemoryContext oldcxt;
839 : :
840 : 381 : oldcxt = MemoryContextSwitchTo(TopMemoryContext);
841 : :
842 [ + + ]: 381 : if (max_custom_options == 0)
843 : : {
844 : 96 : max_custom_options = 8;
10 michael@paquier.xyz 845 :GNC 96 : custom_options = palloc_array(relopt_gen *, max_custom_options);
846 : : }
847 : : else
848 : : {
6443 alvherre@alvh.no-ip. 849 :CBC 285 : max_custom_options *= 2;
10 michael@paquier.xyz 850 :GNC 285 : custom_options = repalloc_array(custom_options, relopt_gen *, max_custom_options);
851 : : }
6443 alvherre@alvh.no-ip. 852 :CBC 381 : MemoryContextSwitchTo(oldcxt);
853 : : }
854 : 3142 : custom_options[num_custom_options++] = newoption;
855 : :
856 : 3142 : need_initialization = true;
857 : 3142 : }
858 : :
859 : : /*
860 : : * init_local_reloptions
861 : : * Initialize local reloptions that will parsed into bytea structure of
862 : : * 'relopt_struct_size'.
863 : : */
864 : : void
1438 pg@bowt.ie 865 : 3682 : init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
866 : : {
867 : 3682 : relopts->options = NIL;
868 : 3682 : relopts->validators = NIL;
869 : 3682 : relopts->relopt_struct_size = relopt_struct_size;
2341 akorotkov@postgresql 870 : 3682 : }
871 : :
872 : : /*
873 : : * register_reloptions_validator
874 : : * Register custom validation callback that will be called at the end of
875 : : * build_local_reloptions().
876 : : */
877 : : void
1438 pg@bowt.ie 878 : 14 : register_reloptions_validator(local_relopts *relopts, relopts_validator validator)
879 : : {
880 : 14 : relopts->validators = lappend(relopts->validators, validator);
2341 akorotkov@postgresql 881 : 14 : }
882 : :
883 : : /*
884 : : * add_local_reloption
885 : : * Add an already-created custom reloption to the local list.
886 : : */
887 : : static void
888 : 2406 : add_local_reloption(local_relopts *relopts, relopt_gen *newoption, int offset)
889 : : {
260 michael@paquier.xyz 890 : 2406 : local_relopt *opt = palloc_object(local_relopt);
891 : :
2341 akorotkov@postgresql 892 [ - + ]: 2406 : Assert(offset < relopts->relopt_struct_size);
893 : :
894 : 2406 : opt->option = newoption;
895 : 2406 : opt->offset = offset;
896 : :
897 : 2406 : relopts->options = lappend(relopts->options, opt);
898 : 2406 : }
899 : :
900 : : /*
901 : : * allocate_reloption
902 : : * Allocate a new reloption and initialize the type-agnostic fields
903 : : * (for types other than string)
904 : : */
905 : : static relopt_gen *
150 nathan@postgresql.or 906 : 5548 : allocate_reloption(uint32 kinds, int type, const char *name, const char *desc,
907 : : LOCKMODE lockmode)
908 : : {
909 : : MemoryContext oldcxt;
910 : : size_t size;
911 : : relopt_gen *newoption;
912 : :
2341 akorotkov@postgresql 913 [ + + ]: 5548 : if (kinds != RELOPT_KIND_LOCAL)
914 : 3142 : oldcxt = MemoryContextSwitchTo(TopMemoryContext);
915 : : else
916 : 2406 : oldcxt = NULL;
917 : :
6443 alvherre@alvh.no-ip. 918 [ + + + + : 5548 : switch (type)
+ + - ]
919 : : {
920 : 1 : case RELOPT_TYPE_BOOL:
921 : 1 : size = sizeof(relopt_bool);
922 : 1 : break;
218 alvherre@kurilemu.de 923 : 1 : case RELOPT_TYPE_TERNARY:
924 : 1 : size = sizeof(relopt_ternary);
925 : 1 : break;
6443 alvherre@alvh.no-ip. 926 : 4412 : case RELOPT_TYPE_INT:
927 : 4412 : size = sizeof(relopt_int);
928 : 4412 : break;
929 : 1131 : case RELOPT_TYPE_REAL:
930 : 1131 : size = sizeof(relopt_real);
931 : 1131 : break;
2528 932 : 1 : case RELOPT_TYPE_ENUM:
933 : 1 : size = sizeof(relopt_enum);
934 : 1 : break;
5497 heikki.linnakangas@i 935 : 2 : case RELOPT_TYPE_STRING:
936 : 2 : size = sizeof(relopt_string);
937 : 2 : break;
6443 alvherre@alvh.no-ip. 938 :UBC 0 : default:
4043 tgl@sss.pgh.pa.us 939 [ # # ]: 0 : elog(ERROR, "unsupported reloption type %d", type);
940 : : return NULL; /* keep compiler quiet */
941 : : }
942 : :
6443 alvherre@alvh.no-ip. 943 :CBC 5548 : newoption = palloc(size);
944 : :
945 : 5548 : newoption->name = pstrdup(name);
946 [ + + ]: 5548 : if (desc)
947 : 5547 : newoption->desc = pstrdup(desc);
948 : : else
949 : 1 : newoption->desc = NULL;
6354 950 : 5548 : newoption->kinds = kinds;
6443 951 : 5548 : newoption->namelen = strlen(name);
952 : 5548 : newoption->type = type;
2528 michael@paquier.xyz 953 : 5548 : newoption->lockmode = lockmode;
954 : :
2341 akorotkov@postgresql 955 [ + + ]: 5548 : if (oldcxt != NULL)
956 : 3142 : MemoryContextSwitchTo(oldcxt);
957 : :
6443 alvherre@alvh.no-ip. 958 : 5548 : return newoption;
959 : : }
960 : :
961 : : /*
962 : : * init_bool_reloption
963 : : * Allocate and initialize a new boolean reloption
964 : : */
965 : : static relopt_bool *
150 nathan@postgresql.or 966 : 1 : init_bool_reloption(uint32 kinds, const char *name, const char *desc,
967 : : bool default_val, LOCKMODE lockmode)
968 : : {
969 : : relopt_bool *newoption;
970 : :
6354 alvherre@alvh.no-ip. 971 : 1 : newoption = (relopt_bool *) allocate_reloption(kinds, RELOPT_TYPE_BOOL,
972 : : name, desc, lockmode);
6443 973 : 1 : newoption->default_val = default_val;
974 : :
2341 akorotkov@postgresql 975 : 1 : return newoption;
976 : : }
977 : :
978 : : /*
979 : : * add_bool_reloption
980 : : * Add a new boolean reloption
981 : : */
982 : : void
150 nathan@postgresql.or 983 : 1 : add_bool_reloption(uint32 kinds, const char *name, const char *desc,
984 : : bool default_val, LOCKMODE lockmode)
985 : : {
2341 akorotkov@postgresql 986 : 1 : relopt_bool *newoption = init_bool_reloption(kinds, name, desc,
987 : : default_val, lockmode);
988 : :
6443 alvherre@alvh.no-ip. 989 : 1 : add_reloption((relopt_gen *) newoption);
990 : 1 : }
991 : :
992 : : /*
993 : : * add_local_bool_reloption
994 : : * Add a new boolean local reloption
995 : : *
996 : : * 'offset' is offset of bool-typed field.
997 : : */
998 : : void
2341 akorotkov@postgresql 999 :UBC 0 : add_local_bool_reloption(local_relopts *relopts, const char *name,
1000 : : const char *desc, bool default_val, int offset)
1001 : : {
1002 : 0 : relopt_bool *newoption = init_bool_reloption(RELOPT_KIND_LOCAL,
1003 : : name, desc,
1004 : : default_val, 0);
1005 : :
1006 : 0 : add_local_reloption(relopts, (relopt_gen *) newoption, offset);
1007 : 0 : }
1008 : :
1009 : : /*
1010 : : * init_ternary_reloption
1011 : : * Allocate and initialize a new ternary reloption
1012 : : */
1013 : : static relopt_ternary *
150 nathan@postgresql.or 1014 :CBC 1 : init_ternary_reloption(uint32 kinds, const char *name, const char *desc,
1015 : : LOCKMODE lockmode)
1016 : : {
1017 : : relopt_ternary *newoption;
1018 : :
1019 : : newoption = (relopt_ternary *)
218 alvherre@kurilemu.de 1020 : 1 : allocate_reloption(kinds, RELOPT_TYPE_TERNARY, name, desc, lockmode);
1021 : :
1022 : 1 : return newoption;
1023 : : }
1024 : :
1025 : : /*
1026 : : * add_ternary_reloption
1027 : : * Add a new ternary reloption
1028 : : */
1029 : : void
150 nathan@postgresql.or 1030 : 1 : add_ternary_reloption(uint32 kinds, const char *name, const char *desc,
1031 : : LOCKMODE lockmode)
1032 : : {
1033 : : relopt_ternary *newoption;
1034 : :
1035 : : newoption =
218 alvherre@kurilemu.de 1036 : 1 : init_ternary_reloption(kinds, name, desc, lockmode);
1037 : :
1038 : 1 : add_reloption((relopt_gen *) newoption);
1039 : 1 : }
1040 : :
1041 : : /*
1042 : : * add_local_ternary_reloption
1043 : : * Add a new ternary local reloption
1044 : : *
1045 : : * 'offset' is offset of ternary-typed field.
1046 : : */
1047 : : void
218 alvherre@kurilemu.de 1048 :UBC 0 : add_local_ternary_reloption(local_relopts *relopts, const char *name,
1049 : : const char *desc, int offset)
1050 : : {
1051 : : relopt_ternary *newoption;
1052 : :
1053 : : newoption =
1054 : 0 : init_ternary_reloption(RELOPT_KIND_LOCAL, name, desc, 0);
1055 : :
1056 : 0 : add_local_reloption(relopts, (relopt_gen *) newoption, offset);
1057 : 0 : }
1058 : :
1059 : : /*
1060 : : * init_real_reloption
1061 : : * Allocate and initialize a new integer reloption
1062 : : */
1063 : : static relopt_int *
150 nathan@postgresql.or 1064 :CBC 4412 : init_int_reloption(uint32 kinds, const char *name, const char *desc,
1065 : : int default_val, int min_val, int max_val,
1066 : : LOCKMODE lockmode)
1067 : : {
1068 : : relopt_int *newoption;
1069 : :
6354 alvherre@alvh.no-ip. 1070 : 4412 : newoption = (relopt_int *) allocate_reloption(kinds, RELOPT_TYPE_INT,
1071 : : name, desc, lockmode);
6443 1072 : 4412 : newoption->default_val = default_val;
1073 : 4412 : newoption->min = min_val;
1074 : 4412 : newoption->max = max_val;
1075 : :
2341 akorotkov@postgresql 1076 : 4412 : return newoption;
1077 : : }
1078 : :
1079 : : /*
1080 : : * add_int_reloption
1081 : : * Add a new integer reloption
1082 : : */
1083 : : void
150 nathan@postgresql.or 1084 : 3136 : add_int_reloption(uint32 kinds, const char *name, const char *desc, int default_val,
1085 : : int min_val, int max_val, LOCKMODE lockmode)
1086 : : {
2341 akorotkov@postgresql 1087 : 3136 : relopt_int *newoption = init_int_reloption(kinds, name, desc,
1088 : : default_val, min_val,
1089 : : max_val, lockmode);
1090 : :
6443 alvherre@alvh.no-ip. 1091 : 3136 : add_reloption((relopt_gen *) newoption);
1092 : 3136 : }
1093 : :
1094 : : /*
1095 : : * add_local_int_reloption
1096 : : * Add a new local integer reloption
1097 : : *
1098 : : * 'offset' is offset of int-typed field.
1099 : : */
1100 : : void
2341 akorotkov@postgresql 1101 : 1276 : add_local_int_reloption(local_relopts *relopts, const char *name,
1102 : : const char *desc, int default_val, int min_val,
1103 : : int max_val, int offset)
1104 : : {
1105 : 1276 : relopt_int *newoption = init_int_reloption(RELOPT_KIND_LOCAL,
1106 : : name, desc, default_val,
1107 : : min_val, max_val, 0);
1108 : :
1109 : 1276 : add_local_reloption(relopts, (relopt_gen *) newoption, offset);
1110 : 1276 : }
1111 : :
1112 : : /*
1113 : : * init_real_reloption
1114 : : * Allocate and initialize a new real reloption
1115 : : */
1116 : : static relopt_real *
150 nathan@postgresql.or 1117 : 1131 : init_real_reloption(uint32 kinds, const char *name, const char *desc,
1118 : : double default_val, double min_val, double max_val,
1119 : : LOCKMODE lockmode)
1120 : : {
1121 : : relopt_real *newoption;
1122 : :
6354 alvherre@alvh.no-ip. 1123 : 1131 : newoption = (relopt_real *) allocate_reloption(kinds, RELOPT_TYPE_REAL,
1124 : : name, desc, lockmode);
6443 1125 : 1131 : newoption->default_val = default_val;
1126 : 1131 : newoption->min = min_val;
1127 : 1131 : newoption->max = max_val;
1128 : :
2341 akorotkov@postgresql 1129 : 1131 : return newoption;
1130 : : }
1131 : :
1132 : : /*
1133 : : * add_real_reloption
1134 : : * Add a new float reloption
1135 : : */
1136 : : void
150 nathan@postgresql.or 1137 : 1 : add_real_reloption(uint32 kinds, const char *name, const char *desc,
1138 : : double default_val, double min_val, double max_val,
1139 : : LOCKMODE lockmode)
1140 : : {
2341 akorotkov@postgresql 1141 : 1 : relopt_real *newoption = init_real_reloption(kinds, name, desc,
1142 : : default_val, min_val,
1143 : : max_val, lockmode);
1144 : :
6443 alvherre@alvh.no-ip. 1145 : 1 : add_reloption((relopt_gen *) newoption);
1146 : 1 : }
1147 : :
1148 : : /*
1149 : : * add_local_real_reloption
1150 : : * Add a new local float reloption
1151 : : *
1152 : : * 'offset' is offset of double-typed field.
1153 : : */
1154 : : void
2341 akorotkov@postgresql 1155 : 1130 : add_local_real_reloption(local_relopts *relopts, const char *name,
1156 : : const char *desc, double default_val,
1157 : : double min_val, double max_val, int offset)
1158 : : {
1159 : 1130 : relopt_real *newoption = init_real_reloption(RELOPT_KIND_LOCAL,
1160 : : name, desc,
1161 : : default_val, min_val,
1162 : : max_val, 0);
1163 : :
1164 : 1130 : add_local_reloption(relopts, (relopt_gen *) newoption, offset);
1165 : 1130 : }
1166 : :
1167 : : /*
1168 : : * init_enum_reloption
1169 : : * Allocate and initialize a new enum reloption
1170 : : */
1171 : : static relopt_enum *
150 nathan@postgresql.or 1172 : 1 : init_enum_reloption(uint32 kinds, const char *name, const char *desc,
1173 : : relopt_enum_elt_def *members, int default_val,
1174 : : const char *detailmsg, LOCKMODE lockmode)
1175 : : {
1176 : : relopt_enum *newoption;
1177 : :
2341 akorotkov@postgresql 1178 : 1 : newoption = (relopt_enum *) allocate_reloption(kinds, RELOPT_TYPE_ENUM,
1179 : : name, desc, lockmode);
1180 : 1 : newoption->members = members;
1181 : 1 : newoption->default_val = default_val;
1182 : 1 : newoption->detailmsg = detailmsg;
1183 : :
1184 : 1 : return newoption;
1185 : : }
1186 : :
1187 : :
1188 : : /*
1189 : : * add_enum_reloption
1190 : : * Add a new enum reloption
1191 : : *
1192 : : * The members array must have a terminating NULL entry.
1193 : : *
1194 : : * The detailmsg is shown when unsupported values are passed, and has this
1195 : : * form: "Valid values are \"foo\", \"bar\", and \"bar\"."
1196 : : *
1197 : : * The members array and detailmsg are not copied -- caller must ensure that
1198 : : * they are valid throughout the life of the process.
1199 : : */
1200 : : void
150 nathan@postgresql.or 1201 : 1 : add_enum_reloption(uint32 kinds, const char *name, const char *desc,
1202 : : relopt_enum_elt_def *members, int default_val,
1203 : : const char *detailmsg, LOCKMODE lockmode)
1204 : : {
2341 akorotkov@postgresql 1205 : 1 : relopt_enum *newoption = init_enum_reloption(kinds, name, desc,
1206 : : members, default_val,
1207 : : detailmsg, lockmode);
1208 : :
2528 alvherre@alvh.no-ip. 1209 : 1 : add_reloption((relopt_gen *) newoption);
1210 : 1 : }
1211 : :
1212 : : /*
1213 : : * add_local_enum_reloption
1214 : : * Add a new local enum reloption
1215 : : *
1216 : : * 'offset' is offset of int-typed field.
1217 : : */
1218 : : void
2341 akorotkov@postgresql 1219 :UBC 0 : add_local_enum_reloption(local_relopts *relopts, const char *name,
1220 : : const char *desc, relopt_enum_elt_def *members,
1221 : : int default_val, const char *detailmsg, int offset)
1222 : : {
1223 : 0 : relopt_enum *newoption = init_enum_reloption(RELOPT_KIND_LOCAL,
1224 : : name, desc,
1225 : : members, default_val,
1226 : : detailmsg, 0);
1227 : :
1228 : 0 : add_local_reloption(relopts, (relopt_gen *) newoption, offset);
1229 : 0 : }
1230 : :
1231 : : /*
1232 : : * init_string_reloption
1233 : : * Allocate and initialize a new string reloption
1234 : : */
1235 : : static relopt_string *
150 nathan@postgresql.or 1236 :CBC 2 : init_string_reloption(uint32 kinds, const char *name, const char *desc,
1237 : : const char *default_val,
1238 : : validate_string_relopt validator,
1239 : : fill_string_relopt filler,
1240 : : LOCKMODE lockmode)
1241 : : {
1242 : : relopt_string *newoption;
1243 : :
1244 : : /* make sure the validator/default combination is sane */
5497 heikki.linnakangas@i 1245 [ + - ]: 2 : if (validator)
1246 : 2 : (validator) (default_val);
1247 : :
1248 : 2 : newoption = (relopt_string *) allocate_reloption(kinds, RELOPT_TYPE_STRING,
1249 : : name, desc, lockmode);
6440 alvherre@alvh.no-ip. 1250 : 2 : newoption->validate_cb = validator;
2341 akorotkov@postgresql 1251 : 2 : newoption->fill_cb = filler;
6443 alvherre@alvh.no-ip. 1252 [ + + ]: 2 : if (default_val)
1253 : : {
2341 akorotkov@postgresql 1254 [ - + ]: 1 : if (kinds == RELOPT_KIND_LOCAL)
2341 akorotkov@postgresql 1255 :UBC 0 : newoption->default_val = strdup(default_val);
1256 : : else
2341 akorotkov@postgresql 1257 :CBC 1 : newoption->default_val = MemoryContextStrdup(TopMemoryContext, default_val);
5497 heikki.linnakangas@i 1258 : 1 : newoption->default_len = strlen(default_val);
6443 alvherre@alvh.no-ip. 1259 : 1 : newoption->default_isnull = false;
1260 : : }
1261 : : else
1262 : : {
5497 heikki.linnakangas@i 1263 : 1 : newoption->default_val = "";
6443 alvherre@alvh.no-ip. 1264 : 1 : newoption->default_len = 0;
1265 : 1 : newoption->default_isnull = true;
1266 : : }
1267 : :
2341 akorotkov@postgresql 1268 : 2 : return newoption;
1269 : : }
1270 : :
1271 : : /*
1272 : : * add_string_reloption
1273 : : * Add a new string reloption
1274 : : *
1275 : : * "validator" is an optional function pointer that can be used to test the
1276 : : * validity of the values. It must elog(ERROR) when the argument string is
1277 : : * not acceptable for the variable. Note that the default value must pass
1278 : : * the validation.
1279 : : */
1280 : : void
150 nathan@postgresql.or 1281 : 2 : add_string_reloption(uint32 kinds, const char *name, const char *desc,
1282 : : const char *default_val, validate_string_relopt validator,
1283 : : LOCKMODE lockmode)
1284 : : {
2341 akorotkov@postgresql 1285 : 2 : relopt_string *newoption = init_string_reloption(kinds, name, desc,
1286 : : default_val,
1287 : : validator, NULL,
1288 : : lockmode);
1289 : :
6443 alvherre@alvh.no-ip. 1290 : 2 : add_reloption((relopt_gen *) newoption);
1291 : 2 : }
1292 : :
1293 : : /*
1294 : : * add_local_string_reloption
1295 : : * Add a new local string reloption
1296 : : *
1297 : : * 'offset' is offset of int-typed field that will store offset of string value
1298 : : * in the resulting bytea structure.
1299 : : */
1300 : : void
2341 akorotkov@postgresql 1301 :UBC 0 : add_local_string_reloption(local_relopts *relopts, const char *name,
1302 : : const char *desc, const char *default_val,
1303 : : validate_string_relopt validator,
1304 : : fill_string_relopt filler, int offset)
1305 : : {
1306 : 0 : relopt_string *newoption = init_string_reloption(RELOPT_KIND_LOCAL,
1307 : : name, desc,
1308 : : default_val,
1309 : : validator, filler,
1310 : : 0);
1311 : :
1312 : 0 : add_local_reloption(relopts, (relopt_gen *) newoption, offset);
1313 : 0 : }
1314 : :
1315 : : /*
1316 : : * Transform a relation options list (list of DefElem) into the text array
1317 : : * format that is kept in pg_class.reloptions, including only those options
1318 : : * that are in the passed namespace. The output values do not include the
1319 : : * namespace.
1320 : : *
1321 : : * This is used for three cases: CREATE TABLE/INDEX, ALTER TABLE SET, and
1322 : : * ALTER TABLE RESET. In the ALTER cases, oldOptions is the existing
1323 : : * reloptions value (possibly NULL), and we replace or remove entries
1324 : : * as needed.
1325 : : *
1326 : : * If acceptOidsOff is true, then we allow oids = false, but throw error when
1327 : : * on. This is solely needed for backwards compatibility.
1328 : : *
1329 : : * Note that this is not responsible for determining whether the options
1330 : : * are valid, but it does check that namespaces for all the options given are
1331 : : * listed in validnsps. The NULL namespace is always valid and need not be
1332 : : * explicitly listed. Passing a NULL pointer means that only the NULL
1333 : : * namespace is valid.
1334 : : *
1335 : : * Both oldOptions and the result are text arrays (or NULL for "default"),
1336 : : * but we declare them as Datums to avoid including array.h in reloptions.h.
1337 : : */
1338 : : Datum
386 nathan@postgresql.or 1339 :CBC 87245 : transformRelOptions(Datum oldOptions, List *defList, const char *nameSpace,
1340 : : const char *const validnsps[], bool acceptOidsOff, bool isReset)
1341 : : {
1342 : : Datum result;
1343 : : ArrayBuildState *astate;
1344 : : ListCell *cell;
1345 : :
1346 : : /* no change if empty list */
7360 tgl@sss.pgh.pa.us 1347 [ + + ]: 87245 : if (defList == NIL)
1348 : 84662 : return oldOptions;
1349 : :
1350 : : /* We build new array using accumArrayResult */
1351 : 2583 : astate = NULL;
1352 : :
1353 : : /* Copy any oldOptions that aren't to be replaced */
337 peter@eisentraut.org 1354 [ + + ]: 2583 : if (DatumGetPointer(oldOptions) != NULL)
1355 : : {
7267 bruce@momjian.us 1356 : 229 : ArrayType *array = DatumGetArrayTypeP(oldOptions);
1357 : : Datum *oldoptions;
1358 : : int noldoptions;
1359 : : int i;
1360 : :
1518 peter@eisentraut.org 1361 : 229 : deconstruct_array_builtin(array, TEXTOID, &oldoptions, NULL, &noldoptions);
1362 : :
7360 tgl@sss.pgh.pa.us 1363 [ + + ]: 598 : for (i = 0; i < noldoptions; i++)
1364 : : {
387 peter@eisentraut.org 1365 : 369 : char *text_str = VARDATA(DatumGetPointer(oldoptions[i]));
1366 : 369 : int text_len = VARSIZE(DatumGetPointer(oldoptions[i])) - VARHDRSZ;
1367 : :
1368 : : /* Search for a match in defList */
7360 tgl@sss.pgh.pa.us 1369 [ + - + + : 556 : foreach(cell, defList)
+ + ]
1370 : : {
6286 bruce@momjian.us 1371 : 403 : DefElem *def = (DefElem *) lfirst(cell);
1372 : : int kw_len;
1373 : :
1374 : : /* ignore if not in the same namespace */
386 nathan@postgresql.or 1375 [ + + ]: 403 : if (nameSpace == NULL)
1376 : : {
6354 tgl@sss.pgh.pa.us 1377 [ - + ]: 371 : if (def->defnamespace != NULL)
6415 alvherre@alvh.no-ip. 1378 :UBC 0 : continue;
1379 : : }
6354 tgl@sss.pgh.pa.us 1380 [ + + ]:CBC 32 : else if (def->defnamespace == NULL)
6415 alvherre@alvh.no-ip. 1381 : 20 : continue;
386 nathan@postgresql.or 1382 [ - + ]: 12 : else if (strcmp(def->defnamespace, nameSpace) != 0)
6415 alvherre@alvh.no-ip. 1383 :UBC 0 : continue;
1384 : :
6354 tgl@sss.pgh.pa.us 1385 :CBC 383 : kw_len = strlen(def->defname);
7360 1386 [ + + + + ]: 383 : if (text_len > kw_len && text_str[kw_len] == '=' &&
3135 1387 [ + + ]: 228 : strncmp(text_str, def->defname, kw_len) == 0)
7360 1388 : 216 : break;
1389 : : }
1390 [ + + ]: 369 : if (!cell)
1391 : : {
1392 : : /* No match, so keep old option */
1393 : 153 : astate = accumArrayResult(astate, oldoptions[i],
1394 : : false, TEXTOID,
1395 : : CurrentMemoryContext);
1396 : : }
1397 : : }
1398 : : }
1399 : :
1400 : : /*
1401 : : * If CREATE/SET, add new options to array; if RESET, just check that the
1402 : : * user didn't say RESET (option=val). (Must do this because the grammar
1403 : : * doesn't enforce it.)
1404 : : */
1405 [ + - + + : 5429 : foreach(cell, defList)
+ + ]
1406 : : {
6286 bruce@momjian.us 1407 : 2870 : DefElem *def = (DefElem *) lfirst(cell);
1408 : :
7360 tgl@sss.pgh.pa.us 1409 [ + + ]: 2870 : if (isReset)
1410 : : {
1411 [ + + ]: 172 : if (def->arg != NULL)
1412 [ + - ]: 8 : ereport(ERROR,
1413 : : (errcode(ERRCODE_SYNTAX_ERROR),
1414 : : errmsg("RESET must not include values for parameters")));
1415 : : }
1416 : : else
1417 : : {
1418 : : const char *name;
1419 : : const char *value;
1420 : : text *t;
1421 : : Size len;
1422 : :
1423 : : /*
1424 : : * Error out if the namespace is not valid. A NULL namespace is
1425 : : * always valid.
1426 : : */
6354 1427 [ + + ]: 2698 : if (def->defnamespace != NULL)
1428 : : {
6286 bruce@momjian.us 1429 : 88 : bool valid = false;
1430 : : int i;
1431 : :
6415 alvherre@alvh.no-ip. 1432 [ + + ]: 88 : if (validnsps)
1433 : : {
1434 [ + + ]: 88 : for (i = 0; validnsps[i]; i++)
1435 : : {
3135 tgl@sss.pgh.pa.us 1436 [ + + ]: 84 : if (strcmp(def->defnamespace, validnsps[i]) == 0)
1437 : : {
6415 alvherre@alvh.no-ip. 1438 : 80 : valid = true;
1439 : 80 : break;
1440 : : }
1441 : : }
1442 : : }
1443 : :
1444 [ + + ]: 88 : if (!valid)
1445 [ + - ]: 8 : ereport(ERROR,
1446 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1447 : : errmsg("unrecognized parameter namespace \"%s\"",
1448 : : def->defnamespace)));
1449 : : }
1450 : :
1451 : : /* ignore if not in the same namespace */
386 nathan@postgresql.or 1452 [ + + ]: 2690 : if (nameSpace == NULL)
1453 : : {
6354 tgl@sss.pgh.pa.us 1454 [ + + ]: 1940 : if (def->defnamespace != NULL)
6415 alvherre@alvh.no-ip. 1455 : 40 : continue;
1456 : : }
6354 tgl@sss.pgh.pa.us 1457 [ + + ]: 750 : else if (def->defnamespace == NULL)
6415 alvherre@alvh.no-ip. 1458 : 710 : continue;
386 nathan@postgresql.or 1459 [ - + ]: 40 : else if (strcmp(def->defnamespace, nameSpace) != 0)
7360 tgl@sss.pgh.pa.us 1460 :UBC 0 : continue;
1461 : :
1462 : : /*
1463 : : * Flatten the DefElem into a text string like "name=arg". If we
1464 : : * have just "name", assume "name=true" is meant. Note: the
1465 : : * namespace is not output.
1466 : : */
451 tgl@sss.pgh.pa.us 1467 :CBC 1940 : name = def->defname;
7360 1468 [ + + ]: 1940 : if (def->arg != NULL)
6354 1469 : 1700 : value = defGetString(def);
1470 : : else
7360 1471 : 240 : value = "true";
1472 : :
1473 : : /* Insist that name not contain "=", else "a=b=c" is ambiguous */
451 1474 [ - + ]: 1940 : if (strchr(name, '=') != NULL)
451 tgl@sss.pgh.pa.us 1475 [ # # ]:UBC 0 : ereport(ERROR,
1476 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1477 : : errmsg("invalid option name \"%s\": must not contain \"=\"",
1478 : : name)));
1479 : :
1480 : : /*
1481 : : * This is not a great place for this test, but there's no other
1482 : : * convenient place to filter the option out. As WITH (oids =
1483 : : * false) will be removed someday, this seems like an acceptable
1484 : : * amount of ugly.
1485 : : */
2837 andres@anarazel.de 1486 [ + + + + ]:CBC 1940 : if (acceptOidsOff && def->defnamespace == NULL &&
451 tgl@sss.pgh.pa.us 1487 [ + + ]: 1029 : strcmp(name, "oids") == 0)
1488 : : {
2837 andres@anarazel.de 1489 [ + + ]: 12 : if (defGetBoolean(def))
1490 [ + - ]: 8 : ereport(ERROR,
1491 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1492 : : errmsg("tables declared WITH OIDS are not supported")));
1493 : : /* skip over option, reloptions machinery doesn't know it */
1494 : 4 : continue;
1495 : : }
1496 : :
451 tgl@sss.pgh.pa.us 1497 : 1928 : len = VARHDRSZ + strlen(name) + 1 + strlen(value);
1498 : : /* +1 leaves room for sprintf's trailing null */
7360 1499 : 1928 : t = (text *) palloc(len + 1);
7121 1500 : 1928 : SET_VARSIZE(t, len);
451 1501 : 1928 : sprintf(VARDATA(t), "%s=%s", name, value);
1502 : :
7360 1503 : 1928 : astate = accumArrayResult(astate, PointerGetDatum(t),
1504 : : false, TEXTOID,
1505 : : CurrentMemoryContext);
1506 : : }
1507 : : }
1508 : :
1509 [ + + ]: 2559 : if (astate)
1510 : 1808 : result = makeArrayResult(astate, CurrentMemoryContext);
1511 : : else
1512 : 751 : result = (Datum) 0;
1513 : :
1514 : 2559 : return result;
1515 : : }
1516 : :
1517 : :
1518 : : /*
1519 : : * Convert the text-array format of reloptions into a List of DefElem.
1520 : : * This is the inverse of transformRelOptions().
1521 : : */
1522 : : List *
6844 1523 : 17450 : untransformRelOptions(Datum options)
1524 : : {
1525 : 17450 : List *result = NIL;
1526 : : ArrayType *array;
1527 : : Datum *optiondatums;
1528 : : int noptions;
1529 : : int i;
1530 : :
1531 : : /* Nothing to do if no options */
337 peter@eisentraut.org 1532 [ + + ]: 17450 : if (DatumGetPointer(options) == NULL)
6844 tgl@sss.pgh.pa.us 1533 : 3043 : return result;
1534 : :
1535 : 14407 : array = DatumGetArrayTypeP(options);
1536 : :
1518 peter@eisentraut.org 1537 : 14407 : deconstruct_array_builtin(array, TEXTOID, &optiondatums, NULL, &noptions);
1538 : :
6844 tgl@sss.pgh.pa.us 1539 [ + + ]: 43352 : for (i = 0; i < noptions; i++)
1540 : : {
1541 : : char *s;
1542 : : char *p;
1543 : 28945 : Node *val = NULL;
1544 : :
6729 1545 : 28945 : s = TextDatumGetCString(optiondatums[i]);
6844 1546 : 28945 : p = strchr(s, '=');
1547 [ + - ]: 28945 : if (p)
1548 : : {
1549 : 28945 : *p++ = '\0';
1444 alvherre@alvh.no-ip. 1550 : 28945 : val = (Node *) makeString(p);
1551 : : }
1552 : 28945 : result = lappend(result, makeDefElem(s, val, -1));
1553 : : }
1554 : :
6844 tgl@sss.pgh.pa.us 1555 : 14407 : return result;
1556 : : }
1557 : :
1558 : : /*
1559 : : * Extract and parse reloptions from a pg_class tuple.
1560 : : *
1561 : : * This is a low-level routine, expected to be used by relcache code and
1562 : : * callers that do not have a table's relcache entry (e.g. autovacuum). For
1563 : : * other uses, consider grabbing the rd_options pointer from the relcache entry
1564 : : * instead.
1565 : : *
1566 : : * tupdesc is pg_class' tuple descriptor. amoptions is a pointer to the index
1567 : : * AM's options parser function in the case of a tuple corresponding to an
1568 : : * index, or NULL otherwise.
1569 : : */
1570 : : bytea *
3875 1571 : 1005358 : extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
1572 : : amoptions_function amoptions)
1573 : : {
1574 : : bytea *options;
1575 : : bool isnull;
1576 : : Datum datum;
1577 : : Form_pg_class classForm;
1578 : :
6422 alvherre@alvh.no-ip. 1579 : 1005358 : datum = fastgetattr(tuple,
1580 : : Anum_pg_class_reloptions,
1581 : : tupdesc,
1582 : : &isnull);
1583 [ + + ]: 1005358 : if (isnull)
1584 : 993250 : return NULL;
1585 : :
1586 : 12108 : classForm = (Form_pg_class) GETSTRUCT(tuple);
1587 : :
1588 : : /* Parse into appropriate format; don't error out here */
1589 [ + - + + : 12108 : switch (classForm->relkind)
- - ]
1590 : : {
1591 : 9014 : case RELKIND_RELATION:
1592 : : case RELKIND_TOASTVALUE:
1593 : : case RELKIND_MATVIEW:
868 akorotkov@postgresql 1594 : 9014 : options = heap_reloptions(classForm->relkind, datum, false);
6422 alvherre@alvh.no-ip. 1595 : 9014 : break;
2478 michael@paquier.xyz 1596 :UBC 0 : case RELKIND_PARTITIONED_TABLE:
1597 : 0 : options = partitioned_table_reloptions(datum, false);
1598 : 0 : break;
4427 alvherre@alvh.no-ip. 1599 :CBC 1501 : case RELKIND_VIEW:
1600 : 1501 : options = view_reloptions(datum, false);
1601 : 1501 : break;
6422 1602 : 1593 : case RELKIND_INDEX:
1603 : : case RELKIND_PARTITIONED_INDEX:
1604 : 1593 : options = index_reloptions(amoptions, datum, false);
1605 : 1593 : break;
5717 rhaas@postgresql.org 1606 :UBC 0 : case RELKIND_FOREIGN_TABLE:
1607 : 0 : options = NULL;
1608 : 0 : break;
6422 alvherre@alvh.no-ip. 1609 : 0 : default:
1610 : 0 : Assert(false); /* can't get here */
1611 : : options = NULL; /* keep compiler quiet */
1612 : : break;
1613 : : }
1614 : :
6422 alvherre@alvh.no-ip. 1615 :CBC 12108 : return options;
1616 : : }
1617 : :
1618 : : static void
2341 akorotkov@postgresql 1619 : 14107 : parseRelOptionsInternal(Datum options, bool validate,
1620 : : relopt_value *reloptions, int numoptions)
1621 : : {
1622 : 14107 : ArrayType *array = DatumGetArrayTypeP(options);
1623 : : Datum *optiondatums;
1624 : : int noptions;
1625 : : int i;
1626 : :
1518 peter@eisentraut.org 1627 : 14107 : deconstruct_array_builtin(array, TEXTOID, &optiondatums, NULL, &noptions);
1628 : :
2341 akorotkov@postgresql 1629 [ + + ]: 29623 : for (i = 0; i < noptions; i++)
1630 : : {
387 peter@eisentraut.org 1631 : 15725 : char *text_str = VARDATA(DatumGetPointer(optiondatums[i]));
1632 : 15725 : int text_len = VARSIZE(DatumGetPointer(optiondatums[i])) - VARHDRSZ;
1633 : : int j;
1634 : :
1635 : : /* Search for a match in reloptions */
2341 akorotkov@postgresql 1636 [ + + ]: 84092 : for (j = 0; j < numoptions; j++)
1637 : : {
1638 : 84036 : int kw_len = reloptions[j].gen->namelen;
1639 : :
1640 [ + + + + ]: 84036 : if (text_len > kw_len && text_str[kw_len] == '=' &&
1641 [ + + ]: 22536 : strncmp(text_str, reloptions[j].gen->name, kw_len) == 0)
1642 : : {
1643 : 15669 : parse_one_reloption(&reloptions[j], text_str, text_len,
1644 : : validate);
1645 : 15504 : break;
1646 : : }
1647 : : }
1648 : :
1649 [ + + + + ]: 15560 : if (j >= numoptions && validate)
1650 : : {
1651 : : char *s;
1652 : : char *p;
1653 : :
1654 : 44 : s = TextDatumGetCString(optiondatums[i]);
1655 : 44 : p = strchr(s, '=');
1656 [ + - ]: 44 : if (p)
1657 : 44 : *p = '\0';
1658 [ + - ]: 44 : ereport(ERROR,
1659 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1660 : : errmsg("unrecognized parameter \"%s\"", s)));
1661 : : }
1662 : : }
1663 : :
1664 : : /* It's worth avoiding memory leaks in this function */
1665 : 13898 : pfree(optiondatums);
1666 : :
1667 [ + + ]: 13898 : if (((void *) array) != DatumGetPointer(options))
1668 : 12284 : pfree(array);
1669 : 13898 : }
1670 : :
1671 : : /*
1672 : : * Interpret reloptions that are given in text-array format.
1673 : : *
1674 : : * options is a reloption text array as constructed by transformRelOptions.
1675 : : * kind specifies the family of options to be processed.
1676 : : *
1677 : : * The return value is a relopt_value * array on which the options actually
1678 : : * set in the options array are marked with isset=true. The length of this
1679 : : * array is returned in *numrelopts. Options not set are also present in the
1680 : : * array; this is so that the caller can easily locate the default values.
1681 : : *
1682 : : * If there are no options of the given kind, numrelopts is set to 0 and NULL
1683 : : * is returned (unless options are illegally supplied despite none being
1684 : : * defined, in which case an error occurs).
1685 : : *
1686 : : * Note: values of type int, bool and real are allocated as part of the
1687 : : * returned array. Values of type string are allocated separately and must
1688 : : * be freed by the caller.
1689 : : */
1690 : : static relopt_value *
6443 alvherre@alvh.no-ip. 1691 : 72776 : parseRelOptions(Datum options, bool validate, relopt_kind kind,
1692 : : int *numrelopts)
1693 : : {
3436 rhaas@postgresql.org 1694 : 72776 : relopt_value *reloptions = NULL;
6443 alvherre@alvh.no-ip. 1695 : 72776 : int numoptions = 0;
1696 : : int i;
1697 : : int j;
1698 : :
1699 [ + + ]: 72776 : if (need_initialization)
1700 : 4229 : initialize_reloptions();
1701 : :
1702 : : /* Build a list of expected options, based on kind */
1703 : :
1704 [ + + ]: 3425574 : for (i = 0; relOpts[i]; i++)
6354 1705 [ + + ]: 3352798 : if (relOpts[i]->kinds & kind)
6443 1706 : 1358114 : numoptions++;
1707 : :
3436 rhaas@postgresql.org 1708 [ + - ]: 72776 : if (numoptions > 0)
1709 : : {
10 michael@paquier.xyz 1710 :GNC 72776 : reloptions = palloc_array(relopt_value, numoptions);
1711 : :
3436 rhaas@postgresql.org 1712 [ + + ]:CBC 3425574 : for (i = 0, j = 0; relOpts[i]; i++)
1713 : : {
1714 [ + + ]: 3352798 : if (relOpts[i]->kinds & kind)
1715 : : {
1716 : 1358114 : reloptions[j].gen = relOpts[i];
1717 : 1358114 : reloptions[j].isset = false;
1718 : 1358114 : j++;
1719 : : }
1720 : : }
1721 : : }
1722 : :
1723 : : /* Done if no options */
337 peter@eisentraut.org 1724 [ + + ]: 72776 : if (DatumGetPointer(options) != NULL)
2341 akorotkov@postgresql 1725 : 13828 : parseRelOptionsInternal(options, validate, reloptions, numoptions);
1726 : :
1727 : 72620 : *numrelopts = numoptions;
1728 : 72620 : return reloptions;
1729 : : }
1730 : :
1731 : : /* Parse local unregistered options. */
1732 : : static relopt_value *
1733 : 1841 : parseLocalRelOptions(local_relopts *relopts, Datum options, bool validate)
1734 : : {
1735 : 1841 : int nopts = list_length(relopts->options);
260 michael@paquier.xyz 1736 : 1841 : relopt_value *values = palloc_array(relopt_value, nopts);
1737 : : ListCell *lc;
2341 akorotkov@postgresql 1738 : 1841 : int i = 0;
1739 : :
1740 [ + - + + : 4247 : foreach(lc, relopts->options)
+ + ]
1741 : : {
1742 : 2406 : local_relopt *opt = lfirst(lc);
1743 : :
1744 : 2406 : values[i].gen = opt->option;
1745 : 2406 : values[i].isset = false;
1746 : :
1747 : 2406 : i++;
1748 : : }
1749 : :
1750 [ + + ]: 1841 : if (options != (Datum) 0)
1751 : 279 : parseRelOptionsInternal(options, validate, values, nopts);
1752 : :
1753 : 1788 : return values;
1754 : : }
1755 : :
1756 : : /*
1757 : : * Subroutine for parseRelOptions, to parse and validate a single option's
1758 : : * value
1759 : : */
1760 : : static void
6443 alvherre@alvh.no-ip. 1761 : 15669 : parse_one_reloption(relopt_value *option, char *text_str, int text_len,
1762 : : bool validate)
1763 : : {
1764 : : char *value;
1765 : : int value_len;
1766 : : bool parsed;
1767 : 15669 : bool nofree = false;
1768 : :
1769 [ + + + + ]: 15669 : if (option->isset && validate)
1770 [ + - ]: 8 : ereport(ERROR,
1771 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1772 : : errmsg("parameter \"%s\" specified more than once",
1773 : : option->gen->name)));
1774 : :
1775 : 15661 : value_len = text_len - option->gen->namelen - 1;
1776 : 15661 : value = (char *) palloc(value_len + 1);
1777 : 15661 : memcpy(value, text_str + option->gen->namelen + 1, value_len);
1778 : 15661 : value[value_len] = '\0';
1779 : :
1780 [ + + + + : 15661 : switch (option->gen->type)
+ + - ]
1781 : : {
1782 : 2201 : case RELOPT_TYPE_BOOL:
1783 : : {
217 alvherre@kurilemu.de 1784 : 2201 : parsed = parse_bool(value, &option->bool_val);
6443 alvherre@alvh.no-ip. 1785 [ + + + + ]: 2201 : if (validate && !parsed)
1786 [ + - ]: 11 : ereport(ERROR,
1787 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1788 : : errmsg("invalid value for boolean option \"%s\": %s",
1789 : : option->gen->name, value)));
1790 : : }
1791 : 2190 : break;
218 alvherre@kurilemu.de 1792 : 6095 : case RELOPT_TYPE_TERNARY:
1793 : : {
1794 : : bool b;
1795 : :
1796 : 6095 : parsed = parse_bool(value, &b);
217 1797 : 6095 : option->ternary_val = b ? PG_TERNARY_TRUE :
1798 : : PG_TERNARY_FALSE;
218 1799 [ + + + + ]: 6095 : if (validate && !parsed)
1800 [ + - ]: 15 : ereport(ERROR,
1801 : : errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1802 : : errmsg("invalid value for boolean option \"%s\": %s",
1803 : : option->gen->name, value));
1804 : : }
1805 : 6080 : break;
6443 alvherre@alvh.no-ip. 1806 : 6146 : case RELOPT_TYPE_INT:
1807 : : {
6286 bruce@momjian.us 1808 : 6146 : relopt_int *optint = (relopt_int *) option->gen;
1809 : :
217 alvherre@kurilemu.de 1810 : 6146 : parsed = parse_int(value, &option->int_val, 0, NULL);
6443 alvherre@alvh.no-ip. 1811 [ + + + + ]: 6146 : if (validate && !parsed)
1812 [ + - ]: 14 : ereport(ERROR,
1813 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1814 : : errmsg("invalid value for integer option \"%s\": %s",
1815 : : option->gen->name, value)));
217 alvherre@kurilemu.de 1816 [ + + + + ]: 6132 : if (validate && (option->int_val < optint->min ||
1817 [ + + ]: 565 : option->int_val > optint->max))
6443 alvherre@alvh.no-ip. 1818 [ + - ]: 75 : ereport(ERROR,
1819 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1820 : : errmsg("value %s out of bounds for option \"%s\"",
1821 : : value, option->gen->name),
1822 : : errdetail("Valid values are between \"%d\" and \"%d\".",
1823 : : optint->min, optint->max)));
1824 : : }
1825 : 6057 : break;
1826 : 324 : case RELOPT_TYPE_REAL:
1827 : : {
6286 bruce@momjian.us 1828 : 324 : relopt_real *optreal = (relopt_real *) option->gen;
1829 : :
217 alvherre@kurilemu.de 1830 : 324 : parsed = parse_real(value, &option->real_val, 0, NULL);
6443 alvherre@alvh.no-ip. 1831 [ + + + + ]: 324 : if (validate && !parsed)
1832 [ + - ]: 10 : ereport(ERROR,
1833 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1834 : : errmsg("invalid value for floating point option \"%s\": %s",
1835 : : option->gen->name, value)));
217 alvherre@kurilemu.de 1836 [ + + + + ]: 314 : if (validate && (option->real_val < optreal->min ||
1837 [ + + ]: 108 : option->real_val > optreal->max))
6443 alvherre@alvh.no-ip. 1838 [ + - ]: 20 : ereport(ERROR,
1839 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1840 : : errmsg("value %s out of bounds for option \"%s\"",
1841 : : value, option->gen->name),
1842 : : errdetail("Valid values are between \"%f\" and \"%f\".",
1843 : : optreal->min, optreal->max)));
1844 : : }
1845 : 294 : break;
2528 1846 : 801 : case RELOPT_TYPE_ENUM:
1847 : : {
1848 : 801 : relopt_enum *optenum = (relopt_enum *) option->gen;
1849 : : relopt_enum_elt_def *elt;
1850 : :
1851 : 801 : parsed = false;
1852 [ + + ]: 1803 : for (elt = optenum->members; elt->string_val; elt++)
1853 : : {
1854 [ + + ]: 1791 : if (pg_strcasecmp(value, elt->string_val) == 0)
1855 : : {
217 alvherre@kurilemu.de 1856 : 789 : option->enum_val = elt->symbol_val;
2528 alvherre@alvh.no-ip. 1857 : 789 : parsed = true;
1858 : 789 : break;
1859 : : }
1860 : : }
1861 [ + + + + ]: 801 : if (validate && !parsed)
1862 [ + - + - ]: 12 : ereport(ERROR,
1863 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1864 : : errmsg("invalid value for enum option \"%s\": %s",
1865 : : option->gen->name, value),
1866 : : optenum->detailmsg ?
1867 : : errdetail_internal("%s", _(optenum->detailmsg)) : 0));
1868 : :
1869 : : /*
1870 : : * If value is not among the allowed string values, but we are
1871 : : * not asked to validate, just use the default numeric value.
1872 : : */
1873 [ - + ]: 789 : if (!parsed)
217 alvherre@kurilemu.de 1874 :UBC 0 : option->enum_val = optenum->default_val;
1875 : : }
2528 alvherre@alvh.no-ip. 1876 :CBC 789 : break;
6443 1877 : 94 : case RELOPT_TYPE_STRING:
1878 : : {
6286 bruce@momjian.us 1879 : 94 : relopt_string *optstring = (relopt_string *) option->gen;
1880 : :
217 alvherre@kurilemu.de 1881 : 94 : option->string_val = value;
6440 alvherre@alvh.no-ip. 1882 : 94 : nofree = true;
6436 1883 [ + + + - ]: 94 : if (validate && optstring->validate_cb)
1884 : 32 : (optstring->validate_cb) (value);
6440 1885 : 94 : parsed = true;
1886 : : }
6443 1887 : 94 : break;
6443 alvherre@alvh.no-ip. 1888 :UBC 0 : default:
1889 [ # # ]: 0 : elog(ERROR, "unsupported reloption type %d", option->gen->type);
1890 : : parsed = true; /* quiet compiler */
1891 : : break;
1892 : : }
1893 : :
6443 alvherre@alvh.no-ip. 1894 [ + - ]:CBC 15504 : if (parsed)
1895 : 15504 : option->isset = true;
1896 [ + + ]: 15504 : if (!nofree)
1897 : 15410 : pfree(value);
1898 : 15504 : }
1899 : :
1900 : : /*
1901 : : * Given the result from parseRelOptions, allocate a struct that's of the
1902 : : * specified base size plus any extra space that's needed for string variables.
1903 : : *
1904 : : * "base" should be sizeof(struct) of the reloptions struct (StdRdOptions or
1905 : : * equivalent).
1906 : : */
1907 : : static void *
6436 1908 : 74408 : allocateReloptStruct(Size base, relopt_value *options, int numoptions)
1909 : : {
6286 bruce@momjian.us 1910 : 74408 : Size size = base;
1911 : : int i;
1912 : :
6436 alvherre@alvh.no-ip. 1913 [ + + ]: 1432783 : for (i = 0; i < numoptions; i++)
1914 : : {
2341 akorotkov@postgresql 1915 : 1358375 : relopt_value *optval = &options[i];
1916 : :
1917 [ + + ]: 1358375 : if (optval->gen->type == RELOPT_TYPE_STRING)
1918 : : {
1919 : 134 : relopt_string *optstr = (relopt_string *) optval->gen;
1920 : :
1921 [ - + ]: 134 : if (optstr->fill_cb)
1922 : : {
217 alvherre@kurilemu.de 1923 [ # # ]:UBC 0 : const char *val = optval->isset ? optval->string_val :
1196 tgl@sss.pgh.pa.us 1924 [ # # ]: 0 : optstr->default_isnull ? NULL : optstr->default_val;
1925 : :
2341 akorotkov@postgresql 1926 : 0 : size += optstr->fill_cb(val, NULL);
1927 : : }
1928 : : else
2341 akorotkov@postgresql 1929 [ + + ]:CBC 134 : size += GET_STRING_RELOPTION_LEN(*optval) + 1;
1930 : : }
1931 : : }
1932 : :
6436 alvherre@alvh.no-ip. 1933 : 74408 : return palloc0(size);
1934 : : }
1935 : :
1936 : : /*
1937 : : * Given the result of parseRelOptions and a parsing table, fill in the
1938 : : * struct (previously allocated with allocateReloptStruct) with the parsed
1939 : : * values.
1940 : : *
1941 : : * rdopts is the pointer to the allocated struct to be filled.
1942 : : * basesize is the sizeof(struct) that was passed to allocateReloptStruct.
1943 : : * options, of length numoptions, is parseRelOptions' output.
1944 : : * elems, of length numelems, is the table describing the allowed options.
1945 : : * When validate is true, it is expected that all options appear in elems.
1946 : : */
1947 : : static void
6366 tgl@sss.pgh.pa.us 1948 : 74408 : fillRelOptions(void *rdopts, Size basesize,
1949 : : relopt_value *options, int numoptions,
1950 : : bool validate,
1951 : : const relopt_parse_elt *elems, int numelems)
1952 : : {
1953 : : int i;
6286 bruce@momjian.us 1954 : 74408 : int offset = basesize;
1955 : :
6436 alvherre@alvh.no-ip. 1956 [ + + ]: 1432783 : for (i = 0; i < numoptions; i++)
1957 : : {
1958 : : int j;
6286 bruce@momjian.us 1959 : 1358375 : bool found = false;
1960 : :
6436 alvherre@alvh.no-ip. 1961 [ + - ]: 17801808 : for (j = 0; j < numelems; j++)
1962 : : {
3135 tgl@sss.pgh.pa.us 1963 [ + + ]: 17801808 : if (strcmp(options[i].gen->name, elems[j].optname) == 0)
1964 : : {
1965 : : relopt_string *optstring;
6286 bruce@momjian.us 1966 : 1358375 : char *itempos = ((char *) rdopts) + elems[j].offset;
1967 : : char *string_val;
1968 : :
6436 alvherre@alvh.no-ip. 1969 [ + + + + : 1358375 : switch (options[i].gen->type)
+ + - ]
1970 : : {
1971 : 58234 : case RELOPT_TYPE_BOOL:
1972 : 116468 : *(bool *) itempos = options[i].isset ?
217 alvherre@kurilemu.de 1973 : 2189 : options[i].bool_val :
6436 alvherre@alvh.no-ip. 1974 [ + + ]: 58234 : ((relopt_bool *) options[i].gen)->default_val;
1975 : 58234 : break;
218 alvherre@kurilemu.de 1976 : 116951 : case RELOPT_TYPE_TERNARY:
1977 : 233902 : *(pg_ternary *) itempos = options[i].isset ?
217 1978 [ + + ]: 116951 : options[i].ternary_val : PG_TERNARY_UNSET;
218 1979 : 116951 : break;
6436 alvherre@alvh.no-ip. 1980 : 844463 : case RELOPT_TYPE_INT:
1981 : 1688926 : *(int *) itempos = options[i].isset ?
217 alvherre@kurilemu.de 1982 [ + + ]: 844463 : options[i].int_val :
6436 alvherre@alvh.no-ip. 1983 : 838423 : ((relopt_int *) options[i].gen)->default_val;
1984 : 844463 : break;
1985 : 267758 : case RELOPT_TYPE_REAL:
1986 : 535516 : *(double *) itempos = options[i].isset ?
217 alvherre@kurilemu.de 1987 [ + + ]: 267758 : options[i].real_val :
6436 alvherre@alvh.no-ip. 1988 : 267473 : ((relopt_real *) options[i].gen)->default_val;
1989 : 267758 : break;
2528 1990 : 70835 : case RELOPT_TYPE_ENUM:
1991 : 141670 : *(int *) itempos = options[i].isset ?
217 alvherre@kurilemu.de 1992 [ + + ]: 70835 : options[i].enum_val :
2528 alvherre@alvh.no-ip. 1993 : 70046 : ((relopt_enum *) options[i].gen)->default_val;
1994 : 70835 : break;
6436 1995 : 134 : case RELOPT_TYPE_STRING:
1996 : 134 : optstring = (relopt_string *) options[i].gen;
1997 [ + + ]: 134 : if (options[i].isset)
217 alvherre@kurilemu.de 1998 : 92 : string_val = options[i].string_val;
6436 alvherre@alvh.no-ip. 1999 [ + + ]: 42 : else if (!optstring->default_isnull)
2000 : 18 : string_val = optstring->default_val;
2001 : : else
2002 : 24 : string_val = NULL;
2003 : :
2341 akorotkov@postgresql 2004 [ - + ]: 134 : if (optstring->fill_cb)
2005 : : {
2006 : : Size size =
1196 tgl@sss.pgh.pa.us 2007 :UBC 0 : optstring->fill_cb(string_val,
2008 : : (char *) rdopts + offset);
2009 : :
2341 akorotkov@postgresql 2010 [ # # ]: 0 : if (size)
2011 : : {
2012 : 0 : *(int *) itempos = offset;
2013 : 0 : offset += size;
2014 : : }
2015 : : else
2016 : 0 : *(int *) itempos = 0;
2017 : : }
2341 akorotkov@postgresql 2018 [ + + ]:CBC 134 : else if (string_val == NULL)
6436 alvherre@alvh.no-ip. 2019 : 24 : *(int *) itempos = 0;
2020 : : else
2021 : : {
2022 : 110 : strcpy((char *) rdopts + offset, string_val);
2023 : 110 : *(int *) itempos = offset;
2024 : 110 : offset += strlen(string_val) + 1;
2025 : : }
2026 : 134 : break;
6436 alvherre@alvh.no-ip. 2027 :UBC 0 : default:
4043 tgl@sss.pgh.pa.us 2028 [ # # ]: 0 : elog(ERROR, "unsupported reloption type %d",
2029 : : options[i].gen->type);
2030 : : break;
2031 : : }
6436 alvherre@alvh.no-ip. 2032 :CBC 1358375 : found = true;
2033 : 1358375 : break;
2034 : : }
2035 : : }
2781 tgl@sss.pgh.pa.us 2036 [ + + - + ]: 1358375 : if (validate && !found)
6366 tgl@sss.pgh.pa.us 2037 [ # # ]:UBC 0 : elog(ERROR, "reloption \"%s\" not found in parse table",
2038 : : options[i].gen->name);
2039 : : }
6436 alvherre@alvh.no-ip. 2040 :CBC 74408 : SET_VARSIZE(rdopts, offset);
2041 : 74408 : }
2042 : :
2043 : :
2044 : : /*
2045 : : * Parse table for StdRdOptions.
2046 : : */
2047 : : static const relopt_parse_elt stdRdOptionsTab[] = {
2048 : : {"fillfactor", RELOPT_TYPE_INT, offsetof(StdRdOptions, fillfactor)},
2049 : : {"autovacuum_enabled", RELOPT_TYPE_TERNARY,
2050 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, enabled)},
2051 : : {"autovacuum_parallel_workers", RELOPT_TYPE_INT,
2052 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, autovacuum_parallel_workers)},
2053 : : {"autovacuum_vacuum_threshold", RELOPT_TYPE_INT,
2054 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_threshold)},
2055 : : {"autovacuum_vacuum_max_threshold", RELOPT_TYPE_INT,
2056 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_max_threshold)},
2057 : : {"autovacuum_vacuum_insert_threshold", RELOPT_TYPE_INT,
2058 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_ins_threshold)},
2059 : : {"autovacuum_analyze_threshold", RELOPT_TYPE_INT,
2060 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, analyze_threshold)},
2061 : : {"autovacuum_vacuum_cost_limit", RELOPT_TYPE_INT,
2062 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_cost_limit)},
2063 : : {"autovacuum_freeze_min_age", RELOPT_TYPE_INT,
2064 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_min_age)},
2065 : : {"autovacuum_freeze_max_age", RELOPT_TYPE_INT,
2066 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_max_age)},
2067 : : {"autovacuum_freeze_table_age", RELOPT_TYPE_INT,
2068 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_table_age)},
2069 : : {"autovacuum_multixact_freeze_min_age", RELOPT_TYPE_INT,
2070 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_min_age)},
2071 : : {"autovacuum_multixact_freeze_max_age", RELOPT_TYPE_INT,
2072 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_max_age)},
2073 : : {"autovacuum_multixact_freeze_table_age", RELOPT_TYPE_INT,
2074 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_table_age)},
2075 : : {"log_autovacuum_min_duration", RELOPT_TYPE_INT,
2076 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, log_vacuum_min_duration)},
2077 : : {"log_autoanalyze_min_duration", RELOPT_TYPE_INT,
2078 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, log_analyze_min_duration)},
2079 : : {"toast_tuple_target", RELOPT_TYPE_INT,
2080 : : offsetof(StdRdOptions, toast_tuple_target)},
2081 : : {"autovacuum_vacuum_cost_delay", RELOPT_TYPE_REAL,
2082 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_cost_delay)},
2083 : : {"autovacuum_vacuum_scale_factor", RELOPT_TYPE_REAL,
2084 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_scale_factor)},
2085 : : {"autovacuum_vacuum_insert_scale_factor", RELOPT_TYPE_REAL,
2086 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_ins_scale_factor)},
2087 : : {"autovacuum_analyze_scale_factor", RELOPT_TYPE_REAL,
2088 : : offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, analyze_scale_factor)},
2089 : : {"user_catalog_table", RELOPT_TYPE_BOOL,
2090 : : offsetof(StdRdOptions, user_catalog_table)},
2091 : : {"parallel_workers", RELOPT_TYPE_INT,
2092 : : offsetof(StdRdOptions, parallel_workers)},
2093 : : {"vacuum_index_cleanup", RELOPT_TYPE_ENUM,
2094 : : offsetof(StdRdOptions, vacuum_index_cleanup)},
2095 : : {"vacuum_truncate", RELOPT_TYPE_TERNARY,
2096 : : offsetof(StdRdOptions, vacuum_truncate)},
2097 : : {"vacuum_max_eager_freeze_failure_rate", RELOPT_TYPE_REAL,
2098 : : offsetof(StdRdOptions, vacuum_max_eager_freeze_failure_rate)}
2099 : : };
2100 : :
2101 : : /*
2102 : : * Option parser for anything that uses StdRdOptions.
2103 : : */
2104 : : bytea *
6443 2105 : 58510 : default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
2106 : : {
2487 michael@paquier.xyz 2107 : 58510 : return (bytea *) build_reloptions(reloptions, validate, kind,
2108 : : sizeof(StdRdOptions),
2109 : : stdRdOptionsTab,
2110 : : lengthof(stdRdOptionsTab));
2111 : : }
2112 : :
2113 : : /*
2114 : : * find_reloption
2115 : : * Look up a reloption of the given kind by name.
2116 : : *
2117 : : * Returns NULL if no such option can be set on relations of that kind. Note
2118 : : * that names are unique only within a kind; "fillfactor", for example, is
2119 : : * declared separately for heaps and for several index access methods.
2120 : : */
2121 : : static relopt_gen *
3 nathan@postgresql.or 2122 :GNC 754 : find_reloption(const char *name, relopt_kind kind)
2123 : : {
2124 [ - + ]: 754 : if (need_initialization)
3 nathan@postgresql.or 2125 :UNC 0 : initialize_reloptions();
2126 : :
3 nathan@postgresql.or 2127 [ + + ]:GNC 23374 : for (int i = 0; relOpts[i]; i++)
2128 : : {
2129 [ + + ]: 23142 : if ((relOpts[i]->kinds & kind) != 0 &&
2130 [ + + ]: 9135 : strcmp(relOpts[i]->name, name) == 0)
2131 : 522 : return relOpts[i];
2132 : : }
2133 : :
2134 : 232 : return NULL;
2135 : : }
2136 : :
2137 : : /*
2138 : : * merge_toast_reloptions
2139 : : * Fill in a TOAST table's unset options from its main table's.
2140 : : *
2141 : : * Any option that may be set on a TOAST table but was not is taken from
2142 : : * main_opts. Either argument may be NULL; if both are, NULL is returned.
2143 : : * Otherwise, the options to use are returned.
2144 : : *
2145 : : * An option counts as unset while it still holds the default declared for it
2146 : : * above, which works because nothing a TOAST table accepts has a default the
2147 : : * user could also set (see assert_toast_defaults_unsettable()).
2148 : : *
2149 : : * If the return value is not NULL, it is palloc'd.
2150 : : */
2151 : : StdRdOptions *
2152 : 27545 : merge_toast_reloptions(const StdRdOptions *toast_opts,
2153 : : const StdRdOptions *main_opts)
2154 : : {
2155 : : StdRdOptions *ret;
2156 : :
2157 : : /* if both arguments are NULL, return NULL */
2158 [ + + + + ]: 27545 : if (toast_opts == NULL && main_opts == NULL)
2159 : 26967 : return NULL;
2160 : :
2161 : : /* if one argument is NULL, return the non-NULL one */
2162 : 578 : ret = palloc_object(StdRdOptions);
2163 [ + + + + ]: 578 : if (toast_opts == NULL || main_opts == NULL)
2164 : : {
2165 [ + + ]: 549 : memcpy(ret, main_opts ? main_opts : toast_opts, sizeof(StdRdOptions));
2166 : 549 : return ret;
2167 : : }
2168 : :
2169 : : /* replace unset TOAST relopts with the main table's */
2170 : 29 : memcpy(ret, toast_opts, sizeof(StdRdOptions));
2171 [ + + ]: 783 : for (int i = 0; i < lengthof(stdRdOptionsTab); i++)
2172 : : {
2173 : 754 : const relopt_parse_elt *elem = &stdRdOptionsTab[i];
2174 : : relopt_gen *gen;
2175 : : char *toast_val;
2176 : : const char *main_val;
2177 : :
2178 : : /* skip anything that cannot be set on a TOAST table */
2179 : 754 : gen = find_reloption(elem->optname, RELOPT_KIND_TOAST);
2180 [ + + ]: 754 : if (gen == NULL)
2181 : 232 : continue;
2182 : :
2183 : 522 : toast_val = (char *) ret + elem->offset;
2184 : 522 : main_val = (const char *) main_opts + elem->offset;
2185 : :
2186 [ + + + + : 522 : switch (gen->type)
- ]
2187 : : {
2188 : 58 : case RELOPT_TYPE_TERNARY:
2189 [ + + ]: 58 : if (*(pg_ternary *) toast_val == PG_TERNARY_UNSET)
2190 : 43 : *(pg_ternary *) toast_val = *(const pg_ternary *) main_val;
2191 : 58 : break;
2192 : :
2193 : 319 : case RELOPT_TYPE_INT:
2194 [ + - ]: 319 : if (*(int *) toast_val == ((relopt_int *) gen)->default_val)
2195 : 319 : *(int *) toast_val = *(const int *) main_val;
2196 : 319 : break;
2197 : :
2198 : 116 : case RELOPT_TYPE_REAL:
2199 [ + + ]: 116 : if (*(double *) toast_val == ((relopt_real *) gen)->default_val)
2200 : 112 : *(double *) toast_val = *(const double *) main_val;
2201 : 116 : break;
2202 : :
2203 : 29 : case RELOPT_TYPE_ENUM:
2204 [ + + ]: 29 : if (*(int *) toast_val == ((relopt_enum *) gen)->default_val)
2205 : 17 : *(int *) toast_val = *(const int *) main_val;
2206 : 29 : break;
2207 : :
3 nathan@postgresql.or 2208 :UNC 0 : default:
2209 [ # # ]: 0 : elog(ERROR, "reloption \"%s\" has a type a TOAST table cannot inherit",
2210 : : elem->optname);
2211 : : }
2212 : : }
2213 : :
3 nathan@postgresql.or 2214 :GNC 29 : return ret;
2215 : : }
2216 : :
2217 : : /*
2218 : : * build_reloptions
2219 : : *
2220 : : * Parses "reloptions" provided by the caller, returning them in a
2221 : : * structure containing the parsed options. The parsing is done with
2222 : : * the help of a parsing table describing the allowed options, defined
2223 : : * by "relopt_elems" of length "num_relopt_elems".
2224 : : *
2225 : : * "validate" must be true if reloptions value is freshly built by
2226 : : * transformRelOptions(), as opposed to being read from the catalog, in which
2227 : : * case the values contained in it must already be valid.
2228 : : *
2229 : : * NULL is returned if the passed-in options did not match any of the options
2230 : : * in the parsing table, unless validate is true in which case an error would
2231 : : * be reported.
2232 : : */
2233 : : void *
2487 michael@paquier.xyz 2234 :CBC 72776 : build_reloptions(Datum reloptions, bool validate,
2235 : : relopt_kind kind,
2236 : : Size relopt_struct_size,
2237 : : const relopt_parse_elt *relopt_elems,
2238 : : int num_relopt_elems)
2239 : : {
2240 : : int numoptions;
2241 : : relopt_value *options;
2242 : : void *rdopts;
2243 : :
2244 : : /* parse options specific to given relation option kind */
6443 alvherre@alvh.no-ip. 2245 : 72776 : options = parseRelOptions(reloptions, validate, kind, &numoptions);
2487 michael@paquier.xyz 2246 [ - + ]: 72620 : Assert(numoptions <= num_relopt_elems);
2247 : :
2248 : : /* if none set, we're done */
6443 alvherre@alvh.no-ip. 2249 [ - + ]: 72620 : if (numoptions == 0)
2250 : : {
2487 michael@paquier.xyz 2251 [ # # ]:UBC 0 : Assert(options == NULL);
7360 tgl@sss.pgh.pa.us 2252 : 0 : return NULL;
2253 : : }
2254 : :
2255 : : /* allocate and fill the structure */
2487 michael@paquier.xyz 2256 :CBC 72620 : rdopts = allocateReloptStruct(relopt_struct_size, options, numoptions);
2257 : 72620 : fillRelOptions(rdopts, relopt_struct_size, options, numoptions,
2258 : : validate, relopt_elems, num_relopt_elems);
2259 : :
6443 alvherre@alvh.no-ip. 2260 : 72620 : pfree(options);
2261 : :
2487 michael@paquier.xyz 2262 : 72620 : return rdopts;
2263 : : }
2264 : :
2265 : : /*
2266 : : * Parse local options, allocate a bytea struct that's of the specified
2267 : : * 'base_size' plus any extra space that's needed for string variables,
2268 : : * fill its option's fields located at the given offsets and return it.
2269 : : */
2270 : : void *
2341 akorotkov@postgresql 2271 : 1841 : build_local_reloptions(local_relopts *relopts, Datum options, bool validate)
2272 : : {
2273 : 1841 : int noptions = list_length(relopts->options);
260 michael@paquier.xyz 2274 : 1841 : relopt_parse_elt *elems = palloc_array(relopt_parse_elt, noptions);
2275 : : relopt_value *vals;
2276 : : void *opts;
2341 akorotkov@postgresql 2277 : 1841 : int i = 0;
2278 : : ListCell *lc;
2279 : :
2280 [ + - + + : 4247 : foreach(lc, relopts->options)
+ + ]
2281 : : {
2282 : 2406 : local_relopt *opt = lfirst(lc);
2283 : :
2284 : 2406 : elems[i].optname = opt->option->name;
2285 : 2406 : elems[i].opttype = opt->option->type;
2286 : 2406 : elems[i].offset = opt->offset;
2287 : :
2288 : 2406 : i++;
2289 : : }
2290 : :
2291 : 1841 : vals = parseLocalRelOptions(relopts, options, validate);
2292 : 1788 : opts = allocateReloptStruct(relopts->relopt_struct_size, vals, noptions);
2293 : 1788 : fillRelOptions(opts, relopts->relopt_struct_size, vals, noptions, validate,
2294 : : elems, noptions);
2295 : :
1222 2296 [ + + ]: 1788 : if (validate)
2297 [ + + + + : 412 : foreach(lc, relopts->validators)
+ + ]
2298 : 3 : ((relopts_validator) lfirst(lc)) (opts, vals, noptions);
2299 : :
2341 2300 [ + - ]: 1787 : if (elems)
2301 : 1787 : pfree(elems);
2302 : :
2303 : 1787 : return opts;
2304 : : }
2305 : :
2306 : : /*
2307 : : * Option parser for partitioned tables
2308 : : */
2309 : : bytea *
2478 michael@paquier.xyz 2310 : 3353 : partitioned_table_reloptions(Datum reloptions, bool validate)
2311 : : {
1387 tgl@sss.pgh.pa.us 2312 [ + - + + ]: 3353 : if (validate && reloptions)
2313 [ + - ]: 8 : ereport(ERROR,
2314 : : errcode(ERRCODE_WRONG_OBJECT_TYPE),
2315 : : errmsg("cannot specify storage parameters for a partitioned table"),
2316 : : errhint("Specify storage parameters for its leaf partitions instead."));
2317 : 3345 : return NULL;
2318 : : }
2319 : :
2320 : : /*
2321 : : * Option parser for views
2322 : : */
2323 : : bytea *
4427 alvherre@alvh.no-ip. 2324 : 12230 : view_reloptions(Datum reloptions, bool validate)
2325 : : {
2326 : : static const relopt_parse_elt tab[] = {
2327 : : {"security_barrier", RELOPT_TYPE_BOOL,
2328 : : offsetof(ViewOptions, security_barrier)},
2329 : : {"security_invoker", RELOPT_TYPE_BOOL,
2330 : : offsetof(ViewOptions, security_invoker)},
2331 : : {"check_option", RELOPT_TYPE_ENUM,
2332 : : offsetof(ViewOptions, check_option)}
2333 : : };
2334 : :
2487 michael@paquier.xyz 2335 : 12230 : return (bytea *) build_reloptions(reloptions, validate,
2336 : : RELOPT_KIND_VIEW,
2337 : : sizeof(ViewOptions),
2338 : : tab, lengthof(tab));
2339 : : }
2340 : :
2341 : : /*
2342 : : * Parse options for heaps, views and toast tables.
2343 : : */
2344 : : bytea *
868 akorotkov@postgresql 2345 : 62558 : heap_reloptions(char relkind, Datum reloptions, bool validate)
2346 : : {
2347 : : StdRdOptions *rdopts;
2348 : :
6354 alvherre@alvh.no-ip. 2349 [ + + + ]: 62558 : switch (relkind)
2350 : : {
2351 : 26087 : case RELKIND_TOASTVALUE:
2352 : : rdopts = (StdRdOptions *)
5925 itagaki.takahiro@gma 2353 : 26087 : default_reloptions(reloptions, validate, RELOPT_KIND_TOAST);
2354 [ + - ]: 26083 : if (rdopts != NULL)
2355 : : {
2356 : : /* adjust default-only parameters for TOAST relations */
2357 : 26083 : rdopts->fillfactor = 100;
868 akorotkov@postgresql 2358 : 26083 : rdopts->autovacuum.analyze_threshold = -1;
2359 : 26083 : rdopts->autovacuum.analyze_scale_factor = -1;
2360 : : }
5925 itagaki.takahiro@gma 2361 : 26083 : return (bytea *) rdopts;
6354 alvherre@alvh.no-ip. 2362 : 32423 : case RELKIND_RELATION:
2363 : : case RELKIND_MATVIEW:
868 akorotkov@postgresql 2364 : 32423 : return default_reloptions(reloptions, validate, RELOPT_KIND_HEAP);
6354 alvherre@alvh.no-ip. 2365 : 4048 : default:
2366 : : /* other relkinds are not supported */
2367 : 4048 : return NULL;
2368 : : }
2369 : : }
2370 : :
2371 : :
2372 : : /*
2373 : : * Parse options for indexes.
2374 : : *
2375 : : * amoptions index AM's option parser function
2376 : : * reloptions options as text[] datum
2377 : : * validate error flag
2378 : : */
2379 : : bytea *
3875 tgl@sss.pgh.pa.us 2380 : 21002 : index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
2381 : : {
2382 [ - + ]: 21002 : Assert(amoptions != NULL);
2383 : :
2384 : : /* Assume function is strict */
337 peter@eisentraut.org 2385 [ + + ]: 21002 : if (DatumGetPointer(reloptions) == NULL)
7360 tgl@sss.pgh.pa.us 2386 : 19072 : return NULL;
2387 : :
3875 2388 : 1930 : return amoptions(reloptions, validate);
2389 : : }
2390 : :
2391 : : /*
2392 : : * Option parser for attribute reloptions
2393 : : */
2394 : : bytea *
6061 rhaas@postgresql.org 2395 : 25 : attribute_reloptions(Datum reloptions, bool validate)
2396 : : {
2397 : : static const relopt_parse_elt tab[] = {
2398 : : {"n_distinct", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct)},
2399 : : {"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)}
2400 : : };
2401 : :
2487 michael@paquier.xyz 2402 : 25 : return (bytea *) build_reloptions(reloptions, validate,
2403 : : RELOPT_KIND_ATTRIBUTE,
2404 : : sizeof(AttributeOpts),
2405 : : tab, lengthof(tab));
2406 : : }
2407 : :
2408 : : /*
2409 : : * Option parser for tablespace reloptions
2410 : : */
2411 : : bytea *
6078 rhaas@postgresql.org 2412 : 81 : tablespace_reloptions(Datum reloptions, bool validate)
2413 : : {
2414 : : static const relopt_parse_elt tab[] = {
2415 : : {"random_page_cost", RELOPT_TYPE_REAL, offsetof(TableSpaceOpts, random_page_cost)},
2416 : : {"seq_page_cost", RELOPT_TYPE_REAL, offsetof(TableSpaceOpts, seq_page_cost)},
2417 : : {"effective_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, effective_io_concurrency)},
2418 : : {"maintenance_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, maintenance_io_concurrency)}
2419 : : };
2420 : :
2487 michael@paquier.xyz 2421 : 81 : return (bytea *) build_reloptions(reloptions, validate,
2422 : : RELOPT_KIND_TABLESPACE,
2423 : : sizeof(TableSpaceOpts),
2424 : : tab, lengthof(tab));
2425 : : }
2426 : :
2427 : : /*
2428 : : * Determine the required LOCKMODE from an option list.
2429 : : *
2430 : : * Called from AlterTableGetLockLevel(), see that function
2431 : : * for a longer explanation of how this works.
2432 : : */
2433 : : LOCKMODE
4031 simon@2ndQuadrant.co 2434 : 498 : AlterTableGetRelOptionsLockLevel(List *defList)
2435 : : {
3731 rhaas@postgresql.org 2436 : 498 : LOCKMODE lockmode = NoLock;
2437 : : ListCell *cell;
2438 : :
4031 simon@2ndQuadrant.co 2439 [ - + ]: 498 : if (defList == NIL)
4031 simon@2ndQuadrant.co 2440 :UBC 0 : return AccessExclusiveLock;
2441 : :
4031 simon@2ndQuadrant.co 2442 [ + + ]:CBC 498 : if (need_initialization)
2443 : 10 : initialize_reloptions();
2444 : :
2445 [ + - + + : 1021 : foreach(cell, defList)
+ + ]
2446 : : {
3731 rhaas@postgresql.org 2447 : 523 : DefElem *def = (DefElem *) lfirst(cell);
2448 : : int i;
2449 : :
4031 simon@2ndQuadrant.co 2450 [ + + ]: 24908 : for (i = 0; relOpts[i]; i++)
2451 : : {
3135 tgl@sss.pgh.pa.us 2452 : 24385 : if (strncmp(relOpts[i]->name,
2453 : 24385 : def->defname,
2454 [ + + ]: 24385 : relOpts[i]->namelen + 1) == 0)
2455 : : {
4031 simon@2ndQuadrant.co 2456 [ + + ]: 747 : if (lockmode < relOpts[i]->lockmode)
2457 : 494 : lockmode = relOpts[i]->lockmode;
2458 : : }
2459 : : }
2460 : : }
2461 : :
2462 : 498 : return lockmode;
2463 : : }
|