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, opcnamespace, opcowner, opcintype, opcdefault, opckeytype)
    VALUES (
        (SELECT oid FROM pg_am WHERE amname = 'gist'),
        'gist_int4_ops',
        (SELECT oid FROM pg_namespace WHERE nspname = 'pg_catalog'),
        1,	-- UID of superuser is hardwired to 1 as of PG 7.3
        (SELECT oid FROM pg_type WHERE typname = 'int4'),
        true,
        (SELECT oid FROM pg_type WHERE typname = 'int4key'));


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';

-- add a new opclass
INSERT INTO pg_opclass (opcamid, opcname, opcnamespace, opcowner, opcintype, opcdefault, opckeytype)
    VALUES (
        (SELECT oid FROM pg_am WHERE amname = 'gist'),
        'gist_timestamp_ops',
        (SELECT oid FROM pg_namespace WHERE nspname = 'pg_catalog'),
        1,	-- UID of superuser is hardwired to 1 as of PG 7.3
        (SELECT oid FROM pg_type WHERE typname = 'timestamp'),
        true,
        (SELECT oid FROM pg_type WHERE typname = 'tskey'));

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;

