Thread: Need some help: attlen is pg_attributes gives a negative value.. .

Need some help: attlen is pg_attributes gives a negative value.. .

From
"Klein, Robert"
Date:
I have a table defined as:

create table contacts (
   c_name    char (20),
   c_title    char (30),
   c_phone    char (13),
   c_ext    char (5),
   c_email    char (60),
   c_sort    char (5)
);

when I query the catalog to get the field length for auto generating HTML
forms I get:

attnum|attname|typname|attlen
------+-------+-------+------
     1|c_name |bpchar |    -1
     2|c_title|bpchar |    -1
     3|c_phone|bpchar |    -1
     4|c_ext  |bpchar |    -1
     5|c_email|bpchar |    -1
     6|c_sort |bpchar |    -1
(6 rows)


I know in previous versions the length as defined in the create table
statement was given.  Any ideas?

Rob
"Klein, Robert" <rvklein@ober.com> writes:
> [ attlen for a char(n) field is -1 ]

> I know in previous versions the length as defined in the create table
> statement was given.  Any ideas?

Must have been quite a few versions back; attlen has been -1 for
variable-length datatypes for as long as I've been paying attention.
(Of course char(n) isn't *really* variable length, but it's treated
that way so that the representation is the same as for varchar(n) and
text.)

atttypmod is what you want for determining the length of char(n) fields.
I believe it's n+4 for a char(n) field.

            regards, tom lane

Re: Need some help: attlen is pg_attributes gives a negative value.. .

From
Roland Roberts
Date:
>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:

    Tom> "Klein, Robert" <rvklein@ober.com> writes:
    >> [ attlen for a char(n) field is -1 ]

    >> I know in previous versions the length as defined in the create
    >> table statement was given.  Any ideas?

    Tom> Must have been quite a few versions back; attlen has been -1
    Tom> for variable-length datatypes for as long as I've been paying
    Tom> attention. (Of course char(n) isn't *really* variable length,
    Tom> but it's treated that way so that the representation is the
    Tom> same as for varchar(n) and text.)

When did this change?  I haven't looked 6.5, but I thought 6.3 still
has bpchar as fixed length and char(n) was blank padded whereas
varchar was not.

roland
--
               PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                    Unix Software Solutions
roberts@panix.com                      76-15 113th Street, Apt 3B
rbroberts@acm.org                          Forest Hills, NY 11375
Roland Roberts <roberts@panix.com> writes:
>>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
Tom> Must have been quite a few versions back; attlen has been -1
Tom> for variable-length datatypes for as long as I've been paying
Tom> attention. (Of course char(n) isn't *really* variable length,
Tom> but it's treated that way so that the representation is the
Tom> same as for varchar(n) and text.)

> When did this change?  I haven't looked 6.5, but I thought 6.3 still
> has bpchar as fixed length and char(n) was blank padded whereas
> varchar was not.

char(n) was and is blank-padded, varchar(n) was and is not.  Sorry
if I was imprecise enough to convey a different impression.

I'm fairly sure that attlen had its present semantics in 6.4.  6.3
was before my time; any old-timers care to comment?

            regards, tom lane

Re: Need some help: attlen is pg_attributes gives a negative value.. .

From
Bruce Momjian
Date:
> Roland Roberts <roberts@panix.com> writes:
> >>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
> Tom> Must have been quite a few versions back; attlen has been -1
> Tom> for variable-length datatypes for as long as I've been paying
> Tom> attention. (Of course char(n) isn't *really* variable length,
> Tom> but it's treated that way so that the representation is the
> Tom> same as for varchar(n) and text.)
>
> > When did this change?  I haven't looked 6.5, but I thought 6.3 still
> > has bpchar as fixed length and char(n) was blank padded whereas
> > varchar was not.
>
> char(n) was and is blank-padded, varchar(n) was and is not.  Sorry
> if I was imprecise enough to convey a different impression.
>
> I'm fairly sure that attlen had its present semantics in 6.4.  6.3
> was before my time; any old-timers care to comment?

Since day 1, they were that way.  atttypmod was added in 6.3.

--
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: Need some help: attlen is pg_attributes gives a negative value.. .

From
Roland Roberts
Date:
-----BEGIN PGP SIGNED MESSAGE-----

>>>>> "Bruce" == Bruce Momjian <pgman@candle.pha.pa.us> writes:

    Bruce> Since day 1, they were that way.  atttypmod was added in
    Bruce> 6.3.

Hmmm, I'm clearly having a memory problem.  I checked my old queries
and for some reason thought they had to handle bpchar different from
varchar when getting field sizes, but I see they don't.

roland
- --
               PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                    Unix Software Solutions
roberts@panix.com                      76-15 113th Street, Apt 3B
rbroberts@acm.org                          Forest Hills, NY 11375

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3a
Charset: noconv
Comment: Processed by Mailcrypt 3.5.4, an Emacs/PGP interface

iQCVAwUBORJQdeoW38lmvDvNAQFqdwP/YBCJoPJ76nYhQhgA4CVogiBgfUqS485A
uyENZsGpKad0fFPpwSx4RJGnXOB2aL7p1XCyqEiOvGvELQLXR6LDyKct5pehxSfi
mAGX9BDuWk87JibTvy2nEYPMU84RDUYr1ZXC9omxaiC7tOBeXNhCiW4ZXFl6R51d
rNi6EI+0XeE=
=tcV6
-----END PGP SIGNATURE-----