PostgreSQL
9.12. Network Address Functions and Operators #
The IP network address types, cidr
and inet
, support the usual comparison operators shown in Table 9.1 as well as the specialized operators and functions shown in Table 9.39 and Table 9.40.
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 9.39. IP Address Operators
Operator Description Example(s) |
---|
Is 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.
|
Is subnet contained by or equal to subnet?
|
Does subnet strictly contain subnet?
|
Does subnet contain or equal subnet?
|
Does either subnet contain or equal the other?
|
Computes bitwise NOT.
|
Computes bitwise AND.
|
|
` `+inet` → Computes bitwise OR. `+inet '192.168.1.6' |
inet '0.0.0.255'` → `+192.168.1.255` |
Adds an offset to an address.
|
Adds an offset to an address.
|
Subtracts an offset from an address.
|
Computes the difference of two addresses.
|
+
Table 9.40. IP Address Functions
Function Description Example(s) |
---|
`+abbrev+` ( `+inet+` ) → `+text+` Creates an abbreviated display format as text. (The result is the same as the
|
Creates an abbreviated display format as text. (The abbreviation consists of dropping all-zero octets to the right of the netmask; more examples are in Table 8.22.)
|
`+broadcast+` ( `+inet+` ) → `+inet+` Computes the broadcast address for the address’s network.
|
`+family+` ( `+inet+` ) → `+integer+` Returns the address’s family:
|
`+host+` ( `+inet+` ) → `+text+` Returns the IP address as text, ignoring the netmask.
|
`+hostmask+` ( `+inet+` ) → `+inet+` Computes the host mask for the address’s network.
|
`+inet_merge+` ( `+inet+`, `+inet+` ) → `+cidr+` Computes the smallest network that includes both of the given networks.
|
`+inet_same_family+` ( `+inet+`, `+inet+` ) → `+boolean+` Tests whether the addresses belong to the same IP family.
|
`+masklen+` ( `+inet+` ) → `+integer+` Returns the netmask length in bits.
|
`+netmask+` ( `+inet+` ) → `+inet+` Computes the network mask for the address’s network.
|
`+network+` ( `+inet+` ) → `+cidr+` Returns the network part of the address, zeroing out whatever is to the right of the netmask. (This is equivalent to casting the value to
|
`+set_masklen+` ( `+inet+`, `+integer+` ) → `+inet+` Sets the netmask length for an
|
Sets the netmask length for a
|
`+text+` ( `+inet+` ) → `+text+` Returns the unabbreviated IP address and netmask length as text. (This has the same result as an explicit cast to
|
+
Tip
The abbrev
, host
, and text
functions are primarily intended to offer alternative display formats for IP addresses.
The MAC address types, macaddr
and macaddr8
, support the usual comparison operators shown in Table 9.1 as well as the specialized functions shown in Table 9.41. In addition, they support the bitwise logical operators ~
, &
and |
(NOT, AND and OR), just as shown above for IP addresses.
Table 9.41. MAC Address Functions
Function Description Example(s) |
---|
`+trunc+` ( `+macaddr+` ) → `+macaddr+` Sets the last 3 bytes of the address to zero. The remaining prefix can be associated with a particular manufacturer (using data not included in PostgreSQL).
|
Sets the last 5 bytes of the address to zero. The remaining prefix can be associated with a particular manufacturer (using data not included in PostgreSQL).
|
`+macaddr8_set7bit+` ( `+macaddr8+` ) → `+macaddr8+` Sets the 7th bit of the address to one, creating what is known as modified EUI-64, for inclusion in an IPv6 address.
|
+
Prev | Up | Next |
---|---|---|
9.11. Geometric Functions and Operators |
9.13. Text Search Functions and Operators |
Submit correction
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.
Copyright © 1996-2024 The PostgreSQL Global Development Group