Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLQGE-0008Nc-VZ for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Oct 2019 11:17:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLQGC-0001OH-VV for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Oct 2019 11:17:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLQGC-0001NK-I2 for pgsql-bugs@lists.postgresql.org; Fri, 18 Oct 2019 11:17:32 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iLQGA-0002a6-1I for pgsql-bugs@lists.postgresql.org; Fri, 18 Oct 2019 11:17:31 +0000 Received: by mail-wr1-x444.google.com with SMTP id n14so5814857wrw.9 for ; Fri, 18 Oct 2019 04:17:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:message-id:in-reply-to:references:subject :mime-version; bh=bsbc8nXvb07GpLbnSt5bnvrp1BQWAT/pznxJvG24ysc=; b=jpU6SpreRNcdmMhOK+HeIap0AWAeOb6OSaECJY5QGWPNeJsfDfKip2RU6uCy4YSFwf 1BLkBnItU+EjtP43rm3chJzXWHGhJEYsXmaIWHtLwyal1/3HsalR787f0DAYmKgz6S79 0u5aUYZVOGoRkZMm3ZsBvIpjsntVm/zQlJZUaC0NDzDhP/KodvNCHmdKlDnMd2Ncv3cI TLKxtkswMyEFhNqijU6MmH/mpvC/aXIqG9ykOF34plLXJulxZmnkhP869WVTxDv2MpDN zbIhxmn8HIH2hKEhvsy21Cu4WB8X2gXTvm6Xg0/sH5GSyYtPqVbleqQWF2ZpQVmZokDm 6O/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to :references:subject:mime-version; bh=bsbc8nXvb07GpLbnSt5bnvrp1BQWAT/pznxJvG24ysc=; b=i7qHvOe4EEiz35oBB1k3788oA91n5wXT4EvPQnjQJJ0Ex0Knxmy+YMU51BIcNorhe7 paKNZbafzzdYElLHbVqFDoB+5bCSsRyUPkkkmR0TPK+woqjMeQWW02KukSGa4jWXzR10 hR6argRpwohNIdZ5FOGViYVwi1Fpckd8RgpfNhg+hRgeab75MBjCJq6ybwdZqoVvkTv/ smqtCG9NVP6lkjTuKCARZ7/Dgj5+nRrCNKKBJ4VqdqroF8eoLPjthtEiGXAZZ7oOnriK SDMwxVMT2jyqmJ6wZ6xcefeHXbeeO34Wbfsb6FF1fWG/WNkJ88mVjU/IZGs7Ecl09NZK bVGw== X-Gm-Message-State: APjAAAV+XF2wDOjCFx1jwa6YAmS7NFbZjNGu/Zun+oSRVgr0BEd4/vp/ Qm6RZFnAa0ESAruaXpkvzXL9L5BGzqI= X-Google-Smtp-Source: APXvYqzdNC9OdiD++LJDjS3IValQbUr7Ei8CdfJph74MMVgxX0uOHNgMFZaExccMgsnhFS/Q+sbmuQ== X-Received: by 2002:adf:9dca:: with SMTP id q10mr7378076wre.183.1571397448452; Fri, 18 Oct 2019 04:17:28 -0700 (PDT) Received: from panta-debian ([46.240.134.242]) by smtp.gmail.com with ESMTPSA id t203sm5981384wmf.42.2019.10.18.04.17.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Oct 2019 04:17:27 -0700 (PDT) Date: Fri, 18 Oct 2019 13:17:26 +0200 From: =?utf-8?Q?Ivan_Panti=C4=87?= To: Tom Lane Cc: "=?utf-8?Q?pgsql-bugs=40lists.postgresql.org?=" Message-ID: In-Reply-To: <28388.1571390016@sss.pgh.pa.us> References: <28388.1571390016@sss.pgh.pa.us> Subject: Re: pg_restore creates duplicate records when used with --role parameter X-Mailer: Mailspring MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5da99f46_32a67cf7_2f44" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5da99f46_32a67cf7_2f44 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi tom, After playing with this a bit more, it seems something was wrong with a particular database I was trying to restore into. When I created a fresh database, the restore worked, including after deleting the public schema and trying again. When I did a db dump of new and old database, the outputs were the same, nothing special in the old DB DDL. After deleting destination_db and creating it from scratch, the restore started working so it's not like its name is cursed or something. Overall, it seems there was some kind corruption of the old database that causes this restore to fail. Very strange and, I suspect, impossible to debug (especially given I no longer have the old database). Thank you for your help. Regards, Panta On Oct 18 2019, at 11:13 am, Tom Lane wrote: > =?utf-8?Q?Ivan_Panti=C4=87?= writes: > > I've created a backup using pg_restore like this: > > x sudo -u postgres pg_dump source_db --format=custom' > /tmp/source_db.db > > I then copied the backup file to the destination server and tried to restore it like this: > > sudo -u postgres psql destination_db -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public AUTHORIZATION destination_user;' > > sudo -u postgres pg_restore -d destination_db --no-owner --role=destination_user -n public /tmp/source_db.db > > > > I've recieved this error: > > pg_restore: [archiver (db)] Error while PROCESSING TOC: > > pg_restore: [archiver (db)] Error from TOC entry 2276; 2606 42862 CONSTRAINT price_history price_history_pkey destination_db > > pg_restore: [archiver (db)] could not execute query: ERROR: could not create unique index "price_history_pkey" > > DETAIL: Key (id)=(64) is duplicated. > > Command was: ALTER TABLE ONLY public.price_history > > ADD CONSTRAINT price_history_pkey PRIMARY KEY (id); > > > FWIW, I could not duplicate this report in several tries. > Usually, the expectation when restoring a dump is that you're restoring > into a totally empty database. Clearing out just the public schema > isn't that, so I wonder if you left behind something that could affect > this table. It's not very clear what that could be though :-(. > > Anyway, if you can provide a self-contained reproduction case for this, > we'd surely look further. > > regards, tom lane --5da99f46_32a67cf7_2f44 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Hi tom,

