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 1pqjsx-0004po-MP for psycopg@arkaria.postgresql.org; Mon, 24 Apr 2023 00:16:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pqjrw-0006f1-TJ for psycopg@arkaria.postgresql.org; Mon, 24 Apr 2023 00:15:48 +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 1pqjrv-0006cq-By for psycopg@lists.postgresql.org; Mon, 24 Apr 2023 00:15:48 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pqjrm-004hdi-RF for psycopg@postgresql.org; Mon, 24 Apr 2023 00:15:46 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 109733200201; Sun, 23 Apr 2023 20:15:33 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sun, 23 Apr 2023 20:15:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc: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= 1682295333; x=1682381733; bh=Q6prYFq2wHMbVXs0hPvcwhhaeV/BA5t1Nqp Yz0BV56k=; b=fEzDZo5BU0Rk6+fknuK1URheTEors55E2+lk55BLSTfEyCtut/A ZQ3Zklg57GwvXS+Vt7exDOkfJL/szfxF/GEuQZWNLQaoNqB2iEVCJdgMdvtXYL1f Td+tCfxRJCi1t/2ynj/ZqJMUJ20UTKJGRLPC3puTKzC8ba2hZDDiSLFYHX1xRfUs iYgjapXQBm2M6jCdxrfuco9/686z4rKeGqT1ab/Lrq1xLXdtKzUnPDy+bt1ut42A Kh7Vk0wcwikOZz1ogCGypa3Em6WRYTVFJyeULbcw3WD1c+z11tnPKQxP1tH+w16x jfzQEV5CxmFKg10PU40R6QRFSK0bDgXwdYQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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= 1682295333; x=1682381733; bh=Q6prYFq2wHMbVXs0hPvcwhhaeV/BA5t1Nqp Yz0BV56k=; b=UPdulokuzDIVDYeAAtq64YeDeR++KJA2qq7ZpLy2jII7LhwACty sPs4A/DScRYhY2A3xJlud2jnnX1UkDUyXfopzTKzMJHRYVGvUVzmgOoxVI297DW6 orJc35SlAKqdlXcHGs83K+3D3R5GFrfLNL1w2BRgpLrFMhg4hURssnjaM6Qj2IK2 qn40bscgfcQ3eXEAsxAGqLSGFrwZrtnDNmuA5qr+ntjob1eboKVDMavb1gr+YnTY G8+ph/FOY2ceD+BaVbprm3BuRqDqtALOCb/QPbW1DYHyJfLsgf4ur3dwRTs3Rkhk 5SVMMqSRHQY8gDpYV+lTAuILilNOTnRzXwQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfedtledgfeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfevfhfhjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpefgfedvge fgieegtdffueffffevtdektdeghfekgeeigedvkefhueffkeeffeefheenucffohhmrghi nhepphhshigtohhpghdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 23 Apr 2023 20:15:33 -0400 (EDT) Message-ID: Date: Sun, 23 Apr 2023 17:15:32 -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" Cc: psycopg@postgresql.org References: <237c1e3f-4118-6ffc-6e0b-ddce824f1f55@aklaver.com> 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 14:55, Michael P. McDonnell wrote: > That helped a ton, I don't understand why I've had to rewrite the crap > out of all of this to get it to work (dropping SqlAlchemy, upgrading > from psycopg2 to psycopg, etc...) but it's working now and I can work > around it. Thank you. Well: 1) SQLAlchemy is an ORM that tries to make all databases look the same. 2) psycopg2 != psycopg. For details see: https://www.psycopg.org/psycopg3/docs/basic/from_pg2.html 3) It would have been more of a surprise if you did not have to change anything. 4) And this with self.connection.cursor() as conn: was just plain wrong. You where trying to make a cursor be a connection and that is not going to work. > > > On Sun, Apr 23, 2023 at 4:25 PM Adrian Klaver > wrote: > > 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 > -- Adrian Klaver adrian.klaver@aklaver.com