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 1iPobU-0002Yf-PR for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Oct 2019 14:05:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iPobT-0007BC-GG for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Oct 2019 14:05:39 +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 1iPobT-0007B3-4y for pgsql-bugs@lists.postgresql.org; Wed, 30 Oct 2019 14:05:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iPobQ-0000kn-GQ for pgsql-bugs@postgresql.org; Wed, 30 Oct 2019 14:05:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x9UE5YRO019075; Wed, 30 Oct 2019 10:05:34 -0400 From: Tom Lane To: Jeremy Finzel cc: pgsql-bugs@postgresql.org Subject: Re: insert into inet from text automatically adding subnet In-reply-to: References: Comments: In-reply-to Jeremy Finzel message dated "Wed, 30 Oct 2019 06:36:00 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19073.1572444334.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 30 Oct 2019 10:05:34 -0400 Message-ID: <19074.1572444334@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Jeremy Finzel writes: > This appears to me to be a bug (using 11.4). But I would like some revi= ew > to ensure I am not missing some intended functionality or nuance with th= is > data type. The example below seems certainly surprising. > I found that using a writable CTE to insert an IP address without a subn= et > into a *text* field, then inserting result into another table with type > *inet*, is automatically adding a subnet. This is the only case in whic= h I > found this happens. I think this is just a complex version of this behavior: regression=3D# select '127.0.0.0'::inet; inet = ----------- 127.0.0.0 (1 row) regression=3D# select '127.0.0.0'::inet::text; text = -------------- 127.0.0.0/32 (1 row) That's documented in table 9.38 in https://www.postgresql.org/docs/current/functions-net.html where it says text(inet) text extract IP address and netmask length as text text(inet '1= 92.168.1.5') 192.168.1.5/32 Admittedly, there's not an explicit mention here that this is also describing the behavior of a cast to text, though you could infer that if you remembered the discussion at https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TY= PE-CASTS It's also worth noting that per the definition of the inet type at https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-I= NET these two strings are equivalent anyway, because /32 is the default assumption for an IPv4 inet value. inet_out is just omitting the netmask when it has the default value. There's also, a bit further down on that page, Tip If you do not like the output format for inet or cidr values, try the functions host, text, and abbrev. So you might try host() or abbrev() to get a text conversion you like better. (My very vague recollection is that this state of affairs emerged because of disagreements over exactly how the text conversion ought to work in such cases. It'd probably be better if the default conversion to text matched what inet_out does, but that's water over the dam now; changing it twenty years later would cause more problems than it'd solve.) regards, tom lane