no primary key on self designed type

Поиск
Список
Период
Сортировка
Искать

no primary key on self designed type

От:
Thomas Drillich <drillich@uniserve.de>
Дата:
Hello,

create type inrecord (
	internallength=VARIABLE,
	input=inr_in,
	output=inr_out
);

create table test (
	data	inrecord not null primary key
);
... result ...
ERROR:  Can't find a default operator class for type 268128.

how can I define the default operator class ??
-- 
mit freundlichem Gruss -- regards
        ,-,
       |  |         Thomas Drillich 
    ___|__|            
   (___,   )           uniserve Internet & Multimedia GmbH
   (___,   )\          Sophienweg 3
   (___,   ) \         Technologiezentrum (MIT)
    (___,_,)/ \        D-59872 Meschede Germany
        \              fon: +49 291 59100 , fax: +49 291 59102

Re: [GENERAL] no primary key on self designed type

От:
"Gene Selkov Jr." <selkovjr@selkovjr.xnet.com>
Дата:
> Hello,
> 
> create type inrecord (
> 	internallength=VARIABLE,
> 	input=inr_in,
> 	output=inr_out
> );
> 
> create table test (
> 	data	inrecord not null primary key
> );
> ... result ...
> ERROR:  Can't find a default operator class for type 268128.
> 
> how can I define the default operator class ??
> -- 

The short answer is,

INSERT INTO pg_opclass (opcname, opcdeftype)
   SELECT 'inrecord_ops', oid 
   FROM pg_type 
   WHERE typname = 'inrecord';

But you won't get away with just that. You probably want a non-empty
opclass. For example, if your type, inrecord, needs a btree opclass,
you'll want to do:


SELECT o.oid AS opoid, o.oprname
   INTO TABLE inrecord_ops_tmp
   FROM pg_operator o, pg_type t
   WHERE o.oprleft = t.oid and o.oprright = t.oid
      and t.typname = 'inrecord';

INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy, 
                     amopselect, amopnpages)
   SELECT am.oid, opcl.oid, c.opoid, 1,
        'btreesel'::regproc, 'btreenpage'::regproc
   FROM pg_am am, pg_opclass opcl, inrecord_ops_tmp c
   WHERE amname = 'btree' and opcname = 'inrecord_ops' 
      and c.oprname = '<';

INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy, 
                     amopselect, amopnpages)
   SELECT am.oid, opcl.oid, c.opoid, 2,
        'btreesel'::regproc, 'btreenpage'::regproc
   FROM pg_am am, pg_opclass opcl, inrecord_ops_tmp c
   WHERE amname = 'btree' and opcname = 'inrecord_ops' 
      and c.oprname = '<=';

INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy, 
                     amopselect, amopnpages)
   SELECT am.oid, opcl.oid, c.opoid, 3,
        'btreesel'::regproc, 'btreenpage'::regproc
   FROM pg_am am, pg_opclass opcl, inrecord_ops_tmp c
   WHERE amname = 'btree' and opcname = 'inrecord_ops' 
      and c.oprname = '=';

INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy, 
                     amopselect, amopnpages)
   SELECT am.oid, opcl.oid, c.opoid, 4,
        'btreesel'::regproc, 'btreenpage'::regproc
   FROM pg_am am, pg_opclass opcl, inrecord_ops_tmp c
   WHERE amname = 'btree' and opcname = 'inrecord_ops' 
      and c.oprname = '>=';

INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy, 
                     amopselect, amopnpages)
   SELECT am.oid, opcl.oid, c.opoid, 5,
        'btreesel'::regproc, 'btreenpage'::regproc
   FROM pg_am am, pg_opclass opcl, inrecord_ops_tmp c
   WHERE amname = 'btree' and opcname = 'inrecord_ops' 
      and c.oprname = '>';

DROP table inrecord_ops_tmp;


Which isn't all yet. The code above assumes that you have defined the
operators, '=', '>=', etc.:

