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.94.2) (envelope-from ) id 1tUegE-001gic-2f for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jan 2025 04:25:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tUefD-0055Jq-IO for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jan 2025 04:24:27 +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.94.2) (envelope-from ) id 1tUefD-0055Jh-6q for pgsql-hackers@lists.postgresql.org; Mon, 06 Jan 2025 04:24:26 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tUefA-0005hC-2n for pgsql-hackers@lists.postgresql.org; Mon, 06 Jan 2025 04:24:25 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-21628b3fe7dso192220945ad.3 for ; Sun, 05 Jan 2025 20:24:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736137464; x=1736742264; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=89B2Bq7L7/Qyvg6cuxdBcP0VvdszwH2zYs12qRkf15k=; b=nsGwZ4zz/pWdpIuJ10v3IZt/Rpv63Ebl2fGOtl7FV5yIybkOjY+G5Gfze9wLv3awjd 0Et7ZDQ8cP7DmhUQWzDP17rJat8yMP+jNO8a6galaLz2+gssbZLPZdCCv8d3WxelUkeM 5Wyg5EwB/A2EybNsflzD8AWApy8OUpIoSLNabyWM7DQTEPsrDKJEqXQ80pvd2FBN9LXo BU5+fVySSFBYze1QzpdsYKN6U7ek2/z3cDN9dc16RMokIu8re5TPf0C15rRIjdRNoONh wgDYmGZeEhnyPXJ2cNc5dVh2wwCah8FCLQGHkXaEDyy1P5bUp+zT3q/M6zYxBF8NRvbj RdMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736137464; x=1736742264; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=89B2Bq7L7/Qyvg6cuxdBcP0VvdszwH2zYs12qRkf15k=; b=ArbDQBlEzgsi23SbftFJTWifkMBiuSS9kqNrlxepiZjspkxCtx5KEa6KU6NykzRV3t WVQ1bBhjKpJ79SPtFgyJGVH5vLYta2wIuU5fTneS1L9VPUtTmhWf0/UDt7jtmkRoFEp2 2pDh2zytlz5nQOwI4S1WLySpHB80M60kjugqzNGwvoUrb22c0S55qi2xsQ7jXgDxdXA9 jhBeCASMvnp2DszkBr7bdIkFBnRqKr36pzncl0teNxce1UKq54OsA1C0Vm2j3jRtF3l0 pyayHGhN+QhDAKM1aBPGnqTJQjcYtLrLceLGM8/TO3rL3vPKVK2QplCqUAzB5I+4BqWN kaJA== X-Gm-Message-State: AOJu0YxOgh34yUHoNqsYKFQwvNWsM9kkZMqc6x36lX5p2hyP+LQEjRg0 5ckt+cAtBFN/7Am1Jobs1WyO3uLI7U2ABPZAU+GEUh+pmr+FrLxdQ7H8n12rUd/3F9lx/tJTPkF ESyolx9C7VqjwNuZ4N/KNQgs7Kf0= X-Gm-Gg: ASbGnctZk3WOD0l6+z/sbEzMRJQgnwm7q2NFD0oAXFFKCH3dY7lA/Q97B0/gGJjt9Eh 699pTAOlWYrTCPbaxBIYRcxaW92A84KzrBnc317bllj/kvFg0GDkZyuzcSuqBl1k3AX0JjucX X-Google-Smtp-Source: AGHT+IH3IOyjNFDJFU0fgmBjJ+Ez7/6KOkUKsb3kJ0IuHLhvmMzS3kZTRRJSzCNUIMJEW9bIJ/KNTX096wSrhBF289g= X-Received: by 2002:a05:6a20:c70a:b0:1e1:e2d9:3f31 with SMTP id adf61e73a8af0-1e5e0480b55mr85059667637.16.1736137463782; Sun, 05 Jan 2025 20:24:23 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Amul Sul Date: Mon, 6 Jan 2025 09:53:46 +0530 Message-ID: Subject: Re: Allow NOT VALID foreign key constraints on partitioned tables. To: =?UTF-8?Q?=C3=81lvaro_Herrera?= Cc: "pgsql-hackers@lists.postgresql.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jan 3, 2025 at 12:11=E2=80=AFAM =C3=81lvaro Herrera wrote: > > > > On Thu, Jan 2, 2025, at 5:49 PM, Amul Sul wrote: > > When adding a new FK constraint or attaching a partitioned table, where > matching FK constraints are merged, we allow the parent constraint to be = NOT > VALID while the child constraint remains VALID, which is harmless. Howeve= r, the > reverse scenario -- where the parent constraint is VALID and the child is= NOT > VALID -- is incorrect. To address this, when merging a NOT VALID FK const= raint > from the child with a VALID parent constraint, it implicitly validates th= e > child constraint against its existing data and marks it as VALID. This be= havior > aligns with adding a new FK constraint directly to the child table, which= would > also validate the existing data. > > > Hmm, I'm not sure about this, which may cause surprising delays. Maybe it= would be better that the operation fails with an error, so that the user c= an do VALIDATE CONSTRAINT explicitly and retry the ATTACH once all the part= itions have been so processed. Error reporting would have made this straightforward, but the delay is not a new, and the patch does not introduce any additional delay. Setting the patch aside for a moment, consider the current behavior on the master branch: if you have a partitioned table(see e.g. below) where one of the child tables has a NOT VALID foreign key constraint, adding a new VALID foreign key constraint to the partitioned table will ignore the existing constraint on the child table. Instead, it creates a new constraint, which ultimately triggers a scan of the child table to validate the new constraint. E.g. create table bar(i int primary key); create table foo(i int) partition by range(i); create table foo_p1 partition of foo for values from (0) to (10); insert into foo_p1 values(1); -- value doesn't exists in bar alter table foo_p1 add constraint fk_con foreign key(i) references bar NOT VALID; -- ok -- following triggers the validation and fails. alter table foo add constraint fk_con foreign key(i) references bar; The behavior with the patch remains the same, but instead of creating a new foreign key constraint, it merges with the existing one and validates it. Regards, Amul