From f0071352047aa4cc7e3822101f9d30a67e12c8e3 Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Mon, 17 Oct 2016 18:04:03 +0300 Subject: [PATCH] Try to reduce CPU consumption 1 use only one hash lookup to get Relation 2 move pgstat_fetch_stat_tabentry() to the last check, because this call is rather expensive --- online_analyze.c | 101 +++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/online_analyze.c b/online_analyze.c index 0414a81..0d85df6 100644 --- a/online_analyze.c +++ b/online_analyze.c @@ -334,8 +334,59 @@ makeAnalyze(Oid relOid, CmdType operation, int32 naffected) /* number if affected rows is unknown */ naffected = 0; - if (get_rel_relkind(relOid) != RELKIND_RELATION) - return; + /* + * includeTables overwrites excludeTables + */ + switch(online_analyze_table_type) + { + case OATT_ALL: + if (get_rel_relkind(relOid) != RELKIND_RELATION || + (matchOid(&excludeTables, relOid) == true && + matchOid(&includeTables, relOid) == false)) + return; + break; + case OATT_NONE: + if (get_rel_relkind(relOid) != RELKIND_RELATION || + matchOid(&includeTables, relOid) == false) + return; + break; + case OATT_TEMPORARY: + case OATT_PERSISTENT: + default: + { + Relation rel; + OnlineAnalyzeTableType reltype; + + rel = RelationIdGetRelation(relOid); + + if (rel->rd_rel->relkind != RELKIND_RELATION) + { + RelationClose(rel); + return; + } + + reltype = +#if PG_VERSION_NUM >= 90100 + (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP) +#else + (rel->rd_istemp || rel->rd_islocaltemp) +#endif + ? OATT_TEMPORARY : OATT_PERSISTENT; + RelationClose(rel); + + /* + * skip analyze if relation's type doesn't not match + * online_analyze_table_type + */ + if ((online_analyze_table_type & reltype) == 0 || + matchOid(&excludeTables, relOid) == true) + { + if (matchOid(&includeTables, relOid) == false) + return; + } + break; + } + } tabentry = pgstat_fetch_stat_tabentry(relOid); @@ -367,51 +418,6 @@ makeAnalyze(Oid relOid, CmdType operation, int32 naffected) memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */ - /* - * includeTables overwrites excludeTables - */ - switch(online_analyze_table_type) - { - case OATT_ALL: - if (matchOid(&excludeTables, relOid) == true && - matchOid(&includeTables, relOid) == false) - return; - break; - case OATT_NONE: - if (matchOid(&includeTables, relOid) == false) - return; - break; - case OATT_TEMPORARY: - case OATT_PERSISTENT: - default: - { - Relation rel; - OnlineAnalyzeTableType reltype; - - rel = RelationIdGetRelation(relOid); - reltype = -#if PG_VERSION_NUM >= 90100 - (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP) -#else - (rel->rd_istemp || rel->rd_islocaltemp) -#endif - ? OATT_TEMPORARY : OATT_PERSISTENT; - RelationClose(rel); - - /* - * skip analyze if relation's type doesn't not match - * online_analyze_table_type - */ - if ((online_analyze_table_type & reltype) == 0 || - matchOid(&excludeTables, relOid) == true) - { - if (matchOid(&includeTables, relOid) == false) - return; - } - } - break; - } - memset(&vacstmt, 0, sizeof(vacstmt)); vacstmt.freeze_min_age = -1; @@ -468,7 +474,6 @@ makeAnalyze(Oid relOid, CmdType operation, int32 naffected) ((double)secs) + ((double)microsecs)/1.0e6); } - if (tabentry == NULL) { /* new table */ -- 2.46.1