Network address types

更新时间:
复制 MD 格式

PolarDB for Oracle provides four dedicated data types for storing network addresses. Use these types instead of text or varchar — they perform input validation and support specialized operators and functions for network address operations.

NameStorage sizeDescription
cidr7 or 19 bytesIPv4 and IPv6 networks
inet7 or 19 bytesIPv4 and IPv6 hosts and networks
macaddr6 bytesMAC addresses
macaddr88 bytesMAC addresses (EUI-64 format)

When sorting inet or cidr values, IPv4 addresses always sort before IPv6 addresses, including IPv4 addresses encapsulated or mapped to IPv6, such as ::10.2.3.4 or ::ffff:10.4.3.2.

inet

The inet type holds an IPv4 or IPv6 host address and, optionally, its subnet — all in one field. The subnet is represented as the number of network address bits in the host address (the netmask).

FormatDescription
IPv4An IPv4 address with an optional subnet mask. If the netmask is 32, the value represents a single host rather than a subnet. Example: 192.168.0.1/24
IPv6An IPv6 address with an optional subnet mask. If the netmask is 128, the value represents a unique host. Example: 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128

The input format is address/y, where address is an IPv4 or IPv6 address and y is the number of bits in the netmask. If you omit /y, the netmask defaults to 32 for IPv4 or 128 for IPv6, meaning the value represents a single host. On display, /y is omitted when the netmask specifies a single host.

To store only networks (no host bits), use cidr instead of inet.

cidr

The cidr type holds an IPv4 or IPv6 network specification and follows Classless Inter-Domain Routing (CIDR) conventions. The input format is address/y, where address is the network's lowest address and y is the number of bits in the netmask.

If y is omitted, it is calculated from classful network assumptions — at least large enough to include all octets written in the input. Specifying a network address with bits set to the right of the netmask is an error.

cidr type input examples

cidr inputcidr outputabbrev(cidr)
192.168.100.128/25192.168.100.128/25192.168.100.128/25
192.168/24192.168.0.0/24192.168.0/24
192.168/25192.168.0.0/25192.168.0.0/25
192.168.1192.168.1.0/24192.168.1/24
192.168192.168.0.0/24192.168.0/24
128.1128.1.0.0/16128.1/16
128128.0.0.0/16128.0/16
128.1.2128.1.2.0/24128.1.2/24
10.1.210.1.2.0/2410.1.2/24
10.110.1.0.0/1610.1/16
1010.0.0.0/810/8
10.1.2.3/3210.1.2.3/3210.1.2.3/32
2001:4f8:3:ba::/642001:4f8:3:ba::/642001:4f8:3:ba/64
2001:4f8:3:ba:​2e0:81ff:fe22:d1f1/1282001:4f8:3:ba:​2e0:81ff:fe22:d1f1/1282001:4f8:3:ba:​2e0:81ff:fe22:d1f1/128
::ffff:1.2.3.0/120::ffff:1.2.3.0/120::ffff:1.2.3/120
::ffff:1.2.3.0/128::ffff:1.2.3.0/128::ffff:1.2.3.0/128

inet vs. cidr

The essential difference is that inet accepts values with nonzero bits to the right of the netmask, while cidr does not. For example, 192.168.0.1/24 is valid for inet but not for cidr.

Tip: To change the output format of inet or cidr values, use the host, text, or abbrev functions.

macaddr

The macaddr type stores MAC addresses — for example, Ethernet card hardware addresses. All of the following input formats are accepted and treated as equivalent:

'08:00:2b:01:02:03'
'08-00-2b-01-02-03'
'08002b:010203'
'08002b-010203'
'0800.2b01.0203'
'0800-2b01-0203'
'08002b010203'

Upper and lowercase are both accepted for the hex digits a through f. Output is always in the first form: 08:00:2b:01:02:03.

IEEE Standard 802-2001 designates the hyphen-separated form (08-00-2b-01-02-03) as the canonical format, and the colon-separated form (08:00:2b:01:02:03) as bit-reversed MSB-first notation — meaning 08-00-2b-01-02-03 and 10:00:D4:80:40:C0 represent the same address in different notations. This convention is largely obsolete and relevant only to legacy protocols such as Token Ring. PolarDB for Oracle makes no provisions for bit reversal; all accepted formats use canonical LSB order.

The last five formats listed are not part of any standard.

macaddr8

The macaddr8 type stores MAC addresses in EUI-64 format. It accepts both 6-byte (EUI-48) and 8-byte (EUI-64) addresses and stores them in 8-byte format. When a 6-byte address is provided, the 4th and 5th bytes are set to FF and FE, respectively.

Any input consisting of hex digit pairs separated consistently by :, -, or . is accepted. The total hex digit count must be either 12 (6 bytes) or 16 (8 bytes). Leading and trailing whitespace is ignored.

All of the following input formats are accepted and treated as equivalent:

'08:00:2b:01:02:03:04:05'
'08-00-2b-01-02-03-04-05'
'08002b:0102030405'
'08002b-0102030405'
'0800.2b01.0203.0405'
'0800-2b01-0203-0405'
'08002b01:02030405'
'08002b0102030405'

Upper and lowercase are both accepted for the hex digits a through f. Output is always in the first form: 08:00:2b:01:02:03:04:05.

The last six formats listed are not part of any standard.

Convert EUI-48 to modified EUI-64 for IPv6

IPv6 uses a modified EUI-64 format where the 7th bit should be set to 1 after conversion from EUI-48. Use macaddr8_set7bit to perform this conversion:

SELECT macaddr8_set7bit('08:00:2b:01:02:03');

    macaddr8_set7bit
-------------------------
 0a:00:2b:ff:fe:01:02:03
(1 row)