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 1pqkYU-00077Y-Ad for psycopg@arkaria.postgresql.org; Mon, 24 Apr 2023 00:59:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pqkYT-0008RO-8F for psycopg@arkaria.postgresql.org; Mon, 24 Apr 2023 00:59:45 +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 1pqkYS-0008RE-T0 for psycopg@lists.postgresql.org; Mon, 24 Apr 2023 00:59:45 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pqkYQ-001XTk-24 for psycopg@postgresql.org; Mon, 24 Apr 2023 00:59:43 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id DBA003200919; Sun, 23 Apr 2023 20:59:39 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 23 Apr 2023 20:59:40 -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= 1682297979; x=1682384379; bh=/NAnuzHOGN5vz0bAqFZGYlL6MIL5FTYj6Zu lVpsq8Tc=; b=jSSKlOw4Rkxj+D1p4L6byjVvHhkiiCQBO8PuTiNY4TxDY410Wgj CvuSXgh/hJcJFyH3du9TYirgLmgTWkJ4ztWvu7oe65CEmnR4EFPi83uIh3WvKKPy F0NRcVPqOjAuDsPjsT1Hq0BYcGqvY3U+WGzp8HULKt7YXS3b/Ozc8b16/tQH5gll nVh96D7JUlLHvE3tV+8FzCxBZhh2u2SguZImZKmt6UGTk6wv7ZOPVAqQI9RjL6zJ db+tDOQawKUitlLS5pKwDws79gcpgPtl8DhLek3gOizwTIjb7y+JVYIGlByjz1Sh DLgJZIxdx6LrPm7HpSgy3aL1tAmFl3O5klg== 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= 1682297979; x=1682384379; bh=/NAnuzHOGN5vz0bAqFZGYlL6MIL5FTYj6Zu lVpsq8Tc=; b=kUX0Rj99/nVFFAijdXNxw8PY1LZ4AwkvxvWGLcz4MXW4f7kcOEc +7lA+9kAYNaAxOtYyk9R+hBIIqo+oWuiPzaWM4Unn68q5N3Wwx+cv3vAZMs0YoA4 ob4QuEep7l7RCibV0V/7Xxj7AgVljqyGNCzh8BxHioy8fMGnxaKJkcPpnowFv6GD B8ZjU5fnAffdtH3pslyCzZacjAsWcb8MgVCyw7IhHEIbaeJlowA0+bGg9mV4ttUE bYL5U3rwoZgprZRlmsEm0yOaNoil0wcUpk/JvqVLZVfVcVQsIaSijAwTlaWHxJp5 eZn6KztwJt6BNfOJ3c2Jy//8bHe/RK0RpoA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfedtledggeduucetufdoteggodetrfdotf 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:59:38 -0400 (EDT) Message-ID: <04fea413-8776-dd4e-7f0e-4b235b454f19@aklaver.com> Date: Sun, 23 Apr 2023 17:59:38 -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 Content-Language: en-US To: "Michael P. McDonnell" Cc: psycopg@postgresql.org References: <237c1e3f-4118-6ffc-6e0b-ddce824f1f55@aklaver.com> 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 17:26, Michael P. McDonnell wrote: > Thanks Adrian - > I appreciate it; and I've been pouring through documentation to try and > get to this point. > I can't help but feel I'm doing it "wrong" but no website I can find > recently seems to have a "right" way of doing things that's reasonably > kept up. > It would be nice if "wrong" had a way of shooting me in the foot with > verbose errors or warnings. Start here: https://www.psycopg.org/psycopg3/docs/basic/index.html and work through the sections in order. > > -Mike > > On Sun, Apr 23, 2023 at 7:15 PM Adrian Klaver > wrote: > > 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 > -- Adrian Klaver adrian.klaver@aklaver.com