Github user Librago commented on a diff in the pull request: https://github.com/apache/incubator-hawq/pull/1384#discussion_r208439468 --- Diff: src/backend/commands/analyze.c --- @@ -3266,3 +3298,380 @@ static void gp_statistics_estimate_reltuples_relpages_parquet(Relation rel, floa pfree(fstotal); return; } + +/** + * This method estimates the number of tuples and pages in an extern relation. We can not get accurate tuple counts + * and pages counts in the catalog. Therefore, we have to get reltuples and relpages manually. + * + * Input: + * rel - Relation. Must be an external table. + * + * Output: + * reltuples - exact number of tuples in relation. + * relpages - exact number of pages. + */ +static void gp_statistics_estimate_reltuples_relpages_external(Relation rel, float4 *relTuples, float4 *relPages){ + Oid extRelationOid = RelationGetRelid(rel); + getExternalRelTuples(extRelationOid, relTuples); + getExternalRelPages(extRelationOid, relPages, rel); +} + +/** + * This method called by analyzeExternalEstimateReltuplesRelpages, + * to get External Relation reltuple counts, we run count(*) sql manually + * + * Input: + * extRelationOid - External Table Relation Oid + * Output: + * relTuples - exact number of tuples in relation. + */ +static void getExternalRelTuples(Oid extRelationOid, float4 *relTuples){ + const char *schemaName = NULL; + const char *tableName = NULL; + schemaName = get_namespace_name(get_rel_namespace(extRelationOid)); /* must be pfreed */ + tableName = get_rel_name(extRelationOid); /* must be pfreed */ + + StringInfoData str; + initStringInfo(&str); + appendStringInfo(&str, "select count(*)::float4 from %s.%s as Ta", + quote_identifier(schemaName), + quote_identifier(tableName)); + + spiExecuteWithCallback(str.data, false /*readonly*/, 0 /*tcount */, + spiCallback_getSingleResultRowColumnAsFloat4, relTuples); + pfree((void *) tableName); --- End diff -- no need (void *), and does it need to be free?
---