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 1kraOb-0007sU-AM for psycopg@arkaria.postgresql.org; Tue, 22 Dec 2020 05:39:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kraOY-00045V-Cb for psycopg@arkaria.postgresql.org; Tue, 22 Dec 2020 05:39:38 +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 1kraOY-00045O-0Y for psycopg@lists.postgresql.org; Tue, 22 Dec 2020 05:39:38 +0000 Received: from mail-ua1-x92b.google.com ([2607:f8b0:4864:20::92b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kraOR-0005cr-23 for psycopg@postgresql.org; Tue, 22 Dec 2020 05:39:36 +0000 Received: by mail-ua1-x92b.google.com with SMTP id t15so4030569ual.6 for ; Mon, 21 Dec 2020 21:39:30 -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=5uamhKfHo4J8SBXShumLRey2OOVzFR8TCN49W8StQtU=; b=oPb78xGQxQzFHU7WVtN0GYhYARuBQm9DOf4bEvjn0q4sXsyFZmR7xiS8z53B7ypR8o Hm131Yg7pmeGX70lhWjOXSomerZBbsIRvQKOVWPcieb4iMVhjtgNXSIgk/iEbJYWwN9O GSE+cKoOTRCklKc9263ulPFfSEpjpI0GNSSkA+dcG+1pLlnvxBfWaoGcqPue5oMfqJ4+ UASogh/8dD/Ci5zI7WvOQi0Oqyu1Zds27iSy3719uB9dtduV9ImihZC7iXcYonUvnIUc hbSIPUmDep6ZiFMJQR3GVl4gWuzdhx0/scrZBT6NnWev3b6Qul1vd18LW+RGlyAmMXVL gDjQ== 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=5uamhKfHo4J8SBXShumLRey2OOVzFR8TCN49W8StQtU=; b=oWXSvuFnVNT+ydE8jN7+BTEXP1+gg+ZPoAkqCO6w4oJq4gk5vG+fJC2v3DvOIWVXDM UeSAx9j0h7L4F3Q4yCDaCtWLWG8JQ+LH+oa5lSXkp4NgUfPpogCckkpfGcK6TjzV/ZSb lSGtWWrKkRdaR7tpr3O25zcMGkcnaCeUK5T8G3/0gdkCxpJRfap26jnymPOwgiU4Vct0 zbaR8t9bpbTDK0BDRQ/GcPCRnQZiUlpJtnFnJm4n3qp6oy4Xga9MEUgBN8J/PemAVnUv 4l6BLS1K7MNORZIAwOyE3VjqtllEY8dSICHmNemM0LXIgYYBc/VkhOe8xQ7SI8f8hcJs iVIw== X-Gm-Message-State: AOAM530cWhu5GEEtIt8lI2A7TUKnS4dvyOKN4e3V88vGXw5zJiSdjyNn d/x9c/MJ1hNd/UKBpDAlyB9KxfjT9mvENTGoTms= X-Google-Smtp-Source: ABdhPJyzq7ZzW17n4Vkdsu7+LUn3QrGvIFIMDlZiEIuadXhAeQybBdT6tT63sc2mlKQhQhGmPO30wwxjYcPuw5qBu8I= X-Received: by 2002:ab0:39c8:: with SMTP id g8mr14324917uaw.120.1608615569921; Mon, 21 Dec 2020 21:39:29 -0800 (PST) MIME-Version: 1.0 References: <25a137a0-9ad3-bb9f-b008-263dcc81a645@aklaver.com> <331f273f-fc13-8d80-b209-c94c0960268b@aklaver.com> In-Reply-To: <331f273f-fc13-8d80-b209-c94c0960268b@aklaver.com> From: Vladimir Ryabtsev Date: Mon, 21 Dec 2020 21:39:19 -0800 Message-ID: Subject: Re: psycopg3, prepared statements To: Adrian Klaver Cc: Daniele Varrazzo , psycopg@postgresql.org Content-Type: multipart/alternative; boundary="000000000000a01a5305b706ffcb" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000a01a5305b706ffcb Content-Type: text/plain; charset="UTF-8" > it's a moment in your > program's lifetime where you know the query to run and the arguments: > most likely now you want to run the query too, not only to prepare it. 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? Generally I think that automatic preparation is a great idea, but if it is achieved by sacrificing reliability to any extent, I would prefer to have such a feature switched off by default. > - if more than `connection.prepared_number` queries are prepared, the > one used least recently is deallocated and evicted from the cache > (proposed default: 100). Why do you need such logic? Why not just keep some limited number of prepared statements? Is it a problem if a PS is in cache but rarely used? -- Vlad On Mon, 21 Dec 2020 at 13:55, Adrian Klaver wrote: > On 12/21/20 12:26 PM, Daniele Varrazzo wrote: > > On Mon, 21 Dec 2020 at 16:02, Adrian Klaver > wrote: > > > > > > > I mistakenly replied Adrian privately. Following, his reply. > > > > > I think so. Personally, I prefer the two step approach as I am becoming > > less and less enthusiastic about hidden 'magic'. To that end a > > global(maybe connection) setting that would disable prepare would be > nice. > > > > ---- > > > > To which, 1) thank you very much, Adrian, for the plpython pointer, > > I'll take a look at it. > > > > 2) About disabling the automatic prepare: the mechanism I have in mind > > is to set prepare_threshold to None on the connection instance; > > So the above is something the user would have to do on each connection? > > > however we could make sure to have the default attribute defined on > > the class: this way who really hates the idea of prepared statements > > can be cheeky and set `psycopg3.Connection.prepare_threshold = None` > > I could get behind that. This all may be premature optimization on my > part. As long as there is some way to turn it off at some level I could > live with it. > > > instead of `myconn.prepare_threshold`... More seriously, if there is a > > large base of people who think that something can go wrong with > > prepared statement we can either provide a better interface to control > > it globally or to have the feature opt-in. > > > > > > -- Daniele > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > > --000000000000a01a5305b706ffcb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
> it's a moment in your
> program's lifet= ime where you know the query to run and the arguments:
> most likely = now you want to run the query too, not only to prepare it.

I am just thinking about type issues in some edge cases...
Wha= t 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 &q= uot;integer"?
There are probably other cases similar to this= , including "None" values
on the moment you want to cap= ture types... Maybe it is more reliable to
oblige the user, who k= nows their data better, to explicitly supply the types
for prepar= ation... Will any type inference work in case of types mismatch?
= If yes, to what extent?

Generally I think that aut= omatic preparation is a great idea, but if it is achieved
by sacr= ificing=C2=A0reliability to any extent, I would prefer to have such a featu= re
switched off by default.

> - if mo= re than `connection.prepared_number` queries are prepared, the
> on= e used least recently is deallocated and evicted from the cache
> (pr= oposed default: 100).

Why do you need such logic? Why no= t just keep some limited number of
prepared statements? Is it a p= roblem if a PS is in cache but rarely used?

--
Vlad


On Mon, 21 Dec 2020 at 13:55, Adrian Klaver= <adrian.= klaver@aklaver.com> wrote:
On 12/21/20 12:26 PM, Daniele Varrazzo wrote:
> On Mon, 21 Dec 2020 at 16:02, Adrian Klaver <adrian.klaver@aklaver.com> = wrote:
>

>
> I mistakenly replied Adrian privately. Following, his reply.
>

> I think so. Personally, I prefer the two step approach as I am becomin= g
> less and less enthusiastic about hidden 'magic'. To that end a=
> global(maybe connection) setting that would disable prepare would be n= ice.
>
> ----
>
> To which, 1) thank you very much, Adrian, for the plpython pointer, > I'll take a look at it.
>
> 2) About disabling the automatic prepare: the mechanism I have in mind=
> is to set prepare_threshold to None on the connection instance;

So the above is something the user would have to do on each connection?

> however we could make sure to have the default attribute defined on > the class: this way who really hates the idea of prepared statements > can be cheeky and set `psycopg3.Connection.prepare_threshold =3D None`=

I could get behind that. This all may be premature optimization on my
part. As long as there is some way to turn it off at some level I could live with it.

> instead of `myconn.prepare_threshold`... More seriously, if there is a=
> large base of people who think that something can go wrong with
> prepared statement we can either provide a better interface to control=
> it globally or to have the feature opt-in.
>
>
> -- Daniele
>


--
Adrian Klaver
adrian.klave= r@aklaver.com


--000000000000a01a5305b706ffcb--