
To scan the small and large tables, the database only had to read 21MB and 25MB and the query was pretty fast.
#Medium make text smaller full#
When we executed our query, the database did a full table scan.

This is why the TOAST table is so big for the large table, but the table itself remained small.ġ 2 id value 500K 1 \x. This is because we inserted large texts into the table, and PostgreSQL stored them out-of-line in the TOAST table.
#Medium make text smaller code#
The TOAST code will compress and/or move field values out-of-line until the row value is shorter than TOAST_TUPLE_TARGET bytes (also normally 2 kB, adjustable) or no more gains can be had The TOAST management code is triggered only when a row value to be stored in a table is wider than TOAST_TUPLE_THRESHOLD bytes (normally 2 kB). It is used to store large pieces of data of TOAST-able columns (the text datatype for example, is TOAST-able). So TOAST is a separate table associated with our table. If any of the columns of a table are TOAST-able, the table will have an associated TOAST table OK, so how is this TOAST working exactly?

The technique is affectionately known as TOAST (or “the best thing since sliced bread”). large field values are compressed and/or broken up into multiple physical rows. So how does the database store large chunks of data? Therefore, it is not possible to store very large field values directly.Īs the documentation explains, PostgreSQL can't store rows (tuples) in multiple pages. PostgreSQL uses a fixed page size (commonly 8 kB), and does not allow tuples to span multiple pages. Intuitively, you might think that the database is storing large pieces of data inline like it does smaller pieces of data, but in fact, it does not: When talking about large chunks of text, or any other field that may contain large amounts of data, we first need to understand how the database handles the data. In this article I demonstrate the surprising impact of medium-size texts on query performance in PostgreSQL.

These type of texts would normally be unrestricted, but naturally smaller than the large texts. These are any text field that is between the small and the large. Medium texts: descriptions, comments, product reviews, stack traces etc. These are large pieces of free, unrestricted text that is stored in the database. Large texts: blog post content, articles, HTML content etc. These are text fields that usually have some low size limit, maybe even using varchar(n) and not text. Small texts: names, slugs, usernames, emails, etc. In this article, I divide text fields into three categories: The Surprising Impact of Medium-Size Texts on PostgreSQL Performance Why TOAST is the best thing since sliced breadĪny database schema is likely to have plenty of text fields.
