From 3aa0f88b6eb8710ea80c9a14d9d0dacb028c5ae0 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 2 Jul 2012 16:14:28 +0100 Subject: [PATCH] MDL-34145 postgres driver: fix get_tables perf on <= 9.0 The query of the information_schema view seems to be painfully slow on older versions of postgres (2000ms instead of 20ms). Therefore, it is worth detecting that case, and using a more postgres specific feedback. This is particularly important for 2.3+ unit tests. Thanks to Petr Skoda for his help with this. --- lib/dml/pgsql_native_moodle_database.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 07de23e786677..782ab11a48931 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -278,12 +278,21 @@ public function get_tables($usecache=true) { } $this->tables = array(); $prefix = str_replace('_', '|_', $this->prefix); - // Get them from information_schema instead of catalog as far as - // we want to get only own session temp objects (catalog returns all) - $sql = "SELECT table_name - FROM information_schema.tables - WHERE table_name LIKE '$prefix%' ESCAPE '|' - AND table_type IN ('BASE TABLE', 'LOCAL TEMPORARY')"; + if ($this->is_min_version('9.1')) { + // Use ANSI standard information_schema in recent versions where it is fast enough. + $sql = "SELECT table_name + FROM information_schema.tables + WHERE table_name LIKE '$prefix%' ESCAPE '|' + AND table_type IN ('BASE TABLE', 'LOCAL TEMPORARY')"; + } else { + // information_schema is horribly slow in <= 9.0, so use pg internals. + // Note the pg_is_other_temp_schema. We only want temp objects from our own session. + $sql = "SELECT c.relname + FROM pg_class c + WHERE c.relname LIKE '$prefix%' ESCAPE '|' + AND c.relkind = 'r' + AND NOT pg_is_other_temp_schema(c.relnamespace)"; + } $this->query_start($sql, null, SQL_QUERY_AUX); $result = pg_query($this->pgsql, $sql); $this->query_end($result);