@@ -1474,9 +1474,6 @@ fillRelOptions(void *rdopts, Size basesize,
14741474bytea *
14751475default_reloptions (Datum reloptions , bool validate , relopt_kind kind )
14761476{
1477- relopt_value * options ;
1478- StdRdOptions * rdopts ;
1479- int numoptions ;
14801477 static const relopt_parse_elt tab [] = {
14811478 {"fillfactor" , RELOPT_TYPE_INT , offsetof(StdRdOptions , fillfactor )},
14821479 {"autovacuum_enabled" , RELOPT_TYPE_BOOL ,
@@ -1521,20 +1518,57 @@ default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
15211518 offsetof(StdRdOptions , vacuum_truncate )}
15221519 };
15231520
1521+ return (bytea * ) build_reloptions (reloptions , validate , kind ,
1522+ sizeof (StdRdOptions ),
1523+ tab , lengthof (tab ));
1524+ }
1525+
1526+ /*
1527+ * build_reloptions
1528+ *
1529+ * Parses "reloptions" provided by the caller, returning them in a
1530+ * structure containing the parsed options. The parsing is done with
1531+ * the help of a parsing table describing the allowed options, defined
1532+ * by "relopt_elems" of length "num_relopt_elems".
1533+ *
1534+ * "validate" must be true if reloptions value is freshly built by
1535+ * transformRelOptions(), as opposed to being read from the catalog, in which
1536+ * case the values contained in it must already be valid.
1537+ *
1538+ * NULL is returned if the passed-in options did not match any of the options
1539+ * in the parsing table, unless validate is true in which case an error would
1540+ * be reported.
1541+ */
1542+ void *
1543+ build_reloptions (Datum reloptions , bool validate ,
1544+ relopt_kind kind ,
1545+ Size relopt_struct_size ,
1546+ const relopt_parse_elt * relopt_elems ,
1547+ int num_relopt_elems )
1548+ {
1549+ int numoptions ;
1550+ relopt_value * options ;
1551+ void * rdopts ;
1552+
1553+ /* parse options specific to given relation option kind */
15241554 options = parseRelOptions (reloptions , validate , kind , & numoptions );
1555+ Assert (numoptions <= num_relopt_elems );
15251556
15261557 /* if none set, we're done */
15271558 if (numoptions == 0 )
1559+ {
1560+ Assert (options == NULL );
15281561 return NULL ;
1562+ }
15291563
1530- rdopts = allocateReloptStruct ( sizeof ( StdRdOptions ), options , numoptions );
1531-
1532- fillRelOptions (( void * ) rdopts , sizeof ( StdRdOptions ) , options , numoptions ,
1533- validate , tab , lengthof ( tab ) );
1564+ /* allocate and fill the structure */
1565+ rdopts = allocateReloptStruct ( relopt_struct_size , options , numoptions );
1566+ fillRelOptions (rdopts , relopt_struct_size , options , numoptions ,
1567+ validate , relopt_elems , num_relopt_elems );
15341568
15351569 pfree (options );
15361570
1537- return ( bytea * ) rdopts ;
1571+ return rdopts ;
15381572}
15391573
15401574/*
@@ -1543,30 +1577,17 @@ default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
15431577bytea *
15441578view_reloptions (Datum reloptions , bool validate )
15451579{
1546- relopt_value * options ;
1547- ViewOptions * vopts ;
1548- int numoptions ;
15491580 static const relopt_parse_elt tab [] = {
15501581 {"security_barrier" , RELOPT_TYPE_BOOL ,
15511582 offsetof(ViewOptions , security_barrier )},
15521583 {"check_option" , RELOPT_TYPE_ENUM ,
15531584 offsetof(ViewOptions , check_option )}
15541585 };
15551586
1556- options = parseRelOptions (reloptions , validate , RELOPT_KIND_VIEW , & numoptions );
1557-
1558- /* if none set, we're done */
1559- if (numoptions == 0 )
1560- return NULL ;
1561-
1562- vopts = allocateReloptStruct (sizeof (ViewOptions ), options , numoptions );
1563-
1564- fillRelOptions ((void * ) vopts , sizeof (ViewOptions ), options , numoptions ,
1565- validate , tab , lengthof (tab ));
1566-
1567- pfree (options );
1568-
1569- return (bytea * ) vopts ;
1587+ return (bytea * ) build_reloptions (reloptions , validate ,
1588+ RELOPT_KIND_VIEW ,
1589+ sizeof (ViewOptions ),
1590+ tab , lengthof (tab ));
15701591}
15711592
15721593/*
@@ -1628,29 +1649,15 @@ index_reloptions(amoptions_function amoptions, Datum reloptions, bool validate)
16281649bytea *
16291650attribute_reloptions (Datum reloptions , bool validate )
16301651{
1631- relopt_value * options ;
1632- AttributeOpts * aopts ;
1633- int numoptions ;
16341652 static const relopt_parse_elt tab [] = {
16351653 {"n_distinct" , RELOPT_TYPE_REAL , offsetof(AttributeOpts , n_distinct )},
16361654 {"n_distinct_inherited" , RELOPT_TYPE_REAL , offsetof(AttributeOpts , n_distinct_inherited )}
16371655 };
16381656
1639- options = parseRelOptions (reloptions , validate , RELOPT_KIND_ATTRIBUTE ,
1640- & numoptions );
1641-
1642- /* if none set, we're done */
1643- if (numoptions == 0 )
1644- return NULL ;
1645-
1646- aopts = allocateReloptStruct (sizeof (AttributeOpts ), options , numoptions );
1647-
1648- fillRelOptions ((void * ) aopts , sizeof (AttributeOpts ), options , numoptions ,
1649- validate , tab , lengthof (tab ));
1650-
1651- pfree (options );
1652-
1653- return (bytea * ) aopts ;
1657+ return (bytea * ) build_reloptions (reloptions , validate ,
1658+ RELOPT_KIND_ATTRIBUTE ,
1659+ sizeof (AttributeOpts ),
1660+ tab , lengthof (tab ));
16541661}
16551662
16561663/*
@@ -1659,30 +1666,16 @@ attribute_reloptions(Datum reloptions, bool validate)
16591666bytea *
16601667tablespace_reloptions (Datum reloptions , bool validate )
16611668{
1662- relopt_value * options ;
1663- TableSpaceOpts * tsopts ;
1664- int numoptions ;
16651669 static const relopt_parse_elt tab [] = {
16661670 {"random_page_cost" , RELOPT_TYPE_REAL , offsetof(TableSpaceOpts , random_page_cost )},
16671671 {"seq_page_cost" , RELOPT_TYPE_REAL , offsetof(TableSpaceOpts , seq_page_cost )},
16681672 {"effective_io_concurrency" , RELOPT_TYPE_INT , offsetof(TableSpaceOpts , effective_io_concurrency )}
16691673 };
16701674
1671- options = parseRelOptions (reloptions , validate , RELOPT_KIND_TABLESPACE ,
1672- & numoptions );
1673-
1674- /* if none set, we're done */
1675- if (numoptions == 0 )
1676- return NULL ;
1677-
1678- tsopts = allocateReloptStruct (sizeof (TableSpaceOpts ), options , numoptions );
1679-
1680- fillRelOptions ((void * ) tsopts , sizeof (TableSpaceOpts ), options , numoptions ,
1681- validate , tab , lengthof (tab ));
1682-
1683- pfree (options );
1684-
1685- return (bytea * ) tsopts ;
1675+ return (bytea * ) build_reloptions (reloptions , validate ,
1676+ RELOPT_KIND_TABLESPACE ,
1677+ sizeof (TableSpaceOpts ),
1678+ tab , lengthof (tab ));
16861679}
16871680
16881681/*
0 commit comments