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

materialized views in dbListTables() #251

Open
dpprdan opened this issue Aug 17, 2020 · 7 comments · May be fixed by #414
Open

materialized views in dbListTables() #251

dpprdan opened this issue Aug 17, 2020 · 7 comments · May be fixed by #414
Labels

Comments

@dpprdan
Copy link
Contributor

dpprdan commented Aug 17, 2020

Should Materialized Views be listed by dbListTables()?

I'd argue they should since they are objects that share characteristics of both Tables and Views, both of which are returned by dbListTables(), see #27 & #29.

See this thread on the psql-hackers list for a discussion on why they are not in the INFORMATION_SCHEMA.tables (which includes Views). Tl;dr: "They are not defined by the SQL standard." I see this as a relatively minor point w.r.t. RPostgres, though.

As it stands there is no helper function like dbListTables() to list Materialized Views in RPostgres.

The following query shows all Tables, Views and Materialized Views (I have not looked into temporary tables, yet):

SELECT tablename AS name FROM pg_tables WHERE schemaname = ANY (current_schemas(false))
UNION
SELECT viewname AS name FROM pg_views WHERE schemaname = ANY (current_schemas(false))
UNION
SELECT matviewname as name FROM pg_matviews WHERE schemaname = ANY (current_schemas(false))
ORDER BY name;
@dpprdan
Copy link
Contributor Author

dpprdan commented Aug 18, 2020

Probably a better implementation and one that is closer to the current one:

SELECT c.relname AS name
FROM   pg_class AS c 
  JOIN pg_namespace AS n 
  ON   c.relnamespace = n.oid
WHERE  (n.nspname = ANY (current_schemas(true)))
  AND  (n.nspname <> 'pg_catalog')
  AND  (relkind IN ('r', 'p', 'f', 'v', 'm'))
  AND NOT relispartition
ORDER BY name

This also includes foreign and partitioned tables like INFORMATION_SCHEMA.tables (pg_class documentation).

AND NOT relispartition omits the partitions of a parrtitioned table. Tables with many partitions would otherwise clutter the list and I don't see a use-case for accessing the partitions directly from R. I might be missing something, though.

@krlmlr
Copy link
Member

krlmlr commented Sep 28, 2020

Thanks. This sounds like a useful extension. Would you like to submit a pull request?

dpprdan added a commit to dpprdan/RPostgres that referenced this issue Sep 28, 2020
use pg_class/pg_namespace instead of information_schema.tables
@dpprdan
Copy link
Contributor Author

dpprdan commented Sep 28, 2020

I submitted a PR for dbListTables() (#261), but now realise that this probably should extend to dbExistsTable() and dbListFields() as well?! Update: And also dbListObjects().

I assume that find_table(), which currently uses INFORMATION_SCHEMA, would have to be adjusted, but I have not fully grasped how that function works, yet.

@krlmlr
Copy link
Member

krlmlr commented Dec 27, 2020

Thanks for the PR. Yes, we should support all methods.

find_table() composes a query against INFORMATION_SCHEMA. We need to determine where and how materialized views are stored in INFORMATION_SCHEMA, and adjust accordingly.

@krlmlr
Copy link
Member

krlmlr commented Dec 27, 2020

If you prefer staying in line with your pull request and use Postgres-specific internal views, we need to rewrite so that all use cases of find_table() are covered.

dpprdan added a commit to dpprdan/RPostgres that referenced this issue Mar 31, 2021
use pg_class/pg_namespace instead of information_schema.tables
@krlmlr
Copy link
Member

krlmlr commented Jul 2, 2021

@krlmlr
Copy link
Member

krlmlr commented Jul 2, 2021

Ancient dplyr issue: tidyverse/dplyr#1007.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants