Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKOcN-00051I-Pq for pgsql-general@arkaria.postgresql.org; Fri, 03 Apr 2020 15:52:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jKOcM-0004jt-LI for pgsql-general@arkaria.postgresql.org; Fri, 03 Apr 2020 15:52:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKOcM-0004jm-Db for pgsql-general@lists.postgresql.org; Fri, 03 Apr 2020 15:52:26 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKOcK-0004Fw-1j for pgsql-general@lists.postgresql.org; Fri, 03 Apr 2020 15:52:25 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id B7ADC1718; Fri, 3 Apr 2020 11:52:22 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 03 Apr 2020 11:52:22 -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=fm2; bh=D +dNerINB3g7zOleQ3pSL2HrOSYU9cz0B5iDYfHNjkA=; b=UoEX/4oeK29A9Koxw mtOdWe8WM715C/3SAAiN8YAD4VbWuG4A2xIBf+8pQmF98GTW43Qorv2E+hUoX70T NcgNHQpI7ONBOBOAF4d1stpcML/CJp577OdBxPNRp/KpzrDw0V0dbRsXorFQvvYJ 79wIGCihTtO9qcpkGlNais2tbQ1W+N+tNqEZa1hKSeL6jwvZPG7kYXibdpp/XKo+ ygT5vKGECrA/0lY6F2EzryT2w0OIDJiNXJVn8NSXwIqXf54x+hvK4ylfHAfgNL22 OfeJcYFQAwgCWX0MZJD0UogIaRPv5s9wYfx9vAZVyrzxrlnWxTRF1L5gHCuPTVmQ N4OQQ== 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=fm2; bh=D+dNerINB3g7zOleQ3pSL2HrOSYU9cz0B5iDYfHNj kA=; b=gwgmPM4DVunqSaaRCiGachCBT3DvlMYG3ulgoRfuUl7p+wXR0gqaT9dRC RuU9IA2dgKbg2Rfc+Q7mJ6/sr6CFfaNkTjeJQ2xC17lFFf74ByIttJrMXnoeNedp 4IMLFR92YgI5lF9Wg0bSYnn5YfKjOHwf0itMYHPjmqjjshpgCYeQZUqYlomwd81h psSDdIN3gcFLqogkv778+krq7qJhjaQ1a1jchSibXR1oyV2JZKbllD14rYnE40Rm y0pDDOQGP8i3q/fcVMS0GC0ZHke+XNHLENHRsa9s5tVES8msGfsuCD8w/zVeBdcF NyiRj2dBzV8lQ7ldrkulxW6WV01cQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrtdeigdelfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjeehrddujedv rdeltddrledvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (75-172-90-92.tukw.qwest.net [75.172.90.92]) by mail.messagingengine.com (Postfix) with ESMTPA id D4EB6306CFE9; Fri, 3 Apr 2020 11:52:21 -0400 (EDT) Subject: Re: Improve COPY performance into table with indexes. To: James Brauman , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Fri, 3 Apr 2020 08:52:21 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/2/20 9:42 PM, James Brauman wrote: > I am using the COPY command to insert 10 million rows from a CSV file > into a database table and I am finding the performance is > unacceptable. When the COPY command is executed the disk I/O is > extremely high which leads to degraded query performance for other > queries being executed on the database. > > I have tried removing the indexes from the database table and this > dramatically improved performance (both reducing the execution time > and reducing disk I/O). Is there the option to use tablespaces to move the index(s) to another disk?: https://www.postgresql.org/docs/12/sql-alterindex.html " SET TABLESPACE This form changes the index's tablespace to the specified tablespace and moves the data file(s) associated with the index to the new tablespace. ..." > > The constraints I am working with are: > - Indexes must be in place to ensure that other queries that are > executed on the table while the COPY command is executing have > acceptable performance. > - The table should not be locked for reads during the COPY (it is > fine to lock for writes). > > I don't know how COPY is implemented, but it seems that the indexes > are updated as data is inserted into the table. Ideally I would like > to delay updating the indexes until the COPY command has completed and > just update the indexes a single time, I think this would lead to much > better performance. Is this possible? > > Another idea that I had was to start a transaction, drop the indexes, > run the COPY command and then recreate the indexes. The problem with > this approach is that DROP INDEX acquires an exclusive lock on the > table, which is not acceptable in this system (otherwise other queries > won't be able to execute). I tried using DROP INDEX CONCURRENTLY, but > that cannot be executed inside a transaction. > > What are my options in this scenario? > > Thanks > > -- Adrian Klaver adrian.klaver@aklaver.com