public inbox for [email protected]  
help / color / mirror / Atom feed
From: Daniele Varrazzo <[email protected]>
To: Vladimir Ryabtsev <[email protected]>
Cc: Adrian Klaver <[email protected]>
Cc: [email protected]
Subject: Re: psycopg3, prepared statements
Date: Thu, 24 Dec 2020 00:14:23 +0000
Message-ID: <CA+mi_8aoK_wbSQimZ4WHHXZT2YTBPfxLj9Zsbfbx8o=gfBWyaQ@mail.gmail.com> (raw)
In-Reply-To: <CAMqTPq=-CQ6Pm3cjn9j77W7zEqiwz2atxxj+JtbxKYY_xzV-7w@mail.gmail.com>
References: <CA+mi_8aAuORkdXZ9bG_GpU34iPCGktMO83ktn+ODjL+Y=Y=e3Q@mail.gmail.com>
	<[email protected]>
	<CA+mi_8YoZ6_0NM4TbfqE8SQY8-b=RgaBH-jUVxzbWQdp+4oZGQ@mail.gmail.com>
	<[email protected]>
	<CAMqTPq=G1E=b32JxNo6dGk1-9qGHSCP7Puo5FCqf68nakiBo_Q@mail.gmail.com>
	<CA+mi_8YhQVo8xouWnc6hCR6RvE1THRB0Wz0OzHhMHsuvdLNYnw@mail.gmail.com>
	<CA+mi_8akdCJC=q9_EH5n5Prj32ttLKCY8-=yncxRr7K8V2mZhw@mail.gmail.com>
	<[email protected]>
	<CAMqTPq=-CQ6Pm3cjn9j77W7zEqiwz2atxxj+JtbxKYY_xzV-7w@mail.gmail.com>

On Wed, 23 Dec 2020 at 23:23, Vladimir Ryabtsev <[email protected]> wrote:
>
> Cause (query, types) can give more combinations than (query,)?

Yes, that's the reason

    In [1]: import psycopg3
    In [2]: cnn = psycopg3.connect()
    In [3]: cnn.prepare_threshold = 2

    In [4]: cnn.execute("select 1 + %s", [1]).fetchone()
    Out[4]: (2,)
    In [5]: cnn.execute("select 1 + %s", [None]).fetchone()
    Out[5]: (None,)
    In [7]: cnn.execute("select 1 + %s", [2]).fetchone()
    Out[7]: (3,)

After 3 times the expression should have been prepared, but the tally
has been spread in two values (0 is unknown oid, 20 is int oid).

    In [8]: cnn._prepared_statements
    Out[8]: OrderedDict([((b'select 1 + $1', (0,)), 1), ((b'select 1 +
$1', (20,)), 2)])

    In [9]: cnn.execute("select 1 + %s", [3]).fetchone()
    Out[9]: (4,)

However, when either key passes the threshold, eventually preparation happens.

    In [10]: cnn._prepared_statements
    Out[10]:
    OrderedDict([((b'select 1 + $1', (0,)), 1),
                ((b'select 1 + $1', (20,)), b'_pg3_0')])

_pg3_0 is the name under which that combination of query and types is
now prepared (it is local per session).

-- Daniele





view thread (13+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: psycopg3, prepared statements
  In-Reply-To: <CA+mi_8aoK_wbSQimZ4WHHXZT2YTBPfxLj9Zsbfbx8o=gfBWyaQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox