Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 664ABB5DC42 for ; Fri, 9 Sep 2011 10:57:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 85792-03 for ; Fri, 9 Sep 2011 13:57:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 32AFDB5DC4B for ; Fri, 9 Sep 2011 10:57:16 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p89DvALU010513; Fri, 9 Sep 2011 09:57:10 -0400 (EDT) To: Raghavendra cc: shuaixf , pgsql-admin@postgresql.org Subject: Re: how can I get the length of columns of a table by system tables/views In-reply-to: References: <1315408840339-4778911.post@n5.nabble.com> Comments: In-reply-to Raghavendra message dated "Fri, 09 Sep 2011 13:11:06 +0530" Date: Fri, 09 Sep 2011 09:57:10 -0400 Message-ID: <10512.1315576630@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/49 X-Sequence-Number: 38286 Raghavendra writes: > On Wed, Sep 7, 2011 at 8:50 PM, shuaixf 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