Header And Logo

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

Defines | Functions

heaptuple.c File Reference

#include "postgres.h"
#include "access/sysattr.h"
#include "access/tuptoaster.h"
#include "executor/tuptable.h"
Include dependency graph for heaptuple.c:

Go to the source code of this file.

Defines

#define ATT_IS_PACKABLE(att)   ((att)->attlen == -1 && (att)->attstorage != 'p')
#define VARLENA_ATT_IS_PACKABLE(att)   ((att)->attstorage != 'p')

Functions

Size heap_compute_data_size (TupleDesc tupleDesc, Datum *values, bool *isnull)
void heap_fill_tuple (TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
bool heap_attisnull (HeapTuple tup, int attnum)
Datum nocachegetattr (HeapTuple tuple, int attnum, TupleDesc tupleDesc)
Datum heap_getsysattr (HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
HeapTuple heap_copytuple (HeapTuple tuple)
void heap_copytuple_with_tuple (HeapTuple src, HeapTuple dest)
HeapTuple heap_form_tuple (TupleDesc tupleDescriptor, Datum *values, bool *isnull)
HeapTuple heap_formtuple (TupleDesc tupleDescriptor, Datum *values, char *nulls)
HeapTuple heap_modify_tuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
HeapTuple heap_modifytuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, char *replNulls, char *replActions)
void heap_deform_tuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
void heap_deformtuple (HeapTuple tuple, TupleDesc tupleDesc, Datum *values, char *nulls)
static void slot_deform_tuple (TupleTableSlot *slot, int natts)
Datum slot_getattr (TupleTableSlot *slot, int attnum, bool *isnull)
void slot_getallattrs (TupleTableSlot *slot)
void slot_getsomeattrs (TupleTableSlot *slot, int attnum)
bool slot_attisnull (TupleTableSlot *slot, int attnum)
void heap_freetuple (HeapTuple htup)
MinimalTuple heap_form_minimal_tuple (TupleDesc tupleDescriptor, Datum *values, bool *isnull)
void heap_free_minimal_tuple (MinimalTuple mtup)
MinimalTuple heap_copy_minimal_tuple (MinimalTuple mtup)
HeapTuple heap_tuple_from_minimal_tuple (MinimalTuple mtup)
MinimalTuple minimal_tuple_from_heap_tuple (HeapTuple htup)

Define Documentation

#define ATT_IS_PACKABLE (   att  )     ((att)->attlen == -1 && (att)->attstorage != 'p')

Definition at line 66 of file heaptuple.c.

Referenced by heap_compute_data_size().

#define VARLENA_ATT_IS_PACKABLE (   att  )     ((att)->attstorage != 'p')

Definition at line 69 of file heaptuple.c.

Referenced by heap_fill_tuple().


Function Documentation

bool heap_attisnull ( HeapTuple  tup,
int  attnum 
)
Size heap_compute_data_size ( TupleDesc  tupleDesc,
Datum values,
bool isnull 
)

Definition at line 84 of file heaptuple.c.

References att_addlength_datum, att_align_datum, ATT_IS_PACKABLE, tupleDesc::attrs, DatumGetPointer, i, tupleDesc::natts, val, VARATT_CAN_MAKE_SHORT, and VARATT_CONVERTED_SHORT_SIZE.

Referenced by heap_form_minimal_tuple(), heap_form_tuple(), index_form_tuple(), toast_flatten_tuple_attribute(), and toast_insert_or_update().

{
    Size        data_length = 0;
    int         i;
    int         numberOfAttributes = tupleDesc->natts;
    Form_pg_attribute *att = tupleDesc->attrs;

    for (i = 0; i < numberOfAttributes; i++)
    {
        Datum       val;

        if (isnull[i])
            continue;

        val = values[i];

        if (ATT_IS_PACKABLE(att[i]) &&
            VARATT_CAN_MAKE_SHORT(DatumGetPointer(val)))
        {
            /*
             * we're anticipating converting to a short varlena header, so
             * adjust length and don't count any alignment
             */
            data_length += VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val));
        }
        else
        {
            data_length = att_align_datum(data_length, att[i]->attalign,
                                          att[i]->attlen, val);
            data_length = att_addlength_datum(data_length, att[i]->attlen,
                                              val);
        }
    }

    return data_length;
}

MinimalTuple heap_copy_minimal_tuple ( MinimalTuple  mtup  ) 

Definition at line 1487 of file heaptuple.c.

References palloc(), and MinimalTupleData::t_len.

Referenced by ExecCopySlotMinimalTuple(), and tuplestore_gettupleslot().

{
    MinimalTuple result;

    result = (MinimalTuple) palloc(mtup->t_len);
    memcpy(result, mtup, mtup->t_len);
    return result;
}

HeapTuple heap_copytuple ( HeapTuple  tuple  ) 
void heap_copytuple_with_tuple ( HeapTuple  src,
HeapTuple  dest 
)

Definition at line 605 of file heaptuple.c.

