Skip to content

Commit 16a0039

Browse files
committed
Reduce lock level for ALTER DOMAIN ... VALIDATE CONSTRAINT
Reduce from ShareLock to ShareUpdateExclusivelock. Validation during ALTER DOMAIN ... ADD CONSTRAINT keeps using ShareLock. Example: create domain d1 as int; create table t (a d1); alter domain d1 add constraint cc10 check (value > 10) not valid; begin; alter domain d1 validate constraint cc10; -- another session insert into t values (8); Now we should still be able to perform DML operations on table t while the domain constraint is being validated. The equivalent works already on table constraints. Author: jian he <jian.universality@gmail.com> Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CACJufxHz92A88NLRTA2msgE2dpXpE-EoZ2QO61od76-6bfqurA%40mail.gmail.com
1 parent 123e65f commit 16a0039

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/backend/commands/typecmds.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static Oid findTypeSubscriptingFunction(List *procname, Oid typeOid);
126126
static Oid findRangeSubOpclass(List *opcname, Oid subtype);
127127
static Oid findRangeCanonicalFunction(List *procname, Oid typeOid);
128128
static Oid findRangeSubtypeDiffFunction(List *procname, Oid subtype);
129-
static void validateDomainCheckConstraint(Oid domainoid, const char *ccbin);
129+
static void validateDomainCheckConstraint(Oid domainoid, const char *ccbin, LOCKMODE lockmode);
130130
static void validateDomainNotNullConstraint(Oid domainoid);
131131
static List *get_rels_with_domain(Oid domainOid, LOCKMODE lockmode);
132132
static void checkEnumOwner(HeapTuple tup);
@@ -2986,7 +2986,7 @@ AlterDomainAddConstraint(List *names, Node *newConstraint,
29862986
* to.
29872987
*/
29882988
if (!constr->skip_validation)
2989-
validateDomainCheckConstraint(domainoid, ccbin);
2989+
validateDomainCheckConstraint(domainoid, ccbin, ShareLock);
29902990

29912991
/*
29922992
* We must send out an sinval message for the domain, to ensure that
@@ -3098,7 +3098,12 @@ AlterDomainValidateConstraint(List *names, const char *constrName)
30983098
val = SysCacheGetAttrNotNull(CONSTROID, tuple, Anum_pg_constraint_conbin);
30993099
conbin = TextDatumGetCString(val);
31003100

3101-
validateDomainCheckConstraint(domainoid, conbin);
3101+
/*
3102+
* Locking related relations with ShareUpdateExclusiveLock is ok because
3103+
* not-yet-valid constraints are still enforced against concurrent inserts
3104+
* or updates.
3105+
*/
3106+
validateDomainCheckConstraint(domainoid, conbin, ShareUpdateExclusiveLock);
31023107

31033108
/*
31043109
* Now update the catalog, while we have the door open.
@@ -3191,9 +3196,16 @@ validateDomainNotNullConstraint(Oid domainoid)
31913196
/*
31923197
* Verify that all columns currently using the domain satisfy the given check
31933198
* constraint expression.
3199+
*
3200+
* It is used to validate existing constraints and to add newly created check
3201+
* constraints to a domain.
3202+
*
3203+
* The lockmode is used for relations using the domain. It should be
3204+
* ShareLock when adding a new constraint to domain. It can be
3205+
* ShareUpdateExclusiveLock when validating an existing constraint.
31943206
*/
31953207
static void
3196-
validateDomainCheckConstraint(Oid domainoid, const char *ccbin)
3208+
validateDomainCheckConstraint(Oid domainoid, const char *ccbin, LOCKMODE lockmode)
31973209
{
31983210
Expr *expr = (Expr *) stringToNode(ccbin);
31993211
List *rels;
@@ -3210,9 +3222,7 @@ validateDomainCheckConstraint(Oid domainoid, const char *ccbin)
32103222
exprstate = ExecPrepareExpr(expr, estate);
32113223

32123224
/* Fetch relation list with attributes based on this domain */
3213-
/* ShareLock is sufficient to prevent concurrent data changes */
3214-
3215-
rels = get_rels_with_domain(domainoid, ShareLock);
3225+
rels = get_rels_with_domain(domainoid, lockmode);
32163226

32173227
foreach(rt, rels)
32183228
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy