From 070da693d0aaba70b452b40776200b7b0698df86 Mon Sep 17 00:00:00 2001 From: jian he Date: Wed, 11 Mar 2026 10:04:13 +0800 Subject: [PATCH v10 1/2] Add DomainHasVolatileConstraints() to check constraint volatility Add a utility function that checks whether a domain type has any constraints, and optionally whether any CHECK constraint contains volatile expressions. This is needed by a subsequent commit that enables the fast default optimization for domains with non-volatile constraints: we can safely evaluate such constraints once at ALTER TABLE time, but volatile constraints require a full table rewrite. Discussion: https://postgr.es/m/CACJufxE_+iZBR1i49k_AHigppPwLTJi6km8NOsC7FWvKdEmmXg@mail.gmail.com Commitfest: https://commitfest.postgresql.org/patch/5641 --- src/backend/utils/cache/typcache.c | 45 ++++++++++++++++++++++++++++++ src/include/utils/typcache.h | 1 + 2 files changed, 46 insertions(+) diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c index 627e534609a..e33d4b807bd 100644 --- a/src/backend/utils/cache/typcache.c +++ b/src/backend/utils/cache/typcache.c @@ -1502,6 +1502,51 @@ DomainHasConstraints(Oid type_id) } +/* + * DomainHasVolatileConstraints --- check if a domain has constraints with + * volatile expressions + * + * Returns true if the domain has any constraints at all. If have_volatile + * is not NULL, also checks whether any CHECK constraint contains a volatile + * expression and sets *have_volatile accordingly. + * + * The caller must initialize *have_volatile before calling (typically to + * false). This function only ever sets it to true, never to false. + * + * This is defined to return false, not fail, if type is not a domain. + */ +bool +DomainHasVolatileConstraints(Oid type_id, bool *have_volatile) +{ + /* + * 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. + */ + TypeCacheEntry *typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO); + + if (typentry->domainData != NULL) + { + if (have_volatile) + { + foreach_node(DomainConstraintState, constrstate, + typentry->domainData->constraints) + { + if (constrstate->constrainttype == DOM_CONSTRAINT_CHECK && + contain_volatile_functions((Node *) constrstate->check_expr)) + { + *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 0e3945aa244..38366ff159e 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 DomainHasVolatileConstraints(Oid type_id, bool *have_volatile); extern TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod); -- 2.34.1