Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions

tuptoaster.h File Reference

#include "access/htup_details.h"
#include "utils/relcache.h"
Include dependency graph for tuptoaster.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define TOAST_INDEX_HACK
#define MaximumBytesPerTuple(tuplesPerPage)
#define TOAST_TUPLES_PER_PAGE   4
#define TOAST_TUPLE_THRESHOLD   MaximumBytesPerTuple(TOAST_TUPLES_PER_PAGE)
#define TOAST_TUPLE_TARGET   TOAST_TUPLE_THRESHOLD
#define TOAST_TUPLES_PER_PAGE_MAIN   1
#define TOAST_TUPLE_TARGET_MAIN   MaximumBytesPerTuple(TOAST_TUPLES_PER_PAGE_MAIN)
#define TOAST_INDEX_TARGET   (MaxHeapTupleSize / 16)
#define EXTERN_TUPLES_PER_PAGE   4
#define EXTERN_TUPLE_MAX_SIZE   MaximumBytesPerTuple(EXTERN_TUPLES_PER_PAGE)
#define TOAST_MAX_CHUNK_SIZE

Functions

HeapTuple toast_insert_or_update (Relation rel, HeapTuple newtup, HeapTuple oldtup, int options)
void toast_delete (Relation rel, HeapTuple oldtup)
struct varlenaheap_tuple_fetch_attr (struct varlena *attr)
struct varlenaheap_tuple_untoast_attr (struct varlena *attr)
struct varlenaheap_tuple_untoast_attr_slice (struct varlena *attr, int32 sliceoffset, int32 slicelength)
HeapTuple toast_flatten_tuple (HeapTuple tup, TupleDesc tupleDesc)
Datum toast_flatten_tuple_attribute (Datum value, Oid typeId, int32 typeMod)
Datum toast_compress_datum (Datum value)
Size toast_raw_datum_size (Datum value)
Size toast_datum_size (Datum value)

Define Documentation

#define EXTERN_TUPLE_MAX_SIZE   MaximumBytesPerTuple(EXTERN_TUPLES_PER_PAGE)

Definition at line 88 of file tuptoaster.h.

#define EXTERN_TUPLES_PER_PAGE   4

Definition at line 86 of file tuptoaster.h.

#define MaximumBytesPerTuple (   tuplesPerPage  ) 
Value:
MAXALIGN_DOWN((BLCKSZ - \
                   MAXALIGN(SizeOfPageHeaderData + (tuplesPerPage) * sizeof(ItemIdData))) \
                  / (tuplesPerPage))

Definition at line 29 of file tuptoaster.h.

#define TOAST_INDEX_HACK

Definition at line 23 of file tuptoaster.h.

Referenced by index_form_tuple().

#define TOAST_INDEX_TARGET   (MaxHeapTupleSize / 16)

Definition at line 74 of file tuptoaster.h.

Referenced by gtsvector_compress(), and index_form_tuple().

#define TOAST_MAX_CHUNK_SIZE
Value:
(EXTERN_TUPLE_MAX_SIZE -                            \
     MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) -  \
     sizeof(Oid) -                                      \
     sizeof(int32) -                                    \
     VARHDRSZ)

Definition at line 90 of file tuptoaster.h.

Referenced by ReadControlFile(), toast_fetch_datum(), toast_fetch_datum_slice(), and toast_save_datum().

#define TOAST_TUPLE_TARGET   TOAST_TUPLE_THRESHOLD

Definition at line 56 of file tuptoaster.h.

Referenced by toast_insert_or_update().

#define TOAST_TUPLE_TARGET_MAIN   MaximumBytesPerTuple(TOAST_TUPLES_PER_PAGE_MAIN)

Definition at line 67 of file tuptoaster.h.

Referenced by toast_insert_or_update().

#define TOAST_TUPLE_THRESHOLD   MaximumBytesPerTuple(TOAST_TUPLES_PER_PAGE)

Definition at line 54 of file tuptoaster.h.

Referenced by heap_prepare_insert(), needs_toast_table(), and raw_heap_insert().

#define TOAST_TUPLES_PER_PAGE   4

Definition at line 52 of file tuptoaster.h.

#define TOAST_TUPLES_PER_PAGE_MAIN   1

Definition at line 65 of file tuptoaster.h.


Function Documentation

struct varlena* heap_tuple_fetch_attr ( struct varlena attr  )  [read]

Definition at line 98 of file tuptoaster.c.

References toast_fetch_datum(), and VARATT_IS_EXTERNAL.

Referenced by index_form_tuple(), toast_insert_or_update(), and tstoreReceiveSlot_detoast().

{
    struct varlena *result;

    if (VARATT_IS_EXTERNAL(attr))
    {
        /*
         * This is an external stored plain value
         */
        result = toast_fetch_datum(attr);
    }
    else
    {
        /*
         * This is a plain value inside of the main tuple - why am I called?
         */
        result = attr;
    }

    return result;
}

struct varlena* heap_tuple_untoast_attr ( struct varlena attr  )  [read]

Definition at line 129 of file tuptoaster.c.

References palloc(), pfree(), pglz_decompress(), PGLZ_RAW_SIZE, SET_VARSIZE, toast_fetch_datum(), VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARATT_IS_SHORT, VARDATA, VARDATA_SHORT, VARHDRSZ, and VARSIZE_SHORT.

