diff -c -r -N contrib.orig/btree_gist/btree_cash.c contrib/btree_gist/btree_cash.c
*** contrib.orig/btree_gist/btree_cash.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_cash.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 71,76 ****
--- 71,81 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8 
+ gbt_cash_dist(const void *a, const void *b)
+ {
+ 	return (float8)Abs(*((Cash *) a) - *((Cash *) b));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 81,89 ****
  	gbt_casheq,
  	gbt_cashle,
  	gbt_cashlt,
! 	gbt_cashkey_cmp
  };
  
  
  /**************************************************
   * Cash ops
--- 86,105 ----
  	gbt_casheq,
  	gbt_cashle,
  	gbt_cashlt,
! 	gbt_cashkey_cmp,
! 	gbt_cash_dist
  };
  
+ PG_FUNCTION_INFO_V1(cash_dist);
+ Datum		cash_dist(PG_FUNCTION_ARGS);
+ Datum
+ cash_dist(PG_FUNCTION_ARGS)
+ {
+ 	Cash		a = PG_GETARG_CASH(0);
+ 	Cash		b = PG_GETARG_CASH(1);
+ 
+ 	PG_RETURN_CASH( Abs(a - b) );
+ }
  
  /**************************************************
   * Cash ops
***************
*** 118,124 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 134,140 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_date.c contrib/btree_gist/btree_date.c
*** contrib.orig/btree_gist/btree_date.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_date.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 84,89 ****
--- 84,97 ----
  	return res;
  }
  
+ static float8
+ gdb_date_dist(const void *a, const void *b)
+ {
+ 	int4 res = DatumGetInt32(
+ 			DirectFunctionCall2(date_mi, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
+ 		);
+ 	return (float8)Abs(res);
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 94,102 ****
  	gbt_dateeq,
  	gbt_datele,
  	gbt_datelt,
! 	gbt_datekey_cmp
  };
  
  
  /**************************************************
   * date ops
--- 102,123 ----
  	gbt_dateeq,
  	gbt_datele,
  	gbt_datelt,
! 	gbt_datekey_cmp,
! 	gdb_date_dist
  };
  
+ PG_FUNCTION_INFO_V1(date_dist);
+ Datum       date_dist(PG_FUNCTION_ARGS);
+ Datum
+ date_dist(PG_FUNCTION_ARGS)
+ {
+ 	int4 res = DatumGetInt32(
+ 			DirectFunctionCall2(date_mi, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1))
+ 		);
+ 
+ 	PG_RETURN_INT32( Abs(res)  );
+ }
+ 
  
  /**************************************************
   * date ops
***************
*** 133,139 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 154,160 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_float4.c contrib/btree_gist/btree_float4.c
*** contrib.orig/btree_gist/btree_float4.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_float4.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 70,75 ****
--- 70,80 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8
+ gbt_float4_dist(const void *a, const void *b)
+ {
+ 	return (float8)Abs(*((float4 *) a) - *((float4 *) b));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 80,88 ****
  	gbt_float4eq,
  	gbt_float4le,
  	gbt_float4lt,
! 	gbt_float4key_cmp
  };
  
  
  /**************************************************
   * float4 ops
--- 85,104 ----
  	gbt_float4eq,
  	gbt_float4le,
  	gbt_float4lt,
! 	gbt_float4key_cmp,
! 	gbt_float4_dist
  };
  
+ PG_FUNCTION_INFO_V1(float4_dist);
+ Datum       float4_dist(PG_FUNCTION_ARGS);
+ Datum
+ float4_dist(PG_FUNCTION_ARGS)
+ {
+     float4        a = PG_GETARG_FLOAT4(0);
+ 	float4        b = PG_GETARG_FLOAT4(1);
+ 
+ 	PG_RETURN_FLOAT4( Abs(a - b) );
+ }
  
  /**************************************************
   * float4 ops
***************
*** 117,123 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 133,139 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_float8.c contrib/btree_gist/btree_float8.c
*** contrib.orig/btree_gist/btree_float8.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_float8.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 71,76 ****
--- 71,81 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8
+ gbt_float8_dist(const void *a, const void *b)
+ {
+ 	return Abs(*((float8 *) a) - *((float8 *) b));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 81,89 ****
  	gbt_float8eq,
  	gbt_float8le,
  	gbt_float8lt,
! 	gbt_float8key_cmp
  };
  
  
  /**************************************************
   * float8 ops
--- 86,105 ----
  	gbt_float8eq,
  	gbt_float8le,
  	gbt_float8lt,
! 	gbt_float8key_cmp,
! 	gbt_float8_dist
  };
  
+ PG_FUNCTION_INFO_V1(float8_dist);
+ Datum       float8_dist(PG_FUNCTION_ARGS);
+ Datum
+ float8_dist(PG_FUNCTION_ARGS)
+ {
+     float8        a = PG_GETARG_FLOAT8(0);
+ 	float8        b = PG_GETARG_FLOAT8(1);
+ 
+ 	PG_RETURN_FLOAT8( Abs(a - b) );
+ }
  
  /**************************************************
   * float8 ops
***************
*** 118,124 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 134,140 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_gist.sql.in contrib/btree_gist/btree_gist.sql.in
*** contrib.orig/btree_gist/btree_gist.sql.in	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_gist.sql.in	2009-12-30 15:44:18.000000000 +0300
***************
*** 84,90 ****
--- 84,233 ----
  	STORAGE = EXTENDED
  );
  
+ --distance operation
+ CREATE OR REPLACE FUNCTION cash_dist(money, money)
+ RETURNS money
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = money,
+ 	RIGHTARG = money,
+ 	PROCEDURE = cash_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION date_dist(date, date)
+ RETURNS int4
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
  
+ CREATE OPERATOR <-> (
+ 	LEFTARG = date,
+ 	RIGHTARG = date,
+ 	PROCEDURE = date_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION float4_dist(float4, float4)
+ RETURNS float4
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = float4,
+ 	RIGHTARG = float4,
+ 	PROCEDURE = float4_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION float8_dist(float8, float8)
+ RETURNS float8
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = float8,
+ 	RIGHTARG = float8,
+ 	PROCEDURE = float8_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION int2_dist(int2, int2)
+ RETURNS int2
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = int2,
+ 	RIGHTARG = int2,
+ 	PROCEDURE = int2_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION int4_dist(int4, int4)
+ RETURNS int4
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = int4,
+ 	RIGHTARG = int4,
+ 	PROCEDURE = int4_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION int8_dist(int8, int8)
+ RETURNS int8
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = int8,
+ 	RIGHTARG = int8,
+ 	PROCEDURE = int8_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION interval_dist(interval, interval)
+ RETURNS interval
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = interval,
+ 	RIGHTARG = interval,
+ 	PROCEDURE = interval_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION oid_dist(oid, oid)
+ RETURNS oid
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = oid,
+ 	RIGHTARG = oid,
+ 	PROCEDURE = oid_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION time_dist(time, time)
+ RETURNS interval
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = time,
+ 	RIGHTARG = time,
+ 	PROCEDURE = time_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION ts_dist(timestamp, timestamp)
+ RETURNS interval
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = timestamp,
+ 	RIGHTARG = timestamp,
+ 	PROCEDURE = ts_dist,
+ 	COMMUTATOR = '<->'
+ );
+ 
+ CREATE OR REPLACE FUNCTION tstz_dist(timestamptz, timestamptz)
+ RETURNS interval
+ AS 'MODULE_PATHNAME'
+ LANGUAGE C IMMUTABLE STRICT;
+ 
+ CREATE OPERATOR <-> (
+ 	LEFTARG = timestamptz,
+ 	RIGHTARG = timestamptz,
+ 	PROCEDURE = tstz_dist,
+ 	COMMUTATOR = '<->'
+ );
  
  --
  --
***************
*** 95,101 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_oid_consistent(internal,oid,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 238,244 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_oid_consistent(internal,oid,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 143,148 ****
--- 286,292 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_oid_consistent (internal, oid, int2, oid, internal),
  	FUNCTION	2	gbt_oid_union (bytea, internal),
  	FUNCTION	3	gbt_oid_compress (internal),
***************
*** 162,168 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_int2_consistent(internal,int2,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 306,312 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_int2_consistent(internal,int2,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 200,205 ****
--- 344,350 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_int2_consistent (internal, int2, int2, oid, internal),
  	FUNCTION	2	gbt_int2_union (bytea, internal),
  	FUNCTION	3	gbt_int2_compress (internal),
***************
*** 218,224 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_int4_consistent(internal,int4,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 363,369 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_int4_consistent(internal,int4,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 256,261 ****
--- 401,407 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_int4_consistent (internal, int4, int2, oid, internal),
  	FUNCTION	2	gbt_int4_union (bytea, internal),
  	FUNCTION	3	gbt_int4_compress (internal),
***************
*** 274,280 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_int8_consistent(internal,int8,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 420,426 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_int8_consistent(internal,int8,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 312,317 ****
--- 458,464 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_int8_consistent (internal, int8, int2, oid, internal),
  	FUNCTION	2	gbt_int8_union (bytea, internal),
  	FUNCTION	3	gbt_int8_compress (internal),
***************
*** 331,337 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_float4_consistent(internal,float4,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 478,484 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_float4_consistent(internal,float4,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 369,374 ****
--- 516,522 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_float4_consistent (internal, float4, int2, oid, internal),
  	FUNCTION	2	gbt_float4_union (bytea, internal),
  	FUNCTION	3	gbt_float4_compress (internal),
***************
*** 390,396 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_float8_consistent(internal,float8,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 538,544 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_float8_consistent(internal,float8,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 428,433 ****
--- 576,582 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_float8_consistent (internal, float8, int2, oid, internal),
  	FUNCTION	2	gbt_float8_union (bytea, internal),
  	FUNCTION	3	gbt_float8_compress (internal),
***************
*** 447,458 ****
  --
  
  CREATE OR REPLACE FUNCTION gbt_ts_consistent(internal,timestamp,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
  CREATE OR REPLACE FUNCTION gbt_tstz_consistent(internal,timestamptz,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
        
--- 596,607 ----
  --
  
  CREATE OR REPLACE FUNCTION gbt_ts_consistent(internal,timestamp,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
  CREATE OR REPLACE FUNCTION gbt_tstz_consistent(internal,timestamptz,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
        
***************
*** 495,500 ****
--- 644,650 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_ts_consistent (internal, timestamp, int2, oid, internal),
  	FUNCTION	2	gbt_ts_union (bytea, internal),
  	FUNCTION	3	gbt_ts_compress (internal),
***************
*** 514,519 ****
--- 664,670 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_tstz_consistent (internal, timestamptz, int2, oid, internal),
  	FUNCTION	2	gbt_ts_union (bytea, internal),
  	FUNCTION	3	gbt_tstz_compress (internal),
***************
*** 533,544 ****
  --
  
  CREATE OR REPLACE FUNCTION gbt_time_consistent(internal,time,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
  CREATE OR REPLACE FUNCTION gbt_timetz_consistent(internal,timetz,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 684,695 ----
  --
  
  CREATE OR REPLACE FUNCTION gbt_time_consistent(internal,time,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
  CREATE OR REPLACE FUNCTION gbt_timetz_consistent(internal,timetz,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 581,586 ****
--- 732,738 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_time_consistent (internal, time, int2, oid, internal),
  	FUNCTION	2	gbt_time_union (bytea, internal),
  	FUNCTION	3	gbt_time_compress (internal),
***************
*** 617,623 ****
  --
  
  CREATE OR REPLACE FUNCTION gbt_date_consistent(internal,date,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 769,775 ----
  --
  
  CREATE OR REPLACE FUNCTION gbt_date_consistent(internal,date,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 655,660 ****
--- 807,813 ----
  	OPERATOR	3	=  ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	>  ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_date_consistent (internal, date, int2, oid, internal),
  	FUNCTION	2	gbt_date_union (bytea, internal),
  	FUNCTION	3	gbt_date_compress (internal),
***************
*** 674,680 ****
  --
  
  CREATE OR REPLACE FUNCTION gbt_intv_consistent(internal,interval,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 827,833 ----
  --
  
  CREATE OR REPLACE FUNCTION gbt_intv_consistent(internal,interval,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 717,722 ****
--- 870,876 ----
  	OPERATOR	3	= ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	> ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_intv_consistent (internal, interval, int2, oid, internal),
  	FUNCTION	2	gbt_intv_union (bytea, internal),
  	FUNCTION	3	gbt_intv_compress (internal),
***************
*** 735,741 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_cash_consistent(internal,money,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 889,895 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_cash_consistent(internal,money,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 773,778 ****
--- 927,933 ----
  	OPERATOR	3	= ,
  	OPERATOR	4	>= ,
  	OPERATOR	5	> ,
+ 	OPERATOR	15	<->,
  	FUNCTION	1	gbt_cash_consistent (internal, money, int2, oid, internal),
  	FUNCTION	2	gbt_cash_union (bytea, internal),
  	FUNCTION	3	gbt_cash_compress (internal),
***************
*** 791,797 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_macad_consistent(internal,macaddr,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 946,952 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_macad_consistent(internal,macaddr,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
***************
*** 1124,1130 ****
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_inet_consistent(internal,inet,int2,oid,internal)
! RETURNS bool
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
--- 1279,1285 ----
  --
  -- define the GiST support methods
  CREATE OR REPLACE FUNCTION gbt_inet_consistent(internal,inet,int2,oid,internal)
! RETURNS float8
  AS 'MODULE_PATHNAME'
  LANGUAGE C IMMUTABLE STRICT;
  
diff -c -r -N contrib.orig/btree_gist/btree_inet.c contrib/btree_gist/btree_inet.c
*** contrib.orig/btree_gist/btree_inet.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_inet.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 84,90 ****
  	gbt_ineteq,
  	gbt_inetle,
  	gbt_inetlt,
! 	gbt_inetkey_cmp
  };
  
  
--- 84,91 ----
  	gbt_ineteq,
  	gbt_inetle,
  	gbt_inetlt,
! 	gbt_inetkey_cmp,
! 	NULL
  };
  
  
***************
*** 135,141 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(gbt_num_consistent(&key, (void *) &query,
  									  &strategy, GIST_LEAF(entry), &tinfo));
  }
  
--- 136,142 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(gbt_num_consistent(&key, (void *) &query,
  									  &strategy, GIST_LEAF(entry), &tinfo));
  }
  
diff -c -r -N contrib.orig/btree_gist/btree_int2.c contrib/btree_gist/btree_int2.c
*** contrib.orig/btree_gist/btree_int2.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_int2.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 70,75 ****
--- 70,80 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8
+ gbt_int2_dist(const void *a, const void *b)
+ {
+ 	return (float8)Abs(*((int16 *) a) - *((int16 *) b));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 80,91 ****
  	gbt_int2eq,
  	gbt_int2le,
  	gbt_int2lt,
! 	gbt_int2key_cmp
  };
  
  
  
! 
  
  
  /**************************************************
--- 85,105 ----
  	gbt_int2eq,
  	gbt_int2le,
  	gbt_int2lt,
! 	gbt_int2key_cmp,
! 	gbt_int2_dist
  };
  
  
+ PG_FUNCTION_INFO_V1(int2_dist);
+ Datum       int2_dist(PG_FUNCTION_ARGS);
+ Datum
+ int2_dist(PG_FUNCTION_ARGS)
+ {
+     int2        a = PG_GETARG_INT16(0);
+ 	int2        b = PG_GETARG_INT16(1);
  
! 	PG_RETURN_INT16( Abs(a - b) );
! }
  
  
  /**************************************************
***************
*** 121,127 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 135,141 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_int4.c contrib/btree_gist/btree_int4.c
*** contrib.orig/btree_gist/btree_int4.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_int4.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 71,76 ****
--- 71,81 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8
+ gbt_int4_dist(const void *a, const void *b)
+ {
+ 	return (float8)Abs(*((int32 *) a) - *((int32 *) b));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 81,89 ****
  	gbt_int4eq,
  	gbt_int4le,
  	gbt_int4lt,
! 	gbt_int4key_cmp
  };
  
  
  /**************************************************
   * int32 ops
--- 86,105 ----
  	gbt_int4eq,
  	gbt_int4le,
  	gbt_int4lt,
! 	gbt_int4key_cmp,
! 	gbt_int4_dist
  };
  
+ PG_FUNCTION_INFO_V1(int4_dist);
+ Datum       int4_dist(PG_FUNCTION_ARGS);
+ Datum
+ int4_dist(PG_FUNCTION_ARGS)
+ {
+     int4        a = PG_GETARG_INT32(0);
+ 	int4        b = PG_GETARG_INT32(1);
+ 
+ 	PG_RETURN_INT32( Abs(a - b) );
+ }
  
  /**************************************************
   * int32 ops
***************
*** 118,124 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 134,140 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_int8.c contrib/btree_gist/btree_int8.c
*** contrib.orig/btree_gist/btree_int8.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_int8.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 71,76 ****
--- 71,81 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8
+ gbt_int8_dist(const void *a, const void *b)
+ {
+ 	return (float8)Abs(*((int64 *) a) - *((int64 *) b));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 81,89 ****
  	gbt_int8eq,
  	gbt_int8le,
  	gbt_int8lt,
! 	gbt_int8key_cmp
  };
  
  
  /**************************************************
   * int64 ops
--- 86,105 ----
  	gbt_int8eq,
  	gbt_int8le,
  	gbt_int8lt,
! 	gbt_int8key_cmp,
! 	gbt_int8_dist
  };
  
+ PG_FUNCTION_INFO_V1(int8_dist);
+ Datum       int8_dist(PG_FUNCTION_ARGS);
+ Datum
+ int8_dist(PG_FUNCTION_ARGS)
+ {
+     int64       a = PG_GETARG_INT64(0);
+ 	int64       b = PG_GETARG_INT64(1);
+ 
+ 	PG_RETURN_INT64( Abs(a - b) );
+ }
  
  /**************************************************
   * int64 ops
***************
*** 118,124 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 134,140 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_interval.c contrib/btree_gist/btree_interval.c
*** contrib.orig/btree_gist/btree_interval.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_interval.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 83,88 ****
--- 83,94 ----
  	return INTERVAL_TO_SEC(i);
  }
  
+ static float8
+ gbt_intv_dist(const void *a, const void *b)
+ {
+ 	return (float8)Abs(intr2num((Interval*)a) - intr2num((Interval*)b));
+ }
+ 
  /*
   * INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
   * in pg_type.	This might be less than sizeof(Interval) if the compiler
***************
*** 99,107 ****
  	gbt_intveq,
  	gbt_intvle,
  	gbt_intvlt,
! 	gbt_intvkey_cmp
  };
  
  
  /**************************************************
   * interval ops
--- 105,136 ----
  	gbt_intveq,
  	gbt_intvle,
  	gbt_intvlt,
! 	gbt_intvkey_cmp,
! 	gbt_intv_dist
  };
  
+ Interval *
+ abs_interval(Interval *a)
+ {
+ 	static Interval zero = {0, 0, 0};
+ 
+ 	if (DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(&zero))))
+ 	{
+ 		a = DatumGetIntervalP(DirectFunctionCall1(interval_um, IntervalPGetDatum(a)));
+ 	}
+ 
+ 	return a;
+ }
+ 
+ PG_FUNCTION_INFO_V1(interval_dist);
+ Datum       interval_dist(PG_FUNCTION_ARGS);
+ Datum
+ interval_dist(PG_FUNCTION_ARGS)
+ {
+ 	Interval*		res = DatumGetIntervalP(DirectFunctionCall2(interval_mi,PG_GETARG_DATUM(0), PG_GETARG_DATUM(1))); 
+ 
+ 	PG_RETURN_INTERVAL_P( abs_interval(res) );
+ }
  
  /**************************************************
   * interval ops
***************
*** 184,190 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 213,219 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_macaddr.c contrib/btree_gist/btree_macaddr.c
*** contrib.orig/btree_gist/btree_macaddr.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_macaddr.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 84,90 ****
  	gbt_macadeq,
  	gbt_macadle,
  	gbt_macadlt,
! 	gbt_macadkey_cmp
  };
  
  
--- 84,91 ----
  	gbt_macadeq,
  	gbt_macadle,
  	gbt_macadlt,
! 	gbt_macadkey_cmp,
! 	NULL
  };
  
  
***************
*** 136,142 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 137,143 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_oid.c contrib/btree_gist/btree_oid.c
*** contrib.orig/btree_gist/btree_oid.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_oid.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 71,76 ****
--- 71,86 ----
  	return (ia->lower > ib->lower) ? 1 : -1;
  }
  
+ static float8
+ gbt_oid_dist(const void *a, const void *b)
+ {
+ 	if (*((Oid *) a) < *((Oid *) b))
+ 		return (float8)( *((Oid *) b) - *((Oid *) a) );
+ 	else if (*((Oid *) a) > *((Oid *) b))
+ 		return (float8)( *((Oid *) a) - *((Oid *) b) );
+ 	else
+ 		return 0.0;
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 81,89 ****
  	gbt_oideq,
  	gbt_oidle,
  	gbt_oidlt,
! 	gbt_oidkey_cmp
  };
  
  
  /**************************************************
   * Oid ops
--- 91,117 ----
  	gbt_oideq,
  	gbt_oidle,
  	gbt_oidlt,
! 	gbt_oidkey_cmp,
! 	gbt_oid_dist
  };
  
+ PG_FUNCTION_INFO_V1(oid_dist);
+ Datum       oid_dist(PG_FUNCTION_ARGS);
+ Datum
+ oid_dist(PG_FUNCTION_ARGS)
+ {
+     Oid        	a = PG_GETARG_OID(0);
+ 	Oid        	b = PG_GETARG_OID(1);
+ 	Oid 		res;
+ 
+ 	if (a > b)
+ 		res = a - b;
+ 	else if (a < b)
+ 		res = b - a;
+ 	else
+ 		res = 0;
+ 	PG_RETURN_OID( res );
+ }
  
  /**************************************************
   * Oid ops
***************
*** 118,124 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 146,152 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_time.c contrib/btree_gist/btree_time.c
*** contrib.orig/btree_gist/btree_time.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_time.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 112,117 ****
--- 112,130 ----
  	return res;
  }
  
+ static float8
+ gbt_time_dist(const void *a, const void *b)
+ {
+ 	const TimeADT *aa = (const TimeADT *) a;
+ 	const TimeADT *bb = (const TimeADT *) b;
+ 	Interval	  *i;
+ 
+ 	i = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
+ 											TimeADTGetDatumFast(*aa),
+ 											TimeADTGetDatumFast(*bb)));
+ 	return (float8)Abs(INTERVAL_TO_SEC(i));
+ }
+ 
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 122,130 ****
  	gbt_timeeq,
  	gbt_timele,
  	gbt_timelt,
! 	gbt_timekey_cmp
  };
  
  
  /**************************************************
   * time ops
--- 135,152 ----
  	gbt_timeeq,
  	gbt_timele,
  	gbt_timelt,
! 	gbt_timekey_cmp,
! 	gbt_time_dist
  };
  
+ PG_FUNCTION_INFO_V1(time_dist);
+ Datum       time_dist(PG_FUNCTION_ARGS);
+ Datum
+ time_dist(PG_FUNCTION_ARGS)
+ {
+ 	Interval	  *r = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,PG_GETARG_DATUM(0),PG_GETARG_DATUM(1)));
+ 	PG_RETURN_INTERVAL_P( abs_interval(r) );
+ }
  
  /**************************************************
   * time ops
***************
*** 191,197 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 213,219 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
***************
*** 221,227 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 243,249 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_ts.c contrib/btree_gist/btree_ts.c
*** contrib.orig/btree_gist/btree_ts.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_ts.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 4,9 ****
--- 4,10 ----
  #include "btree_gist.h"
  #include "btree_utils_num.h"
  #include "utils/datetime.h"
+ #include "utils/builtins.h"
  
  typedef struct
  {
***************
*** 110,115 ****
--- 111,131 ----
  	return res;
  }
  
+ static float8
+ gbt_ts_dist(const void *a, const void *b)
+ {
+     const Timestamp *aa = (const Timestamp *) a;
+ 	const Timestamp *bb = (const Timestamp *) b;
+ 	Interval      *i;
+ 
+ 	if (TIMESTAMP_NOT_FINITE(*aa) || TIMESTAMP_NOT_FINITE(*bb))
+ 		return get_float8_infinity();
+ 
+ 	i = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
+ 											  TimestampGetDatumFast(*aa),
+ 											  TimestampGetDatumFast(*bb)));
+ 	return (float8)Abs(INTERVAL_TO_SEC(i));
+ }
  
  static const gbtree_ninfo tinfo =
  {
***************
*** 120,129 ****
  	gbt_tseq,
  	gbt_tsle,
  	gbt_tslt,
! 	gbt_tskey_cmp
  };
  
  
  /**************************************************
   * timestamp ops
   **************************************************/
