diff options
Diffstat (limited to 'src/backend/catalog/pg_constraint.c')
-rw-r--r-- | src/backend/catalog/pg_constraint.c | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index 342cf75270a..bf174b6a46e 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 constraint exists, complain */ + if (!OidIsValid(conOid) && !missing_ok) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("constraint \"%s\" for domain \"%s\" does not exist", + conname, format_type_be(typid)))); + + heap_close(pg_constraint, AccessShareLock); + + return conOid; +} + +/* * Determine whether a relation can be proven functionally dependent on * a set of grouping columns. If so, return TRUE and add the pg_constraint * OIDs of the constraints needed for the proof to the *constraintDeps list. |