Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow get_tables to see views and v10-style partitioned tables #2549

Merged
merged 3 commits into from
Jun 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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