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 1pqrKU-0007k4-3q for psycopg@arkaria.postgresql.org; Mon, 24 Apr 2023 08:13: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 1pqrKT-0006Y5-1d for psycopg@arkaria.postgresql.org; Mon, 24 Apr 2023 08:13:45 +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 1pqkf7-0001LF-7P for psycopg@lists.postgresql.org; Mon, 24 Apr 2023 01:06:37 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1pqkf3-004hzc-Q0 for psycopg@postgresql.org; Mon, 24 Apr 2023 01:06:36 +0000 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-63b73203e0aso24275706b3a.1 for ; Sun, 23 Apr 2023 18:06:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682298391; x=1684890391; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=yFneZfWlbtOGIJg554jiuAw5/TlMVfs6vTRlb1IMaZw=; b=Eny53WEOfkwRZG+PkYT5PLoRdz6BlAoRqR9s3Wm5qCTIccNxGjRhA48Da4BPPUHo1Q W2YrFXkqv2uhvDR6GY8XR/xLcOfdTkPnPLK7wtJ8QmX2k80vpPWti8pu5aR1DFGQ7lft e4MRtKAhQhDtSmdV+HQ3IozN+Z/GPQp9+ZX59IEBSPQpkwv2Cos34JevJe4WM6K/nkv8 eWDKpwS51FEsR6APXNg8mL7HHvF2GSu2VLCHYwrSoY50Bj0UcFFDNbgtftTJR044uGCM CSLnLoQXpvTXh1RLLBaN56XC4LKxnDYlQ9xh2IiLPeASitOELyGeLaWH7RvLUyfCM91+ 1OhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682298391; x=1684890391; 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=yFneZfWlbtOGIJg554jiuAw5/TlMVfs6vTRlb1IMaZw=; b=YZZy4HMtDBfcmhjwgtAy0IUUciwV8wueCH+XI2ufcFUd9JzS/eXA5/wFuJbn+2/0oQ D26sKQbjdLI/1LOd2+6HUmGRWP1Gg8wxMipnJar6rV90baSpFLts8gt/35RxpnxUokkp GhaJuDyAB5fMGqqyARNjOAmTOtmh0ZsHvQI1EGi1eIG5uZVvniNsJkSkJCftdZvKrc3M ZEBnM8jb/M/7UQytpZWJyRXn7/AQlrtIsO1wgLhHn4B9vz0GhIvL9zp3RS98cw7JNBG8 vb2a6eunAOXRv4HfB0KjH0IbVNvHj1en0BD+LpmhwnfIeoUsiTK/6M9FoJnJMKOu4xij idXA== X-Gm-Message-State: AAQBX9dpfOsXZ7I6N9cflemCBF9fQmg+PR/SChCpfiRaABRoxjIPrE04 upJahwBxDYyvcasKrN1WLcxv+1qBoRjm7DJDKcU= X-Google-Smtp-Source: AKy350aSr45bldVtUmUdBLqtlbyajC3g+WzYr4S645xOgMab85iSqsjzeOr+02bjoCH0BCsmM1XaarIONVFGI80gnY8= X-Received: by 2002:a17:90b:1bc1:b0:247:14ac:4d3a with SMTP id oa1-20020a17090b1bc100b0024714ac4d3amr13763909pjb.20.1682298391174; Sun, 23 Apr 2023 18:06:31 -0700 (PDT) MIME-Version: 1.0 References: <237c1e3f-4118-6ffc-6e0b-ddce824f1f55@aklaver.com> <04fea413-8776-dd4e-7f0e-4b235b454f19@aklaver.com> In-Reply-To: <04fea413-8776-dd4e-7f0e-4b235b454f19@aklaver.com> From: "Michael P. McDonnell" Date: Sun, 23 Apr 2023 20:06:20 -0500 Message-ID: Subject: Re: Consecutive Inserts Freeze Execution of Psycopg3 To: Adrian Klaver Cc: psycopg@postgresql.org Content-Type: multipart/alternative; boundary="00000000000003237205fa0a9e25" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000003237205fa0a9e25 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable So I appreciate you're trying to point to the official source of the documentation; but one place where FastAPI might be a place of inspiration is the consistent building of a single use case example to show how one might do something more complex. Given the docs available - I have no idea how I might take a dict and generate an UPDATE statement using the sql.SQL API you have. And I'm genuinely not trying to nitpick here, I'm relatively new to Python and am more or less winging it and the documentation immediately available doesn't exactly spell out *all the things*; So again - thank you for your help in getting me over the original hump; I appreciate it. On Sun, Apr 23, 2023 at 7:59=E2=80=AFPM Adrian Klaver wrote: > 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=E2=80=AFPM 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 th= e > > 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 !=3D 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=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-written > > > > my code a dozen times and am just slinging mud to see wha= t > > sticks. > > > > > > -- > > > Adrian Klaver > > > adrian.klaver@aklaver.com > > >> > > > > > > > -- > > Adrian Klaver > > adrian.klaver@aklaver.com > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > --00000000000003237205fa0a9e25 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
So I appreciate you're trying to point to the off= icial source of the documentation; but one place where FastAPI might be a p= lace of inspiration is the consistent building of a single use case example= to show how one might do something more complex. Given the docs available = - I have no idea how I might take a dict and generate an UPDATE statement u= sing the sql.SQL API you have.=C2=A0

And I'm g= enuinely not trying to nitpick here, I'm relatively new to Python and a= m more or less winging it and the documentation immediately available doesn= 't exactly spell out *all the things*; So again - thank you for your he= lp in getting me over the original hump; I appreciate it.

