1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-21 10:42:50 +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

@@ -598,7 +598,7 @@ ExecBuildUpdateProjection(List *targetList,
*/
for (int attnum = relDesc->natts; attnum > 0; attnum--)
{
Form_pg_attribute attr = TupleDescAttr(relDesc, attnum - 1);
CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
if (attr->attisdropped)
continue;
@@ -694,7 +694,7 @@ ExecBuildUpdateProjection(List *targetList,
*/
for (int attnum = 1; attnum <= relDesc->natts; attnum++)
{
Form_pg_attribute attr = TupleDescAttr(relDesc, attnum - 1);
CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
if (attr->attisdropped)
{

View File

@@ -3152,7 +3152,7 @@ ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op,
for (int att = 1; att <= tupDesc->natts; att++)
{
/* ignore dropped columns */
if (TupleDescAttr(tupDesc, att - 1)->attisdropped)
if (TupleDescCompactAttr(tupDesc, att - 1)->attisdropped)
continue;
if (heap_attisnull(&tmptup, att, tupDesc))
{
@@ -5296,8 +5296,8 @@ ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
for (int i = 0; i < var_tupdesc->natts; i++)
{
Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
Form_pg_attribute sattr = TupleDescAttr(tupleDesc, i);
CompactAttribute *vattr = TupleDescCompactAttr(var_tupdesc, i);
CompactAttribute *sattr = TupleDescCompactAttr(tupleDesc, i);
if (!vattr->attisdropped)
continue; /* already checked non-dropped cols */

View File

@@ -169,7 +169,7 @@ ExecInitJunkFilterConversion(List *targetList,
t = list_head(targetList);
for (i = 0; i < cleanLength; i++)
{
if (TupleDescAttr(cleanTupType, i)->attisdropped)
if (TupleDescCompactAttr(cleanTupType, i)->attisdropped)
continue; /* map entry is already zero */
for (;;)
{

View File

@@ -187,7 +187,7 @@ tts_virtual_materialize(TupleTableSlot *slot)
/* compute size of memory required */
for (int natt = 0; natt < desc->natts; natt++)
{
Form_pg_attribute att = TupleDescAttr(desc, natt);
CompactAttribute *att = TupleDescCompactAttr(desc, natt);
Datum val;
if (att->attbyval || slot->tts_isnull[natt])
@@ -223,7 +223,7 @@ tts_virtual_materialize(TupleTableSlot *slot)
/* and copy all attributes into the pre-allocated space */
for (int natt = 0; natt < desc->natts; natt++)
{
Form_pg_attribute att = TupleDescAttr(desc, natt);
CompactAttribute *att = TupleDescCompactAttr(desc, natt);
Datum val;
if (att->attbyval || slot->tts_isnull[natt])
@@ -1044,7 +1044,7 @@ slot_deform_heap_tuple(TupleTableSlot *slot, HeapTuple tuple, uint32 *offp,
for (; attnum < natts; attnum++)
{
Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
CompactAttribute *thisatt = TupleDescCompactAttr(tupleDesc, attnum);
if (hasnulls && att_isnull(attnum, bp))
{
@@ -2237,7 +2237,7 @@ BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
*/
for (i = 0; i < natts; i++)
{
if (!TupleDescAttr(tupdesc, i)->attisdropped)
if (!TupleDescCompactAttr(tupdesc, i)->attisdropped)
{
/* Non-dropped attributes */
dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],

View File

@@ -1886,7 +1886,7 @@ check_sql_fn_retval(List *queryTreeLists,
/* remaining columns in rettupdesc had better all be dropped */
for (colindex++; colindex <= tupnatts; colindex++)
{
if (!TupleDescAttr(rettupdesc, colindex - 1)->attisdropped)
if (!TupleDescCompactAttr(rettupdesc, colindex - 1)->attisdropped)
ereport(ERROR,
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
errmsg("return type mismatch in function declared to return %s",

View File

@@ -175,10 +175,10 @@ MemoizeHash_hash(struct memoize_hash *tb, const MemoizeKey *key)
if (!pslot->tts_isnull[i]) /* treat nulls as having hash key 0 */
{
Form_pg_attribute attr;
CompactAttribute *attr;
uint32 hkey;
attr = TupleDescAttr(pslot->tts_tupleDescriptor, i);
attr = TupleDescCompactAttr(pslot->tts_tupleDescriptor, i);
hkey = datum_image_hash(pslot->tts_values[i], attr->attbyval, attr->attlen);
@@ -242,7 +242,7 @@ MemoizeHash_equal(struct memoize_hash *tb, const MemoizeKey *key1,
for (int i = 0; i < numkeys; i++)
{
Form_pg_attribute attr;
CompactAttribute *attr;
if (tslot->tts_isnull[i] != pslot->tts_isnull[i])
{
@@ -255,7 +255,7 @@ MemoizeHash_equal(struct memoize_hash *tb, const MemoizeKey *key1,
continue;
/* perform binary comparison on the two datums */
attr = TupleDescAttr(tslot->tts_tupleDescriptor, i);
attr = TupleDescCompactAttr(tslot->tts_tupleDescriptor, i);
if (!datum_image_eq(tslot->tts_values[i], pslot->tts_values[i],
attr->attbyval, attr->attlen))
{

View File

@@ -496,14 +496,14 @@ ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo,
for (int i = 0; i < natts; i++)
{
Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
if (ri_GeneratedExprs[i])
{
Datum val;
bool isnull;
Assert(attr->attgenerated == ATTRIBUTE_GENERATED_STORED);
Assert(TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED);
econtext->ecxt_scantuple = slot;

View File

@@ -142,8 +142,8 @@ ValuesNext(ValuesScanState *node)
foreach(lc, exprstatelist)
{
ExprState *estate = (ExprState *) lfirst(lc);
Form_pg_attribute attr = TupleDescAttr(slot->tts_tupleDescriptor,
resind);
CompactAttribute *attr = TupleDescCompactAttr(slot->tts_tupleDescriptor,
resind);
values[resind] = ExecEvalExpr(estate,
econtext,

View File

@@ -65,7 +65,7 @@ tstoreStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo)
{
for (i = 0; i < natts; i++)
{
Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
CompactAttribute *attr = TupleDescCompactAttr(typeinfo, i);
if (attr->attisdropped)
continue;
@@ -154,7 +154,7 @@ tstoreReceiveSlot_detoast(TupleTableSlot *slot, DestReceiver *self)
for (i = 0; i < natts; i++)
{
Datum val = slot->tts_values[i];
Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
CompactAttribute *attr = TupleDescCompactAttr(typeinfo, i);
if (!attr->attisdropped && attr->attlen == -1 && !slot->tts_isnull[i])
{