Subject: Re: Postgres data types

From: [email-masked] (Daniel Turk)
Date: Mon, 23 Aug 1993 08:09:32 -0400 (EDT)

> > Hi!  I'm wanting to know how to determine Postgres class field types
> > from within a C program.  I am able to find out class names and all the
> > field names within a given class.  But I'm not sure how to find out what
> > data type each field is specified as.  Can this be done easily?  How?
> > 
> > --------------------------------------------------------------------------
> > Dan Turk     Computer Science & Information Systems     Andrews University
> > Berrien Springs, MI          [email-masked]               (616) 471-3426
> > ==========================================================================
> > 
> 
> 
> Hi Dan,
> 
> i think that should be possible by browsing through the POSTGRES catalogs
> pg_relation and pg_attribute. If you read all tuples from pg_attribute
> where attrelid equals the oid of your class then the attributes attname 
> and atttypid should tell you what you want.
> That's just how i would try to solve your problem. I don't know if it
> works but i can't see why not.
> 
> Bye Wolf

Yes, that's what I'd think too.  Except that the atttypid is just an
integer and I don't know how it maps to logical data types.....  I would
think that this information would be part of Postgres and that I could
access it internally.....

--------------------------------------------------------------------------
Dan Turk     Computer Science & Information Systems     Andrews University
Berrien Springs, MI          [email-masked]               (616) 471-3426
==========================================================================

---

From: [email-masked] (Paul M. Aoki)
Date: Mon, 23 Aug 93 11:34:23 -0700

> access it internally.....

stick this in your monitor:

retrieve (c.relname, a.attname, a.attnum, t.typname) 
	from c in pg_class, a in pg_attribute, t in pg_type
	where t.oid = a.atttypid	/* this is the key mapping */
	and a.attrelid = c.oid
	and a.attnum > 0		/* just print user attributes */
	and c.relname !~ "pg_"		/* don't tell me about catalogs */
	and c.relkind = "r"::char	/* don't tell me about indexes */
	sort by relname, attnum\g

stir well.  add or subtract predicates to taste. :-)
--
  Paul M. Aoki  |  CS Div., Dept. of EECS, UCB  |  [email-masked]
                |  Berkeley, CA 94720           |  ...!uunet!ucbvax!aoki