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 1kcK4k-0004pL-8s for psycopg@arkaria.postgresql.org; Tue, 10 Nov 2020 03:12:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcK4j-0004N8-3V for psycopg@arkaria.postgresql.org; Tue, 10 Nov 2020 03:12:05 +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 1kcK4i-0004LP-Ru for psycopg@lists.postgresql.org; Tue, 10 Nov 2020 03:12:04 +0000 Received: from mail-lf1-x142.google.com ([2a00:1450:4864:20::142]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcK4f-0003AS-PX for psycopg@postgresql.org; Tue, 10 Nov 2020 03:12:03 +0000 Received: by mail-lf1-x142.google.com with SMTP id f11so9189619lfs.3 for ; Mon, 09 Nov 2020 19:12:01 -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=eNFCGezNwguWb8yO8RK64hkP/sJeSDDK82CDqmHJOLI=; b=Z50HenhWVNuPRoUXc5uNRi0i1PzEib2sexs2ZZdQ9F/Q9o1npjUTjCFR7WpLgxqN1H 8vmJzHbzwoSlmWPD2HMtMkZ+lucMg722hxgDRLsTSBkc0tMUIZn5giimKOaI491OOBEm +3n2Ub0VzQX5/QvD4ytJ+S+MzdtUb/lnhUWkLMxrmlmh0IUpvGBcjPnke12oGqKg2DlW 7X1JMwSsIWlhMW91p43U7qSeblgKnoEDqDNXYO72O+bXXclFIc/uojGQqF0Zdg3UWwB8 gn1ABfc68InMSIBNV8C5+5Fsq8Wh469OuR21YzQDcfbBIXphc3XBe9vKtvh7dha1oPu2 mdeg== 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=eNFCGezNwguWb8yO8RK64hkP/sJeSDDK82CDqmHJOLI=; b=pqyXU91EhX5ki0ZA2UWghkZp+i1qc6FkIDOkHSJqBny8n7STsyY9znyUkK18kmbK/w u5c6odWGlrKW3VK++Rr282OELhZ0efEiUAYOd9CHEweJjaVmsiGkNh/NEhE9W8JUYi4g I2/mxG4Qt95kdAD0Y99kd9KeJoUcRJbv+BrdLjckCPcKkwV5aU3bmqU8YgLPaJZkQ5+q kjpkeaDwZ56sRdPzzLzA2HGkACz04lXTBsZZOc/B7IZrJt1rc8Nhe4As5CQPkSOQ/Dfu tJwTO9Nova6BHujBar7yqYY2K0fCZj+hUU74Gz6cZ8Y4SZt/6KmvwmV1J7fcOB2Jh/5I wMdA== X-Gm-Message-State: AOAM533IAVo5fsyTZFRCrfUL/v2sa+saW0QwBvBHfttZEa79hC5KrQhy kRoCfwy3WMg9M73pn7rnFI4BlLAz+00dU0zqdhaLkvP/CdR7snDA X-Google-Smtp-Source: ABdhPJzDbamzjPVPONkI9cnxUrx01Muha0/Gg2oTc602CqKNDq1Hptx1HTMKkjyALVj+GVDR5jjO3oUo8Q/LGhdj3y8= X-Received: by 2002:a19:8b84:: with SMTP id n126mr6462062lfd.555.1604977919844; Mon, 09 Nov 2020 19:11:59 -0800 (PST) MIME-Version: 1.0 References: <2b9859f0-0964-2baa-b6bc-13f975ae0f67@aklaver.com> <88fb7a7e-a182-a816-c1a7-8a1f54b65215@aklaver.com> <5d2207c6-cebb-082d-cf5b-a7a0fe8e58d4@aklaver.com> In-Reply-To: <5d2207c6-cebb-082d-cf5b-a7a0fe8e58d4@aklaver.com> From: Daniele Varrazzo Date: Tue, 10 Nov 2020 03:11:48 +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 Tue, 10 Nov 2020 at 01:19, Adrian Klaver wrote: > > On 11/8/20 2:21 PM, Daniele Varrazzo wrote: > > 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"). > > What is not working here? Postgres has two different types of cast rules: "assignment" and "implicit". https://www.postgresql.org/docs/current/sql-createcast.html Assignment casts are only chosen by the parser on insert. Implicit casts are chosen in other contexts too. It appears that the cast rules from numeric/int8 to integer are of the first kind. So, while inserting into a table with a mismatching type mostly works: piro=# create table mytable (myint integer); CREATE TABLE piro=# insert into mytable (myint) values (42::decimal); INSERT 0 1 piro=# insert into mytable (myint) values (84::int8); INSERT 0 1 using the wrong type in more generic expression may fail: piro=# select 1 << 4::int; ?column? ---------- 16 piro=# select 1 << 4::int8; ERROR: operator does not exist: integer << bigint LINE 1: select 1 << 4::bigint; ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. So a psycopg statement such as `cur.execute("select 1 << %s", [n])`, which used to work if bound client-side, must be rewritten as "select 1 << %s::integer" for server-side binding, both if we choose int8 or numeric as Postgres types to adapt a Python int. -- Daniele