DocsSql ReferenceSqlFunctionsFunctions Net

Network Address Functions and Operators

The IP network address types, cidr and inet, support the usual comparison operators shown in Table 8.1 as well as the specialized operators and functions shown in Table 8.35 and Table 8.36.

Any cidr value can be cast to inet implicitly; therefore, the operators and functions shown below as operating on inet also work on cidr values. (Where there are separate functions for inet and cidr, it is because the behavior should be different for the two cases.) Also, it is permitted to cast an inet value to cidr. When this is done, any bits to the right of the netmask are silently zeroed to create a valid cidr value.

Table IP Address Operators

OperatorDescriptionExample(s)
inet << inetbooleanIs subnet strictly contained by subnet? This operator, and the next four, test for subnet inclusion. They consider only the network parts of the two addresses (ignoring any bits to the right of the netmasks) and determine whether one network is identical to or a subnet of the other.inet '192.168.1.5' << inet '192.168.1/24't
inet '192.168.0.5' << inet '192.168.1/24'f
inet '192.168.1/24' << inet '192.168.1/24'f
inet <<= inetbooleanIs subnet contained by or equal to subnet?inet '192.168.1/24' <<= inet '192.168.1/24't
inet >> inetbooleanDoes subnet strictly contain subnet?inet '192.168.1/24' >> inet '192.168.1.5't
inet >>= inetbooleanDoes subnet contain or equal subnet?inet '192.168.1/24' >>= inet '192.168.1/24't
inet && inetbooleanDoes either subnet contain or equal the other?inet '192.168.1/24' && inet '192.168.1.80/28't
inet '192.168.1/24' && inet '192.168.2.0/28'f
~ inetinetComputes bitwise NOT.~ inet '192.168.1.6'63.87.254.249
inet & inetinetComputes bitwise AND.inet '192.168.1.6' & inet '0.0.0.255'0.0.0.6
inet | inetinetComputes bitwise OR.inet '192.168.1.6' | inet '0.0.0.255'192.168.1.255
inet + bigintinetAdds an offset to an address.inet '192.168.1.6' + 25192.168.1.31
bigint + inetinetAdds an offset to an address.200 + inet '::ffff:fff0:1'::ffff:255.240.0.201
inet - bigintinetSubtracts an offset from an address.inet '192.168.1.43' - 36192.168.1.7
inet - inetbigintComputes the difference of two addresses.inet '192.168.1.43' - inet '192.168.1.19'24
inet '::1' - inet '::ffff:1'-4294901760

Table IP Address Functions

FunctionDescriptionExample(s)
abbrev ( inet ) → textCreates an abbreviated display format as text. (The result is the same as the inet output function produces; it is “abbreviated” only in comparison to the result of an explicit cast to text, which for historical reasons will never suppress the netmask part.)abbrev(inet '10.1.0.0/32')10.1.0.0
broadcast ( inet ) → inetComputes the broadcast address for the address’s network.broadcast(inet '192.168.1.5/24')192.168.1.255/24
family ( inet ) → integerReturns the address’s family: 4 for IPv4, 6 for IPv6.family(inet '::1')6
host ( inet ) → textReturns the IP address as text, ignoring the netmask.host(inet '192.168.1.0/24')192.168.1.0
hostmask ( inet ) → inetComputes the host mask for the address’s network.hostmask(inet '192.168.23.20/30')0.0.0.3
inet_same_family ( inet, inet ) → booleanTests whether the addresses belong to the same IP family.inet_same_family(inet '192.168.1.5/24', inet '::1')f
masklen ( inet ) → integerReturns the netmask length in bits.masklen(inet '192.168.1.5/24')24
netmask ( inet ) → inetComputes the network mask for the address’s network.netmask(inet '192.168.1.5/24')255.255.255.0
set_masklen ( inet, integer ) → inetSets the netmask length for an inet value. The address part does not change.set_masklen(inet '192.168.1.5/24', 16)192.168.1.5/16
text ( inet ) → textReturns the unabbreviated IP address and netmask length as text. (This has the same result as an explicit cast to text.)text(inet '192.168.1.5')192.168.1.5/32
The abbrev, host, and text functions are primarily intended to offer alternative display formats for IP addresses.