Search code examples
postgresqldrupalcharacterbyte

What is the maximum possible size of VARCHAR(255)?


On Drupal documentation page (https://www.drupal.org/docs/7/api/schema-api/data-types/varchar) I read:

Make sure you are aware of the effects of a multi-byte character set. VARCHAR(255) stores 255 characters, which may be more than 255 bytes.

So I got curious what is the maximum possible size of 255 characters?


Solution

  • According to RFC 2279, the highest possible character (code point 0x7FFF FFFF) could be represented by 6 bytes. The highest possible code point you can represent in PostgreSQL is 0x10FFFF, which occupies 4 bytes:

    SELECT E'\U0010FFFF';
    
     ?column? 
    ══════════
     
    (1 row)
    
    SELECT E'\U00110000';
    ERROR:  invalid Unicode escape value at or near "\U00110000"
    LINE 1: SELECT E'\U00110000';
                     ^
    
    SELECT CAST(E'\U0010FFFF' AS bytea);
    
       bytea    
    ════════════
     \xf48fbfbf
    (1 row)
    

    So you are on the safe side if you assume an upper limit of 4 bytes per character.