Commit 8f2ea8b7 authored by Tom Lane's avatar Tom Lane

Resurrect heap_deformtuple(), this time implemented as a singly nested

loop over the fields instead of a loop around heap_getattr.  This is
considerably faster (O(N) instead of O(N^2)) when there are nulls or
varlena fields, since those prevent use of attcacheoff.  Replace loops
over heap_getattr with heap_deformtuple in situations where all or most
of the fields have to be fetched, such as printtup and tuptoaster.
Profiling done more than a year ago shows that this should be a nice
win for situations involving many-column tables.
parent af44cac6
This diff is collapsed.
......@@ -9,7 +9,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/common/printtup.c,v 1.81 2004/05/26 04:41:03 neilc Exp $
* $PostgreSQL: pgsql/src/backend/access/common/printtup.c,v 1.82 2004/06/04 20:35:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -65,6 +65,8 @@ typedef struct
TupleDesc attrinfo; /* The attr info we are set up for */
int nattrs;
PrinttupAttrInfo *myinfo; /* Cached info about each attr */
Datum *values; /* preallocated space for deformtuple */
char *nulls;
} DR_printtup;
/* ----------------
......@@ -103,6 +105,8 @@ printtup_create_DR(CommandDest dest, Portal portal)
self->attrinfo = NULL;
self->nattrs = 0;
self->myinfo = NULL;
self->values = NULL;
self->nulls = NULL;
return (DestReceiver *) self;
}
......@@ -243,15 +247,27 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
int16 *formats = myState->portal->formats;
int i;
/* get rid of any old data */
if (myState->myinfo)
pfree(myState->myinfo); /* get rid of any old data */
pfree(myState->myinfo);
myState->myinfo = NULL;
if (myState->values)
pfree(myState->values);
myState->values = NULL;
if (myState->nulls)
pfree(myState->nulls);
myState->nulls = NULL;
myState->attrinfo = typeinfo;
myState->nattrs = numAttrs;
if (numAttrs <= 0)
return;
myState->myinfo = (PrinttupAttrInfo *)
palloc0(numAttrs * sizeof(PrinttupAttrInfo));
myState->values = (Datum *) palloc(numAttrs * sizeof(Datum));
myState->nulls = (char *) palloc(numAttrs * sizeof(char));
for (i = 0; i < numAttrs; i++)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
......@@ -297,6 +313,11 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
printtup_prepare_info(myState, typeinfo, natts);
/*
* deconstruct the tuple (faster than a heap_getattr loop)
*/
heap_deformtuple(tuple, typeinfo, myState->values, myState->nulls);
/*
* Prepare a DataRow message
*/
......@@ -310,12 +331,10 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
for (i = 0; i < natts; ++i)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
Datum origattr,
Datum origattr = myState->values[i],
attr;
bool isnull;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
if (myState->nulls[i] == 'n')
{
pq_sendint(&buf, -1, 4);
continue;
......@@ -383,6 +402,11 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
printtup_prepare_info(myState, typeinfo, natts);
/*
* deconstruct the tuple (faster than a heap_getattr loop)
*/
heap_deformtuple(tuple, typeinfo, myState->values, myState->nulls);
/*
* tell the frontend to expect new tuple data (in ASCII style)
*/
......@@ -395,7 +419,7 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
k = 1 << 7;
for (i = 0; i < natts; ++i)
{
if (!heap_attisnull(tuple, i + 1))
if (myState->nulls[i] != 'n')
j |= k; /* set bit if not null */
k >>= 1;
if (k == 0) /* end of byte? */
......@@ -414,13 +438,11 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
for (i = 0; i < natts; ++i)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
Datum origattr,
Datum origattr = myState->values[i],
attr;
bool isnull;
char *outputstr;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
if (myState->nulls[i] == 'n')
continue;
Assert(thisState->format == 0);
......@@ -461,6 +483,13 @@ printtup_shutdown(DestReceiver *self)
if (myState->myinfo)
pfree(myState->myinfo);
myState->myinfo = NULL;
if (myState->values)
pfree(myState->values);
myState->values = NULL;
if (myState->nulls)
pfree(myState->nulls);
myState->nulls = NULL;
myState->attrinfo = NULL;
}
......@@ -587,6 +616,11 @@ printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
printtup_prepare_info(myState, typeinfo, natts);
/*
* deconstruct the tuple (faster than a heap_getattr loop)
*/
heap_deformtuple(tuple, typeinfo, myState->values, myState->nulls);
/*
* tell the frontend to expect new tuple data (in binary style)
*/
......@@ -599,7 +633,7 @@ printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
k = 1 << 7;
for (i = 0; i < natts; ++i)
{
if (!heap_attisnull(tuple, i + 1))
if (myState->nulls[i] != 'n')
j |= k; /* set bit if not null */
k >>= 1;
if (k == 0) /* end of byte? */
......@@ -618,13 +652,11 @@ printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
for (i = 0; i < natts; ++i)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
Datum origattr,
Datum origattr = myState->values[i],
attr;
bool isnull;
bytea *outputbytes;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
if (myState->nulls[i] == 'n')
continue;
Assert(thisState->format == 1);
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.41 2003/11/29 19:51:40 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.42 2004/06/04 20:35:21 tgl Exp $
*
*
* INTERFACE ROUTINES
......@@ -281,15 +281,26 @@ toast_delete(Relation rel, HeapTuple oldtup)
Form_pg_attribute *att;
int numAttrs;
int i;
Datum value;
bool isnull;
Datum toast_values[MaxHeapAttributeNumber];
char toast_nulls[MaxHeapAttributeNumber];
/*
* Get the tuple descriptor, the number of and attribute descriptors.
* Get the tuple descriptor and break down the tuple into fields.
*
* NOTE: it's debatable whether to use heap_deformtuple() 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_deformtuple 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;
numAttrs = tupleDesc->natts;
att = tupleDesc->attrs;
numAttrs = tupleDesc->natts;
Assert(numAttrs <= MaxHeapAttributeNumber);
heap_deformtuple(oldtup, tupleDesc, toast_values, toast_nulls);
/*
* Check for external stored attributes and delete them from the
......@@ -299,8 +310,9 @@ toast_delete(Relation rel, HeapTuple oldtup)
{
if (att[i]->attlen == -1)
{
value = heap_getattr(oldtup, i + 1, tupleDesc, &isnull);
if (!isnull && VARATT_IS_EXTERNAL(value))
Datum value = toast_values[i];
if (toast_nulls[i] != 'n' && VARATT_IS_EXTERNAL(value))
toast_delete_datum(rel, value);
}
}
......@@ -321,8 +333,6 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
Form_pg_attribute *att;
int numAttrs;
int i;
bool old_isnull;
bool new_isnull;
bool need_change = false;
bool need_free = false;
......@@ -333,18 +343,24 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
char toast_action[MaxHeapAttributeNumber];
char toast_nulls[MaxHeapAttributeNumber];
char toast_oldnulls[MaxHeapAttributeNumber];
Datum toast_values[MaxHeapAttributeNumber];
Datum toast_oldvalues[MaxHeapAttributeNumber];
int32 toast_sizes[MaxHeapAttributeNumber];
bool toast_free[MaxHeapAttributeNumber];
bool toast_delold[MaxHeapAttributeNumber];
/*
* Get the tuple descriptor, the number of and attribute descriptors
* and the location of the tuple values.
* Get the tuple descriptor and break down the tuple(s) into fields.
*/
tupleDesc = rel->rd_att;
numAttrs = tupleDesc->natts;
att = tupleDesc->attrs;
numAttrs = tupleDesc->natts;
Assert(numAttrs <= MaxHeapAttributeNumber);
heap_deformtuple(newtup, tupleDesc, toast_values, toast_nulls);
if (oldtup != NULL)
heap_deformtuple(oldtup, tupleDesc, toast_oldvalues, toast_oldnulls);
/* ----------
* Then collect information about the values given
......@@ -353,12 +369,15 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
* ' ' 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_nulls, ' ', numAttrs * sizeof(char));
memset(toast_free, 0, numAttrs * sizeof(bool));
memset(toast_delold, 0, numAttrs * sizeof(bool));
for (i = 0; i < numAttrs; i++)
{
varattrib *old_value;
......@@ -369,27 +388,24 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
/*
* For UPDATE get the old and new values of this attribute
*/
old_value = (varattrib *) DatumGetPointer(
heap_getattr(oldtup, i + 1, tupleDesc, &old_isnull));
toast_values[i] =
heap_getattr(newtup, i + 1, tupleDesc, &new_isnull);
old_value = (varattrib *) DatumGetPointer(toast_oldvalues[i]);
new_value = (varattrib *) 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 (!old_isnull && att[i]->attlen == -1 &&
if (att[i]->attlen == -1 && toast_oldnulls[i] != 'n' &&
VARATT_IS_EXTERNAL(old_value))
{
if (new_isnull || !VARATT_IS_EXTERNAL(new_value) ||
if (toast_nulls[i] == 'n' || !VARATT_IS_EXTERNAL(new_value) ||
old_value->va_content.va_external.va_valueid !=
new_value->va_content.va_external.va_valueid ||
old_value->va_content.va_external.va_toastrelid !=
new_value->va_content.va_external.va_toastrelid)
{
/*
* The old external store value isn't needed any more
* The old external stored value isn't needed any more
* after the update
*/
toast_delold[i] = true;
......@@ -413,23 +429,21 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
/*
* For INSERT simply get the new value
*/
toast_values[i] =
heap_getattr(newtup, i + 1, tupleDesc, &new_isnull);
new_value = (varattrib *) DatumGetPointer(toast_values[i]);
}
/*
* Handle NULL attributes
*/
if (new_isnull)
if (toast_nulls[i] == 'n')
{
toast_action[i] = 'p';
toast_nulls[i] = 'n';
has_nulls = true;
continue;
}
/*
* Now look at varsize attributes
* Now look at varlena attributes
*/
if (att[i]->attlen == -1)
{
......@@ -461,10 +475,9 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
else
{
/*
* Not a variable size attribute, plain storage always
* Not a varlena attribute, plain storage always
*/
toast_action[i] = 'p';
toast_sizes[i] = att[i]->attlen;
}
}
......@@ -768,8 +781,7 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
if (need_delold)
for (i = 0; i < numAttrs; i++)
if (toast_delold[i])
toast_delete_datum(rel,
heap_getattr(oldtup, i + 1, tupleDesc, &old_isnull));
toast_delete_datum(rel, toast_oldvalues[i]);
}
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.109 2004/06/02 21:01:08 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.110 2004/06/04 20:35:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -2342,10 +2342,10 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
newslot = MakeTupleTableSlot();
ExecSetSlotDescriptor(newslot, newTupDesc, false);
/* Preallocate values/nulls arrays (+1 in case natts==0) */
/* Preallocate values/nulls arrays */
i = Max(newTupDesc->natts, oldTupDesc->natts);
values = (Datum *) palloc(i * sizeof(Datum) + 1);
nulls = (char *) palloc(i * sizeof(char) + 1);
values = (Datum *) palloc(i * sizeof(Datum));
nulls = (char *) palloc(i * sizeof(char));
memset(values, 0, i * sizeof(Datum));
memset(nulls, 'n', i * sizeof(char));
......@@ -2363,24 +2363,14 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
* Extract data from old tuple. We can force to null any
* columns that are deleted according to the new tuple.
*/
int natts = oldTupDesc->natts;
bool isNull;
int natts = newTupDesc->natts;
heap_deformtuple(tuple, oldTupDesc, values, nulls);
for (i = 0; i < natts; i++)
{
if (newTupDesc->attrs[i]->attisdropped)
nulls[i] = 'n';
else
{
values[i] = heap_getattr(tuple,
i + 1,
oldTupDesc,
&isNull);
if (isNull)
nulls[i] = 'n';
else
nulls[i] = ' ';
}
}
/*
......@@ -2393,6 +2383,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
foreach(l, tab->newvals)
{
NewColumnValue *ex = lfirst(l);
bool isNull;
values[ex->attnum - 1] = ExecEvalExpr(ex->exprstate,
econtext,
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.57 2004/05/26 04:41:12 neilc Exp $
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.58 2004/06/04 20:35:21 tgl Exp $
*
* DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the
......@@ -1329,12 +1329,8 @@ AlterDomainNotNull(List *names, bool notNull)
for (i = 0; i < rtc->natts; i++)
{
int attnum = rtc->atts[i];
Datum d;
bool isNull;
d = heap_getattr(tuple, attnum, tupdesc, &isNull);
if (isNull)
if (heap_attisnull(tuple, attnum))
ereport(ERROR,
(errcode(ERRCODE_NOT_NULL_VIOLATION),
errmsg("column \"%s\" of table \"%s\" contains null values",
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/execJunk.c,v 1.40 2004/05/26 04:41:14 neilc Exp $
* $PostgreSQL: pgsql/src/backend/executor/execJunk.c,v 1.41 2004/06/04 20:35:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -246,12 +246,15 @@ ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
TupleDesc cleanTupType;
TupleDesc tupType;
int cleanLength;
bool isNull;
int i;
Datum *values;
char *nulls;
Datum *old_values;
char *old_nulls;
Datum values_array[64];
Datum old_values_array[64];
char nulls_array[64];
char old_nulls_array[64];
/*
* get info from the slot and the junk filter
......@@ -265,11 +268,15 @@ ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
/*
* Create the arrays that will hold the attribute values and the null
* information for the new "clean" tuple.
* information for the old tuple and new "clean" tuple.
*
* Note: we use memory on the stack to optimize things when we are
* dealing with a small number of attributes. for large tuples we just
* use palloc.
*
* Note: we could use just one set of arrays if we were willing to
* assume that the resno mapping is monotonic... I think it is, but
* won't take the risk of breaking things right now.
*/
if (cleanLength > 64)
{
......@@ -281,36 +288,52 @@ ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
values = values_array;
nulls = nulls_array;
}
if (tupType->natts > 64)
{
old_values = (Datum *) palloc(tupType->natts * sizeof(Datum));
old_nulls = (char *) palloc(tupType->natts * sizeof(char));
}
else
{
old_values = old_values_array;
old_nulls = old_nulls_array;
}
/*
* Exctract one by one all the values of the "clean" tuple.
* Extract all the values of the old tuple.
*/
heap_deformtuple(tuple, tupType, old_values, old_nulls);
/*
* Transpose into proper fields of the new tuple.
*/
for (i = 0; i < cleanLength; i++)
{
values[i] = heap_getattr(tuple, cleanMap[i], tupType, &isNull);
int j = cleanMap[i] - 1;
if (isNull)
nulls[i] = 'n';
else
nulls[i] = ' ';
values[i] = old_values[j];
nulls[i] = old_nulls[j];
}
/*
* Now form the new tuple.
*/
cleanTuple = heap_formtuple(cleanTupType,
values,
nulls);
cleanTuple = heap_formtuple(cleanTupType, values, nulls);
/*
* We are done. Free any space allocated for 'values' and 'nulls' and
* return the new tuple.
*/
if (cleanLength > 64)
if (values != values_array)
{
pfree(values);
pfree(nulls);
}
if (old_values != old_values_array)
{
pfree(old_values);
pfree(old_nulls);
}
return cleanTuple;
}
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.115 2004/05/30 23:40:26 neilc Exp $
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.116 2004/06/04 20:35:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -427,7 +427,6 @@ SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,
int numberOfAttributes;
Datum *v;
char *n;
bool isnull;
int i;
if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
......@@ -448,11 +447,7 @@ SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,
n = (char *) palloc(numberOfAttributes * sizeof(char));
/* fetch old values and nulls */
for (i = 0; i < numberOfAttributes; i++)
{
v[i] = heap_getattr(tuple, i + 1, rel->rd_att, &isnull);
n[i] = (isnull) ? 'n' : ' ';
}
heap_deformtuple(tuple, rel->rd_att, v, n);
/* replace values and nulls */
for (i = 0; i < natts; i++)
......@@ -474,7 +469,7 @@ SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum,
mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
mtuple->t_self = tuple->t_self;
mtuple->t_tableOid = tuple->t_tableOid;
if (rel->rd_rel->relhasoids)
if (rel->rd_att->tdhasoid)
HeapTupleSetOid(mtuple, HeapTupleGetOid(tuple));
}
else
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.89 2004/04/21 18:24:26 tgl Exp $
* $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.90 2004/06/04 20:35:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -184,9 +184,9 @@ extern XLogRecPtr log_heap_move(Relation reln, Buffer oldbuf,
Buffer newbuf, HeapTuple newtup);
/* in common/heaptuple.c */
extern Size ComputeDataSize(TupleDesc tupleDesc, Datum *value, char *nulls);
extern Size ComputeDataSize(TupleDesc tupleDesc, Datum *values, char *nulls);
extern void DataFill(char *data, TupleDesc tupleDesc,
Datum *value, char *nulls, uint16 *infomask,
Datum *values, char *nulls, uint16 *infomask,
bits8 *bit);
extern int heap_attisnull(HeapTuple tup, int attnum);
extern Datum nocachegetattr(HeapTuple tup, int attnum,
......@@ -194,9 +194,14 @@ extern Datum nocachegetattr(HeapTuple tup, int attnum,
extern HeapTuple heap_copytuple(HeapTuple tuple);
extern void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
extern HeapTuple heap_formtuple(TupleDesc tupleDescriptor,
Datum *value, char *nulls);
Datum *values, char *nulls);
extern HeapTuple heap_modifytuple(HeapTuple tuple,
Relation relation, Datum *replValue, char *replNull, char *repl);
Relation relation,
Datum *replValues,
char *replNulls,
char *replActions);
extern void heap_deformtuple(HeapTuple tuple, TupleDesc tupleDesc,
Datum *values, char *nulls);
extern void heap_freetuple(HeapTuple tuple);
extern HeapTuple heap_addheader(int natts, bool withoid, Size structlen, void *structure);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment