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 1pqhDD-0003gW-H1 for psycopg@arkaria.postgresql.org; Sun, 23 Apr 2023 21:25:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pqhDB-00005q-QC for psycopg@arkaria.postgresql.org; Sun, 23 Apr 2023 21:25:33 +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 1pqhDA-00005h-3L for psycopg@lists.postgresql.org; Sun, 23 Apr 2023 21:25:33 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pqhD6-001Vrq-PH for psycopg@postgresql.org; Sun, 23 Apr 2023 21:25:30 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 24F175C00B4; Sun, 23 Apr 2023 17:25:27 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 23 Apr 2023 17:25:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm2; t= 1682285127; x=1682371527; bh=oyXdd4rvVYJbuZtfKBjKHnVxr2p93IMF+VB HfmjmHbo=; b=qaGLQAyh3iemppqJ3KVrWWp8Ko9e0ZlWDBiwN7aNgdWOsNWbT6D fqG1bmAG0lMGLGVjiaQTP1xJ9RYMGkoh4G+yu7XUlaZQghg1HWp9fY05+M8ahrfA n6mY7Yvii8MCM00pofPRZYx7+aES09fj87iECZtfUMgvqQJa7Pc8ymJkp+Dm/AJ1 bRYcq6Vhy59vCQ603xiY62E2J4VgRx+SmHJ1hTfA6PeFsH5WEIgWDLOoJ44BVFph PX1LXke8qu347VHIK+CpTMPvEYr6hpskAdDjZ4daRZMu9GkGdJxE3OEX8S4gPnUj 6G3OY+dhv0fXEB4WwJwOSbK4aWC5ps0yyrw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1682285127; x= 1682371527; bh=oyXdd4rvVYJbuZtfKBjKHnVxr2p93IMF+VBHfmjmHbo=; b=V e2xoYpndmC/1vwMUR5eJ09VIkzS5puQ1f7CkF1zLqXcXKY6/K7RZt8YaC+1qQjdj XMQv4mfLB2hZv1H4cWyIrhWwKCH9z0nuxU+c2hjTN7ut7XG3DFpaxsxeQYjLXAWv /pOpbaT9CNsSY+DhsRVRGMO04CxC2lBZfD4ZlYU/82ut7GMmiPoYMyanDsyDFd/j JknbxXLR/g7fezZMyR9xQxzSiBvkr6+R9OMYq5XSpivsjZYlgUOWMSRId5qYQFZt bp2DkRFfQ4atRv6giZCHQTgAk0U8FRTHBd7Gcdlvl/jKd6D/368qoNvRSWl305XO EnARWM+s9FsiNp9H+FJ/w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfedtkedgudeitdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvfhfhjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeethefgve dtvedutdejudfgfeffvefgffeludehkeffffduteevtdelfffhjeduheenucffohhmrghi nhepphhshigtohhpghdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 23 Apr 2023 17:25:26 -0400 (EDT) Message-ID: <237c1e3f-4118-6ffc-6e0b-ddce824f1f55@aklaver.com> Date: Sun, 23 Apr 2023 14:25:25 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: Consecutive Inserts Freeze Execution of Psycopg3 To: "Michael P. McDonnell" , psycopg@postgresql.org References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/23/23 13:45, Michael P. McDonnell wrote: > Python 3.10.6 > psycopg library 3.1.8 > > Running consecutive inserts sourced in files. > All inserts are of the same format: > > INSERT INTO _____ (field1, field2, field3) > SELECT field1, field2, field3 FROM ____, Join ___, join ___ etc... > > The code I've written is this: > > for qi in range(qlen): >             query = queries[qi] >             qparams = params[qi] >             with self.connection.cursor() as conn: >                 conn.execute(query, qparams) In above you are running the context manager(with) over the cursor not the connection. This will not automatically commit the transaction. You will need to either explicitly do connection.commit() or use the with over the connection per: https://www.psycopg.org/psycopg3/docs/basic/transactions.html > > When I run the queries in dbeaver - the first query takes 120s (it's > 1.9M rows), the second query takes 2s (7000 rows). > When I run the queries in python - it freezes on the second query. > > Any guidance on how to attack this would be awesome as I have re-written > my code a dozen times and am just slinging mud to see what sticks. -- Adrian Klaver adrian.klaver@aklaver.com