Commit 1f1ca182 authored by Tom Lane's avatar Tom Lane

Make inet/cidr << and <<= operators indexable. From Alex Pilosov <alex@pilosoft.com>.

parent 2917f0a5
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.106 2001/06/05 17:13:51 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.107 2001/06/17 02:05:19 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -95,6 +95,7 @@ static bool match_special_index_operator(Expr *clause, Oid opclass, Oid relam, ...@@ -95,6 +95,7 @@ static bool match_special_index_operator(Expr *clause, Oid opclass, Oid relam,
bool indexkey_on_left); bool indexkey_on_left);
static List *prefix_quals(Var *leftop, Oid expr_op, static List *prefix_quals(Var *leftop, Oid expr_op,
char *prefix, Pattern_Prefix_Status pstatus); char *prefix, Pattern_Prefix_Status pstatus);
static List *network_prefix_quals(Var *leftop, Oid expr_op, Datum rightop);
static Oid find_operator(const char *opname, Oid datatype); static Oid find_operator(const char *opname, Oid datatype);
static Datum string_to_datum(const char *str, Oid datatype); static Datum string_to_datum(const char *str, Oid datatype);
static Const *string_to_const(const char *str, Oid datatype); static Const *string_to_const(const char *str, Oid datatype);
...@@ -1761,6 +1762,13 @@ match_special_index_operator(Expr *clause, Oid opclass, Oid relam, ...@@ -1761,6 +1762,13 @@ match_special_index_operator(Expr *clause, Oid opclass, Oid relam,
pfree(patt); pfree(patt);
} }
break; break;
case OID_INET_SUB_OP:
case OID_INET_SUBEQ_OP:
case OID_CIDR_SUB_OP:
case OID_CIDR_SUBEQ_OP:
isIndexable = true;
break;
} }
/* done if the expression doesn't look indexable */ /* done if the expression doesn't look indexable */
...@@ -1810,6 +1818,22 @@ match_special_index_operator(Expr *clause, Oid opclass, Oid relam, ...@@ -1810,6 +1818,22 @@ match_special_index_operator(Expr *clause, Oid opclass, Oid relam,
!op_class(find_operator("<", NAMEOID), opclass, relam)) !op_class(find_operator("<", NAMEOID), opclass, relam))
isIndexable = false; isIndexable = false;
break; break;
case OID_INET_SUB_OP:
case OID_INET_SUBEQ_OP:
/* for SUB we actually need ">" not ">=", but this should do */
if (!op_class(find_operator(">=", INETOID), opclass, relam) ||
!op_class(find_operator("<=", INETOID), opclass, relam))
isIndexable = false;
break;
case OID_CIDR_SUB_OP:
case OID_CIDR_SUBEQ_OP:
/* for SUB we actually need ">" not ">=", but this should do */
if (!op_class(find_operator(">=", CIDROID), opclass, relam) ||
!op_class(find_operator("<=", CIDROID), opclass, relam))
isIndexable = false;
break;
} }
return isIndexable; return isIndexable;
...@@ -1924,6 +1948,16 @@ expand_indexqual_conditions(List *indexquals) ...@@ -1924,6 +1948,16 @@ expand_indexqual_conditions(List *indexquals)
pfree(patt); pfree(patt);
break; break;
case OID_INET_SUB_OP:
case OID_INET_SUBEQ_OP:
case OID_CIDR_SUB_OP:
case OID_CIDR_SUBEQ_OP:
constvalue = ((Const *) rightop)->constvalue;
resultquals = nconc(resultquals,
network_prefix_quals(leftop, expr_op,
constvalue));
break;
default: default:
resultquals = lappend(resultquals, clause); resultquals = lappend(resultquals, clause);
break; break;
...@@ -2037,6 +2071,86 @@ prefix_quals(Var *leftop, Oid expr_op, ...@@ -2037,6 +2071,86 @@ prefix_quals(Var *leftop, Oid expr_op,
return result; return result;
} }
/*
* Given a leftop and a rightop, and a inet-class sup/sub operator,
* generate suitable indexqual condition(s). expr_op is the original
* operator.
*/
static List *
network_prefix_quals(Var *leftop, Oid expr_op, Datum rightop)
{
bool is_eq;
char *opr1name;
Datum opr1right;
Datum opr2right;
Oid opr1oid;
Oid opr2oid;
List *result;
Oid datatype;
Oper *op;
Expr *expr;
switch (expr_op)
{
case OID_INET_SUB_OP:
datatype = INETOID;
is_eq = false;
break;
case OID_INET_SUBEQ_OP:
datatype = INETOID;
is_eq = true;
break;
case OID_CIDR_SUB_OP:
datatype = CIDROID;
is_eq = false;
break;
case OID_CIDR_SUBEQ_OP:
datatype = CIDROID;
is_eq = true;
break;
default:
elog(ERROR, "network_prefix_quals: unexpected operator %u",
expr_op);
return NIL;
}
/*
* create clause "key >= network_scan_first( rightop )", or ">"
* if the operator disallows equality.
*/
opr1name = is_eq ? ">=" : ">";
opr1oid = find_operator(opr1name, datatype);
if (opr1oid == InvalidOid)
elog(ERROR, "network_prefix_quals: no %s operator for type %u",
opr1name, datatype);
opr1right = network_scan_first( rightop );
op = makeOper(opr1oid, InvalidOid, BOOLOID);
expr = make_opclause(op, leftop,
(Var *) makeConst(datatype, -1, opr1right,
false, false, false, false));
result = makeList1(expr);
/* create clause "key <= network_scan_last( rightop )" */
opr2oid = find_operator("<=", datatype);
if (opr2oid == InvalidOid)
elog(ERROR, "network_prefix_quals: no <= operator for type %u",
datatype);
opr2right = network_scan_last( rightop );
op = makeOper(opr2oid, InvalidOid, BOOLOID);
expr = make_opclause(op, leftop,
(Var *) makeConst(datatype, -1, opr2right,
false, false, false, false));
result = lappend(result, expr);
return result;
}
/* /*
* Handy subroutines for match_special_index_operator() and friends. * Handy subroutines for match_special_index_operator() and friends.
*/ */
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* is for IP V4 CIDR notation, but prepared for V6: just * is for IP V4 CIDR notation, but prepared for V6: just
* add the necessary bits where the comments indicate. * add the necessary bits where the comments indicate.
* *
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.31 2001/06/13 21:08:59 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.32 2001/06/17 02:05:20 tgl Exp $
* *
* Jon Postel RIP 16 Oct 1998 * Jon Postel RIP 16 Oct 1998
*/ */
...@@ -707,3 +707,31 @@ v4addressOK(unsigned long a1, int bits) ...@@ -707,3 +707,31 @@ v4addressOK(unsigned long a1, int bits)
return true; return true;
return false; return false;
} }
/*
* These functions are used by planner to generate indexscan limits
* for clauses a << b and a <<= b
*/
/* return the minimal value for an IP on a given network */
Datum
network_scan_first(Datum in)
{
return DirectFunctionCall1(network_network, in);
}
/*
* return "last" IP on a given network. It's the broadcast address,
* however, masklen has to be set to 32, since
* 192.168.0.255/24 is considered less than 192.168.0.255/32
*
* NB: this is not IPv6 ready ...
*/
Datum
network_scan_last(Datum in)
{
return DirectFunctionCall2(inet_set_masklen,
DirectFunctionCall1(network_broadcast, in),
Int32GetDatum(32));
}
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_operator.h,v 1.90 2001/06/10 22:32:35 tgl Exp $ * $Id: pg_operator.h,v 1.91 2001/06/17 02:05:20 tgl Exp $
* *
* NOTES * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki
...@@ -674,9 +674,13 @@ DATA(insert OID = 1204 ( "<=" PGUID 0 b t f 869 869 16 1206 1205 0 0 ...@@ -674,9 +674,13 @@ DATA(insert OID = 1204 ( "<=" PGUID 0 b t f 869 869 16 1206 1205 0 0
DATA(insert OID = 1205 ( ">" PGUID 0 b t f 869 869 16 1203 1204 0 0 network_gt scalargtsel scalargtjoinsel )); DATA(insert OID = 1205 ( ">" PGUID 0 b t f 869 869 16 1203 1204 0 0 network_gt scalargtsel scalargtjoinsel ));
DATA(insert OID = 1206 ( ">=" PGUID 0 b t f 869 869 16 1204 1203 0 0 network_ge scalargtsel scalargtjoinsel )); DATA(insert OID = 1206 ( ">=" PGUID 0 b t f 869 869 16 1204 1203 0 0 network_ge scalargtsel scalargtjoinsel ));
DATA(insert OID = 931 ( "<<" PGUID 0 b t f 869 869 16 933 0 0 0 network_sub - - )); DATA(insert OID = 931 ( "<<" PGUID 0 b t f 869 869 16 933 0 0 0 network_sub - - ));
#define OID_INET_SUB_OP 931
DATA(insert OID = 932 ( "<<=" PGUID 0 b t f 869 869 16 934 0 0 0 network_subeq - - )); DATA(insert OID = 932 ( "<<=" PGUID 0 b t f 869 869 16 934 0 0 0 network_subeq - - ));
#define OID_INET_SUBEQ_OP 932
DATA(insert OID = 933 ( ">>" PGUID 0 b t f 869 869 16 931 0 0 0 network_sup - - )); DATA(insert OID = 933 ( ">>" PGUID 0 b t f 869 869 16 931 0 0 0 network_sup - - ));
#define OID_INET_SUP_OP 933
DATA(insert OID = 934 ( ">>=" PGUID 0 b t f 869 869 16 932 0 0 0 network_supeq - - )); DATA(insert OID = 934 ( ">>=" PGUID 0 b t f 869 869 16 932 0 0 0 network_supeq - - ));
#define OID_INET_SUPEQ_OP 934
/* CIDR type */ /* CIDR type */
DATA(insert OID = 820 ( "=" PGUID 0 b t f 650 650 16 820 821 822 822 network_eq eqsel eqjoinsel )); DATA(insert OID = 820 ( "=" PGUID 0 b t f 650 650 16 820 821 822 822 network_eq eqsel eqjoinsel ));
...@@ -686,9 +690,13 @@ DATA(insert OID = 823 ( "<=" PGUID 0 b t f 650 650 16 825 824 0 0 netwo ...@@ -686,9 +690,13 @@ DATA(insert OID = 823 ( "<=" PGUID 0 b t f 650 650 16 825 824 0 0 netwo
DATA(insert OID = 824 ( ">" PGUID 0 b t f 650 650 16 822 823 0 0 network_gt scalargtsel scalargtjoinsel )); DATA(insert OID = 824 ( ">" PGUID 0 b t f 650 650 16 822 823 0 0 network_gt scalargtsel scalargtjoinsel ));
DATA(insert OID = 825 ( ">=" PGUID 0 b t f 650 650 16 823 822 0 0 network_ge scalargtsel scalargtjoinsel )); DATA(insert OID = 825 ( ">=" PGUID 0 b t f 650 650 16 823 822 0 0 network_ge scalargtsel scalargtjoinsel ));
DATA(insert OID = 826 ( "<<" PGUID 0 b t f 650 650 16 828 0 0 0 network_sub - - )); DATA(insert OID = 826 ( "<<" PGUID 0 b t f 650 650 16 828 0 0 0 network_sub - - ));
#define OID_CIDR_SUB_OP 826
DATA(insert OID = 827 ( "<<=" PGUID 0 b t f 650 650 16 1004 0 0 0 network_subeq - - )); DATA(insert OID = 827 ( "<<=" PGUID 0 b t f 650 650 16 1004 0 0 0 network_subeq - - ));
#define OID_CIDR_SUBEQ_OP 827
DATA(insert OID = 828 ( ">>" PGUID 0 b t f 650 650 16 826 0 0 0 network_sup - - )); DATA(insert OID = 828 ( ">>" PGUID 0 b t f 650 650 16 826 0 0 0 network_sup - - ));
#define OID_CIDR_SUP_OP 828
DATA(insert OID = 1004 ( ">>=" PGUID 0 b t f 650 650 16 827 0 0 0 network_supeq - - )); DATA(insert OID = 1004 ( ">>=" PGUID 0 b t f 650 650 16 827 0 0 0 network_supeq - - ));
#define OID_CIDR_SUPEQ_OP 1004
/* case-insensitive LIKE hacks */ /* case-insensitive LIKE hacks */
DATA(insert OID = 1625 ( "~~*" PGUID 0 b t f 19 25 16 0 1626 0 0 nameiclike iclikesel iclikejoinsel )); DATA(insert OID = 1625 ( "~~*" PGUID 0 b t f 19 25 16 0 1626 0 0 nameiclike iclikesel iclikejoinsel ));
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: builtins.h,v 1.154 2001/06/14 01:09:22 tgl Exp $ * $Id: builtins.h,v 1.155 2001/06/17 02:05:20 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -527,6 +527,8 @@ extern double convert_network_to_scalar(Datum value, Oid typid); ...@@ -527,6 +527,8 @@ extern double convert_network_to_scalar(Datum value, Oid typid);
extern Datum text_cidr(PG_FUNCTION_ARGS); extern Datum text_cidr(PG_FUNCTION_ARGS);
extern Datum text_inet(PG_FUNCTION_ARGS); extern Datum text_inet(PG_FUNCTION_ARGS);
extern Datum inet_set_masklen(PG_FUNCTION_ARGS); extern Datum inet_set_masklen(PG_FUNCTION_ARGS);
extern Datum network_scan_first(Datum in);
extern Datum network_scan_last(Datum in);
/* mac.c */ /* mac.c */
extern Datum macaddr_in(PG_FUNCTION_ARGS); extern Datum macaddr_in(PG_FUNCTION_ARGS);
......
...@@ -7,6 +7,10 @@ ERROR: table "inet_tbl" does not exist ...@@ -7,6 +7,10 @@ ERROR: table "inet_tbl" does not exist
CREATE TABLE INET_TBL (c cidr, i inet); 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', '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.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', '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.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.3', '10.1.2.3/32');
...@@ -26,6 +30,10 @@ SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL; ...@@ -26,6 +30,10 @@ SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL;
-----+----------------+------------------ -----+----------------+------------------
| 192.168.1.0/24 | 192.168.1.226/24 | 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.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/8 | 10.1.2.3/8
| 10.0.0.0/32 | 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.3/32 | 10.1.2.3
...@@ -34,7 +42,7 @@ SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL; ...@@ -34,7 +42,7 @@ SELECT '' AS ten, c AS cidr, i AS inet FROM INET_TBL;
| 10.0.0.0/8 | 10.1.2.3/8 | 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 | 11.1.2.3/8
| 10.0.0.0/8 | 9.1.2.3/8 | 10.0.0.0/8 | 9.1.2.3/8
(10 rows) (14 rows)
-- now test some support functions -- now test some support functions
SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL; SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL;
...@@ -42,6 +50,10 @@ SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL; ...@@ -42,6 +50,10 @@ SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL;
-----+------------------+---------------+------------------ -----+------------------+---------------+------------------
| 192.168.1.226/24 | 192.168.1.226 | 192.168.1.226/24 | 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.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/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 | 10.1.2.3 | 10.1.2.3/32
...@@ -50,7 +62,7 @@ SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL; ...@@ -50,7 +62,7 @@ SELECT '' AS ten, i AS inet, host(i), text(i) FROM INET_TBL;
| 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
| 11.1.2.3/8 | 11.1.2.3 | 11.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 | 9.1.2.3/8 | 9.1.2.3 | 9.1.2.3/8
(10 rows) (14 rows)
SELECT '' AS ten, c AS cidr, broadcast(c), SELECT '' AS ten, c AS cidr, broadcast(c),
i AS inet, broadcast(i) FROM INET_TBL; i AS inet, broadcast(i) FROM INET_TBL;
...@@ -58,6 +70,10 @@ SELECT '' AS ten, c AS cidr, broadcast(c), ...@@ -58,6 +70,10 @@ SELECT '' AS ten, c AS cidr, broadcast(c),
-----+----------------+------------------+------------------+------------------ -----+----------------+------------------+------------------+------------------
| 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/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.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/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.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.3/32 | 10.1.2.3 | 10.1.2.3 | 10.1.2.3
...@@ -66,7 +82,7 @@ SELECT '' AS ten, c AS cidr, broadcast(c), ...@@ -66,7 +82,7 @@ SELECT '' AS ten, c AS cidr, broadcast(c),
| 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 | 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 | 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 | 10.0.0.0/8 | 10.255.255.255/8 | 9.1.2.3/8 | 9.255.255.255/8
(10 rows) (14 rows)
SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)", SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)",
i AS inet, network(i) AS "network(inet)" FROM INET_TBL; i AS inet, network(i) AS "network(inet)" FROM INET_TBL;
...@@ -74,6 +90,10 @@ SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)", ...@@ -74,6 +90,10 @@ SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)",
-----+----------------+----------------+------------------+------------------ -----+----------------+----------------+------------------+------------------
| 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/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.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/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.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.3/32 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3/32
...@@ -82,7 +102,7 @@ SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)", ...@@ -82,7 +102,7 @@ SELECT '' AS ten, c AS cidr, network(c) AS "network(cidr)",
| 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 | 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 | 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 | 10.0.0.0/8 | 10.0.0.0/8 | 9.1.2.3/8 | 9.0.0.0/8
(10 rows) (14 rows)
SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)", SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)",
i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL; i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL;
...@@ -90,6 +110,10 @@ SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)", ...@@ -90,6 +110,10 @@ SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)",
-----+----------------+---------------+------------------+--------------- -----+----------------+---------------+------------------+---------------
| 192.168.1.0/24 | 24 | 192.168.1.226/24 | 24 | 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.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/8 | 8 | 10.1.2.3/8 | 8
| 10.0.0.0/32 | 32 | 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.3/32 | 32 | 10.1.2.3 | 32
...@@ -98,7 +122,7 @@ SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)", ...@@ -98,7 +122,7 @@ SELECT '' AS ten, c AS cidr, masklen(c) AS "masklen(cidr)",
| 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 | 11.1.2.3/8 | 8
| 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8 | 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8
(10 rows) (14 rows)
SELECT '' AS four, c AS cidr, masklen(c) AS "masklen(cidr)", SELECT '' AS four, c AS cidr, masklen(c) AS "masklen(cidr)",
i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL
...@@ -113,10 +137,11 @@ SELECT '' AS four, c AS cidr, masklen(c) AS "masklen(cidr)", ...@@ -113,10 +137,11 @@ SELECT '' AS four, c AS cidr, masklen(c) AS "masklen(cidr)",
SELECT '' AS six, c AS cidr, i AS inet FROM INET_TBL SELECT '' AS six, c AS cidr, i AS inet FROM INET_TBL
WHERE c = i; WHERE c = i;
six | cidr | inet six | cidr | inet
-----+-------------+---------- -----+----------------+----------------
| 10.1.2.3/32 | 10.1.2.3 | 192.168.1.0/24 | 192.168.1.0/24
(1 row) | 10.1.2.3/32 | 10.1.2.3
(2 rows)
SELECT '' AS ten, i, c, SELECT '' AS ten, i, c,
i < c AS lt, i <= c AS le, i = c AS eq, i < c AS lt, i <= c AS le, i = c AS eq,
...@@ -128,6 +153,10 @@ SELECT '' AS ten, i, c, ...@@ -128,6 +153,10 @@ SELECT '' AS ten, i, c,
-----+------------------+----------------+----+----+----+----+----+----+----+-----+-----+----- -----+------------------+----------------+----+----+----+----+----+----+----+-----+-----+-----
| 192.168.1.226/24 | 192.168.1.0/24 | f | f | f | t | t | t | f | t | f | t | 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.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/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/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 | 10.1.2.3/32 | f | t | t | t | f | f | f | t | f | t
...@@ -136,14 +165,18 @@ SELECT '' AS ten, i, c, ...@@ -136,14 +165,18 @@ SELECT '' AS ten, i, c,
| 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/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 | 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 | 9.1.2.3/8 | 10.0.0.0/8 | t | t | f | f | f | t | f | f | f | f
(10 rows) (14 rows)
-- check the conversion to/from text and set_netmask -- check the conversion to/from text and set_netmask
select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL; select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
ten | set_masklen ten | set_masklen
-----+------------------ -----+------------------
| 192.168.1.226/24 | 192.168.1.226/24
| 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
...@@ -152,5 +185,29 @@ select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL; ...@@ -152,5 +185,29 @@ select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
| 10.1.2.3/24 | 10.1.2.3/24
| 11.1.2.3/24 | 11.1.2.3/24
| 9.1.2.3/24 | 9.1.2.3/24
(10 rows) (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;
...@@ -8,6 +8,10 @@ DROP TABLE INET_TBL; ...@@ -8,6 +8,10 @@ DROP TABLE INET_TBL;
CREATE TABLE INET_TBL (c cidr, i inet); 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', '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.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', '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.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.3', '10.1.2.3/32');
...@@ -49,3 +53,11 @@ SELECT '' AS ten, i, c, ...@@ -49,3 +53,11 @@ SELECT '' AS ten, i, c,
-- check the conversion to/from text and set_netmask -- check the conversion to/from text and set_netmask
select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL; select '' AS ten, set_masklen(inet(text(i)), 24) FROM INET_TBL;
-- 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;
select * from inet_tbl where i<<='192.168.1.0/24'::cidr;
set enable_seqscan to on;
drop index inet_idx1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment