aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/typecmds.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/commands/typecmds.c')
-rw-r--r--src/backend/commands/typecmds.c85
1 files changed, 44 insertions, 41 deletions
diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c
index 175ecc8b484..b018585aef8 100644
--- a/src/backend/commands/typecmds.c
+++ b/src/backend/commands/typecmds.c
@@ -2444,6 +2444,8 @@ AlterDomainNotNull(List *names, bool notNull)
* AlterDomainDropConstraint
*
* Implements the ALTER DOMAIN DROP CONSTRAINT statement
+ *
+ * Returns ObjectAddress of the modified domain.
*/
ObjectAddress
AlterDomainDropConstraint(List *names, const char *constrName,
@@ -2455,10 +2457,10 @@ AlterDomainDropConstraint(List *names, const char *constrName,
Relation rel;
Relation conrel;
SysScanDesc conscan;
- ScanKeyData key[1];
+ ScanKeyData skey[3];
HeapTuple contup;
bool found = false;
- ObjectAddress address = InvalidObjectAddress;
+ ObjectAddress address;
/* Make a TypeName so we can use standard type lookup machinery */
typename = makeTypeNameFromNameList(names);
@@ -2477,37 +2479,36 @@ AlterDomainDropConstraint(List *names, const char *constrName,
/* Grab an appropriate lock on the pg_constraint relation */
conrel = heap_open(ConstraintRelationId, RowExclusiveLock);
- /* Use the index to scan only constraints of the target relation */
- ScanKeyInit(&key[0],
+ /* Find and remove the target constraint */
+ ScanKeyInit(&skey[0],
+ Anum_pg_constraint_conrelid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(InvalidOid));
+ ScanKeyInit(&skey[1],
Anum_pg_constraint_contypid,
BTEqualStrategyNumber, F_OIDEQ,
- ObjectIdGetDatum(HeapTupleGetOid(tup)));
+ ObjectIdGetDatum(domainoid));
+ ScanKeyInit(&skey[2],
+ Anum_pg_constraint_conname,
+ BTEqualStrategyNumber, F_NAMEEQ,
+ CStringGetDatum(constrName));
- conscan = systable_beginscan(conrel, ConstraintTypidIndexId, true,
- NULL, 1, key);
+ conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
+ NULL, 3, skey);
- /*
- * Scan over the result set, removing any matching entries.
- */
- while ((contup = systable_getnext(conscan)) != NULL)
+ /* There can be at most one matching row */
+ if ((contup = systable_getnext(conscan)) != NULL)
{
- Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(contup);
-
- if (strcmp(NameStr(con->conname), constrName) == 0)
- {
- ObjectAddress conobj;
+ ObjectAddress conobj;
- conobj.classId = ConstraintRelationId;
- conobj.objectId = HeapTupleGetOid(contup);
- conobj.objectSubId = 0;
+ conobj.classId = ConstraintRelationId;
+ conobj.objectId = HeapTupleGetOid(contup);
+ conobj.objectSubId = 0;
- performDeletion(&conobj, behavior, 0);
- found = true;
- }
+ performDeletion(&conobj, behavior, 0);
+ found = true;
}
- ObjectAddressSet(address, TypeRelationId, domainoid);
-
/* Clean up after the scan */
systable_endscan(conscan);
heap_close(conrel, RowExclusiveLock);
@@ -2527,6 +2528,8 @@ AlterDomainDropConstraint(List *names, const char *constrName,
constrName, TypeNameToString(typename))));
}
+ ObjectAddressSet(address, TypeRelationId, domainoid);
+
return address;
}
@@ -2652,16 +2655,15 @@ AlterDomainValidateConstraint(List *names, const char *constrName)
Relation typrel;
Relation conrel;
HeapTuple tup;
- Form_pg_constraint con = NULL;
+ Form_pg_constraint con;
Form_pg_constraint copy_con;
char *conbin;
SysScanDesc scan;
Datum val;
- bool found = false;
bool isnull;
HeapTuple tuple;
HeapTuple copyTuple;
- ScanKeyData key;
+ ScanKeyData skey[3];
ObjectAddress address;
/* Make a TypeName so we can use standard type lookup machinery */
@@ -2682,29 +2684,31 @@ AlterDomainValidateConstraint(List *names, const char *constrName)
* Find and check the target constraint
*/
conrel = heap_open(ConstraintRelationId, RowExclusiveLock);
- ScanKeyInit(&key,
+
+ ScanKeyInit(&skey[0],
+ Anum_pg_constraint_conrelid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(InvalidOid));
+ ScanKeyInit(&skey[1],
Anum_pg_constraint_contypid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(domainoid));
- scan = systable_beginscan(conrel, ConstraintTypidIndexId,
- true, NULL, 1, &key);
+ ScanKeyInit(&skey[2],
+ Anum_pg_constraint_conname,
+ BTEqualStrategyNumber, F_NAMEEQ,
+ CStringGetDatum(constrName));
- while (HeapTupleIsValid(tuple = systable_getnext(scan)))
- {
- con = (Form_pg_constraint) GETSTRUCT(tuple);
- if (strcmp(NameStr(con->conname), constrName) == 0)
- {
- found = true;
- break;
- }
- }
+ scan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
+ NULL, 3, skey);
- if (!found)
+ /* There can be at most one matching row */
+ if (!HeapTupleIsValid(tuple = systable_getnext(scan)))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("constraint \"%s\" of domain \"%s\" does not exist",
constrName, TypeNameToString(typename))));
+ con = (Form_pg_constraint) GETSTRUCT(tuple);
if (con->contype != CONSTRAINT_CHECK)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
@@ -3072,7 +3076,6 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
{
if (ConstraintNameIsUsed(CONSTRAINT_DOMAIN,
domainOid,
- domainNamespace,
constr->conname))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),