This is an automated email from the ASF dual-hosted git repository. yjhjstz pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/cloudberry.git
commit 21e41c2b74aa9734d89a71f61263476df6366370 Author: Huansong Fu <[email protected]> AuthorDate: Mon May 8 11:59:27 2023 -0700 Syscache lookup for pg_attribute_encoding The GPDB catalog pg_attribute_encoding has been used more frequently than earlier. It now cincludes column's "filenum" and "lastrownums" to support ALTER COLUMN TYPE and missing-mode ADD COLUMN for co tables. More importantly, it will also be used in planner for costing calculations. So it makes sense to add it to the syscache for better performance. For now, use syscache in these places: * GetFilenumForAttribute() - it is a point search used in a lot of relfile related operations; * get_rel_attoptions() - it will be used for planner. * GetAttnumToLastrownumMapping() - it will be used by scan/fetch of ao/co tables. For other places table/index scan of pg_attribute_encoding is still used, as they are for updating/deleting or in non-critical path where the caching need isn't too much. --- src/backend/catalog/pg_attribute_encoding.c | 89 ++++++++++++----------------- src/backend/utils/cache/syscache.c | 12 ++++ src/include/utils/syscache.h | 5 +- 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/src/backend/catalog/pg_attribute_encoding.c b/src/backend/catalog/pg_attribute_encoding.c index b73e5bd44d..361222613c 100644 --- a/src/backend/catalog/pg_attribute_encoding.c +++ b/src/backend/catalog/pg_attribute_encoding.c @@ -103,48 +103,41 @@ get_funcs_for_compression(char *compresstype) Datum * get_rel_attoptions(Oid relid, AttrNumber max_attno) { - Form_pg_attribute attform; - ScanKeyData skey; - SysScanDesc scan; - HeapTuple tuple; - Datum *dats; - Relation pgae = heap_open(AttributeEncodingRelationId, - AccessShareLock); + HeapTuple atttuple; + Form_pg_attribute attform; + Datum *dats; + CatCList *attenclist; /* used for attbyval and len below */ - attform = TupleDescAttr(pgae->rd_att, Anum_pg_attribute_encoding_attoptions - 1); + atttuple = SearchSysCache2(ATTNUM, + ObjectIdGetDatum(AttributeEncodingRelationId), + Int16GetDatum(Anum_pg_attribute_encoding_attoptions)); + attform = (Form_pg_attribute) GETSTRUCT(atttuple); dats = palloc0(max_attno * sizeof(Datum)); - ScanKeyInit(&skey, - Anum_pg_attribute_encoding_attrelid, - BTEqualStrategyNumber, F_OIDEQ, - ObjectIdGetDatum(relid)); - scan = systable_beginscan(pgae, AttributeEncodingAttrelidIndexId, true, - NULL, 1, &skey); - - while (HeapTupleIsValid(tuple = systable_getnext(scan))) + attenclist = SearchSysCacheList1(ATTENCODINGNUM, relid); + for (int i = 0; i < attenclist->n_members; i++) { - Form_pg_attribute_encoding a = - (Form_pg_attribute_encoding)GETSTRUCT(tuple); - int16 attnum = a->attnum; - Datum attoptions; - bool isnull; + HeapTuple tuple = &attenclist->members[i]->tuple; + Form_pg_attribute_encoding form = + (Form_pg_attribute_encoding)GETSTRUCT(tuple); + AttrNumber attnum = form->attnum; + Datum attoptions; + bool isnull; Assert(attnum > 0 && attnum <= max_attno); - attoptions = heap_getattr(tuple, Anum_pg_attribute_encoding_attoptions, - RelationGetDescr(pgae), &isnull); - Assert(!isnull); - - dats[attnum - 1] = datumCopy(attoptions, - attform->attbyval, - attform->attlen); + attoptions = SysCacheGetAttr(ATTENCODINGNUM, tuple, Anum_pg_attribute_encoding_attoptions, + &isnull); + if (!isnull) + dats[attnum - 1] = datumCopy(attoptions, + attform->attbyval, + attform->attlen); } + ReleaseSysCacheList(attenclist); - systable_endscan(scan); - - heap_close(pgae, AccessShareLock); + ReleaseSysCache(atttuple); return dats; @@ -338,9 +331,6 @@ RemoveAttributeEncodingsByRelid(Oid relid) FileNumber GetFilenumForAttribute(Oid relid, AttrNumber attnum) { - Relation rel; - SysScanDesc scan; - ScanKeyData skey[2]; HeapTuple tup; FileNumber filenum; bool isnull; @@ -348,26 +338,21 @@ GetFilenumForAttribute(Oid relid, AttrNumber attnum) Assert(OidIsValid(relid)); Assert(AttributeNumberIsValid(attnum)); - rel = heap_open(AttributeEncodingRelationId, AccessShareLock); + tup = SearchSysCache2(ATTENCODINGNUM, + ObjectIdGetDatum(relid), + Int16GetDatum(attnum)); + if (!HeapTupleIsValid(tup)) + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("unable to find pg_attribute_encoding entry for attribute %d of relation %u", + attnum, relid))); - ScanKeyInit(&skey[0], - Anum_pg_attribute_encoding_attrelid, - BTEqualStrategyNumber, F_OIDEQ, - ObjectIdGetDatum(relid)); - ScanKeyInit(&skey[1], - Anum_pg_attribute_encoding_attnum, - BTEqualStrategyNumber, F_INT2EQ, - Int16GetDatum(attnum)); - scan = systable_beginscan(rel, AttributeEncodingAttrelidAttnumIndexId, true, - NULL, 2, skey); - - tup = systable_getnext(scan); - Assert(HeapTupleIsValid(tup)); - filenum = heap_getattr(tup, Anum_pg_attribute_encoding_filenum, - RelationGetDescr(rel), &isnull); + filenum = SysCacheGetAttr(ATTENCODINGNUM, + tup, + Anum_pg_attribute_encoding_filenum, + &isnull); Assert(!isnull); - systable_endscan(scan); - heap_close(rel, AccessShareLock); + ReleaseSysCache(tup); return filenum; } diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c index 0db5f5d433..91b0233e95 100644 --- a/src/backend/utils/cache/syscache.c +++ b/src/backend/utils/cache/syscache.c @@ -26,6 +26,7 @@ #include "access/htup_details.h" #include "access/sysattr.h" #include "catalog/pg_aggregate.h" +#include "catalog/pg_attribute_encoding.h" #include "catalog/pg_am.h" #include "catalog/pg_amop.h" #include "catalog/pg_amproc.h" @@ -1209,6 +1210,17 @@ static const struct cachedesc cacheinfo[] = { 0 }, 2 + }, + {AttributeEncodingRelationId, /* ATTENCODINGNUM */ + AttributeEncodingAttrelidAttnumIndexId, + 2, + { + Anum_pg_attribute_encoding_attrelid, + Anum_pg_attribute_encoding_attnum, + 0, + 0 + }, + 128 } }; diff --git a/src/include/utils/syscache.h b/src/include/utils/syscache.h index 0a7f66bd4e..e8b7df31f0 100644 --- a/src/include/utils/syscache.h +++ b/src/include/utils/syscache.h @@ -128,9 +128,10 @@ enum SysCacheIdentifier MVAUXOID, MVTABLESMVRELOID, USERMAPPINGOID, - USERMAPPINGUSERSERVER + USERMAPPINGUSERSERVER, + ATTENCODINGNUM -#define SysCacheSize (USERMAPPINGUSERSERVER + 1) +#define SysCacheSize (ATTENCODINGNUM + 1) }; extern void InitCatalogCache(void); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
