-- -- INET -- -- prepare the table... DROP TABLE INET_TBL; ERROR: table "inet_tbl" does not exist CREATE TABLE INET_TBL (c cidr, i inet); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1', '192.168.1.226/24'); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.0/24', '192.168.1.226'); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1', '192.168.1.0/24'); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1', '192.168.1.0/25'); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1', '192.168.1.255/24'); INSERT INTO INET_TBL (c, i) VALUES ('192.168.1', '192.168.1.255/25'); INSERT INTO INET_TBL (c, i) VALUES ('10', '10.1.2.3/8'); INSERT INTO INET_TBL (c, i) VALUES ('10.0.0.0', '10.1.2.3/8'); INSERT INTO INET_TBL (c, i) VALUES ('10.1.2.3', '10.1.2.3/32'); INSERT INTO INET_TBL (c, i) VALUES ('10.1.2', '10.1.2.3/24'); INSERT INTO INET_TBL (c, i) VALUES ('10.1', '10.1.2.3/16'); INSERT INTO INET_TBL (c, i) VALUES ('10', '10.1.2.3/8'); INSERT INTO INET_TBL (c, i) VALUES ('10', '11.1.2.3/8'); INSERT INTO INET_TBL (c, i) VALUES ('10', '9.1.2.3/8'); -- check that CIDR rejects invalid input: INSERT INTO INET_TBL (c, i) VALUES ('192.168.1.2/24', '192.168.1.226'); ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask -- check that CIDR rejects invalid input when converting from text: INSERT INTO INET_TBL (c, i) VALUES (cidr('192.168.1.2/24'), '192.168.1.226'); ERROR: invalid CIDR value '192.168.1.2/24': has bits set to right of mask SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL; ten | cidr | inet -----+----------------+------------------ | 192.168.1.0/24 | 192.168.1.226/24 | 192.168.1.0/24 | 192.168.1.226 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/25 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.0/24 | 192.168.1.255/25 | 10.0.0.0/8 | 10.1.2.3/8 | 10.0.0.0/32 | 10.1.2.3/8 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.0/24 | 10.1.2.3/24 | 10.1.0.0/16 | 10.1.2.3/16 | 10.0.0.0/8 | 10.1.2.3/8 | 10.0.0.0/8 | 11.1.2.3/8 | 10.0.0.0/8 | 9.1.2.3/8 (14 rows) -- now test some support functions SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL; ten | inet | host | text -----+------------------+---------------+------------------ | 192.168.1.226/24 | 192.168.1.226 | 192.168.1.226/24 | 192.168.1.226 | 192.168.1.226 | 192.168.1.226/32 | 192.168.1.0/24 | 192.168.1.0 | 192.168.1.0/24 | 192.168.1.0/25 | 192.168.1.0 | 192.168.1.0/25 | 192.168.1.255/24 | 192.168.1.255 | 192.168.1.255/24 | 192.168.1.255/25 | 192.168.1.255 | 192.168.1.255/25 | 10.1.2.3/8 | 10.1.2.3 | 10.1.2.3/8 | 10.1.2.3/8 | 10.1.2.3 | 10.1.2.3/8 | 10.1.2.3 | 10.1.2.3 | 10.1.2.3/32 | 10.1.2.3/24 | 10.1.2.3 | 10.1.2.3/24 | 10.1.2.3/16 | 10.1.2.3 | 10.1.2.3/16 | 10.1.2.3/8 | 10.1.2.3 | 10.1.2.3/8 | 11.1.2.3/8 | 11.1.2.3 | 11.1.2.3/8 | 9.1.2.3/8 | 9.1.2.3 | 9.1.2.3/8 (14 rows) SELECT '' AS ten, c AS cidr, broadcast(c), i AS inet, broadcast(i) FROM INET_TBL; ten | cidr | broadcast | inet | broadcast -----+----------------+------------------+------------------+------------------ | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.226/24 | 192.168.1.255/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.226 | 192.168.1.226 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.0/25 | 192.168.1.127/25 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.255/24 | 192.168.1.255/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.255/25 | 192.168.1.255/25 | 10.0.0.0/8 | 10.255.255.255/8 | 10.1.2.3/8 | 10.255.255.255/8 | 10.0.0.0/32 | 10.0.0.0 | 10.1.2.3/8 | 10.255.255.255/8 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3 | 10.1.2.3 | 10.1.2.0/24 | 10.1.2.255/24 | 10.1.2.3/24 | 10.1.2.255/24 | 10.1.0.0/16 | 10.1.255.255/16 | 10.1.2.3/16 | 10.1.255.255/16 | 10.0.0.0/8 | 10.255.255.255/8 | 10.1.2.3/8 | 10.255.255.255/8 | 10.0.0.0/8 | 10.255.255.255/8 | 11.1.2.3/8 | 11.255.255.255/8 | 10.0.0.0/8 | 10.255.255.255/8 | 9.1.2.3/8 | 9.255.255.255/8 (14 rows) SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)", i AS inet, network(i) AS "network(inet)" FROM INET_TBL; ten | cidr | network(cidr) | inet | network(inet) -----+----------------+----------------+------------------+------------------ | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.226/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.226 | 192.168.1.226/32 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/25 | 192.168.1.0/25 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.255/25 | 192.168.1.128/25 | 10.0.0.0/8 | 10.0.0.0/8 | 10.1.2.3/8 | 10.0.0.0/8 | 10.0.0.0/32 | 10.0.0.0/32 | 10.1.2.3/8 | 10.0.0.0/8 | 10.1.2.3/32 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3/32 | 10.1.2.0/24 | 10.1.2.0/24 | 10.1.2.3/24 | 10.1.2.0/24 | 10.1.0.0/16 | 10.1.0.0/16 | 10.1.2.3/16 | 10.1.0.0/16 | 10.0.0.0/8 | 10.0.0.0/8 | 10.1.2.3/8 | 10.0.0.0/8 | 10.0.0.0/8 | 10.0.0.0/8 | 11.1.2.3/8 | 11.0.0.0/8 | 10.0.0.0/8 | 10.0.0.0/8 | 9.1.2.3/8 | 9.0.0.0/8 (14 rows) SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)", i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL; ten | cidr | masklen(cidr) | inet | masklen(inet) -----+----------------+---------------+------------------+--------------- | 192.168.1.0/24 | 24 | 192.168.1.226/24 | 24 | 192.168.1.0/24 | 24 | 192.168.1.226 | 32 | 192.168.1.0/24 | 24 | 192.168.1.0/24 | 24 | 192.168.1.0/24 | 24 | 192.168.1.0/25 | 25 | 192.168.1.0/24 | 24 | 192.168.1.255/24 | 24 | 192.168.1.0/24 | 24 | 192.168.1.255/25 | 25 | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 | 10.0.0.0/32 | 32 | 10.1.2.3/8 | 8 | 10.1.2.3/32 | 32 | 10.1.2.3 | 32 | 10.1.2.0/24 | 24 | 10.1.2.3/24 | 24 | 10.1.0.0/16 | 16 | 10.1.2.3/16 | 16 | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 | 10.0.0.0/8 | 8 | 11.1.2.3/8 | 8 | 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8 (14 rows) SELECT '' AS four, c AS cidr, masklen(c) AS "masklen(cidr)", i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL WHERE masklen(c) <= 8; four | cidr | masklen(cidr) | inet | masklen(inet) ------+------------+---------------+------------+--------------- | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 | 10.0.0.0/8 | 8 | 10.1.2.3/8 | 8 | 10.0.0.0/8 | 8 | 11.1.2.3/8 | 8 | 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8 (4 rows) SELECT '' AS six, c AS cidr, i AS inet FROM INET_TBL WHERE c = i; six | cidr | inet -----+----------------+---------------- | 192.168.1.0/24 | 192.168.1.0/24 | 10.1.2.3/32 | 10.1.2.3 (2 rows) SELECT '' AS ten, i, c, i < c AS lt, i <= c AS le, i = c AS eq, i >= c AS ge, i > c AS gt, i <> c AS ne, i << c AS sb, i <<= c AS sbe, i >> c AS sup, i >>= c AS spe FROM INET_TBL; ten | i | c | lt | le | eq | ge | gt | ne | sb | sbe | sup | spe -----+------------------+----------------+----+----+----+----+----+----+----+-----+-----+----- | 192.168.1.226/24 | 192.168.1.0/24 | f | f | f | t | t | t | f | t | f | t | 192.168.1.226 | 192.168.1.0/24 | f | f | f | t | t | t | t | t | f | f | 192.168.1.0/24 | 192.168.1.0/24 | f | t | t | t | f | f | f | t | f | t | 192.168.1.0/25 | 192.168.1.0/24 | f | f | f | t | t | t | t | t | f | f | 192.168.1.255/24 | 192.168.1.0/24 | f | f | f | t | t | t | f | t | f | t | 192.168.1.255/25 | 192.168.1.0/24 | f | f | f | t | t | t | t | t | f | f | 10.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | t | f | t | 10.1.2.3/8 | 10.0.0.0/32 | t | t | f | f | f | t | f | f | t | t | 10.1.2.3 | 10.1.2.3/32 | f | t | t | t | f | f | f | t | f | t | 10.1.2.3/24 | 10.1.2.0/24 | f | f | f | t | t | t | f | t | f | t | 10.1.2.3/16 | 10.1.0.0/16 | f | f | f | t | t | t | f | t | f | t | 10.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | t | f | t | 11.1.2.3/8 | 10.0.0.0/8 | f | f | f | t | t | t | f | f | f | f | 9.1.2.3/8 | 10.0.0.0/8 | t | t | f | f | f | t | f | f | f | f (14 rows) -- check the conversion to/from text and set_netmask select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL; ten | set_masklen -----+------------------ | 192.168.1.226/24 | 192.168.1.226/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.255/24 | 10.1.2.3/24 | 10.1.2.3/24 | 10.1.2.3/24 | 10.1.2.3/24 | 10.1.2.3/24 | 10.1.2.3/24 | 11.1.2.3/24 | 9.1.2.3/24 (14 rows) -- check that index works correctly create index inet_idx1 on inet_tbl(i); set enable_seqscan to off; select * from inet_tbl where i<<'192.168.1.0/24'::cidr; c | i ----------------+------------------ 192.168.1.0/24 | 192.168.1.0/25 192.168.1.0/24 | 192.168.1.255/25 192.168.1.0/24 | 192.168.1.226 (3 rows) select * from inet_tbl where i<<='192.168.1.0/24'::cidr; c | i ----------------+------------------ 192.168.1.0/24 | 192.168.1.0/24 192.168.1.0/24 | 192.168.1.226/24 192.168.1.0/24 | 192.168.1.255/24 192.168.1.0/24 | 192.168.1.0/25 192.168.1.0/24 | 192.168.1.255/25 192.168.1.0/24 | 192.168.1.226 (6 rows) set enable_seqscan to on; drop index inet_idx1;