Referenced by inv_getsize(), inv_read(), inv_truncate(), inv_write(), pg_detoast_datum(), pg_detoast_datum_copy(), pg_detoast_datum_packed(), toast_flatten_tuple_attribute(), and toast_insert_or_update().

{
    if (VARATT_IS_EXTERNAL(attr))
    {
        /*
         * This is an externally stored datum --- fetch it back from there
         */
        attr = toast_fetch_datum(attr);
        /* If it's compressed, decompress it */
        if (VARATT_IS_COMPRESSED(attr))
        {
            PGLZ_Header *tmp = (PGLZ_Header *) attr;

            attr = (struct varlena *) palloc(PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
            SET_VARSIZE(attr, PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
            pglz_decompress(tmp, VARDATA(attr));
            pfree(tmp);
        }
    }
    else if (VARATT_IS_COMPRESSED(attr))
    {
        /*
         * This is a compressed value inside of the main tuple
         */
        PGLZ_Header *tmp = (PGLZ_Header *) attr;

        attr = (struct varlena *) palloc(PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
        SET_VARSIZE(attr, PGLZ_RAW_SIZE(tmp) + VARHDRSZ);
        pglz_decompress(tmp, VARDATA(attr));
    }
    else if (VARATT_IS_SHORT(attr))
    {
        /*
         * This is a short-header varlena --- convert to 4-byte header format
         */
        Size        data_size = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT;
        Size        new_size = data_size + VARHDRSZ;
        struct varlena *new_attr;

        new_attr = (struct varlena *) palloc(new_size);
        SET_VARSIZE(new_attr, new_size);
        memcpy(VARDATA(new_attr), VARDATA_SHORT(attr), data_size);
        attr = new_attr;
    }

    return attr;
}

struct varlena* heap_tuple_untoast_attr_slice ( struct varlena attr,
int32  sliceoffset,
int32  slicelength 
) [read]

Definition at line 186 of file tuptoaster.c.

References palloc(), pfree(), pglz_decompress(), PGLZ_RAW_SIZE, SET_VARSIZE, toast_fetch_datum(), toast_fetch_datum_slice(), VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARATT_IS_SHORT, VARDATA, VARDATA_SHORT, VARHDRSZ, VARSIZE, and VARSIZE_SHORT.

Referenced by pg_detoast_datum_slice().

{
    struct varlena *preslice;
    struct varlena *result;
    char       *attrdata;
    int32       attrsize;

    if (VARATT_IS_EXTERNAL(attr))
    {
        struct varatt_external toast_pointer;

        VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);

        /* fast path for non-compressed external datums */
        if (!VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
            return toast_fetch_datum_slice(attr, sliceoffset, slicelength);

        /* fetch it back (compressed marker will get set automatically) */
        preslice = toast_fetch_datum(attr);
    }
    else
        preslice = attr;

    if (VARATT_IS_COMPRESSED(preslice))
    {
        PGLZ_Header *tmp = (PGLZ_Header *) preslice;
        Size        size = PGLZ_RAW_SIZE(tmp) + VARHDRSZ;

        preslice = (struct varlena *) palloc(size);
        SET_VARSIZE(preslice, size);
        pglz_decompress(tmp, VARDATA(preslice));

        if (tmp != (PGLZ_Header *) attr)
            pfree(tmp);
    }

    if (VARATT_IS_SHORT(preslice))
    {
        attrdata = VARDATA_SHORT(preslice);
        attrsize = VARSIZE_SHORT(preslice) - VARHDRSZ_SHORT;
    }
    else
    {
        attrdata = VARDATA(preslice);
        attrsize = VARSIZE(preslice) - VARHDRSZ;
    }

    /* slicing of datum for compressed cases and plain value */

    if (sliceoffset >= attrsize)
    {
        sliceoffset = 0;
        slicelength = 0;
    }

    if (((sliceoffset + slicelength) > attrsize) || slicelength < 0)
        slicelength = attrsize - sliceoffset;

    result = (struct varlena *) palloc(slicelength + VARHDRSZ);
    SET_VARSIZE(result, slicelength + VARHDRSZ);

    memcpy(VARDATA(result), attrdata + sliceoffset, slicelength);

    if (preslice != attr)
        pfree(preslice);

    return result;
}

Datum toast_compress_datum ( Datum  value  ) 

Definition at line 1180 of file tuptoaster.c.

References Assert, DatumGetPointer, PGLZ_Strategy::max_input_size, NULL, palloc(), pfree(), pglz_compress(), PGLZ_MAX_OUTPUT, PGLZ_strategy_default, PointerGetDatum, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARDATA_ANY, VARSIZE, and VARSIZE_ANY_EXHDR.

Referenced by index_form_tuple(), and toast_insert_or_update().

{
    struct varlena *tmp;
    int32       valsize = VARSIZE_ANY_EXHDR(DatumGetPointer(value));

    Assert(!VARATT_IS_EXTERNAL(DatumGetPointer(value)));
    Assert(!VARATT_IS_COMPRESSED(DatumGetPointer(value)));

    /*
     * No point in wasting a palloc cycle if value size is out of the allowed
     * range for compression
     */
    if (valsize < PGLZ_strategy_default->min_input_size ||
        valsize > PGLZ_strategy_default->max_input_size)
        return PointerGetDatum(NULL);

    tmp = (struct varlena *) palloc(PGLZ_MAX_OUTPUT(valsize));

    /*
     * We recheck the actual size even if pglz_compress() reports success,
     * because it might be satisfied with having saved as little as one byte
     * in the compressed data --- which could turn into a net loss once you
     * consider header and alignment padding.  Worst case, the compressed
     * format might require three padding bytes (plus header, which is
     * included in VARSIZE(tmp)), whereas the uncompressed format would take
     * only one header byte and no padding if the value is short enough.  So
     * we insist on a savings of more than 2 bytes to ensure we have a gain.
     */
    if (pglz_compress(VARDATA_ANY(DatumGetPointer(value)), valsize,
                      (PGLZ_Header *) tmp, PGLZ_strategy_default) &&
        VARSIZE(tmp) < valsize - 2)
    {
        /* successful compression */
        return PointerGetDatum(tmp);
    }
    else
    {
        /* incompressible data */
        pfree(tmp);
        return PointerGetDatum(NULL);
    }
}

Size toast_datum_size ( Datum  value  ) 

Definition at line 306 of file tuptoaster.c.

References DatumGetPointer, varatt_external::va_extsize, VARATT_EXTERNAL_GET_POINTER, VARATT_IS_EXTERNAL, VARATT_IS_SHORT, VARSIZE, and VARSIZE_SHORT.

Referenced by pg_column_size().

{
    struct varlena *attr = (struct varlena *) DatumGetPointer(value);
    Size        result;

    if (VARATT_IS_EXTERNAL(attr))
    {
        /*
         * Attribute is stored externally - return the extsize whether
         * compressed or not.  We do not count the size of the toast pointer
         * ... should we?
         */
        struct varatt_external toast_pointer;

        VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
        result = toast_pointer.va_extsize;
    }
    else if (VARATT_IS_SHORT(attr))
    {
        result = VARSIZE_SHORT(attr);
    }
    else
    {
        /*
         * Attribute is stored inline either compressed or not, just calculate
         * the size of the datum in either case.
         */
        result = VARSIZE(attr);
    }
    return result;
}

void toast_delete ( Relation  rel,
HeapTuple  oldtup 
)

Definition at line 346 of file tuptoaster.c.

References Assert, tupleDesc::attrs, heap_deform_tuple(), i, MaxHeapAttributeNumber, tupleDesc::natts, PointerGetDatum, RelationData::rd_att, RelationData::rd_rel, RELKIND_MATVIEW, RELKIND_RELATION, toast_delete_datum(), value, and VARATT_IS_EXTERNAL.

Referenced by heap_delete().

{
    TupleDesc   tupleDesc;
    Form_pg_attribute *att;
    int         numAttrs;
    int         i;
    Datum       toast_values[MaxHeapAttributeNumber];
    bool        toast_isnull[MaxHeapAttributeNumber];

    /*
     * We should only ever be called for tuples of plain relations or
     * materialized views --- recursing on a toast rel is bad news.
     */
    Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
           rel->rd_rel->relkind == RELKIND_MATVIEW);

    /*
     * Get the tuple descriptor and break down the tuple into fields.
     *
     * NOTE: it's debatable whether to use heap_deform_tuple() here or just
     * heap_getattr() only the varlena columns.  The latter could win if there
     * are few varlena columns and many non-varlena ones. However,
     * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
     * O(N^2) if there are many varlena columns, so it seems better to err on
     * the side of linear cost.  (We won't even be here unless there's at
     * least one varlena column, by the way.)
     */
    tupleDesc = rel->rd_att;
    att = tupleDesc->attrs;
    numAttrs = tupleDesc->natts;

    Assert(numAttrs <= MaxHeapAttributeNumber);
    heap_deform_tuple(oldtup, tupleDesc, toast_values, toast_isnull);

    /*
     * Check for external stored attributes and delete them from the secondary
     * relation.
     */
    for (i = 0; i < numAttrs; i++)
    {
        if (att[i]->attlen == -1)
        {
            Datum       value = toast_values[i];

            if (!toast_isnull[i] && VARATT_IS_EXTERNAL(PointerGetDatum(value)))
                toast_delete_datum(rel, value);
        }
    }
}

HeapTuple toast_flatten_tuple ( HeapTuple  tup,
TupleDesc  tupleDesc 
)

Definition at line 952 of file tuptoaster.c.

References Assert, tupleDesc::attrs, DatumGetPointer, heap_deform_tuple(), heap_form_tuple(), HeapTupleGetOid, HeapTupleSetOid, i, MaxTupleAttributeNumber, tupleDesc::natts, pfree(), PointerGetDatum, HeapTupleHeaderData::t_choice, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_self, HeapTupleData::t_tableOid, tupleDesc::tdhasoid, toast_fetch_datum(), and VARATT_IS_EXTERNAL.

Referenced by CatalogCacheCreateEntry().

{
    HeapTuple   new_tuple;
    Form_pg_attribute *att = tupleDesc->attrs;
    int         numAttrs = tupleDesc->natts;
    int         i;
    Datum       toast_values[MaxTupleAttributeNumber];
    bool        toast_isnull[MaxTupleAttributeNumber];
    bool        toast_free[MaxTupleAttributeNumber];

    /*
     * Break down the tuple into fields.
     */
    Assert(numAttrs <= MaxTupleAttributeNumber);
    heap_deform_tuple(tup, tupleDesc, toast_values, toast_isnull);

    memset(toast_free, 0, numAttrs * sizeof(bool));

    for (i = 0; i < numAttrs; i++)
    {
        /*
         * Look at non-null varlena attributes
         */
        if (!toast_isnull[i] && att[i]->attlen == -1)
        {
            struct varlena *new_value;

            new_value = (struct varlena *) DatumGetPointer(toast_values[i]);
            if (VARATT_IS_EXTERNAL(new_value))
            {
                new_value = toast_fetch_datum(new_value);
                toast_values[i] = PointerGetDatum(new_value);
                toast_free[i] = true;
            }
        }
    }

    /*
     * Form the reconfigured tuple.
     */
    new_tuple = heap_form_tuple(tupleDesc, toast_values, toast_isnull);

    /*
     * Be sure to copy the tuple's OID and identity fields.  We also make a
     * point of copying visibility info, just in case anybody looks at those
     * fields in a syscache entry.
     */
    if (tupleDesc->tdhasoid)
        HeapTupleSetOid(new_tuple, HeapTupleGetOid(tup));

    new_tuple->t_self = tup->t_self;
    new_tuple->t_tableOid = tup->t_tableOid;

    new_tuple->t_data->t_choice = tup->t_data->t_choice;
    new_tuple->t_data->t_ctid = tup->t_data->t_ctid;
    new_tuple->t_data->t_infomask &= ~HEAP_XACT_MASK;
    new_tuple->t_data->t_infomask |=
        tup->t_data->t_infomask & HEAP_XACT_MASK;
    new_tuple->t_data->t_infomask2 &= ~HEAP2_XACT_MASK;
    new_tuple->t_data->t_infomask2 |=
        tup->t_data->t_infomask2 & HEAP2_XACT_MASK;

    /*
     * Free allocated temp values
     */
    for (i = 0; i < numAttrs; i++)
        if (toast_free[i])
            pfree(DatumGetPointer(toast_values[i]));

    return new_tuple;
}

Datum toast_flatten_tuple_attribute ( Datum  value,
Oid  typeId,
int32  typeMod 
)

Definition at line 1038 of file tuptoaster.c.

References Assert, tupleDesc::attrs, BITMAPLEN, DatumGetHeapTupleHeader, DatumGetPointer, heap_compute_data_size(), heap_deform_tuple(), heap_fill_tuple(), HEAP_HASOID, heap_tuple_untoast_attr(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetOid, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetNatts, HeapTupleHeaderSetOid, i, ItemPointerSetInvalid, lookup_rowtype_tupdesc_noerror(), MAXALIGN, MaxTupleAttributeNumber, tupleDesc::natts, NULL, offsetof, palloc0(), pfree(), PointerGetDatum, ReleaseTupleDesc, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, VARATT_IS_COMPRESSED, and VARATT_IS_EXTERNAL.

Referenced by heap_form_minimal_tuple(), and heap_form_tuple().

{
    TupleDesc   tupleDesc;
    HeapTupleHeader olddata;
    HeapTupleHeader new_data;
    int32       new_header_len;
    int32       new_data_len;
    int32       new_tuple_len;
    HeapTupleData tmptup;
    Form_pg_attribute *att;
    int         numAttrs;
    int         i;
    bool        need_change = false;
    bool        has_nulls = false;
    Datum       toast_values[MaxTupleAttributeNumber];
    bool        toast_isnull[MaxTupleAttributeNumber];
    bool        toast_free[MaxTupleAttributeNumber];

    /*
     * See if it's a composite type, and get the tupdesc if so.
     */
    tupleDesc = lookup_rowtype_tupdesc_noerror(typeId, typeMod, true);
    if (tupleDesc == NULL)
        return value;           /* not a composite type */

    att = tupleDesc->attrs;
    numAttrs = tupleDesc->natts;

    /*
     * Break down the tuple into fields.
     */
    olddata = DatumGetHeapTupleHeader(value);
    Assert(typeId == HeapTupleHeaderGetTypeId(olddata));
    Assert(typeMod == HeapTupleHeaderGetTypMod(olddata));
    /* Build a temporary HeapTuple control structure */
    tmptup.t_len = HeapTupleHeaderGetDatumLength(olddata);
    ItemPointerSetInvalid(&(tmptup.t_self));
    tmptup.t_tableOid = InvalidOid;
    tmptup.t_data = olddata;

    Assert(numAttrs <= MaxTupleAttributeNumber);
    heap_deform_tuple(&tmptup, tupleDesc, toast_values, toast_isnull);

    memset(toast_free, 0, numAttrs * sizeof(bool));

    for (i = 0; i < numAttrs; i++)
    {
        /*
         * Look at non-null varlena attributes
         */
        if (toast_isnull[i])
            has_nulls = true;
        else if (att[i]->attlen == -1)
        {
            struct varlena *new_value;

            new_value = (struct varlena *) DatumGetPointer(toast_values[i]);
            if (VARATT_IS_EXTERNAL(new_value) ||
                VARATT_IS_COMPRESSED(new_value))
            {
                new_value = heap_tuple_untoast_attr(new_value);
                toast_values[i] = PointerGetDatum(new_value);
                toast_free[i] = true;
                need_change = true;
            }
        }
    }

    /*
     * If nothing to untoast, just return the original tuple.
     */
    if (!need_change)
    {
        ReleaseTupleDesc(tupleDesc);
        return value;
    }

    /*
     * Calculate the new size of the tuple.
     *
     * This should match the reconstruction code in toast_insert_or_update.
     */
    new_header_len = offsetof(HeapTupleHeaderData, t_bits);
    if (has_nulls)
        new_header_len += BITMAPLEN(numAttrs);
    if (olddata->t_infomask & HEAP_HASOID)
        new_header_len += sizeof(Oid);
    new_header_len = MAXALIGN(new_header_len);
    new_data_len = heap_compute_data_size(tupleDesc,
                                          toast_values, toast_isnull);
    new_tuple_len = new_header_len + new_data_len;

    new_data = (HeapTupleHeader) palloc0(new_tuple_len);

    /*
     * Copy the existing tuple header, but adjust natts and t_hoff.
     */
    memcpy(new_data, olddata, offsetof(HeapTupleHeaderData, t_bits));
    HeapTupleHeaderSetNatts(new_data, numAttrs);
    new_data->t_hoff = new_header_len;
    if (olddata->t_infomask & HEAP_HASOID)
        HeapTupleHeaderSetOid(new_data, HeapTupleHeaderGetOid(olddata));

    /* Reset the datum length field, too */
    HeapTupleHeaderSetDatumLength(new_data, new_tuple_len);

    /* Copy over the data, and fill the null bitmap if needed */
    heap_fill_tuple(tupleDesc,
                    toast_values,
                    toast_isnull,
                    (char *) new_data + new_header_len,
                    new_data_len,
                    &(new_data->t_infomask),
                    has_nulls ? new_data->t_bits : NULL);

    /*
     * Free allocated temp values
     */
    for (i = 0; i < numAttrs; i++)
        if (toast_free[i])
            pfree(DatumGetPointer(toast_values[i]));
    ReleaseTupleDesc(tupleDesc);

    return PointerGetDatum(new_data);
}

HeapTuple toast_insert_or_update ( Relation  rel,
HeapTuple  newtup,
HeapTuple  oldtup,
int  options 
)

Definition at line 416 of file tuptoaster.c.

References Assert, tupleDesc::attrs, BITMAPLEN, DatumGetPointer, heap_compute_data_size(), heap_deform_tuple(), heap_fill_tuple(), HEAP_HASOID, heap_tuple_fetch_attr(), heap_tuple_untoast_attr(), HeapTupleHeaderGetOid, HeapTupleHeaderSetNatts, HeapTupleHeaderSetOid, HEAPTUPLESIZE, i, InvalidOid, MAXALIGN, MaxHeapAttributeNumber, memcmp(), tupleDesc::natts, NULL, offsetof, palloc0(), pfree(), PointerGetDatum, RelationData::rd_att, RelationData::rd_rel, RELKIND_MATVIEW, RELKIND_RELATION, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, toast_compress_datum(), toast_delete_datum(), TOAST_POINTER_SIZE, toast_save_datum(), TOAST_TUPLE_TARGET, TOAST_TUPLE_TARGET_MAIN, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARSIZE, VARSIZE_ANY, and VARSIZE_EXTERNAL.

Referenced by heap_prepare_insert(), heap_update(), and raw_heap_insert().

{
    HeapTuple   result_tuple;
    TupleDesc   tupleDesc;
    Form_pg_attribute *att;
    int         numAttrs;
    int         i;

    bool        need_change = false;
    bool        need_free = false;
    bool        need_delold = false;
    bool        has_nulls = false;

    Size        maxDataLen;
    Size        hoff;

    char        toast_action[MaxHeapAttributeNumber];
    bool        toast_isnull[MaxHeapAttributeNumber];
    bool        toast_oldisnull[MaxHeapAttributeNumber];
    Datum       toast_values[MaxHeapAttributeNumber];
    Datum       toast_oldvalues[MaxHeapAttributeNumber];
    struct varlena *toast_oldexternal[MaxHeapAttributeNumber];
    int32       toast_sizes[MaxHeapAttributeNumber];
    bool        toast_free[MaxHeapAttributeNumber];
    bool        toast_delold[MaxHeapAttributeNumber];

    /*
     * We should only ever be called for tuples of plain relations ---
     * recursing on a toast rel is bad news.
     */
    Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
           rel->rd_rel->relkind == RELKIND_MATVIEW);

    /*
     * Get the tuple descriptor and break down the tuple(s) into fields.
     */
    tupleDesc = rel->rd_att;
    att = tupleDesc->attrs;
    numAttrs = tupleDesc->natts;

    Assert(numAttrs <= MaxHeapAttributeNumber);
    heap_deform_tuple(newtup, tupleDesc, toast_values, toast_isnull);
    if (oldtup != NULL)
        heap_deform_tuple(oldtup, tupleDesc, toast_oldvalues, toast_oldisnull);

    /* ----------
     * Then collect information about the values given
     *
     * NOTE: toast_action[i] can have these values:
     *      ' '     default handling
     *      'p'     already processed --- don't touch it
     *      'x'     incompressible, but OK to move off
     *
     * NOTE: toast_sizes[i] is only made valid for varlena attributes with
     *      toast_action[i] different from 'p'.
     * ----------
     */
    memset(toast_action, ' ', numAttrs * sizeof(char));
    memset(toast_oldexternal, 0, numAttrs * sizeof(struct varlena *));
    memset(toast_free, 0, numAttrs * sizeof(bool));
    memset(toast_delold, 0, numAttrs * sizeof(bool));

    for (i = 0; i < numAttrs; i++)
    {
        struct varlena *old_value;
        struct varlena *new_value;

        if (oldtup != NULL)
        {
            /*
             * For UPDATE get the old and new values of this attribute
             */
            old_value = (struct varlena *) DatumGetPointer(toast_oldvalues[i]);
            new_value = (struct varlena *) DatumGetPointer(toast_values[i]);

            /*
             * If the old value is an external stored one, check if it has
             * changed so we have to delete it later.
             */
            if (att[i]->attlen == -1 && !toast_oldisnull[i] &&
                VARATT_IS_EXTERNAL(old_value))
            {
                if (toast_isnull[i] || !VARATT_IS_EXTERNAL(new_value) ||
                    memcmp((char *) old_value, (char *) new_value,
                           VARSIZE_EXTERNAL(old_value)) != 0)
                {
                    /*
                     * The old external stored value isn't needed any more
                     * after the update
                     */
                    toast_delold[i] = true;
                    need_delold = true;
                }
                else
                {
                    /*
                     * This attribute isn't changed by this update so we reuse
                     * the original reference to the old value in the new
                     * tuple.
                     */
                    toast_action[i] = 'p';
                    continue;
                }
            }
        }
        else
        {
            /*
             * For INSERT simply get the new value
             */
            new_value = (struct varlena *) DatumGetPointer(toast_values[i]);
        }

        /*
         * Handle NULL attributes
         */
        if (toast_isnull[i])
        {
            toast_action[i] = 'p';
            has_nulls = true;
            continue;
        }

        /*
         * Now look at varlena attributes
         */
        if (att[i]->attlen == -1)
        {
            /*
             * If the table's attribute says PLAIN always, force it so.
             */
            if (att[i]->attstorage == 'p')
                toast_action[i] = 'p';

            /*
             * We took care of UPDATE above, so any external value we find
             * still in the tuple must be someone else's we cannot reuse.
             * Fetch it back (without decompression, unless we are forcing
             * PLAIN storage).  If necessary, we'll push it out as a new
             * external value below.
             */
            if (VARATT_IS_EXTERNAL(new_value))
            {
                toast_oldexternal[i] = new_value;
                if (att[i]->attstorage == 'p')
                    new_value = heap_tuple_untoast_attr(new_value);
                else
                    new_value = heap_tuple_fetch_attr(new_value);
                toast_values[i] = PointerGetDatum(new_value);
                toast_free[i] = true;
                need_change = true;
                need_free = true;
            }

            /*
             * Remember the size of this attribute
             */
            toast_sizes[i] = VARSIZE_ANY(new_value);
        }
        else
        {
            /*
             * Not a varlena attribute, plain storage always
             */
            toast_action[i] = 'p';
        }
    }

    /* ----------
     * Compress and/or save external until data fits into target length
     *
     *  1: Inline compress attributes with attstorage 'x', and store very
     *     large attributes with attstorage 'x' or 'e' external immediately
     *  2: Store attributes with attstorage 'x' or 'e' external
     *  3: Inline compress attributes with attstorage 'm'
     *  4: Store attributes with attstorage 'm' external
     * ----------
     */

    /* compute header overhead --- this should match heap_form_tuple() */
    hoff = offsetof(HeapTupleHeaderData, t_bits);
    if (has_nulls)
        hoff += BITMAPLEN(numAttrs);
    if (newtup->t_data->t_infomask & HEAP_HASOID)
        hoff += sizeof(Oid);
    hoff = MAXALIGN(hoff);
    /* now convert to a limit on the tuple data size */
    maxDataLen = TOAST_TUPLE_TARGET - hoff;

    /*
     * Look for attributes with attstorage 'x' to compress.  Also find large
     * attributes with attstorage 'x' or 'e', and store them external.
     */
    while (heap_compute_data_size(tupleDesc,
                                  toast_values, toast_isnull) > maxDataLen)
    {
        int         biggest_attno = -1;
        int32       biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
        Datum       old_value;
        Datum       new_value;

        /*
         * Search for the biggest yet unprocessed internal attribute
         */
        for (i = 0; i < numAttrs; i++)
        {
            if (toast_action[i] != ' ')
                continue;
            if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                continue;       /* can't happen, toast_action would be 'p' */
            if (VARATT_IS_COMPRESSED(DatumGetPointer(toast_values[i])))
                continue;
            if (att[i]->attstorage != 'x' && att[i]->attstorage != 'e')
                continue;
            if (toast_sizes[i] > biggest_size)
            {
                biggest_attno = i;
                biggest_size = toast_sizes[i];
            }
        }

        if (biggest_attno < 0)
            break;

        /*
         * Attempt to compress it inline, if it has attstorage 'x'
         */
        i = biggest_attno;
        if (att[i]->attstorage == 'x')
        {
            old_value = toast_values[i];
            new_value = toast_compress_datum(old_value);

            if (DatumGetPointer(new_value) != NULL)
            {
                /* successful compression */
                if (toast_free[i])
                    pfree(DatumGetPointer(old_value));
                toast_values[i] = new_value;
                toast_free[i] = true;
                toast_sizes[i] = VARSIZE(DatumGetPointer(toast_values[i]));
                need_change = true;
                need_free = true;
            }
            else
            {
                /* incompressible, ignore on subsequent compression passes */
                toast_action[i] = 'x';
            }
        }
        else
        {
            /* has attstorage 'e', ignore on subsequent compression passes */
            toast_action[i] = 'x';
        }

        /*
         * If this value is by itself more than maxDataLen (after compression
         * if any), push it out to the toast table immediately, if possible.
         * This avoids uselessly compressing other fields in the common case
         * where we have one long field and several short ones.
         *
         * XXX maybe the threshold should be less than maxDataLen?
         */
        if (toast_sizes[i] > maxDataLen &&
            rel->rd_rel->reltoastrelid != InvalidOid)
        {
            old_value = toast_values[i];
            toast_action[i] = 'p';
            toast_values[i] = toast_save_datum(rel, toast_values[i],
                                               toast_oldexternal[i], options);
            if (toast_free[i])
                pfree(DatumGetPointer(old_value));
            toast_free[i] = true;
            need_change = true;
            need_free = true;
        }
    }

    /*
     * Second we look for attributes of attstorage 'x' or 'e' that are still
     * inline.  But skip this if there's no toast table to push them to.
     */
    while (heap_compute_data_size(tupleDesc,
                                  toast_values, toast_isnull) > maxDataLen &&
           rel->rd_rel->reltoastrelid != InvalidOid)
    {
        int         biggest_attno = -1;
        int32       biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
        Datum       old_value;

        /*------
         * Search for the biggest yet inlined attribute with
         * attstorage equals 'x' or 'e'
         *------
         */
        for (i = 0; i < numAttrs; i++)
        {
            if (toast_action[i] == 'p')
                continue;
            if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                continue;       /* can't happen, toast_action would be 'p' */
            if (att[i]->attstorage != 'x' && att[i]->attstorage != 'e')
                continue;
            if (toast_sizes[i] > biggest_size)
            {
                biggest_attno = i;
                biggest_size = toast_sizes[i];
            }
        }

        if (biggest_attno < 0)
            break;

        /*
         * Store this external
         */
        i = biggest_attno;
        old_value = toast_values[i];
        toast_action[i] = 'p';
        toast_values[i] = toast_save_datum(rel, toast_values[i],
                                           toast_oldexternal[i], options);
        if (toast_free[i])
            pfree(DatumGetPointer(old_value));
        toast_free[i] = true;

        need_change = true;
        need_free = true;
    }

    /*
     * Round 3 - this time we take attributes with storage 'm' into
     * compression
     */
    while (heap_compute_data_size(tupleDesc,
                                  toast_values, toast_isnull) > maxDataLen)
    {
        int         biggest_attno = -1;
        int32       biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
        Datum       old_value;
        Datum       new_value;

        /*
         * Search for the biggest yet uncompressed internal attribute
         */
        for (i = 0; i < numAttrs; i++)
        {
            if (toast_action[i] != ' ')
                continue;
            if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                continue;       /* can't happen, toast_action would be 'p' */
            if (VARATT_IS_COMPRESSED(DatumGetPointer(toast_values[i])))
                continue;
            if (att[i]->attstorage != 'm')
                continue;
            if (toast_sizes[i] > biggest_size)
            {
                biggest_attno = i;
                biggest_size = toast_sizes[i];
            }
        }

        if (biggest_attno < 0)
            break;

        /*
         * Attempt to compress it inline
         */
        i = biggest_attno;
        old_value = toast_values[i];
        new_value = toast_compress_datum(old_value);

        if (DatumGetPointer(new_value) != NULL)
        {
            /* successful compression */
            if (toast_free[i])
                pfree(DatumGetPointer(old_value));
            toast_values[i] = new_value;
            toast_free[i] = true;
            toast_sizes[i] = VARSIZE(DatumGetPointer(toast_values[i]));
            need_change = true;
            need_free = true;
        }
        else
        {
            /* incompressible, ignore on subsequent compression passes */
            toast_action[i] = 'x';
        }
    }

    /*
     * Finally we store attributes of type 'm' externally.  At this point we
     * increase the target tuple size, so that 'm' attributes aren't stored
     * externally unless really necessary.
     */
    maxDataLen = TOAST_TUPLE_TARGET_MAIN - hoff;

    while (heap_compute_data_size(tupleDesc,
                                  toast_values, toast_isnull) > maxDataLen &&
           rel->rd_rel->reltoastrelid != InvalidOid)
    {
        int         biggest_attno = -1;
        int32       biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
        Datum       old_value;

        /*--------
         * Search for the biggest yet inlined attribute with
         * attstorage = 'm'
         *--------
         */
        for (i = 0; i < numAttrs; i++)
        {
            if (toast_action[i] == 'p')
                continue;
            if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                continue;       /* can't happen, toast_action would be 'p' */
            if (att[i]->attstorage != 'm')
                continue;
            if (toast_sizes[i] > biggest_size)
            {
                biggest_attno = i;
                biggest_size = toast_sizes[i];
            }
        }

        if (biggest_attno < 0)
            break;

        /*
         * Store this external
         */
        i = biggest_attno;
        old_value = toast_values[i];
        toast_action[i] = 'p';
        toast_values[i] = toast_save_datum(rel, toast_values[i],
                                           toast_oldexternal[i], options);
        if (toast_free[i])
            pfree(DatumGetPointer(old_value));
        toast_free[i] = true;

        need_change = true;
        need_free = true;
    }

    /*
     * In the case we toasted any values, we need to build a new heap tuple
     * with the changed values.
     */
    if (need_change)
    {
        HeapTupleHeader olddata = newtup->t_data;
        HeapTupleHeader new_data;
        int32       new_header_len;
        int32       new_data_len;
        int32       new_tuple_len;

        /*
         * Calculate the new size of the tuple.
         *
         * Note: we used to assume here that the old tuple's t_hoff must equal
         * the new_header_len value, but that was incorrect.  The old tuple
         * might have a smaller-than-current natts, if there's been an ALTER
         * TABLE ADD COLUMN since it was stored; and that would lead to a
         * different conclusion about the size of the null bitmap, or even
         * whether there needs to be one at all.
         */
        new_header_len = offsetof(HeapTupleHeaderData, t_bits);
        if (has_nulls)
            new_header_len += BITMAPLEN(numAttrs);
        if (olddata->t_infomask & HEAP_HASOID)
            new_header_len += sizeof(Oid);
        new_header_len = MAXALIGN(new_header_len);
        new_data_len = heap_compute_data_size(tupleDesc,
                                              toast_values, toast_isnull);
        new_tuple_len = new_header_len + new_data_len;

        /*
         * Allocate and zero the space needed, and fill HeapTupleData fields.
         */
        result_tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + new_tuple_len);
        result_tuple->t_len = new_tuple_len;
        result_tuple->t_self = newtup->t_self;
        result_tuple->t_tableOid = newtup->t_tableOid;
        new_data = (HeapTupleHeader) ((char *) result_tuple + HEAPTUPLESIZE);
        result_tuple->t_data = new_data;

        /*
         * Copy the existing tuple header, but adjust natts and t_hoff.
         */
        memcpy(new_data, olddata, offsetof(HeapTupleHeaderData, t_bits));
        HeapTupleHeaderSetNatts(new_data, numAttrs);
        new_data->t_hoff = new_header_len;
        if (olddata->t_infomask & HEAP_HASOID)
            HeapTupleHeaderSetOid(new_data, HeapTupleHeaderGetOid(olddata));

        /* Copy over the data, and fill the null bitmap if needed */
        heap_fill_tuple(tupleDesc,
                        toast_values,
                        toast_isnull,
                        (char *) new_data + new_header_len,
                        new_data_len,
                        &(new_data->t_infomask),
                        has_nulls ? new_data->t_bits : NULL);
    }
    else
        result_tuple = newtup;

    /*
     * Free allocated temp values
     */
    if (need_free)
        for (i = 0; i < numAttrs; i++)
            if (toast_free[i])
                pfree(DatumGetPointer(toast_values[i]));

    /*
     * Delete external values from the old tuple
     */
    if (need_delold)
        for (i = 0; i < numAttrs; i++)
            if (toast_delold[i])
                toast_delete_datum(rel, toast_oldvalues[i]);

    return result_tuple;
}

Size toast_raw_datum_size ( Datum  value  ) 

Definition at line 265 of file tuptoaster.c.

References DatumGetPointer, varatt_external::va_rawsize, VARATT_EXTERNAL_GET_POINTER, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARATT_IS_SHORT, VARHDRSZ_SHORT, VARRAWSIZE_4B_C, VARSIZE, and VARSIZE_SHORT.

Referenced by bpcharoctetlen(), byteaeq(), byteane(), byteaoctetlen(), compute_array_stats(), compute_minimal_stats(), compute_scalar_stats(), text_length(), texteq(), textne(), and textoctetlen().

{
    struct varlena *attr = (struct varlena *) DatumGetPointer(value);
    Size        result;

    if (VARATT_IS_EXTERNAL(attr))
    {
        /* va_rawsize is the size of the original datum -- including header */
        struct varatt_external toast_pointer;

        VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
        result = toast_pointer.va_rawsize;
    }
    else if (VARATT_IS_COMPRESSED(attr))
    {
        /* here, va_rawsize is just the payload size */
        result = VARRAWSIZE_4B_C(attr) + VARHDRSZ;
    }
    else if (VARATT_IS_SHORT(attr))
    {
        /*
         * we have to normalize the header length to VARHDRSZ or else the
         * callers of this function will be confused.
         */
        result = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT + VARHDRSZ;
    }
    else
    {
        /* plain untoasted datum */
        result = VARSIZE(attr);
    }
    return result;
}