mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
Revise TupleTableSlot code to avoid unnecessary construction and disassembly
of tuples when passing data up through multiple plan nodes. A slot can now hold either a normal "physical" HeapTuple, or a "virtual" tuple consisting of Datum/isnull arrays. Upper plan levels can usually just copy the Datum arrays, avoiding heap_formtuple() and possible subsequent nocachegetattr() calls to extract the data again. This work extends Atsushi Ogawa's earlier patch, which provided the key idea of adding Datum arrays to TupleTableSlots. (I believe however that something like this was foreseen way back in Berkeley days --- see the old comment on ExecProject.) A test case involving many levels of join of fairly wide tables (about 80 columns altogether) showed about 3x overall speedup, though simple queries will probably not be helped very much. I have also duplicated some code in heaptuple.c in order to provide versions of heap_formtuple and friends that use "bool" arrays to indicate null attributes, instead of the old convention of "char" arrays containing either 'n' or ' '. This provides a better match to the convention used by ExecEvalExpr. While I have not made a concerted effort to get rid of uses of the old routines, I think they should be deprecated and eventually removed.
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.82 2005/02/11 00:41:12 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.83 2005/03/16 21:38:05 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -448,14 +448,11 @@ compute_index_stats(Relation onerel, double totalrows,
|
||||
{
|
||||
MemoryContext ind_context,
|
||||
old_context;
|
||||
TupleDesc heapDescriptor;
|
||||
Datum attdata[INDEX_MAX_KEYS];
|
||||
char nulls[INDEX_MAX_KEYS];
|
||||
int ind,
|
||||
i;
|
||||
|
||||
heapDescriptor = RelationGetDescr(onerel);
|
||||
|
||||
ind_context = AllocSetContextCreate(anl_context,
|
||||
"Analyze Index",
|
||||
ALLOCSET_DEFAULT_MINSIZE,
|
||||
@ -468,7 +465,6 @@ compute_index_stats(Relation onerel, double totalrows,
|
||||
AnlIndexData *thisdata = &indexdata[ind];
|
||||
IndexInfo *indexInfo = thisdata->indexInfo;
|
||||
int attr_cnt = thisdata->attr_cnt;
|
||||
TupleTable tupleTable;
|
||||
TupleTableSlot *slot;
|
||||
EState *estate;
|
||||
ExprContext *econtext;
|
||||
@ -492,9 +488,7 @@ compute_index_stats(Relation onerel, double totalrows,
|
||||
estate = CreateExecutorState();
|
||||
econtext = GetPerTupleExprContext(estate);
|
||||
/* Need a slot to hold the current heap tuple, too */
|
||||
tupleTable = ExecCreateTupleTable(1);
|
||||
slot = ExecAllocTableSlot(tupleTable);
|
||||
ExecSetSlotDescriptor(slot, heapDescriptor, false);
|
||||
slot = MakeSingleTupleTableSlot(RelationGetDescr(onerel));
|
||||
|
||||
/* Arrange for econtext's scan tuple to be the tuple under test */
|
||||
econtext->ecxt_scantuple = slot;
|
||||
@ -532,8 +526,7 @@ compute_index_stats(Relation onerel, double totalrows,
|
||||
* convenient.
|
||||
*/
|
||||
FormIndexDatum(indexInfo,
|
||||
heapTuple,
|
||||
heapDescriptor,
|
||||
slot,
|
||||
estate,
|
||||
attdata,
|
||||
nulls);
|
||||
@ -585,7 +578,7 @@ compute_index_stats(Relation onerel, double totalrows,
|
||||
/* And clean up */
|
||||
MemoryContextSwitchTo(ind_context);
|
||||
|
||||
ExecDropTupleTable(tupleTable, true);
|
||||
ExecDropSingleTupleTableSlot(slot);
|
||||
FreeExecutorState(estate);
|
||||
MemoryContextResetAndDeleteChildren(ind_context);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.237 2005/03/12 05:41:34 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.238 2005/03/16 21:38:05 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -1487,7 +1487,6 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
|
||||
bool isnull;
|
||||
ResultRelInfo *resultRelInfo;
|
||||
EState *estate = CreateExecutorState(); /* for ExecConstraints() */
|
||||
TupleTable tupleTable;
|
||||
TupleTableSlot *slot;
|
||||
bool file_has_oids;
|
||||
int *defmap;
|
||||
@ -1518,10 +1517,8 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
|
||||
estate->es_num_result_relations = 1;
|
||||
estate->es_result_relation_info = resultRelInfo;
|
||||
|
||||
/* Set up a dummy tuple table too */
|
||||
tupleTable = ExecCreateTupleTable(1);
|
||||
slot = ExecAllocTableSlot(tupleTable);
|
||||
ExecSetSlotDescriptor(slot, tupDesc, false);
|
||||
/* Set up a tuple slot too */
|
||||
slot = MakeSingleTupleTableSlot(tupDesc);
|
||||
|
||||
econtext = GetPerTupleExprContext(estate);
|
||||
|
||||
@ -1989,7 +1986,7 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
|
||||
pfree(constraintexprs);
|
||||
pfree(force_notnull);
|
||||
|
||||
ExecDropTupleTable(tupleTable, true);
|
||||
ExecDropSingleTupleTableSlot(slot);
|
||||
|
||||
ExecCloseIndices(resultRelInfo);
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.146 2005/02/09 23:17:26 neilc Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.147 2005/03/16 21:38:05 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -2455,7 +2455,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
|
||||
{
|
||||
ExprContext *econtext;
|
||||
Datum *values;
|
||||
char *nulls;
|
||||
bool *isnull;
|
||||
TupleTableSlot *oldslot;
|
||||
TupleTableSlot *newslot;
|
||||
HeapScanDesc scan;
|
||||
@ -2471,17 +2471,15 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
|
||||
* the tuples are the same, the tupDescs might not be (consider
|
||||
* ADD COLUMN without a default).
|
||||
*/
|
||||
oldslot = MakeTupleTableSlot();
|
||||
ExecSetSlotDescriptor(oldslot, oldTupDesc, false);
|
||||
newslot = MakeTupleTableSlot();
|
||||
ExecSetSlotDescriptor(newslot, newTupDesc, false);
|
||||
oldslot = MakeSingleTupleTableSlot(oldTupDesc);
|
||||
newslot = MakeSingleTupleTableSlot(newTupDesc);
|
||||
|
||||
/* Preallocate values/nulls arrays */
|
||||
/* Preallocate values/isnull arrays */
|
||||
i = Max(newTupDesc->natts, oldTupDesc->natts);
|
||||
values = (Datum *) palloc(i * sizeof(Datum));
|
||||
nulls = (char *) palloc(i * sizeof(char));
|
||||
isnull = (bool *) palloc(i * sizeof(bool));
|
||||
memset(values, 0, i * sizeof(Datum));
|
||||
memset(nulls, 'n', i * sizeof(char));
|
||||
memset(isnull, true, i * sizeof(bool));
|
||||
|
||||
/*
|
||||
* Any attributes that are dropped according to the new tuple
|
||||
@ -2512,11 +2510,11 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
|
||||
if (newrel)
|
||||
{
|
||||
/* Extract data from old tuple */
|
||||
heap_deformtuple(tuple, oldTupDesc, values, nulls);
|
||||
heap_deform_tuple(tuple, oldTupDesc, values, isnull);
|
||||
|
||||
/* Set dropped attributes to null in new tuple */
|
||||
foreach (lc, dropped_attrs)
|
||||
nulls[lfirst_int(lc)] = 'n';
|
||||
isnull[lfirst_int(lc)] = true;
|
||||
|
||||
/*
|
||||
* Process supplied expressions to replace selected
|
||||
@ -2528,16 +2526,11 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
|
||||
foreach(l, tab->newvals)
|
||||
{
|
||||
NewColumnValue *ex = lfirst(l);
|
||||
bool isNull;
|
||||
|
||||
values[ex->attnum - 1] = ExecEvalExpr(ex->exprstate,
|
||||
econtext,
|
||||
&isNull,
|
||||
&isnull[ex->attnum - 1],
|
||||
NULL);
|
||||
if (isNull)
|
||||
nulls[ex->attnum - 1] = 'n';
|
||||
else
|
||||
nulls[ex->attnum - 1] = ' ';
|
||||
}
|
||||
|
||||
/*
|
||||
@ -2545,7 +2538,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
|
||||
* pfree it, since the per-tuple memory context will
|
||||
* be reset shortly.
|
||||
*/
|
||||
tuple = heap_formtuple(newTupDesc, values, nulls);
|
||||
tuple = heap_form_tuple(newTupDesc, values, isnull);
|
||||
}
|
||||
|
||||
/* Now check any constraints on the possibly-changed tuple */
|
||||
|
@ -13,7 +13,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.303 2005/03/04 20:21:06 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.304 2005/03/16 21:38:05 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -114,7 +114,6 @@ typedef struct ExecContextData
|
||||
{
|
||||
ResultRelInfo *resultRelInfo;
|
||||
EState *estate;
|
||||
TupleTable tupleTable;
|
||||
TupleTableSlot *slot;
|
||||
} ExecContextData;
|
||||
typedef ExecContextData *ExecContext;
|
||||
@ -141,16 +140,14 @@ ExecContext_Init(ExecContext ec, Relation rel)
|
||||
ec->estate->es_num_result_relations = 1;
|
||||
ec->estate->es_result_relation_info = ec->resultRelInfo;
|
||||
|
||||
/* Set up a dummy tuple table too */
|
||||
ec->tupleTable = ExecCreateTupleTable(1);
|
||||
ec->slot = ExecAllocTableSlot(ec->tupleTable);
|
||||
ExecSetSlotDescriptor(ec->slot, tupdesc, false);
|
||||
/* Set up a tuple slot too */
|
||||
ec->slot = MakeSingleTupleTableSlot(tupdesc);
|
||||
}
|
||||
|
||||
static void
|
||||
ExecContext_Finish(ExecContext ec)
|
||||
{
|
||||
ExecDropTupleTable(ec->tupleTable, true);
|
||||
ExecDropSingleTupleTableSlot(ec->slot);
|
||||
ExecCloseIndices(ec->resultRelInfo);
|
||||
FreeExecutorState(ec->estate);
|
||||
}
|
||||
|
Reference in New Issue
Block a user