Обсуждение: column length?

Поиск
Список
Период
Сортировка

column length?

От
"Mark McArthey"
Дата:
Hi all,
I am wondering how the column length is determined.  The attached output is
for my test table.  Running '\d player' shows me that the columns are
varchar(20), but the attlen is listed as -1.  Where does the value '20' come
from?
Thanks!
Mark

> psql -E

=# \d player
********* QUERY *********
SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules
FROM pg_class WHERE relname='player'
*************************

********* QUERY *********
SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
a.atthasdef, a
.attnum
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relname = 'player'
AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
ORDER BY a.attnum
*************************

Table "player"
Attribute |    Type     | Modifier
-----------+-------------+----------
name      | varchar(20) |
password  | varchar(20) |


=# \i query1.sql
relhasindex | relkind | relchecks | reltriggers | relhasrules
-------------+---------+-----------+-------------+-------------
f           | r       |         0 |           0 | f
(1 row)

markm=# \i query2.sql
attname  | typname | attlen | atttypmod | attnotnull | atthasdef | attnum
----------+---------+--------+-----------+------------+-----------+--------
name     | varchar |     -1 |        24 | f          | f         |      1
password | varchar |     -1 |        24 | f          | f         |      2
(2 rows)



Re: column length?

От
Tom Lane
Дата:
"Mark McArthey" <mcarthey@execpc.com> writes:
> I am wondering how the column length is determined.  The attached output is
> for my test table.  Running '\d player' shows me that the columns are
> varchar(20), but the attlen is listed as -1.  Where does the value '20' come
> from?

attlen = -1 is a generic value indicating "variable length type".  All
columns of varchar type will have attlen = -1 (as will all columns of
any other variable-length datatype).  The gold is hidden in the
atttypmod column, which has a datatype-specific interpretation.
For varchar columns, atttypmod is interpreted as "max chars plus 4".
(The plus-4 is a historical wart.)  See
src/backend/utils/adt/format_type.c for info about the interpretation of
typmod for other datatypes.

            regards, tom lane