From 3781d439a33727aee57fe6345804ed04a9758cfc Mon Sep 17 00:00:00 2001 From: jian he Date: Mon, 19 May 2025 11:11:01 +0800 Subject: [PATCH v3 1/2] add function DomainHaveVolatileConstraints the signature is: extern bool DomainHaveVolatileConstraints(Oid type_id, bool *have_volatile); Returns true if the Domain has any constraints. If you want check this domain have any volatile check constraints, make sure have_volatile is not NULL. discussion: https://postgr.es/m/CACJufxE_+iZBR1i49k_AHigppPwLTJi6km8NOsC7FWvKdEmmXg@mail.gmail.com --- src/backend/utils/cache/typcache.c | 40 ++++++++++++++++++++++++++++++ src/include/utils/typcache.h | 1 + 2 files changed, 41 insertions(+) diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c index 0c17d99d021..e449f59b3b6 100644 --- a/src/backend/utils/cache/typcache.c +++ b/src/backend/utils/cache/typcache.c @@ -1499,6 +1499,46 @@ DomainHasConstraints(Oid type_id) } +/* + * Check whether a domain has any constraints, and determine if any of those + * constraints contain volatile expressions. + * + * To detect volatile expressions within domain check constraints, ensure that + * have_volatile is not NULL. If have_volatile is NULL, the behavior is + * equivalent to that of DomainHasConstraints. + */ +bool +DomainHaveVolatileConstraints(Oid type_id, bool *have_volatile) +{ + TypeCacheEntry *typentry; + + /* + * Note: a side effect is to cause the typcache's domain data to become + * valid. This is fine since we'll likely need it soon if there is any. + */ + typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO); + + if (typentry->domainData != NULL) + { + ListCell *lc; + + foreach(lc, typentry->domainData->constraints) + { + DomainConstraintState *r = (DomainConstraintState *) lfirst(lc); + + if (r->constrainttype == DOM_CONSTRAINT_CHECK && + contain_volatile_functions((Node *) r->check_expr)) + { + if (have_volatile) + *have_volatile = true; + break; + } + } + return true; + } + return false; +} + /* * array_element_has_equality and friends are helper routines to check * whether we should believe that array_eq and related functions will work diff --git a/src/include/utils/typcache.h b/src/include/utils/typcache.h index 1cb30f1818c..aa1c86e35c3 100644 --- a/src/include/utils/typcache.h +++ b/src/include/utils/typcache.h @@ -184,6 +184,7 @@ extern void InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref, extern void UpdateDomainConstraintRef(DomainConstraintRef *ref); extern bool DomainHasConstraints(Oid type_id); +extern bool DomainHaveVolatileConstraints(Oid type_id, bool *have_volatile); extern TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod); -- 2.34.1