postgresql 9.2中varchar(n)的最大长度是多少,哪个最好使用varchar(n)或text?

时间:2021-08-08 16:58:24

Hi I am using postgresql 9.2 and I want to use varchar(n) to store some long string but I don't know the maximum length of character which varchar(n) supports. and which one is better to use so could you please suggest me? thanks

您好我使用postgresql 9.2我想使用varchar(n)来存储一些长字符串,但我不知道varchar(n)支持的字符的最大长度。哪一个更好用,你能建议我吗?谢谢

2 个解决方案

#1


26  

tl;dr: 1 GB (each character (really: codepoint) may be represented by 1 or more bytes, depending on where they are on a unicode plane - assuming a UTF-8 encoded database). You should always use text datatype for arbitrary-length character data in Postgresql now.

tl; dr:1 GB(每个字符(实际上:代码点)可以由1个或更多字节表示,具体取决于它们在unicode平面上的位置 - 假设是UTF-8编码的数据库)。现在,您应该始终在Postgresql中使用text数据类型作为任意长度的字符数据。

Explanation: varchar(n) and text use the same backend storage type (varlena): a variable length byte array with a 32bit length counter. For indexing behavior text may even have some performance benefits. It is considered a best practice in Postgres to use text type for new development; varchar(n) remains for SQL standard support reasons. NB: varchar() (with empty brackets) is a Postgres-specific alias for text.

说明:varchar(n)和text使用相同的后端存储类型(varlena):具有32位长度计数器的可变长度字节数组。对于索引行为,文本甚至可能具有一些性能优势。 Postgres被认为是使用文本类型进行新开发的最佳实践; varchar(n)仍然是出于SQL标准支持的原因。注意:varchar()(带空括号)是Postgres特定的文本别名。

See also: http://www.postgresql.org/about/

另见:http://www.postgresql.org/about/

#2


11  

According to the official documentation ( http://www.postgresql.org/docs/9.2/static/datatype-character.html ):

根据官方文件(http://www.postgresql.org/docs/9.2/static/datatype-character.html):

In any case, the longest possible character string that can be stored is about 1 GB. (The maximum value that will be allowed for n in the data type declaration is less than that. It wouldn't be useful to change this because with multibyte character encodings the number of characters and bytes can be quite different. If you desire to store long strings with no specific upper limit, use text or character varying without a length specifier, rather than making up an arbitrary length limit.)

在任何情况下,可存储的最长字符串大约为1 GB。 (数据类型声明中n允许的最大值小于此值。更改此值没有用,因为使用多字节字符编码时,字符数和字节数可能完全不同。如果您希望存储没有特定上限的长字符串,使用不带长度说明符的文本或字符变化,而不是构成任意长度限制。)

Searching online reveals that the maximum value allowed varies depending on the installation and compilation options, some users report a maximum of 10485760 characters (10MiB exactly, assuming 1-byte-per-character fixed encoding).

在线搜索显示允许的最大值取决于安装和编译选项,一些用户报告最多10485760个字符(精确10MiB,假设每个字符固定编码为1个字节)。

#1


26  

tl;dr: 1 GB (each character (really: codepoint) may be represented by 1 or more bytes, depending on where they are on a unicode plane - assuming a UTF-8 encoded database). You should always use text datatype for arbitrary-length character data in Postgresql now.

tl; dr:1 GB(每个字符(实际上:代码点)可以由1个或更多字节表示,具体取决于它们在unicode平面上的位置 - 假设是UTF-8编码的数据库)。现在,您应该始终在Postgresql中使用text数据类型作为任意长度的字符数据。

Explanation: varchar(n) and text use the same backend storage type (varlena): a variable length byte array with a 32bit length counter. For indexing behavior text may even have some performance benefits. It is considered a best practice in Postgres to use text type for new development; varchar(n) remains for SQL standard support reasons. NB: varchar() (with empty brackets) is a Postgres-specific alias for text.

说明:varchar(n)和text使用相同的后端存储类型(varlena):具有32位长度计数器的可变长度字节数组。对于索引行为,文本甚至可能具有一些性能优势。 Postgres被认为是使用文本类型进行新开发的最佳实践; varchar(n)仍然是出于SQL标准支持的原因。注意:varchar()(带空括号)是Postgres特定的文本别名。

See also: http://www.postgresql.org/about/

另见:http://www.postgresql.org/about/

#2


11  

According to the official documentation ( http://www.postgresql.org/docs/9.2/static/datatype-character.html ):

根据官方文件(http://www.postgresql.org/docs/9.2/static/datatype-character.html):

In any case, the longest possible character string that can be stored is about 1 GB. (The maximum value that will be allowed for n in the data type declaration is less than that. It wouldn't be useful to change this because with multibyte character encodings the number of characters and bytes can be quite different. If you desire to store long strings with no specific upper limit, use text or character varying without a length specifier, rather than making up an arbitrary length limit.)

在任何情况下,可存储的最长字符串大约为1 GB。 (数据类型声明中n允许的最大值小于此值。更改此值没有用,因为使用多字节字符编码时,字符数和字节数可能完全不同。如果您希望存储没有特定上限的长字符串,使用不带长度说明符的文本或字符变化,而不是构成任意长度限制。)

Searching online reveals that the maximum value allowed varies depending on the installation and compilation options, some users report a maximum of 10485760 characters (10MiB exactly, assuming 1-byte-per-character fixed encoding).

在线搜索显示允许的最大值取决于安装和编译选项,一些用户报告最多10485760个字符(精确10MiB,假设每个字符固定编码为1个字节)。