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 1hc90Y-0007D6-5I for pgsql-general@arkaria.postgresql.org; Sat, 15 Jun 2019 13:46:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hc8zW-0007q4-SN for pgsql-general@arkaria.postgresql.org; Sat, 15 Jun 2019 13:45:10 +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 1hc8zU-0007px-T6 for pgsql-general@lists.postgresql.org; Sat, 15 Jun 2019 13:45:10 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hc8zM-00029h-KT for pgsql-general@lists.postgresql.org; Sat, 15 Jun 2019 13:45:07 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 60801460; Sat, 15 Jun 2019 09:44:58 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 15 Jun 2019 09:44:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=c PJdqbJ5yer73Uz5O+Qh05IJ4zUivarUBaGTspzG3Ek=; b=YdUmqI5pX7yYBdYpI MGwgKZMdJlC6ik572F3zBovP1cVXq8K+bBIEsB2Kydzc+jEh4+IWzJGOoSne9eqD XKI+aeVTiUcPTli0eyjO5sXM6LWaxPg3ViHgBf4DLbEGWqEWj0EA72F0Tt7oHOYw 4534W1kNDxjJQmwZuxhTCeE4MMVB56uo2pSaZA6dMNaXir0jQ2IeDT2gmRmHCMN1 dMfSfni5/CqcDdKHuoqaonqhtrHsy7NMuWSBTVGqw/y8bUZiB0dbUgAPx16rJli4 Rpy8CeNmtInOW+6hTJlo/0b/AuN/H5aW/WU8FbK5qmYrmDeCyCy/O9boV/t7InZN K0Uuw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=cPJdqbJ5yer73Uz5O+Qh05IJ4zUivarUBaGTspzG3 Ek=; b=CgDU9mUnfU8DU8FvgMFVBe/ZWBAwkXM/v51abr6dl8lSl8cyZBYP+KBgr ZgeKosELuLd5e82sxk2GI5ZBSrkG0XESUOcGy172E99YiEPftaIgWmJ9BvTU3zSm FbQOGANlgmX4vaH1v4cZQRHRRhAoEzfpKl9aKRI1hueiBHVhQavOFb3GrFdnBQjX pwSMPpAReqaqPwl/Bi/1Fe7EmLG2/Ltv6iW7xrYHa9uY7xoPhFfcfX68Wcnjpl1O yrbjNpZfE0zGBlaTnrg/XYs35WFB9Y5fxF0exP/bzPY9gdjZRquCxKzLgfeEoies xjbISQeueRvvH26149IiegWFzc+Ww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrudeifedgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepthdurdhnrghmvgdpthdvrdhnrghmvgenucfkphepjedurddv uddvrdduhedtrddvtddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkh hlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id D092F80060; Sat, 15 Jun 2019 09:44:56 -0400 (EDT) Subject: Re: Copy Bulk Ignore Duplicated To: =?UTF-8?Q?Leandro_Guimar=c3=a3es?= , Tim Cross Cc: pgsql-general@lists.postgresql.org, pgsql-general@postgresql.org References: <87o92zpzba.fsf@gmail.com> From: Adrian Klaver Message-ID: Date: Sat, 15 Jun 2019 06:44:55 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 6/14/19 7:24 PM, Leandro Guimarães wrote: > Hi Tim, thanks for you answer! > > The columns were just examples, but let me explain the database > structure, the fields in *bold are the keys*: > > *customer_id integer* > *date_time timestamp* > *indicator_id integer* > *element_id integer* > indicator_value double precision Huh, earlier you said you had a check constraint that was causing issues. Does that also exist or where you referring to the keys above? Are the keys above formally defined as the PRIMARY KEY? - > > The table is partitioned per day and customer_id (it works great) the > problem is just the duplicated key situation that I'm really worried about. > > I populate the database via a Java Application with JDBC. > > Maybe this info could help to provide some light! > > Thanks Again! > > Leandro Guimarães > > > > On Fri, Jun 14, 2019 at 7:39 PM Tim Cross > wrote: > > > Leandro Guimarães > writes: > > > Hi, > > > >    I have a scenario with a large table and I'm trying to insert > it via a > > COPY command with a csv file. > > > >    Everything works, but sometimes my source .csv file has > duplicated data > > in the previously fulfilled table. If I add a check constraint > and try to > > run the COPY command I have an error that stops the whole insertion. > > > >   I've tried to put the data in a tmp table and fill the main using > > distinct this way (the fields and names are just examples): > > > > INSERT INTO final_table values (name, document) > >    SELECT DISTINCT name, document > >    FROM tmp_TABLE t1 > >    WHERE NOT EXISTS ( > >    SELECT 1 FROM final_table t2 > >    WHERE (t2.name , t2.document) > >    IS NOT DISTINCT FROM (t1.name , t1.document)) > > > > The problem is that my final_table is a large (and partitioned) > table and > > this query is taking a long time to execute. > > > > Someone have any idea (really guys anything would be great) how > to solve > > this situation? I need to ignore duplicates instead to have some > error. > > > > I'm using* PostgreSQL 9.4* so I can't use "ON CONFLICT" and > upgrade is not > > an option. > > > > Explain plan would probably shed some light, but I suspect your > performance is being heavily hit by the sub query. Distinct is an > expensive operation and you are performing it once for every > distinct row > in your temp table. > > It isn't clear what the primary key is for your final table - name + > document seems suspicious given these seem to be the only two columns > your inserting as well. You don't indicate what the data types are > either - it document is something like 'text' then using it in a > distinct clause is likely to have huge performance impact. > > The first thing I'd do is to eliminate duplicates from your temp table > as a separate statement or by pre-filtering the CSV before import. I > would then try something like an outer join to identify rows in your > temp table which don't exist in your final table and select from there > to insert into the final table. You don't really need the distinct in > the sub query as all you really need to know is if (name, document) > exists - it doesn't matter if more than one exists (for this test). > > If you really don't have something more specific for a primary key, > depending on what data type 'document' is and how large it is, you may > find adding a column which is a checksum of your 'document' field a > useful addition. I have done this in the past where I had an application > where name was not unique and we only wanted distinct instances of > 'document' (document was a fairly large XML document in this case). > > -- > Tim Cross > > -- Adrian Klaver adrian.klaver@aklaver.com