pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Rob Burne <robburne@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Help with displaying data types.
Date: Thu, 23 Nov 2000 11:22:44 -0500
Message-ID: <7117.974996564@sss.pgh.pa.us> (raw)
In-Reply-To: <F76OeIiGXix4A2DreBB0000154f@hotmail.com>
References: <F76OeIiGXix4A2DreBB0000154f@hotmail.com>

"Rob Burne" <robburne@hotmail.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



view thread (2+ messages)

Message-ID: <7117.974996564@sss.pgh.pa.us>
Permalink:  ../7117.974996564@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/7117.974996564@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, robburne@hotmail.com
  Subject: Re: Help with displaying data types.
  In-Reply-To: <7117.974996564@sss.pgh.pa.us>
  References: <F76OeIiGXix4A2DreBB0000154f@hotmail.com> <7117.974996564@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox