The following table shows the general-purpose character types available in Postgres Plus Advanced Server.
Table 3-3 Character Types
Name |
Description |
CHAR [ (n) ] |
Fixed-length, blank-padded |
CLOB |
Large variable-length up to 1 GB |
VARCHAR2(n) |
Variable-length with limit |
The two primary character types are CHAR(n) and VARCHAR2(n), where n is a positive integer. Both of these types can store strings up to n characters in length. In the case of type CHAR, n defaults to 1 if omitted. An attempt to store a longer string into a column of these types will result in an error, unless the excess characters are all spaces, in which case the string will be truncated to the maximum length. If the string to be stored is shorter than the declared length, values of type CHAR will be space-padded; values of type VARCHAR2 will simply store the shorter string.
If one explicitly casts a value to VARCHAR2(n) or CHAR(n), then an over-length value will be truncated to n characters without raising an error. (This too is required by the SQL standard.)
Values of type CHAR are physically padded with spaces to the specified width n, and are stored and displayed that way. However, the padding spaces are treated as semantically insignificant. Trailing spaces are disregarded when comparing two values of type CHAR, and they will be removed when converting a CHAR value to one of the other string types. Note that trailing spaces are semantically significant in VARCHAR2 values.
A third character type used for storing large character strings is the CLOB data type. CLOB is semantically equivalent to VARCHAR2 except no length limit is specified. Generally, use CLOB over VARCHAR2 if the maximum string length is not known.
The longest possible character string that can be stored in a CLOB type is about 1 GB.
The storage requirement for data of these three types is the actual string plus 1 byte if the string is less than 127 bytes, or 4 bytes if the string is 127 bytes or greater. In the case of CHAR, the padding also requires storage. Long strings are compressed by the system automatically, so the physical requirement on disk may be less. Long values are also stored in background tables so they do not interfere with rapid access to the shorter column values.
The database character set determines the character set used to store textual values.