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 1hbuJu-0005Jh-Fv for pgsql-general@arkaria.postgresql.org; Fri, 14 Jun 2019 22:05: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 1hbuJs-0001Iu-3M for pgsql-general@arkaria.postgresql.org; Fri, 14 Jun 2019 22:05:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbuJr-0001Im-Po for pgsql-general@lists.postgresql.org; Fri, 14 Jun 2019 22:05:11 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbuJo-0003YD-Qh for pgsql-general@postgresql.org; Fri, 14 Jun 2019 22:05:11 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 1AB7E2214C; Fri, 14 Jun 2019 18:05:07 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 14 Jun 2019 18:05:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=5 wEEhssVeTwgNNAESwWQ549EnAsk+oXAWXCxTVzsy2A=; b=uHJQWIFei00OK7+6J jR9CUzLJam7uHrUMCd8i7p3Al5tfgaZZsloItkp4b2UZdB13oGSyqJn9i92MP/Uz MWc74acssu4XBCy7iv9F+0MnUnobDCg92X6Nbdy3A0LnWupYIasp/ZCNxwDSQ9oL kNObExpsQs8rPnaj84PgCcufkyypnwnuE9i+qadIYwHzF70Wj305Xm7I/CcMa1T1 wbyNXktEfJuBxSbo4dk+cUrlEoh15NfOQVy26eQSPKLXVlFpNIuKVArsa1asgjPX 3fgagKGoy1mmdIoa/OsAMBDUn37yCQVVrliRkFmS2yUMmKzqkYgQWLbQHM5tV5wg iGHNQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=5wEEhssVeTwgNNAESwWQ549EnAsk+oXAWXCxTVzsy 2A=; b=toh2+ubXocjpUo9k5ZSWlARgqGH2wncP5cejtPak801gjIWYgcoOJmTj1 PqXYY3dg7O9PtVwusFVFfNhhDifMvpBycLLZLGAEbfwGTH3fvTZII0q4K2VMkaT4 D/Ga0tHfbsognTnEVfF04ybM/rZleZIli1wKs28hgpSYkJxT+sjfTnse3SPjnEUL b1NLxujdyvlge1aVcIivG5AirvOeW5+e9OvSxBElZxfKzp6oO9sGgPE08UMpRW1n 7dAlgniLRZVJ0tKZZgpFhW3bxTsLNc418pxE+F1WdMI0BMYpfNVoANwvFnZoh75y 4xUHU31H1en4APdl6c4zjfNWHtixg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrudeivddgtdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepthdurdhnrghmvgdpthdvrdhnrghmvgenucfkphephedtrddu leejrdektddrvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id 389FB8005A; Fri, 14 Jun 2019 18:05:06 -0400 (EDT) Subject: Re: Copy Bulk Ignore Duplicated To: =?UTF-8?Q?Leandro_Guimar=c3=a3es?= , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <46160c63-4e2f-ad70-5c33-260eef49557d@aklaver.com> Date: Fri, 14 Jun 2019 15:05:05 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 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 2:04 PM, Leandro Guimarães wrote: > 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)) Hit enter too soon on previous post. Alternative to above query(rough code): DELETE FROM tmp_TABLE where ; INSERT INTO final_table VALUES () SELECT * tmp_TABLE; > > 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. > > Thanks and Kind Regards! > > > Leandro Guimarães > -- Adrian Klaver adrian.klaver@aklaver.com