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 1kc17E-0008WD-8q for psycopg@arkaria.postgresql.org; Mon, 09 Nov 2020 06:57:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kc17D-0005Nw-7C for psycopg@arkaria.postgresql.org; Mon, 09 Nov 2020 06:57: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 1kc17D-0005Nk-1q for psycopg@lists.postgresql.org; Mon, 09 Nov 2020 06:57:23 +0000 Received: from mail.dndg.it ([178.32.136.2]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kc17A-0006t1-Vj for psycopg@lists.postgresql.org; Mon, 09 Nov 2020 06:57:22 +0000 Received: from [192.168.1.159] (93-40-251-202.ip41.fastwebnet.it [93.40.251.202]) by mail.dndg.it (Postfix) with ESMTPSA id 26E23A0C78 for ; Mon, 9 Nov 2020 06:59:59 +0000 (UTC) Subject: Re: psycopg3 and adaptation choices To: psycopg@lists.postgresql.org References: <2b9859f0-0964-2baa-b6bc-13f975ae0f67@aklaver.com> <88fb7a7e-a182-a816-c1a7-8a1f54b65215@aklaver.com> From: Federico Di Gregorio Organization: DNDG srl Message-ID: <4830fb8d-fa57-e0f8-0e4f-a96ed040dede@dndg.it> Date: Mon, 9 Nov 2020 07:57:17 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,NICE_REPLY_A autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 08/11/20 23:21, Daniele Varrazzo wrote: [snip] > 1. If we specify `numeric` or `int8` as oid, inserting in an int field > in a table will work ok, but some functions/operators won't (e.g. "1 >>> %s"). > 2. If we specify `int4` it would work for those few functions defined > as `integer`, but if we try to write a number that doesn't fit in 32 > bits into a Postgres bigint field I assume something will overflow > along the way, even if both python and postgres can handle it. > 3. If we specify `unknown` it might work more often, but > `cursor.execute("select %s", [10]) will return the string "10" instead > of a number. > > So I wonder what's the best compromise to do here: the less bad seems > 1. 3. might work in more contexts, but it's a very counterintuitive > behaviour, and roundtripping other objects (dates, uuid) works no > problem: they don't come back as strings. Looking at what the adapters in other languages/frameworks do the common solution is to choose the "best fitting" type and let the programmer add a cast when needed. This is easier in statically typed languages where we have an almost perfect overlap between PostgreSQL and platform types but a bit more difficult in dynamic typed languages like Python where the available types are abstracted over the platform ones (numbers are a good example). In your example I'd just go for int8 (the largest possible int in PostgreSQL). Decimal would probably be better (largest range) but it is not what the majority of people would expect. IMHO, oid is a bad idea because it has a very specific semantic and the error messages generated by PostgreSQL will be more confusing. federico