From fcd984aca6b5af7d316065ff604d6d8c08d9a847 Mon Sep 17 00:00:00 2001 From: Corey Huinker Date: Mon, 21 May 2018 15:18:59 -0400 Subject: [PATCH 1/3] Allow get_tables to see views and v10-style partitioned tables And it does so with just one query. --- redash/query_runner/pg.py | 48 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index fe3375b896..1bf89182a1 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -106,33 +106,43 @@ def _get_definitions(self, schema, query): schema[table_name]['columns'].append(row['column_name']) def _get_tables(self, schema): - query = """ - SELECT table_schema, table_name, column_name - FROM information_schema.columns - WHERE table_schema NOT IN ('pg_catalog', 'information_schema'); - """ + ''' + relkind constants per https://www.postgresql.org/docs/10/static/catalog-pg-class.html + r = regular table + v = view + m = materialized view + f = foreign table + p = partitioned table (new in 10) + --- + i = index + S = sequence + t = TOAST table + c = composite type + ''' - self._get_definitions(schema, query) - materialized_views_query = """ - SELECT ns.nspname as table_schema, - mv.relname as table_name, - atr.attname as column_name - FROM pg_class mv - JOIN pg_namespace ns ON mv.relnamespace = ns.oid - JOIN pg_attribute atr - ON atr.attrelid = mv.oid - AND atr.attnum > 0 - AND NOT atr.attisdropped - WHERE mv.relkind = 'm'; + query = """ + SELECT s.nspname as table_schema, + c.relname as table_name, + a.attname as column_name + FROM pg_class c + JOIN pg_namespace s + ON c.relnamespace = s.oid + AND s.nspname NOT IN ('pg_catalog', 'information_schema') + JOIN pg_attribute a + ON a.attrelid = c.oid + AND a.attnum > 0 + AND NOT a.attisdropped + WHERE c.relkind IN ('r', 'v', 'm', 'f', 'p') """ - self._get_definitions(schema, materialized_views_query) + self._get_definitions(schema, query) return schema.values() def _get_connection(self): - connection = psycopg2.connect(user=self.configuration.get('user'), + + connection = psycopg2.connect(user=self.configuration.get('user'), password=self.configuration.get('password'), host=self.configuration.get('host'), port=self.configuration.get('port'), From 95593481a5b0df223a8670580d1040332363cb27 Mon Sep 17 00:00:00 2001 From: Corey Huinker Date: Mon, 21 May 2018 15:26:46 -0400 Subject: [PATCH 2/3] Update pg.py --- redash/query_runner/pg.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index 1bf89182a1..f343ce73a0 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -141,8 +141,7 @@ def _get_tables(self, schema): return schema.values() def _get_connection(self): - - connection = psycopg2.connect(user=self.configuration.get('user'), + connection = psycopg2.connect(user=self.configuration.get('user'), password=self.configuration.get('password'), host=self.configuration.get('host'), port=self.configuration.get('port'), From 76e86817c8d9fb67c8681b5bdef8dced77ecf90b Mon Sep 17 00:00:00 2001 From: Corey Huinker Date: Mon, 21 May 2018 15:37:00 -0400 Subject: [PATCH 3/3] remove blank line --- redash/query_runner/pg.py | 1 - 1 file changed, 1 deletion(-) diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index f343ce73a0..2c90e00570 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -120,7 +120,6 @@ def _get_tables(self, schema): c = composite type ''' - query = """ SELECT s.nspname as table_schema, c.relname as table_name,