LCOV - code coverage report
Current view: top level - src/backend/access/common - reloptions.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12devel Lines: 351 402 87.3 %
Date: 2018-09-20 15:21:09 Functions: 20 23 87.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * reloptions.c
       4             :  *    Core support for relation options (pg_class.reloptions)
       5             :  *
       6             :  * Portions Copyright (c) 1996-2018, 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/htup_details.h"
      23             : #include "access/nbtree.h"
      24             : #include "access/reloptions.h"
      25             : #include "access/spgist.h"
      26             : #include "access/tuptoaster.h"
      27             : #include "catalog/pg_type.h"
      28             : #include "commands/defrem.h"
      29             : #include "commands/tablespace.h"
      30             : #include "commands/view.h"
      31             : #include "nodes/makefuncs.h"
      32             : #include "postmaster/postmaster.h"
      33             : #include "utils/array.h"
      34             : #include "utils/attoptcache.h"
      35             : #include "utils/builtins.h"
      36             : #include "utils/guc.h"
      37             : #include "utils/memutils.h"
      38             : #include "utils/rel.h"
      39             : 
      40             : /*
      41             :  * Contents of pg_class.reloptions
      42             :  *
      43             :  * To add an option:
      44             :  *
      45             :  * (i) decide on a type (integer, real, bool, string), name, default value,
      46             :  * upper and lower bounds (if applicable); for strings, consider a validation
      47             :  * routine.
      48             :  * (ii) add a record below (or use add_<type>_reloption).
      49             :  * (iii) add it to the appropriate options struct (perhaps StdRdOptions)
      50             :  * (iv) add it to the appropriate handling routine (perhaps
      51             :  * default_reloptions)
      52             :  * (v) make sure the lock level is set correctly for that operation
      53             :  * (vi) don't forget to document the option
      54             :  *
      55             :  * Note that we don't handle "oids" in relOpts because it is handled by
      56             :  * interpretOidsOption().
      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 because it applies only to subsequent changes made to
      75             :  * data blocks, as documented in heapio.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 affect until the next ANALYZE, so no need for stronger lock.
      81             :  *
      82             :  * Planner-related parameters can be set with 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 is safe, since it acts the same as
      90             :  * max_parallel_workers_per_gather which is a USERSET parameter that doesn't
      91             :  * affect existing plans or queries.
      92             :  */
      93             : 
      94             : static relopt_bool boolRelOpts[] =
      95             : {
      96             :     {
      97             :         {
      98             :             "autosummarize",
      99             :             "Enables automatic summarization on this BRIN index",
     100             :             RELOPT_KIND_BRIN,
     101             :             AccessExclusiveLock
     102             :         },
     103             :         false
     104             :     },
     105             :     {
     106             :         {
     107             :             "autovacuum_enabled",
     108             :             "Enables autovacuum in this relation",
     109             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     110             :             ShareUpdateExclusiveLock
     111             :         },
     112             :         true
     113             :     },
     114             :     {
     115             :         {
     116             :             "user_catalog_table",
     117             :             "Declare a table as an additional catalog table, e.g. for the purpose of logical replication",
     118             :             RELOPT_KIND_HEAP,
     119             :             AccessExclusiveLock
     120             :         },
     121             :         false
     122             :     },
     123             :     {
     124             :         {
     125             :             "fastupdate",
     126             :             "Enables \"fast update\" feature for this GIN index",
     127             :             RELOPT_KIND_GIN,
     128             :             AccessExclusiveLock
     129             :         },
     130             :         true
     131             :     },
     132             :     {
     133             :         {
     134             :             "recheck_on_update",
     135             :             "Recheck functional index expression for changed value after update",
     136             :             RELOPT_KIND_INDEX,
     137             :             ShareUpdateExclusiveLock    /* since only applies to later UPDATEs */
     138             :         },
     139             :         true
     140             :     },
     141             :     {
     142             :         {
     143             :             "security_barrier",
     144             :             "View acts as a row security barrier",
     145             :             RELOPT_KIND_VIEW,
     146             :             AccessExclusiveLock
     147             :         },
     148             :         false
     149             :     },
     150             :     /* list terminator */
     151             :     {{NULL}}
     152             : };
     153             : 
     154             : static relopt_int intRelOpts[] =
     155             : {
     156             :     {
     157             :         {
     158             :             "fillfactor",
     159             :             "Packs table pages only to this percentage",
     160             :             RELOPT_KIND_HEAP,
     161             :             ShareUpdateExclusiveLock    /* since it applies only to later
     162             :                                          * inserts */
     163             :         },
     164             :         HEAP_DEFAULT_FILLFACTOR, HEAP_MIN_FILLFACTOR, 100
     165             :     },
     166             :     {
     167             :         {
     168             :             "fillfactor",
     169             :             "Packs btree index pages only to this percentage",
     170             :             RELOPT_KIND_BTREE,
     171             :             ShareUpdateExclusiveLock    /* since it applies only to later
     172             :                                          * inserts */
     173             :         },
     174             :         BTREE_DEFAULT_FILLFACTOR, BTREE_MIN_FILLFACTOR, 100
     175             :     },
     176             :     {
     177             :         {
     178             :             "fillfactor",
     179             :             "Packs hash index pages only to this percentage",
     180             :             RELOPT_KIND_HASH,
     181             :             ShareUpdateExclusiveLock    /* since it applies only to later
     182             :                                          * inserts */
     183             :         },
     184             :         HASH_DEFAULT_FILLFACTOR, HASH_MIN_FILLFACTOR, 100
     185             :     },
     186             :     {
     187             :         {
     188             :             "fillfactor",
     189             :             "Packs gist index pages only to this percentage",
     190             :             RELOPT_KIND_GIST,
     191             :             ShareUpdateExclusiveLock    /* since it applies only to later
     192             :                                          * inserts */
     193             :         },
     194             :         GIST_DEFAULT_FILLFACTOR, GIST_MIN_FILLFACTOR, 100
     195             :     },
     196             :     {
     197             :         {
     198             :             "fillfactor",
     199             :             "Packs spgist index pages only to this percentage",
     200             :             RELOPT_KIND_SPGIST,
     201             :             ShareUpdateExclusiveLock    /* since it applies only to later
     202             :                                          * inserts */
     203             :         },
     204             :         SPGIST_DEFAULT_FILLFACTOR, SPGIST_MIN_FILLFACTOR, 100
     205             :     },
     206             :     {
     207             :         {
     208             :             "autovacuum_vacuum_threshold",
     209             :             "Minimum number of tuple updates or deletes prior to vacuum",
     210             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     211             :             ShareUpdateExclusiveLock
     212             :         },
     213             :         -1, 0, INT_MAX
     214             :     },
     215             :     {
     216             :         {
     217             :             "autovacuum_analyze_threshold",
     218             :             "Minimum number of tuple inserts, updates or deletes prior to analyze",
     219             :             RELOPT_KIND_HEAP,
     220             :             ShareUpdateExclusiveLock
     221             :         },
     222             :         -1, 0, INT_MAX
     223             :     },
     224             :     {
     225             :         {
     226             :             "autovacuum_vacuum_cost_delay",
     227             :             "Vacuum cost delay in milliseconds, for autovacuum",
     228             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     229             :             ShareUpdateExclusiveLock
     230             :         },
     231             :         -1, 0, 100
     232             :     },
     233             :     {
     234             :         {
     235             :             "autovacuum_vacuum_cost_limit",
     236             :             "Vacuum cost amount available before napping, for autovacuum",
     237             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     238             :             ShareUpdateExclusiveLock
     239             :         },
     240             :         -1, 1, 10000
     241             :     },
     242             :     {
     243             :         {
     244             :             "autovacuum_freeze_min_age",
     245             :             "Minimum age at which VACUUM should freeze a table row, for autovacuum",
     246             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     247             :             ShareUpdateExclusiveLock
     248             :         },
     249             :         -1, 0, 1000000000
     250             :     },
     251             :     {
     252             :         {
     253             :             "autovacuum_multixact_freeze_min_age",
     254             :             "Minimum multixact age at which VACUUM should freeze a row multixact's, for autovacuum",
     255             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     256             :             ShareUpdateExclusiveLock
     257             :         },
     258             :         -1, 0, 1000000000
     259             :     },
     260             :     {
     261             :         {
     262             :             "autovacuum_freeze_max_age",
     263             :             "Age at which to autovacuum a table to prevent transaction ID wraparound",
     264             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     265             :             ShareUpdateExclusiveLock
     266             :         },
     267             :         -1, 100000, 2000000000
     268             :     },
     269             :     {
     270             :         {
     271             :             "autovacuum_multixact_freeze_max_age",
     272             :             "Multixact age at which to autovacuum a table to prevent multixact wraparound",
     273             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     274             :             ShareUpdateExclusiveLock
     275             :         },
     276             :         -1, 10000, 2000000000
     277             :     },
     278             :     {
     279             :         {
     280             :             "autovacuum_freeze_table_age",
     281             :             "Age at which VACUUM should perform a full table sweep to freeze row versions",
     282             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     283             :             ShareUpdateExclusiveLock
     284             :         }, -1, 0, 2000000000
     285             :     },
     286             :     {
     287             :         {
     288             :             "autovacuum_multixact_freeze_table_age",
     289             :             "Age of multixact at which VACUUM should perform a full table sweep to freeze row versions",
     290             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     291             :             ShareUpdateExclusiveLock
     292             :         }, -1, 0, 2000000000
     293             :     },
     294             :     {
     295             :         {
     296             :             "log_autovacuum_min_duration",
     297             :             "Sets the minimum execution time above which autovacuum actions will be logged",
     298             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     299             :             ShareUpdateExclusiveLock
     300             :         },
     301             :         -1, -1, INT_MAX
     302             :     },
     303             :     {
     304             :         {
     305             :             "toast_tuple_target",
     306             :             "Sets the target tuple length at which external columns will be toasted",
     307             :             RELOPT_KIND_HEAP,
     308             :             ShareUpdateExclusiveLock
     309             :         },
     310             :         TOAST_TUPLE_TARGET, 128, TOAST_TUPLE_TARGET_MAIN
     311             :     },
     312             :     {
     313             :         {
     314             :             "pages_per_range",
     315             :             "Number of pages that each page range covers in a BRIN index",
     316             :             RELOPT_KIND_BRIN,
     317             :             AccessExclusiveLock
     318             :         }, 128, 1, 131072
     319             :     },
     320             :     {
     321             :         {
     322             :             "gin_pending_list_limit",
     323             :             "Maximum size of the pending list for this GIN index, in kilobytes.",
     324             :             RELOPT_KIND_GIN,
     325             :             AccessExclusiveLock
     326             :         },
     327             :         -1, 64, MAX_KILOBYTES
     328             :     },
     329             :     {
     330             :         {
     331             :             "effective_io_concurrency",
     332             :             "Number of simultaneous requests that can be handled efficiently by the disk subsystem.",
     333             :             RELOPT_KIND_TABLESPACE,
     334             :             ShareUpdateExclusiveLock
     335             :         },
     336             : #ifdef USE_PREFETCH
     337             :         -1, 0, MAX_IO_CONCURRENCY
     338             : #else
     339             :         0, 0, 0
     340             : #endif
     341             :     },
     342             :     {
     343             :         {
     344             :             "parallel_workers",
     345             :             "Number of parallel processes that can be used per executor node for this relation.",
     346             :             RELOPT_KIND_HEAP,
     347             :             ShareUpdateExclusiveLock
     348             :         },
     349             :         -1, 0, 1024
     350             :     },
     351             : 
     352             :     /* list terminator */
     353             :     {{NULL}}
     354             : };
     355             : 
     356             : static relopt_real realRelOpts[] =
     357             : {
     358             :     {
     359             :         {
     360             :             "autovacuum_vacuum_scale_factor",
     361             :             "Number of tuple updates or deletes prior to vacuum as a fraction of reltuples",
     362             :             RELOPT_KIND_HEAP | RELOPT_KIND_TOAST,
     363             :             ShareUpdateExclusiveLock
     364             :         },
     365             :         -1, 0.0, 100.0
     366             :     },
     367             :     {
     368             :         {
     369             :             "autovacuum_analyze_scale_factor",
     370             :             "Number of tuple inserts, updates or deletes prior to analyze as a fraction of reltuples",
     371             :             RELOPT_KIND_HEAP,
     372             :             ShareUpdateExclusiveLock
     373             :         },
     374             :         -1, 0.0, 100.0
     375             :     },
     376             :     {
     377             :         {
     378             :             "seq_page_cost",
     379             :             "Sets the planner's estimate of the cost of a sequentially fetched disk page.",
     380             :             RELOPT_KIND_TABLESPACE,
     381             :             ShareUpdateExclusiveLock
     382             :         },
     383             :         -1, 0.0, DBL_MAX
     384             :     },
     385             :     {
     386             :         {
     387             :             "random_page_cost",
     388             :             "Sets the planner's estimate of the cost of a nonsequentially fetched disk page.",
     389             :             RELOPT_KIND_TABLESPACE,
     390             :             ShareUpdateExclusiveLock
     391             :         },
     392             :         -1, 0.0, DBL_MAX
     393             :     },
     394             :     {
     395             :         {
     396             :             "n_distinct",
     397             :             "Sets the planner's estimate of the number of distinct values appearing in a column (excluding child relations).",
     398             :             RELOPT_KIND_ATTRIBUTE,
     399             :             ShareUpdateExclusiveLock
     400             :         },
     401             :         0, -1.0, DBL_MAX
     402             :     },
     403             :     {
     404             :         {
     405             :             "n_distinct_inherited",
     406             :             "Sets the planner's estimate of the number of distinct values appearing in a column (including child relations).",
     407             :             RELOPT_KIND_ATTRIBUTE,
     408             :             ShareUpdateExclusiveLock
     409             :         },
     410             :         0, -1.0, DBL_MAX
     411             :     },
     412             :     {
     413             :         {
     414             :             "vacuum_cleanup_index_scale_factor",
     415             :             "Number of tuple inserts prior to index cleanup as a fraction of reltuples.",
     416             :             RELOPT_KIND_BTREE,
     417             :             ShareUpdateExclusiveLock
     418             :         },
     419             :         -1, 0.0, 1e10
     420             :     },
     421             :     /* list terminator */
     422             :     {{NULL}}
     423             : };
     424             : 
     425             : static relopt_string stringRelOpts[] =
     426             : {
     427             :     {
     428             :         {
     429             :             "buffering",
     430             :             "Enables buffering build for this GiST index",
     431             :             RELOPT_KIND_GIST,
     432             :             AccessExclusiveLock
     433             :         },
     434             :         4,
     435             :         false,
     436             :         gistValidateBufferingOption,
     437             :         "auto"
     438             :     },
     439             :     {
     440             :         {
     441             :             "check_option",
     442             :             "View has WITH CHECK OPTION defined (local or cascaded).",
     443             :             RELOPT_KIND_VIEW,
     444             :             AccessExclusiveLock
     445             :         },
     446             :         0,
     447             :         true,
     448             :         validateWithCheckOption,
     449             :         NULL
     450             :     },
     451             :     /* list terminator */
     452             :     {{NULL}}
     453             : };
     454             : 
     455             : static relopt_gen **relOpts = NULL;
     456             : static bits32 last_assigned_kind = RELOPT_KIND_LAST_DEFAULT;
     457             : 
     458             : static int  num_custom_options = 0;
     459             : static relopt_gen **custom_options = NULL;
     460             : static bool need_initialization = true;
     461             : 
     462             : static void initialize_reloptions(void);
     463             : static void parse_one_reloption(relopt_value *option, char *text_str,
     464             :                     int text_len, bool validate);
     465             : 
     466             : /*
     467             :  * initialize_reloptions
     468             :  *      initialization routine, must be called before parsing
     469             :  *
     470             :  * Initialize the relOpts array and fill each variable's type and name length.
     471             :  */
     472             : static void
     473        3296 : initialize_reloptions(void)
     474             : {
     475             :     int         i;
     476             :     int         j;
     477             : 
     478        3296 :     j = 0;
     479       23072 :     for (i = 0; boolRelOpts[i].gen.name; i++)
     480             :     {
     481             :         Assert(DoLockModesConflict(boolRelOpts[i].gen.lockmode,
     482             :                                    boolRelOpts[i].gen.lockmode));
     483       19776 :         j++;
     484             :     }
     485       72512 :     for (i = 0; intRelOpts[i].gen.name; i++)
     486             :     {
     487             :         Assert(DoLockModesConflict(intRelOpts[i].gen.lockmode,
     488             :                                    intRelOpts[i].gen.lockmode));
     489       69216 :         j++;
     490             :     }
     491       26368 :     for (i = 0; realRelOpts[i].gen.name; i++)
     492             :     {
     493             :         Assert(DoLockModesConflict(realRelOpts[i].gen.lockmode,
     494             :                                    realRelOpts[i].gen.lockmode));
     495       23072 :         j++;
     496             :     }
     497        9888 :     for (i = 0; stringRelOpts[i].gen.name; i++)
     498             :     {
     499             :         Assert(DoLockModesConflict(stringRelOpts[i].gen.lockmode,
     500             :                                    stringRelOpts[i].gen.lockmode));
     501        6592 :         j++;
     502             :     }
     503        3296 :     j += num_custom_options;
     504             : 
     505        3296 :     if (relOpts)
     506           0 :         pfree(relOpts);
     507        3296 :     relOpts = MemoryContextAlloc(TopMemoryContext,
     508        3296 :                                  (j + 1) * sizeof(relopt_gen *));
     509             : 
     510        3296 :     j = 0;
     511       23072 :     for (i = 0; boolRelOpts[i].gen.name; i++)
     512             :     {
     513       19776 :         relOpts[j] = &boolRelOpts[i].gen;
     514       19776 :         relOpts[j]->type = RELOPT_TYPE_BOOL;
     515       19776 :         relOpts[j]->namelen = strlen(relOpts[j]->name);
     516       19776 :         j++;
     517             :     }
     518             : 
     519       72512 :     for (i = 0; intRelOpts[i].gen.name; i++)
     520             :     {
     521       69216 :         relOpts[j] = &intRelOpts[i].gen;
     522       69216 :         relOpts[j]->type = RELOPT_TYPE_INT;
     523       69216 :         relOpts[j]->namelen = strlen(relOpts[j]->name);
     524       69216 :         j++;
     525             :     }
     526             : 
     527       26368 :     for (i = 0; realRelOpts[i].gen.name; i++)
     528             :     {
     529       23072 :         relOpts[j] = &realRelOpts[i].gen;
     530       23072 :         relOpts[j]->type = RELOPT_TYPE_REAL;
     531       23072 :         relOpts[j]->namelen = strlen(relOpts[j]->name);
     532       23072 :         j++;
     533             :     }
     534             : 
     535        9888 :     for (i = 0; stringRelOpts[i].gen.name; i++)
     536             :     {
     537        6592 :         relOpts[j] = &stringRelOpts[i].gen;
     538        6592 :         relOpts[j]->type = RELOPT_TYPE_STRING;
     539        6592 :         relOpts[j]->namelen = strlen(relOpts[j]->name);
     540        6592 :         j++;
     541             :     }
     542             : 
     543        3362 :     for (i = 0; i < num_custom_options; i++)
     544             :     {
     545          66 :         relOpts[j] = custom_options[i];
     546          66 :         j++;
     547             :     }
     548             : 
     549             :     /* add a list terminator */
     550        3296 :     relOpts[j] = NULL;
     551             : 
     552             :     /* flag the work is complete */
     553        3296 :     need_initialization = false;
     554        3296 : }
     555             : 
     556             : /*
     557             :  * add_reloption_kind
     558             :  *      Create a new relopt_kind value, to be used in custom reloptions by
     559             :  *      user-defined AMs.
     560             :  */
     561             : relopt_kind
     562           2 : add_reloption_kind(void)
     563             : {
     564             :     /* don't hand out the last bit so that the enum's behavior is portable */
     565           2 :     if (last_assigned_kind >= RELOPT_KIND_MAX)
     566           0 :         ereport(ERROR,
     567             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     568             :                  errmsg("user-defined relation parameter types limit exceeded")));
     569           2 :     last_assigned_kind <<= 1;
     570           2 :     return (relopt_kind) last_assigned_kind;
     571             : }
     572             : 
     573             : /*
     574             :  * add_reloption
     575             :  *      Add an already-created custom reloption to the list, and recompute the
     576             :  *      main parser table.
     577             :  */
     578             : static void
     579          66 : add_reloption(relopt_gen *newoption)
     580             : {
     581             :     static int  max_custom_options = 0;
     582             : 
     583          66 :     if (num_custom_options >= max_custom_options)
     584             :     {
     585             :         MemoryContext oldcxt;
     586             : 
     587           8 :         oldcxt = MemoryContextSwitchTo(TopMemoryContext);
     588             : 
     589           8 :         if (max_custom_options == 0)
     590             :         {
     591           2 :             max_custom_options = 8;
     592           2 :             custom_options = palloc(max_custom_options * sizeof(relopt_gen *));
     593             :         }
     594             :         else
     595             :         {
     596           6 :             max_custom_options *= 2;
     597           6 :             custom_options = repalloc(custom_options,
     598             :                                       max_custom_options * sizeof(relopt_gen *));
     599             :         }
     600           8 :         MemoryContextSwitchTo(oldcxt);
     601             :     }
     602          66 :     custom_options[num_custom_options++] = newoption;
     603             : 
     604          66 :     need_initialization = true;
     605          66 : }
     606             : 
     607             : /*
     608             :  * allocate_reloption
     609             :  *      Allocate a new reloption and initialize the type-agnostic fields
     610             :  *      (for types other than string)
     611             :  */
     612             : static relopt_gen *
     613          66 : allocate_reloption(bits32 kinds, int type, const char *name, const char *desc)
     614             : {
     615             :     MemoryContext oldcxt;
     616             :     size_t      size;
     617             :     relopt_gen *newoption;
     618             : 
     619          66 :     oldcxt = MemoryContextSwitchTo(TopMemoryContext);
     620             : 
     621          66 :     switch (type)
     622             :     {
     623             :         case RELOPT_TYPE_BOOL:
     624           0 :             size = sizeof(relopt_bool);
     625           0 :             break;
     626             :         case RELOPT_TYPE_INT:
     627          66 :             size = sizeof(relopt_int);
     628          66 :             break;
     629             :         case RELOPT_TYPE_REAL:
     630           0 :             size = sizeof(relopt_real);
     631           0 :             break;
     632             :         case RELOPT_TYPE_STRING:
     633           0 :             size = sizeof(relopt_string);
     634           0 :             break;
     635             :         default:
     636           0 :             elog(ERROR, "unsupported reloption type %d", type);
     637             :             return NULL;        /* keep compiler quiet */
     638             :     }
     639             : 
     640          66 :     newoption = palloc(size);
     641             : 
     642          66 :     newoption->name = pstrdup(name);
     643          66 :     if (desc)
     644          66 :         newoption->desc = pstrdup(desc);
     645             :     else
     646           0 :         newoption->desc = NULL;
     647          66 :     newoption->kinds = kinds;
     648          66 :     newoption->namelen = strlen(name);
     649          66 :     newoption->type = type;
     650             : 
     651          66 :     MemoryContextSwitchTo(oldcxt);
     652             : 
     653          66 :     return newoption;
     654             : }
     655             : 
     656             : /*
     657             :  * add_bool_reloption
     658             :  *      Add a new boolean reloption
     659             :  */
     660             : void
     661           0 : add_bool_reloption(bits32 kinds, const char *name, const char *desc, bool default_val)
     662             : {
     663             :     relopt_bool *newoption;
     664             : 
     665           0 :     newoption = (relopt_bool *) allocate_reloption(kinds, RELOPT_TYPE_BOOL,
     666             :                                                    name, desc);
     667           0 :     newoption->default_val = default_val;
     668             : 
     669           0 :     add_reloption((relopt_gen *) newoption);
     670           0 : }
     671             : 
     672             : /*
     673             :  * add_int_reloption
     674             :  *      Add a new integer reloption
     675             :  */
     676             : void
     677          66 : add_int_reloption(bits32 kinds, const char *name, const char *desc, int default_val,
     678             :                   int min_val, int max_val)
     679             : {
     680             :     relopt_int *newoption;
     681             : 
     682          66 :     newoption = (relopt_int *) allocate_reloption(kinds, RELOPT_TYPE_INT,
     683             :                                                   name, desc);
     684          66 :     newoption->default_val = default_val;
     685          66 :     newoption->min = min_val;
     686          66 :     newoption->max = max_val;
     687             : 
     688          66 :     add_reloption((relopt_gen *) newoption);
     689          66 : }
     690             : 
     691             : /*
     692             :  * add_real_reloption
     693             :  *      Add a new float reloption
     694             :  */
     695             : void
     696           0 : add_real_reloption(bits32 kinds, const char *name, const char *desc, double default_val,
     697             :                    double min_val, double max_val)
     698             : {
     699             :     relopt_real *newoption;
     700             : 
     701           0 :     newoption = (relopt_real *) allocate_reloption(kinds, RELOPT_TYPE_REAL,
     702             :                                                    name, desc);
     703           0 :     newoption->default_val = default_val;
     704           0 :     newoption->min = min_val;
     705           0 :     newoption->max = max_val;
     706             : 
     707           0 :     add_reloption((relopt_gen *) newoption);
     708           0 : }
     709             : 
     710             : /*
     711             :  * add_string_reloption
     712             :  *      Add a new string reloption
     713             :  *
     714             :  * "validator" is an optional function pointer that can be used to test the
     715             :  * validity of the values.  It must elog(ERROR) when the argument string is
     716             :  * not acceptable for the variable.  Note that the default value must pass
     717             :  * the validation.
     718             :  */
     719             : void
     720           0 : add_string_reloption(bits32 kinds, const char *name, const char *desc, const char *default_val,
     721             :                      validate_string_relopt validator)
     722             : {
     723             :     relopt_string *newoption;
     724             : 
     725             :     /* make sure the validator/default combination is sane */
     726           0 :     if (validator)
     727           0 :         (validator) (default_val);
     728             : 
     729           0 :     newoption = (relopt_string *) allocate_reloption(kinds, RELOPT_TYPE_STRING,
     730             :                                                      name, desc);
     731           0 :     newoption->validate_cb = validator;
     732           0 :     if (default_val)
     733             :     {
     734           0 :         newoption->default_val = MemoryContextStrdup(TopMemoryContext,
     735             :                                                      default_val);
     736           0 :         newoption->default_len = strlen(default_val);
     737           0 :         newoption->default_isnull = false;
     738             :     }
     739             :     else
     740             :     {
     741           0 :         newoption->default_val = "";
     742           0 :         newoption->default_len = 0;
     743           0 :         newoption->default_isnull = true;
     744             :     }
     745             : 
     746           0 :     add_reloption((relopt_gen *) newoption);
     747           0 : }
     748             : 
     749             : /*
     750             :  * Transform a relation options list (list of DefElem) into the text array
     751             :  * format that is kept in pg_class.reloptions, including only those options
     752             :  * that are in the passed namespace.  The output values do not include the
     753             :  * namespace.
     754             :  *
     755             :  * This is used for three cases: CREATE TABLE/INDEX, ALTER TABLE SET, and
     756             :  * ALTER TABLE RESET.  In the ALTER cases, oldOptions is the existing
     757             :  * reloptions value (possibly NULL), and we replace or remove entries
     758             :  * as needed.
     759             :  *
     760             :  * If ignoreOids is true, then we should ignore any occurrence of "oids"
     761             :  * in the list (it will be or has been handled by interpretOidsOption()).
     762             :  *
     763             :  * Note that this is not responsible for determining whether the options
     764             :  * are valid, but it does check that namespaces for all the options given are
     765             :  * listed in validnsps.  The NULL namespace is always valid and need not be
     766             :  * explicitly listed.  Passing a NULL pointer means that only the NULL
     767             :  * namespace is valid.
     768             :  *
     769             :  * Both oldOptions and the result are text arrays (or NULL for "default"),
     770             :  * but we declare them as Datums to avoid including array.h in reloptions.h.
     771             :  */
     772             : Datum
     773      115242 : transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
     774             :                     char *validnsps[], bool ignoreOids, bool isReset)
     775             : {
     776             :     Datum       result;
     777             :     ArrayBuildState *astate;
     778             :     ListCell   *cell;
     779             : 
     780             :     /* no change if empty list */
     781      115242 :     if (defList == NIL)
     782      108128 :         return oldOptions;
     783             : 
     784             :     /* We build new array using accumArrayResult */
     785        7114 :     astate = NULL;
     786             : 
     787             :     /* Copy any oldOptions that aren't to be replaced */
     788        7114 :     if (PointerIsValid(DatumGetPointer(oldOptions)))
     789             :     {
     790         144 :         ArrayType  *array = DatumGetArrayTypeP(oldOptions);
     791             :         Datum      *oldoptions;
     792             :         int         noldoptions;
     793             :         int         i;
     794             : 
     795         144 :         deconstruct_array(array, TEXTOID, -1, false, 'i',
     796             :                           &oldoptions, NULL, &noldoptions);
     797             : 
     798         336 :         for (i = 0; i < noldoptions; i++)
     799             :         {
     800         192 :             char       *text_str = VARDATA(oldoptions[i]);
     801         192 :             int         text_len = VARSIZE(oldoptions[i]) - VARHDRSZ;
     802             : 
     803             :             /* Search for a match in defList */
     804         276 :             foreach(cell, defList)
     805             :             {
     806         220 :                 DefElem    *def = (DefElem *) lfirst(cell);
     807             :                 int         kw_len;
     808             : 
     809             :                 /* ignore if not in the same namespace */
     810         220 :                 if (namspace == NULL)
     811             :                 {
     812         200 :                     if (def->defnamespace != NULL)
     813           0 :                         continue;
     814             :                 }
     815          20 :                 else if (def->defnamespace == NULL)
     816          12 :                     continue;
     817           8 :                 else if (strcmp(def->defnamespace, namspace) != 0)
     818           0 :                     continue;
     819             : 
     820         208 :                 kw_len = strlen(def->defname);
     821         344 :                 if (text_len > kw_len && text_str[kw_len] == '=' &&
     822         136 :                     strncmp(text_str, def->defname, kw_len) == 0)
     823         136 :                     break;
     824             :             }
     825         192 :             if (!cell)
     826             :             {
     827             :                 /* No match, so keep old option */
     828          56 :                 astate = accumArrayResult(astate, oldoptions[i],
     829             :                                           false, TEXTOID,
     830             :                                           CurrentMemoryContext);
     831             :             }
     832             :         }
     833             :     }
     834             : 
     835             :     /*
     836             :      * If CREATE/SET, add new options to array; if RESET, just check that the
     837             :      * user didn't say RESET (option=val).  (Must do this because the grammar
     838             :      * doesn't enforce it.)
     839             :      */
     840       14358 :     foreach(cell, defList)
     841             :     {
     842        7260 :         DefElem    *def = (DefElem *) lfirst(cell);
     843             : 
     844        7260 :         if (isReset)
     845             :         {
     846         132 :             if (def->arg != NULL)
     847           8 :                 ereport(ERROR,
     848             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     849             :                          errmsg("RESET must not include values for parameters")));
     850             :         }
     851             :         else
     852             :         {
     853             :             text       *t;
     854             :             const char *value;
     855             :             Size        len;
     856             : 
     857             :             /*
     858             :              * Error out if the namespace is not valid.  A NULL namespace is
     859             :              * always valid.
     860             :              */
     861        7128 :             if (def->defnamespace != NULL)
     862             :             {
     863          52 :                 bool        valid = false;
     864             :                 int         i;
     865             : 
     866          52 :                 if (validnsps)
     867             :                 {
     868          52 :                     for (i = 0; validnsps[i]; i++)
     869             :                     {
     870          48 :                         if (strcmp(def->defnamespace, validnsps[i]) == 0)
     871             :                         {
     872          44 :                             valid = true;
     873          44 :                             break;
     874             :                         }
     875             :                     }
     876             :                 }
     877             : 
     878          52 :                 if (!valid)
     879           8 :                     ereport(ERROR,
     880             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     881             :                              errmsg("unrecognized parameter namespace \"%s\"",
     882             :                                     def->defnamespace)));
     883             :             }
     884             : 
     885        7120 :             if (ignoreOids && strcmp(def->defname, "oids") == 0)
     886        5656 :                 continue;
     887             : 
     888             :             /* ignore if not in the same namespace */
     889        1464 :             if (namspace == NULL)
     890             :             {
     891        1238 :                 if (def->defnamespace != NULL)
     892          22 :                     continue;
     893             :             }
     894         226 :             else if (def->defnamespace == NULL)
     895         204 :                 continue;
     896          22 :             else if (strcmp(def->defnamespace, namspace) != 0)
     897           0 :                 continue;
     898             : 
     899             :             /*
     900             :              * Flatten the DefElem into a text string like "name=arg". If we
     901             :              * have just "name", assume "name=true" is meant.  Note: the
     902             :              * namespace is not output.
     903             :              */
     904        1238 :             if (def->arg != NULL)
     905         894 :                 value = defGetString(def);
     906             :             else
     907         344 :                 value = "true";
     908        1238 :             len = VARHDRSZ + strlen(def->defname) + 1 + strlen(value);
     909             :             /* +1 leaves room for sprintf's trailing null */
     910        1238 :             t = (text *) palloc(len + 1);
     911        1238 :             SET_VARSIZE(t, len);
     912        1238 :             sprintf(VARDATA(t), "%s=%s", def->defname, value);
     913             : 
     914        1238 :             astate = accumArrayResult(astate, PointerGetDatum(t),
     915             :                                       false, TEXTOID,
     916             :                                       CurrentMemoryContext);
     917             :         }
     918             :     }
     919             : 
     920        7098 :     if (astate)
     921        1176 :         result = makeArrayResult(astate, CurrentMemoryContext);
     922             :     else
     923        5922 :         result = (Datum) 0;
     924             : 
     925        7098 :     return result;
     926             : }
     927             : 
     928             : 
     929             : /*
     930             :  * Convert the text-array format of reloptions into a List of DefElem.
     931             :  * This is the inverse of transformRelOptions().
     932             :  */
     933             : List *
     934       16752 : untransformRelOptions(Datum options)
     935             : {
     936       16752 :     List       *result = NIL;
     937             :     ArrayType  *array;
     938             :     Datum      *optiondatums;
     939             :     int         noptions;
     940             :     int         i;
     941             : 
     942             :     /* Nothing to do if no options */
     943       16752 :     if (!PointerIsValid(DatumGetPointer(options)))
     944         874 :         return result;
     945             : 
     946       15878 :     array = DatumGetArrayTypeP(options);
     947             : 
     948       15878 :     deconstruct_array(array, TEXTOID, -1, false, 'i',
     949             :                       &optiondatums, NULL, &noptions);
     950             : 
     951       46102 :     for (i = 0; i < noptions; i++)
     952             :     {
     953             :         char       *s;
     954             :         char       *p;
     955       30224 :         Node       *val = NULL;
     956             : 
     957       30224 :         s = TextDatumGetCString(optiondatums[i]);
     958       30224 :         p = strchr(s, '=');
     959       30224 :         if (p)
     960             :         {
     961       30224 :             *p++ = '\0';
     962       30224 :             val = (Node *) makeString(pstrdup(p));
     963             :         }
     964       30224 :         result = lappend(result, makeDefElem(pstrdup(s), val, -1));
     965             :     }
     966             : 
     967       15878 :     return result;
     968             : }
     969             : 
     970             : /*
     971             :  * Extract and parse reloptions from a pg_class tuple.
     972             :  *
     973             :  * This is a low-level routine, expected to be used by relcache code and
     974             :  * callers that do not have a table's relcache entry (e.g. autovacuum).  For
     975             :  * other uses, consider grabbing the rd_options pointer from the relcache entry
     976             :  * instead.
     977             :  *
     978             :  * tupdesc is pg_class' tuple descriptor.  amoptions is a pointer to the index
     979             :  * AM's options parser function in the case of a tuple corresponding to an
     980             :  * index, or NULL otherwise.
     981             :  */
     982             : bytea *
     983      664040 : extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
     984             :                   amoptions_function amoptions)
     985             : {
     986             :     bytea      *options;
     987             :     bool        isnull;
     988             :     Datum       datum;
     989             :     Form_pg_class classForm;
     990             : 
     991      664040 :     datum = fastgetattr(tuple,
     992             :                         Anum_pg_class_reloptions,
     993             :                         tupdesc,
     994             :                         &isnull);
     995      664040 :     if (isnull)
     996      658622 :         return NULL;
     997             : 
     998        5418 :     classForm = (Form_pg_class) GETSTRUCT(tuple);
     999             : 
    1000             :     /* Parse into appropriate format; don't error out here */
    1001        5418 :     switch (classForm->relkind)
    1002             :     {
    1003             :         case RELKIND_RELATION:
    1004             :         case RELKIND_TOASTVALUE:
    1005             :         case RELKIND_MATVIEW:
    1006             :         case RELKIND_PARTITIONED_TABLE:
    1007        3526 :             options = heap_reloptions(classForm->relkind, datum, false);
    1008        3526 :             break;
    1009             :         case RELKIND_VIEW:
    1010         882 :             options = view_reloptions(datum, false);
    1011         882 :             break;
    1012             :         case RELKIND_INDEX:
    1013             :         case RELKIND_PARTITIONED_INDEX:
    1014        1010 :             options = index_reloptions(amoptions, datum, false);
    1015        1010 :             break;
    1016             :         case RELKIND_FOREIGN_TABLE:
    1017           0 :             options = NULL;
    1018           0 :             break;
    1019             :         default:
    1020             :             Assert(false);      /* can't get here */
    1021           0 :             options = NULL;     /* keep compiler quiet */
    1022           0 :             break;
    1023             :     }
    1024             : 
    1025        5418 :     return options;
    1026             : }
    1027             : 
    1028             : /*
    1029             :  * Interpret reloptions that are given in text-array format.
    1030             :  *
    1031             :  * options is a reloption text array as constructed by transformRelOptions.
    1032             :  * kind specifies the family of options to be processed.
    1033             :  *
    1034             :  * The return value is a relopt_value * array on which the options actually
    1035             :  * set in the options array are marked with isset=true.  The length of this
    1036             :  * array is returned in *numrelopts.  Options not set are also present in the
    1037             :  * array; this is so that the caller can easily locate the default values.
    1038             :  *
    1039             :  * If there are no options of the given kind, numrelopts is set to 0 and NULL
    1040             :  * is returned (unless options are illegally supplied despite none being
    1041             :  * defined, in which case an error occurs).
    1042             :  *
    1043             :  * Note: values of type int, bool and real are allocated as part of the
    1044             :  * returned array.  Values of type string are allocated separately and must
    1045             :  * be freed by the caller.
    1046             :  */
    1047             : relopt_value *
    1048       78490 : parseRelOptions(Datum options, bool validate, relopt_kind kind,
    1049             :                 int *numrelopts)
    1050             : {
    1051       78490 :     relopt_value *reloptions = NULL;
    1052       78490 :     int         numoptions = 0;
    1053             :     int         i;
    1054             :     int         j;
    1055             : 
    1056       78490 :     if (need_initialization)
    1057        3290 :         initialize_reloptions();
    1058             : 
    1059             :     /* Build a list of expected options, based on kind */
    1060             : 
    1061     2905582 :     for (i = 0; relOpts[i]; i++)
    1062     2827092 :         if (relOpts[i]->kinds & kind)
    1063      664594 :             numoptions++;
    1064             : 
    1065       78490 :     if (numoptions > 0)
    1066             :     {
    1067       77028 :         reloptions = palloc(numoptions * sizeof(relopt_value));
    1068             : 
    1069     2851488 :         for (i = 0, j = 0; relOpts[i]; i++)
    1070             :         {
    1071     2774460 :             if (relOpts[i]->kinds & kind)
    1072             :             {
    1073      664594 :                 reloptions[j].gen = relOpts[i];
    1074      664594 :                 reloptions[j].isset = false;
    1075      664594 :                 j++;
    1076             :             }
    1077             :         }
    1078             :     }
    1079             : 
    1080             :     /* Done if no options */
    1081       78490 :     if (PointerIsValid(DatumGetPointer(options)))
    1082             :     {
    1083        6610 :         ArrayType  *array = DatumGetArrayTypeP(options);
    1084             :         Datum      *optiondatums;
    1085             :         int         noptions;
    1086             : 
    1087        6610 :         deconstruct_array(array, TEXTOID, -1, false, 'i',
    1088             :                           &optiondatums, NULL, &noptions);
    1089             : 
    1090       13790 :         for (i = 0; i < noptions; i++)
    1091             :         {
    1092        7336 :             char       *text_str = VARDATA(optiondatums[i]);
    1093        7336 :             int         text_len = VARSIZE(optiondatums[i]) - VARHDRSZ;
    1094             :             int         j;
    1095             : 
    1096             :             /* Search for a match in reloptions */
    1097       37296 :             for (j = 0; j < numoptions; j++)
    1098             :             {
    1099       37256 :                 int         kw_len = reloptions[j].gen->namelen;
    1100             : 
    1101       44698 :                 if (text_len > kw_len && text_str[kw_len] == '=' &&
    1102        7442 :                     strncmp(text_str, reloptions[j].gen->name, kw_len) == 0)
    1103             :                 {
    1104        7296 :                     parse_one_reloption(&reloptions[j], text_str, text_len,
    1105             :                                         validate);
    1106        7180 :                     break;
    1107             :                 }
    1108             :             }
    1109             : 
    1110        7220 :             if (j >= numoptions && validate)
    1111             :             {
    1112             :                 char       *s;
    1113             :                 char       *p;
    1114             : 
    1115          40 :                 s = TextDatumGetCString(optiondatums[i]);
    1116          40 :                 p = strchr(s, '=');
    1117          40 :                 if (p)
    1118          40 :                     *p = '\0';
    1119          40 :                 ereport(ERROR,
    1120             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1121             :                          errmsg("unrecognized parameter \"%s\"", s)));
    1122             :             }
    1123             :         }
    1124             : 
    1125             :         /* It's worth avoiding memory leaks in this function */
    1126        6454 :         pfree(optiondatums);
    1127        6454 :         if (((void *) array) != DatumGetPointer(options))
    1128        5434 :             pfree(array);
    1129             :     }
    1130             : 
    1131       78334 :     *numrelopts = numoptions;
    1132       78334 :     return reloptions;
    1133             : }
    1134             : 
    1135             : /*
    1136             :  * Subroutine for parseRelOptions, to parse and validate a single option's
    1137             :  * value
    1138             :  */
    1139             : static void
    1140        7296 : parse_one_reloption(relopt_value *option, char *text_str, int text_len,
    1141             :                     bool validate)
    1142             : {
    1143             :     char       *value;
    1144             :     int         value_len;
    1145             :     bool        parsed;
    1146        7296 :     bool        nofree = false;
    1147             : 
    1148        7296 :     if (option->isset && validate)
    1149           4 :         ereport(ERROR,
    1150             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1151             :                  errmsg("parameter \"%s\" specified more than once",
    1152             :                         option->gen->name)));
    1153             : 
    1154        7292 :     value_len = text_len - option->gen->namelen - 1;
    1155        7292 :     value = (char *) palloc(value_len + 1);
    1156        7292 :     memcpy(value, text_str + option->gen->namelen + 1, value_len);
    1157        7292 :     value[value_len] = '\0';
    1158             : 
    1159        7292 :     switch (option->gen->type)
    1160             :     {
    1161             :         case RELOPT_TYPE_BOOL:
    1162             :             {
    1163        2740 :                 parsed = parse_bool(value, &option->values.bool_val);
    1164        2740 :                 if (validate && !parsed)
    1165          16 :                     ereport(ERROR,
    1166             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1167             :                              errmsg("invalid value for boolean option \"%s\": %s",
    1168             :                                     option->gen->name, value)));
    1169             :             }
    1170        2724 :             break;
    1171             :         case RELOPT_TYPE_INT:
    1172             :             {
    1173        3902 :                 relopt_int *optint = (relopt_int *) option->gen;
    1174             : 
    1175        3902 :                 parsed = parse_int(value, &option->values.int_val, 0, NULL);
    1176        3902 :                 if (validate && !parsed)
    1177          16 :                     ereport(ERROR,
    1178             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1179             :                              errmsg("invalid value for integer option \"%s\": %s",
    1180             :                                     option->gen->name, value)));
    1181        4208 :                 if (validate && (option->values.int_val < optint->min ||
    1182         322 :                                  option->values.int_val > optint->max))
    1183          44 :                     ereport(ERROR,
    1184             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1185             :                              errmsg("value %s out of bounds for option \"%s\"",
    1186             :                                     value, option->gen->name),
    1187             :                              errdetail("Valid values are between \"%d\" and \"%d\".",
    1188             :                                        optint->min, optint->max)));
    1189             :             }
    1190        3842 :             break;
    1191             :         case RELOPT_TYPE_REAL:
    1192             :             {
    1193         194 :                 relopt_real *optreal = (relopt_real *) option->gen;
    1194             : 
    1195         194 :                 parsed = parse_real(value, &option->values.real_val);
    1196         194 :                 if (validate && !parsed)
    1197          16 :                     ereport(ERROR,
    1198             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1199             :                              errmsg("invalid value for floating point option \"%s\": %s",
    1200             :                                     option->gen->name, value)));
    1201         264 :                 if (validate && (option->values.real_val < optreal->min ||
    1202          86 :                                  option->values.real_val > optreal->max))
    1203          12 :                     ereport(ERROR,
    1204             :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1205             :                              errmsg("value %s out of bounds for option \"%s\"",
    1206             :                                     value, option->gen->name),
    1207             :                              errdetail("Valid values are between \"%f\" and \"%f\".",
    1208             :                                        optreal->min, optreal->max)));
    1209             :             }
    1210         166 :             break;
    1211             :         case RELOPT_TYPE_STRING:
    1212             :             {
    1213         456 :                 relopt_string *optstring = (relopt_string *) option->gen;
    1214             : 
    1215         456 :                 option->values.string_val = value;
    1216         456 :                 nofree = true;
    1217         456 :                 if (validate && optstring->validate_cb)
    1218         110 :                     (optstring->validate_cb) (value);
    1219         448 :                 parsed = true;
    1220             :             }
    1221         448 :             break;
    1222             :         default:
    1223           0 :             elog(ERROR, "unsupported reloption type %d", option->gen->type);
    1224             :             parsed = true;      /* quiet compiler */
    1225             :             break;
    1226             :     }
    1227             : 
    1228        7180 :     if (parsed)
    1229        7180 :         option->isset = true;
    1230        7180 :     if (!nofree)
    1231        6732 :         pfree(value);
    1232        7180 : }
    1233             : 
    1234             : /*
    1235             :  * Given the result from parseRelOptions, allocate a struct that's of the
    1236             :  * specified base size plus any extra space that's needed for string variables.
    1237             :  *
    1238             :  * "base" should be sizeof(struct) of the reloptions struct (StdRdOptions or
    1239             :  * equivalent).
    1240             :  */
    1241             : void *
    1242       76872 : allocateReloptStruct(Size base, relopt_value *options, int numoptions)
    1243             : {
    1244       76872 :     Size        size = base;
    1245             :     int         i;
    1246             : 
    1247      739870 :     for (i = 0; i < numoptions; i++)
    1248      662998 :         if (options[i].gen->type == RELOPT_TYPE_STRING)
    1249       37162 :             size += GET_STRING_RELOPTION_LEN(options[i]) + 1;
    1250             : 
    1251       76872 :     return palloc0(size);
    1252             : }
    1253             : 
    1254             : /*
    1255             :  * Given the result of parseRelOptions and a parsing table, fill in the
    1256             :  * struct (previously allocated with allocateReloptStruct) with the parsed
    1257             :  * values.
    1258             :  *
    1259             :  * rdopts is the pointer to the allocated struct to be filled.
    1260             :  * basesize is the sizeof(struct) that was passed to allocateReloptStruct.
    1261             :  * options, of length numoptions, is parseRelOptions' output.
    1262             :  * elems, of length numelems, is the table describing the allowed options.
    1263             :  * When validate is true, it is expected that all options appear in elems.
    1264             :  */
    1265             : void
    1266       76872 : fillRelOptions(void *rdopts, Size basesize,
    1267             :                relopt_value *options, int numoptions,
    1268             :                bool validate,
    1269             :                const relopt_parse_elt *elems, int numelems)
    1270             : {
    1271             :     int         i;
    1272       76872 :     int         offset = basesize;
    1273             : 
    1274      739870 :     for (i = 0; i < numoptions; i++)
    1275             :     {
    1276             :         int         j;
    1277      662998 :         bool        found = false;
    1278             : 
    1279     5460682 :         for (j = 0; j < numelems; j++)
    1280             :         {
    1281     5459684 :             if (strcmp(options[i].gen->name, elems[j].optname) == 0)
    1282             :             {
    1283             :                 relopt_string *optstring;
    1284      662000 :                 char       *itempos = ((char *) rdopts) + elems[j].offset;
    1285             :                 char       *string_val;
    1286             : 
    1287      662000 :                 switch (options[i].gen->type)
    1288             :                 {
    1289             :                     case RELOPT_TYPE_BOOL:
    1290      193644 :                         *(bool *) itempos = options[i].isset ?
    1291      190944 :                             options[i].values.bool_val :
    1292       94122 :                             ((relopt_bool *) options[i].gen)->default_val;
    1293       96822 :                         break;
    1294             :                     case RELOPT_TYPE_INT:
    1295      937620 :                         *(int *) itempos = options[i].isset ?
    1296      933782 :                             options[i].values.int_val :
    1297      464972 :                             ((relopt_int *) options[i].gen)->default_val;
    1298      468810 :                         break;
    1299             :                     case RELOPT_TYPE_REAL:
    1300      118412 :                         *(double *) itempos = options[i].isset ?
    1301      118254 :                             options[i].values.real_val :
    1302       59048 :                             ((relopt_real *) options[i].gen)->default_val;
    1303       59206 :                         break;
    1304             :                     case RELOPT_TYPE_STRING:
    1305       37162 :                         optstring = (relopt_string *) options[i].gen;
    1306       37162 :                         if (options[i].isset)
    1307         448 :                             string_val = options[i].values.string_val;
    1308       36714 :                         else if (!optstring->default_isnull)
    1309          32 :                             string_val = optstring->default_val;
    1310             :                         else
    1311       36682 :                             string_val = NULL;
    1312             : 
    1313       37162 :                         if (string_val == NULL)
    1314       36682 :                             *(int *) itempos = 0;
    1315             :                         else
    1316             :                         {
    1317         480 :                             strcpy((char *) rdopts + offset, string_val);
    1318         480 :                             *(int *) itempos = offset;
    1319         480 :                             offset += strlen(string_val) + 1;
    1320             :                         }
    1321       37162 :                         break;
    1322             :                     default:
    1323           0 :                         elog(ERROR, "unsupported reloption type %d",
    1324             :                              options[i].gen->type);
    1325             :                         break;
    1326             :                 }
    1327      662000 :                 found = true;
    1328      662000 :                 break;
    1329             :             }
    1330             :         }
    1331      662998 :         if (validate && !found && options[i].gen->kinds != RELOPT_KIND_INDEX)
    1332           0 :             elog(ERROR, "reloption \"%s\" not found in parse table",
    1333             :                  options[i].gen->name);
    1334             :     }
    1335       76872 :     SET_VARSIZE(rdopts, offset);
    1336       76872 : }
    1337             : 
    1338             : 
    1339             : /*
    1340             :  * Option parser for anything that uses StdRdOptions.
    1341             :  */
    1342             : bytea *
    1343       40364 : default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
    1344             : {
    1345             :     relopt_value *options;
    1346             :     StdRdOptions *rdopts;
    1347             :     int         numoptions;
    1348             :     static const relopt_parse_elt tab[] = {
    1349             :         {"fillfactor", RELOPT_TYPE_INT, offsetof(StdRdOptions, fillfactor)},
    1350             :         {"autovacuum_enabled", RELOPT_TYPE_BOOL,
    1351             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, enabled)},
    1352             :         {"autovacuum_vacuum_threshold", RELOPT_TYPE_INT,
    1353             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_threshold)},
    1354             :         {"autovacuum_analyze_threshold", RELOPT_TYPE_INT,
    1355             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, analyze_threshold)},
    1356             :         {"autovacuum_vacuum_cost_delay", RELOPT_TYPE_INT,
    1357             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_cost_delay)},
    1358             :         {"autovacuum_vacuum_cost_limit", RELOPT_TYPE_INT,
    1359             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_cost_limit)},
    1360             :         {"autovacuum_freeze_min_age", RELOPT_TYPE_INT,
    1361             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_min_age)},
    1362             :         {"autovacuum_freeze_max_age", RELOPT_TYPE_INT,
    1363             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_max_age)},
    1364             :         {"autovacuum_freeze_table_age", RELOPT_TYPE_INT,
    1365             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, freeze_table_age)},
    1366             :         {"autovacuum_multixact_freeze_min_age", RELOPT_TYPE_INT,
    1367             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_min_age)},
    1368             :         {"autovacuum_multixact_freeze_max_age", RELOPT_TYPE_INT,
    1369             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_max_age)},
    1370             :         {"autovacuum_multixact_freeze_table_age", RELOPT_TYPE_INT,
    1371             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, multixact_freeze_table_age)},
    1372             :         {"log_autovacuum_min_duration", RELOPT_TYPE_INT,
    1373             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, log_min_duration)},
    1374             :         {"toast_tuple_target", RELOPT_TYPE_INT,
    1375             :         offsetof(StdRdOptions, toast_tuple_target)},
    1376             :         {"autovacuum_vacuum_scale_factor", RELOPT_TYPE_REAL,
    1377             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, vacuum_scale_factor)},
    1378             :         {"autovacuum_analyze_scale_factor", RELOPT_TYPE_REAL,
    1379             :         offsetof(StdRdOptions, autovacuum) + offsetof(AutoVacOpts, analyze_scale_factor)},
    1380             :         {"user_catalog_table", RELOPT_TYPE_BOOL,
    1381             :         offsetof(StdRdOptions, user_catalog_table)},
    1382             :         {"parallel_workers", RELOPT_TYPE_INT,
    1383             :         offsetof(StdRdOptions, parallel_workers)},
    1384             :         {"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
    1385             :         offsetof(StdRdOptions, vacuum_cleanup_index_scale_factor)}
    1386             :     };
    1387             : 
    1388       40364 :     options = parseRelOptions(reloptions, validate, kind, &numoptions);
    1389             : 
    1390             :     /* if none set, we're done */
    1391       40252 :     if (numoptions == 0)
    1392        1462 :         return NULL;
    1393             : 
    1394       38790 :     rdopts = allocateReloptStruct(sizeof(StdRdOptions), options, numoptions);
    1395             : 
    1396       38790 :     fillRelOptions((void *) rdopts, sizeof(StdRdOptions), options, numoptions,
    1397             :                    validate, tab, lengthof(tab));
    1398             : 
    1399       38790 :     pfree(options);
    1400             : 
    1401       38790 :     return (bytea *) rdopts;
    1402             : }
    1403             : 
    1404             : /*
    1405             :  * Option parser for views
    1406             :  */
    1407             : bytea *
    1408       37102 : view_reloptions(Datum reloptions, bool validate)
    1409             : {
    1410             :     relopt_value *options;
    1411             :     ViewOptions *vopts;
    1412             :     int         numoptions;
    1413             :     static const relopt_parse_elt tab[] = {
    1414             :         {"security_barrier", RELOPT_TYPE_BOOL,
    1415             :         offsetof(ViewOptions, security_barrier)},
    1416             :         {"check_option", RELOPT_TYPE_STRING,
    1417             :         offsetof(ViewOptions, check_option_offset)}
    1418             :     };
    1419             : 
    1420       37102 :     options = parseRelOptions(reloptions, validate, RELOPT_KIND_VIEW, &numoptions);
    1421             : 
    1422             :     /* if none set, we're done */
    1423       37082 :     if (numoptions == 0)
    1424           0 :         return NULL;
    1425             : 
    1426       37082 :     vopts = allocateReloptStruct(sizeof(ViewOptions), options, numoptions);
    1427             : 
    1428       37082 :     fillRelOptions((void *) vopts, sizeof(ViewOptions), options, numoptions,
    1429             :                    validate, tab, lengthof(tab));
    1430             : 
    1431       37082 :     pfree(options);
    1432             : 
    1433       37082 :     return (bytea *) vopts;
    1434             : }
    1435             : 
    1436             : /*
    1437             :  * Parse options for heaps, views and toast tables.
    1438             :  */
    1439             : bytea *
    1440       41452 : heap_reloptions(char relkind, Datum reloptions, bool validate)
    1441             : {
    1442             :     StdRdOptions *rdopts;
    1443             : 
    1444       41452 :     switch (relkind)
    1445             :     {
    1446             :         case RELKIND_TOASTVALUE:
    1447       18050 :             rdopts = (StdRdOptions *)
    1448       18050 :                 default_reloptions(reloptions, validate, RELOPT_KIND_TOAST);
    1449       18046 :             if (rdopts != NULL)
    1450             :             {
    1451             :                 /* adjust default-only parameters for TOAST relations */
    1452       18046 :                 rdopts->fillfactor = 100;
    1453       18046 :                 rdopts->autovacuum.analyze_threshold = -1;
    1454       18046 :                 rdopts->autovacuum.analyze_scale_factor = -1;
    1455             :             }
    1456       18046 :             return (bytea *) rdopts;
    1457             :         case RELKIND_RELATION:
    1458             :         case RELKIND_MATVIEW:
    1459       20500 :             return default_reloptions(reloptions, validate, RELOPT_KIND_HEAP);
    1460             :         case RELKIND_PARTITIONED_TABLE:
    1461        1462 :             return default_reloptions(reloptions, validate,
    1462             :                                       RELOPT_KIND_PARTITIONED);
    1463             :         default:
    1464             :             /* other relkinds are not supported */
    1465        1440 :             return NULL;
    1466             :     }
    1467             : }
    1468             : 
    1469             : 
    1470             : /*
    1471             :  * Parse options for indexes.
    1472             :  *
    1473             :  *  amoptions   index AM's option parser function
    1474             :  *  reloptions  options as text[] datum
    1475             :  *  validate    error flag
    1476             :  */
    1477             : bytea *
    1478       42032 : index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
    1479             : {
    1480             :     Assert(amoptions != NULL);
    1481             : 
    1482             :     /* Assume function is strict */
    1483       42032 :     if (!PointerIsValid(DatumGetPointer(reloptions)))
    1484       40730 :         return NULL;
    1485             : 
    1486        1302 :     return amoptions(reloptions, validate);
    1487             : }
    1488             : 
    1489             : /*
    1490             :  * Parse generic options for all indexes.
    1491             :  *
    1492             :  *  reloptions  options as text[] datum
    1493             :  *  validate    error flag
    1494             :  */
    1495             : bytea *
    1496           8 : index_generic_reloptions(Datum reloptions, bool validate)
    1497             : {
    1498             :     int         numoptions;
    1499             :     GenericIndexOpts *idxopts;
    1500             :     relopt_value *options;
    1501             :     static const relopt_parse_elt tab[] = {
    1502             :         {"recheck_on_update", RELOPT_TYPE_BOOL, offsetof(GenericIndexOpts, recheck_on_update)}
    1503             :     };
    1504             : 
    1505           8 :     options = parseRelOptions(reloptions, validate,
    1506             :                               RELOPT_KIND_INDEX,
    1507             :                               &numoptions);
    1508             : 
    1509             :     /* if none set, we're done */
    1510           8 :     if (numoptions == 0)
    1511           0 :         return NULL;
    1512             : 
    1513           8 :     idxopts = allocateReloptStruct(sizeof(GenericIndexOpts), options, numoptions);
    1514             : 
    1515           8 :     fillRelOptions((void *) idxopts, sizeof(GenericIndexOpts), options, numoptions,
    1516             :                    validate, tab, lengthof(tab));
    1517             : 
    1518           8 :     pfree(options);
    1519             : 
    1520           8 :     return (bytea *) idxopts;
    1521             : }
    1522             : 
    1523             : /*
    1524             :  * Option parser for attribute reloptions
    1525             :  */
    1526             : bytea *
    1527          26 : attribute_reloptions(Datum reloptions, bool validate)
    1528             : {
    1529             :     relopt_value *options;
    1530             :     AttributeOpts *aopts;
    1531             :     int         numoptions;
    1532             :     static const relopt_parse_elt tab[] = {
    1533             :         {"n_distinct", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct)},
    1534             :         {"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)}
    1535             :     };
    1536             : 
    1537          26 :     options = parseRelOptions(reloptions, validate, RELOPT_KIND_ATTRIBUTE,
    1538             :                               &numoptions);
    1539             : 
    1540             :     /* if none set, we're done */
    1541          26 :     if (numoptions == 0)
    1542           0 :         return NULL;
    1543             : 
    1544          26 :     aopts = allocateReloptStruct(sizeof(AttributeOpts), options, numoptions);
    1545             : 
    1546          26 :     fillRelOptions((void *) aopts, sizeof(AttributeOpts), options, numoptions,
    1547             :                    validate, tab, lengthof(tab));
    1548             : 
    1549          26 :     pfree(options);
    1550             : 
    1551          26 :     return (bytea *) aopts;
    1552             : }
    1553             : 
    1554             : /*
    1555             :  * Option parser for tablespace reloptions
    1556             :  */
    1557             : bytea *
    1558          40 : tablespace_reloptions(Datum reloptions, bool validate)
    1559             : {
    1560             :     relopt_value *options;
    1561             :     TableSpaceOpts *tsopts;
    1562             :     int         numoptions;
    1563             :     static const relopt_parse_elt tab[] = {
    1564             :         {"random_page_cost", RELOPT_TYPE_REAL, offsetof(TableSpaceOpts, random_page_cost)},
    1565             :         {"seq_page_cost", RELOPT_TYPE_REAL, offsetof(TableSpaceOpts, seq_page_cost)},
    1566             :         {"effective_io_concurrency", RELOPT_TYPE_INT, offsetof(TableSpaceOpts, effective_io_concurrency)}
    1567             :     };
    1568             : 
    1569          40 :     options = parseRelOptions(reloptions, validate, RELOPT_KIND_TABLESPACE,
    1570             :                               &numoptions);
    1571             : 
    1572             :     /* if none set, we're done */
    1573          32 :     if (numoptions == 0)
    1574           0 :         return NULL;
    1575             : 
    1576          32 :     tsopts = allocateReloptStruct(sizeof(TableSpaceOpts), options, numoptions);
    1577             : 
    1578          32 :     fillRelOptions((void *) tsopts, sizeof(TableSpaceOpts), options, numoptions,
    1579             :                    validate, tab, lengthof(tab));
    1580             : 
    1581          32 :     pfree(options);
    1582             : 
    1583          32 :     return (bytea *) tsopts;
    1584             : }
    1585             : 
    1586             : /*
    1587             :  * Determine the required LOCKMODE from an option list.
    1588             :  *
    1589             :  * Called from AlterTableGetLockLevel(), see that function
    1590             :  * for a longer explanation of how this works.
    1591             :  */
    1592             : LOCKMODE
    1593         328 : AlterTableGetRelOptionsLockLevel(List *defList)
    1594             : {
    1595         328 :     LOCKMODE    lockmode = NoLock;
    1596             :     ListCell   *cell;
    1597             : 
    1598         328 :     if (defList == NIL)
    1599           0 :         return AccessExclusiveLock;
    1600             : 
    1601         328 :     if (need_initialization)
    1602           6 :         initialize_reloptions();
    1603             : 
    1604         672 :     foreach(cell, defList)
    1605             :     {
    1606         344 :         DefElem    *def = (DefElem *) lfirst(cell);
    1607             :         int         i;
    1608             : 
    1609       12728 :         for (i = 0; relOpts[i]; i++)
    1610             :         {
    1611       24768 :             if (strncmp(relOpts[i]->name,
    1612       12384 :                         def->defname,
    1613       12384 :                         relOpts[i]->namelen + 1) == 0)
    1614             :             {
    1615         536 :                 if (lockmode < relOpts[i]->lockmode)
    1616         328 :                     lockmode = relOpts[i]->lockmode;
    1617             :             }
    1618             :         }
    1619             :     }
    1620             : 
    1621         328 :     return lockmode;
    1622             : }

Generated by: LCOV version 1.13