--- 136,201 ----
  	gbt_tseq,
  	gbt_tsle,
  	gbt_tslt,
! 	gbt_tskey_cmp,
! 	gbt_ts_dist
  };
  
  
+ PG_FUNCTION_INFO_V1(ts_dist);
+ Datum       ts_dist(PG_FUNCTION_ARGS);
+ Datum
+ ts_dist(PG_FUNCTION_ARGS)
+ {
+ 	Timestamp	a = PG_GETARG_TIMESTAMP(0); 
+ 	Timestamp	b = PG_GETARG_TIMESTAMP(1); 
+     Interval      *r;
+ 
+ 	if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
+ 	{
+ 		Interval *p = palloc(sizeof(Interval));
+ 
+ 		p->day = INT_MAX;
+ 		p->month = INT_MAX;
+ #ifdef HAVE_INT64_TIMESTAMP
+ 		p->time = INT64CONST(0x7FFFFFFFFFFFFFFF);	
+ #else
+ 		p->time = DBL_MAX;
+ #endif
+ 		PG_RETURN_INTERVAL_P(p);
+ 	}
+ 	else
+ 
+ 	r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,PG_GETARG_DATUM(0),PG_GETARG_DATUM(1)));
+ 	PG_RETURN_INTERVAL_P( abs_interval(r) );
+ }
+ 
+ PG_FUNCTION_INFO_V1(tstz_dist);
+ Datum       tstz_dist(PG_FUNCTION_ARGS);
+ Datum
+ tstz_dist(PG_FUNCTION_ARGS)
+ {
+ 	TimestampTz	a = PG_GETARG_TIMESTAMPTZ(0); 
+ 	TimestampTz	b = PG_GETARG_TIMESTAMPTZ(1); 
+     Interval      *r;
+ 
+ 	if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b))
+ 	{
+ 		Interval *p = palloc(sizeof(Interval));
+ 
+ 		p->day = INT_MAX;
+ 		p->month = INT_MAX;
+ #ifdef HAVE_INT64_TIMESTAMP
+ 		p->time = INT64CONST(0x7FFFFFFFFFFFFFFF);	
+ #else
+ 		p->time = DBL_MAX;
+ #endif
+ 		PG_RETURN_INTERVAL_P(p);
+ 	}
+ 
+ 	r = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,PG_GETARG_DATUM(0),PG_GETARG_DATUM(1)));
+ 	PG_RETURN_INTERVAL_P( abs_interval(r) );
+ }
+ 
  /**************************************************
   * timestamp ops
   **************************************************/
