I'm working on a project where I need to calculate the final cost (gasto) for each expense by applying various taxes. I also need to implement Row-Level Security (RLS) to restrict data access based on the user ID.
Context:
- I have a table called
gastosthat stores individual expenses. - I have another table called
taxesValuesthat stores different types of taxes. - A third table,
gastosAppliedTaxes, establishes a many-to-many relationship betweengastosandtaxesValues, indicating which taxes are applied to each expense. - I initially used a view called
final_gastosto calculate the final cost (gasto) for each expense after applying the taxes. The view essentially joinsgastos,taxesValues, andgastosAppliedTaxesto perform this calculation.
Why Materialized View?
I opted for a materialized view for two main reasons:
Performance: Materialized views store the result of the query, making data retrieval faster, especially when dealing with complex joins and calculations.
Row-Level Security (RLS): Unlike regular views, materialized views in PostgreSQL allow for the application of RLS policies. This is crucial for my application as I need to restrict data based on the user ID.
Questions:
Materialized Views for RLS:
Given the above, I switched from using a view to a materialized view calledfinal_gastosso that I can apply RLS. Is this a good approach? Are there any downsides or better alternatives?Supabase UI Limitations:
I noticed that the Supabase UI doesn't show policies for materialized views and lacks a UI for setting policies like it does for normal tables. Is this a limitation of Supabase, or am I missing something?