On Sun, Apr = 23, 2023 at 7:59=E2=80=AFPM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 4/23/23 17:26, Michael P. McDon= nell wrote:
> Thanks Adrian -
> I appreciate it; and I've been pouring through documentation to tr= y and
> get to this point.
> I can't help but feel I'm doing it "wrong" but no we= bsite I can find
> recently seems to have a "right" way of doing things that= 9;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/in= dex.html

and work through the sections in order.

>
> -Mike
>
> On Sun, Apr 23, 2023 at 7:15=E2=80=AFPM Adrian Klaver <adrian.klaver@aklaver.co= m
> <mailto:adrian.klaver@aklaver.com>> wrote:
>
>=C2=A0 =C2=A0 =C2=A0On 4/23/23 14:55, Michael P. McDonnell wrote:
>=C2=A0 =C2=A0 =C2=A0 > That helped a ton, I don't understand why= I've had to rewrite the
>=C2=A0 =C2=A0 =C2=A0crap
>=C2=A0 =C2=A0 =C2=A0 > out of all of this to get it to work (droppin= g SqlAlchemy, upgrading
>=C2=A0 =C2=A0 =C2=A0 > from psycopg2 to psycopg, etc...) but it'= s working now and I can
>=C2=A0 =C2=A0 =C2=A0work
>=C2=A0 =C2=A0 =C2=A0 > around it. Thank you.
>
>=C2=A0 =C2=A0 =C2=A0Well:
>
>=C2=A0 =C2=A0 =C2=A01) SQLAlchemy is an ORM that tries to make all data= bases look the same.
>
>=C2=A0 =C2=A0 =C2=A02) psycopg2 !=3D psycopg. For details see:
>
>=C2=A0 =C2=A0 =C2=A0https://www.psycopg= .org/psycopg3/docs/basic/from_pg2.html
>=C2=A0 =C2=A0 =C2=A0<https://www.psy= copg.org/psycopg3/docs/basic/from_pg2.html>
>
>=C2=A0 =C2=A0 =C2=A03) It would have been more of a surprise if you did= not have to change
>=C2=A0 =C2=A0 =C2=A0anything.
>
>=C2=A0 =C2=A0 =C2=A04) And this
>
>=C2=A0 =C2=A0 =C2=A0with self.connection.cursor() as conn:
>
>=C2=A0 =C2=A0 =C2=A0was just plain wrong. You where trying to make a cu= rsor be a connection
>=C2=A0 =C2=A0 =C2=A0and that is not going to work.
>
>
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 > On Sun, Apr 23, 2023 at 4:25=E2=80=AFPM Adria= n Klaver
>=C2=A0 =C2=A0 =C2=A0<adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com&= gt;
>=C2=A0 =C2=A0 =C2=A0 > <mailto:adrian.klaver@aklaver.com
>=C2=A0 =C2=A0 =C2=A0<mailto:adrian.klaver@aklaver.com>>> wrote:
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0On 4/23/23 13:45, Michael = P. McDonnell wrote:
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > Python 3.10.6
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > psycopg library 3.1.= 8
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > Running consecutive = inserts sourced in files.
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > All inserts are of t= he same format:
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > INSERT INTO _____ (f= ield1, field2, field3)
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > SELECT field1, field= 2, field3 FROM ____, Join ___, join
>=C2=A0 =C2=A0 =C2=A0___ etc...
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > The code I've wr= itten is this:
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > for qi in range(qlen= ):
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 query =3D queries[qi]
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >=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 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 with self.connection.cursor() as conn:
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 conn.execute(query, qparams)
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0In above you are running t= he context manager(with) over the
>=C2=A0 =C2=A0 =C2=A0cursor not
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0the connection. This will = not automatically commit the
>=C2=A0 =C2=A0 =C2=A0transaction. You
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0will need to either explic= itly do connection.commit() or use
>=C2=A0 =C2=A0 =C2=A0the with
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0over the connection per: >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 > https://w= ww.psycopg.org/psycopg3/docs/basic/transactions.html
>=C2=A0 =C2=A0 =C2=A0<https://www= .psycopg.org/psycopg3/docs/basic/transactions.html>
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0
>=C2=A0 =C2=A0 =C2=A0 =C2=A0<http= s://www.psycopg.org/psycopg3/docs/basic/transactions.html
>=C2=A0 =C2=A0 =C2=A0<https://www= .psycopg.org/psycopg3/docs/basic/transactions.html>>
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > When I run the queri= es in dbeaver - the first query takes
>=C2=A0 =C2=A0 =C2=A0120s (it's
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > 1.9M rows), the seco= nd query takes 2s (7000 rows).
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > When I run the queri= es in python - it freezes on the
>=C2=A0 =C2=A0 =C2=A0second query.
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > Any guidance on how = to attack this would be awesome as I have
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0re-written
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0 > my code a dozen time= s and am just slinging mud to see what
>=C2=A0 =C2=A0 =C2=A0sticks.
>=C2=A0 =C2=A0 =C2=A0 >
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0--
>=C2=A0 =C2=A0 =C2=A0 >=C2=A0 =C2=A0 =C2=A0Adrian Klaver
>=C2=A0 =C2=A0 =C2=A0 > adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>=C2=A0 =C2=A0 =C2=A0<mailto:
adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.c= om>>
>=C2=A0 =C2=A0 =C2=A0 >
>
>=C2=A0 =C2=A0 =C2=A0--
>=C2=A0 =C2=A0 =C2=A0Adrian Klaver
>=C2=A0 =C2=A0 =C2=A0adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com><= br> >

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

--00000000000003237205fa0a9e25--