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

Support materialized views #18758

Open
janpio opened this issue Apr 14, 2023 · 10 comments
Open

Support materialized views #18758

janpio opened this issue Apr 14, 2023 · 10 comments
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. topic: views

Comments

@janpio
Copy link
Member

janpio commented Apr 14, 2023

We implemented support for views via #678 and #17335, but as materialized views are special (they need some Client API and would also need to be migrated differently) we currently do not introspect them yet at all and also have no special construct in PSL to differentiate they from normal views.

(If you handwrite a views definition, it might work to be able to access the views. No guarantees though.)

@janpio janpio added kind/feature A request for a new feature. team/schema Issue for team Schema. topic: views labels Apr 14, 2023
@pimeys
Copy link
Contributor

pimeys commented Apr 14, 2023

PostgreSQL has view definitions in pg_views, but this misses the materialized views. To get those, one must look into pg_matviews.

@pimeys
Copy link
Contributor

pimeys commented Apr 14, 2023

There are some interesting questions when implementing this feature, one is what is the hasindexes column and can we somehow get the index information to the PSL.

@pimeys
Copy link
Contributor

pimeys commented Apr 14, 2023

  • PostgreSQL: docs
  • MySQL: no support
  • SQLite: no support
  • SQL Server: there are indexed views which is kind of similar...
  • MongoDB: docs

@janpio
Copy link
Member Author

janpio commented Apr 14, 2023

@mentos1386
Copy link

mentos1386 commented May 19, 2023

@janpio what's the progress on this?

@janpio
Copy link
Member Author

janpio commented Jun 5, 2023

We are currently gathering feedback to our views implementation via #678 and #3108.

You can most probably create a manual model representing your materialized view (even via Introspection if you for example create a non-materialized copy of it) and then use a raw query to trigger the materialization.

@teaforchris
Copy link

teaforchris commented Oct 12, 2023

As a temporary work-around specifically for PostgreSQL, until materialized views are implemented, I found the following worked pretty well for me. I had a really complicated matview with over 30 cols and didn't fancy manually creating it.

  • Create a shadow table which matches the structure of your matview
create table my_matview_shadow as
 select * from my_matview
 where null is not null
;

ℹ️ where null is not null always returns false, so no data is added, since we're only interested in the structure

  • Introspect the db in the usual way
prisma db pull
  • Manually rename the generated relation from model my_matview_shadow to view my_matview
  • Deal with the unique identifier issue by adding @unique to the relevant field (typically id) and removing @@ignore
  • Manually add indexes if you have them on your view
  • Regenerate
prisma generate

...and away you go, query it like a normal view.

@KilianB
Copy link

KilianB commented Feb 22, 2024

We are also extensively using materialized views. Setting up the views by hand once is fine and we can work around it. What we have found to be quite unexpected and inconvenient is the fact that introspection is removing the manually added views again.

Maybe the feature request could be split into a task of 2. generating view definitions and 1. not removing materialized view definitions upon introspection

@egeste

This comment was marked as off-topic.

@leppaott
Copy link

leppaott commented May 6, 2024

Try a view on top of the materialized view.

CREATE VIEW "PrismaView" AS
  SELECT * FROM "MaterializedView";

This seems to work at least migrator-wise. Hmm even access without this to materialized view with the views definition works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. topic: views
Projects
None yet
Development

No branches or pull requests

7 participants