References HeapTupleIsValid, NULL, palloc(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by CatalogCacheCreateEntry(), exec_eval_datum(), and SearchCatCacheList().

{
    if (!HeapTupleIsValid(src) || src->t_data == NULL)
    {
        dest->t_data = NULL;
        return;
    }

    dest->t_len = src->t_len;
    dest->t_self = src->t_self;
    dest->t_tableOid = src->t_tableOid;
    dest->t_data = (HeapTupleHeader) palloc(src->t_len);
    memcpy((char *) dest->t_data, (char *) src->t_data, src->t_len);
}

void heap_deform_tuple ( HeapTuple  tuple,
TupleDesc  tupleDesc,
Datum values,
bool isnull 
)

Definition at line 886 of file heaptuple.c.

References att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, tupleDesc::attrs, fetchatt, HeapTupleHasNulls, HeapTupleHeaderGetNatts, Min, tupleDesc::natts, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, and HeapTupleHeaderData::t_hoff.

Referenced by ATRewriteTable(), CopyTo(), do_convert_tuple(), ExecEvalFieldStore(), heap_deformtuple(), heap_modify_tuple(), hstore_from_record(), hstore_populate_record(), json_populate_record(), populate_recordset_object_end(), record_cmp(), record_eq(), record_out(), record_send(), reform_and_rewrite_tuple(), SPI_modifytuple(), toast_delete(), toast_flatten_tuple(), toast_flatten_tuple_attribute(), and toast_insert_or_update().

{
    HeapTupleHeader tup = tuple->t_data;
    bool        hasnulls = HeapTupleHasNulls(tuple);
    Form_pg_attribute *att = tupleDesc->attrs;
    int         tdesc_natts = tupleDesc->natts;
    int         natts;          /* number of atts to extract */
    int         attnum;
    char       *tp;             /* ptr to tuple data */
    long        off;            /* offset in tuple data */
    bits8      *bp = tup->t_bits;       /* ptr to null bitmap in tuple */
    bool        slow = false;   /* can we use/set attcacheoff? */

    natts = HeapTupleHeaderGetNatts(tup);

    /*
     * In inheritance situations, it is possible that the given tuple actually
     * has more fields than the caller is expecting.  Don't run off the end of
     * the caller's arrays.
     */
    natts = Min(natts, tdesc_natts);

    tp = (char *) tup + tup->t_hoff;

    off = 0;

    for (attnum = 0; attnum < natts; attnum++)
    {
        Form_pg_attribute thisatt = att[attnum];

        if (hasnulls && att_isnull(attnum, bp))
        {
            values[attnum] = (Datum) 0;
            isnull[attnum] = true;
            slow = true;        /* can't use attcacheoff anymore */
            continue;
        }

        isnull[attnum] = false;

        if (!slow && thisatt->attcacheoff >= 0)
            off = thisatt->attcacheoff;
        else if (thisatt->attlen == -1)
        {
            /*
             * We can only cache the offset for a varlena attribute if the
             * offset is already suitably aligned, so that there would be no
             * pad bytes in any case: then the offset will be valid for either
             * an aligned or unaligned value.
             */
            if (!slow &&
                off == att_align_nominal(off, thisatt->attalign))
                thisatt->attcacheoff = off;
            else
            {
                off = att_align_pointer(off, thisatt->attalign, -1,
                                        tp + off);
                slow = true;
            }
        }
        else
        {
            /* not varlena, so safe to use att_align_nominal */
            off = att_align_nominal(off, thisatt->attalign);

            if (!slow)
                thisatt->attcacheoff = off;
        }

        values[attnum] = fetchatt(thisatt, tp + off);

        off = att_addlength_pointer(off, thisatt->attlen, tp + off);

        if (thisatt->attlen <= 0)
            slow = true;        /* can't use attcacheoff anymore */
    }

    /*
     * If tuple doesn't have all the atts indicated by tupleDesc, read the
     * rest as null
     */
    for (; attnum < tdesc_natts; attnum++)
    {
        values[attnum] = (Datum) 0;
        isnull[attnum] = true;
    }
}

void heap_deformtuple ( HeapTuple  tuple,
TupleDesc  tupleDesc,
Datum values,
char *  nulls 
)

Definition at line 998 of file heaptuple.c.

References heap_deform_tuple(), tupleDesc::natts, palloc(), and pfree().

{
    int         natts = tupleDesc->natts;
    bool       *boolNulls = (bool *) palloc(natts * sizeof(bool));
    int         attnum;

    heap_deform_tuple(tuple, tupleDesc, values, boolNulls);

    for (attnum = 0; attnum < natts; attnum++)
        nulls[attnum] = (boolNulls[attnum] ? 'n' : ' ');

    pfree(boolNulls);
}

void heap_fill_tuple ( TupleDesc  tupleDesc,
Datum values,
bool isnull,
char *  data,
Size  data_size,
uint16 infomask,
bits8 bit 
)

Definition at line 133 of file heaptuple.c.

References Assert, att_align_nominal, tupleDesc::attrs, DatumGetCString, DatumGetPointer, HEAP_HASNULL, HEAP_HASVARWIDTH, HIGHBIT, i, tupleDesc::natts, NULL, SET_VARSIZE_SHORT, store_att_byval, val, VARATT_CAN_MAKE_SHORT, VARATT_CONVERTED_SHORT_SIZE, VARATT_IS_EXTERNAL, VARATT_IS_SHORT, VARDATA, VARLENA_ATT_IS_PACKABLE, VARSIZE, VARSIZE_EXTERNAL, and VARSIZE_SHORT.

Referenced by heap_form_minimal_tuple(), heap_form_tuple(), index_form_tuple(), toast_flatten_tuple_attribute(), and toast_insert_or_update().

{
    bits8      *bitP;
    int         bitmask;
    int         i;
    int         numberOfAttributes = tupleDesc->natts;
    Form_pg_attribute *att = tupleDesc->attrs;

#ifdef USE_ASSERT_CHECKING
    char       *start = data;
#endif

    if (bit != NULL)
    {
        bitP = &bit[-1];
        bitmask = HIGHBIT;
    }
    else
    {
        /* just to keep compiler quiet */
        bitP = NULL;
        bitmask = 0;
    }

    *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);

    for (i = 0; i < numberOfAttributes; i++)
    {
        Size        data_length;

        if (bit != NULL)
        {
            if (bitmask != HIGHBIT)
                bitmask <<= 1;
            else
            {
                bitP += 1;
                *bitP = 0x0;
                bitmask = 1;
            }

            if (isnull[i])
            {
                *infomask |= HEAP_HASNULL;
                continue;
            }

            *bitP |= bitmask;
        }

        /*
         * XXX we use the att_align macros on the pointer value itself, not on
         * an offset.  This is a bit of a hack.
         */

        if (att[i]->attbyval)
        {
            /* pass-by-value */
            data = (char *) att_align_nominal(data, att[i]->attalign);
            store_att_byval(data, values[i], att[i]->attlen);
            data_length = att[i]->attlen;
        }
        else if (att[i]->attlen == -1)
        {
            /* varlena */
            Pointer     val = DatumGetPointer(values[i]);

            *infomask |= HEAP_HASVARWIDTH;
            if (VARATT_IS_EXTERNAL(val))
            {
                *infomask |= HEAP_HASEXTERNAL;
                /* no alignment, since it's short by definition */
                data_length = VARSIZE_EXTERNAL(val);
                memcpy(data, val, data_length);
            }
            else if (VARATT_IS_SHORT(val))
            {
                /* no alignment for short varlenas */
                data_length = VARSIZE_SHORT(val);
                memcpy(data, val, data_length);
            }
            else if (VARLENA_ATT_IS_PACKABLE(att[i]) &&
                     VARATT_CAN_MAKE_SHORT(val))
            {
                /* convert to short varlena -- no alignment */
                data_length = VARATT_CONVERTED_SHORT_SIZE(val);
                SET_VARSIZE_SHORT(data, data_length);
                memcpy(data + 1, VARDATA(val), data_length - 1);
            }
            else
            {
                /* full 4-byte header varlena */
                data = (char *) att_align_nominal(data,
                                                  att[i]->attalign);
                data_length = VARSIZE(val);
                memcpy(data, val, data_length);
            }
        }
        else if (att[i]->attlen == -2)
        {
            /* cstring ... never needs alignment */
            *infomask |= HEAP_HASVARWIDTH;
            Assert(att[i]->attalign == 'c');
            data_length = strlen(DatumGetCString(values[i])) + 1;
            memcpy(data, DatumGetPointer(values[i]), data_length);
        }
        else
        {
            /* fixed-length pass-by-reference */
            data = (char *) att_align_nominal(data, att[i]->attalign);
            Assert(att[i]->attlen > 0);
            data_length = att[i]->attlen;
            memcpy(data, DatumGetPointer(values[i]), data_length);
        }

        data += data_length;
    }

    Assert((data - start) == data_size);
}

