From a38aea3408179db7e2340c9960c819fc7c63f1fb Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Tue, 20 Nov 2012 23:28:41 +0000 Subject: [PATCH] #2101, sql functions for selectivity inspection git-svn-id: http://svn.osgeo.org/postgis/trunk@10725 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/geometry_estimate.c | 21 ++++++++++++++------- postgis/postgis.sql.in.c | 27 +++------------------------ 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/postgis/geometry_estimate.c b/postgis/geometry_estimate.c index 2aebf0a17..461ebffee 100644 --- a/postgis/geometry_estimate.c +++ b/postgis/geometry_estimate.c @@ -116,9 +116,9 @@ static float8 estimate_selectivity(GBOX *box, GEOM_STATS *geomstats); Datum geometry_gist_sel_2d(PG_FUNCTION_ARGS); Datum geometry_gist_joinsel_2d(PG_FUNCTION_ARGS); -Datum geometry_gist_read_selectivity(PG_FUNCTION_ARGS); Datum geometry_analyze_2d(PG_FUNCTION_ARGS); Datum geometry_estimated_extent(PG_FUNCTION_ARGS); +Datum _postgis_geometry_sel(PG_FUNCTION_ARGS); #if ! REALLY_DO_JOINSEL @@ -612,27 +612,34 @@ estimate_selectivity(GBOX *box, GEOM_STATS *geomstats) * Utility function to read the calculated selectivity for a given search * box and table/column. Used for debugging the selectivity code. */ -PG_FUNCTION_INFO_V1(geometry_gist_read_selectivity); -Datum geometry_gist_read_selectivity(PG_FUNCTION_ARGS) +PG_FUNCTION_INFO_V1(_postgis_geometry_sel); +Datum _postgis_geometry_sel(PG_FUNCTION_ARGS) { HeapTuple stats_tuple; float4 *floatptr; - Oid table_oid = PG_GETARG_INT32(0); - int16 attr_num = PG_GETARG_INT32(1); + Oid table_oid = PG_GETARG_OID(0); Datum geom_datum = PG_GETARG_DATUM(2); + text *att_text = PG_GETARG_TEXT_P(1); + const char *att_name = text2cstring(att_text); int rv; GBOX gbox; int32 nvalues = 0; float8 selectivity = 0; + AttrNumber att_num; /* Calculate the gbox */ if ( ! gserialized_datum_get_gbox_p(geom_datum, &gbox) ) elog(ERROR, "Unable to calculate bounding box from geometry"); + + /* Get the attribute number */ + att_num = get_attnum(table_oid, att_name); + if ( ! att_num ) + elog(ERROR, "Unable to attribute number for attribute '%s'", att_name); /* First pull the stats tuple */ - stats_tuple = SearchSysCache2(STATRELATT, ObjectIdGetDatum(table_oid), Int16GetDatum(attr_num)); + stats_tuple = SearchSysCache2(STATRELATT, table_oid, att_num); if ( ! stats_tuple ) - elog(ERROR, "Unable to retreive stats tuple for oid(%d) attrnum(%d), run ANALYZE?", table_oid, attr_num); + elog(ERROR, "Unable to retreive stats tuple for oid(%d) attrnum(%d), run ANALYZE?", table_oid, att_num); /* Then read the geom status histogram from that */ rv = get_attstatsslot(stats_tuple, 0, 0, STATISTIC_KIND_GEOMETRY, InvalidOid, NULL, NULL, NULL, &floatptr, &nvalues); diff --git a/postgis/postgis.sql.in.c b/postgis/postgis.sql.in.c index 39c75bbe6..2dd174074 100644 --- a/postgis/postgis.sql.in.c +++ b/postgis/postgis.sql.in.c @@ -429,31 +429,10 @@ CREATE OR REPLACE FUNCTION geometry_gist_joinsel_2d(internal, oid, internal, sma LANGUAGE 'c'; -- Availability: 2.1.0 -CREATE OR REPLACE FUNCTION geometry_gist_read_selectivity(relid oid, attnum int2, geom geometry) +CREATE OR REPLACE FUNCTION _postgis_geometry_sel(tbl regclass, att_name text, geom geometry) RETURNS float8 - AS 'MODULE_PATHNAME', 'geometry_gist_read_selectivity' - LANGUAGE 'c'; - --- Availability: 2.1.0 -CREATE OR REPLACE FUNCTION geometry_gist_selectivity(schemaname varchar, tablename varchar, attrname varchar, geom geometry) - RETURNS float8 - AS $$ - DECLARE - selectivity float8; - BEGIN - SELECT geometry_gist_read_selectivity(r.oid, a.attnum, 'SRID=4326;LINESTRING(0 0, 1 1)'::geometry) - INTO selectivity - FROM pg_class r - JOIN pg_attribute a ON (r.oid = a.attrelid) - JOIN pg_namespace n ON (r.relnamespace = n.oid) - WHERE n.nspname = $1 AND r.relname = $2 and a.attname = $3; - IF NOT FOUND THEN - RAISE EXCEPTION 'Couldn''t find table? column?'; - END IF; - RETURN selectivity; - END; - $$ - LANGUAGE 'plpgsql'; + AS 'MODULE_PATHNAME', '_postgis_geometry_sel' + LANGUAGE 'c' STRICT; -- 2.40.0