473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CHAR(N) storage requirement

Is it the 4+N (aka. same as VARCHAR(n)) or is it N? Sorry, it was 100%
not clear for me after reading the docs, though the docs imply the
first: "The storage requirement for data of these types is 4 bytes plus
the actual string, and in case of character plus the padding."

As a comparison, MySQL seems to do storage saving for fixed-length
character (it doesn't store the length of the string).

--
dave

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
4 2524
David Garamond wrote:
Is it the 4+N (aka. same as VARCHAR(n)) or is it N? Sorry, it was
100% not clear for me after reading the docs, though the docs imply
the first: "The storage requirement for data of these types is 4
bytes plus the actual string, and in case of character plus the
padding."


Storing varchar(n) takes 4 bytes plus as many bytes as are required to
store the actual string. This may be more or less then "n".

Storing char(n) takes 4 bytes plus as many bytes are are required to
store the actual string, plus n - length(value) bytes for padding
spaces. This is at least "n" bytes.
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2
Peter Eisentraut <pe*****@gmx.net> writes:
Storing char(n) takes 4 bytes plus as many bytes are are required to
store the actual string, plus n - length(value) bytes for padding
spaces. This is at least "n" bytes.


Peter omitted one critical point that I think David hasn't absorbed
yet: char(N) measures N in characters, not bytes. When using a
multibyte encoding, N characters may require more than N bytes.
Only in single-byte encodings can you make any simple statements
about the number of bytes occupied by char(N). This is why the docs
are a bit vague.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #3
Peter Eisentraut wrote:
Is it the 4+N (aka. same as VARCHAR(n)) or is it N? Sorry, it was
100% not clear for me after reading the docs, though the docs imply
the first: "The storage requirement for data of these types is 4
bytes plus the actual string, and in case of character plus the
padding."


Storing varchar(n) takes 4 bytes plus as many bytes as are required to
store the actual string. This may be more or less then "n".

Storing char(n) takes 4 bytes plus as many bytes are are required to
store the actual string, plus n - length(value) bytes for padding
spaces. This is at least "n" bytes.


I see. Then there is no real benefits at all in using CHAR(N) in
PostgreSQL, is that right? I wonder why there is no storage saving done
by PostgreSQL for CHAR compared to VARCHAR (since the string length of
CHAR field will be the same for all rows)... Or perhaps I can use array
or CREATE TYPE to achieve this (that is, minimizing storage to only "n"
bytes to store CHAR(n)). Assuming no multibyte/Unicode, only US-ASCII
values.

--
dave
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #4
David Garamond wrote:
Peter Eisentraut wrote:
Is it the 4+N (aka. same as VARCHAR(n)) or is it N? Sorry, it was
100% not clear for me after reading the docs, though the docs imply
the first: "The storage requirement for data of these types is 4
bytes plus the actual string, and in case of character plus the
padding."


Storing varchar(n) takes 4 bytes plus as many bytes as are required to
store the actual string. This may be more or less then "n".

Storing char(n) takes 4 bytes plus as many bytes are are required to
store the actual string, plus n - length(value) bytes for padding
spaces. This is at least "n" bytes.


I see. Then there is no real benefits at all in using CHAR(N) in
PostgreSQL, is that right? I wonder why there is no storage saving done
by PostgreSQL for CHAR compared to VARCHAR (since the string length of
CHAR field will be the same for all rows)... Or perhaps I can use array
or CREATE TYPE to achieve this (that is, minimizing storage to only "n"
bytes to store CHAR(n)). Assuming no multibyte/Unicode, only US-ASCII
values.


The reason we store the length on disk for CHAR() is that the routines
to handle variable-length data types are used by all variable-length
data types, not just CHAR(), and there isn't an easy way to pass that
information around. The only way we could do it would be to pull the
row from disk, add needed lengths so they can be passed around to the
inernal routines, then strip them when writing --- it didn't seem worth
it.
--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is...
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
43
by: TheDrunkenDead | last post by:
Hello, just wondering how I would assign a char array of four elements to the four bytes used in an int. As of right now my code is: cNameSize = (char)((void)NameSize); cFileSize =...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
24
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.