CREATE OPERATOR = (
    leftarg = inrecord, rightarg = inrecord_code, procedure = inrecord_eq,
   restrict = eqsel, join = eqjoinsel
);

If that didn't make you sick already, you also need to define the
procedures, such as inrecord_eq in this example, and possibly write
some c code for them:

CREATE FUNCTION inrecord_eq(inrecord, inrecord) RETURNS bool
AS '${LIBDIR}/inrecord.so' LANGUAGE 'c';

INSERT INTO pg_description (objoid, description)
   SELECT oid, 'equals'::text
   FROM pg_proc
   WHERE proname = 'inrecord_eq'::name;

Thar's, in short, what is required to build a completely new type. One
might as well attempt to borrow some code or the whole opclass from
existing similar types, but I would hesitate to even consider doing that
without the thorough knowledge of the current postgres schema, which
is unfortunately not yet covered by the contemporary docs.


--Gene

pg_typemgr ;-) [was Re: [GENERAL] no primary key on self designed type]

От:
Thomas Drillich <drillich@uniserve.de>
Дата:

NAME pg_typemgr [Options] <dbname> <type> [|<type>[,<prefix>]]


Description

pg_typemgr trys to manage self designed types for postgreSQL.


Options

-h <host> | --host=<host>

set the hostname where the postmaster runs, default `localhost'.

-u <user> | --user=<user>

connect to database as <user>.

--pass=<password>

use <password> on connect.

-d | --delete

delete <type>, you have to backup the tables before.

-f <file> | --file=<file>

sql command file to redefine <type>.

-l <what> | --l<what>

list one of

proc

the procedures depend on type in pg_proc.

oper

the operators of type in pg_operators.

opclass

the defined opclasses in pg_opclass.

amop

the definitions in pg_amop.

amproc

the definitions in pg_amproc.

table

tables and indezies where <type> occours.

-p <prefix> | --prefix=<prefix>

prefix of functions which should include in the query, eg. <prefix>_cmp>, on default <prefix> == <type>

--lt=<ltfunction>

the function for the operator `<' default bool _lt(<type> *,<type> *)>.

--le=<lefunction>

the function for the operator `<='

--eq=<eqfunction>

the function for the operator `='

--ge=<gefunction>

the function for the operator `>='

--gt=<gtfunction>

the function for the operator `>'

--ne=<nefunction>

the function for the operator `!='

--cmp=<cmpfunction>

function which returns (-1,0,1) on (a<b,a==b,a>b) the compare function, default

int4 <prefix>_cmp(<type> *,<type> *)


Placeholders

<dbname>

the name of the database where pg_typemgr operates on.

<type>

the name of the data type.

<prefix>

the prefix of functions which should include in the query, default <type>.

<host>

the hostname of the postgreSQL server.


Bugs

this is the alpha version, need testing ;-)


Todo

correct update pg_amproc on insert of <type>.


See Also

psql
http://www.postgreSQL.org
http://www.uniserve.de my homepage


Copyright

(c) 1999-2002 Thomas Drillich <drillich@uniserve.de>


Disclaimer

Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.

IN NO EVENT SHALL THE AUTHOR OR HIS COMPANY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE THE AUTHOR OR HIS COMPANY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN ``AS IS'' BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

Re: pg_typemgr ;-) [was Re: [GENERAL] no primary key on self designed type]

От:
Bruce Momjian <pgman@candle.pha.pa.us>
Дата:
> Hi Gene,
> 
> is this correct
> 
> pg_typemgr.html	# description of my perl-program
> 
> pg_typemgr -p inr -d test inrecord # delete type
> pg_typemgr -p inr -f in_record.sql test inrecord # delete and restore
> pg_typemgr -p inr -l all test inrecord > inrecord.out # list anything
> pg_typemgr -p inr -l all test inet > inet.out  # to compare
> 
> When I have to setup pg_amproc like in inet.out ?
> Whats about inet | = | hashsel  | hashnpage  | hash  ..

I have just fixed cidr indexing in pg_amproc.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
FAQ