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 1kbqAr-00087i-RN for psycopg@arkaria.postgresql.org; Sun, 08 Nov 2020 19:16:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kbqAp-0003LG-Tk for psycopg@arkaria.postgresql.org; Sun, 08 Nov 2020 19:16:23 +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 1kbqAp-0003L5-MN for psycopg@lists.postgresql.org; Sun, 08 Nov 2020 19:16:23 +0000 Received: from mail-lj1-x22e.google.com ([2a00:1450:4864:20::22e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kbqAj-0001C0-Dq for psycopg@postgresql.org; Sun, 08 Nov 2020 19:16:23 +0000 Received: by mail-lj1-x22e.google.com with SMTP id r17so1812422ljg.5 for ; Sun, 08 Nov 2020 11:16:17 -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=Dwutrt2w7qlTpJHlqrJvHzVW9upB7Ntdf0k22sRMh5A=; b=J7cFArUpUkQLSZkXsix3IbstC4UurYVahssUxcRv5AWd7gTq9jcSxy1Tu8X7hH5rRd fYEOnS6dnFj8gugT5jIfpFCZgmey3CZ4ujbN9iAhI7Xy/dWcttDZL4C3LQ/z72X3lqKw D7vqVdZ6UXJ0zf6Ip1nSpNMzs7sTZzfrw5rV4TwJNrCWcs6VLOMV50t86aYaTRlocmDJ ea460k1k2RFutji75klGDCdYDo0wDaF9NEgrYmqSB7/QWzGSejlTBy7n6RdVYnDpZKwG iKCd3Qs9LOoHfO9uMR6AUUfGbnADptHvoZZKP28SQpCooY0oLX00vSanqyk5tRZIO0r2 gmRw== 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=Dwutrt2w7qlTpJHlqrJvHzVW9upB7Ntdf0k22sRMh5A=; b=cVPQfLYnD/g1++VD9S+A86rcJrulzoQ3irStLt5QyJ//XFwtwAiuohEBgQKiRkMpSS I01ipHTYYOrpfZiOloQuIsvlwwWGS7U6+TSHWWrDVWRujQjo+NrL9YygXAkYmUATVnyN RV/97GQo/0Wvr9qzgNFhekbuAXbQ1r3OAxOpRikISLqiPoo/Rg0Mi1dHwucusWhUGX/K iHuoVdyHmjZsFfhcGVkMZfFjajme3FhQjZTNrgqQcBla56iKKBav1yvAd+rk99ATqTDN HCBIS0aq3/Hu8yLXfAVYkbAiQG0jQZfU9TcWMiWHNsPy07c6o4V0/hiQIj4YUmQZa9+H aeeg== X-Gm-Message-State: AOAM533AOwgAZUtpje8vdC+y5wYbWfn9bwObC5oQasxQLhhOx80BQqCq 7/DbZQuAnRF5VGIYNvYT4w9uP3/y9q0+tVJNk+hiWxQfK8fUEA== X-Google-Smtp-Source: ABdhPJwpKBUbQWmWJAO4miYYbqlRZLRPDkhoU0mSV/EkglEBPWpQ9cit0gpe9FMGyadrg/9UWtoi3lw0fAKjtnnc0jc= X-Received: by 2002:a2e:98d1:: with SMTP id s17mr4601571ljj.463.1604862975713; Sun, 08 Nov 2020 11:16:15 -0800 (PST) MIME-Version: 1.0 References: <2b9859f0-0964-2baa-b6bc-13f975ae0f67@aklaver.com> In-Reply-To: <2b9859f0-0964-2baa-b6bc-13f975ae0f67@aklaver.com> From: Daniele Varrazzo Date: Sun, 8 Nov 2020 19:16:04 +0000 Message-ID: Subject: Re: psycopg3 and adaptation choices To: Adrian Klaver Cc: 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 Sun, 8 Nov 2020 at 18:43, Adrian Klaver wrote: > If I'm following correctly in psycopg2 the adapter does type adaption on > the client side and passes that to server with oid for processing. In > psycopg3 you are proposing to let the server do more of the type > adaption and to that end you are looking for lowest common denominator > type to pass to server and then let it do the final casting. Is that > about right or am I way off base? No, not really. In psycopg2 we compose a query entirely client-side, and we pass it to the server with no oid indication, only as a big literal, like it was typed all in psql. In the example of `cursor.execute("select %s, %s", ["hello", 10])`, the server receives a literal `select 'hello', 10` and has no idea that there were two parameters. In psycopg3 the idea is to use a more advanced protocol, which separates query and parameters. It brings several benefits: can use prepared statements (send a query once, several parameters later), passing large data doesn't bloat the parser (the params don't hit the lexer/parser), can use binary format (useful to pass large binary blobs without escaping them in a textual form), the format of the data is more homogeneous (no need to quoting), so we can use Python objects in COPY instead of limiting the interface for the copy functions to file-like objects only. Both in psycopg2 and 3 there is an adaptation from Python types to Postgres string representation. In pg2 there is additional quoting, because apart from numbers and bools you need to quote a literal string to merge it to the query and make it syntactically valid. -- Daniele