1 of 47

Revitalizing Outdated Data Models with PostgreSQL Views

Newvick Lee

Software developer, Careteam Technologies

POSETTE | June 11-13, 2024

Newvick Lee

2 of 47

You’re using a complicated and/or outdated data model.

What do you do?

3 of 47

You’re using a complicated and/or outdated data model.

What do you do?

  1. Refactor the data model and related code

4 of 47

You’re using a complicated and/or outdated data model.

What do you do?

  • Refactor the data model and related code
  • Give up and continue using it grudgingly

5 of 47

You’re using a complicated and/or outdated data model.

What do you do?

  • Refactor the data model and related code
  • Give up and continue using it grudgingly
  • Something else?

6 of 47

Goal 🎯: Learn about PostgreSQL Views � and how to use them � to improve your data models

7 of 47

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

8 of 47

Case study: A complicated data model� with multiple polymorphic � associations

9 of 47

🦆

🐓

🦆

🦆

🦆

🦆

10 of 47

11 of 47

Polymorphic

association

Polymorphic

association

12 of 47

13 of 47

14 of 47

How would you query for a chicken in a large barn?

Table chicken.id

Table large_barn.id

15 of 47

How would you query for a duck in a small barn?

Table duck.id

Table small_barn.id

16 of 47

Complications?

  • Example of a potentially more complicated data model that could exist in practice�
  • Ways it could be more difficult to work with:
    • Many more animal types: Goat 🐐, Cow 🐄, Horse 🐎, …
    • Many more barn types: Wood 🛖, Extra Large, …
    • Other polymorphic associations in the same table

17 of 47

🐐

🐎

🐄

🦆

🐄

🐓

🦆

🦆

🐎

🐑

🐑

🐑

🐑

🐑

🦆

🦆

18 of 47

What is a PostgreSQL View?

  • A virtual table that doesn’t store data (not physically materialized)
  • A predefined SQL query that acts like a regular table
  • Like a “shortcut”

19 of 47

What is a PostgreSQL View?

20 of 47

Application: Using PostgreSQL Views to� help with our example case

21 of 47

Querying for a chicken in a large barn

22 of 47

Creating a view for chicken_in_large_barn

23 of 47

Querying for chicken in the large barn

Original

Using a View

🆚

24 of 47

Querying for chicken in the large barn

25 of 47

Querying for chicken in the large barn

26 of 47

Outcome: Querying is much simpler now!

27 of 47

But… What about other actions?

insert/ update/ delete

28 of 47

Views are updatable

Automatically updatable, if they satisfy all of these conditions:

  • Has exactly one entry in its FROM list
  • Must not contain WITH, DISTINCT, GROUP BY, HAVING, LIMIT, or OFFSET clauses
  • Must not contain set operations (UNION, INTERSECT, EXCERPT)
  • Select list must not contain any aggregates, window functions or set-returning functions

29 of 47

This is possible…

30 of 47

This is possible…

But animal_type and barn_type will be NULL

31 of 47

We can use a trigger to replace the insert on the view

32 of 47

We can use a trigger to replace the insert on the view

33 of 47

We can use a trigger to replace the insert on the view

34 of 47

We can use a trigger to replace the insert on the view

35 of 47

We can use a trigger to replace the insert on the View

36 of 47

Insight: What is the trigger function doing?

37 of 47

38 of 47

Insert now fills animal_type and barn_type columns

chicken_id

39 of 47

Insert now fills animal_type and barn_type columns

large_barn_id

40 of 47

Insight: The same method will work � for update and delete

41 of 47

Insight: Views can easily improve � a data model without affecting� the underlying tables

42 of 47

Useful View parameters

43 of 47

Useful View parameters

WITH [ CASCADED | LOCAL ] CHECK OPTION

  • Controls behaviour of automatically updatable views
  • If option is specified, INSERT and UPDATE commands on the view�are checked to ensure that new rows are visible through the view
  • Example: Preventing insertion of chicken from Small_Barn into chicken_large_barn view

44 of 47

Useful View parameters

WITH ( view_option_name [= view_option_value] [, ... ] )

  • security_barrier (boolean): allows view to provide row-level security
  • security_invoker (boolean): checks the base relations against the privileges of the user of the view rather than the view owner

45 of 47

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)

46 of 47

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

47 of 47

You’re using a complicated and/or outdated data model.

What do you do now?

  • Refactor the data model and related code
  • Give up and continue using it grudgingly
  • Use PostgreSQL Views and prosper!