Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wRlGx-002fYa-1d for pgsql-hackers@arkaria.postgresql.org; Tue, 26 May 2026 06:28:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wRlGv-003QV3-1D for pgsql-hackers@arkaria.postgresql.org; Tue, 26 May 2026 06:28:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wRlGv-003QUv-0J for pgsql-hackers@lists.postgresql.org; Tue, 26 May 2026 06:28:14 +0000 Received: from mail-dy1-x1336.google.com ([2607:f8b0:4864:20::1336]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wRlGt-00000000oVh-3CCE for pgsql-hackers@lists.postgresql.org; Tue, 26 May 2026 06:28:12 +0000 Received: by mail-dy1-x1336.google.com with SMTP id 5a478bee46e88-304545f5206so5013200eec.0 for ; Mon, 25 May 2026 23:28:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779776890; x=1780381690; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=zA47Ld4D+ymS7YHqpDH+cHRWJ89djX/VvZgB5q7z9ww=; b=fX8DsIIb4wO6gCLtSTIEAcHU+S1OBq2+kMEFN7aEzJC0KntkO7YsZfO0dFFdCy9pPO D0Sc7GIAIU+pumwty9qzXDsQwv/tEnm0A5lLW2ERpcwKNtxP0DN1AtNbtFhfsRJ6oNhY MGvxgTPx6Jt8UAun1MJOgDrcaN1pVQxHmkXk8C0XqIbUXvnw1qBoxC89ov0dX2jsXeRA 0a5qinGhUNdzk6f6G9KpGrEqpKlH9HqmB6pyEYRXCl2BnqQ45H+nHYyOhvLjHdW112WA sZb889VwL/InQzpxHWefzczRTh4bfjpq5H2eC7lO4/NQF978ajevyiYdbElFt0JJujjN yfIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779776890; x=1780381690; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zA47Ld4D+ymS7YHqpDH+cHRWJ89djX/VvZgB5q7z9ww=; b=LOrUh9iBU/LjQFrv/SJVY5/J3b0fu/iTtxgXuCNlLwNBD0lduNcVP9/ZkqJahJC63W TMl4YaCy6E7FDs94bnzniksSJfik3TPwFQ/KGmyTpu4dA+rr7GOxLMZKV4KJ+U78pJ0j t8Zeo5do/ABpCZIcXRbDze9h9kNCjGAYtbyTRS12SyiXzkWBH/P33Wy2JbbcT3/R4W03 +RouHgIWAnnRyCRu/cVQg7rs86q7BUo1R9O9kjPZvjM0tbU+MLvYtl1+L6Ds9tKoYcY5 xKw+e8Al13b+TBPC0j+iTG8vDGlFvnDPYUfF9EqBai2Cyzyx2sDZOMqwP1ZAuCId8XEv NdIA== X-Gm-Message-State: AOJu0Yzb89qkA/B7TfePp4TC5BmAapp2sUyxBf4bE+ZODH0YTeVS9Ytc 1m+Vy4Vhk9v/sFBHRfyPJvBw1kTeCM3uAwwCKjRoxvJMDA6YY27oSHr9 X-Gm-Gg: Acq92OGmXaxJ0u/64Wh+rgD3frcR3z/+2rjvW6fmZXdLand8pSIjcbEL3D126JP5v88 nqBBILzUb5jyPsxAyncRqbVJhNfOksHyUIOi4qx5wm7cTH1Zh/nEJ1AO+UtZ14fqyNVefsElOoa G+G6dgaj1zk8F4PxXULPTFH/XJIhEb+3U9b5va21sRh7e1hp0C1N4wrFxEu2YBlmEvPrQv95c3q PGxdWv1fF1/K/+5JJX9rU0135y0PLZ6p6PRVqza0gjCou08kt9oqeEfVG/8M9tEqTF48oZLSHsY tAT7ggVEs57NkN+RQ0BrGh1bbjHIqZrOApcYFnAvFSU2jc8hbLaFZaOo0Zx8WH1MIPfcRZSzTcW aD5VGgMaLpy7A0IuCx/pafdafrODW561jg9RjOS9E0wjf5gMg196Jk0zM3UnQ//UwvAIgT3eN9K tYoRkaJt3fb0JzSLb2T6IBTn6Yi59yxFxlCfiheVin X-Received: by 2002:a05:7300:e605:b0:2d4:94cc:eebb with SMTP id 5a478bee46e88-30449001e3bmr7567050eec.13.1779776889665; Mon, 25 May 2026 23:28:09 -0700 (PDT) Received: from smtpclient.apple ([64.32.14.230]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-30451f1fe3esm15672995eec.7.2026.05.25.23.28.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 May 2026 23:28:09 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: Fix bug of CHECK constraint enforceability recursion From: Chao Li In-Reply-To: Date: Tue, 26 May 2026 14:27:34 +0800 Cc: PostgreSQL Hackers , Andrew Dunstan Content-Transfer-Encoding: quoted-printable Message-Id: References: To: jian he X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On May 26, 2026, at 14:05, jian he = wrote: >=20 > On Tue, May 26, 2026 at 11:51=E2=80=AFAM Chao Li = wrote: >>=20 >> Hi, >>=20 >> I just tested =E2=80=9CAdd support for altering CHECK constraint = enforceability=E2=80=9D and found an issue where recursion is not = handled properly. >>=20 >> Here is a repro with inheritance tables: >> ``` >> evantest=3D# create table p(a int constraint ck check (a > 0) = enforced); >> CREATE TABLE >> evantest=3D# create table c() inherits (p); >> CREATE TABLE >> evantest=3D# alter table c alter constraint ck not enforced; >> ALTER TABLE >> evantest=3D# insert into c values (-1); >> INSERT 0 1 >> evantest=3D# alter table p alter constraint ck enforced; >> ALTER TABLE >> evantest=3D# insert into c values (-2); >> INSERT 0 1 >> evantest=3D# select * from p; >> a >> ---- >> -1 >> -2 >> (2 rows) >> ``` >>=20 >> In this repro, the constraint on parent table p is already ENFORCED, = but the constraint on child table c was altered to NOT ENFORCED. So when = altering p to ENFORCED again, it didn't recurse to c. >>=20 >> The same problem can happen with partitioned tables as well: >> ``` >=20 > Hi. >=20 > In MergeConstraintsIntoExisting, we have: > /* > * A NOT ENFORCED child constraint cannot be merged with an > * ENFORCED parent constraint. However, the reverse is allowed, > * where the child constraint is ENFORCED. > */ > if (parent_con->conenforced && !child_con->conenforced) > ereport(ERROR, > (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), > errmsg("constraint \"%s\" conflicts with NOT ENFORCED > constraint on child table \"%s\"", > NameStr(child_con->conname), > RelationGetRelationName(child_rel)))); >=20 > MergeWithExistingConstraint, we have comments like: > /* > * If the child constraint is required to be enforced while the = parent > * constraint is not, this should be allowed by marking the = child > * constraint as enforced. In the reverse case, an error would = have > * already been thrown before reaching this point. > */ >=20 > So other commands (CREATE TABLE, ALTER TABLE ATTACH PARTITION) do not = expect a > state where the parent constraint is enforced but the child constraint = is not. > We can now reach this state via ALTER TABLE ALTER CONSTRAINT. >=20 > We don't need to worry about Foreign Key Constraints because the > foreign key constraint's conparentid is valid, therefore we cannot > directly alter a partition's FK constraint. > StoreRelCheck->CreateConstraintEntry comments ``/* no parent > constraint */`` means that each CHECK constraint is on its own. >=20 > Overall, i tend to think that we should reject ALTER TABLE ALTER > CONSTRAINT if it > would result in the parent constraint being enforced while the child = constraint > is not enforced. >=20 I am not against the idea of "rejecting ALTER TABLE ALTER CONSTRAINT if = it would result in the parent constraint being enforced while the child = constrain is not enforced", but I=E2=80=99m afraid it=E2=80=99s too late = for PG19. So, I guess we still need to fix the issue for 19, right? Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/