If you made it to the end of the article, you saw the author recommends to consider making these sidecar tables by hand if you have medium-size texts. I think I would try very hard to tune TOAST before going that far. Also note that changing the table storage parameters doesn't automatically re-write the existing data. You need to re-store the data to make that happen. I believe `UPDATE t SET foo = foo` would do it, but I haven't tested.
The 8kB page size also matters when you use Postgres as a vector database, e.g. with pgvector. Jonathan Katz gave a great lightning talk about this a few months ago at PGCon. Vectors these days can have a lot of parameters, so they are big. But since the whole point is to query against the vector, you don't really want it TOASTed. I believe he has been working on some tricks to solve that problem.
We currently do string-interning at the application level, by:
1. creating an explicit table of strings to join with, with an auto-increment pkey and a unique index on the string;
2. "registering" newly-discovered strings in the data, by inserting into this strings table with ON CONFLICT DO NOTHING;
3. then querying back for the string IDs, loading these into a local in-memory cache, and using the cache to map strings to their string IDs;
4. then actually inserting the data, where all strings have been mapped to string IDs.
It really feels like something PG should be doing itself (like TOAST!) But so far I haven't seen any extension that attempts to solve this problem.
(This is just a very close analogy, not what we're actually doing:) imagine a data warehouse for relationally querying IPLD (https://ipld.io/) -hypermedia-document-shaped data, where every piece of data has embedded "foreign keys" to other data, that come in the form of content hashes (really IPFS CIDs, but you can strip that down to just a hash for internal use.) An average modelled-document-type table will have 3-12 of these foreign keys as toplevel columns; and these columns will have various indices on them, to allow the various tables of this data to be efficiently joined through these keys.
And there will be billions of records in each of these modelled-document-type tables.
We still need the content hashes — we can't just swap them out for BIGSERIAL values at ingest time and drop the original content-hash values — because people query the data by content-hash, and also expect content-hash references to other documents to appear in query results.
Throwing all the content hashes into a big "interned strings" table, and then replacing them with content-hash-IDs in the regular tables, saves tons of space (and thereby makes much more of the regular tables fit into memory at once.) And as we can join content-hash-ID foreign-keys to content-hash-ID primary-keys, we only ever need to involve the "interned strings" table 1. as an initial lookup to find the first document to work with, and 2. to translate any content-hash-IDs in the result back to content hashes.
(If you think about it carefully, while "string interning" might be the strategy used, the problem is more like "keeping a bijective mapping between large expensive globally-unique public identifiers, and cheap small per-shard-unique private identifiers, where translation occurs between global and local identifiers at the system API boundary.")
(Also, to be pedantic, a Postgres BYTEA is a string; it's just what a programming language would call a "raw string" — i.e. a string of bytes. Postgres TEXT, meanwhile, is a string of characters, required to be valid in a given character encoding. The PG TEXT and BYTEA types are the same ADT with the same set of applicable operations. Of course, this puts the lie to the name "BYTEA" [i.e. "byte array"] — it's definitely not an array ADT of any kind. If it were, then you'd be able to use PG's array operations on it!)
Your application-based approach is the way you have to do it.
I think what's happening is that since the tuples in the medium table are larger, less are stored on each page. Just looking at the sizes of the tables:
medium: 977 MB
Large: 25 MB
we can assume that the medium query had to access 39.08 ( 977 / 25 ) * more pages.
medium time: 3198.539
large time: 85.74
3198.539 / 85.74 = 37 which seems to track.
What was confusing me is that I thought that the io would take longer for the large text objects because there's more of it. I think that the reason why this isn't the case is because TOAST is probably optimised to make fewer calls to io, but they tend to be much larger. it's not necessarily the size of the data, but rather how many syscalls you have to make to request the data.
pretty unsure about this, but it seems to make sense now.
The surprising impact of medium-size texts on PostgreSQL performance - https://news.ycombinator.com/item?id=24836979 - Oct 2020 (16 comments)
Also, worth mentioning that jsonb is only benefitial when you plan on indexing/manipulating the data in the database. Otherwise, using json type may be a better choice (smaller size, stored as text, parsed on demand)
EDIT: Now confirmed by author https://news.ycombinator.com/item?id=37323895
From the article:
> We executed a query against all three tables to search for a string within the text value...This time, the results are more consistent with what we would expect. [Large is slower than medium.]
---
I.E. This isn't a "bug" ... this is a performance tradeoff. Transitioning values from inline to a separate table has performance positives and negatives. This article highlights a performance positive from TOAST once the toast_tuple_target boundary is crossed.
This drastically reduces the impact of unused fields compared to a full table scan.
tl;dr; CPU cache sizes are small and text is large. Using numeric fields will always be faster. "hackyhacky" is equal to 1.25 Int64s.