functions-net.md 15.5 KB
Newer Older
K
KyleZhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
## 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](functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE) as well as the specialized operators and functions shown in [Table 9.38](functions-net.html#CIDR-INET-OPERATORS-TABLE) and [Table 9.39](functions-net.html#CIDR-INET-FUNCTIONS-TABLE).

 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.38. IP Address Operators**

|                                                                                                                                                                                                                                  Operator<br/><br/> Description<br/><br/> Example(s)                                                                                                                                                                                                                                 |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|`inet` `<<` `inet``boolean`<br/><br/> 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.<br/><br/>`inet '192.168.1.5' << inet '192.168.1/24'``t`<br/><br/>`inet '192.168.0.5' << inet '192.168.1/24'``f`<br/><br/>`inet '192.168.1/24' << inet '192.168.1/24'``f`|
|                                                                                                                                                                                  `inet` `<<=` `inet``boolean`<br/><br/> Is subnet contained by or equal to subnet?<br/><br/>`inet '192.168.1/24' <<= inet '192.168.1/24'``t`                                                                                                                                                                                   |
|                                                                                                                                                                                       `inet` `>>` `inet``boolean`<br/><br/> Does subnet strictly contain subnet?<br/><br/>`inet '192.168.1/24' >> inet '192.168.1.5'``t`                                                                                                                                                                                       |
|                                                                                                                                                                                     `inet` `>>=` `inet``boolean`<br/><br/> Does subnet contain or equal subnet?<br/><br/>`inet '192.168.1/24' >>= inet '192.168.1/24'``t`                                                                                                                                                                                      |
|                                                                                                                                                 `inet` `&&` `inet``boolean`<br/><br/> Does either subnet contain or equal the other?<br/><br/>`inet '192.168.1/24' && inet '192.168.1.80/28'``t`<br/><br/>`inet '192.168.1/24' && inet '192.168.2.0/28'``f`                                                                                                                                                 |
|                                                                                                                                                                                                        `~` `inet``inet`<br/><br/> Computes bitwise NOT.<br/><br/>`~ inet '192.168.1.6'``63.87.254.249`                                                                                                                                                                                                         |
|                                                                                                                                                                                               `inet` `&` `inet``inet`<br/><br/> Computes bitwise AND.<br/><br/>`inet '192.168.1.6' & inet '0.0.0.255'``0.0.0.6`                                                                                                                                                                                                |
|                                                                                                                                                                                             `inet` `|` `inet``inet`<br/><br/> Computes bitwise OR.<br/><br/>`inet '192.168.1.6' | inet '0.0.0.255'``192.168.1.255`                                                                                                                                                                                             |
|                                                                                                                                                                                               `inet` `+` `bigint``inet`<br/><br/> Adds an offset to an address.<br/><br/>`inet '192.168.1.6' + 25``192.168.1.31`                                                                                                                                                                                               |
|                                                                                                                                                                                         `bigint` `+` `inet``inet`<br/><br/> Adds an offset to an address.<br/><br/>`200 + inet '::ffff:fff0:1'``::ffff:255.240.0.201`                                                                                                                                                                                          |
|                                                                                                                                                                                           `inet` `-` `bigint``inet`<br/><br/> Subtracts an offset from an address.<br/><br/>`inet '192.168.1.43' - 36``192.168.1.7`                                                                                                                                                                                            |
|                                                                                                                                                         `inet` `-` `inet``bigint`<br/><br/> Computes the difference of two addresses.<br/><br/>`inet '192.168.1.43' - inet '192.168.1.19'``24`<br/><br/>`inet '::1' - inet '::ffff:1'``-4294901760`                                                                                                                                                         |

**Table 9.39. IP Address Functions**

