diff --git a/doc/src/sgml/ref/alter_index.sgml b/doc/src/sgml/ref/alter_index.sgml index d210077..5e9ee9d 100644 --- a/doc/src/sgml/ref/alter_index.sgml +++ b/doc/src/sgml/ref/alter_index.sgml @@ -82,6 +82,14 @@ ALTER INDEX [ IF EXISTS ] name RESE to get the desired effects. + + + A custom name can be used as namespace to define a storage parameter. + Storage option pattern: namespace.option=value + (namespace=custom name, option=option name and value=option value). + See example bellow. + + @@ -202,6 +210,17 @@ ALTER INDEX distributors SET (fillfactor = 75); REINDEX INDEX distributors; + + To set a custom storage parameter: + +ALTER INDEX distributors + SET (bdr.do_replicate=true); + + (bdr=custom name, do_replicate=option name and + true=option value) + + + diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index 89649a2..6fd9d67 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -213,6 +213,16 @@ ALTER TABLE [ IF EXISTS ] name of statistics by the PostgreSQL query planner, refer to . + + + + A custom name can be used as namespace to define a storage parameter. + Storage option pattern: namespace.option=value + (namespace=custom name, option=option name and value=option value). + See example bellow. + + + @@ -476,6 +486,10 @@ ALTER TABLE [ IF EXISTS ] name ALTER TABLE does not treat OIDS as a storage parameter. Instead use the SET WITH OIDS and SET WITHOUT OIDS forms to change OID status. + A custom name can be used as namespace to define a storage parameter. + Storage option pattern: namespace.option=value + (namespace=custom name, option=option name and value=option value). + See example bellow. @@ -1112,6 +1126,26 @@ ALTER TABLE distributors DROP CONSTRAINT distributors_pkey, ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx; + + To set a custom per-attribute option: + +ALTER TABLE distributors + ALTER COLUMN dist_id SET (bdr.do_replicate=true); + + (bdr=custom name, do_replicate=option name and + true=option value) + + + + To set a custom storage parameter: + +ALTER TABLE distributors + SET (bdr.do_replicate=true); + + (bdr=custom name, do_replicate=option name and + true=option value) + + diff --git a/src/backend/access/common/reloptions.c b/src/backend/access/common/reloptions.c index fa08c45..7de2003 100644 --- a/src/backend/access/common/reloptions.c +++ b/src/backend/access/common/reloptions.c @@ -283,6 +283,9 @@ static void initialize_reloptions(void); static void parse_one_reloption(relopt_value *option, char *text_str, int text_len, bool validate); +static bool is_custom_namespace(char *namespace); +static bool is_reserved_namespace(char *namespace); + /* * initialize_reloptions * initialization routine, must be called before parsing @@ -610,13 +613,15 @@ transformRelOptions(Datum oldOptions, List *defList, char *namspace, /* Search for a match in defList */ foreach(cell, defList) { - DefElem *def = (DefElem *) lfirst(cell); + DefElem *def = (DefElem *) lfirst(cell); int kw_len; + char *text_compare; /* ignore if not in the same namespace */ if (namspace == NULL) { - if (def->defnamespace != NULL) + if (def->defnamespace != NULL && + !is_custom_namespace(def->defnamespace)) continue; } else if (def->defnamespace == NULL) @@ -625,8 +630,17 @@ transformRelOptions(Datum oldOptions, List *defList, char *namspace, continue; kw_len = strlen(def->defname); + if (is_custom_namespace(def->defnamespace)) + kw_len += strlen(def->defnamespace) + 1; + + text_compare = (char *) palloc(kw_len + 1); + if (is_custom_namespace(def->defnamespace)) + sprintf(text_compare, "%s.%s", def->defnamespace, def->defname); + else + sprintf(text_compare, "%s", def->defname); + if (text_len > kw_len && text_str[kw_len] == '=' && - pg_strncasecmp(text_str, def->defname, kw_len) == 0) + pg_strncasecmp(text_str, text_compare, kw_len) == 0) break; } if (!cell) @@ -668,22 +682,11 @@ transformRelOptions(Datum oldOptions, List *defList, char *namspace, if (def->defnamespace != NULL) { bool valid = false; - int i; if (validnsps) - { - for (i = 0; validnsps[i]; i++) - { - if (pg_strcasecmp(def->defnamespace, - validnsps[i]) == 0) - { - valid = true; - break; - } - } - } + valid = is_reserved_namespace(def->defnamespace); - if (!valid) + if (!valid && !is_custom_namespace(def->defnamespace)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("unrecognized parameter namespace \"%s\"", @@ -696,7 +699,8 @@ transformRelOptions(Datum oldOptions, List *defList, char *namspace, /* ignore if not in the same namespace */ if (namspace == NULL) { - if (def->defnamespace != NULL) + if (def->defnamespace != NULL && + !is_custom_namespace(def->defnamespace)) continue; } else if (def->defnamespace == NULL) @@ -714,10 +718,18 @@ transformRelOptions(Datum oldOptions, List *defList, char *namspace, else value = "true"; len = VARHDRSZ + strlen(def->defname) + 1 + strlen(value); + + if (is_custom_namespace(def->defnamespace)) + len += strlen(def->defnamespace) + 1; + /* +1 leaves room for sprintf's trailing null */ t = (text *) palloc(len + 1); SET_VARSIZE(t, len); - sprintf(VARDATA(t), "%s=%s", def->defname, value); + + if (is_custom_namespace(def->defnamespace)) + sprintf(VARDATA(t), "%s.%s=%s", def->defnamespace, def->defname, value); + else + sprintf(VARDATA(t), "%s=%s", def->defname, value); astate = accumArrayResult(astate, PointerGetDatum(t), false, TEXTOID, @@ -921,13 +933,24 @@ parseRelOptions(Datum options, bool validate, relopt_kind kind, if (j >= numoptions && validate) { - char *s; - char *p; + char *s; + char *n; + char *p; s = TextDatumGetCString(optiondatums[i]); p = strchr(s, '='); if (p) + { *p = '\0'; + n = strchr(s, '.'); + if (n) + { + *n = '\0'; + if (is_custom_namespace(s)) + continue; + } + } + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("unrecognized parameter \"%s\"", s))); @@ -1326,3 +1349,36 @@ tablespace_reloptions(Datum reloptions, bool validate) return (bytea *) tsopts; } + +bool +is_reserved_namespace(char *namespace) +{ + static char *validnsps[] = HEAP_RELOPT_NAMESPACES; + bool is_reserved = false; + int i; + + for (i = 0; validnsps[i]; i++) + { + if (pg_strcasecmp(namespace, + validnsps[i]) == 0) + { + is_reserved = true; + break; + } + } + + return is_reserved; +} + +bool +is_custom_namespace(char *namespace) +{ + + if (namespace == NULL) + return false; + + if (is_reserved_namespace(namespace)) + return false; + + return true; +} diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index 0f0c638..0d15403 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -2370,3 +2370,101 @@ TRUNCATE old_system_table; ALTER TABLE old_system_table DROP CONSTRAINT new_system_table_pkey; ALTER TABLE old_system_table DROP COLUMN othercol; DROP TABLE old_system_table; +-- +-- Custom Option Test +-- +CREATE TABLE custom_reloption_test(id SERIAL PRIMARY KEY); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; + relname | reloptions +----------------------------+------------ + custom_reloption_test | + custom_reloption_test.id | + custom_reloption_test_pkey | +(3 rows) + +ALTER TABLE custom_reloption_test SET (fillfactor=70); +ALTER INDEX custom_reloption_test_pkey SET (fillfactor=80); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (n_distinct=100); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; + relname | reloptions +----------------------------+------------------ + custom_reloption_test | {fillfactor=70} + custom_reloption_test.id | {n_distinct=100} + custom_reloption_test_pkey | {fillfactor=80} +(3 rows) + +ALTER TABLE custom_reloption_test SET (xx.bdr.do_replicate=true); +ERROR: syntax error at or near "." +LINE 1: ALTER TABLE custom_reloption_test SET (xx.bdr.do_replicate=t... + ^ +ALTER INDEX custom_reloption_test_pkey SET (xx.bdr.do_replicate=true); +ERROR: syntax error at or near "." +LINE 1: ALTER INDEX custom_reloption_test_pkey SET (xx.bdr.do_replic... + ^ +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (xx.bdr.do_replicate=true); +ERROR: syntax error at or near "." +LINE 1: ... custom_reloption_test ALTER COLUMN id SET (xx.bdr.do_replic... + ^ +ALTER TABLE custom_reloption_test SET (xx.bdr=true); +ALTER INDEX custom_reloption_test_pkey SET (xx.bdr=true); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (xx.bdr=true); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; + relname | reloptions +----------------------------+------------------------------ + custom_reloption_test | {fillfactor=70,xx.bdr=true} + custom_reloption_test.id | {n_distinct=100,xx.bdr=true} + custom_reloption_test_pkey | {fillfactor=80,xx.bdr=true} +(3 rows) + +-- test extension reloptions +ALTER TABLE custom_reloption_test SET (plpgsql.option=true); +ALTER INDEX custom_reloption_test_pkey SET (plpgsql.option=true); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (plpgsql.option=true); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; + relname | reloptions +----------------------------+-------------------------------------------------- + custom_reloption_test | {fillfactor=70,xx.bdr=true,plpgsql.option=true} + custom_reloption_test.id | {n_distinct=100,xx.bdr=true,plpgsql.option=true} + custom_reloption_test_pkey | {fillfactor=80,xx.bdr=true,plpgsql.option=true} +(3 rows) + +ALTER TABLE custom_reloption_test SET (plpgsql.foo=1234); +ALTER INDEX custom_reloption_test_pkey SET (plpgsql.foo=1234); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (plpgsql.foo=1234); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; + relname | reloptions +----------------------------+------------------------------------------------------------------- + custom_reloption_test | {fillfactor=70,xx.bdr=true,plpgsql.option=true,plpgsql.foo=1234} + custom_reloption_test.id | {n_distinct=100,xx.bdr=true,plpgsql.option=true,plpgsql.foo=1234} + custom_reloption_test_pkey | {fillfactor=80,xx.bdr=true,plpgsql.option=true,plpgsql.foo=1234} +(3 rows) + +ALTER TABLE custom_reloption_test RESET (plpgsql.foo); +ALTER INDEX custom_reloption_test_pkey RESET (plpgsql.foo); +ALTER TABLE custom_reloption_test ALTER COLUMN id RESET (plpgsql.foo); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; + relname | reloptions +----------------------------+-------------------------------------------------- + custom_reloption_test | {fillfactor=70,xx.bdr=true,plpgsql.option=true} + custom_reloption_test.id | {n_distinct=100,xx.bdr=true,plpgsql.option=true} + custom_reloption_test_pkey | {fillfactor=80,xx.bdr=true,plpgsql.option=true} +(3 rows) + diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index 87973c1..9b04c08 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -1594,3 +1594,51 @@ TRUNCATE old_system_table; ALTER TABLE old_system_table DROP CONSTRAINT new_system_table_pkey; ALTER TABLE old_system_table DROP COLUMN othercol; DROP TABLE old_system_table; + +-- +-- Custom Option Test +-- +CREATE TABLE custom_reloption_test(id SERIAL PRIMARY KEY); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; +ALTER TABLE custom_reloption_test SET (fillfactor=70); +ALTER INDEX custom_reloption_test_pkey SET (fillfactor=80); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (n_distinct=100); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; +ALTER TABLE custom_reloption_test SET (xx.bdr.do_replicate=true); +ALTER INDEX custom_reloption_test_pkey SET (xx.bdr.do_replicate=true); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (xx.bdr.do_replicate=true); +ALTER TABLE custom_reloption_test SET (xx.bdr=true); +ALTER INDEX custom_reloption_test_pkey SET (xx.bdr=true); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (xx.bdr=true); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; +-- test extension reloptions +ALTER TABLE custom_reloption_test SET (plpgsql.option=true); +ALTER INDEX custom_reloption_test_pkey SET (plpgsql.option=true); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (plpgsql.option=true); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; +ALTER TABLE custom_reloption_test SET (plpgsql.foo=1234); +ALTER INDEX custom_reloption_test_pkey SET (plpgsql.foo=1234); +ALTER TABLE custom_reloption_test ALTER COLUMN id SET (plpgsql.foo=1234); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1; +ALTER TABLE custom_reloption_test RESET (plpgsql.foo); +ALTER INDEX custom_reloption_test_pkey RESET (plpgsql.foo); +ALTER TABLE custom_reloption_test ALTER COLUMN id RESET (plpgsql.foo); +SELECT relname, reloptions FROM pg_class WHERE oid IN ('custom_reloption_test'::regclass, 'custom_reloption_test_pkey'::regclass) +UNION ALL +SELECT attrelid::regclass||'.'||attname, attoptions FROM pg_attribute WHERE attrelid = 'custom_reloption_test'::regclass AND attname = 'id' +ORDER BY 1;