django-postgres-extra icon indicating copy to clipboard operation
django-postgres-extra copied to clipboard

Support for materialized views

Open Photonios opened this issue 7 years ago • 10 comments

https://www.postgresql.org/docs/9.3/static/sql-creatematerializedview.html

Photonios avatar Jun 08 '17 09:06 Photonios

This is a work in progress:

https://github.com/SectorLabs/django-postgres-extra/tree/materialized-views

Photonios avatar Jun 08 '17 09:06 Photonios

Done 😄

Photonios avatar Jul 18 '17 06:07 Photonios

Looks awesome! Thanks for all your work. Have you got a timeline on release of the Materialized View functionality?

richie-blake avatar Jul 30 '17 11:07 richie-blake

We've been testing this internally and we ran into some trouble. The basics are working just fine. It's mostly polishing and smashing some bugs. One of the issues is that I'd like to support Django querysets for the view's query. This is kind of tricky. During migrations, anything that is not in the model's meta objecy, is discarded. That means that the query has to be in the model's meta object. Custom meta options require some monkey patching in Django, which hasn't really worked reliably so far.

It's sort of usuable in its current state, but don't expect too much, and certainely not in migrations.

Thanks for the interest though! I am actively battle testing and working on this. I can't give an exact timeline unfortunately as it depends on a lot of circumstances. I'd say anywhere between a week and a month before I have a completely stable version that I feel comfortable releasing.

Photonios avatar Jul 30 '17 11:07 Photonios

@Photonios Sorry for reviving an old issue. I would like to use materialized views and the documentation does not state any instabilities. Are they resolved? Any pitfalls we should be aware of? Thanks!

GeorgeLubaretsi avatar Sep 17 '17 12:09 GeorgeLubaretsi

like here we can have separate command to sync views instead of using migrations. Any thoughts?

jnoortheen avatar Feb 07 '18 08:02 jnoortheen

@Photonios whatever happened to this? Seems like the people over at https://github.com/mypebble/django-pgviews have stopped supporting it. Would you be up for a PR to add views to this project?

kingbuzzman avatar Sep 24 '19 19:09 kingbuzzman

@Photonios Please take a look at @kingbuzzman's suggestion. Such a solution gives a chance for the development of a frozen project.

Mogost avatar Oct 10 '19 12:10 Mogost

You can see my current progress on this here: https://github.com/SectorLabs/django-postgres-extra/compare/master...views

Creating/dropping views and materialized views works (including automatically generating migrations). Following is still missing:

  • You can't "update" the query backing a materialized view without dropping it and re-creating it. Not sure how the migrator should handle that.
  • No command yet to refresh a materialized view (you can do Model.refresh() tho).

Other than that, the way it works is a little different from django-pgviews. For one, it supports both query sets and raw queries for views/materialized views. I want to make (materialized) views a first class citizen, including support for migrations etc.

Small example of what works now

from psqlextra.models import PostgresMaterializedViewModel

class MyView(PostgresMaterializedViewModel):
       class ViewMeta:
              query = MyOtherModel.objects.filter(name="test1")

this generates a migration that correctly builds a materialized view backed by the query set

python manage.py pgmakemigrations

Photonios avatar Oct 23 '19 12:10 Photonios

@Photonios I see that Materialized Views are in master. They are ready? Can they be tested already? At the moment, I do not see any documentation.

Mogost avatar Dec 09 '19 21:12 Mogost