From fab6c1d1aa1af067978a746949222eff4a2ad5da Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Wed, 5 Dec 2012 18:50:42 +0000 Subject: [PATCH] #2132, _postgis_stats crashes backend on missing stats git-svn-id: http://svn.osgeo.org/postgis/trunk@10800 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/gserialized_estimate.c | 17 +++++++++++++---- regress/regress_selectivity.sql | 13 +++++++++++++ regress/regress_selectivity_expected | 4 ++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/postgis/gserialized_estimate.c b/postgis/gserialized_estimate.c index f1d3f957d..7b8aa3c29 100644 --- a/postgis/gserialized_estimate.c +++ b/postgis/gserialized_estimate.c @@ -1873,6 +1873,8 @@ estimate_selectivity(const GBOX *box, const ND_STATS *nd_stats, int mode) PG_FUNCTION_INFO_V1(_postgis_gserialized_stats); Datum _postgis_gserialized_stats(PG_FUNCTION_ARGS) { + Oid table_oid = PG_GETARG_OID(0); + text *att_text = PG_GETARG_TEXT_P(1); ND_STATS *nd_stats; char *str; text *json; @@ -1883,7 +1885,11 @@ Datum _postgis_gserialized_stats(PG_FUNCTION_ARGS) mode = text_p_get_mode(PG_GETARG_TEXT_P(2)); /* Retrieve the stats object */ - nd_stats = pg_get_nd_stats_by_name(PG_GETARG_OID(0), PG_GETARG_TEXT_P(1), mode); + nd_stats = pg_get_nd_stats_by_name(table_oid, att_text, mode); + if ( ! nd_stats ) + elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid), text2cstring(att_text)); + + /* Convert to JSON */ str = nd_stats_to_json(nd_stats); json = cstring2text(str); pfree(str); @@ -1915,7 +1921,7 @@ Datum _postgis_gserialized_sel(PG_FUNCTION_ARGS) nd_stats = pg_get_nd_stats_by_name(table_oid, att_text, mode); if ( ! nd_stats ) - elog(ERROR, "unable to load statistics! analyze?"); + elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid), text2cstring(att_text)); /* Calculate the gbox */ if ( ! gserialized_datum_get_gbox_p(geom_datum, &gbox) ) @@ -1951,8 +1957,11 @@ Datum _postgis_gserialized_joinsel(PG_FUNCTION_ARGS) nd_stats1 = pg_get_nd_stats_by_name(table_oid1, att_text1, mode); nd_stats2 = pg_get_nd_stats_by_name(table_oid2, att_text2, mode); - if ( ! ( nd_stats1 && nd_stats2 ) ) - elog(ERROR, "unable to load statistics! analyze?"); + if ( ! nd_stats1 ) + elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid1), text2cstring(att_text1)); + + if ( ! nd_stats2 ) + elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid2), text2cstring(att_text2)); /* Check if we've been asked to not use 2d mode */ if ( ! PG_ARGISNULL(4) ) diff --git a/regress/regress_selectivity.sql b/regress/regress_selectivity.sql index ba3d63cd2..6ea5f7bd3 100644 --- a/regress/regress_selectivity.sql +++ b/regress/regress_selectivity.sql @@ -1,3 +1,16 @@ + +-- Check for error messages +create table no_stats ( g geometry, id integer ); +create table no_stats_join ( g geometry, id integer ); +select _postgis_selectivity('no_stats','g', 'LINESTRING(0 0, 1 1)'); +select _postgis_stats('no_stats','g'); +select _postgis_join_selectivity('no_stats', 'g', 'no_stats_join', 'g'); +insert into no_stats (g, id) values ('POINT(0 0)', 0); +analyze no_stats; +select _postgis_join_selectivity('no_stats', 'g', 'no_stats_join', 'g'); +drop table no_stats; +drop table no_stats_join; + -- Table with uniformly variable density, highest at 1,1, lowest at 10,10 create table regular_overdots as with diff --git a/regress/regress_selectivity_expected b/regress/regress_selectivity_expected index 97acacc92..1ce252849 100644 --- a/regress/regress_selectivity_expected +++ b/regress/regress_selectivity_expected @@ -1,3 +1,7 @@ +ERROR: stats for "no_stats.g" do not exist +ERROR: stats for "no_stats.g" do not exist +ERROR: stats for "no_stats.g" do not exist +ERROR: stats for "no_stats_join.g" do not exist selectivity_00|2127 selectivity_01|1068 selectivity_02|actual|0.502 -- 2.40.0