mirror of
https://github.com/postgres/postgres.git
synced 2025-04-22 23:02:54 +03:00
Improve performance of index-only scans with many index columns.
StoreIndexTuple was a loop over index_getattr, which is O(N^2) if the index columns are variable-width, and the performance impact is already quite visible at ten columns. The obvious move is to replace that with a call to index_deform_tuple ... but that's *also* a loop over index_getattr. Improve it to be essentially a clone of heap_deform_tuple. (There are a few other places that loop over all index columns with index_getattr, and perhaps should be changed likewise, but most of them don't seem performance-critical. Anyway, the rest would mostly only be interested in the index key columns, which there aren't likely to be so many of. Wide index tuples are a new thing with INCLUDE.) Konstantin Knizhnik Discussion: https://postgr.es/m/e06b2d27-04fc-5c0e-bb8c-ecd72aa24959@postgrespro.ru
This commit is contained in:
parent
78b408a20a
commit
80b9e9c466
@ -418,19 +418,80 @@ nocache_index_getattr(IndexTuple tup,
|
||||
*
|
||||
* The caller must allocate sufficient storage for the output arrays.
|
||||
* (INDEX_MAX_KEYS entries should be enough.)
|
||||
*
|
||||
* This is nearly the same as heap_deform_tuple(), but for IndexTuples.
|
||||
* One difference is that the tuple should never have any missing columns.
|
||||
*/
|
||||
void
|
||||
index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
|
||||
Datum *values, bool *isnull)
|
||||
{
|
||||
int i;
|
||||
int hasnulls = IndexTupleHasNulls(tup);
|
||||
int natts = tupleDescriptor->natts; /* number of atts to extract */
|
||||
int attnum;
|
||||
char *tp; /* ptr to tuple data */
|
||||
int off; /* offset in tuple data */
|
||||
bits8 *bp; /* ptr to null bitmap in tuple */
|
||||
bool slow = false; /* can we use/set attcacheoff? */
|
||||
|
||||
/* Assert to protect callers who allocate fixed-size arrays */
|
||||
Assert(tupleDescriptor->natts <= INDEX_MAX_KEYS);
|
||||
Assert(natts <= INDEX_MAX_KEYS);
|
||||
|
||||
for (i = 0; i < tupleDescriptor->natts; i++)
|
||||
/* XXX "knows" t_bits are just after fixed tuple header! */
|
||||
bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
|
||||
|
||||
tp = (char *) tup + IndexInfoFindDataOffset(tup->t_info);
|
||||
off = 0;
|
||||
|
||||
for (attnum = 0; attnum < natts; attnum++)
|
||||
{
|
||||
values[i] = index_getattr(tup, i + 1, tupleDescriptor, &isnull[i]);
|
||||
Form_pg_attribute thisatt = TupleDescAttr(tupleDescriptor, 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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,23 +269,17 @@ IndexOnlyNext(IndexOnlyScanState *node)
|
||||
static void
|
||||
StoreIndexTuple(TupleTableSlot *slot, IndexTuple itup, TupleDesc itupdesc)
|
||||
{
|
||||
int nindexatts = itupdesc->natts;
|
||||
Datum *values = slot->tts_values;
|
||||
bool *isnull = slot->tts_isnull;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Note: we must use the tupdesc supplied by the AM in index_getattr, not
|
||||
* the slot's tupdesc, in case the latter has different datatypes (this
|
||||
* happens for btree name_ops in particular). They'd better have the same
|
||||
* number of columns though, as well as being datatype-compatible which is
|
||||
* something we can't so easily check.
|
||||
* Note: we must use the tupdesc supplied by the AM in index_deform_tuple,
|
||||
* not the slot's tupdesc, in case the latter has different datatypes
|
||||
* (this happens for btree name_ops in particular). They'd better have
|
||||
* the same number of columns though, as well as being datatype-compatible
|
||||
* which is something we can't so easily check.
|
||||
*/
|
||||
Assert(slot->tts_tupleDescriptor->natts == nindexatts);
|
||||
Assert(slot->tts_tupleDescriptor->natts == itupdesc->natts);
|
||||
|
||||
ExecClearTuple(slot);
|
||||
for (i = 0; i < nindexatts; i++)
|
||||
values[i] = index_getattr(itup, i + 1, itupdesc, &isnull[i]);
|
||||
index_deform_tuple(itup, itupdesc, slot->tts_values, slot->tts_isnull);
|
||||
ExecStoreVirtualTuple(slot);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user