|                                                                                                                                                         Function<br/><br/> Description<br/><br/> Example(s)                                                                                                                                                         |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|[]() `abbrev` ( `inet` ) → `text`<br/><br/> Creates 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.)<br/><br/>`abbrev(inet '10.1.0.0/32')``10.1.0.0`|
|                          `abbrev` ( `cidr` ) → `text`<br/><br/> 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](datatype-net-types.html#DATATYPE-NET-CIDR-TABLE).)<br/><br/>`abbrev(cidr '10.1.0.0/16')``10.1/16`                           |
|                                                                                              []() `broadcast` ( `inet` ) → `inet`<br/><br/> Computes the broadcast address for the address's network.<br/><br/>`broadcast(inet '192.168.1.5/24')``192.168.1.255/24`                                                                                              |
|                                                                                                            []() `family` ( `inet` ) → `integer`<br/><br/> Returns the address's family: `4` for IPv4, `6` for IPv6.<br/><br/>`family(inet '::1')``6`                                                                                                             |
|                                                                                                       []() `host` ( `inet` ) → `text`<br/><br/> Returns the IP address as text, ignoring the netmask.<br/><br/>`host(inet '192.168.1.0/24')``192.168.1.0`                                                                                                        |
|                                                                                                      []() `hostmask` ( `inet` ) → `inet`<br/><br/> Computes the host mask for the address's network.<br/><br/>`hostmask(inet '192.168.23.20/30')``0.0.0.3`                                                                                                       |
|                                                                       []() `inet_merge` ( `inet`, `inet` ) → `cidr`<br/><br/> Computes the smallest network that includes both of the given networks.<br/><br/>`inet_merge(inet '192.168.1.5/24', inet '192.168.2.5/24')``192.168.0.0/22`                                                                        |
|                                                                                   []() `inet_same_family` ( `inet`, `inet` ) → `boolean`<br/><br/> Tests whether the addresses belong to the same IP family.<br/><br/>`inet_same_family(inet '192.168.1.5/24', inet '::1')``f`                                                                                   |
|                                                                                                                []() `masklen` ( `inet` ) → `integer`<br/><br/> Returns the netmask length in bits.<br/><br/>`masklen(inet '192.168.1.5/24')``24`                                                                                                                 |
|                                                                                                    []() `netmask` ( `inet` ) → `inet`<br/><br/> Computes the network mask for the address's network.<br/><br/>`netmask(inet '192.168.1.5/24')``255.255.255.0`                                                                                                    |
|                                                    []() `network` ( `inet` ) → `cidr`<br/><br/> 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 `cidr`.)<br/><br/>`network(inet '192.168.1.5/24')``192.168.1.0/24`                                                     |
|                                                                           []() `set_masklen` ( `inet`, `integer` ) → `inet`<br/><br/> Sets the netmask length for an `inet` value. The address part does not change.<br/><br/>`set_masklen(inet '192.168.1.5/24', 16)``192.168.1.5/16`                                                                           |
|                                                                `set_masklen` ( `cidr`, `integer` ) → `cidr`<br/><br/> Sets the netmask length for a `cidr` value. Address bits to the right of the new netmask are set to zero.<br/><br/>`set_masklen(cidr '192.168.1.0/24', 16)``192.168.0.0/16`                                                                |
|                                                                     []() `text` ( `inet` ) → `text`<br/><br/> Returns the unabbreviated IP address and netmask length as text. (This has the same result as an explicit cast to `text`.)<br/><br/>`text(inet '192.168.1.5')``192.168.1.5/32`                                                                     |

### 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](functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE) as well as the specialized functions shown in [Table 9.40](functions-net.html#MACADDR-FUNCTIONS-TABLE). In addition, they support the bitwise logical operators `~`, `&` and `|` (NOT, AND and OR), just as shown above for IP addresses.

**Table 9.40. MAC Address Functions**

|                                                                                                                    Function<br/><br/> Description<br/><br/> Example(s)                                                                                                                    |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|     []() `trunc` ( `macaddr` ) → `macaddr`<br/><br/> 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).<br/><br/>`trunc(macaddr '12:34:56:78:90:ab')``12:34:56:00:00:00`     |
|`trunc` ( `macaddr8` ) → `macaddr8`<br/><br/> 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).<br/><br/>`trunc(macaddr8 '12:34:56:78:90:ab:cd:ef')``12:34:56:00:00:00:00:00`|
|         []() `macaddr8_set7bit` ( `macaddr8` ) → `macaddr8`<br/><br/> Sets the 7th bit of the address to one, creating what is known as modified EUI-64, for inclusion in an IPv6 address.<br/><br/>`macaddr8_set7bit(macaddr8 '00:34:56:ab:cd:ef')``02:34:56:ff:fe:ab:cd:ef`          |