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 1ksTgP-0004J9-QK for psycopg@arkaria.postgresql.org; Thu, 24 Dec 2020 16:41: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 1ksTgO-00046n-Oc for psycopg@arkaria.postgresql.org; Thu, 24 Dec 2020 16:41:44 +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 1ksTgN-00046g-EZ for psycopg@lists.postgresql.org; Thu, 24 Dec 2020 16:41:44 +0000 Received: from [66.111.4.27] (helo=out3-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ksTgH-0002nN-K8 for psycopg@postgresql.org; Thu, 24 Dec 2020 16:41:40 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id D57B35C0099; Thu, 24 Dec 2020 11:41:26 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 24 Dec 2020 11:41:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=7 +nNu+eeRZfJR7IyiUTXOI7oBIrgd3JbETxcHorNxp8=; b=COpZtrWL2sdNTDyrB c1HzoUDBmrdLJiMEgLyD2f6uSU13uN/9voEh6SnztLJ9p04/lvEqL6s94mJNfkB5 e5Xe8Fe9t+wQS/T3ueu/by/46lj34wv2sDP9DkSfA2U7dLfaf5Gu6hd0DbJERyR1 dAq8Yo37DZXptv5WgPAyACws2DhRWRS1dHTVWsI/kHoUw1sYGUmZul5Hb9fCmKFj B20ixULie+aaIh8lK5f/8oQULUXcTWhNZtcV82Quw1dTZ1mSP/WV7+YyP0TxWEOT Q/HoGZGpb/2/1jxISfdty4mws6bdVHFzsL3J+E1nDS2P/r/WrrIe4Sw3Lmys+NLH HekQg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=7+nNu+eeRZfJR7IyiUTXOI7oBIrgd3JbETxcHorNx p8=; b=eJsvgkxc6GV5q32SLXiKt3D4qWaT5kuUBbRYnV+uh7tOmYQCMxi5Eg97J PR4kdKSlQVoZBCsPIGRYRe+W4vXHl+bfQySpH2SJ031FKXycEmrgcL2YtjFB5ULU uyClSmVlGSMU4o+DwW9oSkoiYVTfBryPoVSqHA3yoftWOAg9FGxd/AZV1hiSQBLD uyIKWUx65cE6jKqH8E+5pqOohj80nEampORS5+bPjrC9h/baw5tGRIs5tYvswEwX ESBaymjZyZLGMr7QFsqpbj4RXfXroZEqmXDc+WwAucEhgR6FZsF3ToC8MY/GzeU+ kRcTKaSWjDMqzPWh7RJ2ExjoXSuvQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvddtledgledvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeetjedvteffjeeuvdelieeffeehuedthffgvdfhhffh kedvudevgedvjedufefgveenucfkphepjeehrddujedvrddugedrudejnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgr vhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.4] (75-172-14-17.tukw.qwest.net [75.172.14.17]) by mail.messagingengine.com (Postfix) with ESMTPA id 401F0108005F; Thu, 24 Dec 2020 11:41:26 -0500 (EST) Subject: Re: psycopg3, prepared statements To: Daniele Varrazzo , Vladimir Ryabtsev Cc: psycopg@postgresql.org References: <25a137a0-9ad3-bb9f-b008-263dcc81a645@aklaver.com> <331f273f-fc13-8d80-b209-c94c0960268b@aklaver.com> From: Adrian Klaver Message-ID: <3a5a5655-51f6-4251-bbb9-64964e423e5b@aklaver.com> Date: Thu, 24 Dec 2020 08:41:25 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.27 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/23/20 4:14 PM, Daniele Varrazzo wrote: > On Wed, 23 Dec 2020 at 23:23, Vladimir Ryabtsev 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,) Alright, I was misunderstanding. I thought you where referring to something like: cur.execute("select * from some table where id > %s and user_name = %s and current = %s", [2, 'aklaver', True]) > > 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 > -- Adrian Klaver adrian.klaver@aklaver.com