Thread: Field length ??

Field length ??

From
Louise Catherine
Date:
I've this table :<br /><br />create table test(]<br />satu char(10),<br />dua char(5)<br />);<br /><br />How could I
getthe Field length not the Data length ?<br /><br /><br />Thanks,<br />Rina<br /><p><hr size="1" />Love cheap thrills?
EnjoyPC-to-Phone <a
href="http://us.rd.yahoo.com/mail_us/taglines/postman9/*http://us.rd.yahoo.com/evt=39666/*http://beta.messenger.yahoo.com/">
callsto 30+ countries</a> for just 2¢/min with Yahoo! Messenger with Voice. 

Re: Field length ??

From
Louise Catherine
Date:
Hi ,<br /><br />With this table :<br />create table test(<br />satu char(10),<br />dua char(5)<br />);<br /><br />To
getthe field length, I use this query:<br /><br />select atttypmod from pg_attribute where <br />attname='satu' and <br
/>attrelid=(selectoid from pg_class where relname='test');<br />---> result 1 : 14<br /><br />select atttypmod from
pg_attributewhere <br />attname='dua' and <br />attrelid=(select oid from pg_class where relname='test');<br />--->
result2 : 9<br /><br /><br />Could anyone explain, why the field length must be add by 4 :<br />result 1 : 10 + 4
=14<br/>result 2 : 5 + 4 = 9<br /><br />Could I use this query ? Is it valid?<br />select atttypmod-4 from pg_attribute
where<br /> attname='satu' and <br /> attrelid=(select oid from pg_class where relname='test');<br /><br />Thank's,<br
/>Rina<br/><p><hr size="1" />How low will we go? Check out Yahoo! Messenger’s low <a
href="http://us.rd.yahoo.com/mail_us/taglines/postman8/*http://us.rd.yahoo.com/evt=39663/*http://voice.yahoo.com">
PC-to-Phonecall rates.</a> 

Re: Field length ??

From
Markus Schaber
Date:
Hi, Louise,

Louise Catherine wrote:

> Could anyone explain, why the field length must be add by 4 :
> result 1 : 10 + 4 =14
> result 2 : 5 + 4 = 9

I guess that it is because all variable length datatypes (and text types
are such) internally contain a 4 bytes length field.

http://www.postgresql.org/docs/8.1/interactive/xtypes.html might be a
start if you want to read more on this topic.

HTH,
Markus

-- 
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org


Re: Field length ??

From
Tom Lane
Date:
Markus Schaber <schabi@logix-tt.com> writes:
> Louise Catherine wrote:
>> Could anyone explain, why the field length must be add by 4 :

> I guess that it is because all variable length datatypes (and text types
> are such) internally contain a 4 bytes length field.

It's an ancient decision that no longer has much of anything to do with
reality, but changing it would break more code than it's worth to have
a slightly cleaner definition of atttypmod ...
        regards, tom lane


Re: Field length ??

From
Frank Bax
Date:
At 05:16 AM 4/20/06, Markus Schaber wrote:

>Hi, Louise,
>
>Louise Catherine wrote:
>
> > Could anyone explain, why the field length must be add by 4 :
> > result 1 : 10 + 4 =14
> > result 2 : 5 + 4 = 9
>
>I guess that it is because all variable length datatypes (and text types
>are such) internally contain a 4 bytes length field.


Except that the original fields were neither variable length datatypes, not 
type text.        create table test(]        satu char(10),        dua char(5)        );




Re: Field length ??

From
Markus Schaber
Date:
Hi, Frank,

Frank Bax wrote:

> Except that the original fields were neither variable length datatypes,
> not type text.
>         create table test(]
>         satu char(10),
>         dua char(5)
>         );

char is a textual type (in opposite to numeric types), and they are
handled as variable length datatypes internally (even if given a limit).

See http://www.postgresql.org/docs/8.1/static/datatype-character.html

HTH,
Markus
-- 
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org


Re: Field length ??

From
Tom Lane
Date:
Markus Schaber <schabi@logix-tt.com> writes:
> Frank Bax wrote:
>> Except that the original fields were neither variable length datatypes,
>> not type text.

> char is a textual type (in opposite to numeric types), and they are
> handled as variable length datatypes internally (even if given a limit).

char(n) *is* a variable-length type, not merely "handled as such",
because the limit N is measured in characters not bytes.  To support
variable-length encodings such as UTF8 we have to treat it as variable
length.
        regards, tom lane