mirror of
https://github.com/postgres/postgres.git
synced 2025-11-18 02:02:55 +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:
24
src/backend/utils/cache/relcache.c
vendored
24
src/backend/utils/cache/relcache.c
vendored
@@ -585,6 +585,8 @@ RelationBuildTupleDesc(Relation relation)
|
||||
attp,
|
||||
ATTRIBUTE_FIXED_PART_SIZE);
|
||||
|
||||
populate_compact_attribute(relation->rd_att, attnum - 1);
|
||||
|
||||
/* Update constraint/default info */
|
||||
if (attp->attnotnull)
|
||||
constr->has_not_null = true;
|
||||
@@ -661,8 +663,7 @@ RelationBuildTupleDesc(Relation relation)
|
||||
|
||||
/*
|
||||
* The attcacheoff values we read from pg_attribute should all be -1
|
||||
* ("unknown"). Verify this if assert checking is on. They will be
|
||||
* computed when and if needed during tuple access.
|
||||
* ("unknown"). Verify this if assert checking is on.
|
||||
*/
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
{
|
||||
@@ -674,12 +675,12 @@ RelationBuildTupleDesc(Relation relation)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* However, we can easily set the attcacheoff value for the first
|
||||
* attribute: it must be zero. This eliminates the need for special cases
|
||||
* for attnum=1 that used to exist in fastgetattr() and index_getattr().
|
||||
* We can easily set the attcacheoff value for the first attribute: it
|
||||
* must be zero. This eliminates the need for special cases for attnum=1
|
||||
* that used to exist in fastgetattr() and index_getattr().
|
||||
*/
|
||||
if (RelationGetNumberOfAttributes(relation) > 0)
|
||||
TupleDescAttr(relation->rd_att, 0)->attcacheoff = 0;
|
||||
TupleDescCompactAttr(relation->rd_att, 0)->attcacheoff = 0;
|
||||
|
||||
/*
|
||||
* Set up constraint/default info
|
||||
@@ -1965,10 +1966,12 @@ formrdesc(const char *relationName, Oid relationReltype,
|
||||
has_not_null |= attrs[i].attnotnull;
|
||||
/* make sure attcacheoff is valid */
|
||||
TupleDescAttr(relation->rd_att, i)->attcacheoff = -1;
|
||||
|
||||
populate_compact_attribute(relation->rd_att, i);
|
||||
}
|
||||
|
||||
/* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
|
||||
TupleDescAttr(relation->rd_att, 0)->attcacheoff = 0;
|
||||
TupleDescCompactAttr(relation->rd_att, 0)->attcacheoff = 0;
|
||||
|
||||
/* mark not-null status */
|
||||
if (has_not_null)
|
||||
@@ -3579,6 +3582,7 @@ RelationBuildLocalRelation(const char *relname,
|
||||
datt->attgenerated = satt->attgenerated;
|
||||
datt->attnotnull = satt->attnotnull;
|
||||
has_not_null |= satt->attnotnull;
|
||||
populate_compact_attribute(rel->rd_att, i);
|
||||
}
|
||||
|
||||
if (has_not_null)
|
||||
@@ -4399,10 +4403,12 @@ BuildHardcodedDescriptor(int natts, const FormData_pg_attribute *attrs)
|
||||
memcpy(TupleDescAttr(result, i), &attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
|
||||
/* make sure attcacheoff is valid */
|
||||
TupleDescAttr(result, i)->attcacheoff = -1;
|
||||
|
||||
populate_compact_attribute(result, i);
|
||||
}
|
||||
|
||||
/* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
|
||||
TupleDescAttr(result, 0)->attcacheoff = 0;
|
||||
TupleDescCompactAttr(result, 0)->attcacheoff = 0;
|
||||
|
||||
/* Note: we don't bother to set up a TupleConstr entry */
|
||||
|
||||
@@ -6199,6 +6205,8 @@ load_relcache_init_file(bool shared)
|
||||
goto read_failed;
|
||||
|
||||
has_not_null |= attr->attnotnull;
|
||||
|
||||
populate_compact_attribute(rel->rd_att, i);
|
||||
}
|
||||
|
||||
/* next read the access method specific field */
|
||||
|
||||
Reference in New Issue
Block a user