"Joe Conway" <mail@joeconway.com> writes:
> Sorry for my ignorance, but I haven't been able to keep up lately --
> what is the difference between pg_detoast_datum_packed and pg_detoast_datum,
> and how do I know when to use each? E.g. I notice that the related macro
> PG_GETARG_TEXT_PP is used in place of PG_GETARG_TEXT_P in many (but not all)
> places in the backend.
We now use only 1 byte for varlena length headers when the datum is up to 126
bytes long. This saves 3-6 bytes since we also don't have to do the four byte
alignment that 4-byte headers require.
This gets expanded into a regular 4-byte header by pg_detoast_datum() so that
all regular data type functions never see the packed varlenas with 1-byte
headers. That lets them use the regular VARDATA() and VARSIZE() macros and
lets them assume 4-byte alignment.
It's always safe to just use the old PG_DETOAST_DATUM() even on a datatype
like text.
In heavily used functions on data types such as text which don't care about
alignment we can avoid having to allocate memory to hold a 4-byte header copy
of the packed varlenas. But we still have to detoast externally stored or
compressed data.
The interface to do so is to use PG_DETOAST_DATUM_PACKED() and then use
VARDATA_ANY() and VARSIZE_ANY_EXHDR() instead of VARDATA and VARSIZE. This
detoasts large data but keeps small data packed and lets you work with either
1-byte or 4-byte headers without knowing which you have.
There's a comment i fmgr.h above pg_detoast_datum which says most of this and
more detailed comments in postgres.h.
-- Gregory Stark EnterpriseDB http://www.enterprisedb.com