From f92b5971ac7ecd3e77b664f8b219637d8f93acef Mon Sep 17 00:00:00 2001 From: Shinsuke Nara Date: Fri, 15 Mar 2019 17:34:51 +0900 Subject: [PATCH 1/3] Show accessible tables only. --- redash/query_runner/pg.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index 048f5880f0..6d7352b5ec 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -147,7 +147,12 @@ def _get_tables(self, schema): FROM pg_class c JOIN pg_namespace s ON c.relnamespace = s.oid - AND s.nspname NOT IN ('pg_catalog', 'information_schema') + AND s.nspname IN ( + SELECT table_schema + FROM information_schema.table_privileges + WHERE grantee = (SELECT current_user) + AND table_schema NOT IN ('pg_catalog', 'information_schema') + GROUP BY table_schema) JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 From 9314e5203209272f126121af8f05ec024b317eb1 Mon Sep 17 00:00:00 2001 From: Shinsuke Nara Date: Mon, 18 Mar 2019 18:59:06 +0900 Subject: [PATCH 2/3] Get table information from information_schema.columns. --- redash/query_runner/pg.py | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index 6d7352b5ec..bc37f0d57e 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -126,38 +126,12 @@ def _get_definitions(self, schema, query): schema[table_name]['columns'].append(row['column_name']) def _get_tables(self, 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 - ''' - 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 IN ( - SELECT table_schema - FROM information_schema.table_privileges - WHERE grantee = (SELECT current_user) - AND table_schema NOT IN ('pg_catalog', 'information_schema') - GROUP BY table_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') + SELECT table_schema, + table_name, + column_name + FROM information_schema.columns + WHERE table_schema NOT IN ('pg_catalog', 'information_schema') """ self._get_definitions(schema, query) From d766d2dfdb59a4cc64eb7171e4ff0219eae15715 Mon Sep 17 00:00:00 2001 From: Shinsuke Nara Date: Tue, 26 Mar 2019 12:43:54 +0900 Subject: [PATCH 3/3] Union old query. --- redash/query_runner/pg.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index bc37f0d57e..5524375de6 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -126,7 +126,36 @@ def _get_definitions(self, schema, query): schema[table_name]['columns'].append(row['column_name']) def _get_tables(self, 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 + ''' + 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 ('m', 'f', 'p') + + UNION + SELECT table_schema, table_name, column_name