Re: Help with displaying data types.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Rob Burne" <robburne(at)hotmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with displaying data types.
Date: 2000-11-23 16:22:44
Message-ID: 7117.974996564@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Rob Burne" <robburne(at)hotmail(dot)com> writes:
> I am trying to perform a query that will:
> 1. Return all user defined attributes from a relation.
> 2. Also return the datatypes of each relation.

> So far I can only achieve part 1 with the following:

> select attname from pg_class,pg_attribute
> where relname=relation_name and
> attrelid = pg_class.oid and
> atttypid != 26 and
> atttypid != 27 and
> atttypid != 28 and
> atttypid != 29;

That's a bad way to do it, because your query will drop user-defined
columns of type OID (among others). I'd suggest testing for attnum > 0
to get rid of system columns.

> But what do I need to add to return the datatype of each attribute. Any
> suggestions please?

Join against pg_type. For example,

select attname, typname
from pg_class c, pg_attribute a, pg_type t
where relname = relation_name and
attrelid = c.oid and
atttypid = t.oid and
attnum > 0
order by attnum;

psql does a lot of these sorts of queries for its \d features.
Try starting psql with -E so you can see what queries it issues
when you do a \d ...

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message bmccoy 2000-11-23 16:40:52 Re: Create tables in one query
Previous Message Joel Mc Graw 2000-11-23 16:12:45 Re: