Skip site navigation (1) Skip section navigation (2)

Re: pg_detoast_datum_packed and friends

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Joe Conway" <mail(at)joeconway(dot)com>
Cc: "Hackers \(PostgreSQL\)" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: pg_detoast_datum_packed and friends
Date: 2007-06-02 09:38:56
Message-ID: 87ps4e66hb.fsf@oxford.xeocode.com (view raw or flat)
Thread:
Lists: pgsql-hackers
"Joe Conway" <mail(at)joeconway(dot)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


In response to

pgsql-hackers by date

Next:From: Andrew DunstanDate: 2007-06-02 12:30:58
Subject: Re: [PATCHES] build/install xml2 when configured with libxml
Previous:From: Tom LaneDate: 2007-06-02 05:24:32
Subject: Re: Command tags in create/drop scripts

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group