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 1pqib7-0000HO-0E for psycopg@arkaria.postgresql.org; Sun, 23 Apr 2023 22:54:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pqib4-0002pZ-JE for psycopg@arkaria.postgresql.org; Sun, 23 Apr 2023 22:54:18 +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 1pqhgT-0005sl-9K for psycopg@lists.postgresql.org; Sun, 23 Apr 2023 21:55:49 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1pqhgR-004gXr-6p for psycopg@postgresql.org; Sun, 23 Apr 2023 21:55:48 +0000 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-51452556acdso2865214a12.2 for ; Sun, 23 Apr 2023 14:55:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682286945; x=1684878945; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JPldzDt1tkYiC5IMYTFgtbHf1GJqCBGFtmCBle5CkcA=; b=pt41BPK1705+PugjinfBDGlBh+dPLqB0P/fz2zGdJ7Szf81YTRlBKcnq9jo94j2h1o vyjt4NzT690/x2efYHaXPKauhhGzY9EPacvXKW5hWkAvaFYS2DBh4ts8weqSxZc6g8zJ VBw7bY7Kf8fplgdm7jBFVXZmA3l2fpfSWpdSCDtk9cCHm0SrzbdcJU+D6+/St9CDGm/A xpBq6aoCAIS/fLPq6Ngt+V6FN2q/FYC9kHveoP1M8Xj6A7TvoEPIQCwYPdIKWfaA+vn2 COVkXCESJAzRIhE7TyPOMogNZvtJ8TdIHON9QU4et4HqhDNJY83hwDsYMK7c60FKbEa8 /BjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682286945; x=1684878945; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JPldzDt1tkYiC5IMYTFgtbHf1GJqCBGFtmCBle5CkcA=; b=hoeI4Uo4Xavnc5j8e3GQoJT/3Et4AEx1KFHnEts3aKncw/bVsqZ1lJYL1nn5hHUG14 aqHcK8+SMpG7g6PNHu5sRfy/lZL1fF8CFC+pYwZ4pSNAVIQvMWdj3dpT8ENOwlVuxk/T 1VQFiTfxV7hnpkCdGtRgvHg0n8nLQgBIadGdPnizW5mMuUSPm5pP/rGYqTyRhbwW5RJB FJmQE/5X3plPZnAoqzSNx6Al9x93c4i+4QOgmEO5ri+fAq5ht543JuKpM0A6pNDbJnd7 q0q1FaqiYXb7VDjIVp+7i/BEH2lv9lCKYIJnMWf/Xt3NYnqywgbB9QzCogNMIAnhq5SP +xNQ== X-Gm-Message-State: AAQBX9d/CFZ7KEhnEzJDksfD14IF72UC6LR228GY1rTwr709iK8y8GL0 TZoGOtO6VW5JsVc4AHPwBScyvys0LAkQz6FlIX7Q0yML X-Google-Smtp-Source: AKy350bMt/peh6fUKGxRHAGVAczQ5fq0m/j+4RjCDBqxqyIpLGob0fMQr6wkuIG8N9BrN4ogA5Wp1rTOVciHtxzZ+Uc= X-Received: by 2002:a17:90b:1286:b0:246:9c9f:59a3 with SMTP id fw6-20020a17090b128600b002469c9f59a3mr12097127pjb.32.1682286944802; Sun, 23 Apr 2023 14:55:44 -0700 (PDT) MIME-Version: 1.0 References: <237c1e3f-4118-6ffc-6e0b-ddce824f1f55@aklaver.com> In-Reply-To: <237c1e3f-4118-6ffc-6e0b-ddce824f1f55@aklaver.com> From: "Michael P. McDonnell" Date: Sun, 23 Apr 2023 16:55:34 -0500 Message-ID: Subject: Re: Consecutive Inserts Freeze Execution of Psycopg3 To: Adrian Klaver Cc: psycopg@postgresql.org Content-Type: multipart/alternative; boundary="000000000000c15e0105fa07f3b2" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c15e0105fa07f3b2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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. On Sun, Apr 23, 2023 at 4:25=E2=80=AFPM 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 =3D queries[qi] > > qparams =3D 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-writte= n > > my code a dozen times and am just slinging mud to see what sticks. > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > --000000000000c15e0105fa07f3b2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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 SqlAlch= emy, upgrading from psycopg2 to psycopg, etc...) but it's working now a= nd I can work around it. Thank you.


On Sun, Apr 23, 2023 at 4= :25=E2=80=AFPM Adrian Klaver <adrian.klaver@aklaver.com> 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):
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 query =3D queries[qi]<= br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 qparams =3D params[qi]=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 with self.connection.c= ursor() as conn:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 conn.exe= cute(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/b= asic/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-writt= en
> my code a dozen times and am just slinging mud to see what sticks.

--
Adrian Klaver
adrian.klave= r@aklaver.com

--000000000000c15e0105fa07f3b2--