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
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
According to the official documentation ( http://www.postgresql.org/docs/9.2/static/datatype-character.html ):
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).By "the installation and compilation options" I mean that you can always build PostgreSQL from source yourself and before you compile PostgreSQL to make your own database server you can configure how it stores text to change the maximum amount you can store - but if you do this then it means you might run into trouble if you try to use your database files with a "normal", non-customized build of PostgreSQL.
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.Explanation:
varchar(n)
andtext
use the same backend storage type (varlena
): a variable length byte array with a 32bit length counter. For indexing behaviortext
may even have some performance benefits. It is considered a best practice in Postgres to usetext
type for new development;varchar(n)
remains for SQL standard support reasons. NB:varchar()
(with empty brackets) is a Postgres-specific alias fortext
.See also: http://www.postgresql.org/about/