X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 43B7CD1B475 for ; Tue, 23 Dec 2003 21:43:16 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 48115-10 for ; Tue, 23 Dec 2003 17:42:29 -0400 (AST) Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [192.108.102.143]) by svr1.postgresql.org (Postfix) with ESMTP id B1463D1B483 for ; Tue, 23 Dec 2003 17:42:26 -0400 (AST) Received: from [10.0.1.3] glaesema@smtp-send.myrealbox.com [68.112.116.204] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 3.47 $ on Novell NetWare; Tue, 23 Dec 2003 14:42:27 -0700 Mime-Version: 1.0 (Apple Message framework v609) Content-Transfer-Encoding: quoted-printable Message-Id: Content-Type: text/plain; charset=WINDOWS-1252; format=flowed To: 'pgsql-sql@postgresql.org' From: Michael Glaesemann Subject: Distributed keys Date: Tue, 23 Dec 2003 15:42:24 -0600 X-Mailer: Apple Mail (2.609) X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/166 X-Sequence-Number: 16332 Hello all! An area of the relational database model that is not included in the=20 SQL standard are distributed keys and distributed foreign keys. A quick=20 example (Case 1): employees (id, name); schools (name, location); teachers (employee_id, school_name); subs (employee_id); managers (employee_id, school_name); with constraints unique employees(id) teachers(employee_id) references employees(id) teachers(school_name) references schools(name) subs(employee_id) references employees(id) managers(employee_id) references employees(id) The idea is that employees must be in one (and only one) of either=20 teachers, subs, or managers. Currently, this might be represented in=20 something like (Case 2) employees (id, name, employee_type, school_name); schools (name, location); employee_types (type); with constraints employees(employee_type) references employee_types(type) employees(school_name) references schools(name) where employee_types includes "teacher", "sub", and "manager" Or it might be represented with a number of rules or triggers that=20 perform all of the necessary checking. employees(school_name) can't have a not null constraint because if the=20 employee is a sub, they aren't associated with a school. Using the terms "distributed key" and "foreign distributed key", in the=20 first case employee_id is a "distributed key" in that it must occur in=20 only one of the tables teachers, subs, or managers. Distributed keys=20 are similar in concept to primary keys=97they must be unique. This=20 guarantees an employee_id in teachers is not found in subs or managers,=20 an employee_id in subs is not found in managers or teachers, and an=20 employee_id in managers is not found in subs or teachers. employees(id) is a foreign distributed key in teachers, subs, and=20 managers (as employee_id). Foreign distributed keys are similar in=20 concept to foreign keys in that employees(id) must be referenced by a=20 single tuple in one of teachers, subs, or managers. Another use would be in this situation (something I'm working on right=20 now): I want to link comments by employees by employee_id, but comments=20 from non-employees by name (as they don't have an id). comments(id, comment); comments_nonemployees(comment_id, name); comments_employees(comment_id, employee_id); with constraints comments_nonemployees(comment_id) references comments(id) comments_employees(comment_id) references comments(id) and comments(id) must be listed in either=20 comments_nonemployees(comment_id) or comments_employees(comment_id) I haven't looked very far into how to implement distributed keys and=20 foreign distributed keys in PostgreSQL beyond briefly looking at the=20 pg_constraint system table, thinking a distributed key would be=20 something making employee_id unique in teachers(employee_id) UNION=20 subs(employee_id) UNION managers(employee_id). A distributed key is=20 distributed over a number of tables, rather than a single one, so=20 there'd have to be a list of relid-attnum pairs, rather than a single=20 relid-attnum pair, such as conrelid and conkey in pg_constraint. Here's=20 a brief sketch of the idea: pg_distributed distname name the name of the distributed key constraint distrelid oid the relid of one of the tables involved in the=20 distributed keys distkey int2[] a list of the attnum of the columns of the table with=20 oid distrelid involved in the distributed key distforkey bool true if foreign distributed key distfrelid oid if a foreign distributed key, the relid of the the=20 referenced table, else 0 distfkey int2[] if a foreign distributed key, a list of the attnum of=20 the columns of the table with oid distfrelid referenced by the foreign=20 distributed key, else 0 In pg_distributed, distname, distfrelid, distfkey would be the same in=20 every tuple involved in the distributed key, while distrelid and=20 distkey would vary. Basically a one-to-many relation. These are just=20 some thoughts, and the first time I've looked at the PostgreSQL=20 internals, so I wouldn't be suprised if this approach is wrongheaded. I know PostgreSQL endeavors to adhere closely to the SQL standard, and=20 I think this is important as SQL *is* a standard. One of the things=20 that makes PostgreSQL great in my opinion is that it goes beyond the=20 standard in some areas where users and developers have found useful,=20 such as the PostgreSQL rule system. I think distributed key support=20 would extend the usefulness of PostgreSQL without hindering SQL=20 conformance. I'm interested in hearing what others have to say, especially along the=20 lines of implementation. I haven't seen much discussion of distributed=20 keys on the lists (other than Josh Berkus), so perhaps there isn't much=20 interest. Would there be any foreseeable opposition if I (and/or=20 others) worked on this? Regards, Michael Glaesemann grzm myrealbox com