***************
*** 208,214 ****
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 280,286 ----
  	key.lower = (GBT_NUMKEY *) &kkk->lower;
  	key.upper = (GBT_NUMKEY *) &kkk->upper;
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
***************
*** 233,239 ****
  	key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
  	qqq = tstz_to_ts_gmt(query);
  
! 	PG_RETURN_BOOL(
  				   gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
--- 305,311 ----
  	key.upper = (GBT_NUMKEY *) &kkk[MAXALIGN(tinfo.size)];
  	qqq = tstz_to_ts_gmt(query);
  
! 	PG_RETURN_FLOAT8(
  				   gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
  		);
  }
diff -c -r -N contrib.orig/btree_gist/btree_utils_num.c contrib/btree_gist/btree_utils_num.c
*** contrib.orig/btree_gist/btree_utils_num.c	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_utils_num.c	2009-12-30 15:44:18.000000000 +0300
***************
*** 187,193 ****
  ** The GiST consistent method
  */
  
! bool
  gbt_num_consistent(
  				   const GBT_NUMKEY_R *key,
  				   const void *query,
--- 187,193 ----
  ** The GiST consistent method
  */
  
! float8
  gbt_num_consistent(
  				   const GBT_NUMKEY_R *key,
  				   const void *query,
***************
*** 197,235 ****
  )
  {
  
! 	bool		retval = FALSE;
  
  	switch (*strategy)
  	{
  		case BTLessEqualStrategyNumber:
! 			retval = (*tinfo->f_ge) (query, key->lower);
  			break;
  		case BTLessStrategyNumber:
  			if (is_leaf)
! 				retval = (*tinfo->f_gt) (query, key->lower);
  			else
! 				retval = (*tinfo->f_ge) (query, key->lower);
  			break;
  		case BTEqualStrategyNumber:
  			if (is_leaf)
! 				retval = (*tinfo->f_eq) (query, key->lower);
  			else
! 				retval = (*tinfo->f_le) (key->lower, query) && (*tinfo->f_le) (query, key->upper);
  			break;
  		case BTGreaterStrategyNumber:
  			if (is_leaf)
! 				retval = (*tinfo->f_lt) (query, key->upper);
  			else
! 				retval = (*tinfo->f_le) (query, key->upper);
  			break;
  		case BTGreaterEqualStrategyNumber:
! 			retval = (*tinfo->f_le) (query, key->upper);
  			break;
  		default:
! 			retval = FALSE;
  	}
  
! 	return (retval);
  }
  
  
--- 197,245 ----
  )
  {
  
! 	float8		retval = -1.0;
  
  	switch (*strategy)
  	{
  		case BTLessEqualStrategyNumber:
! 			retval = (*tinfo->f_ge) (query, key->lower) ? 0.0 : -1.0;
  			break;
  		case BTLessStrategyNumber:
  			if (is_leaf)
! 				retval = (*tinfo->f_gt) (query, key->lower) ? 0.0 : -1.0;
  			else
! 				retval = (*tinfo->f_ge) (query, key->lower) ? 0.0 : -1.0;
  			break;
  		case BTEqualStrategyNumber:
  			if (is_leaf)
! 				retval = (*tinfo->f_eq) (query, key->lower) ? 0.0 : -1.0;
  			else
! 				retval = ((*tinfo->f_le) (key->lower, query) && (*tinfo->f_le) (query, key->upper)) ? 0.0 : -1.0;
  			break;
  		case BTGreaterStrategyNumber:
  			if (is_leaf)
! 				retval = (*tinfo->f_lt) (query, key->upper) ? 0.0 : -1.0;
  			else
! 				retval = (*tinfo->f_le) (query, key->upper) ? 0.0 : -1.0;
  			break;
  		case BTGreaterEqualStrategyNumber:
! 			retval = (*tinfo->f_le) (query, key->upper) ? 0.0 : -1.0;
! 			break;
! 		case KNNSearchStrategyNumber:
! 			if (tinfo->f_dist == NULL)
! 				elog(ERROR,"KNNsearch is not supported");
! 			if ( tinfo->f_le(query, key->lower) )
! 				retval = tinfo->f_dist(query, key->lower);
! 			else if ( tinfo->f_ge(query, key->upper) )
! 				retval = tinfo->f_dist(query, key->upper);
! 			else
! 				retval = 0.0;
  			break;
  		default:
! 			retval = -1.0;
  	}
  
! 	return retval;
  }
  
  
