Revitalizing Outdated Data Models with PostgreSQL Views
Newvick Lee
Software developer, Careteam Technologies
POSETTE | June 11-13, 2024
Newvick Lee
You’re using a complicated and/or outdated data model.
What do you do?
You’re using a complicated and/or outdated data model.
What do you do?
You’re using a complicated and/or outdated data model.
What do you do?
You’re using a complicated and/or outdated data model.
What do you do?
Goal 🎯: Learn about PostgreSQL Views � and how to use them � to improve your data models
Outline
☐ Review an example case
☐ What is a PostgreSQL View and how does it help?
☐ Creating a View
☐ Updatable Views
☐ Using triggers to augment updates
☐ Useful parameters
☐ Views vs Materialized Views
Case study: A complicated data model� with multiple polymorphic � associations
🦆
🐓
🦆
🦆
🦆
🦆
Polymorphic
association
Polymorphic
association
How would you query for a chicken in a large barn?
Table chicken.id
Table large_barn.id
How would you query for a duck in a small barn?
Table duck.id
Table small_barn.id
Complications?
🐐
🐎
🐄
🦆
🐄
🐓
🦆
🦆
🐎
🐑
🐑
🐑
🐑
🐑
🦆
🦆
What is a PostgreSQL View?
What is a PostgreSQL View?
Application: Using PostgreSQL Views to� help with our example case
Querying for a chicken in a large barn
Creating a view for chicken_in_large_barn
Querying for chicken in the large barn
★
★
Original
Using a View
🆚
Querying for chicken in the large barn
Querying for chicken in the large barn
Outcome: Querying is much simpler now!
But… What about other actions?
insert/ update/ delete
Views are updatable
Automatically updatable, if they satisfy all of these conditions:
This is possible…
This is possible…
But animal_type and barn_type will be NULL
We can use a trigger to replace the insert on the view
We can use a trigger to replace the insert on the view
We can use a trigger to replace the insert on the view
We can use a trigger to replace the insert on the view
We can use a trigger to replace the insert on the View
Insight: What is the trigger function doing?
Insert now fills animal_type and barn_type columns
chicken_id
Insert now fills animal_type and barn_type columns
large_barn_id
Insight: The same method will work � for update and delete
Insight: Views can easily improve � a data model without affecting� the underlying tables
Useful View parameters
Useful View parameters
WITH [ CASCADED | LOCAL ] CHECK OPTION
Useful View parameters
WITH ( view_option_name [= view_option_value] [, ... ] )
Lastly, Views vs Materialized Views
Views | Materialized Views |
Virtual (does not store data) | Physical (store data) |
Common use cases:�- Simplifying data models�- Encapsulating complex� queries | Common use cases:�- Improve performance for� read-heavy workloads�- Aggregating and summarizing� data (analytics) |
Goal 🎯: Learn about PostgreSQL Views and how � to use them to improve your data models
✅ Views are virtual
✅ Example case: create view as chicken_large_barn
✅ Can be automatically updatable (insert/update/delete)
✅ Can use trigger functions to augment updates
✅ Useful view parameters
✅ Views vs Materialized Views
You’re using a complicated and/or outdated data model.
What do you do now?