Can psql show the column type of a result ?

Started by "Nils O. Selåsdal"over 15 years ago4 messagesgeneral
Jump to latest
#1"Nils O. Selåsdal"
noselasd@asgaard.homelinux.org

Hello,
I'm wondering if there's a way to show the column type of a result with
psql, e.g.

select sum(r) from mytable;

r
-------
101.0

I'd like to see the type of the 'r' column.

#2Richard Broersma
richard.broersma@gmail.com
In reply to: "Nils O. Selåsdal" (#1)
Re: Can psql show the column type of a result ?

On Wed, Sep 22, 2010 at 12:52 PM, "Nils O. Selåsdal"
<noselasd@asgaard.homelinux.org> wrote:

 Hello,
I'm wondering if there's a way to show the column type of a result with
psql, e.g.

select sum(r) from mytable;

  r
-------
 101.0

I'd like to see the type of the 'r' column.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

From psql you can use:

\d mytable

sql to show this might look like:

SELECT a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod),
(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
a.attnotnull, a.attnum
FROM pg_catalog.pg_attribute AS a
INNER JOIN pg_catalog.pg_class c
ON C.oid = a.attrelid
WHERE c.relname ~ '^(mytable)$' AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;

--
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug

#3"Nils O. Selåsdal"
noselasd@asgaard.homelinux.org
In reply to: Richard Broersma (#2)
Re: Can psql show the column type of a result ?

On 22.09.2010 23:26, Richard Broersma wrote:

On Wed, Sep 22, 2010 at 12:52 PM, "Nils O. Sel�sdal"
<noselasd@asgaard.homelinux.org> wrote:

Hello,
I'm wondering if there's a way to show the column type of a result with
psql, e.g.

select sum(r) from mytable;

r
-------
101.0

I'd like to see the type of the 'r' column.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

From psql you can use:

\d mytable

sql to show this might look like:

SELECT a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod),
(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
a.attnotnull, a.attnum
FROM pg_catalog.pg_attribute AS a
INNER JOIN pg_catalog.pg_class c
ON C.oid = a.attrelid
WHERE c.relname ~ '^(mytable)$' AND a.attnum> 0 AND NOT a.attisdropped
ORDER BY a.attnum;

This is ok for showing tables, but insufficient for showing the type of
a result set, which might
differ from the table being queried ,depending on what
operators/constants/functions a query uses to pull out data
- which was my current problem.

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: "Nils O. Selåsdal" (#1)
Re: Can psql show the column type of a result ?

=?UTF-8?B?Ik5pbHMgTy4gU2Vsw6VzZGFsIg==?= <noselasd@asgaard.homelinux.org> writes:

I'm wondering if there's a way to show the column type of a result with
psql, e.g.

select sum(r) from mytable;

r
-------
101.0

I'd like to see the type of the 'r' column.

There's nothing built-in to psql, but you could modify the query, eg

select pg_typeof(sum(r)) from mytable;

(Adding "LIMIT 1" would be wise in most cases, though you don't need it
here.)

regards, tom lane