Skip to content
Advertisement

Emulating materialized views in PostgreSQL with concurrent refreshes

I’m using PostgreSQL 9.2.4 and would like to emulate a materialized view. Are there any well-known methods for doing this, including concurrent refreshes?

Advertisement

Answer

PostgreSQL wiki – materialized views links to two trigger-based implementations.

The general idea is to put AFTER INSERT OR UPDATE OR DELETE ... FOR EACH ROW triggers on each involved table that do partial updates on the target table. Implementation is fairly specific to the nature of the view.

For some more complex views you can’t really do partial updates and need to do a concurrent view refresh instead. That usually involves creating a new table, populating it, committing, beginning a new transaction, dropping the old table, renaming the new one to the name of the old one, and committing again.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement