Skip to content

Commit

Permalink
Merge pull request getredash#2549 from coreyhuinker/patch-1
Browse files Browse the repository at this point in the history
Allow get_tables to see views and v10-style partitioned tables
  • Loading branch information
arikfr authored Jun 6, 2018
2 parents 8bffc6f + 76e8681 commit 672c86e
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions redash/query_runner/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,37 @@ 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 table_schema, table_name, column_name
FROM information_schema.columns
WHERE table_schema NOT IN ('pg_catalog', 'information_schema');
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, 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';
"""

self._get_definitions(schema, materialized_views_query)

return schema.values()

def _get_connection(self):
Expand Down

0 comments on commit 672c86e

Please sign in to comment.