PostgreSQL复合类型长度限制咨询:是固定8KB还是依组成数据而定?
Hey there! Let’s clear up this confusion about composite type size limits in PostgreSQL once and for all.
First off: PostgreSQL does NOT impose a hard 8KB limit on composite types themselves. That 8KB number you’re thinking of is actually the default size of a PostgreSQL data page—the basic unit for storing table rows—but it doesn’t directly restrict composite types.
The actual maximum size of a composite type depends entirely on its constituent elements and how PostgreSQL handles large data via its TOAST (The Oversized-Attribute Storage Technique) system:
If your composite type includes TOAST-able data types (like
text,bytea, arrays, or other variable-length types that support compression/offloading):- There’s effectively no practical size limit (other than your available disk space). When a composite type’s total size exceeds a threshold (controlled by the
toast_tuple_targetconfiguration parameter, usually around 2KB), PostgreSQL automatically compresses the large elements or stores them in a separate TOAST table, leaving only a small pointer in the main table row. This means even a composite type with multiple huge text fields can be stored without hitting the 8KB page barrier.
- There’s effectively no practical size limit (other than your available disk space). When a composite type’s total size exceeds a threshold (controlled by the
If your composite type only uses non-TOAST-able, fixed-size types (like
int,bool,char(n), or numeric types with fixed precision):- The total size of the composite type can’t exceed the maximum row size in PostgreSQL, which is roughly 8160 bytes (the default 8KB page minus metadata overhead). Since these types can’t be compressed or offloaded, their combined size has to fit within a single data page. This scenario is pretty rare for "large, complex structures" though—most use cases for composite types will include at least some TOAST-able fields.
To put it simply: The 8KB page size is a storage implementation detail, not a hard cap on composite types. Your composite type’s maximum size is determined by what you put inside it, with TOAST handling the heavy lifting for large, variable-length data.
内容的提问来源于stack exchange,提问作者Shore

