pass Form_pg_attribute to examine_attribute rather than Relation structure.

Started by amul sulover 11 years ago2 messages
#1amul sul
sul_amul@yahoo.co.in
1 attachment(s)

Hi,

examine_attribute() function accept Relation type argument, and extract attribute from it.

For more granularity, I think passing Form_pg_attribute to examine_attribute() function  rather than passing Relation will be more relevant & makes it simple to understand.

Thinking to change examine_attribute as,

- examine_attribute(Relation onerel, int attnum, Node *index_expr)
+examine_attribute(Form_pg_attribute attr, Node *index_expr)
 

I think there wont any loss or gain except little optimization of function call stack space. 

Thoughts? comments?

Please have look attached patch.

Regards,

Amul Sul

Attachments:

0001-examine_attribute-function-s-arguments-changed.patchapplication/octet-stream; name=0001-examine_attribute-function-s-arguments-changed.patchDownload
From ae53ad69e61e56b0e2615ca1559364b502434b84 Mon Sep 17 00:00:00 2001
From: Amul Sul <sul_amul@yahoo.co.in>
Date: Wed, 4 Jun 2014 15:43:52 +0530
Subject: [PATCH] examine_attribute function's arguments changed.
 Pass single attribute rather than whole Relation and attribute index

---
 src/backend/commands/analyze.c |   21 +++++++++++----------
 1 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index c09ca7e..aa909ee 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -96,7 +96,7 @@ static void compute_index_stats(Relation onerel, double totalrows,
 					AnlIndexData *indexdata, int nindexes,
 					HeapTuple *rows, int numrows,
 					MemoryContext col_context);
-static VacAttrStats *examine_attribute(Relation onerel, int attnum,
+static VacAttrStats *examine_attribute(Form_pg_attribute attr,
 				  Node *index_expr);
 static int acquire_sample_rows(Relation onerel, int elevel,
 					HeapTuple *rows, int targrows,
@@ -386,9 +386,10 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
 						(errcode(ERRCODE_UNDEFINED_COLUMN),
 					errmsg("column \"%s\" of relation \"%s\" does not exist",
 						   col, RelationGetRelationName(onerel))));
-			vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
+			vacattrstats[tcnt] = examine_attribute(onerel->rd_att->attrs[i - 1],
+																	NULL);
 			if (vacattrstats[tcnt] != NULL)
-				tcnt++;
+				vacattrstats[tcnt++]->tupattnum = i;
 		}
 		attr_cnt = tcnt;
 	}
@@ -400,9 +401,10 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
 		tcnt = 0;
 		for (i = 1; i <= attr_cnt; i++)
 		{
-			vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
+			vacattrstats[tcnt] = examine_attribute(onerel->rd_att->attrs[i - 1],
+																	NULL);
 			if (vacattrstats[tcnt] != NULL)
-				tcnt++;
+				vacattrstats[tcnt++]->tupattnum = i;
 		}
 		attr_cnt = tcnt;
 	}
@@ -454,9 +456,10 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
 						indexkey = (Node *) lfirst(indexpr_item);
 						indexpr_item = lnext(indexpr_item);
 						thisdata->vacattrstats[tcnt] =
-							examine_attribute(Irel[ind], i + 1, indexkey);
+							examine_attribute(Irel[ind]->rd_att->attrs[i],
+																	indexkey);
 						if (thisdata->vacattrstats[tcnt] != NULL)
-							tcnt++;
+							thisdata->vacattrstats[tcnt++]->tupattnum = i + 1;
 					}
 				}
 				thisdata->attr_cnt = tcnt;
@@ -855,9 +858,8 @@ compute_index_stats(Relation onerel, double totalrows,
  * and index_expr is the expression tree representing the column's data.
  */
 static VacAttrStats *
-examine_attribute(Relation onerel, int attnum, Node *index_expr)
+examine_attribute(Form_pg_attribute attr, Node *index_expr)
 {
-	Form_pg_attribute attr = onerel->rd_att->attrs[attnum - 1];
 	HeapTuple	typtuple;
 	VacAttrStats *stats;
 	int			i;
@@ -905,7 +907,6 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
 		elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
 	stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
 	stats->anl_context = anl_context;
-	stats->tupattnum = attnum;
 
 	/*
 	 * The fields describing the stats->stavalues[n] element types default to
-- 
1.7.1

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: amul sul (#1)
Re: pass Form_pg_attribute to examine_attribute rather than Relation structure.

amul sul <sul_amul@yahoo.co.in> writes:

For more granularity,�I think passing�Form_pg_attribute to�examine_attribute() function �rather than passing Relation will be more relevant & makes it simple to understand.

I don't find that to be a good idea at all. It makes examine_attribute
inconsistent with most other functions in analyze.c, and it limits our
ability to add logic inside that function that might want to look at
other properties of the relation.

Even without that argument, moving the responsibility for initializing
stats->tupattnum to the callers of examine_attribute is certainly a
net loss in readability and reliability.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers