Type of bare text strings

Started by Jim C. Nasbyover 19 years ago4 messages
#1Jim C. Nasby
jnasby@pervasive.com

What type are bare strings considered if they haven't been cast? I'm curious as
to how the first case is of size 5, and how the last case is 301...

decibel=# select pg_column_size('test');
5

decibel=# select pg_column_size('test'::varchar);
8

decibel=# select pg_column_size('test'::text);
8

decibel=# select pg_column_size('test'::name);
64

decibel=# select pg_column_size('test'::char(4));
8

decibel=# select pg_column_size('123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890');
301

decibel=#
--
Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461

#2Alvaro Herrera
alvherre@commandprompt.com
In reply to: Jim C. Nasby (#1)
Re: Type of bare text strings

Jim C. Nasby wrote:

What type are bare strings considered if they haven't been cast? I'm curious as
to how the first case is of size 5, and how the last case is 301...

decibel=# select pg_column_size('test');
5

"unknown". This seems to be a cstring (i.e. length 5 considering the
trailing \0)

decibel=# select pg_column_size('test'::varchar);
8
decibel=# select pg_column_size('test'::text);
8
decibel=# select pg_column_size('test'::char(4));
8

4 fixed varlena + 4 string length

decibel=# select pg_column_size('test'::name);
64

name is fixed 64 bytes (not varlena)

decibel=# select pg_column_size('123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890');
301

Same as the first case. (There are actual 300 chars here according to
my count, is that right?)

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

#3Alvaro Herrera
alvherre@commandprompt.com
In reply to: Alvaro Herrera (#2)
Re: Type of bare text strings

Alvaro Herrera wrote:

Jim C. Nasby wrote:

What type are bare strings considered if they haven't been cast? I'm curious as
to how the first case is of size 5, and how the last case is 301...

decibel=# select pg_column_size('test');
5

"unknown". This seems to be a cstring (i.e. length 5 considering the
trailing \0)

Yup. This is handled by this code:

else if (typlen == -2)
{
/* cstring */
result = strlen(DatumGetCString(value)) + 1;
}

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#2)
Re: Type of bare text strings

Alvaro Herrera <alvherre@commandprompt.com> writes:

"unknown". This seems to be a cstring (i.e. length 5 considering the
trailing \0)

Yeah. "unknown" used to have the same representation as "text", ie
varlena, but I changed it recently because I realized that the normal
thing we do with an "unknown" literal is feed it to some datatype's
input converter. Making it the same as cstring saves a conversion step.

regards, tom lane