Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmila-0007JP-O9 for pgsql-general@arkaria.postgresql.org; Thu, 24 Jan 2019 17:26:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gmilY-0008IS-0G for pgsql-general@arkaria.postgresql.org; Thu, 24 Jan 2019 17:26:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmilX-0008IK-JE for pgsql-general@lists.postgresql.org; Thu, 24 Jan 2019 17:26:11 +0000 Received: from fomalhaut.augure.net ([62.210.205.117]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gmilU-0001An-Gk for pgsql-general@lists.postgresql.org; Thu, 24 Jan 2019 17:26:10 +0000 Received: from perseus.augure.net (121.220.30.109.rev.sfr.net [109.30.220.121]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: alkan) by fomalhaut.augure.net (Postfix) with ESMTPSA id 037CC27C0236 for ; Thu, 24 Jan 2019 18:26:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=augure.net; s=mail; t=1548350767; bh=6pKJvSP2sdAZwNpW9d4ZmBkbJTHDQ5fqN+aezBYwetY=; h=Subject:To:References:From:Date:In-Reply-To:From; b=qNKrMgRRk4MghGRP5EwVNwqo6UYdHE8cEDD273tw5C4KLqv4ytwwhN2IeAGpC2Ti5 JVV687i0ycBvAeWE48k1bOS2D2XtaINzazmooHFjKQGBKbHlx+oWc1GdzELdh5qfHg qkGAYGnFjc+nNbwvCO++ge0nvA6yAAWFxZyaaDQM= Subject: Re: Casting Integer to Boolean in assignment To: pgsql-general@lists.postgresql.org References: From: Alexandre GRAIL Organization: Augure Engineering Message-ID: Date: Thu, 24 Jan 2019 18:26:06 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 24/01/2019 12:45, Geoff Winkless wrote: > My own opinion is that non-0 should implicitly cast as true and 0 > should cast as false. I just run > > UPDATE pg_cast SET castcontext = 'i' WHERE oid IN ( > SELECT c.oid > FROM pg_cast c > inner join pg_type src ON src.oid = c.castsource > inner join pg_type tgt ON tgt.oid = c.casttarget > WHERE (src.typname ILIKE '%int%' AND tgt.typname ILIKE 'bool%') > OR (src.typname ILIKE 'bool%' AND tgt.typname ILIKE '%int%') > OR (src.typname ILIKE 'bool%' AND tgt.typname ILIKE 'bit%') > OR (src.typname ILIKE 'bit%' AND tgt.typname ILIKE 'bool%') > OR (src.typname ILIKE 'bit%' AND tgt.typname ILIKE '%int') > ); Thanks Geoff for this solution, I was thinking it cannot be changed ! I end up doing this : UPDATE pg_cast SET castcontext = 'a' WHERE oid IN (  SELECT c.oid  FROM pg_cast c  inner join pg_type src ON src.oid = c.castsource  inner join pg_type tgt ON tgt.oid = c.casttarget  WHERE (src.typname ILIKE '%int%' AND tgt.typname ILIKE 'bool%') ); Only to have 0/1 => bool working in assignment. It saved me from injecting ALTER TYPE before and after any INSERT/UPDATE. In my case I don't control the query which is auto generated. (And the framework assumes 1 and 0 are safe boolean values without cast or quote. Changing that is not possible.)