1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-22 12:22:45 +03:00

Introduce CompactAttribute array in TupleDesc, take 2

The new compact_attrs array stores a few select fields from
FormData_pg_attribute in a more compact way, using only 16 bytes per
column instead of the 104 bytes that FormData_pg_attribute uses.  Using
CompactAttribute allows performance-critical operations such as tuple
deformation to be performed without looking at the FormData_pg_attribute
element in TupleDesc which means fewer cacheline accesses.

For some workloads, tuple deformation can be the most CPU intensive part
of processing the query.  Some testing with 16 columns on a table
where the first column is variable length showed around a 10% increase in
transactions per second for an OLAP type query performing aggregation on
the 16th column.  However, in certain cases, the increases were much
higher, up to ~25% on one AMD Zen4 machine.

This also makes pg_attribute.attcacheoff redundant.  A follow-on commit
will remove it, thus shrinking the FormData_pg_attribute struct by 4
bytes.

Author: David Rowley
Reviewed-by: Andres Freund, Victor Yegorov
Discussion: https://postgr.es/m/CAApHDvrBztXP3yx=NKNmo3xwFAFhEdyPnvrDg3=M0RhDs+4vYw@mail.gmail.com
This commit is contained in:
David Rowley
2024-12-20 22:31:26 +13:00
parent 8ac0021b6f
commit 5983a4cffc
44 changed files with 374 additions and 154 deletions

View File

@@ -303,13 +303,13 @@ nocache_index_getattr(IndexTuple tup,
if (!slow)
{
Form_pg_attribute att;
CompactAttribute *att;
/*
* 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.
*/
att = TupleDescAttr(tupleDesc, attnum);
att = TupleDescCompactAttr(tupleDesc, attnum);
if (att->attcacheoff >= 0)
return fetchatt(att, tp + att->attcacheoff);
@@ -324,7 +324,7 @@ nocache_index_getattr(IndexTuple tup,
for (j = 0; j <= attnum; j++)
{
if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
if (TupleDescCompactAttr(tupleDesc, j)->attlen <= 0)
{
slow = true;
break;
@@ -347,18 +347,18 @@ nocache_index_getattr(IndexTuple tup,
* fixed-width columns, in hope of avoiding future visits to this
* routine.
*/
TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
TupleDescCompactAttr(tupleDesc, 0)->attcacheoff = 0;
/* we might have set some offsets in the slow path previously */
while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
while (j < natts && TupleDescCompactAttr(tupleDesc, j)->attcacheoff > 0)
j++;
off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
TupleDescAttr(tupleDesc, j - 1)->attlen;
off = TupleDescCompactAttr(tupleDesc, j - 1)->attcacheoff +
TupleDescCompactAttr(tupleDesc, j - 1)->attlen;
for (; j < natts; j++)
{
Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
CompactAttribute *att = TupleDescCompactAttr(tupleDesc, j);
if (att->attlen <= 0)
break;
@@ -372,7 +372,7 @@ nocache_index_getattr(IndexTuple tup,
Assert(j > attnum);
off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
off = TupleDescCompactAttr(tupleDesc, attnum)->attcacheoff;
}
else
{
@@ -392,7 +392,7 @@ nocache_index_getattr(IndexTuple tup,
off = 0;
for (i = 0;; i++) /* loop exit is at "break" */
{
Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
CompactAttribute *att = TupleDescCompactAttr(tupleDesc, i);
if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
{
@@ -440,7 +440,7 @@ nocache_index_getattr(IndexTuple tup,
}
}
return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
return fetchatt(TupleDescCompactAttr(tupleDesc, attnum), tp + off);
}
/*
@@ -490,7 +490,7 @@ index_deform_tuple_internal(TupleDesc tupleDescriptor,
for (attnum = 0; attnum < natts; attnum++)
{
Form_pg_attribute thisatt = TupleDescAttr(tupleDescriptor, attnum);
CompactAttribute *thisatt = TupleDescCompactAttr(tupleDescriptor, attnum);
if (hasnulls && att_isnull(attnum, bp))
{
@@ -587,10 +587,8 @@ index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source,
if (leavenatts == sourceDescriptor->natts)
return CopyIndexTuple(source);
/* Create temporary descriptor to scribble on */
truncdesc = palloc(TupleDescSize(sourceDescriptor));
TupleDescCopy(truncdesc, sourceDescriptor);
truncdesc->natts = leavenatts;
/* Create temporary truncated tuple descriptor */
truncdesc = CreateTupleDescTruncatedCopy(sourceDescriptor, leavenatts);
/* Deform, form copy of tuple with fewer attributes */
index_deform_tuple(source, truncdesc, values, isnull);