3 ms·
>Variable length strings are kept in a separate file (TOAST tables), referenced by the tuple. This keeps records fixed size... That's not entirely true: variab
by uhoh-itsmaciek 4y ago
>Variable length strings are kept in a separate file (TOAST tables), referenced by the tuple. This keeps records fixed size...
That's not entirely true: variable-width row values under a size threshold are stored inline (possibly compressed).
The beauty part about TOAST is that they're mostly just regular tables (any table that needs its data TOASTed gets its own TOAST table). You can look them up in the system catalogs and query them directly.
The schema for these is (chunk_id, chunk_seq, chunk_data). Chunk "pieces" are limited in size so they can be stored inline (obviously there's not much benefit to a TOAST table itself having a TOAST table), so any row values that need to be TOASTed may also be broken up into multiple pieces. Row values stored inline in the "owning" table reference a chunk_id where the TOASTed data is stored, and queries that need to return that data look up all the chunks for a chunk id, ordered by chunk_seq.
You're right that this scheme avoids reading TOASTed data when you're not actually using it (a good reason to avoid "SELECT *" if you've got big TOASTed data).