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