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 1nQCxr-00031B-9n for pgsql-performance@arkaria.postgresql.org; Fri, 04 Mar 2022 18:47:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nQCxp-0007s4-BH for pgsql-performance@arkaria.postgresql.org; Fri, 04 Mar 2022 18:47:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nQCxp-0007rG-1J for pgsql-performance@lists.postgresql.org; Fri, 04 Mar 2022 18:47:41 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nQCxm-0003K1-Ii for pgsql-performance@lists.postgresql.org; Fri, 04 Mar 2022 18:47:40 +0000 Received: from bruce by momjian.us with local (Exim 4.94.2) (envelope-from ) id 1nQCxj-0093Lh-At; Fri, 04 Mar 2022 13:47:35 -0500 Date: Fri, 4 Mar 2022 13:47:35 -0500 From: Bruce Momjian To: Tom Lane Cc: aditya desai , Pgsql Performance Subject: Re: Any way to speed up INSERT INTO Message-ID: References: <3967140.1646419359@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3967140.1646419359@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Mar 4, 2022 at 01:42:39PM -0500, Tom Lane wrote: > aditya desai writes: > > One of the service layer app is inserting Millions of records in a table > > but one row at a time. Although COPY is the fastest way to import a file in > > a table. Application has a requirement of processing a row and inserting it > > into a table. Is there any way this INSERT can be tuned by increasing > > parameters? It is taking almost 10 hours for just 2.2 million rows in a > > table. Table does not have any indexes or triggers. > > Using a prepared statement for the INSERT would help a little bit. Yeah, I thought about that but it seems it would only minimally help. > What would help more, if you don't expect any insertion failures, > is to group multiple inserts per transaction (ie put BEGIN ... COMMIT > around each batch of 100 or 1000 or so insertions). There's not > going to be any magic bullet that lets you get away without changing > the app, though. Yeah, he/she could insert via multiple rows too: CREATE TABLE test (x int); INSERT INTO test VALUES (1), (2), (3); > It's quite possible that network round trip costs are a big chunk of your > problem, in which case physically grouping multiple rows into each INSERT > command (... or COPY ...) is the only way to fix it. But I'd start with > trying to reduce the transaction commit overhead. Agreed, turning off synchronous_commit for that those queries would be my first approach. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.