diff -c -r -N contrib.orig/btree_gist/btree_utils_num.h contrib/btree_gist/btree_utils_num.h
*** contrib.orig/btree_gist/btree_utils_num.h	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/btree_utils_num.h	2009-12-30 15:44:18.000000000 +0300
***************
*** 46,51 ****
--- 46,52 ----
  	bool		(*f_le) (const void *, const void *);	/* less equal */
  	bool		(*f_lt) (const void *, const void *);	/* less then */
  	int			(*f_cmp) (const void *, const void *);	/* key compare function */
+ 	float8		(*f_dist) (const void *, const void *);	/* key distance function */
  } gbtree_ninfo;
  
  
***************
*** 91,98 ****
  	 (ivp)->month * (30.0 * SECS_PER_DAY))
  #endif
  
  
! extern bool gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query,
  				   const StrategyNumber *strategy, bool is_leaf,
  				   const gbtree_ninfo *tinfo);
  
--- 92,100 ----
  	 (ivp)->month * (30.0 * SECS_PER_DAY))
  #endif
  
+ extern Interval * abs_interval(Interval *a);
  
! extern float8 gbt_num_consistent(const GBT_NUMKEY_R *key, const void *query,
  				   const StrategyNumber *strategy, bool is_leaf,
  				   const gbtree_ninfo *tinfo);
  
