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 1rxxWa-00DS7q-7M for pgsql-general@arkaria.postgresql.org; Fri, 19 Apr 2024 23:20:08 +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 1rxxWY-006615-Oc for pgsql-general@arkaria.postgresql.org; Fri, 19 Apr 2024 23:20:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxxWY-00660x-CK for pgsql-general@lists.postgresql.org; Fri, 19 Apr 2024 23:20:06 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rxxWV-001t8c-VA for pgsql-general@lists.postgresql.org; Fri, 19 Apr 2024 23:20:05 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-51967f75729so3024164e87.0 for ; Fri, 19 Apr 2024 16:20:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713568802; x=1714173602; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JlSY8CW6umi+JtjCR5b9eOuuIDR+CL/S4fKhb111Zto=; b=mhgTfTclRSubjVIhb9tHVrvB7YUnJTPYtX2YxHKMzkJwZx9xf4pdQHhVTq/KCUwrAc +OrLJjx0sNp3bLlWw1V0ln8JdL1X9D4W87ToI01p+1isVRLCqkWOQfJxrj2xXFnqescE ApMdaYMc9pu4HoztRec/I67zrBYAgJjcubgnxr3yKJiLJFKQ9Tr5kxh3oLx2VGNxmTVZ pZj7QRVu0bboNhTjZxndN2K7IWxvcm6+KfQOnGzC/HMtoGHGapw5qrFKgXFIkJSKK3DC RMdMCugSEA5MwVQcM4Jn2oSvaEl+dTHMaB5VrnhU6gTxnTr6PUgcHo40vPmFEy3IMaJ/ yZnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713568802; x=1714173602; h=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=JlSY8CW6umi+JtjCR5b9eOuuIDR+CL/S4fKhb111Zto=; b=vOS/bhXktBTEj4bxC/7F9OVQuWdElKsU+D7uzU+xqTuml8f45ZU7t+2lcrIPf/wmzG jgnDNUWuqKE+9POFKY7dyayAVOn1DyKK91DPicljo2XkpS1/OkwXspsp9jEFEOw+dUXQ Nt51mtpv1dGFsyf4w+vqQ8/iNTE7daZ6xNZBw9vUhZ/7+pIOtn9yWHCPplWC1DPumuci JEi5Fo4D+vcdImhkZSU79X+0QgSKt5bR6LJtUiwxf8zQZ1Ohuqzl14qHcWBU5X77VdYl TGclYY2juEm6Iz2QIwbbAUV0fkOwY35af0V9PRx1Ph+PlEyC6NR3wrPFTgptvFMLKQdk N8Cg== X-Gm-Message-State: AOJu0YzGoRcxDAaYLqwnADHU4iAv76gBbqhh/KhlMjte6Enuf5M9tINJ H9UvGzD+KeBC6FBZIpN8m66VYzIuV418YF2l9VYbqrXNrnXINx8jrPKvl3VZJYBbIvuu2s9jf+6 fvpRfvGpxXP44f/RsNCb9VMeGVRDKTwfs X-Google-Smtp-Source: AGHT+IFkySoOQOy6etCLfTITrI4sgRo+yIkU+H7VhH9ACXbYIO31eQZobRupQviZUxAW5vqWBJstZFhiPmOFHTDhOss= X-Received: by 2002:a05:6512:945:b0:515:d1b9:3066 with SMTP id u5-20020a056512094500b00515d1b93066mr2254953lft.46.1713568801538; Fri, 19 Apr 2024 16:20:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Sat, 20 Apr 2024 11:19:47 +1200 Message-ID: Subject: Re: Foreign Key error between two partitioned tables To: Michael Corey Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 19 Apr 2024 at 05:48, Michael Corey wrote: > ALTER TABLE ONLY par_log_file > ADD CONSTRAINT plf_pk PRIMARY KEY (par_file_id); > ALTER TABLE par_log_definition > ADD CONSTRAINT pld_fk FOREIGN KEY (par_file_id) REFERENCES par_log_file(par_file_id); > I receive the following error when creating a foreign key between two partitioned tables. > ERROR: there is no unique constraint matching given keys for referenced table "par_log_file" > Version Postgres 13.13 No problems running that script here on 13.13. I imagine you've probably got a partition attached to par_log_file already and since your "plf_pk" constraint is on ONLY par_log_file, then the supporting index is likely invalid. I'd suggest checking if this is the case with: select indexrelid::regclass,indisvalid from pg_index where indrelid = 'par_log_file'::regclass; The correct way to create the PK constraint is with: ALTER TABLE par_log_file ADD CONSTRAINT plf_pk PRIMARY KEY (par_file_id); You might want to create supporting unique indexes on each partition CONCURRENTLY before doing that so that the ALTER TABLE becomes a meta-data-only operation. David