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 1vqmuu-000NdQ-0X for pgsql-general@arkaria.postgresql.org; Fri, 13 Feb 2026 06:44:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vqmtu-00CseD-1D for pgsql-general@arkaria.postgresql.org; Fri, 13 Feb 2026 06:43:39 +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.96) (envelope-from ) id 1vqmtu-00Cse5-01 for pgsql-general@lists.postgresql.org; Fri, 13 Feb 2026 06:43:38 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vqmts-00000000RYL-3MQt for pgsql-general@postgresql.org; Fri, 13 Feb 2026 06:43:38 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-48329eb96a7so2922395e9.3 for ; Thu, 12 Feb 2026 22:43:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1770965014; x=1771569814; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=2n9ZpPMPr6q8vPsFBW60Bx2vo2zBnXMh0el2/19PTvw=; b=by8wmV0ew+fYVqUjBDW63ZYYRF3O7c+xvu+r9txaeXpo12nOykp4vS9+cBjIqMTh1x s26F3uadIKGePF8OZgDBI6Q0ihCcDaEDBUvVBGg06uYddh0tworV1LLJ8Zld0dW0gU8D 3T1KIx8lgeov93lmrlMC2wZGaTXkWokDtzpne1BWJFSXquZ1kucXTX3QImJ0Z0ai1cvX E/yBjKttTKq5jjpWXwt9FneOoBK/EGiwrf55nL7aT+088ee9snDR58sT3bARskzwfKwx 3+7XBU8ycpa/P/G43qEbdmCMEIFED8iZTVNbTZIaGi4YKEkF95DICxJ61mshdAf+fC3u 6eKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770965014; x=1771569814; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2n9ZpPMPr6q8vPsFBW60Bx2vo2zBnXMh0el2/19PTvw=; b=DUrg5VVy5qFhQgd3e7+g2lgBxdUaNLUTQ1e9SpglcRgD7j2WHVTHn0psTGahFVRFp8 DGA8UEuQjmv/qWVhUUXhXVbDR8ya3Z9XEFLD9bYjcik2NnvTuqB+iCSyDgVv2B4nf3D9 L+pIzIwxWIrULQjVr2fEnVRN8sVQurkLUPoafIorHFenf7a8r/LlGORN/70wc5rdFiH9 53gDM+aMIOD6Zk8X1cKFflNd2/9dRlpUko5H4KiGGsv+mCeo5TqFFRacu7aE+UTixb50 JmeQ5rt8u+bAkaMikylBDsN8MDfoZ7Uu3Tp835h3P+94L2kO18ipyqBtLgJtm2DnYdsF +19A== X-Forwarded-Encrypted: i=1; AJvYcCVmsRmbe5NgbwdwbD/U8EWYypDDUBv7auOyfHY0zehk3b4TqdruTCNKUPe3S6MxuXMSjxtbbSusjbFzKo+/@postgresql.org X-Gm-Message-State: AOJu0Yxdr9b8xSfAGOXAcnG5VkZTpRJ/fpOhnyDusWFL/VGRJ5E+W9K6 IqyrS51Bbk9Sc8hC+tQkr/o8kZ/lwGaGJE9dDps2XjZufPnb5VbqUF8slxlwMTdHE8M= X-Gm-Gg: AZuq6aLmi15TawtjcTk0Wc8YbQr/qQa2M0MBAj+LkDc0nOQjnjq79pBwWXb1iaodk1N RdtWeJeUcRU8uYZx0TUsv+1qFIvG274/NhHwbrmGRSVnHA+NPGSjTaOFh6h1iYEnovtIrVOocUC 4L16+olqsbKQs1vDe+4ZFi5b4eOGJoqnJVGQVpxrbiZDFZx1+JIG2YNrUb8eK8Dfs9COTrIXX8e JcajVjHQaOj+SMMqRW/nbR/vaDDLQETUdx+1nBNUcTDABM3CuNtQXJeuizTncYYCuv+sL5E6uGl j+WylfYtupeg2yP9cfOhwZMeoqv+BiPg8MVvjMfqF3aC3pMAbpHWRNkrT3lVhEViDCX6NCSJBqG RFefqiXFL/pjzDYBkERBq3xwEL6T+yctxAPHlVVvAEf9KD8NkTmE8SdF84VfzfByvlfmrzRjJHR k5m7uayEDpLX34SP0P89n5Eteuij1E0Dw2+lX6DIPvzHid/Onegj0HjT8= X-Received: by 2002:a05:600c:4585:b0:480:1e40:3d2 with SMTP id 5b1f17b1804b1-48373a4f1e6mr8443635e9.29.1770965014322; Thu, 12 Feb 2026 22:43:34 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:270:4cd4:ad4e:e4e4:66cb:81ac]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4835d99497asm259704405e9.6.2026.02.12.22.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Feb 2026 22:43:34 -0800 (PST) Message-ID: <6511b21d495f1c010f05ae9841ee3fdc649dcff2.camel@cybertec.at> Subject: Re: pg_restore failed on foreign key constraint From: Laurenz Albe To: Ron Johnson , pgsql-general Date: Fri, 13 Feb 2026 07:43:33 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.2 (3.58.2-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2026-02-12 at 16:35 -0500, Ron Johnson wrote: > When running pg_restore 17.7 against a PG 14.20 database directory dump, = I got this in the log: >=20 > pg_restore: while PROCESSING TOC: > pg_restore: from TOC entry 171927; 2606 3149123877 FK CONSTRAINT rel_user= _email fk_rel_user_email_2 TAP > pg_restore: error: could not execute query: ERROR: =C2=A0insert or update= on table "rel_user_email" violates foreign key constraint "fk_rel_user_ema= il_2" > DETAIL: =C2=A0Key (access_email_id)=3D(2073) is not present in table "acc= ess_email". > Command was: ALTER TABLE ONLY public.rel_user_email > =C2=A0 =C2=A0 ADD CONSTRAINT fk_rel_user_email_2 FOREIGN KEY (access_emai= l_id) REFERENCES public.access_email(access_email_id); >=20 > So, I went to the source database: >=20 > TAPd=3D# \d rel_user_email > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0Table "public.rel_user_email" > =C2=A0 =C2=A0 =C2=A0Column =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0Type =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | Collation= | Nullable | Default=20 > -----------------+-----------------------------+-----------+----------+--= ------- > =C2=A0user_id =C2=A0 =C2=A0 =C2=A0 =C2=A0 | integer =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 | not null |=20 > =C2=A0access_email_id | integer =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | not nul= l |=20 > =C2=A0modified_by =C2=A0 =C2=A0 | integer =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=20 > =C2=A0modified_on =C2=A0 =C2=A0 | timestamp without time zone | =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 | not null |=20 > Indexes: > =C2=A0 =C2=A0 "idx_rel_user_email" UNIQUE, btree (user_id, access_email_i= d) > Foreign-key constraints: > =C2=A0 =C2=A0 "fk_rel_user_email_1" FOREIGN KEY (user_id) REFERENCES acce= ss_user(user_id) > =C2=A0 =C2=A0 "fk_rel_user_email_2" FOREIGN KEY (access_email_id) REFEREN= CES access_email(access_email_id) >=20 > TAPd=3D# select * from rel_user_email where access_email_id=3D2073; > =C2=A0user_id | access_email_id | modified_by | =C2=A0 =C2=A0 =C2=A0 modi= fied_on =C2=A0 =C2=A0 =C2=A0=20 > ---------+-----------------+-------------+------------------------- > =C2=A0 =C2=A0 2452 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A02073 | =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A041 | 2013-03-11 10:52:20.331 > (1 row) >=20 > TAPd=3D# \d access_email > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0Table "public.access_email" > =C2=A0 =C2=A0 =C2=A0Column =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0Type =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | Collation= | Nullable | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0Default =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 > -----------------+-----------------------------+-----------+----------+--= ----------------------------------------------------- > =C2=A0access_email_id | integer =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | not nul= l | nextval('access_email_access_email_id_seq'::regclass) > =C2=A0type =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| numeric(10,0) =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=20 > =C2=A0email_address =C2=A0 | character varying(255) =C2=A0 =C2=A0 =C2=A0|= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|= =20 > =C2=A0created_on =C2=A0 =C2=A0 =C2=A0| timestamp without time zone | =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | not null |=20 > =C2=A0modified_on =C2=A0 =C2=A0 | timestamp without time zone | =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=20 > =C2=A0created_by =C2=A0 =C2=A0 =C2=A0| integer =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=20 > =C2=A0modified_by =C2=A0 =C2=A0 | integer =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=20 > Indexes: > =C2=A0 =C2=A0 "pk_access_email" PRIMARY KEY, btree (access_email_id) > Referenced by: > =C2=A0 =C2=A0 TABLE "rel_user_email" CONSTRAINT "fk_rel_user_email_2" FOR= EIGN KEY (access_email_id) REFERENCES access_email(access_email_id) >=20 > TAPd=3D# select * from access_email where access_email_id=3D2073; > =C2=A0access_email_id | type | email_address | created_on | modified_on |= created_by | modified_by=20 > -----------------+------+---------------+------------+-------------+-----= -------+------------- > (0 rows) >=20 > Looks like index corruption.=C2=A0=C2=A0 >=20 > $ pg_amcheck -t public.rel_user_email -t public.access_email TAPd > $ echo $? > 0 > $ pg_amcheck -i idx_rel_user_email -i pk_access_email --parent-check --he= apallindexed TAPd > $ echo $? > 0 >=20 > But amcheck shows no problems. >=20 > Before I get worried that there' s corrupt=C2=A0data: am I missing someth= ing obvious? Try SET enable_indexscan =3D off; SELECT * FROM access_email WHERE access_email_id =3D 2073; Only if that returns a row, I would assume index corruption, and that one s= hould have been caught with "heapallindexed". It is the foreign key that is violated. The normal ways to end up with bro= ken foreign keys are SET session_replication_role =3D replica; and ALTER TABLE rel_user_email DISABLE TRIGGER ALL; both of which require superuser privileges. Yours, Laurenz Albe