-- Silly wrapper around a smallint.
CREATE TYPE t;

CREATE FUNCTION t_in(cstring)    RETURNS t       AS 'int2in'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_out(t)         RETURNS cstring AS 'int2out'   LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_recv(internal) RETURNS t       AS 'int2recv'  LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_send(t)        RETURNS bytea   AS 'int2send'  LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_cmp(t, t)      RETURNS int     AS 'btint2cmp' LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_lt(t, t)       RETURNS boolean AS 'int2lt'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_le(t, t)       RETURNS boolean AS 'int2le'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_eq(t, t)       RETURNS boolean AS 'int2eq'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_ge(t, t)       RETURNS boolean AS 'int2ge'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_gt(t, t)       RETURNS boolean AS 'int2gt'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
CREATE FUNCTION t_ne(t, t)       RETURNS boolean AS 'int2ne'    LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;

CREATE TYPE t (
    internallength = 2,
    input = t_in,
    output = t_out,
    receive = t_recv,
    send = t_send,
    alignment = int2,
    passedbyvalue
);

CREATE OPERATOR <  (leftarg = t, rightarg = t, procedure = t_lt, commutator = > , negator = >=, restrict = scalarltsel, join = scalarltjoinsel);
CREATE OPERATOR <= (leftarg = t, rightarg = t, procedure = t_le, commutator = >=, negator = > , restrict = scalarlesel, join = scalarlejoinsel);
CREATE OPERATOR =  (leftarg = t, rightarg = t, procedure = t_eq, commutator = = , negator = <>, restrict = eqsel,       join = eqjoinsel, HASHES, MERGES);
CREATE OPERATOR <> (leftarg = t, rightarg = t, procedure = t_ne, commutator = <>, negator = =,  restrict = neqsel,      join = neqjoinsel);
CREATE OPERATOR >= (leftarg = t, rightarg = t, procedure = t_ge, commutator = <=, negator = < , restrict = scalargesel, join = scalargejoinsel);
CREATE OPERATOR >  (leftarg = t, rightarg = t, procedure = t_gt, commutator = < , negator = <=, restrict = scalargtsel, join = scalargtjoinsel);

-- Implicitly creates a t_btree_ops operator family.
CREATE OPERATOR CLASS t_btree_ops DEFAULT FOR TYPE t USING btree AS
    OPERATOR 1 <,
    OPERATOR 2 <=,
    OPERATOR 3 =,
    OPERATOR 4 >=,
    OPERATOR 5 >,
    FUNCTION 1 t_cmp(t, t),
    FUNCTION 4 btequalimage(oid);


-- All the formalities done, now drop the type.
DROP TYPE t CASCADE;

-- The operator family still exists and can't be dropped:
DROP OPERATOR FAMILY t_btree_ops USING btree;
-- ERROR:  cache lookup failed for type 9967078
