Re: how can I get the length of columns of a table by system tables/views

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Raghavendra <raghavendra(dot)rao(at)enterprisedb(dot)com>
Cc: shuaixf <shuaixf(at)gmail(dot)com>, pgsql-admin(at)postgresql(dot)org
Subject: Re: how can I get the length of columns of a table by system tables/views
Date: 2011-09-09 13:57:10
Message-ID: 10512.1315576630@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Raghavendra <raghavendra(dot)rao(at)enterprisedb(dot)com> writes:
> On Wed, Sep 7, 2011 at 8:50 PM, shuaixf <shuaixf(at)gmail(dot)com> wrote:
>> According to pg_class, pg_attribute, pg_type, I can get the tablename,
>> column name, column type
>> however, how to get the length of columns of a table by system
>> tables/views?

> select pg_column_size(name) from tb;

That would provide the physical size of individual values. I think what
the OP was after was how to get the declared length limit of a column.
That's encoded in pg_attribute.atttypmod. Rather than hard-wiring
knowledge of the way it's encoded, it's best to rely on format_type(),
which knows the rules:

regression=# \d varchar_tbl
Table "public.varchar_tbl"
Column | Type | Modifiers
--------+----------------------+-----------
f1 | character varying(4) |

regression=# select format_type(atttypid, atttypmod) from pg_attribute where attrelid = 'varchar_tbl'::regclass and attname = 'f1';
format_type
----------------------
character varying(4)
(1 row)

regards, tom lane

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message shuaixf 2011-09-10 05:45:26 Re: how can I get the length of columns of a table by system tables/views
Previous Message Kevin Grittner 2011-09-09 13:35:39 Re: corrupted rows in pg_attribute deleted