diff -c -r -N contrib.orig/btree_gist/expected/cash.out contrib/btree_gist/expected/cash.out
*** contrib.orig/btree_gist/expected/cash.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/cash.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     253
  (1 row)
  
+ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
+      a      | ?column? 
+ ------------+----------
+  $21,472.79 |    $0.00
+  $21,469.25 |    $3.54
+  $21,915.01 |  $442.22
+ (3 rows)
+ 
  CREATE INDEX moneyidx ON moneytmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM moneytmp WHERE a <  '22649.64'::money;
***************
*** 64,66 ****
--- 72,91 ----
     253
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
+                    QUERY PLAN                   
+ ------------------------------------------------
+  Limit
+    ->  Index Scan using moneyidx on moneytmp
+          Sort Cond: (a <-> '$21,472.79'::money)
+ (3 rows)
+ 
+ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
+      a      | ?column? 
+ ------------+----------
+  $21,472.79 |    $0.00
+  $21,469.25 |    $3.54
+  $21,915.01 |  $442.22
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/date.out contrib/btree_gist/expected/date.out
*** contrib.orig/btree_gist/expected/date.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/date.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     313
  (1 row)
  
+ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
+      a      | ?column? 
+ ------------+----------
+  02-13-2001 |        0
+  02-11-2001 |        2
+  03-24-2001 |       39
+ (3 rows)
+ 
  CREATE INDEX dateidx ON datetmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM datetmp WHERE a <  '2001-02-13'::date;
