This page in other versions: 9.1 / 9.2 / 9.3 / 9.4 / current (9.5)  |  Development versions: devel  |  Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3 / 8.4 / 9.0

9.12. Network Address Functions and Operators

Table 9-34 shows the operators available for the cidr and inet types. The operators <<, <<=, >>, and >>= test for subnet inclusion. They consider only the network parts of the two addresses (ignoring any host part) and determine whether one network is identical to or a subnet of the other.

Table 9-34. cidr and inet Operators

Operator Description Example
< is less than inet '' < inet ''
<= is less than or equal inet '' <= inet ''
= equals inet '' = inet ''
>= is greater or equal inet '' >= inet ''
> is greater than inet '' > inet ''
<> is not equal inet '' <> inet ''
<< is contained within inet '' << inet '192.168.1/24'
<<= is contained within or equals inet '192.168.1/24' <<= inet '192.168.1/24'
>> contains inet '192.168.1/24' >> inet ''
>>= contains or equals inet '192.168.1/24' >>= inet '192.168.1/24'
~ bitwise NOT ~ inet ''
& bitwise AND inet '' & inet ''
| bitwise OR inet '' | inet ''
+ addition inet '' + 25
- subtraction inet '' - 36
- subtraction inet '' - inet ''

Table 9-35 shows the functions available for use with the cidr and inet types. The abbrev, host, and text functions are primarily intended to offer alternative display formats.

Table 9-35. cidr and inet Functions

Function Return Type Description Example Result
abbrev(inet) text abbreviated display format as text abbrev(inet '')
abbrev(cidr) text abbreviated display format as text abbrev(cidr '') 10.1/16
broadcast(inet) inet broadcast address for network broadcast('')
family(inet) int extract family of address; 4 for IPv4, 6 for IPv6 family('::1') 6
host(inet) text extract IP address as text host('')
hostmask(inet) inet construct host mask for network hostmask('')
masklen(inet) int extract netmask length masklen('') 24
netmask(inet) inet construct netmask for network netmask('')
network(inet) cidr extract network part of address network('')
set_masklen(inet, int) inet set netmask length for inet value set_masklen('', 16)
set_masklen(cidr, int) cidr set netmask length for cidr value set_masklen(''::cidr, 16)
text(inet) text extract IP address and netmask length as text text(inet '')

Any cidr value can be cast to inet implicitly or explicitly; therefore, the functions shown above 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. In addition, you can cast a text value to inet or cidr using normal casting syntax: for example, inet(expression) or colname::cidr.

Table 9-36 shows the functions available for use with the macaddr type. The function trunc(macaddr) returns a MAC address with the last 3 bytes set to zero. This can be used to associate the remaining prefix with a manufacturer.

Table 9-36. macaddr Functions

Function Return Type Description Example Result
trunc(macaddr) macaddr set last 3 bytes to zero trunc(macaddr '12:34:56:78:90:ab') 12:34:56:00:00:00

The macaddr type also supports the standard relational operators (>, <=, etc.) for lexicographical ordering.


Nov. 18, 2011, 12:02 a.m.

possibly it should be noted that the abbrev function can create something that looks somewhat like an old-style class A address but with different value.

127.1 means the same as in the old "classful" style,
and 127.65536 means the same as

As this old style predates CIDR (and is also not supported by postgresql's inet types) 127.1/16 is unambiguous.

I have successfuly used the old style in connection strings however, I suspect that this depends on how gethostbyname()
handles it, and not something internal to postgres.

Privacy Policy | About PostgreSQL
Copyright © 1996-2016 The PostgreSQL Global Development Group