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 1ksD1N-0004Bq-Fg for psycopg@arkaria.postgresql.org; Wed, 23 Dec 2020 22:54:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ksD1M-0004eE-En for psycopg@arkaria.postgresql.org; Wed, 23 Dec 2020 22:54:16 +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 1ksD1M-0004e7-7G for psycopg@lists.postgresql.org; Wed, 23 Dec 2020 22:54:16 +0000 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ksD1H-0007vG-S8 for psycopg@postgresql.org; Wed, 23 Dec 2020 22:54:15 +0000 Received: by mail-lf1-x12d.google.com with SMTP id s26so942463lfc.8 for ; Wed, 23 Dec 2020 14:54:11 -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=6c7+4jH66faUwbC2ybZBnBZRSC4vIkagzeRBtWWaBl8=; b=DD1KvwS4S1KyyfoveTSnPjO9A1L2d70cgVTz1C/R4x0dHLvgYY7ArQPSA0tQO0U/mB 0Bazj0VBa93cCKkUlOk4Tt56LIKof8G0Crg4iIbvrP3HEVbpndFkPhds4rxuqyislsl8 p303bi2nooTHSVmpVu9pwSdLQWsUkVNQlm7MNdj4NQplX07osKmia9QvGiLfxdV6DZd+ vCN4fCDDzQxmGiyxTxp5x+1o1vNZ+7vevt95DNHqI4BVvzMiMeKeQ8jHY7pFiyM8ayjj AD74XZHnc9XU6A7jmLdpVMFgGVQ2s6Yq5NDoL0VdNYCVi2KZAb5I6s4cpUbcgOuAq3cN Z3vQ== 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=6c7+4jH66faUwbC2ybZBnBZRSC4vIkagzeRBtWWaBl8=; b=Hhy+62UxrZBIv3LKsBxHGLvwVbcccGHmQR/P4QYhexUdk95XpZoRV1eFbMQBF7ceR1 mIdcu+GvhdwOwi834E0R5YICsVXT3bKM5zhLckcLR5Qsiy7TZj2Ej9rBmXQPuKWrvQoJ RcXBaytlWcy/Ahc8XdmprpEeapkmXHZxAwvPyjDm3eORfaLA6Fv3drFWNyEyJ9z2Gg4P zqYxhdVum0s/guUqw5kewGjKwQEwOI1ajCZd81k+Zvg/4HlpItdDYtMaZ8uKp0bnHfOk PqicldcpzNqS5neTcW+01gi3OJ3faPWnN6jwjbKmtRuKPbGA7yICJq5CbqD7Y45qjMpu Dbqg== X-Gm-Message-State: AOAM533wKZFm0HrpVxYn19JZM8GsH/9LTX0HXhKtCAPkuoAsmpKyLcdU RiFiAQ1tSt2QZ2xwykKv70ZpdG7tBv3DAl4uE4E= X-Google-Smtp-Source: ABdhPJwtftn2tYZ2a6N09cHbckRDuYBl+94wQQhBaWYBpGvyRG0/jzv/zuwJG1zCLwun11GWgsFpI8JfFaucgSWvRvE= X-Received: by 2002:a2e:810c:: with SMTP id d12mr12312013ljg.400.1608764049550; Wed, 23 Dec 2020 14:54:09 -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: Wed, 23 Dec 2020 22:53:58 +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 Tue, 22 Dec 2020 at 22:36, Daniele Varrazzo wrote: > > On Tue, 22 Dec 2020 at 05:39, Vladimir Ryabtsev wrote: > > I am just thinking about type issues in some edge cases... > > What if the data is such that in the first row it has some small integers, > > but on subsequent rows it has larger numbers that do not fit into "integer"? > > There are probably other cases similar to this, including "None" values > > on the moment you want to capture types... Maybe it is more reliable to > > oblige the user, who knows their data better, to explicitly supply the types > > for preparation... Will any type inference work in case of types mismatch? > > If yes, to what extent? > > The only case I have thought where this can happen is with None vs. > not None, in which case the oid info would be missing. Oid selection > is based only on the Python type, not on the specific values (for > instance Python datetime is always passed as timestamptz, never > timestamp, even if the tezinfo is missing, and it relies on Postgres > cast): I have avoided so far to make a choice based on the values > exactly to avoid to find ourselves in this type of situations. > > I don't have in mind a case where a missing value would make a > difference but that doesn't mean that they don't exist. What I think > is that if in a certain context a NULL is acceptable and Python can > pass either a None or e.g. a date, preparing witn unknown or date oid > shouldn't make a difference. Of course it is possible to trigger > ambiguities: trivially the query `SELECT %s` could be prepared with a > date and then you can throw it a string that will make it fail. But if > a placeholder is part of an expression or target for an insert I > believe (but will test too) that preparing with unknown types will be > equivalent. Heads up about this: it's better than I thought! I wrote a first implementation of the prepared statements cache using the query as a key, but it's actually enough to use the (query, types) tuple in order to tell apart statements that are executed with different types. This way even the "SELECT %s" case won't be a problem. Of course a statement executed with a mix of types will be prepared later than `prepare_threshold`, but I think it's perfectly acceptable: the case doesn't happen often and having the query prepared after 10 times instead of 5 doesn't change much if it will be executed hundreds of times or more. What seems a feature-complete branch is available in [1]. The tests [2] illustrate the main behaviour of the prepared statements system. [1]: https://github.com/psycopg/psycopg3/tree/prepared-statements>. [2]: https://github.com/psycopg/psycopg3/blob/prepared-statements/tests/test_prepared.py Off to do some benchmarks now... -- Daniele