v32-0013-Consolidate-attribute-syscache-lookups-into-one-.patch
text/x-patch
Filename: v32-0013-Consolidate-attribute-syscache-lookups-into-one-.patch
Type: text/x-patch
Part: 12
Message:
Re: Statistics Import and Export
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v32-0013
Subject: Consolidate attribute syscache lookups into one call by name.
| File | + | − |
|---|---|---|
| src/backend/statistics/attribute_stats.c | 20 | 27 |
From 433b3b2dc09445f68d14677d61a77e266c4f7fed Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Thu, 14 Nov 2024 03:53:33 -0500
Subject: [PATCH v32 13/14] Consolidate attribute syscache lookups into one
call by name.
Previously we were doing one lookup by attname and one lookup by attnum,
which seems wasteful.
---
src/backend/statistics/attribute_stats.c | 47 ++++++++++--------------
1 file changed, 20 insertions(+), 27 deletions(-)
diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index 42f458c280..1d39281c03 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -78,8 +78,8 @@ static struct StatsArgInfo attarginfo[] =
static bool attribute_statistics_update(FunctionCallInfo fcinfo, int elevel);
static Node *get_attr_expr(Relation rel, int attnum);
-static void get_attr_stat_type(Oid reloid, AttrNumber attnum, int elevel,
- Oid *atttypid, int32 *atttypmod,
+static void get_attr_stat_type(Oid reloid, Name attname, int elevel,
+ AttrNumber *attnum, Oid *atttypid, int32 *atttypmod,
char *atttyptype, Oid *atttypcoll,
Oid *eq_opr, Oid *lt_opr);
static bool get_elem_stat_type(Oid atttypid, char atttyptype, int elevel,
@@ -160,17 +160,16 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
attname = PG_GETARG_NAME(ATTNAME_ARG);
- attnum = get_attnum(reloid, NameStr(*attname));
- /* maybe use (!AttrNumberIsForUserDefinedAttr(attnum)) instead? */
- if (attnum <= InvalidAttrNumber)
- ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_COLUMN),
- errmsg("column \"%s\" of relation \"%s\" does not exist",
- NameStr(*attname), get_rel_name(reloid))));
stats_check_required_arg(fcinfo, attarginfo, INHERITED_ARG);
inherited = PG_GETARG_BOOL(INHERITED_ARG);
+ /* derive type information from attribute */
+ get_attr_stat_type(reloid, attname, elevel,
+ &attnum, &atttypid, &atttypmod,
+ &atttyptype, &atttypcoll,
+ &eq_opr, <_opr);
+
/*
* Check argument sanity. If some arguments are unusable, emit at elevel
* and set the corresponding argument to NULL in fcinfo.
@@ -220,12 +219,6 @@ attribute_statistics_update(FunctionCallInfo fcinfo, int elevel)
result = false;
}
- /* derive information from attribute */
- get_attr_stat_type(reloid, attnum, elevel,
- &atttypid, &atttypmod,
- &atttyptype, &atttypcoll,
- &eq_opr, <_opr);
-
/* if needed, derive element type */
if (do_mcelem || do_dechist)
{
@@ -491,8 +484,8 @@ get_attr_expr(Relation rel, int attnum)
* Derive type information from the attribute.
*/
static void
-get_attr_stat_type(Oid reloid, AttrNumber attnum, int elevel,
- Oid *atttypid, int32 *atttypmod,
+get_attr_stat_type(Oid reloid, Name attname, int elevel,
+ AttrNumber *attnum, Oid *atttypid, int32 *atttypmod,
char *atttyptype, Oid *atttypcoll,
Oid *eq_opr, Oid *lt_opr)
{
@@ -502,24 +495,25 @@ get_attr_stat_type(Oid reloid, AttrNumber attnum, int elevel,
Node *expr;
TypeCacheEntry *typcache;
- atup = SearchSysCache2(ATTNUM, ObjectIdGetDatum(reloid),
- Int16GetDatum(attnum));
+ atup = SearchSysCacheAttName(reloid, NameStr(*attname));
- /* Attribute not found */
+ /* Attribute not found or is dropped */
if (!HeapTupleIsValid(atup))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
- errmsg("attribute %d of relation \"%s\" does not exist",
- attnum, RelationGetRelationName(rel))));
+ errmsg("column \"%s\" of relation \"%s\" does not exist",
+ NameStr(*attname), get_rel_name(reloid))));
attr = (Form_pg_attribute) GETSTRUCT(atup);
- if (attr->attisdropped)
+ /* Reject system columns */
+ if (!AttrNumberIsForUserDefinedAttr(attr->attnum))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
- errmsg("attribute %d of relation \"%s\" does not exist",
- attnum, RelationGetRelationName(rel))));
+ errmsg("column \"%s\" of relation \"%s\" does not exist",
+ NameStr(*attname), get_rel_name(reloid))));
+ *attnum = attr->attnum;
expr = get_attr_expr(rel, attr->attnum);
/*
@@ -871,8 +865,7 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
stats_check_required_arg(fcinfo, attarginfo, ATTNAME_ARG);
attname = PG_GETARG_NAME(ATTNAME_ARG);
attnum = get_attnum(reloid, NameStr(*attname));
- /* maybe use (!AttrNumberIsForUserDefinedAttr(attnum)) instead? */
- if (attnum <= InvalidAttrNumber)
+ if (!AttrNumberIsForUserDefinedAttr(attnum))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" of relation \"%s\" does not exist",
--
2.47.0