Re: Self referencing composite datatype

From: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>
To: Sameer Thakur <samthakur74(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Self referencing composite datatype
Date: 2013-08-08 02:11:33
Message-ID: CAL_0b1ujSes0=2bFvTnpu3nFkTNhmkn5QQo8tPuhPWYhEqEwzg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Aug 7, 2013 at 4:57 AM, Sameer Thakur <samthakur74(at)gmail(dot)com> wrote:
> I wanted to create a composite datatype to represent a Node. So it would
> have a few attributes and an array of type Node which is the children of
> this node.
> create type Node as (r integer, s integer, children Node []);
> But i get error type Node[] does not exist. I understand that Node is not
> defined hence the error.
> But how do i get around this problem?

I just wonder how are you going to use this kind of types?

In 9.3 you will be able to use foreign keys with arrays like it is describe here
http://blog.2ndquadrant.com/postgresql-9-3-development-array-element-foreign-keys/

eg.

create table node as (
id integer primary key,
r integer, s integer,
children integer[] element references node
);

so you could download 9.3rc2 and experimant with it.

Now (on <=9.2.x) you can create the table without FK

create table node as (
id integer primary key,
r integer, s integer,
children integer[]
);

and check integrity by triggers.

--
Kind regards,
Sergey Konoplev
PostgreSQL Consultant and DBA

http://www.linkedin.com/in/grayhemp
+1 (415) 867-9984, +7 (901) 903-0499, +7 (988) 888-1979
gray(dot)ru(at)gmail(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sergey Konoplev 2013-08-08 02:13:08 Re: Self referencing composite datatype
Previous Message Victor Hooi 2013-08-08 02:01:17 Performance of ORDER BY RANDOM to select random rows?