Thread: libpq - description of columns

libpq - description of columns

From
Christian Gregoire
Date:
Hello,

I'm having troubles to get the description of columns,
using the APIs listed in the PostgreSQL doc.

Using '\d TABLENAME', psql gives me character(15)
for a column, when libpq API PQfsize returns -1 for
the same column, whereas -1 should only indicate
fields with variable size (like TEXT i guess). Any
idea ?

I then looked at psql source code and it uses system
tables to get descriptions of tables. Fine, but in my
application, i have to get description of cursors,
that is description of something like
SELECT col1,col2,...,colN FROM
table1,table2,...,tableN WHERE ....
which are not known at compile time. Do i have to
parse it at runtime to dynamically build the select
command to request the system tables ?

Thanks for any hints.

Christian




=====


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com


Re: libpq - description of columns

From
Tom Lane
Date:
Christian Gregoire <cgregoir99@yahoo.com> writes:
> Using '\d TABLENAME', psql gives me
>   character(15)
> for a column, when libpq API PQfsize returns -1 for
> the same column, whereas -1 should only indicate
> fields with variable size (like TEXT i guess). Any
> idea ?

Yes, char(N) is considered a variable-length type.  (Think about
multibyte encodings to see why this must be so.)  psql is looking
at the typmod (PQfmod) to find out what the char length limit is.

> I then looked at psql source code and it uses system
> tables to get descriptions of tables. Fine, but in my
> application, i have to get description of cursors,
> that is description of something like
> SELECT col1,col2,...,colN FROM
> table1,table2,...,tableN WHERE ....
> which are not known at compile time.

The cleanest solution would be to do
select format_type(typeOID, typmod)

where you can get the type OID and typmod of each column from libpq.
If that seems too slow, you'll need to cache results on the application
side and/or hard-wire some of what format_type knows into your
application.
        regards, tom lane


Re: libpq - description of columns

From
Ron Johnson
Date:
On Thu, 2002-05-23 at 09:26, Tom Lane wrote:
> Christian Gregoire <cgregoir99@yahoo.com> writes:
> > Using '\d TABLENAME', psql gives me
> >   character(15)
> > for a column, when libpq API PQfsize returns -1 for
> > the same column, whereas -1 should only indicate
> > fields with variable size (like TEXT i guess). Any
> > idea ?
> 
> Yes, char(N) is considered a variable-length type.  (Think about
> multibyte encodings to see why this must be so.)  psql is looking

So it is only to the "user" that pg says "char() is fixed-length"?

-- 
+---------------------------------------------------------+
| Ron Johnson, Jr.        Home: ron.l.johnson@cox.net     |
| Jefferson, LA  USA      http://ronandheather.dhs.org:81 |
|                                                         |
| "I have created a government of whirled peas..."        |
|   Maharishi Mahesh Yogi, 12-May-2002,                   |
!   CNN, Larry King Live                                  |
+---------------------------------------------------------+