hi.

Looking at AlterDomainValidateConstraint, it seems currently, ALTER
DOMAIN VALIDATE
CONSTRAINT will re-validate a VALID constraint, which
would just waste cycles.
Ideally, this operation should be a no-op.

The attached patch addresses this by making ALTER DOMAIN VALIDATE CONSTRAINT a
no-op in such cases.

ALTER TABLE VALIDATE CONSTRAINT is already a no-op when the constraint is VALID.
From b9356b888d397c48b993dbb7fc093ffd3f9fa65c Mon Sep 17 00:00:00 2001
From: jian he <jian.universal...@gmail.com>
Date: Wed, 21 May 2025 14:18:53 +0800
Subject: [PATCH v1 1/1] make ALTER DOMAIN VALIDATE CONSTRAINT no-op when
 constraint is validated

Looking at AlterDomainValidateConstraint, it seems currently, ALTER DOMAIN
VALIDATE CONSTRAINT will re-validate a constraint that has already been
validated, which would just waste cycles.  Ideally, this operation should be a
no-op when the constraint is already validated.
---
 src/backend/commands/typecmds.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c
index 45ae7472ab5..2c2587ffc14 100644
--- a/src/backend/commands/typecmds.c
+++ b/src/backend/commands/typecmds.c
@@ -3087,24 +3087,29 @@ AlterDomainValidateConstraint(List *names, const char *constrName)
 				 errmsg("constraint \"%s\" of domain \"%s\" is not a check constraint",
 						constrName, TypeNameToString(typename))));
 
-	val = SysCacheGetAttrNotNull(CONSTROID, tuple, Anum_pg_constraint_conbin);
-	conbin = TextDatumGetCString(val);
+	if (!con->convalidated)
+	{
+		val = SysCacheGetAttrNotNull(CONSTROID, tuple, Anum_pg_constraint_conbin);
+		conbin = TextDatumGetCString(val);
 
-	validateDomainCheckConstraint(domainoid, conbin);
+		validateDomainCheckConstraint(domainoid, conbin);
 
-	/*
-	 * Now update the catalog, while we have the door open.
-	 */
-	copyTuple = heap_copytuple(tuple);
-	copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
-	copy_con->convalidated = true;
-	CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
+		/*
+		 * Now update the catalog, while we have the door open.
+		*/
+		copyTuple = heap_copytuple(tuple);
+		copy_con = (Form_pg_constraint) GETSTRUCT(copyTuple);
+		copy_con->convalidated = true;
+		CatalogTupleUpdate(conrel, &copyTuple->t_self, copyTuple);
 
-	InvokeObjectPostAlterHook(ConstraintRelationId, con->oid, 0);
+		InvokeObjectPostAlterHook(ConstraintRelationId, con->oid, 0);
 
-	ObjectAddressSet(address, TypeRelationId, domainoid);
+		ObjectAddressSet(address, TypeRelationId, domainoid);
 
-	heap_freetuple(copyTuple);
+		heap_freetuple(copyTuple);
+	}
+	else
+		address = InvalidObjectAddress; /* already validated */
 
 	systable_endscan(scan);
 
-- 
2.34.1

Reply via email to