
Because of this variable sized columns shouldĪlways be at the end of a table.

PostgreSQL determines this can be embedded directly into a row it may do so, butįor very large values it stores the data externally and store a pointer (ofġ word in size) in the column.
POSTGRESQL ALTER TABLE COLUMN SIZE PLUS
Type Size Alignment needed smallint 2 bytes 1 word integer 4 bytes 1 word bigint 8 bytes 8 bytes real 4 bytes 1 word double precision 8 bytes 8 bytes boolean 1 byte not needed text / string variable, 1 byte plus the data 1 word bytea variable, 1 or 4 bytes plus the data 1 word timestamp 8 bytes 8 bytes timestamptz 8 bytes 8 bytes date 4 bytes 1 wordĪ “variable” size means the actual size depends on the value being stored.
POSTGRESQL ALTER TABLE COLUMN SIZE 64 BITS
Here “word” refers to the word size, which is 4 bytes for a 32īits platform and 8 bytes for a 64 bits platform. Of information we list the sizes of common types here so it’s easier to While the PostgreSQL documentation contains plenty We are using integer in the examples to showcase a more realistic reordering scenario. Since Ruby on Rails 5.1, the default data type for IDs is bigint, which uses 8 bytes. Means we only need 8 bytes to store both of them. In these examples, the id and user_id columns are packed together, which Ideal column order would be the following: Rows you can save space by using a different order.

However, once you start storing millions of This means thatĮach row requires at least 16 bytes for the two 4-byte integers. The user_idĬolumn takes only 4 bytes, and on 64-bit platform, 4 zeroes are added forĪlignment padding, to allow storing the next row beginning with the “clear” word.Īs a result, the actual size of each column would be (omitting variable lengthĭata and 24-byte tuple header): 8 bytes, variable, 8 bytes.

The space between rows is also subject to alignment padding. Padding, and only next name is being stored. To meet the alignment requirements, four zeros are to be added rightĪfter the first column, so id occupies 4 bytes, then 4 bytes of alignment Text data type requires 1-word alignment, and on 64-bit platform, 1 word is 8īytes. This is because the size of columns is aligned depending on the type of Similar to C structures the space of a table is influenced by the order ofĬolumns. Type size in descending order with variable sizes ( text, varchar, arrays, An easy way of doing this is to order them based on the
