inet_same_family() 是一个系统函数,用于判断提供的 IP 地址是否属于同一“族” (IPv4 或 IPv6)。
inet_same_family() 在 PostgreSQL 9.5 中被添加。
用法
inet_same_family (inet,inet) →boolean
如果提供了无效值,则会引发 ERROR 错误。
变更历史
- PostgreSQL 9.5
- 添加于 (commit 3b6db1f4)
示例
关于 inet_same_family() 的基本用法示例
postgres=# SELECT inet_same_family('192.168.1.2/24', '127.0.0.1');
inet_same_family
------------------
t
(1 row)
postgres=# SELECT inet_same_family('192.168.1.2', '::0');
inet_same_family
------------------
f
(1 row)
如果提供了无效地址,则会引发 ERROR 错误
postgres=# SELECT inet_same_family('192.168.1.999', '::0');
ERROR: invalid input syntax for type inet: "192.168.1.999"
LINE 1: SELECT inet_same_family('192.168.1.999', '::0');
参考资料
- PostgreSQL 文档: IP 地址函数