***************
*** 64,66 ****
--- 72,91 ----
     313
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
+                   QUERY PLAN                   
+ -----------------------------------------------
+  Limit
+    ->  Index Scan using dateidx on datetmp
+          Sort Cond: (a <-> '02-13-2001'::date)
+ (3 rows)
+ 
+ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
+      a      | ?column? 
+ ------------+----------
+  02-13-2001 |        0
+  02-11-2001 |        2
+  03-24-2001 |       39
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/float4.out contrib/btree_gist/expected/float4.out
*** contrib.orig/btree_gist/expected/float4.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/float4.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     302
  (1 row)
  
+ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
+     a     | ?column? 
+ ----------+----------
+      -179 |        0
+  -189.024 |  10.0239
+  -158.177 |  20.8226
+ (3 rows)
+ 
  CREATE INDEX float4idx ON float4tmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM float4tmp WHERE a <  -179.0::float4;
***************
*** 64,66 ****
--- 72,91 ----
     302
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
+                   QUERY PLAN                   
+ -----------------------------------------------
+  Limit
+    ->  Index Scan using float4idx on float4tmp
+          Sort Cond: (a <-> (-179)::real)
+ (3 rows)
+ 
+ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
+     a     | ?column? 
+ ----------+----------
+      -179 |        0
+  -189.024 |  10.0239
+  -158.177 |  20.8226
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/float8.out contrib/btree_gist/expected/float8.out
*** contrib.orig/btree_gist/expected/float8.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/float8.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     306
  (1 row)
  
+ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
+       a       |  ?column?  
+ --------------+------------
+         -1890 |          0
+  -2003.634512 | 113.634512
+   -1769.73634 |  120.26366
+ (3 rows)
+ 
  CREATE INDEX float8idx ON float8tmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM float8tmp WHERE a <  -1890.0::float8;
***************
*** 64,66 ****
--- 72,91 ----
     306
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
+                       QUERY PLAN                      
+ ------------------------------------------------------
+  Limit
+    ->  Index Scan using float8idx on float8tmp
+          Sort Cond: (a <-> (-1890)::double precision)
+ (3 rows)
+ 
+ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
+       a       |  ?column?  
+ --------------+------------
+         -1890 |          0
+  -2003.634512 | 113.634512
+   -1769.73634 |  120.26366
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/int2.out contrib/btree_gist/expected/int2.out
*** contrib.orig/btree_gist/expected/int2.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/int2.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     248
  (1 row)
  
+ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
+   a  | ?column? 
+ -----+----------
+  237 |        0
+  232 |        5
+  228 |        9
+ (3 rows)
+ 
  CREATE INDEX int2idx ON int2tmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM int2tmp WHERE a <  237::int2;
***************
*** 64,66 ****
--- 72,91 ----
     248
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
+                 QUERY PLAN                 
+ -------------------------------------------
+  Limit
+    ->  Index Scan using int2idx on int2tmp
+          Sort Cond: (a <-> 237::smallint)
+ (3 rows)
+ 
+ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
+   a  | ?column? 
+ -----+----------
+  237 |        0
+  232 |        5
+  228 |        9
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/int4.out contrib/btree_gist/expected/int4.out
*** contrib.orig/btree_gist/expected/int4.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/int4.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     248
  (1 row)
  
+ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
+   a  | ?column? 
+ -----+----------
+  237 |        0
+  232 |        5
+  228 |        9
+ (3 rows)
+ 
  CREATE INDEX int4idx ON int4tmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM int4tmp WHERE a <  237::int4;
***************
*** 64,66 ****
--- 72,91 ----
     248
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
+                 QUERY PLAN                 
+ -------------------------------------------
+  Limit
+    ->  Index Scan using int4idx on int4tmp
+          Sort Cond: (a <-> 237)
+ (3 rows)
+ 
+ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
+   a  | ?column? 
+ -----+----------
+  237 |        0
+  232 |        5
+  228 |        9
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/int8.out contrib/btree_gist/expected/int8.out
*** contrib.orig/btree_gist/expected/int8.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/int8.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     270
  (1 row)
  
+ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
+         a        |    ?column?    
+ -----------------+----------------
+  464571291354841 |              0
+  457257666629329 |  7313624725512
+  478227196042750 | 13655904687909
+ (3 rows)
+ 
  CREATE INDEX int8idx ON int8tmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM int8tmp WHERE a <  464571291354841::int8;
