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 1kc5Fw-0001oU-OP for psycopg@arkaria.postgresql.org; Mon, 09 Nov 2020 11:22: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 1kc5Fv-00005V-Ms for psycopg@arkaria.postgresql.org; Mon, 09 Nov 2020 11:22:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kc5Fv-00005O-HV for psycopg@lists.postgresql.org; Mon, 09 Nov 2020 11:22:39 +0000 Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kc5Ft-0000ei-LR for psycopg@postgresql.org; Mon, 09 Nov 2020 11:22:39 +0000 Received: by mail-lj1-x235.google.com with SMTP id 23so9809214ljv.7 for ; Mon, 09 Nov 2020 03:22: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=R/uFxNlB6X7AKlvwLiU9DaXL0pFXGkGyK2BE44iEAwY=; b=kJH9FUO+JvV4CR0pMVf5yIpid+4SHADtG5lotbKryN/fVOtVPwG5e+dBLW7UlKz5CC 93dZvrjVx82TxR0m59lfy9p0kbXx+j5VmZdIjz4YgCT6A8ebA630WiyL27Nf13izY4zD D7rPNilCa4+EhRvfpo+nzGiK7CdKrkXP8RPaGtlmCd35oGa0xVP9/ZBkc2Mcan3J9gi/ i5BTZwxVhAaVJi3iCDFgtu//PkiusklyfOCIoTimYqxpsr3ccZELAhMc41+gsaQ07fWI kAowxhaO/Rs60Hg2F+mE2TVT97jT1BQ/7AA9CZWYx7OqC6MpA1/W9OEscOgH1obtcFQW r0jA== 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=R/uFxNlB6X7AKlvwLiU9DaXL0pFXGkGyK2BE44iEAwY=; b=k9wjWocRsHzgqhiOJNvKHw/ZrD6+J0C37ty8Z74l22TafDWUxoICYlxpkfENWs9nj8 4mbl7ySSJqKoHcekftTGA0f/biNQVKRikqvACVrVrR/HGNQEFmLHV6Kz94ib4hOU+et+ d/MbWNBUc/ZjoZ48akIMErfwh96fCeJofzYigtvUSxCeCawxxF2F/AX2psBCktRNFZKw TCW6mgN45DJxbhd23OOJzVJ/rkrkX3/5jD8Z966wa28+b9KeZ+c6khY5UVAG8xaDPI24 T/qoK1WKhmkl89CbdNwtz6biuC0z+8xb4Iuf7PvE+9RedI0yur6J7A+iXrGhbnyXd2zD j48A== X-Gm-Message-State: AOAM533mqt3nd3IE1opxHnrsD1mvFr0iDz77S8XEMj9mmSfYP5jzZvXV irwXjAx06LkDKX2LIPpek+qizPIts1InECutXRg= X-Google-Smtp-Source: ABdhPJzhmp5DJSeFMzls2g0T2aV3ELHBIiogm+lQBMEkGKrReMQeeX1oVpb/IJHf4m5ncCnMrYDDa8cexrT1YEMNTkg= X-Received: by 2002:a05:651c:54f:: with SMTP id q15mr1704524ljp.186.1604920956834; Mon, 09 Nov 2020 03:22:36 -0800 (PST) MIME-Version: 1.0 References: <2b9859f0-0964-2baa-b6bc-13f975ae0f67@aklaver.com> <88fb7a7e-a182-a816-c1a7-8a1f54b65215@aklaver.com> In-Reply-To: From: Daniele Varrazzo Date: Mon, 9 Nov 2020 11:22:25 +0000 Message-ID: Subject: Re: psycopg3 and adaptation choices 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 Mon, 9 Nov 2020 at 02:19, Vladimir Ryabtsev wrote: > BTW, may I ask another question regarding parameters? > Don't you want to step away from '%s' syntax and use '$1, $2, ...' which seems to be more traditional in the database world? > '%s' feels like old-school string formatting, new server-side parameter binding may want to give some new impression. > Moreover, it appears more convenient when you have parameters numbered and can reuse them a few times in a query. Hi Vladmir, I wouldn't want to step away from the %s placeholder, because that would mean that every query of every program written in psycopg2 would need to be rewritten, and that would be an impossibly steep adoption curve. Furthermore the %(named)s placeholders are a much better convenience over $n: you couldn't pass {name: param} mapping otherwise. The $n parameters are also cumbersome in requiring an explicit mapping, where an implicit one would have worked (so it has to be "$1 $2 $3" and counting, instead of uniform "%s, %s, %s") and slipping a parameter in the middle of a sequence of parameters requires to renumber all the following ones. Another feature added to psycopg3 is support for binary parameters: I added %b and %(name)b placeholders to mark the placeholders requiring a binary param, so that you can `execute("insert into image (name, data) values (%s, %b)", [filename, image_bytes])`: if you used $n placeholders you would need a new parameter to specify, of the list (or mapping) of parameters, which one do you want in text and in binary format, or some other mechanism, such as a `Binary(image_bytes)` wrapper. Said that, because we are using the server-side binding, we are actually passing $n parameters to the server: in psycopg3 there is a parser to convert %s and %(name)s placeholders to $n style and to reorder params mapping when needed. So the query above results in a call like `pgconn.exec_params(b"select $1, $2", [filename.encode(...), image_bytes], [TEXT, BINARY])`. If there is interest we can think about how to make this querying layer more accessible (e.g. using a `cur.execute(PgQuery("select $1, $2"), [...])` or some other wrapping mechanism. -- Daniele