Re: [HACKERS] renaming domain constraint
On ons, 2012-03-21 at 11:57 -0300, Alvaro Herrera wrote: > Excerpts from Robert Haas's message of mié mar 21 11:43:17 -0300 2012: > > On Fri, Mar 16, 2012 at 1:34 PM, Peter Eisentraut wrote: > > > Here is a patch for being able to rename constraints of domains. It > > > goes on top of the previously committed patch for renaming table > > > constraints. > > > > I don't like the way you've modified get_constraint_oid(), which is > > currently parallel to many other get_whatever_oid() functions and with > > this patch, would no longer be. There seems to be little point in > > shoehorning the new functionality into the existing function anyway, > > considering that you've conditionalized basically every piece of logic > > in the function. I think you should just invent a completely separate > > function and be done with it. > > get_relation_constraint_oid() plus get_domain_constraint_oid()? Makes sense. Updated patch attached. diff --git a/doc/src/sgml/ref/alter_domain.sgml b/doc/src/sgml/ref/alter_domain.sgml index 2511a12..c59975a 100644 --- a/doc/src/sgml/ref/alter_domain.sgml +++ b/doc/src/sgml/ref/alter_domain.sgml @@ -32,6 +32,8 @@ ALTER DOMAIN name ALTER DOMAIN name DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] ALTER DOMAIN name + RENAME CONSTRAINT constraint_name TO new_constraint_name +ALTER DOMAIN name VALIDATE CONSTRAINT constraint_name ALTER DOMAIN name OWNER TO new_owner @@ -103,6 +105,15 @@ ALTER DOMAIN name +RENAME CONSTRAINT + + + This form changes the name of a constraint on a domain. + + + + + VALIDATE CONSTRAINT @@ -182,7 +193,7 @@ ALTER DOMAIN name constraint_name -Name of an existing constraint to drop. +Name of an existing constraint to drop or rename. @@ -226,6 +237,15 @@ ALTER DOMAIN name + new_constraint_name + + +The new name for the constraint. + + + + + new_owner @@ -289,6 +309,13 @@ ALTER DOMAIN zipcode DROP CONSTRAINT zipchk; + To rename a check constraint on a domain: + +ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check; + + + + To move the domain into a different schema: ALTER DOMAIN zipcode SET SCHEMA customers; diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c index e6e0347..250069f 100644 --- a/src/backend/catalog/objectaddress.c +++ b/src/backend/catalog/objectaddress.c @@ -753,7 +753,7 @@ get_object_address_relobject(ObjectType objtype, List *objname, case OBJECT_CONSTRAINT: address.classId = ConstraintRelationId; address.objectId = - get_constraint_oid(reloid, depname, missing_ok); + get_relation_constraint_oid(reloid, depname, missing_ok); address.objectSubId = 0; break; default: diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index 342cf75..bf174b6 100644 --- a/src/backend/catalog/pg_constraint.c +++ b/src/backend/catalog/pg_constraint.c @@ -736,12 +736,12 @@ AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, } /* - * get_constraint_oid + * get_relation_constraint_oid * Find a constraint on the specified relation with the specified name. * Returns constraint's OID. */ Oid -get_constraint_oid(Oid relid, const char *conname, bool missing_ok) +get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok) { Relation pg_constraint; HeapTuple tuple; @@ -794,6 +794,64 @@ get_constraint_oid(Oid relid, const char *conname, bool missing_ok) } /* + * get_domain_constraint_oid + * Find a constraint on the specified domain with the specified name. + * Returns constraint's OID. + */ +Oid +get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok) +{ + Relation pg_constraint; + HeapTuple tuple; + SysScanDesc scan; + ScanKeyData skey[1]; + Oid conOid = InvalidOid; + + /* + * Fetch the constraint tuple from pg_constraint. There may be more than + * one match, because constraints are not required to have unique names; + * if so, error out. + */ + pg_constraint = heap_open(ConstraintRelationId, AccessShareLock); + + ScanKeyInit(&skey[0], +Anum_pg_constraint_contypid, +BTEqualStrategyNumber, F_OIDEQ, +ObjectIdGetDatum(typid)); + + scan = systable_beginscan(pg_constraint, ConstraintTypidIndexId, true, + SnapshotNow, 1, skey); + + while (HeapTupleIsValid(tuple = systable_getnext(scan))) + { + Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple); + + if (strcmp(NameStr(con->conname), conname) == 0) + { + if (OidIsValid(conOid)) +ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("domain \"%s\" has multiple constraints named \"%s\"", + format_type_be(typid), conname))); + conOid = HeapTupleGetOid(tuple); + } + } + + systable_endscan(scan); + + /* If no such con
Re: [HACKERS] renaming domain constraint
Excerpts from Robert Haas's message of mié mar 21 11:43:17 -0300 2012: > On Fri, Mar 16, 2012 at 1:34 PM, Peter Eisentraut wrote: > > Here is a patch for being able to rename constraints of domains. It > > goes on top of the previously committed patch for renaming table > > constraints. > > I don't like the way you've modified get_constraint_oid(), which is > currently parallel to many other get_whatever_oid() functions and with > this patch, would no longer be. There seems to be little point in > shoehorning the new functionality into the existing function anyway, > considering that you've conditionalized basically every piece of logic > in the function. I think you should just invent a completely separate > function and be done with it. get_relation_constraint_oid() plus get_domain_constraint_oid()? -- Álvaro Herrera The PostgreSQL Company - Command Prompt, Inc. PostgreSQL Replication, Consulting, Custom Development, 24x7 support -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] renaming domain constraint
On Fri, Mar 16, 2012 at 1:34 PM, Peter Eisentraut wrote: > Here is a patch for being able to rename constraints of domains. It > goes on top of the previously committed patch for renaming table > constraints. I don't like the way you've modified get_constraint_oid(), which is currently parallel to many other get_whatever_oid() functions and with this patch, would no longer be. There seems to be little point in shoehorning the new functionality into the existing function anyway, considering that you've conditionalized basically every piece of logic in the function. I think you should just invent a completely separate function and be done with it. -- Robert Haas EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
[HACKERS] renaming domain constraint
Here is a patch for being able to rename constraints of domains. It goes on top of the previously committed patch for renaming table constraints. diff --git a/doc/src/sgml/ref/alter_domain.sgml b/doc/src/sgml/ref/alter_domain.sgml index 2511a12..c59975a 100644 --- a/doc/src/sgml/ref/alter_domain.sgml +++ b/doc/src/sgml/ref/alter_domain.sgml @@ -32,6 +32,8 @@ ALTER DOMAIN name ALTER DOMAIN name DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] ALTER DOMAIN name + RENAME CONSTRAINT constraint_name TO new_constraint_name +ALTER DOMAIN name VALIDATE CONSTRAINT constraint_name ALTER DOMAIN name OWNER TO new_owner @@ -103,6 +105,15 @@ ALTER DOMAIN name +RENAME CONSTRAINT + + + This form changes the name of a constraint on a domain. + + + + + VALIDATE CONSTRAINT @@ -182,7 +193,7 @@ ALTER DOMAIN name constraint_name -Name of an existing constraint to drop. +Name of an existing constraint to drop or rename. @@ -226,6 +237,15 @@ ALTER DOMAIN name + new_constraint_name + + +The new name for the constraint. + + + + + new_owner @@ -289,6 +309,13 @@ ALTER DOMAIN zipcode DROP CONSTRAINT zipchk; + To rename a check constraint on a domain: + +ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check; + + + + To move the domain into a different schema: ALTER DOMAIN zipcode SET SCHEMA customers; diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c index e6e0347..08de88b 100644 --- a/src/backend/catalog/objectaddress.c +++ b/src/backend/catalog/objectaddress.c @@ -753,7 +753,7 @@ get_object_address_relobject(ObjectType objtype, List *objname, case OBJECT_CONSTRAINT: address.classId = ConstraintRelationId; address.objectId = - get_constraint_oid(reloid, depname, missing_ok); + get_constraint_oid(reloid, InvalidOid, depname, missing_ok); address.objectSubId = 0; break; default: diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index 342cf75..4377207 100644 --- a/src/backend/catalog/pg_constraint.c +++ b/src/backend/catalog/pg_constraint.c @@ -737,17 +737,20 @@ AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, /* * get_constraint_oid - * Find a constraint on the specified relation with the specified name. + * Find a constraint on the specified relation or domain with the specified name. * Returns constraint's OID. */ Oid -get_constraint_oid(Oid relid, const char *conname, bool missing_ok) +get_constraint_oid(Oid relid, Oid typid, const char *conname, bool missing_ok) { Relation pg_constraint; HeapTuple tuple; SysScanDesc scan; ScanKeyData skey[1]; Oid conOid = InvalidOid; + Oid indexId; + + AssertArg(!relid || !typid); /* * Fetch the constraint tuple from pg_constraint. There may be more than @@ -756,12 +759,24 @@ get_constraint_oid(Oid relid, const char *conname, bool missing_ok) */ pg_constraint = heap_open(ConstraintRelationId, AccessShareLock); - ScanKeyInit(&skey[0], -Anum_pg_constraint_conrelid, -BTEqualStrategyNumber, F_OIDEQ, -ObjectIdGetDatum(relid)); + if (relid) + { + ScanKeyInit(&skey[0], + Anum_pg_constraint_conrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(relid)); + indexId = ConstraintRelidIndexId; + } + else + { + ScanKeyInit(&skey[0], + Anum_pg_constraint_contypid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(typid)); + indexId = ConstraintTypidIndexId; + } - scan = systable_beginscan(pg_constraint, ConstraintRelidIndexId, true, + scan = systable_beginscan(pg_constraint, indexId, true, SnapshotNow, 1, skey); while (HeapTupleIsValid(tuple = systable_getnext(scan))) @@ -771,10 +786,18 @@ get_constraint_oid(Oid relid, const char *conname, bool missing_ok) if (strcmp(NameStr(con->conname), conname) == 0) { if (OidIsValid(conOid)) -ereport(ERROR, - (errcode(ERRCODE_DUPLICATE_OBJECT), - errmsg("table \"%s\" has multiple constraints named \"%s\"", - get_rel_name(relid), conname))); + { +if (relid) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("table \"%s\" has multiple constraints named \"%s\"", + get_rel_name(relid), conname))); +else + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("domain \"%s\" has multiple constraints named \"%s\"", + format_type_be(typid), conname))); + } conOid = HeapTupleGetOid(tuple); } } @@ -783,10 +806,18 @@ get_constraint_oid(Oid relid, const char *conname, bool missing_ok) /* If no such constraint exists, complain */ if (!OidIsValid(conOid) && !missing_ok) - ereport(ERROR, -(errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("constraint \"%s\" for t