After playing with this a bit more, it seems s= omething was wrong with a particular database I was trying to restore int= o.

When I created a fresh database, the restore worked, inc= luding after deleting the public schema and trying again. When I did a db= dump of new and old database, the outputs were the same, nothing special= in the old DB DDL. After del= eting destinatio= n=5Fdb and creating it from scratch, the restore started working so it's = not like its name is cursed or something.

Ove= rall, it seems there was some kind corruption of the old database that ca= uses this restore to fail.

Very strange and, I suspect, imp= ossible to debug (especially given I no longer have the old database).
Thank you for your help.

Regards,
Pan= ta

On Oct 18 2019= , at 11:13 am, Tom Lane <tgl=40sss.pgh.pa.us> wrote:
=3D=3Futf-8=3FQ=3FIvan=5FPanti=3DC4=3D87=3F=3D <ivanpanti= c82=40gmail.com> writes:
I've created a backup u= sing pg=5Frestore like this:
x sudo -u postgres pg=5Fdump sourc= e=5Fdb --format=3Dcustom' > /tmp/source=5Fdb.db
I then copie= d the backup file to the destination server and tried to restore it like = this:
sudo -u postgres psql destination=5Fdb -c 'DROP SCHEMA pu= blic CASCADE; CREATE SCHEMA public AUTHORIZATION destination=5Fuser;'
sudo -u postgres pg=5Frestore -d destination=5Fdb --no-owner --rol= e=3Ddestination=5Fuser -n public /tmp/source=5Fdb.db
I've recieved this error:
pg=5Frestore: =5Ba= rchiver (db)=5D Error while PROCESSING TOC:
pg=5Frestore: =5Bar= chiver (db)=5D Error from TOC entry 2276; 2606 42862 CONSTRAINT price=5Fh= istory price=5Fhistory=5Fpkey destination=5Fdb
pg=5Frestore: =5B= archiver (db)=5D could not execute query: ERROR: could not create unique = index =22price=5Fhistory=5Fpkey=22
DETAIL: Key (id)=3D(64) is d= uplicated.
Command was: ALTER TABLE ONLY public.price=5Fhistory=
ADD CONSTRAINT price=5Fhistory=5Fpkey PRIMARY KEY (id);
<= /blockquote>
=46WIW, I could not duplicate this report in several= tries.

Usually, the expectation when restoring a dump is t= hat you're restoring
into a totally empty database. Clearing ou= t just the public schema
isn't that, so I wonder if you left be= hind something that could affect
this table. It's not very clea= r what that could be though :-(.

Anyway, if you can provide= a self-contained reproduction case for this,
we'd surely look = further.

regards, tom lane
--5da99f46_32a67cf7_2f44--