MinimalTuple heap_form_minimal_tuple ( TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 1383 of file heaptuple.c.

References tupleDesc::attrs, BITMAPLEN, ereport, errcode(), errmsg(), ERROR, heap_compute_data_size(), heap_fill_tuple(), HeapTupleHeaderSetNatts, i, MAXALIGN, MaxTupleAttributeNumber, tupleDesc::natts, NULL, offsetof, palloc0(), MinimalTupleData::t_bits, MinimalTupleData::t_hoff, MinimalTupleData::t_infomask, MinimalTupleData::t_len, tupleDesc::tdhasoid, toast_flatten_tuple_attribute(), and VARATT_IS_EXTENDED.

Referenced by ExecCopySlotMinimalTuple(), and tuplestore_putvalues().

{
    MinimalTuple tuple;         /* return tuple */
    Size        len,
                data_len;
    int         hoff;
    bool        hasnull = false;
    Form_pg_attribute *att = tupleDescriptor->attrs;
    int         numberOfAttributes = tupleDescriptor->natts;
    int         i;

    if (numberOfAttributes > MaxTupleAttributeNumber)
        ereport(ERROR,
                (errcode(ERRCODE_TOO_MANY_COLUMNS),
                 errmsg("number of columns (%d) exceeds limit (%d)",
                        numberOfAttributes, MaxTupleAttributeNumber)));

    /*
     * Check for nulls and embedded tuples; expand any toasted attributes in
     * embedded tuples.  This preserves the invariant that toasting can only
     * go one level deep.
     *
     * We can skip calling toast_flatten_tuple_attribute() if the attribute
     * couldn't possibly be of composite type.  All composite datums are
     * varlena and have alignment 'd'; furthermore they aren't arrays. Also,
     * if an attribute is already toasted, it must have been sent to disk
     * already and so cannot contain toasted attributes.
     */
    for (i = 0; i < numberOfAttributes; i++)
    {
        if (isnull[i])
            hasnull = true;
        else if (att[i]->attlen == -1 &&
                 att[i]->attalign == 'd' &&
                 att[i]->attndims == 0 &&
                 !VARATT_IS_EXTENDED(values[i]))
        {
            values[i] = toast_flatten_tuple_attribute(values[i],
                                                      att[i]->atttypid,
                                                      att[i]->atttypmod);
        }
    }

    /*
     * Determine total space needed
     */
    len = offsetof(MinimalTupleData, t_bits);

    if (hasnull)
        len += BITMAPLEN(numberOfAttributes);

    if (tupleDescriptor->tdhasoid)
        len += sizeof(Oid);

    hoff = len = MAXALIGN(len); /* align user data safely */

    data_len = heap_compute_data_size(tupleDescriptor, values, isnull);

    len += data_len;

    /*
     * Allocate and zero the space needed.
     */
    tuple = (MinimalTuple) palloc0(len);

    /*
     * And fill in the information.
     */
    tuple->t_len = len;
    HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
    tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;

    if (tupleDescriptor->tdhasoid)      /* else leave infomask = 0 */
        tuple->t_infomask = HEAP_HASOID;

    heap_fill_tuple(tupleDescriptor,
                    values,
                    isnull,
                    (char *) tuple + hoff,
                    data_len,
                    &tuple->t_infomask,
                    (hasnull ? tuple->t_bits : NULL));

    return tuple;
}

HeapTuple heap_form_tuple ( TupleDesc  tupleDescriptor,
Datum values,
bool isnull 
)

Definition at line 628 of file heaptuple.c.

References tupleDesc::attrs, BITMAPLEN, DatumGetPointer, ereport, errcode(), errmsg(), ERROR, heap_compute_data_size(), heap_fill_tuple(), HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetNatts, HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, HEAPTUPLESIZE, i, ItemPointerSetInvalid, MAXALIGN, MaxTupleAttributeNumber, tupleDesc::natts, NULL, offsetof, palloc0(), HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, tupleDesc::tdhasoid, tupleDesc::tdtypeid, tupleDesc::tdtypmod, toast_flatten_tuple_attribute(), and VARATT_IS_EXTENDED.

Referenced by aclexplode(), AddEnumLabel(), AddRoleMems(), AggregateCreate(), AlterSetting(), ATRewriteTable(), build_dummy_tuple(), BuildTupleFromCStrings(), CollationCreate(), ConversionCreate(), CopyFrom(), create_proc_lang(), CreateCast(), CreateComments(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreateRole(), CreateSharedComments(), CreateTableSpace(), CreateTrigger(), CreateUserMapping(), DefineOpClass(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), do_convert_tuple(), each_object_field_end(), elements_array_element_end(), EnumValuesCreate(), exec_move_row(), ExecCopySlotTuple(), ExecEvalFieldStore(), ExecEvalRow(), file_acquire_sample_rows(), heap_formtuple(), heap_modify_tuple(), heap_page_items(), hstore_each(), hstore_populate_record(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertOneTuple(), InsertPgAttributeTuple(), InsertPgClassTuple(), InsertRule(), inv_truncate(), inv_write(), json_populate_record(), LargeObjectCreate(), make_tuple_from_result_row(), make_tuple_from_row(), MakeConfigurationMapping(), NamespaceCreate(), OperatorCreate(), OperatorShellMake(), page_header(), pg_buffercache_pages(), pg_identify_object(), pg_lock_status(), pg_prepared_xact(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_activity(), pg_timezone_abbrevs(), pg_timezone_names(), pg_xlogfile_name_offset(), pgstatginindex(), plperl_build_tuple_result(), PLyGenericObject_ToComposite(), PLyMapping_ToComposite(), PLySequence_ToComposite(), populate_recordset_object_end(), ProcedureCreate(), RangeCreate(), record_in(), record_recv(), recordMultipleDependencies(), reform_and_rewrite_tuple(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepAddDependency(), shdepChangeDep(), SPI_modifytuple(), StoreAttrDefault(), StoreCatalogInheritance1(), storeOperators(), storeProcedures(), toast_flatten_tuple(), toast_save_datum(), TypeCreate(), TypeShellMake(), update_attstats(), and UpdateIndexRelation().

{
    HeapTuple   tuple;          /* return tuple */
    HeapTupleHeader td;         /* tuple data */
    Size        len,
                data_len;
    int         hoff;
    bool        hasnull = false;
    Form_pg_attribute *att = tupleDescriptor->attrs;
    int         numberOfAttributes = tupleDescriptor->natts;
    int         i;

    if (numberOfAttributes > MaxTupleAttributeNumber)
        ereport(ERROR,
                (errcode(ERRCODE_TOO_MANY_COLUMNS),
                 errmsg("number of columns (%d) exceeds limit (%d)",
                        numberOfAttributes, MaxTupleAttributeNumber)));

    /*
     * Check for nulls and embedded tuples; expand any toasted attributes in
     * embedded tuples.  This preserves the invariant that toasting can only
     * go one level deep.
     *
     * We can skip calling toast_flatten_tuple_attribute() if the attribute
     * couldn't possibly be of composite type.  All composite datums are
     * varlena and have alignment 'd'; furthermore they aren't arrays. Also,
     * if an attribute is already toasted, it must have been sent to disk
     * already and so cannot contain toasted attributes.
     */
    for (i = 0; i < numberOfAttributes; i++)
    {
        if (isnull[i])
            hasnull = true;
        else if (att[i]->attlen == -1 &&
                 att[i]->attalign == 'd' &&
                 att[i]->attndims == 0 &&
                 !VARATT_IS_EXTENDED(DatumGetPointer(values[i])))
        {
            values[i] = toast_flatten_tuple_attribute(values[i],
                                                      att[i]->atttypid,
                                                      att[i]->atttypmod);
        }
    }

    /*
     * Determine total space needed
     */
    len = offsetof(HeapTupleHeaderData, t_bits);

    if (hasnull)
        len += BITMAPLEN(numberOfAttributes);

    if (tupleDescriptor->tdhasoid)
        len += sizeof(Oid);

    hoff = len = MAXALIGN(len); /* align user data safely */

    data_len = heap_compute_data_size(tupleDescriptor, values, isnull);

    len += data_len;

    /*
     * Allocate and zero the space needed.  Note that the tuple body and
     * HeapTupleData management structure are allocated in one chunk.
     */
    tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
    tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);

    /*
     * And fill in the information.  Note we fill the Datum fields even though
     * this tuple may never become a Datum.
     */
    tuple->t_len = len;
    ItemPointerSetInvalid(&(tuple->t_self));
    tuple->t_tableOid = InvalidOid;

    HeapTupleHeaderSetDatumLength(td, len);
    HeapTupleHeaderSetTypeId(td, tupleDescriptor->tdtypeid);
    HeapTupleHeaderSetTypMod(td, tupleDescriptor->tdtypmod);

    HeapTupleHeaderSetNatts(td, numberOfAttributes);
    td->t_hoff = hoff;

    if (tupleDescriptor->tdhasoid)      /* else leave infomask = 0 */
        td->t_infomask = HEAP_HASOID;

    heap_fill_tuple(tupleDescriptor,
                    values,
                    isnull,
                    (char *) td + hoff,
                    data_len,
                    &td->t_infomask,
                    (hasnull ? td->t_bits : NULL));

    return tuple;
}

HeapTuple heap_formtuple ( TupleDesc  tupleDescriptor,
Datum values,
char *  nulls 
)

Definition at line 740 of file heaptuple.c.

References heap_form_tuple(), i, tupleDesc::natts, palloc(), and pfree().

{
    HeapTuple   tuple;          /* return tuple */
    int         numberOfAttributes = tupleDescriptor->natts;
    bool       *boolNulls = (bool *) palloc(numberOfAttributes * sizeof(bool));
    int         i;

    for (i = 0; i < numberOfAttributes; i++)
        boolNulls[i] = (nulls[i] == 'n');

    tuple = heap_form_tuple(tupleDescriptor, values, boolNulls);

    pfree(boolNulls);

    return tuple;
}

void heap_free_minimal_tuple ( MinimalTuple  mtup  ) 

Definition at line 1475 of file heaptuple.c.

References pfree().

Referenced by ExecClearTuple(), ExecStoreMinimalTuple(), ExecStoreTuple(), and writetup_heap().

{
    pfree(mtup);
}

void heap_freetuple ( HeapTuple  htup  ) 

Definition at line 1365 of file heaptuple.c.

References pfree().

Referenced by acquire_inherited_sample_rows(), acquire_sample_rows(), AddEnumLabel(), AfterTriggerExecute(), AlterDatabaseOwner(), AlterDomainDefault(), AlterDomainNotNull(), AlterDomainValidateConstraint(), AlterEventTrigger(), AlterEventTriggerOwner(), AlterEventTriggerOwner_oid(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner(), AlterForeignDataWrapperOwner_oid(), AlterForeignServer(), AlterForeignServerOwner(), AlterForeignServerOwner_oid(), AlterFunction(), AlterObjectRename_internal(), AlterRelationNamespaceInternal(), AlterRole(), AlterSchemaOwner_internal(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterUserMapping(), analyze_row_processor(), ATExecAddColumn(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecChangeOwner(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropInherit(), ATExecDropOf(), ATExecGenericOptions(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), ATExecSetStorage(), ATExecSetTableSpace(), ATExecValidateConstraint(), build_tuplestore_recursively(), CatalogCacheCreateEntry(), change_owner_fix_column_acls(), changeDependencyFor(), CollationCreate(), ConversionCreate(), copy_heap_data(), copyTemplateDependencies(), create_toast_table(), CreateCast(), CreateComments(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreateSharedComments(), CreateTableSpace(), CreateTrigger(), CreateUserMapping(), crosstab(), DefineOpClass(), DefineQueryRewrite(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), EnableDisableRule(), EnableDisableTrigger(), EnumValuesCreate(), EvalPlanQualSetTuple(), examine_attribute(), exec_assign_value(), exec_move_row(), exec_stmt_block(), exec_stmt_return_next(), exec_stmt_return_query(), ExecARDeleteTriggers(), ExecARUpdateTriggers(), ExecBRDeleteTriggers(), ExecBRInsertTriggers(), ExecBRUpdateTriggers(), ExecClearTuple(), ExecIRDeleteTriggers(), ExecIRInsertTriggers(), ExecIRUpdateTriggers(), ExecReScanAgg(), ExecReScanSetOp(), ExecScanSubPlan(), ExecSetParamPlan(), ExecStoreMinimalTuple(), ExecStoreTuple(), file_acquire_sample_rows(), heap_insert(), heap_update(), index_build(), index_constraint_create(), index_update_stats(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertOneTuple(), InsertPgAttributeTuple(), InsertPgClassTuple(), InsertRule(), inv_truncate(), inv_write(), LargeObjectCreate(), MakeConfigurationMapping(), mark_index_clustered(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), OperatorShellMake(), ProcedureCreate(), RangeCreate(), raw_heap_insert(), record_in(), record_recv(), recordMultipleDependencies(), reform_and_rewrite_tuple(), RelationBuildDesc(), RelationReloadIndexInfo(), RelationSetNewRelfilenode(), RemoveConstraintById(), renameatt_internal(), RenameConstraintById(), RenameRelationInternal(), RenameRewriteRule(), RenameSchema(), RenameTypeInternal(), RenumberEnumType(), rewrite_heap_dead_tuple(), rewrite_heap_tuple(), SearchCatCache(), SearchCatCacheList(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepAddDependency(), shdepChangeDep(), SPI_freetuple(), StoreAttrDefault(), StoreCatalogInheritance1(), storeOperators(), storeProcedures(), swap_relation_files(), table_recheck_autovac(), toast_save_datum(), TypeShellMake(), update_attstats(), UpdateIndexRelation(), vac_update_datfrozenxid(), writetup_cluster(), and xpath_table().

{
    pfree(htup);
}

Datum heap_getsysattr ( HeapTuple  tup,
int  attnum,
TupleDesc  tupleDesc,
bool isnull 
)

Definition at line 523 of file heaptuple.c.

References Assert, CommandIdGetDatum, elog, ERROR, HeapTupleGetOid, HeapTupleHeaderGetRawCommandId, HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetXmin, MaxCommandIdAttributeNumber, MaxTransactionIdAttributeNumber, MinCommandIdAttributeNumber, MinTransactionIdAttributeNumber, ObjectIdAttributeNumber, ObjectIdGetDatum, PointerGetDatum, SelfItemPointerAttributeNumber, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, TableOidAttributeNumber, and TransactionIdGetDatum.

Referenced by slot_getattr().

{
    Datum       result;

    Assert(tup);

    /* Currently, no sys attribute ever reads as NULL. */
    *isnull = false;

    switch (attnum)
    {
        case SelfItemPointerAttributeNumber:
            /* pass-by-reference datatype */
            result = PointerGetDatum(&(tup->t_self));
            break;
        case ObjectIdAttributeNumber:
            result = ObjectIdGetDatum(HeapTupleGetOid(tup));
            break;
        case MinTransactionIdAttributeNumber:
            result = TransactionIdGetDatum(HeapTupleHeaderGetXmin(tup->t_data));
            break;
        case MaxTransactionIdAttributeNumber:
            result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmax(tup->t_data));
            break;
        case MinCommandIdAttributeNumber:
        case MaxCommandIdAttributeNumber:

            /*
             * cmin and cmax are now both aliases for the same field, which
             * can in fact also be a combo command id.  XXX perhaps we should
             * return the "real" cmin or cmax if possible, that is if we are
             * inside the originating transaction?
             */
            result = CommandIdGetDatum(HeapTupleHeaderGetRawCommandId(tup->t_data));
            break;
        case TableOidAttributeNumber:
            result = ObjectIdGetDatum(tup->t_tableOid);
            break;
        default:
            elog(ERROR, "invalid attnum: %d", attnum);
            result = 0;         /* keep compiler quiet */
            break;
    }
    return result;
}

HeapTuple heap_modify_tuple ( HeapTuple  tuple,
TupleDesc  tupleDesc,
Datum replValues,
bool replIsnull,
bool doReplace 
)

Definition at line 772 of file heaptuple.c.

References heap_deform_tuple(), heap_form_tuple(), HeapTupleGetOid, HeapTupleSetOid, tupleDesc::natts, palloc(), pfree(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, tupleDesc::tdhasoid, and values.

Referenced by AddRoleMems(), AlterDatabase(), AlterDatabaseOwner(), AlterDomainDefault(), AlterForeignDataWrapper(), AlterForeignServer(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterRole(), AlterSchemaOwner_internal(), AlterSetting(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterUserMapping(), ApplyExtensionUpdates(), ATExecAlterColumnGenericOptions(), ATExecChangeOwner(), ATExecGenericOptions(), ATExecSetOptions(), ATExecSetRelOptions(), change_owner_fix_column_acls(), copyTemplateDependencies(), create_proc_lang(), CreateComments(), CreateSharedComments(), DelRoleMems(), exec_assign_value(), ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), extension_config_remove(), heap_modifytuple(), InsertRule(), inv_truncate(), inv_write(), MakeConfigurationMapping(), movedb(), OperatorCreate(), OperatorUpd(), pg_extension_config_dump(), ProcedureCreate(), RenameRole(), SetDefaultACL(), SetSecurityLabel(), SetSharedSecurityLabel(), TypeCreate(), and update_attstats().

{
    int         numberOfAttributes = tupleDesc->natts;
    int         attoff;
    Datum      *values;
    bool       *isnull;
    HeapTuple   newTuple;

    /*
     * allocate and fill values and isnull arrays from either the tuple or the
     * repl information, as appropriate.
     *
     * NOTE: it's debatable whether to use heap_deform_tuple() here or just
     * heap_getattr() only the non-replaced colums.  The latter could win if
     * there are many replaced columns and few non-replaced ones. However,
     * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
     * O(N^2) if there are many non-replaced columns, so it seems better to
     * err on the side of linear cost.
     */
    values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
    isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));

    heap_deform_tuple(tuple, tupleDesc, values, isnull);

    for (attoff = 0; attoff < numberOfAttributes; attoff++)
    {
        if (doReplace[attoff])
        {
            values[attoff] = replValues[attoff];
            isnull[attoff] = replIsnull[attoff];
        }
    }

    /*
     * create a new tuple from the values and isnull arrays
     */
    newTuple = heap_form_tuple(tupleDesc, values, isnull);

    pfree(values);
    pfree(isnull);

    /*
     * copy the identification info of the old tuple: t_ctid, t_self, and OID
     * (if any)
     */
    newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
    newTuple->t_self = tuple->t_self;
    newTuple->t_tableOid = tuple->t_tableOid;
    if (tupleDesc->tdhasoid)
        HeapTupleSetOid(newTuple, HeapTupleGetOid(tuple));

    return newTuple;
}

HeapTuple heap_modifytuple ( HeapTuple  tuple,
TupleDesc  tupleDesc,
Datum replValues,
char *  replNulls,
char *  replActions 
)

Definition at line 842 of file heaptuple.c.

References heap_modify_tuple(), tupleDesc::natts, palloc(), and pfree().

{
    HeapTuple   result;
    int         numberOfAttributes = tupleDesc->natts;
    bool       *boolNulls = (bool *) palloc(numberOfAttributes * sizeof(bool));
    bool       *boolActions = (bool *) palloc(numberOfAttributes * sizeof(bool));
    int         attnum;

    for (attnum = 0; attnum < numberOfAttributes; attnum++)
    {
        boolNulls[attnum] = (replNulls[attnum] == 'n');
        boolActions[attnum] = (replActions[attnum] == 'r');
    }

    result = heap_modify_tuple(tuple, tupleDesc, replValues, boolNulls, boolActions);

    pfree(boolNulls);
    pfree(boolActions);

    return result;
}

HeapTuple heap_tuple_from_minimal_tuple ( MinimalTuple  mtup  ) 

Definition at line 1506 of file heaptuple.c.

References HEAPTUPLESIZE, ItemPointerSetInvalid, MINIMAL_TUPLE_OFFSET, offsetof, palloc(), HeapTupleData::t_data, HeapTupleData::t_len, MinimalTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by ExecCopySlotTuple().

{
    HeapTuple   result;
    uint32      len = mtup->t_len + MINIMAL_TUPLE_OFFSET;

    result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
    result->t_len = len;
    ItemPointerSetInvalid(&(result->t_self));
    result->t_tableOid = InvalidOid;
    result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
    memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
    memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
    return result;
}

MinimalTuple minimal_tuple_from_heap_tuple ( HeapTuple  htup  ) 

Definition at line 1528 of file heaptuple.c.

References Assert, MINIMAL_TUPLE_OFFSET, palloc(), HeapTupleData::t_data, MinimalTupleData::t_len, and HeapTupleData::t_len.

Referenced by copytup_heap(), and ExecCopySlotMinimalTuple().

{
    MinimalTuple result;
    uint32      len;

    Assert(htup->t_len > MINIMAL_TUPLE_OFFSET);
    len = htup->t_len - MINIMAL_TUPLE_OFFSET;
    result = (MinimalTuple) palloc(len);
    memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
    result->t_len = len;
    return result;
}

Datum nocachegetattr ( HeapTuple  tuple,
int  attnum,
TupleDesc  tupleDesc 
)

Definition at line 323 of file heaptuple.c.

References Assert, att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, tupleDesc::attrs, byte, fetchatt, HeapTupleHasNulls, HeapTupleHasVarWidth, HeapTupleNoNulls, i, tupleDesc::natts, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, and HeapTupleHeaderData::t_hoff.

{
    HeapTupleHeader tup = tuple->t_data;
    Form_pg_attribute *att = tupleDesc->attrs;
    char       *tp;             /* ptr to data part of tuple */
    bits8      *bp = tup->t_bits;       /* ptr to null bitmap in tuple */
    bool        slow = false;   /* do we have to walk attrs? */
    int         off;            /* current offset within data */

    /* ----------------
     *   Three cases:
     *
     *   1: No nulls and no variable-width attributes.
     *   2: Has a null or a var-width AFTER att.
     *   3: Has nulls or var-widths BEFORE att.
     * ----------------
     */

    attnum--;

    if (!HeapTupleNoNulls(tuple))
    {
        /*
         * there's a null somewhere in the tuple
         *
         * check to see if any preceding bits are null...
         */
        int         byte = attnum >> 3;
        int         finalbit = attnum & 0x07;

        /* check for nulls "before" final bit of last byte */
        if ((~bp[byte]) & ((1 << finalbit) - 1))
            slow = true;
        else
        {
            /* check for nulls in any "earlier" bytes */
            int         i;

            for (i = 0; i < byte; i++)
            {
                if (bp[i] != 0xFF)
                {
                    slow = true;
                    break;
                }
            }
        }
    }

    tp = (char *) tup + tup->t_hoff;

    if (!slow)
    {
        /*
         * If we get here, there are no nulls up to and including the target
         * attribute.  If we have a cached offset, we can use it.
         */
        if (att[attnum]->attcacheoff >= 0)
        {
            return fetchatt(att[attnum],
                            tp + att[attnum]->attcacheoff);
        }

        /*
         * Otherwise, check for non-fixed-length attrs up to and including
         * target.  If there aren't any, it's safe to cheaply initialize the
         * cached offsets for these attrs.
         */
        if (HeapTupleHasVarWidth(tuple))
        {
            int         j;

            for (j = 0; j <= attnum; j++)
            {
                if (att[j]->attlen <= 0)
                {
                    slow = true;
                    break;
                }
            }
        }
    }

    if (!slow)
    {
        int         natts = tupleDesc->natts;
        int         j = 1;

        /*
         * If we get here, we have a tuple with no nulls or var-widths up to
         * and including the target attribute, so we can use the cached offset
         * ... only we don't have it yet, or we'd not have got here.  Since
         * it's cheap to compute offsets for fixed-width columns, we take the
         * opportunity to initialize the cached offsets for *all* the leading
         * fixed-width columns, in hope of avoiding future visits to this
         * routine.
         */
        att[0]->attcacheoff = 0;

        /* we might have set some offsets in the slow path previously */
        while (j < natts && att[j]->attcacheoff > 0)
            j++;

        off = att[j - 1]->attcacheoff + att[j - 1]->attlen;

        for (; j < natts; j++)
        {
            if (att[j]->attlen <= 0)
                break;

            off = att_align_nominal(off, att[j]->attalign);

            att[j]->attcacheoff = off;

            off += att[j]->attlen;
        }

        Assert(j > attnum);

        off = att[attnum]->attcacheoff;
    }
    else
    {
        bool        usecache = true;
        int         i;

        /*
         * Now we know that we have to walk the tuple CAREFULLY.  But we still
         * might be able to cache some offsets for next time.
         *
         * Note - This loop is a little tricky.  For each non-null attribute,
         * we have to first account for alignment padding before the attr,
         * then advance over the attr based on its length.  Nulls have no
         * storage and no alignment padding either.  We can use/set
         * attcacheoff until we reach either a null or a var-width attribute.
         */
        off = 0;
        for (i = 0;; i++)       /* loop exit is at "break" */
        {
            if (HeapTupleHasNulls(tuple) && att_isnull(i, bp))
            {
                usecache = false;
                continue;       /* this cannot be the target att */
            }

            /* If we know the next offset, we can skip the rest */
            if (usecache && att[i]->attcacheoff >= 0)
                off = att[i]->attcacheoff;
            else if (att[i]->attlen == -1)
            {
                /*
                 * We can only cache the offset for a varlena attribute if the
                 * offset is already suitably aligned, so that there would be
                 * no pad bytes in any case: then the offset will be valid for
                 * either an aligned or unaligned value.
                 */
                if (usecache &&
                    off == att_align_nominal(off, att[i]->attalign))
                    att[i]->attcacheoff = off;
                else
                {
                    off = att_align_pointer(off, att[i]->attalign, -1,
                                            tp + off);
                    usecache = false;
                }
            }
            else
            {
                /* not varlena, so safe to use att_align_nominal */
                off = att_align_nominal(off, att[i]->attalign);

                if (usecache)
                    att[i]->attcacheoff = off;
            }

            if (i == attnum)
                break;

            off = att_addlength_pointer(off, att[i]->attlen, tp + off);

            if (usecache && att[i]->attlen <= 0)
                usecache = false;
        }
    }

    return fetchatt(att[attnum], tp + off);
}

bool slot_attisnull ( TupleTableSlot slot,
int  attnum 
)

Definition at line 1321 of file heaptuple.c.

References elog, ERROR, heap_attisnull(), tupleDesc::natts, NULL, TupleTableSlot::tts_isnull, TupleTableSlot::tts_minhdr, TupleTableSlot::tts_tuple, and TupleTableSlot::tts_tupleDescriptor.

Referenced by ExecConstraints(), slotAllNulls(), and slotNoNulls().

{
    HeapTuple   tuple = slot->tts_tuple;
    TupleDesc   tupleDesc = slot->tts_tupleDescriptor;

    /*
     * system attributes are handled by heap_attisnull
     */
    if (attnum <= 0)
    {
        if (tuple == NULL)      /* internal error */
            elog(ERROR, "cannot extract system attribute from virtual tuple");
        if (tuple == &(slot->tts_minhdr))       /* internal error */
            elog(ERROR, "cannot extract system attribute from minimal tuple");
        return heap_attisnull(tuple, attnum);
    }

    /*
     * fast path if desired attribute already cached
     */
    if (attnum <= slot->tts_nvalid)
        return slot->tts_isnull[attnum - 1];

    /*
     * return NULL if attnum is out of range according to the tupdesc
     */
    if (attnum > tupleDesc->natts)
        return true;

    /*
     * otherwise we had better have a physical tuple (tts_nvalid should equal
     * natts in all virtual-tuple cases)
     */
    if (tuple == NULL)          /* internal error */
        elog(ERROR, "cannot extract attribute from empty tuple slot");

    /* and let the tuple tell it */
    return heap_attisnull(tuple, attnum);
}

static void slot_deform_tuple ( TupleTableSlot slot,
int  natts 
) [static]

Definition at line 1027 of file heaptuple.c.

References att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, tupleDesc::attrs, fetchatt, HeapTupleHasNulls, HeapTupleHeaderData::t_bits, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, TupleTableSlot::tts_isnull, TupleTableSlot::tts_nvalid, TupleTableSlot::tts_off, TupleTableSlot::tts_slow, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, and values.

Referenced by slot_getallattrs(), slot_getattr(), and slot_getsomeattrs().

{
    HeapTuple   tuple = slot->tts_tuple;
    TupleDesc   tupleDesc = slot->tts_tupleDescriptor;
    Datum      *values = slot->tts_values;
    bool       *isnull = slot->tts_isnull;
    HeapTupleHeader tup = tuple->t_data;
    bool        hasnulls = HeapTupleHasNulls(tuple);
    Form_pg_attribute *att = tupleDesc->attrs;
    int         attnum;
    char       *tp;             /* ptr to tuple data */
    long        off;            /* offset in tuple data */
    bits8      *bp = tup->t_bits;       /* ptr to null bitmap in tuple */
    bool        slow;           /* can we use/set attcacheoff? */

    /*
     * Check whether the first call for this tuple, and initialize or restore
     * loop state.
     */
    attnum = slot->tts_nvalid;
    if (attnum == 0)
    {
        /* Start from the first attribute */
        off = 0;
        slow = false;
    }
    else
    {
        /* Restore state from previous execution */
        off = slot->tts_off;
        slow = slot->tts_slow;
    }

    tp = (char *) tup + tup->t_hoff;

    for (; attnum < natts; attnum++)
    {
        Form_pg_attribute thisatt = att[attnum];

        if (hasnulls && att_isnull(attnum, bp))
        {
            values[attnum] = (Datum) 0;
            isnull[attnum] = true;
            slow = true;        /* can't use attcacheoff anymore */
            continue;
        }

        isnull[attnum] = false;

        if (!slow && thisatt->attcacheoff >= 0)
            off = thisatt->attcacheoff;
        else if (thisatt->attlen == -1)
        {
            /*
             * We can only cache the offset for a varlena attribute if the
             * offset is already suitably aligned, so that there would be no
             * pad bytes in any case: then the offset will be valid for either
             * an aligned or unaligned value.
             */
            if (!slow &&
                off == att_align_nominal(off, thisatt->attalign))
                thisatt->attcacheoff = off;
            else
            {
                off = att_align_pointer(off, thisatt->attalign, -1,
                                        tp + off);
                slow = true;
            }
        }
        else
        {
            /* not varlena, so safe to use att_align_nominal */
            off = att_align_nominal(off, thisatt->attalign);

            if (!slow)
                thisatt->attcacheoff = off;
        }

        values[attnum] = fetchatt(thisatt, tp + off);

        off = att_addlength_pointer(off, thisatt->attlen, tp + off);

        if (thisatt->attlen <= 0)
            slow = true;        /* can't use attcacheoff anymore */
    }

    /*
     * Save state for next execution
     */
    slot->tts_nvalid = attnum;
    slot->tts_off = off;
    slot->tts_slow = slow;
}

void slot_getallattrs ( TupleTableSlot slot  ) 

Definition at line 1230 of file heaptuple.c.

References elog, ERROR, HeapTupleHeaderGetNatts, Min, tupleDesc::natts, NULL, slot_deform_tuple(), HeapTupleData::t_data, TupleTableSlot::tts_isnull, TupleTableSlot::tts_nvalid, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by copy_dest_receive(), ExecBuildSlotValueDescription(), ExecFilterJunk(), printtup(), printtup_20(), printtup_internal_20(), and tstoreReceiveSlot_detoast().

{
    int         tdesc_natts = slot->tts_tupleDescriptor->natts;
    int         attnum;
    HeapTuple   tuple;

    /* Quick out if we have 'em all already */
    if (slot->tts_nvalid == tdesc_natts)
        return;

    /*
     * otherwise we had better have a physical tuple (tts_nvalid should equal
     * natts in all virtual-tuple cases)
     */
    tuple = slot->tts_tuple;
    if (tuple == NULL)          /* internal error */
        elog(ERROR, "cannot extract attribute from empty tuple slot");

    /*
     * load up any slots available from physical tuple
     */
    attnum = HeapTupleHeaderGetNatts(tuple->t_data);
    attnum = Min(attnum, tdesc_natts);

    slot_deform_tuple(slot, attnum);

    /*
     * If tuple doesn't have all the atts indicated by tupleDesc, read the
     * rest as null
     */
    for (; attnum < tdesc_natts; attnum++)
    {
        slot->tts_values[attnum] = (Datum) 0;
        slot->tts_isnull[attnum] = true;
    }
    slot->tts_nvalid = tdesc_natts;
}

Datum slot_getattr ( TupleTableSlot slot,
int  attnum,
bool isnull 
)

Definition at line 1134 of file heaptuple.c.

References att_isnull, tupleDesc::attrs, elog, ERROR, heap_getsysattr(), HeapTupleHasNulls, HeapTupleHeaderGetNatts, tupleDesc::natts, NULL, slot_deform_tuple(), HeapTupleHeaderData::t_bits, HeapTupleData::t_data, TupleTableSlot::tts_isnull, TupleTableSlot::tts_minhdr, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by buildSubPlanHash(), convert_prep_stmt_params(), debugtup(), execCurrentOf(), ExecEvalScalarVar(), ExecEvalScalarVarFast(), ExecGetJunkAttribute(), ExecMakeFunctionResult(), ExecNestLoop(), ExecScanSubPlan(), ExecSetParamPlan(), execTuplesMatch(), execTuplesUnequal(), fetch_tuple_flag(), FormIndexDatum(), heap_compare_slots(), postquel_get_single_result(), and TupleHashTableHash().

{
    HeapTuple   tuple = slot->tts_tuple;
    TupleDesc   tupleDesc = slot->tts_tupleDescriptor;
    HeapTupleHeader tup;

    /*
     * system attributes are handled by heap_getsysattr
     */
    if (attnum <= 0)
    {
        if (tuple == NULL)      /* internal error */
            elog(ERROR, "cannot extract system attribute from virtual tuple");
        if (tuple == &(slot->tts_minhdr))       /* internal error */
            elog(ERROR, "cannot extract system attribute from minimal tuple");
        return heap_getsysattr(tuple, attnum, tupleDesc, isnull);
    }

    /*
     * fast path if desired attribute already cached
     */
    if (attnum <= slot->tts_nvalid)
    {
        *isnull = slot->tts_isnull[attnum - 1];
        return slot->tts_values[attnum - 1];
    }

    /*
     * return NULL if attnum is out of range according to the tupdesc
     */
    if (attnum > tupleDesc->natts)
    {
        *isnull = true;
        return (Datum) 0;
    }

    /*
     * otherwise we had better have a physical tuple (tts_nvalid should equal
     * natts in all virtual-tuple cases)
     */
    if (tuple == NULL)          /* internal error */
        elog(ERROR, "cannot extract attribute from empty tuple slot");

    /*
     * return NULL if attnum is out of range according to the tuple
     *
     * (We have to check this separately because of various inheritance and
     * table-alteration scenarios: the tuple could be either longer or shorter
     * than the tupdesc.)
     */
    tup = tuple->t_data;
    if (attnum > HeapTupleHeaderGetNatts(tup))
    {
        *isnull = true;
        return (Datum) 0;
    }

    /*
     * check if target attribute is null: no point in groveling through tuple
     */
    if (HeapTupleHasNulls(tuple) && att_isnull(attnum - 1, tup->t_bits))
    {
        *isnull = true;
        return (Datum) 0;
    }

    /*
     * If the attribute's column has been dropped, we force a NULL result.
     * This case should not happen in normal use, but it could happen if we
     * are executing a plan cached before the column was dropped.
     */
    if (tupleDesc->attrs[attnum - 1]->attisdropped)
    {
        *isnull = true;
        return (Datum) 0;
    }

    /*
     * Extract the attribute, along with any preceding attributes.
     */
    slot_deform_tuple(slot, attnum);

    /*
     * The result is acquired from tts_values array.
     */
    *isnull = slot->tts_isnull[attnum - 1];
    return slot->tts_values[attnum - 1];
}

void slot_getsomeattrs ( TupleTableSlot slot,
int  attnum 
)

Definition at line 1274 of file heaptuple.c.

References elog, ERROR, HeapTupleHeaderGetNatts, Min, tupleDesc::natts, NULL, slot_deform_tuple(), HeapTupleData::t_data, TupleTableSlot::tts_isnull, TupleTableSlot::tts_nvalid, TupleTableSlot::tts_tuple, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by ExecProject(), lookup_hash_entry(), and process_ordered_aggregate_multi().

{
    HeapTuple   tuple;
    int         attno;

    /* Quick out if we have 'em all already */
    if (slot->tts_nvalid >= attnum)
        return;

    /* Check for caller error */
    if (attnum <= 0 || attnum > slot->tts_tupleDescriptor->natts)
        elog(ERROR, "invalid attribute number %d", attnum);

    /*
     * otherwise we had better have a physical tuple (tts_nvalid should equal
     * natts in all virtual-tuple cases)
     */
    tuple = slot->tts_tuple;
    if (tuple == NULL)          /* internal error */
        elog(ERROR, "cannot extract attribute from empty tuple slot");

    /*
     * load up any slots available from physical tuple
     */
    attno = HeapTupleHeaderGetNatts(tuple->t_data);
    attno = Min(attno, attnum);

    slot_deform_tuple(slot, attno);

    /*
     * If tuple doesn't have all the atts indicated by tupleDesc, read the
     * rest as null
     */
    for (; attno < attnum; attno++)
    {
        slot->tts_values[attno] = (Datum) 0;
        slot->tts_isnull[attno] = true;
    }
    slot->tts_nvalid = attnum;
}