Updated on 2024-05-21 GMT+08:00

Character Types

Table 1 lists the character data types supported by GaussDB. For string operators and related built-in functions, see Character Processing Functions and Operators.

Table 1 Character types

Name

Description

Storage Space

CHAR(n)

CHARACTER(n)

NCHAR(n)

Fixed-length character string, blank padded. n indicates the string length. If it is not specified, the default precision 1 is used.

The maximum value of n is 10485760 (10 MB).

VARCHAR(n)

CHARACTER VARYING(n)

Variable-length string. In PostgreSQL-compatible mode, n indicates the string length. In other compatibility modes, n indicates the byte length.

The maximum value of n is 10485760 (10 MB).

If n is not specified, the maximum storage length is 1 GB – 85 bytes – Length of the first n columns. For example, the maximum length of (a int, b varchar) is 1,073,741,735 bytes (= 1 GB – 85 bytes – 4 bytes).

VARCHAR2(n)

Variable-length string. It is the alias of the VARCHAR(n) type. n indicates the string length.

The maximum value of n is 10485760 (10 MB).

If n is not specified, the maximum storage length is 1 GB – 85 bytes – Length of the first n columns. For example, the maximum length of (a int, b varchar) is 1,073,741,735 bytes (= 1 GB – 85 bytes – 4 bytes).

NVARCHAR2(n)

Variable-length string. n indicates the string length.

The maximum value of n is 10485760 (10 MB).

If n is not specified, the maximum storage length is 1 GB – 85 bytes – Length of the first n columns. For example, the maximum length of (a int, b varchar) is 1,073,741,735 bytes (= 1 GB – 85 bytes – 4 bytes).

CLOB

Big text object.

For Astore, the maximum size is 32 TB minus 1 byte. However, the size of the column description header and the size of the tuple (less than 32 TB minus 1 byte) where the column is located must also be considered. Therefore, the maximum size of the CLOB type may be less than 32 TB minus 1 byte.

For Ustore, the maximum size is 1 GB minus 1 byte. However, the size of the column description header and the size of the tuple (less than 1 GB minus 1 byte) where the column is located must also be considered. Therefore, the maximum size of the CLOB type may be less than 1 GB minus 1 byte.

TEXT

Variable-length string.

The maximum size is 1 GB minus 1 byte. However, the size of the column description header and the size of the tuple (less than 1 GB minus 1 byte) where the column is located must also be considered. Therefore, the maximum size of the TEXT type may be less than 1 GB minus 1 byte.

  1. In addition to the restriction on the size of each column, the total size of each tuple cannot exceed 1 GB minus 85 bytes and is affected by the control header information of the column, the control header information of the tuple, and whether null fields exist in the tuple.
  2. NCHAR is the alias of the bpchar type, and VARCHAR2(n) is the alias of the VARCHAR(n) type.
  3. Only advanced package dbe_lob supports CLOBs whose size is greater than 1 GB. System functions do not support CLOBs whose size is greater than 1 GB.
  4. In ORA-compatible mode, the received empty string is converted to null by default.
  5. GaussDB supports a maximum of 1 GB data transfer, and the maximum size of the result string returned by the function is 1 GB.

GaussDB has two other fixed-length character types, as shown in Table 2. The name type exists only for the storage of identifiers in the internal system catalogs and is not intended for use by general users. Its length is currently defined as 64 bytes (63 usable characters plus terminator). The type "char" only uses one byte of storage. It is internally used in the system catalogs as a simplistic enumeration type.

Table 2 Special character types

Name

Description

Storage Space

name

Internal type for object names

64 bytes

"char"

Single-byte internal type

1 byte

Examples

-- Create a table.
gaussdb=# CREATE TABLE char_type_t1 
(
    CT_COL1 CHARACTER(4)
)DISTRIBUTE BY HASH (CT_COL1);

-- Insert data.
gaussdb=# INSERT INTO char_type_t1 VALUES ('ok');

-- Query data in the table.
gaussdb=# SELECT ct_col1, char_length(ct_col1) FROM char_type_t1;
 ct_col1 | char_length 
---------+-------------
 ok      |           4
(1 row)

-- Delete the table.
gaussdb=# DROP TABLE char_type_t1;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- Create a table.
gaussdb=# CREATE TABLE char_type_t2  
(
    CT_COL1 VARCHAR(5)
)DISTRIBUTE BY HASH (CT_COL1);

-- Insert data.
gaussdb=# INSERT INTO char_type_t2 VALUES ('ok');

gaussdb=# INSERT INTO char_type_t2 VALUES ('good');

-- Specify the type length. An error is reported if an inserted string exceeds this length.
gaussdb=# INSERT INTO char_type_t2 VALUES ('too long');
ERROR:  value too long for type character varying(5)
CONTEXT:  referenced column: ct_col1

-- Specify the type length. A string exceeding this length is truncated.
gaussdb=# INSERT INTO char_type_t2 VALUES ('too long'::varchar(5));

-- Query data.
gaussdb=# SELECT ct_col1, char_length(ct_col1) FROM char_type_t2;
 ct_col1 | char_length 
---------+-------------
 ok      |           2
 good    |           4
 too l   |           5
(3 rows)

-- Delete data.
gaussdb=# DROP TABLE char_type_t2;