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 1iKlkb-0005F1-U0 for pgsql-bugs@arkaria.postgresql.org; Wed, 16 Oct 2019 16:02:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iKlkZ-0007Wm-4L for pgsql-bugs@arkaria.postgresql.org; Wed, 16 Oct 2019 16:02:11 +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 1iKlkY-0007WZ-Gx for pgsql-bugs@lists.postgresql.org; Wed, 16 Oct 2019 16:02:10 +0000 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iKlkR-0004ky-Cn for pgsql-bugs@lists.postgresql.org; Wed, 16 Oct 2019 16:02:09 +0000 Received: by mail-wr1-x42a.google.com with SMTP id v8so28722656wrt.2 for ; Wed, 16 Oct 2019 09:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:message-id:subject:mime-version; bh=LiFUG98lih4ToftTi9YtRRLYe6QnG3eii+XSKI+dSyk=; b=WOe16aoJH/fbgc3RkpZV3l/H0Srd9R0fRRMhbi6JE7S9zL42t1EKmx83p+3YnWeB2G 7Brb/cwBaFHwRyJp4Q7wEsL5HlAnrxAic3CP2lzEEU1CWdnGoMIrxxDQv4TVkP90GFTG aVWIPzWTlJl/723sNayvddS4OF7ti27qlmFZgaMw6DxdVdMJfhPH0s7oVa60zzNMAkNy bmCjD4H1Ngn+7eGh8kVZXGwiyR/PEZIzzKrFlzbnnyL21bEJtF0njMYU/+DN0TjneAZm MB2boBe3tN+JgDhmpk0ZD/6fdyfzNZCXZG0sVmYsOgXNPg33A3QQsk63aqZYFiPBU/Jp 3C6Q== 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:message-id:subject:mime-version; bh=LiFUG98lih4ToftTi9YtRRLYe6QnG3eii+XSKI+dSyk=; b=a39VTNtbq/gDEF70Cj0KUtNMrYMarxGf7699xo7kzKN0t+BVgNf4v60yG3dz2LQC7j mqc7htvmikIsPairO+2yA0Q7FgK6DaC2CketMh1x5J3O6v1tS1kn7eVIK4XMtutdmUy5 xImEKGEqE1gXvkMZSBcy0rvkF05yr8FJ25Xo+5t4UK6IwdP6nKgxBqo6t+2oYeyTsypX mOGy/oycJaU2wB5cQPcSju32j9Skyq4Dn2gYD+Or+xYWlSlmzzpsY0CvnI3d0E2IojdV XBB1KbhYavHQFFrvWv2RX/gn2Ih+DLUZb7mGoLWye6TXWQ5W7k+mY7LCrW6eW0PEnV3I wNRQ== X-Gm-Message-State: APjAAAXz2OFTQI0gl5jTpGYWQ02qQa9oxFU8afIUybkshx+cg45Ry1YH 7tPTze2QCWRMoqGUuIniPvzYsu6DHvA= X-Google-Smtp-Source: APXvYqwYEftOgfXLedAoDPzM4fyXNv7GZ1m13MfxUZzd3V0Cp4HssAKSwv4oIu+nyZTgUvqR1ENLSA== X-Received: by 2002:adf:f192:: with SMTP id h18mr1841417wro.148.1571241720820; Wed, 16 Oct 2019 09:02:00 -0700 (PDT) Received: from panta-debian ([46.240.134.242]) by smtp.gmail.com with ESMTPSA id c21sm2589932wmb.46.2019.10.16.09.01.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Oct 2019 09:02:00 -0700 (PDT) Date: Wed, 16 Oct 2019 18:01:58 +0200 From: =?utf-8?Q?Ivan_Panti=C4=87?= To: "=?utf-8?Q?pgsql-bugs=40lists.postgresql.org?=" Message-ID: Subject: pg_restore creates duplicate records when used with --role parameter X-Mailer: Mailspring MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5da73ef6_72ca7029_5332" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5da73ef6_72ca7029_5332 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello, 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); WARNING: errors ignored on restore: 1 Here is the DDL of the table in question: CREATE TABLE price_history ( id SERIAL NOT NULL CONSTRAINT price_history_pkey PRIMARY KEY, pair VARCHAR(21) NOT NULL, open NUMERIC(32, 16) NOT NULL, high NUMERIC(32, 16) NOT NULL, low NUMERIC(32, 16) NOT NULL, close NUMERIC(32, 16) NOT NULL, count INTEGER NOT NULL, volume NUMERIC(32, 16) NOT NULL, timestamp TIMESTAMP WITH TIME ZONE NOT NULL, CONSTRAINT price_history_timestamp_pair_unique UNIQUE (timestamp, pair) ); This is what I saw in the destination table: id pair open high low close count volume timestamp 60 ETH_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 61 SFX_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 61 SFX_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 62 SFT_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 62 SFT_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 63 ETH_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 63 ETH_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 64 SFT_SFX 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 64 SFT_SFX 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 65 BTC_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000 66 SFX_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000 67 SFT_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000 68 ETH_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000 69 SFX_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000 70 SFT_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000 Excerpts from source table: id pair open high low close count volume timestamp 57 BTC_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 63 ETH_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 60 ETH_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 62 SFT_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 59 SFT_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 64 SFT_SFX 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 61 SFX_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 58 SFX_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000 id pair open high low close count volume timestamp 1625769 BTC_RSD 959304.0618596000000000 959304.0618596000000000 959304.0618596000000000 959304.0618596000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625775 ETH_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625772 ETH_RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625774 SFT_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625771 SFT_RSD 55.0000000000000000 55.0000000000000000 55.0000000000000000 55.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625776 SFT_SFX 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625773 SFX_BTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 1625770 SFX_RSD 60.0000000000000000 60.0000000000000000 60.0000000000000000 60.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000 So it seems like touple 1625776 got inserted with an auto-generated id, which is duplicate of a different tuple's id. I ran this import several times, all from the same backup file. It generates duplicate rows about 50% of times, usually from rows near the end of table (there is a total of 1627664 records). The same happens when restoring from a different backup and from a backup in tar format, so I suspect the problem is in restore utility. However, when I remove the "--role=destination_user" argument, the problem disappears. I wrote a script which tried delete/restore 10 times in a loop, and it worked each time. So it seems there is some kind of race condition or something when using the "role" argument. Systrem info: pg_restore (PostgreSQL) 9.6.15 Linux vm-dev 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64 GNU/Linux Regards, Panta --5da73ef6_72ca7029_5332 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
  Hello,
