Re: Getting the column to a which a sequence belongs.

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Getting the column to a which a sequence belongs.
Date: 2009-08-27 19:51:42
Message-ID: 20090827195142.GD5407@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Aug 27, 2009 at 09:18:57PM +0200, Thomas Kellerer wrote:
> I'm trying to extend the Postgres support in my SQL tool. I'm trying to
> recreate the SQL for a sequence, and I wonder if there is a way to find
> out the column to which a sequence "belongs".

The information is all in the system catalogs; I've not had much
opportunity to fiddle with them so far but the following may be a start
to help get things out for you.

SELECT c.relname, a.attname, t.relname
FROM pg_class c, pg_depend d, pg_class t, pg_attribute a
WHERE c.relkind = 'S'
AND d.objid = c.oid
AND d.refobjid = t.oid
AND (d.refobjid,d.refobjsubid) = (a.attrelid,a.attnum);

The first reference to "pg_class" can probably be dropped as you can
convert the names of tables/sequences into their oid by using literals
of type "regclass". For example, to pull out all the column names from
table "foo", you can do:

SELECT attname
FROM pg_attribute
WHERE attrelid = 'foo'::regclass;

Have a look here for docs:

http://www.postgresql.org/docs/current/static/catalogs.html

--
Sam http://samason.me.uk/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2009-08-27 19:54:22 Re: Getting the column to a which a sequence belongs.
Previous Message Alvaro Herrera 2009-08-27 19:32:15 Re: [SQL] Data audit trail techniques in postgresql