Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UfU8d-0000z5-6K for pgsql-bugs@arkaria.postgresql.org; Thu, 23 May 2013 11:56:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UfU8c-0002pH-5Y for pgsql-bugs@arkaria.postgresql.org; Thu, 23 May 2013 11:56:54 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UfU8T-0002i5-1C for pgsql-bugs@postgresql.org; Thu, 23 May 2013 11:56:45 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UfU8L-0004EQ-QE for pgsql-bugs@postgresql.org; Thu, 23 May 2013 11:56:44 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id ED9CD286C4; Thu, 23 May 2013 04:56:36 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id E013CB0AFA; Thu, 23 May 2013 04:56:36 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id 89E4212002F; Thu, 23 May 2013 05:02:00 -0700 (PDT) Received: from [10.0.0.55] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTPSA; Thu, 23 May 2013 05:01:59 -0700 (PDT) Message-ID: <519E03F3.70306@vmware.com> Date: Thu, 23 May 2013 07:56:35 -0400 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: lalbin@fhcrc.org CC: pgsql-bugs@postgresql.org Subject: Re: BUG #8173: Inserting heap tuples in bulk in COPY patch return wrong line on failure 999 out of 1000 times. References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -5.3 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 21.05.2013 18:20, lalbin@fhcrc.org wrote: > The following bug has been logged on the website: > > Bug reference: 8173 > Logged by: Lloyd Albin > Email address: lalbin@fhcrc.org > PostgreSQL version: 9.2.4 > Operating system: SUSE Linux (64-bit) > Description: > > During testing for our 9.2 upgrade, we found that the error messages we were > expecting did not match what was given by the program. In looking over the > revision notes from our current version of 9.0.12 through the 9.2.4, that we > are testing, I believe that I have tracked down the issue to "Improve COPY > performance by adding tuples to the heap in batches". When I looked at the > patch code in > http://www.postgresql.org/message-id/4E708759.40206@enterprisedb.com I found > that you are inserting 1000 rows at a time. The problem is that on failure, > you return either row 1000 or the last line, whichever comes first. This can > be confusing as you will see in the demo code below. > > CREATE TABLE public.table1 ( > key INTEGER, > PRIMARY KEY(key) > ); > > Create a csv file with only one column of data, numbered from 1 to 1008. > > Make two copies of the file and name them csv_test.csv and csv_test2.csv. > > Edit csv_test.csv and change the entry 1000 to 500. > > Edit csv_test2.csv and change the entry 900 to 500. > > On 9.0.12 Server > > COPY public.table1 FROM 'csv_test.csv'; > > ERROR: duplicate key value violates unique constraint "table1_pkey" > DETAIL: Key (key)=(500) already exists. > CONTEXT: COPY table1, line 1000: "500" > > COPY public.table1 FROM 'csv_test2.csv'; > > ERROR: duplicate key value violates unique constraint "table1_pkey" > DETAIL: Key (key)=(500) already exists. > CONTEXT: COPY table1, line 900: "500" > > Both times the context gave us the correct information. > > Now try the same thing on 9.2.4 Server > > COPY public.table1 FROM 'csv_test.csv'; > > ERROR: duplicate key value violates unique constraint "table1_pkey" > DETAIL: Key (key)=(500) already exists. > CONTEXT: COPY table1, line 1000: "500" > > COPY public.table1 FROM 'csv_test2.csv'; > > ERROR: duplicate key value violates unique constraint "table1_pkey" > DETAIL: Key (key)=(500) already exists. > CONTEXT: COPY table1, line 1000: "1000" > > As you can see, the second test returned the last line of the set of tuples > being recorded not the line that actually failed. > > Make a copy of csv_test2.csv and name it csv_test3.csv. > Edit csv_test3.csv and remove all entries after 994. > > COPY public.table1 FROM 'csv_test3.csv'; > > ERROR: duplicate key value violates unique constraint "table1_pkey" > DETAIL: Key (key)=(500) already exists. > CONTEXT: COPY table1, line 995: "" > > If you are writing less than 1000 lines then it will return the line after > the last line with a value of "". Hmm, yeah, it's quite self-evident what's happening; the server reports the last line that was *read*, no the line where the error happened. Committed a fix for this. Unfortunately we only keep the last line read buffered in text format, so after this you'll only get the line number, not the content of that line: postgres=# copy foo from '/tmp/foo'; ERROR: duplicate key value violates unique constraint "foo_pkey" DETAIL: Key (id)=(4500) already exists. CONTEXT: COPY foo, line 4500 Thanks for the report! - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs