Blog post

Postgres Views

11-18-20209 minute read

A quick summary of Postgres views, materialized views, and why you should use them.

What is a View?

A view is a convenient shortcut to a query. Creating a view does not involve new tables or data. When run, an underlying query is executed, returning its results to the user.

Basic Example

Say we have the following tables from a database of a university:

students

idnametype
1Arunundergraduate
2Zackgraduate
3Joygraduate

courses

idtitlecode
1Introduction to PostgresPG101
2Authentication TheoriesAUTH205
3Fundamentals of SupabaseSUP412

grades

idstudent_idcourse_idresult
111B+
213A+
322A
431A-
532A
633B-

Creating a view consisting of all the three tables will look like this:

1create view transcripts as
2    select
3        students.name,
4        students.type,
5        courses.title,
6        courses.code,
7        grades.result
8    from grades
9    left join students where grades.student_id = students.id
10    left join courses where grades.course_id = courses.id;
11

Once done, we can now access the underlying query with:

1select * from transcripts;
2

For additional parameters or options, refer here.

Why should we use Views?

Views provide the several benefits:

  • Simplicity
  • Consistency
  • Logical Organization
  • Security

Simplicity

As a query becomes complex it becomes a hassle to call it. Especially when we run it at regularly. In the example above, instead of repeatedly running:

1select
2    students.name,
3    students.type,
4    courses.title,
5    courses.code,
6    grades.result
7from grades
8left join students where grades.student_id = students.id
9left join courses where grades.course_id = courses.id;
10

We can run this instead:

1select * from transcripts;
2

Additionally, a view behaves like a typical table. We can safely use it in table JOINs or even create new views using existing views.

Consistency

Views ensure that the likelihood of mistakes decreases when repeatedly executing a query. In our example above, we may decide that we want to exclude the course Introduction to Postgres. The query would become:

1select
2    students.name,
3    students.type,
4    courses.title,
5    courses.code,
6    grades.result
7from grades
8    left join students where grades.student_id = students.id
9    left join courses where grades.course_id = courses.id
10where courses.code != 'PG101';
11

Without a view, we would need to go into every dependent query to add the new rule. This would increase in the likelihood of errors and inconsistencies, as well as introducing a lot of effort for a developer. With views, we can alter just the underlying query in the view transcripts. The change will be applied to all applications using this view.

Logical Organization

With views, we can give our query a name. This is extremely useful for teams working with the same database. Instead of guessing what a query is supposed to do, a well-named view can easily explain it. For example, by looking at the name of the view transcripts, we can infer that the underlying query might involve the students, courses, and grades tables.

Security

Views can restrict the amount and type of data presented to a user. Instead of allowing a user direct access to a set of tables, we provide them a view instead. We can prevent them from reading sensitive columns by excluding them from the underlying query.

What is a Materialized View?

A materialized view is a form of view but with the added feature of physically storing the results. In subsequent reads of a materialized view, the time taken to return its results would be much faster than a conventional view. This is because the data is readily available for a materialized view while the conventional view executes the underlying query each time it is called.

Basic Example

Using our example above, a materialized view can be created like this:

1create materialized view transcripts as
2    select
3        students.name,
4        students.type,
5        courses.title,
6        courses.code,
7        grades.result
8    from grades
9    left join students where grades.student_id = students.id
10    left join courses where grades.course_id = courses.id;
11

Reading from the materialized view is the same as a conventional view:

1select * from transcripts;
2

For additional parameters or options, refer here.

Refreshing

Unfortunately, there is a trade-off - data in materialized views are not always up to date. We need to refresh it regularly to prevent the data from becoming too stale. To do so:

1refresh materialized view transcripts;
2

It's up to you how regularly refresh your materialized views, and it's probably different for each view depending on its use-case.

Materialized views vs Conventional views

Materialized views are useful when execution times for queries or views become unbearable or exceed the service level agreements of a business. These could likely occur in views or queries involving multiple tables and millions of rows. When using such a view, however, there should be tolerance towards data being outdated. Some use-cases for materialized views are internal dashboards and analytics.

Creating a materialized view is not a solution to inefficient queries. You should always seek to optimize a slow running query even if you are implementing a materialized view.

Conclusion

Postgres views and materialized views are a great way to organize and view results from commonly used queries. Although similar to one another, each has its purpose. Views simplify the process of running queries. Materialized views are usually faster at returning results, with the trade-off of having stale data.

Last post

Supabase Alpha November 2020

1 December 2020
supabase
Next post

Supabase Alpha October 2020

2 November 2020
supabase
Related articles
Protecting reserved roles with PostgreSQL Hooks
Developers stay up to date with intheloop.dev
Using Supabase in Replit
Postgres as a CRON Server
Cracking PostgreSQL Interview Questions
View all posts

Build in a weekend, scale to millions