New contrib module for BTREE emulation in GiST.
From Oleg Bartunov and Teodor Sigaev.
This commit is contained in:
parent
a54075a6d6
commit
a9289708af
@ -1,4 +1,4 @@
|
||||
# $Header: /cvsroot/pgsql/contrib/Makefile,v 1.23 2001/07/06 23:07:19 petere Exp $
|
||||
# $Header: /cvsroot/pgsql/contrib/Makefile,v 1.24 2001/08/22 18:27:53 tgl Exp $
|
||||
|
||||
subdir = contrib
|
||||
top_builddir = ..
|
||||
@ -6,18 +6,19 @@ include $(top_builddir)/src/Makefile.global
|
||||
|
||||
WANTED_DIRS = \
|
||||
array \
|
||||
btree_gist \
|
||||
cube \
|
||||
dbase \
|
||||
dblink \
|
||||
earthdistance \
|
||||
findoidjoins \
|
||||
fulltextindex \
|
||||
fuzzystrmatch \
|
||||
intarray \
|
||||
isbn_issn \
|
||||
lo \
|
||||
mSQL-interface \
|
||||
mac \
|
||||
metaphone \
|
||||
miscutil \
|
||||
noupdate \
|
||||
oid2name \
|
||||
@ -30,7 +31,6 @@ WANTED_DIRS = \
|
||||
rserv \
|
||||
rtree_gist \
|
||||
seg \
|
||||
soundex \
|
||||
spi \
|
||||
string \
|
||||
tips \
|
||||
|
@ -30,6 +30,14 @@ array -
|
||||
Array iterator functions
|
||||
by Massimo Dal Zotto <dz@cs.unitn.it>
|
||||
|
||||
btree_gist -
|
||||
Support for emulating BTREE indexing in GiST
|
||||
by Oleg Bartunov <oleg@sai.msu.su> and Teodor Sigaev <teodor@stack.net>
|
||||
|
||||
chkpass -
|
||||
An auto-encrypted password datatype
|
||||
by D'Arcy J.M. Cain <darcy@druid.net>
|
||||
|
||||
cube -
|
||||
Multidimensional-cube datatype (GiST indexing example)
|
||||
by Gene Selkov, Jr. <selkovjr@mcs.anl.gov>
|
||||
|
66
contrib/btree_gist/Makefile
Normal file
66
contrib/btree_gist/Makefile
Normal file
@ -0,0 +1,66 @@
|
||||
subdir = contrib/btree_gist
|
||||
top_builddir = ../..
|
||||
include $(top_builddir)/src/Makefile.global
|
||||
|
||||
# override libdir to install shlib in contrib not main directory
|
||||
libdir := $(libdir)/contrib
|
||||
|
||||
# shared library parameters
|
||||
NAME= btree_gist
|
||||
SO_MAJOR_VERSION= 1
|
||||
SO_MINOR_VERSION= 0
|
||||
|
||||
override CPPFLAGS += -I$(srcdir)
|
||||
|
||||
OBJS= btree_gist.o
|
||||
|
||||
all: all-lib $(NAME).sql
|
||||
|
||||
# Shared library stuff
|
||||
include $(top_srcdir)/src/Makefile.shlib
|
||||
|
||||
|
||||
$(NAME).sql: $(NAME).sql.in
|
||||
sed -e 's:MODULE_PATHNAME:$(libdir)/$(shlib):g' < $< > $@
|
||||
|
||||
.PHONY: submake
|
||||
submake:
|
||||
$(MAKE) -C $(top_builddir)/src/test/regress pg_regress
|
||||
|
||||
# against installed postmaster
|
||||
installcheck: submake
|
||||
$(top_builddir)/src/test/regress/pg_regress btree_gist
|
||||
|
||||
# in-tree test doesn't work yet (no way to install my shared library)
|
||||
#check: all submake
|
||||
# $(top_builddir)/src/test/regress/pg_regress --temp-install \
|
||||
# --top-builddir=$(top_builddir) btree_gist
|
||||
check:
|
||||
@echo "'make check' is not supported."
|
||||
@echo "Do 'make install', then 'make installcheck' instead."
|
||||
|
||||
install: all installdirs install-lib
|
||||
#$(INSTALL_DATA) $(srcdir)/README.$(NAME) $(docdir)/contrib
|
||||
$(INSTALL_DATA) $(NAME).sql $(datadir)/contrib
|
||||
|
||||
installdirs:
|
||||
$(mkinstalldirs) $(docdir)/contrib $(datadir)/contrib $(libdir)
|
||||
|
||||
uninstall: uninstall-lib
|
||||
rm -f $(docdir)/contrib/README.$(NAME) $(datadir)/contrib/$(NAME).sql
|
||||
|
||||
clean distclean maintainer-clean: clean-lib
|
||||
rm -f *.so y.tab.c y.tab.h $(OBJS) $(NAME).sql
|
||||
# things created by various check targets
|
||||
rm -rf results tmp_check log
|
||||
rm -f regression.diffs regression.out regress.out run_check.out
|
||||
ifeq ($(PORTNAME), win)
|
||||
rm -f regress.def
|
||||
endif
|
||||
|
||||
depend dep:
|
||||
$(CC) -MM $(CFLAGS) *.c >depend
|
||||
|
||||
ifeq (depend,$(wildcard depend))
|
||||
include depend
|
||||
endif
|
31
contrib/btree_gist/README.btree_gist
Normal file
31
contrib/btree_gist/README.btree_gist
Normal file
@ -0,0 +1,31 @@
|
||||
This is B-Tree implementation using GiST for int4 and
|
||||
timestamp types.
|
||||
|
||||
All work was done by Teodor Sigaev (teodor@stack.net) and Oleg Bartunov
|
||||
(oleg@sai.msu.su). See http://www.sai.msu.su/~megera/postgres/gist
|
||||
for additional information.
|
||||
|
||||
NOTICE:
|
||||
This version will works only with postgresql version 7.2 and above
|
||||
because of changes in interface of function calling and in system
|
||||
tables.
|
||||
|
||||
INSTALLATION:
|
||||
|
||||
gmake
|
||||
gmake install
|
||||
-- load functions
|
||||
psql <database> < btree_gist.sql
|
||||
|
||||
REGRESSION TEST:
|
||||
|
||||
gmake installcheck
|
||||
|
||||
EXAMPLE USAGE:
|
||||
|
||||
create table test (a int4);
|
||||
-- create index
|
||||
create index testidx on test using gist (a);
|
||||
-- query
|
||||
select * from test where a < 10;
|
||||
|
557
contrib/btree_gist/btree_gist.c
Normal file
557
contrib/btree_gist/btree_gist.c
Normal file
@ -0,0 +1,557 @@
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/gist.h"
|
||||
#include "access/itup.h"
|
||||
#include "access/nbtree.h"
|
||||
|
||||
#include "utils/palloc.h"
|
||||
#include "utils/geo_decls.h"
|
||||
#include "utils/elog.h"
|
||||
|
||||
typedef int (*CMPFUNC)(const void *a, const void *b);
|
||||
typedef void (*BINARY_UNION)(Datum*, char*);
|
||||
|
||||
typedef struct intkey {
|
||||
int4 lower;
|
||||
int4 upper;
|
||||
} INT4KEY;
|
||||
|
||||
typedef struct tskey {
|
||||
Timestamp lower;
|
||||
Timestamp upper;
|
||||
} TSKEY;
|
||||
|
||||
/* used for sorting */
|
||||
typedef struct rix {
|
||||
int index;
|
||||
char *r;
|
||||
} RIX;
|
||||
|
||||
/*
|
||||
** int4key in/out
|
||||
*/
|
||||
PG_FUNCTION_INFO_V1(int4key_in);
|
||||
PG_FUNCTION_INFO_V1(int4key_out);
|
||||
Datum int4key_in(PG_FUNCTION_ARGS);
|
||||
Datum int4key_out(PG_FUNCTION_ARGS);
|
||||
|
||||
/*
|
||||
** tskey in/out
|
||||
*/
|
||||
PG_FUNCTION_INFO_V1(tskey_in);
|
||||
PG_FUNCTION_INFO_V1(tskey_out);
|
||||
Datum tskey_in(PG_FUNCTION_ARGS);
|
||||
Datum tskey_out(PG_FUNCTION_ARGS);
|
||||
|
||||
/*
|
||||
** int4 ops
|
||||
*/
|
||||
PG_FUNCTION_INFO_V1(gint4_compress);
|
||||
PG_FUNCTION_INFO_V1(gint4_union);
|
||||
PG_FUNCTION_INFO_V1(gint4_picksplit);
|
||||
PG_FUNCTION_INFO_V1(gint4_consistent);
|
||||
PG_FUNCTION_INFO_V1(gint4_penalty);
|
||||
PG_FUNCTION_INFO_V1(gint4_same);
|
||||
|
||||
Datum gint4_compress(PG_FUNCTION_ARGS);
|
||||
Datum gint4_union(PG_FUNCTION_ARGS);
|
||||
Datum gint4_picksplit(PG_FUNCTION_ARGS);
|
||||
Datum gint4_consistent(PG_FUNCTION_ARGS);
|
||||
Datum gint4_penalty(PG_FUNCTION_ARGS);
|
||||
Datum gint4_same(PG_FUNCTION_ARGS);
|
||||
|
||||
static void gint4_binary_union(Datum *r1, char *r2);
|
||||
static int int4key_cmp(const void *a, const void *b);
|
||||
|
||||
/*
|
||||
** timestamp ops
|
||||
*/
|
||||
PG_FUNCTION_INFO_V1(gts_compress);
|
||||
PG_FUNCTION_INFO_V1(gts_union);
|
||||
PG_FUNCTION_INFO_V1(gts_picksplit);
|
||||
PG_FUNCTION_INFO_V1(gts_consistent);
|
||||
PG_FUNCTION_INFO_V1(gts_penalty);
|
||||
PG_FUNCTION_INFO_V1(gts_same);
|
||||
|
||||
Datum gts_compress(PG_FUNCTION_ARGS);
|
||||
Datum gts_union(PG_FUNCTION_ARGS);
|
||||
Datum gts_picksplit(PG_FUNCTION_ARGS);
|
||||
Datum gts_consistent(PG_FUNCTION_ARGS);
|
||||
Datum gts_penalty(PG_FUNCTION_ARGS);
|
||||
Datum gts_same(PG_FUNCTION_ARGS);
|
||||
|
||||
static void gts_binary_union(Datum *r1, char *r2);
|
||||
static int tskey_cmp(const void *a, const void *b);
|
||||
|
||||
/* define for comparison */
|
||||
#define TSGE( ts1, ts2 ) (DatumGetBool(DirectFunctionCall2( \
|
||||
timestamp_ge, \
|
||||
PointerGetDatum( ts1 ), \
|
||||
PointerGetDatum( ts2 ) \
|
||||
)))
|
||||
#define TSGT( ts1, ts2 ) (DatumGetBool(DirectFunctionCall2( \
|
||||
timestamp_gt, \
|
||||
PointerGetDatum( ts1 ), \
|
||||
PointerGetDatum( ts2 ) \
|
||||
)))
|
||||
#define TSEQ( ts1, ts2 ) (DatumGetBool(DirectFunctionCall2( \
|
||||
timestamp_eq, \
|
||||
PointerGetDatum( ts1 ), \
|
||||
PointerGetDatum( ts2 ) \
|
||||
)))
|
||||
#define TSLT( ts1, ts2 ) (DatumGetBool(DirectFunctionCall2( \
|
||||
timestamp_lt, \
|
||||
PointerGetDatum( ts1 ), \
|
||||
PointerGetDatum( ts2 ) \
|
||||
)))
|
||||
#define TSLE( ts1, ts2 ) (DatumGetBool(DirectFunctionCall2( \
|
||||
timestamp_le, \
|
||||
PointerGetDatum( ts1 ), \
|
||||
PointerGetDatum( ts2 ) \
|
||||
)))
|
||||
|
||||
/*
|
||||
** Common btree-function (for all ops)
|
||||
*/
|
||||
static GIST_SPLITVEC * btree_picksplit(bytea *entryvec, GIST_SPLITVEC *v,
|
||||
BINARY_UNION bu, CMPFUNC cmp);
|
||||
|
||||
PG_FUNCTION_INFO_V1(btree_decompress);
|
||||
Datum btree_decompress(PG_FUNCTION_ARGS);
|
||||
|
||||
/**************************************************
|
||||
* int4 ops
|
||||
**************************************************/
|
||||
|
||||
Datum
|
||||
gint4_compress(PG_FUNCTION_ARGS)
|
||||
{
|
||||
GISTENTRY *entry=(GISTENTRY*)PG_GETARG_POINTER(0);
|
||||
GISTENTRY *retval;
|
||||
|
||||
if ( entry->leafkey) {
|
||||
INT4KEY *r = palloc(sizeof(INT4KEY));
|
||||
retval = palloc(sizeof(GISTENTRY));
|
||||
r->lower = r->upper = (entry->key);
|
||||
|
||||
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page,
|
||||
entry->offset, sizeof(INT4KEY),FALSE);
|
||||
|
||||
} else {
|
||||
retval = entry;
|
||||
}
|
||||
PG_RETURN_POINTER( retval );
|
||||
}
|
||||
|
||||
Datum
|
||||
gint4_consistent(PG_FUNCTION_ARGS)
|
||||
{
|
||||
GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
|
||||
int4 query = PG_GETARG_INT32(1);
|
||||
INT4KEY *kkk= (INT4KEY *)DatumGetPointer(entry->key);
|
||||
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
||||
bool retval;
|
||||
|
||||
switch(strategy) {
|
||||
case BTLessEqualStrategyNumber:
|
||||
retval = ( query >= kkk->lower );
|
||||
break;
|
||||
case BTLessStrategyNumber:
|
||||
if (GIST_LEAF(entry))
|
||||
retval = ( query > kkk->lower );
|
||||
else
|
||||
retval = ( query >= kkk->lower );
|
||||
break;
|
||||
case BTEqualStrategyNumber:
|
||||
/* in leaf page kkk->lower always = kkk->upper */
|
||||
if (GIST_LEAF(entry))
|
||||
retval = ( query == kkk->lower );
|
||||
else
|
||||
retval = ( kkk->lower <= query && query <= kkk->upper );
|
||||
break;
|
||||
case BTGreaterStrategyNumber:
|
||||
if (GIST_LEAF(entry))
|
||||
retval = ( query < kkk->upper );
|
||||
else
|
||||
retval = ( query <= kkk->upper );
|
||||
break;
|
||||
case BTGreaterEqualStrategyNumber:
|
||||
retval = ( query <= kkk->upper );
|
||||
break;
|
||||
default:
|
||||
retval = FALSE;
|
||||
}
|
||||
PG_RETURN_BOOL(retval);
|
||||
}
|
||||
|
||||
Datum
|
||||
gint4_union(PG_FUNCTION_ARGS)
|
||||
{
|
||||
bytea *entryvec = (bytea*) PG_GETARG_POINTER(0);
|
||||
int i, numranges;
|
||||
INT4KEY *cur, *out=palloc(sizeof(INT4KEY));
|
||||
|
||||
numranges = (VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY);
|
||||
*(int*) PG_GETARG_POINTER(1) = sizeof(INT4KEY);
|
||||
|
||||
cur = (INT4KEY *)DatumGetPointer( (((GISTENTRY *)(VARDATA(entryvec)))[0].key) );
|
||||
out->lower = cur->lower;
|
||||
out->upper = cur->upper;
|
||||
|
||||
for (i = 1; i < numranges; i++) {
|
||||
cur = (INT4KEY *)DatumGetPointer( (((GISTENTRY *)(VARDATA(entryvec)))[i].key) );
|
||||
if ( out->lower > cur->lower ) out->lower = cur->lower;
|
||||
if ( out->upper < cur->upper ) out->upper = cur->upper;
|
||||
}
|
||||
|
||||
PG_RETURN_POINTER( out );
|
||||
}
|
||||
|
||||
Datum
|
||||
gint4_penalty(PG_FUNCTION_ARGS)
|
||||
{
|
||||
INT4KEY *origentry = (INT4KEY*) DatumGetPointer( ((GISTENTRY*) PG_GETARG_POINTER(0))->key );
|
||||
INT4KEY *newentry = (INT4KEY*) DatumGetPointer( ((GISTENTRY*) PG_GETARG_POINTER(1))->key );
|
||||
float *result = (float*) PG_GETARG_POINTER(2);
|
||||
|
||||
*result = Max( newentry->upper - origentry->upper, 0 ) +
|
||||
Max( origentry->lower - newentry->lower, 0 );
|
||||
|
||||
PG_RETURN_POINTER( result );
|
||||
}
|
||||
|
||||
Datum
|
||||
gint4_picksplit(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_POINTER( btree_picksplit(
|
||||
(bytea*)PG_GETARG_POINTER(0),
|
||||
(GIST_SPLITVEC*)PG_GETARG_POINTER(1),
|
||||
gint4_binary_union,
|
||||
int4key_cmp
|
||||
) );
|
||||
}
|
||||
|
||||
Datum
|
||||
gint4_same(PG_FUNCTION_ARGS)
|
||||
{
|
||||
INT4KEY *b1 = (INT4KEY*) PG_GETARG_POINTER(0);
|
||||
INT4KEY *b2 = (INT4KEY*) PG_GETARG_POINTER(1);
|
||||
bool *result = (bool*) PG_GETARG_POINTER(2);
|
||||
|
||||
*result = ( b1->lower == b2->lower && b1->upper == b2->upper ) ? TRUE : FALSE;
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
|
||||
static void
|
||||
gint4_binary_union(Datum *r1, char *r2)
|
||||
{
|
||||
INT4KEY *b1;
|
||||
INT4KEY *b2 = (INT4KEY*) r2;
|
||||
if ( ! DatumGetPointer( *r1 ) ) {
|
||||
*r1 = PointerGetDatum( palloc( sizeof(INT4KEY) ) );
|
||||
b1 = (INT4KEY*)DatumGetPointer( *r1 );
|
||||
b1->upper = b2->upper;
|
||||
b1->lower = b2->lower;
|
||||
} else {
|
||||
b1 = (INT4KEY*)DatumGetPointer( *r1 );
|
||||
|
||||
b1->lower = ( b1->lower > b2->lower ) ?
|
||||
b2->lower : b1->lower;
|
||||
b1->upper = ( b1->upper > b2->upper ) ?
|
||||
b1->upper : b2->upper;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
int4key_cmp(const void *a, const void *b) {
|
||||
return ( ((INT4KEY*)(((RIX*)a)->r))->lower - ((INT4KEY*)(((RIX*)b)->r))->lower );
|
||||
}
|
||||
|
||||
/**************************************************
|
||||
* timestamp ops
|
||||
**************************************************/
|
||||
|
||||
Datum
|
||||
gts_compress(PG_FUNCTION_ARGS)
|
||||
{
|
||||
GISTENTRY *entry=(GISTENTRY*)PG_GETARG_POINTER(0);
|
||||
GISTENTRY *retval;
|
||||
|
||||
if ( entry->leafkey) {
|
||||
TSKEY *r = (TSKEY *)palloc( sizeof(TSKEY) );
|
||||
retval = palloc(sizeof(GISTENTRY));
|
||||
if ( entry->key ) {
|
||||
r->lower = r->upper = *(Timestamp*)(entry->key);
|
||||
|
||||
gistentryinit(*retval, PointerGetDatum(r),
|
||||
entry->rel, entry->page,
|
||||
entry->offset, sizeof(TSKEY), FALSE);
|
||||
|
||||
} else {
|
||||
gistentryinit(*retval, PointerGetDatum(NULL),
|
||||
entry->rel, entry->page,
|
||||
entry->offset, 0, FALSE);
|
||||
}
|
||||
} else {
|
||||
retval = entry;
|
||||
}
|
||||
PG_RETURN_POINTER( retval );
|
||||
}
|
||||
|
||||
Datum
|
||||
gts_consistent(PG_FUNCTION_ARGS)
|
||||
{
|
||||
GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
|
||||
Timestamp *query = (Timestamp *)PG_GETARG_POINTER(1);
|
||||
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
||||
bool retval;
|
||||
TSKEY *key;
|
||||
/*
|
||||
** if entry is not leaf, use gbox_internal_consistent,
|
||||
** else use gbox_leaf_consistent
|
||||
*/
|
||||
if ( ! entry->key )
|
||||
return FALSE;
|
||||
key = (TSKEY*) DatumGetPointer(entry->key);
|
||||
|
||||
switch(strategy) {
|
||||
case BTLessEqualStrategyNumber:
|
||||
retval = TSGE( query, &(key->lower) );
|
||||
break;
|
||||
case BTLessStrategyNumber:
|
||||
if (GIST_LEAF(entry))
|
||||
retval = TSGT( query, &(key->lower) );
|
||||
else
|
||||
retval = TSGE( query, &(key->lower) );
|
||||
break;
|
||||
case BTEqualStrategyNumber:
|
||||
/* in leaf page key->lower always = key->upper */
|
||||
if (GIST_LEAF(entry))
|
||||
retval = TSEQ( query, &(key->lower));
|
||||
else
|
||||
retval = ( TSLE( &(key->lower), query ) && TSLE( query, &(key->upper) ) );
|
||||
break;
|
||||
case BTGreaterStrategyNumber:
|
||||
if (GIST_LEAF(entry))
|
||||
retval = TSLT( query, &(key->upper) );
|
||||
else
|
||||
retval = TSLE( query, &(key->upper) );
|
||||
break;
|
||||
case BTGreaterEqualStrategyNumber:
|
||||
retval = TSLE( query, &(key->upper) );
|
||||
break;
|
||||
default:
|
||||
retval = FALSE;
|
||||
}
|
||||
PG_RETURN_BOOL(retval);
|
||||
}
|
||||
|
||||
Datum
|
||||
gts_union(PG_FUNCTION_ARGS)
|
||||
{
|
||||
bytea *entryvec = (bytea*) PG_GETARG_POINTER(0);
|
||||
int i, numranges;
|
||||
TSKEY *cur, *out=palloc(sizeof(TSKEY));
|
||||
|
||||
numranges = (VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY);
|
||||
*(int*) PG_GETARG_POINTER(1) = sizeof(TSKEY);
|
||||
|
||||
cur = (TSKEY *)DatumGetPointer( (((GISTENTRY *)(VARDATA(entryvec)))[0].key) );
|
||||
out->lower = cur->lower;
|
||||
out->upper = cur->upper;
|
||||
|
||||
for (i = 1; i < numranges; i++) {
|
||||
cur = (TSKEY *)DatumGetPointer( (((GISTENTRY *)(VARDATA(entryvec)))[i].key) );
|
||||
if ( TSGT( &out->lower, &cur->lower ) ) out->lower = cur->lower;
|
||||
if ( TSLT( &out->upper, &cur->upper ) ) out->upper = cur->upper;
|
||||
}
|
||||
|
||||
PG_RETURN_POINTER( out );
|
||||
}
|
||||
|
||||
Datum
|
||||
gts_penalty(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TSKEY *origentry = (TSKEY*) DatumGetPointer( ((GISTENTRY*) PG_GETARG_POINTER(0))->key );
|
||||
TSKEY *newentry = (TSKEY*) DatumGetPointer( ((GISTENTRY*) PG_GETARG_POINTER(1))->key );
|
||||
float *result = (float*) PG_GETARG_POINTER(2);
|
||||
Interval *intr;
|
||||
|
||||
intr = DatumGetIntervalP( DirectFunctionCall2(
|
||||
timestamp_mi,
|
||||
TimestampGetDatum( newentry->upper ),
|
||||
TimestampGetDatum( origentry->upper )) );
|
||||
|
||||
/* see interval_larger */
|
||||
*result = Max( intr->time+intr->month * (30.0 * 86400),0 );
|
||||
pfree( intr );
|
||||
|
||||
intr = DatumGetIntervalP( DirectFunctionCall2(
|
||||
timestamp_mi,
|
||||
TimestampGetDatum( origentry->lower ),
|
||||
TimestampGetDatum( newentry->lower )) );
|
||||
|
||||
/* see interval_larger */
|
||||
*result += Max( intr->time+intr->month * (30.0 * 86400),0 );
|
||||
pfree( intr );
|
||||
|
||||
PG_RETURN_POINTER( result );
|
||||
}
|
||||
|
||||
Datum
|
||||
gts_picksplit(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_POINTER( btree_picksplit(
|
||||
(bytea*)PG_GETARG_POINTER(0),
|
||||
(GIST_SPLITVEC*)PG_GETARG_POINTER(1),
|
||||
gts_binary_union,
|
||||
tskey_cmp
|
||||
) );
|
||||
}
|
||||
|
||||
Datum
|
||||
gts_same(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TSKEY *b1 = (TSKEY*) PG_GETARG_POINTER(0);
|
||||
TSKEY *b2 = (TSKEY*) PG_GETARG_POINTER(1);
|
||||
|
||||
bool *result = (bool*) PG_GETARG_POINTER(2);
|
||||
if ( b1 && b2 )
|
||||
*result = ( TSEQ( &(b1->lower), &(b2->lower) ) && TSEQ( &(b1->upper), &(b2->upper) ) ) ? TRUE : FALSE;
|
||||
else
|
||||
*result = ( b1==NULL && b2==NULL ) ? TRUE : FALSE;
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
|
||||
static void
|
||||
gts_binary_union(Datum *r1, char *r2)
|
||||
{
|
||||
TSKEY *b1;
|
||||
TSKEY *b2 = (TSKEY*) r2;
|
||||
|
||||
if ( ! DatumGetPointer( *r1 ) ) {
|
||||
*r1 = PointerGetDatum( palloc( sizeof(TSKEY) ) );
|
||||
b1 = (TSKEY*)DatumGetPointer( *r1 );
|
||||
b1->upper = b2->upper;
|
||||
b1->lower = b2->lower;
|
||||
} else {
|
||||
b1 = (TSKEY*)DatumGetPointer( *r1 );
|
||||
|
||||
b1->lower = ( TSGT( &b1->lower, &b2->lower) ) ?
|
||||
b2->lower : b1->lower;
|
||||
b1->upper = ( TSGT( &b1->upper, &b2->upper) ) ?
|
||||
b1->upper : b2->upper;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
tskey_cmp(const void *a, const void *b) {
|
||||
Interval *intr;
|
||||
float result;
|
||||
|
||||
intr = DatumGetIntervalP( DirectFunctionCall2(
|
||||
timestamp_mi,
|
||||
TimestampGetDatum( ((TSKEY*)(((RIX*)a)->r))->lower ),
|
||||
TimestampGetDatum( ((TSKEY*)(((RIX*)b)->r))->lower )) );
|
||||
|
||||
/* see interval_larger */
|
||||
result = intr->time+intr->month * (30.0 * 86400);
|
||||
pfree( intr );
|
||||
if ( result == 0.0 )
|
||||
return 0;
|
||||
else
|
||||
return ( result>0 ) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**************************************************
|
||||
* Common btree-function (for all ops)
|
||||
**************************************************/
|
||||
|
||||
/*
|
||||
** The GiST PickSplit method
|
||||
*/
|
||||
static GIST_SPLITVEC *
|
||||
btree_picksplit(bytea *entryvec, GIST_SPLITVEC *v, BINARY_UNION bu, CMPFUNC cmp)
|
||||
{
|
||||
OffsetNumber i;
|
||||
RIX *array;
|
||||
OffsetNumber maxoff;
|
||||
int nbytes;
|
||||
|
||||
maxoff = ((VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY)) - 1;
|
||||
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
||||
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
||||
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
||||
v->spl_nleft = 0;
|
||||
v->spl_nright = 0;
|
||||
v->spl_ldatum = PointerGetDatum( 0 );
|
||||
v->spl_rdatum = PointerGetDatum( 0 );
|
||||
array = (RIX*)palloc( sizeof(RIX) * (maxoff+1) );
|
||||
|
||||
/* copy the data into RIXes, and sort the RIXes */
|
||||
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
|
||||
array[i].index = i;
|
||||
array[i].r=(char *)DatumGetPointer( (((GISTENTRY *)(VARDATA(entryvec)))[i].key) );
|
||||
}
|
||||
qsort((void*)&array[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1,
|
||||
sizeof(RIX), cmp);
|
||||
|
||||
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
|
||||
if (i <= (maxoff - FirstOffsetNumber + 1)/2) {
|
||||
v->spl_left[ v->spl_nleft ] = array[i].index;
|
||||
v->spl_nleft++;
|
||||
(*bu)( &v->spl_ldatum, array[i].r );
|
||||
} else {
|
||||
v->spl_right[ v->spl_nright ] = array[i].index;
|
||||
v->spl_nright++;
|
||||
(*bu)( &v->spl_rdatum, array[i].r );
|
||||
}
|
||||
}
|
||||
pfree(array);
|
||||
|
||||
return( v );
|
||||
}
|
||||
|
||||
/*
|
||||
** GiST DeCompress methods
|
||||
** do not do anything.
|
||||
*/
|
||||
Datum
|
||||
btree_decompress(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
|
||||
}
|
||||
|
||||
|
||||
/**************************************************
|
||||
* In/Out for keys, not really needed
|
||||
**************************************************/
|
||||
Datum
|
||||
int4key_in(PG_FUNCTION_ARGS) {
|
||||
INT4KEY *key = palloc(sizeof(INT4KEY));
|
||||
|
||||
if ( sscanf( PG_GETARG_POINTER(0), "%d|%d", &(key->lower), &(key->upper)) != 2 )
|
||||
elog(ERROR, "Error in input format");
|
||||
|
||||
PG_RETURN_POINTER( key );
|
||||
}
|
||||
|
||||
Datum int4key_out(PG_FUNCTION_ARGS) {
|
||||
INT4KEY *key = (INT4KEY *) PG_GETARG_POINTER(0);
|
||||
char *str=palloc(sizeof(char)*22);
|
||||
sprintf(str,"%d|%d", key->lower, key->upper);
|
||||
PG_RETURN_POINTER( str );
|
||||
}
|
||||
|
||||
Datum
|
||||
tskey_in(PG_FUNCTION_ARGS) {
|
||||
elog(ERROR, "Not implemented");
|
||||
PG_RETURN_POINTER( NULL );
|
||||
}
|
||||
|
||||
Datum
|
||||
tskey_out(PG_FUNCTION_ARGS) {
|
||||
elog(ERROR, "Not implemented");
|
||||
PG_RETURN_POINTER( NULL );
|
||||
}
|
263
contrib/btree_gist/btree_gist.sql.in
Normal file
263
contrib/btree_gist/btree_gist.sql.in
Normal file
@ -0,0 +1,263 @@
|
||||
begin transaction;
|
||||
-- create type of int4 key
|
||||
|
||||
CREATE FUNCTION int4key_in(opaque)
|
||||
RETURNS opaque
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE 'c' with (isstrict);
|
||||
|
||||
CREATE FUNCTION int4key_out(opaque)
|
||||
RETURNS opaque
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE 'c' with (isstrict);
|
||||
|
||||
CREATE TYPE int4key (
|
||||
internallength = 8,
|
||||
input = int4key_in,
|
||||
output = int4key_out
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
--
|
||||
--
|
||||
-- int4 ops
|
||||
--
|
||||
--
|
||||
--
|
||||
-- define the GiST support methods
|
||||
create function gint4_consistent(opaque,int4,int2) returns bool as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gint4_compress(opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function btree_decompress(opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gint4_penalty(opaque,opaque,opaque) returns opaque as 'MODULE_PATHNAME' language 'C' with(isstrict);
|
||||
|
||||
create function gint4_picksplit(opaque, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gint4_union(bytea, opaque) returns int4 as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gint4_same(opaque, opaque, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
-- add a new opclass
|
||||
INSERT INTO pg_opclass (opcamid, opcname, opcintype, opckeytype, opcdefault)
|
||||
SELECT pg_am.oid, 'gist_int4_ops', pg_type.oid, pg_key.oid, true
|
||||
FROM pg_type, pg_am, pg_type pg_key
|
||||
WHERE pg_type.typname = 'int4' and
|
||||
pg_key.typname = 'int4key' and
|
||||
pg_am.amname='gist';
|
||||
|
||||
|
||||
SELECT o.oid AS opoid, o.oprname
|
||||
INTO TABLE int_ops_tmp
|
||||
FROM pg_operator o, pg_type t
|
||||
WHERE o.oprleft = t.oid and o.oprright = t.oid
|
||||
and t.typname = 'int4';
|
||||
|
||||
-- get the comparators for int4es and store them in a tmp table
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 1, 'f'
|
||||
FROM pg_opclass opcl, int_ops_tmp c
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and c.oprname = '<';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 2, 'f'
|
||||
FROM pg_opclass opcl, int_ops_tmp c
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and c.oprname = '<=';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 3, 'f'
|
||||
FROM pg_opclass opcl, int_ops_tmp c
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and c.oprname = '=';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 4, 'f'
|
||||
FROM pg_opclass opcl, int_ops_tmp c
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and c.oprname = '>=';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 5, 'f'
|
||||
FROM pg_opclass opcl, int_ops_tmp c
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and c.oprname = '>';
|
||||
|
||||
|
||||
DROP table int_ops_tmp;
|
||||
|
||||
-- add the entries to amproc for the support methods
|
||||
-- note the amprocnum numbers associated with each are specific!
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 1
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'gint4_consistent';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 2
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'gint4_union';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 3
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'gint4_compress';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 4
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'btree_decompress';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 5
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'gint4_penalty';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 6
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'gint4_picksplit';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 7
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_int4_ops'
|
||||
and proname = 'gint4_same';
|
||||
|
||||
--
|
||||
--
|
||||
--
|
||||
-- timestamp ops
|
||||
--
|
||||
--
|
||||
--
|
||||
-- create type of timestamp key
|
||||
|
||||
CREATE FUNCTION tskey_in(opaque)
|
||||
RETURNS opaque
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE 'c' with (isstrict);
|
||||
|
||||
CREATE FUNCTION tskey_out(opaque)
|
||||
RETURNS opaque
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE 'c' with (isstrict);
|
||||
|
||||
CREATE TYPE tskey (
|
||||
internallength = 16,
|
||||
input = tskey_in,
|
||||
output = tskey_out
|
||||
);
|
||||
|
||||
create function gts_consistent(opaque,timestamp,int2) returns bool as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gts_compress(opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gts_penalty(opaque,opaque,opaque) returns opaque as 'MODULE_PATHNAME' language 'C' with(isstrict);
|
||||
|
||||
create function gts_picksplit(opaque, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gts_union(bytea, opaque) returns int4 as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
create function gts_same(opaque, opaque, opaque) returns opaque as 'MODULE_PATHNAME' language 'C';
|
||||
|
||||
INSERT INTO pg_opclass (opcamid, opcname, opcintype, opckeytype, opcdefault)
|
||||
SELECT pg_am.oid, 'gist_timestamp_ops', pg_type.oid, pg_key.oid, true
|
||||
FROM pg_type, pg_am, pg_type pg_key
|
||||
WHERE pg_type.typname = 'timestamp' and
|
||||
pg_key.typname = 'tskey' and
|
||||
pg_am.amname='gist';
|
||||
|
||||
SELECT o.oid AS opoid, o.oprname
|
||||
INTO TABLE timestamp_ops_tmp
|
||||
FROM pg_operator o, pg_type t
|
||||
WHERE o.oprleft = t.oid and o.oprright = t.oid
|
||||
and t.typname = 'timestamp';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 1, 'f'
|
||||
FROM pg_opclass opcl, timestamp_ops_tmp c
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and c.oprname = '<';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 2, 'f'
|
||||
FROM pg_opclass opcl, timestamp_ops_tmp c
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and c.oprname = '<=';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 3, 'f'
|
||||
FROM pg_opclass opcl, timestamp_ops_tmp c
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and c.oprname = '=';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 4, 'f'
|
||||
FROM pg_opclass opcl, timestamp_ops_tmp c
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and c.oprname = '>=';
|
||||
|
||||
INSERT INTO pg_amop (amopclaid, amopopr, amopstrategy, amopreqcheck)
|
||||
SELECT opcl.oid, c.opoid, 5, 'f'
|
||||
FROM pg_opclass opcl, timestamp_ops_tmp c
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and c.oprname = '>';
|
||||
|
||||
DROP table timestamp_ops_tmp;
|
||||
|
||||
-- add the entries to amproc for the support methods
|
||||
-- note the amprocnum numbers associated with each are specific!
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 1
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'gts_consistent';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 2
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'gts_union';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 3
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'gts_compress';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 4
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'btree_decompress';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 5
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'gts_penalty';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 6
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'gts_picksplit';
|
||||
|
||||
INSERT INTO pg_amproc (amopclaid, amproc, amprocnum)
|
||||
SELECT opcl.oid, pro.oid, 7
|
||||
FROM pg_opclass opcl, pg_proc pro
|
||||
WHERE opcname = 'gist_timestamp_ops'
|
||||
and proname = 'gts_same';
|
||||
|
||||
end transaction;
|
||||
|
1000
contrib/btree_gist/data/test_btree.data
Normal file
1000
contrib/btree_gist/data/test_btree.data
Normal file
File diff suppressed because it is too large
Load Diff
1000
contrib/btree_gist/data/test_btree_ts.data
Normal file
1000
contrib/btree_gist/data/test_btree_ts.data
Normal file
File diff suppressed because it is too large
Load Diff
39
contrib/btree_gist/expected/btree_gist.out
Normal file
39
contrib/btree_gist/expected/btree_gist.out
Normal file
@ -0,0 +1,39 @@
|
||||
--
|
||||
-- first, define the datatype. Turn off echoing so that expected file
|
||||
-- does not depend on contents of seg.sql.
|
||||
--
|
||||
\set ECHO none
|
||||
create table inttmp (b int4);
|
||||
\copy inttmp from 'data/test_btree.data'
|
||||
create table tstmp ( t datetime );
|
||||
\copy tstmp from 'data/test_btree_ts.data'
|
||||
-- without idx
|
||||
select count(*) from inttmp where b <=10;
|
||||
count
|
||||
-------
|
||||
11
|
||||
(1 row)
|
||||
|
||||
select count(*) from tstmp where t < '2001-05-29 08:33:09+04';
|
||||
count
|
||||
-------
|
||||
7
|
||||
(1 row)
|
||||
|
||||
-- create idx
|
||||
create index aaaidx on inttmp using gist ( b );
|
||||
create index tsidx on tstmp using gist ( t );
|
||||
--with idx
|
||||
set enable_seqscan=off;
|
||||
select count(*) from inttmp where b <=10;
|
||||
count
|
||||
-------
|
||||
11
|
||||
(1 row)
|
||||
|
||||
select count(*) from tstmp where t < '2001-05-29 08:33:09+04';
|
||||
count
|
||||
-------
|
||||
7
|
||||
(1 row)
|
||||
|
36
contrib/btree_gist/sql/btree_gist.sql
Normal file
36
contrib/btree_gist/sql/btree_gist.sql
Normal file
@ -0,0 +1,36 @@
|
||||
--
|
||||
-- first, define the datatype. Turn off echoing so that expected file
|
||||
-- does not depend on contents of seg.sql.
|
||||
--
|
||||
\set ECHO none
|
||||
\i btree_gist.sql
|
||||
\set ECHO all
|
||||
|
||||
create table inttmp (b int4);
|
||||
|
||||
\copy inttmp from 'data/test_btree.data'
|
||||
|
||||
create table tstmp ( t datetime );
|
||||
|
||||
\copy tstmp from 'data/test_btree_ts.data'
|
||||
|
||||
-- without idx
|
||||
|
||||
select count(*) from inttmp where b <=10;
|
||||
|
||||
select count(*) from tstmp where t < '2001-05-29 08:33:09+04';
|
||||
|
||||
-- create idx
|
||||
|
||||
create index aaaidx on inttmp using gist ( b );
|
||||
|
||||
create index tsidx on tstmp using gist ( t );
|
||||
|
||||
--with idx
|
||||
|
||||
set enable_seqscan=off;
|
||||
|
||||
select count(*) from inttmp where b <=10;
|
||||
|
||||
select count(*) from tstmp where t < '2001-05-29 08:33:09+04';
|
||||
|
Loading…
Reference in New Issue
Block a user