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