From 8f11e930d317e83b42f3dd0adbb47044fd89e962 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Fri, 17 Apr 2015 12:37:06 -0400 Subject: [PATCH] LP#1438136: Protect PG < 9.2 from our stats probe Signed-off-by: Mike Rylander Signed-off-by: Chris Sharp Signed-off-by: Jason Stephenson --- .../Application/Storage/Driver/Pg/QueryParser.pm | 22 ++++++++++++++++------ Open-ILS/src/sql/Pg/300.schema.staged_search.sql | 1 + .../src/sql/Pg/upgrade/XXXX.schema.vlist_freq.sql | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm index ac3174facf..84fdbeea79 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm @@ -697,6 +697,7 @@ my $apputils = "OpenILS::Application::AppUtils"; our %_dfilter_controlled_cache = (); our %_dfilter_stats_cache = (); +our $_pg_version = 0; sub dynamic_filter_compile { my ($self, $filter, $params, $negate) = @_; @@ -704,11 +705,20 @@ sub dynamic_filter_compile { $negate = $negate ? '!' : ''; - if (!scalar keys %_dfilter_stats_cache) { - my $data = $e->json_query({from => ['evergreen.pg_statistics', 'record_attr_vector_list', 'vlist']}); - %_dfilter_stats_cache = map { - ( $_->{element}, $_->{frequency} ) - } grep { $_->{frequency} > 5 } @$data; # Pin floor to 5% of the table + if (!$_pg_version) { + ($_pg_version = $e->json_query({from => ['version']})->[0]->{version}) =~ s/^.+?(\d\.\d).+$/$1/; + } + + my $common = 0; + if ($_pg_version >= 9.2) { + if (!scalar keys %_dfilter_stats_cache) { + my $data = $e->json_query({from => ['evergreen.pg_statistics', 'record_attr_vector_list', 'vlist']}); + %_dfilter_stats_cache = map { + ( $_->{element}, $_->{frequency} ) + } grep { $_->{frequency} > 5 } @$data; # Pin floor to 5% of the table + } + } else { + $common = 1; # Assume it's expensive } if (!exists($_dfilter_controlled_cache{$filter})) { @@ -727,7 +737,7 @@ sub dynamic_filter_compile { 'code' : 'value'; my $attr_objects = $e->$method({ $attr_field => $filter, $value_field => $params }); - my $common = scalar(grep { exists($_dfilter_stats_cache{$_->id}) } @$attr_objects); + $common = scalar(grep { exists($_dfilter_stats_cache{$_->id}) } @$attr_objects) unless $common; return (sprintf('%s(%s)', $negate, join( diff --git a/Open-ILS/src/sql/Pg/300.schema.staged_search.sql b/Open-ILS/src/sql/Pg/300.schema.staged_search.sql index cb4338483b..f405c1ae50 100644 --- a/Open-ILS/src/sql/Pg/300.schema.staged_search.sql +++ b/Open-ILS/src/sql/Pg/300.schema.staged_search.sql @@ -23,6 +23,7 @@ CREATE SCHEMA search; CREATE OR REPLACE FUNCTION evergreen.pg_statistics (tab TEXT, col TEXT) RETURNS TABLE(element TEXT, frequency INT) AS $$ BEGIN + -- This query will die on PG < 9.2, but the function can be created. We just won't use it where we can't. RETURN QUERY SELECT e, f diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.vlist_freq.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.vlist_freq.sql index cff061a1da..2b8e8429a0 100644 --- a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.vlist_freq.sql +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.vlist_freq.sql @@ -4,6 +4,7 @@ SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); CREATE OR REPLACE FUNCTION evergreen.pg_statistics (tab TEXT, col TEXT) RETURNS TABLE(element TEXT, frequency INT) AS $$ BEGIN + -- This query will die on PG < 9.2, but the function can be created. We just won't use it where we can't. RETURN QUERY SELECT e, f -- 2.11.0