=
I've created a backup = using pg=5Frestore like this:

x sudo -u postgres pg=5Fdump source=5Fdb --format=3Dc= ustom' > /tmp/source=5Fdb.db

I then copied the backup file to the destination se= rver and tried to restore it like this:

sudo -u postgres psql destination=5Fdb -c 'D= ROP SCHEMA public CASCADE; CREATE SCHEMA public AUTHORIZATION destination= =5Fuser;'
sud= o -u postgres pg=5Frestore -d destination=5Fdb --no-owner --role=3Ddestin= ation=5Fuser -n public /tmp/source=5Fdb.db

I've recieved this error:
pg=5Frestore: =5Barchiv= er (db)=5D Error while PROCESSING TOC:
pg=5Frestore: =5Barchiver (db)=5D Error from TO= C entry 2276; 2606 42862 CONSTRAINT price=5Fhistory price=5Fhistory=5Fpke= y destination=5Fdb
pg=5Frestore: =5Barchiver (db)=5D could not execute query: ERROR:&n= bsp; could not create unique index =22price=5Fhistory=5Fpkey=22
DETAIL:  Key (id)= =3D(64) is duplicated.
    Command was: ALTER TABLE ONLY public.price=5F= history
 = ;   ADD CONSTRAINT price=5Fhistory=5Fpkey PRIMARY KEY (id);



WARN= ING: errors ignored on restore: 1

Here is the DDL of the table in question:<= /div>
CREATE TABLE pri= ce=5Fhistory (
  id SERIAL NOT NULL
    CONSTRAINT price=5Fhistory=5Fpkey PRIMA= RY KEY,
 = ; pair VARCHAR(21) NOT NULL,
  open NUMERIC(32, 16) NOT NULL,
  high NUMERIC(32, 16) NOT = NULL,
  = low NUMERIC(32, 16) NOT NULL,
  close NUMERIC(32, 16) NOT NULL,
=   count INTEGER NOT NULL,=
  volum= e NUMERIC(32, 16) NOT NULL,
  timestamp TIMESTAMP WITH TIME ZONE NOT NULL,=
  CONSTRAINT p= rice=5Fhistory=5Ftimestamp=5Fpair=5Funique UNIQUE (timestamp, pair)
);
This is what I saw in the= destination table:

id pair open high low close count volume timestamp
60 ETH=5FRSD 0.000000000= 0000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000= 000000000000 2019-05-28 08:51:00.000000
61 S=46X=5FBTC 0.0000000000000000 0.00000000000= 00000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-= 16 11:45:00.000000
61 S=46X=5FBTC 0.0000000000000000 0.0000000000000000 0.000000000000= 0000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000
62 S=46T=5FBTC= 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000= 000 0 0.0000000000000000 2019-10-16 11:45:00.000000
62 S=46T=5FBTC 0.0000000000000000 = 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.000000000000= 0000 2019-05-28 08:51:00.000000
63 ETH=5FBTC 0.0000000000000000 0.0000000000000000 0.0= 000000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:= 00.000000
63 = ETH=5FBTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000= 000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000
=
64 S=46T=5FS=46X 0.000000= 0000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0= 000000000000000 2019-10-16 11:45:00.000000
64 S=46T=5FS=46X 0.0000000000000000 0.000000= 0000000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 201= 9-05-28 08:51:00.000000
65 BTC=5FRSD 0.0000000000000000 0.0000000000000000 0.000000000= 0000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.00000= 0
66 S=46X=5F= RSD 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000= 000000 0 0.0000000000000000 2019-05-28 08:52:00.000000
<= font style=3D=22font-family:monospace=22> 67 S=46T=5FRSD 0.00000000000000= 00 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.000000000= 0000000 2019-05-28 08:52:00.000000
68 ETH=5FRSD 0.0000000000000000 0.0000000000000000 = 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:= 52:00.000000
= 69 S=46X=5FBTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0= .0000000000000000 0 0.0000000000000000 2019-05-28 08:52:00.000000<= /div>
70 S=46T=5FBTC 0.000= 0000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 = 0.0000000000000000 2019-05-28 08:52:00.000000

Excerpts from source table:
id pair open high = low close count volume timestamp
57 BTC=5FRSD 0.0000000000000000 0.0000000000000000 0.= 0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51= :00.000000
63= ETH=5FBTC 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.000= 0000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000
60 ETH=5FRSD 0.000000000= 0000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.0000= 000000000000 2019-05-28 08:51:00.000000
62 S=46T=5FBTC 0.0000000000000000 0.00000000000= 00000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-= 28 08:51:00.000000
59 S=46T=5FRSD 0.0000000000000000 0.0000000000000000 0.000000000000= 0000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:51:00.000000
64 S=46T=5FS=46= X 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.000000000000= 0000 0 0.0000000000000000 2019-05-28 08:51:00.000000
61 S=46X=5FBTC 0.0000000000000000= 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.00000000000= 00000 2019-05-28 08:51:00.000000
58 S=46X=5FRSD 0.0000000000000000 0.0000000000000000 = 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-05-28 08:= 51:00.000000

id pair open high low close count volume timestamp
1625769 BTC=5FRSD 959304.06185960= 00000000 959304.0618596000000000 959304.0618596000000000 959304.061859600= 0000000 0 0.0000000000000000 2019-10-16 11:45:00.000000
= 1625775 ETH=5FBTC 0.0000000000= 000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.00000= 00000000000 2019-10-16 11:45:00.000000
1625772 ETH=5FRSD 0.0000000000000000 0.00000000= 00000000 0.0000000000000000 0.0000000000000000 0 0.0000000000000000 2019-= 10-16 11:45:00.000000
1625774 S=46T=5FBTC 0.0000000000000000 0.0000000000000000 0.0000= 000000000000 0.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.= 000000
162577= 1 S=46T=5FRSD 55.0000000000000000 55.0000000000000000 55.0000000000000000= 55.0000000000000000 0 0.0000000000000000 2019-10-16 11:45:00.000000
1625776 S=46T=5FS= =46X 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.000000000= 0000000 0 0.0000000000000000 2019-10-16 11:45:00.000000
= 1625773 S=46X=5FBTC 0.00000000= 00000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0 0.000= 0000000000000 2019-10-16 11:45:00.000000
1625770 S=46X=5FRSD 60.0000000000000000 60.0000= 000000000000 60.0000000000000000 60.0000000000000000 0 0.0000000000000000= 2019-10-16 11:45:00.000000

So it seems like touple 1625776 got inserted with an au= to-generated id, which is duplicate of a different tuple's id.
I ran this import se= veral times, all from the same backup file. It generates duplicate rows a= bout 50% of times, usually from rows near the end of table (there is a to= tal of 1627664 records).
The same happens when restoring from a different backup and fr= om a backup in tar format, so I suspect the problem is in restore utility= .

However,= when I remove the =22--role=3Ddestination=5Fuser=22 argument, the proble= m disappears. I wrote a script which tried delete/restore 10 times in a l= oop, and it worked each time.
So it seems there is some kind of race condition or somet= hing when using the =22role=22 argument.

Systrem info:
pg=5Frestore (PostgreSQL) 9.6.15
Linux vm-dev 4.9.0-6-= amd64 =231 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86=5F64 GNU/Linux

Regards,
Panta
--5da73ef6_72ca7029_5332--