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 1ksEHA-0006xb-R4 for psycopg@arkaria.postgresql.org; Thu, 24 Dec 2020 00:14:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ksEH9-0001k4-Q9 for psycopg@arkaria.postgresql.org; Thu, 24 Dec 2020 00:14:39 +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 1ksEH9-0001jw-IO for psycopg@lists.postgresql.org; Thu, 24 Dec 2020 00:14:39 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ksEH7-0000KP-DJ for psycopg@postgresql.org; Thu, 24 Dec 2020 00:14:38 +0000 Received: by mail-lf1-x12a.google.com with SMTP id m25so1235848lfc.11 for ; Wed, 23 Dec 2020 16:14:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2IkaWDx2VCanD3xD8FvvdoXzz5MbrJNf/NoTvGu8RoU=; b=EDv7lFnoPyz1UC69Dm8ZSsMm6lAUDdURdkVxjQie2JdBEUh+ikGHnxQLHXUYSogtKJ DVe8Jtxp7D6pYyV3sd98f8mVPblfAi4pti8LiVYDQalso/G/XP7inKW9MMbb/dorr9K8 G3Eqcii1depGKhs9wHGWUomwk4UUbd6/U5+2jNCrEkx1DwAmGqVatnqYoBbGXWx52nbl CJBLSdr8Get8/wt/z08uH2Zd13axoijtgM+2ru+wFQf0+Kz2E2ffDnoTGaJ0XtZQQ0h1 rEDmlABdd703hVCF5Jf7LiPL8NnHukNodljuV/aIYfsa7UJIK2br75w4Oo96BBvfHJkQ 8zHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2IkaWDx2VCanD3xD8FvvdoXzz5MbrJNf/NoTvGu8RoU=; b=AGy3/YYHzC0T/bUdjF0rJt6+7ofT11WLPYpDJS7NOYNyYpHZZ0NSyQR5Na1z/KdkPx XeGyPoQi4hAF/y5yesYdeMR0K+lXD5tPI2sCuDvyG/CK2r8qq6ZWfQ7OXMzXTp5Et79B 5VfHEtkK9SAalprZQCKK5v95/6a3tgY1DCUm7nvVVSwqsflRSmJ2GQGeYSNsEX6gVr/5 k7nauQxCdxqNUkkVkbxn4DDkTC1mA/cMwr5K8ZaODnWunGkaEDb7P8bG5HsRoO/tzAYI SwtUOoHhSN9U6bLdqFulm5vn96Z0J979ZC2lKCx4fVTef62ah89RsrvIX0B924VvuxQ2 aG6w== X-Gm-Message-State: AOAM531HWFMdRn9TuNLLFGSLz6hD38t0SeNmx0A8YgLFt1oMmtEIM9AA pAI0wc5R3p2YYO0pow1u1reI2i58EylFF1y5HCI= X-Google-Smtp-Source: ABdhPJzF1NQTsjt+B9CPUHY4IcGKsD9jc9zosKtGyO5bdom7TXGRRY9lqRAp2SVwjksO47wT+iM8MAAD0b4Z9Ui9PBM= X-Received: by 2002:a2e:88c3:: with SMTP id a3mr12279382ljk.230.1608768874884; Wed, 23 Dec 2020 16:14:34 -0800 (PST) MIME-Version: 1.0 References: <25a137a0-9ad3-bb9f-b008-263dcc81a645@aklaver.com> <331f273f-fc13-8d80-b209-c94c0960268b@aklaver.com> In-Reply-To: From: Daniele Varrazzo Date: Thu, 24 Dec 2020 00:14:23 +0000 Message-ID: Subject: Re: psycopg3, prepared statements To: Vladimir Ryabtsev Cc: Adrian Klaver , psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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,) 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