***************
*** 64,66 ****
--- 72,91 ----
     270
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
+                      QUERY PLAN                     
+ ----------------------------------------------------
+  Limit
+    ->  Index Scan using int8idx on int8tmp
+          Sort Cond: (a <-> 464571291354841::bigint)
+ (3 rows)
+ 
+ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
+         a        |    ?column?    
+ -----------------+----------------
+  464571291354841 |              0
+  457257666629329 |  7313624725512
+  478227196042750 | 13655904687909
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/interval.out contrib/btree_gist/expected/interval.out
*** contrib.orig/btree_gist/expected/interval.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/interval.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     270
  (1 row)
  
+ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
+                   a                  |               ?column?               
+ -------------------------------------+--------------------------------------
+  @ 199 days 21 hours 21 mins 23 secs | @ 0
+  @ 183 days 6 hours 52 mins 48 secs  | @ 16 days 14 hours 28 mins 35 secs
+  @ 220 days 19 hours 5 mins 42 secs  | @ 21 days -2 hours -15 mins -41 secs
+ (3 rows)
+ 
  CREATE INDEX intervalidx ON intervaltmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM intervaltmp WHERE a <  '199 days 21:21:23'::interval;
***************
*** 64,66 ****
--- 72,91 ----
     270
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
+                                  QUERY PLAN                                 
+ ----------------------------------------------------------------------------
+  Limit
+    ->  Index Scan using intervalidx on intervaltmp
+          Sort Cond: (a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval)
+ (3 rows)
+ 
+ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
+                   a                  |               ?column?               
+ -------------------------------------+--------------------------------------
+  @ 199 days 21 hours 21 mins 23 secs | @ 0
+  @ 183 days 6 hours 52 mins 48 secs  | @ 16 days 14 hours 28 mins 35 secs
+  @ 220 days 19 hours 5 mins 42 secs  | @ 21 days -2 hours -15 mins -41 secs
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/time.out contrib/btree_gist/expected/time.out
*** contrib.orig/btree_gist/expected/time.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/time.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     292
  (1 row)
  
+ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
+     a     |    ?column?     
+ ----------+-----------------
+  10:57:11 | @ 0
+  10:57:10 | @ 1 sec
+  10:55:32 | @ 1 min 39 secs
+ (3 rows)
+ 
  CREATE INDEX timeidx ON timetmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM timetmp WHERE a <  '10:57:11'::time;
***************
*** 64,66 ****
--- 72,91 ----
     292
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
+                           QUERY PLAN                           
+ ---------------------------------------------------------------
+  Limit
+    ->  Index Scan using timeidx on timetmp
+          Sort Cond: (a <-> '10:57:11'::time without time zone)
+ (3 rows)
+ 
+ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
+     a     |    ?column?     
+ ----------+-----------------
+  10:57:11 | @ 0
+  10:57:10 | @ 1 sec
+  10:55:32 | @ 1 min 39 secs
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/timestamp.out contrib/btree_gist/expected/timestamp.out
*** contrib.orig/btree_gist/expected/timestamp.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/timestamp.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 32,37 ****
--- 32,45 ----
     289
  (1 row)
  
+ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
+             a             |              ?column?              
+ --------------------------+------------------------------------
+  Tue Oct 26 08:55:08 2004 | @ 0
+  Sun Oct 31 06:35:03 2004 | @ 4 days 21 hours 39 mins 55 secs
+  Mon Nov 29 20:12:43 2004 | @ 34 days 11 hours 17 mins 35 secs
+ (3 rows)
+ 
  CREATE INDEX timestampidx ON timestamptmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM timestamptmp WHERE a <  '2004-10-26 08:55:08'::timestamp;
***************
*** 64,66 ****
--- 72,91 ----
     289
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
+                                      QUERY PLAN                                     
+ ------------------------------------------------------------------------------------
+  Limit
+    ->  Index Scan using timestampidx on timestamptmp
+          Sort Cond: (a <-> 'Tue Oct 26 08:55:08 2004'::timestamp without time zone)
+ (3 rows)
+ 
+ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
+             a             |              ?column?              
+ --------------------------+------------------------------------
+  Tue Oct 26 08:55:08 2004 | @ 0
+  Sun Oct 31 06:35:03 2004 | @ 4 days 21 hours 39 mins 55 secs
+  Mon Nov 29 20:12:43 2004 | @ 34 days 11 hours 17 mins 35 secs
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/expected/timestamptz.out contrib/btree_gist/expected/timestamptz.out
*** contrib.orig/btree_gist/expected/timestamptz.out	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/expected/timestamptz.out	2009-12-30 15:44:18.000000000 +0300
***************
*** 92,97 ****
--- 92,105 ----
     157
  (1 row)
  
+ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
+               a               |             ?column?              
+ ------------------------------+-----------------------------------
+  Tue Dec 18 05:59:54 2018 PST | @ 1 hour
+  Thu Jan 10 03:01:34 2019 PST | @ 22 days 22 hours 1 min 40 secs
+  Thu Jan 24 12:28:12 2019 PST | @ 37 days 7 hours 28 mins 18 secs
+ (3 rows)
+ 
  CREATE INDEX timestamptzidx ON timestamptztmp USING gist ( a );
  SET enable_seqscan=off;
  SELECT count(*) FROM timestamptztmp WHERE a <  '2018-12-18 10:59:54 GMT+3'::timestamptz;
***************
*** 184,186 ****
--- 192,211 ----
     157
  (1 row)
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
+                                      QUERY PLAN                                      
+ -------------------------------------------------------------------------------------
+  Limit
+    ->  Index Scan using timestamptzidx on timestamptztmp
+          Sort Cond: (a <-> 'Tue Dec 18 04:59:54 2018 PST'::timestamp with time zone)
+ (3 rows)
+ 
+ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
+               a               |             ?column?              
+ ------------------------------+-----------------------------------
+  Tue Dec 18 05:59:54 2018 PST | @ 1 hour
+  Thu Jan 10 03:01:34 2019 PST | @ 22 days 22 hours 1 min 40 secs
+  Thu Jan 24 12:28:12 2019 PST | @ 37 days 7 hours 28 mins 18 secs
+ (3 rows)
+ 
diff -c -r -N contrib.orig/btree_gist/sql/cash.sql contrib/btree_gist/sql/cash.sql
*** contrib.orig/btree_gist/sql/cash.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/cash.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM moneytmp WHERE a >  '22649.64';
  
+ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
+ 
  CREATE INDEX moneyidx ON moneytmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 29,31 ****
--- 31,37 ----
  SELECT count(*) FROM moneytmp WHERE a >= '22649.64'::money;
  
  SELECT count(*) FROM moneytmp WHERE a >  '22649.64'::money;
+ 
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
+ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/date.sql contrib/btree_gist/sql/date.sql
*** contrib.orig/btree_gist/sql/date.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/date.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM datetmp WHERE a >  '2001-02-13';
  
+ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
+ 
  CREATE INDEX dateidx ON datetmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM datetmp WHERE a >  '2001-02-13'::date;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
+ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/float4.sql contrib/btree_gist/sql/float4.sql
*** contrib.orig/btree_gist/sql/float4.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/float4.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM float4tmp WHERE a >  -179.0;
  
+ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
+ 
  CREATE INDEX float4idx ON float4tmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 29,31 ****
--- 31,37 ----
  SELECT count(*) FROM float4tmp WHERE a >= -179.0::float4;
  
  SELECT count(*) FROM float4tmp WHERE a >  -179.0::float4;
+ 
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
+ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/float8.sql contrib/btree_gist/sql/float8.sql
*** contrib.orig/btree_gist/sql/float8.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/float8.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM float8tmp WHERE a >  -1890.0;
  
+ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
+ 
  CREATE INDEX float8idx ON float8tmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 29,31 ****
--- 31,37 ----
  SELECT count(*) FROM float8tmp WHERE a >= -1890.0::float8;
  
  SELECT count(*) FROM float8tmp WHERE a >  -1890.0::float8;
+ 
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
+ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/int2.sql contrib/btree_gist/sql/int2.sql
*** contrib.orig/btree_gist/sql/int2.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/int2.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM int2tmp WHERE a >  237;
  
+ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
+ 
  CREATE INDEX int2idx ON int2tmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM int2tmp WHERE a >  237::int2;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
+ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/int4.sql contrib/btree_gist/sql/int4.sql
*** contrib.orig/btree_gist/sql/int4.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/int4.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM int4tmp WHERE a >  237;
  
+ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
+ 
  CREATE INDEX int4idx ON int4tmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM int4tmp WHERE a >  237::int4;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
+ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/int8.sql contrib/btree_gist/sql/int8.sql
*** contrib.orig/btree_gist/sql/int8.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/int8.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM int8tmp WHERE a >  464571291354841;
  
+ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
+ 
  CREATE INDEX int8idx ON int8tmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM int8tmp WHERE a >  464571291354841::int8;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
+ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/interval.sql contrib/btree_gist/sql/interval.sql
*** contrib.orig/btree_gist/sql/interval.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/interval.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM intervaltmp WHERE a >  '199 days 21:21:23';
  
+ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
+ 
  CREATE INDEX intervalidx ON intervaltmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM intervaltmp WHERE a >  '199 days 21:21:23'::interval;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
+ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/time.sql contrib/btree_gist/sql/time.sql
*** contrib.orig/btree_gist/sql/time.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/time.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM timetmp WHERE a >  '10:57:11';
  
+ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
+ 
  CREATE INDEX timeidx ON timetmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM timetmp WHERE a >  '10:57:11'::time;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
+ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/timestamp.sql contrib/btree_gist/sql/timestamp.sql
*** contrib.orig/btree_gist/sql/timestamp.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/timestamp.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 16,21 ****
--- 16,23 ----
  
  SELECT count(*) FROM timestamptmp WHERE a >  '2004-10-26 08:55:08';
  
+ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
+ 
  CREATE INDEX timestampidx ON timestamptmp USING gist ( a );
  
  SET enable_seqscan=off;
***************
*** 30,32 ****
--- 32,37 ----
  
  SELECT count(*) FROM timestamptmp WHERE a >  '2004-10-26 08:55:08'::timestamp;
  
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
+ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/sql/timestamptz.sql contrib/btree_gist/sql/timestamptz.sql
*** contrib.orig/btree_gist/sql/timestamptz.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/sql/timestamptz.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 37,43 ****
  
  SELECT count(*) FROM timestamptztmp WHERE a >  '2018-12-18 10:59:54 GMT+4';
  
! 
  
  CREATE INDEX timestamptzidx ON timestamptztmp USING gist ( a );
  
--- 37,43 ----
  
  SELECT count(*) FROM timestamptztmp WHERE a >  '2018-12-18 10:59:54 GMT+4';
  
! SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
  
  CREATE INDEX timestamptzidx ON timestamptztmp USING gist ( a );
  
***************
*** 74,76 ****
--- 74,80 ----
  SELECT count(*) FROM timestamptztmp WHERE a >= '2018-12-18 10:59:54 GMT+4'::timestamptz;
  
  SELECT count(*) FROM timestamptztmp WHERE a >  '2018-12-18 10:59:54 GMT+4'::timestamptz;
+ 
+ EXPLAIN (COSTS OFF)
+ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
+ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
diff -c -r -N contrib.orig/btree_gist/uninstall_btree_gist.sql contrib/btree_gist/uninstall_btree_gist.sql
*** contrib.orig/btree_gist/uninstall_btree_gist.sql	2009-12-30 15:36:26.000000000 +0300
--- contrib/btree_gist/uninstall_btree_gist.sql	2009-12-30 15:44:18.000000000 +0300
***************
*** 278,280 ****
--- 278,329 ----
  DROP TYPE gbtreekey8 CASCADE;
  
  DROP TYPE gbtreekey4 CASCADE;
+ 
+ DROP OPERATOR <->(money, money);
+ 
+ DROP OPERATOR <->(date, date);
+ 
+ DROP OPERATOR <->(float4, float4);
+ 
+ DROP OPERATOR <->(float8, float8);
+ 
+ DROP OPERATOR <->(int2, int2);
+ 
+ DROP OPERATOR <->(int4, int4);
+ 
+ DROP OPERATOR <->(int8, int8);
+ 
+ DROP OPERATOR <->(interval, interval);
+ 
+ DROP OPERATOR <->(oid, oid);
+ 
+ DROP OPERATOR <->(time, time);
+ 
+ DROP OPERATOR <->(timestamp, timestamp);
+ 
+ DROP OPERATOR <->(timestamptz, timestamptz);
+ 
+ DROP FUNCTION cash_dist(money, money);
+ 
+ DROP FUNCTION date_dist(date, date);
+ 
+ DROP FUNCTION float4_dist(float4, float4);
+ 
+ DROP FUNCTION float8_dist(float8, float8);
+ 
+ DROP FUNCTION int2_dist(int2, int2);
+ 
+ DROP FUNCTION int4_dist(int4, int4);
+ 
+ DROP FUNCTION int8_dist(int8, int8);
+ 
+ DROP FUNCTION interval_dist(interval, interval);
+ 
+ DROP FUNCTION oid_dist(oid, oid);
+ 
+ DROP FUNCTION time_dist(time, time);
+ 
+ DROP FUNCTION ts_dist(timestamp, timestamp);
+ 
+ DROP FUNCTION tstz_dist(timestamptz, timestamptz);
+ 
