On Wed, 23 Dec 2020 at 23:23, Vladimir Ryabtsev <greatvovan@gmail.com> 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