Thread: Column Length

  1. No Profile Picture
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    London, England
    Rep Power

    Question Column Length


    Does anyone know where in the system tables I can find the length of a particular column?

    I've had a look in the pg_attribute table, but it doesn't appear to show me the max length of my VarChar fields - the length appears as -1 (variable).

    Also, in which table is an attribute linked to a table? My hunch is that a lot of system table data is hidden from view but if anyone knows how I might get as the things I've mentioned, I'd be very grateful.

    Thanks in advance, NICK
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Rep Power
    For variable length fields, you need to check the atttypmod field. Postgres will add four more bytes (presumably to hold the actual size of the data?). For example, if a column is varchar(25), its value in atttypmod will be 29. By the way, in case your interested, the PHP function pg_fieldsize will only ever return '-1' for varchar fields, which I think is pretty weak. Fortunately it's a simple fix, just patch <php_4.1_source>/ext/pgsql/pgsql.c with this:
    >                       if ( return_value->value.lval == -1 )
    >                               return_value->value.lval = PQfmod( pgsql_result, Z_LVAL_PP(field) );
    Sorry, just noticed the other part of your post: check out the online manual at (I think there's even a mirror in Britain.)
    Last edited by billyo; December 24th, 2001 at 04:30 AM.

IMN logo majestic logo threadwatch logo seochat tools logo