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 1qiJvR-0066HB-2K for pgsql-general@arkaria.postgresql.org; Mon, 18 Sep 2023 19:28:53 +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 1qiJvO-002wLA-NH for pgsql-general@arkaria.postgresql.org; Mon, 18 Sep 2023 19:28:50 +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 1qiJvO-002wKn-5Q for pgsql-general@lists.postgresql.org; Mon, 18 Sep 2023 19:28:50 +0000 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qiJvL-005JN2-3F for pgsql-general@lists.postgresql.org; Mon, 18 Sep 2023 19:28:48 +0000 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-40472c3faadso48354375e9.2 for ; Mon, 18 Sep 2023 12:28:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1695065325; x=1695670125; darn=lists.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=nfZlc0iPWF9e+KSZlh9jooZ5KEkl/ywV4+ihL/xta5k=; b=LinMoqH+BVd4HHr2DwpqHJqSBGgb2o+a8Ecz0vu+A2obDIFcvXF4lkYVo9lZ9aKZI5 I9v4+pkNupLNHa/he50PTDti0Q2SedO95ysSTX8WrsnnOguwT8XvRF/umunOVd+EIBoH +dc4/7kXbROknPdOKHNMobr9gtn0qWyRpYk9Tel/WQKwIKJyzmQL8T9nhEXYLglu18G0 77Ah8xpFhhkj6A5s6foyo/+qhibvR89tjs8+Momi6WJkNv+Sg/yx0/vzN8u5jQhAkMVQ YjFZ3oJQ2EzTrYtZDFBNp8LEzLCyMJs+OWpoOgDjqOGCwCx8WvLC/A5N2MoqL7TWEhhV J2AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695065325; x=1695670125; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nfZlc0iPWF9e+KSZlh9jooZ5KEkl/ywV4+ihL/xta5k=; b=Jh9Fgry49OvYs0+xF7vcRzXQkRuvOmkai1YrnCuSKDv/IBjveOGQDdVAisha4yVxfO Bw1dVzOyeruXDUG3GNnMk8Rbh/37EOOIuGqv+4PhYM4dWed0DLywXibH8aAeUE3TQuiy AWZfhkPWG3Od+wqPgYGlUQ5yOVNLZr1IySSBGW2HxmBO/s5Mn9BARTLdkU8SAkhF50J6 Z+z0k/5xF13L4fmU5qVSdUVTlPxiwccfbXcjsnCW6yJevc5aQwQiLzeH57CcriSeW8H5 fEoiNeV4LxjNOuTAMff3Ma57gK4cYrm2eiJei3eicIXSJCVHImw6QVUJKeRr7lBHdc9J r6xg== X-Gm-Message-State: AOJu0YzpF//ZUKeespcAzHhBrCYrdxX+X+6nOgk6yc1dDCHU6AtFlxia s3gjoxUrOhWmGxhMTL8nvARQNg== X-Google-Smtp-Source: AGHT+IHaNLU/s+GuiFrGKtxofIEOF2RzG7VDFGIChYpPTa7aNtCaQBn+sY7VOoAEcIfR/lTyVOdoyg== X-Received: by 2002:a7b:c8da:0:b0:403:e21:1355 with SMTP id f26-20020a7bc8da000000b004030e211355mr9136032wml.36.1695065325155; Mon, 18 Sep 2023 12:28:45 -0700 (PDT) Received: from localhost.localdomain (178-190-180-35.adsl.highway.telekom.at. [178.190.180.35]) by smtp.gmail.com with ESMTPSA id 18-20020a05600c025200b003fe1a092925sm13299905wmj.19.2023.09.18.12.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 12:28:44 -0700 (PDT) Message-ID: <11b8ca504cd625d45d561c9a1b69700f1ec5ef55.camel@cybertec.at> Subject: Re: trouble restoring a database backed up with pg_dump/pg_dump_all - relations are created in the wrong order From: Laurenz Albe To: Harry Green , "pgsql-general@lists.postgresql.org" Date: Mon, 18 Sep 2023 21:28:43 +0200 In-Reply-To: <73791286.4632684.1695049729344@mail.yahoo.com> References: <73791286.4632684.1695049729344.ref@mail.yahoo.com> <73791286.4632684.1695049729344@mail.yahoo.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.fc38) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2023-09-18 at 15:08 +0000, Harry Green wrote: > I am having trouble restoring a database backed up with pg_dump/pg_dump_a= ll. > The error messages I get are below and appear to suggest that certain sql= -language or > pl/pgsql-language functions which include an sql statement referencing a = table are > trying to be executed before the table which they reference has been crea= ted. > I am surprised that pg_dump could get the order wrong, but that is what i= s happening.=C2=A0 >=20 > Looking at the first of the error messages, the pl/pgsql function is crea= ted as > =C2=A0 create=C2=A0 function public.check_account_from_bill_items(charact= er...)=C2=A0on line 95, > but the instruction to create the accounts table=C2=A0 '... create table = public.accounts ... ' > appears on line 510.=C2=A0=C2=A0 >=20 > Consequently, the restore does not work because the relations are created= in the > wrong order. Any ideas how I can solve it? >=20 > Please see the error message below: >=20 > psql:20230913_1300.sql: ERROR:=C2=A0 relation "accounts" does not exist > LINE 1: select=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 name from accounts where= (type=3D'Expense' or ... > =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 ^ > QUERY:=C2=A0 select=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 name from accounts = where (type=3D'Expense' or type=3D'Asset' or type=3D'Stock') and name=3Dacc= ount > CONTEXT:=C2=A0 PL/pgSQL function public.check_account_from_bill_items(cha= racter varying) line 6 at SQL statement > COPY bill_items, line 1: "4096 Website 0 11.6099999999999994 \N \N 0 11.6= 099999999999994 5852 1 \N" >=20 > psql:20230913_1300.sql:201718: ERROR:=C2=A0 relation "ebooks" does not ex= ist > LINE 1: select count(*) from ebooks where isbn13=3D$1=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^ > QUERY:=C2=A0 select count(*) from ebooks where isbn13=3D$1=C2=A0 > CONTEXT:=C2=A0 SQL function "ebook_records_with_isbn13" during inlining > COPY book_information, line 1: "Title abcd \N \N 1-2222-2222-1 111-1-2222= -2222-1 11.11 111.11 1 \N ..." >=20 > [...] > > psql:20230913_1300.sql:20459125: ERROR:=C2=A0 insert or update on table "= book_praise" violates foreign key constraint "book_praise_title_fkey" > DETAIL:=C2=A0 Key (title, binding, isbn)=3D(XXCCC, back, 1111-55) is not = present in table "book_information". >=20 > psql:20230913_1300.sql:20459381: ERROR:=C2=A0 insert or update on table "= posters" violates foreign key constraint "posters_isbn_fkey" > DETAIL:=C2=A0 Key (isbn)=3D(1-3333-3333-3) is not present in table "book_= information". > > [...] It seems like you are restoring the dump in the wrong way. pg_dump dumps foreign key constraints last of all, so during a restore they= are created after all the data have been inserted into the tables, and the error you se= e can never happen. You must be restoring a data-only dump into an already existing sc= hema with foreign keys in place. That won't work and isn't supported. Don't forget = that foreign keys can be circular, and there might be no "correct order" to dump the tab= les. The first errors with the function calls are less obvious, but existing tri= ggers on the tables micht be an explanation. Since one of the functions is called "chec= k_...", another explanation could be that you have check constraints that use funct= ions that access other tables. That won't work and is not allowed. Without knowing more, I cannot be certain what exactly is wrong, but it doe= sn't look like a PostgreSQL bug to me. Perhaps you can provide more details. Yours, Laurenz Albe