What if you could update a complex view just like a regular table without breaking anything?
Why INSTEAD OF trigger for views in PostgreSQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a view that combines data from multiple tables to show a summary report. You want to let users update this view directly, but since views don't store data themselves, you have to manually update each underlying table every time someone changes the view.
Manually updating all related tables is slow and complicated. It's easy to forget a table or make mistakes, causing inconsistent data. This manual approach is frustrating and error-prone, especially as the database grows.
INSTEAD OF triggers let you write special code that runs when someone tries to insert, update, or delete data on a view. This code can update the right underlying tables automatically, making the view behave like a real table for changes.
UPDATE table1 SET col = val WHERE id = x; UPDATE table2 SET col = val WHERE id = x;
CREATE TRIGGER trg_instead_of INSTEAD OF UPDATE ON view_name FOR EACH ROW EXECUTE FUNCTION update_underlying_tables();
This lets users interact with complex views as if they were simple tables, making data updates seamless and reliable.
A sales dashboard view combines customer info and orders. With INSTEAD OF triggers, sales reps can update customer details or order status directly on the dashboard view, and the database updates all related tables correctly behind the scenes.
Manual updates on views require complex, error-prone steps.
INSTEAD OF triggers automate updates on views by handling underlying tables.
This makes views updatable and easier to work with for users.
Practice
INSTEAD OF trigger on a view in PostgreSQL?Solution
Step 1: Understand what views normally do
Views in PostgreSQL are virtual tables that do not store data themselves and usually cannot be directly updated.Step 2: Role of INSTEAD OF triggers
INSTEAD OF triggers let you define custom actions that run instead of the usual insert, update, or delete on the view, making it updatable.Final Answer:
To run custom code instead of the default insert, update, or delete on the view -> Option BQuick Check:
INSTEAD OF triggers = custom code on views [OK]
- Thinking INSTEAD OF triggers speed up queries
- Confusing INSTEAD OF triggers with indexes
- Believing INSTEAD OF triggers block all changes
my_view for INSERT operations?Solution
Step 1: Recall correct trigger syntax in PostgreSQL
The syntax requires specifying the trigger name, the event timing (INSTEAD OF), the event type (INSERT), the target (ON my_view), and the function to execute.Step 2: Identify the full correct syntax
CREATE TRIGGER trg_instead_of_insert ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func(); correctly includes "ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func();" which is the proper syntax for INSTEAD OF triggers on views.Final Answer:
CREATE TRIGGER trg_instead_of_insert ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func(); -> Option AQuick Check:
INSTEAD OF triggers need FOR EACH ROW and correct order [OK]
- Placing INSTEAD OF before ON
- Omitting FOR EACH ROW
- Using BEFORE instead of INSTEAD OF
CREATE VIEW emp_view AS SELECT id, name FROM employees; CREATE FUNCTION emp_view_insert() RETURNS trigger AS $$ BEGIN INSERT INTO employees(id, name) VALUES (NEW.id, NEW.name); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER trg_emp_insert INSTEAD OF INSERT ON emp_view FOR EACH ROW EXECUTE FUNCTION emp_view_insert(); INSERT INTO emp_view (id, name) VALUES (1, 'Alice'); SELECT * FROM employees WHERE id = 1;
What will the SELECT query return?
Solution
Step 1: Understand the INSTEAD OF trigger effect
The trigger function inserts the new row into the base table employees when an insert is done on the view emp_view.Step 2: Trace the INSERT and SELECT
The INSERT INTO emp_view triggers emp_view_insert(), which inserts (1, 'Alice') into employees. The SELECT then finds this row.Final Answer:
(1, 'Alice') -> Option DQuick Check:
INSTEAD OF trigger inserts row into employees [OK]
- Assuming views cannot be inserted into
- Expecting syntax error on insert
- Forgetting trigger function inserts data
CREATE FUNCTION trg_update() RETURNS trigger AS $$ BEGIN UPDATE employees SET name = NEW.name WHERE id = NEW.id; END; $$ LANGUAGE plpgsql;
When you try to update the view, you get an error. What is the problem?
Solution
Step 1: Check trigger function requirements
Trigger functions must return a value, usually NEW or OLD, depending on the trigger type.Step 2: Identify missing RETURN statement
The function lacks a RETURN statement, causing an error when the trigger tries to get a result.Final Answer:
The function does not return a value -> Option AQuick Check:
Trigger functions must return NEW or OLD [OK]
- Omitting RETURN in trigger functions
- Confusing INSTEAD OF with BEFORE triggers
- Assuming plpgsql is not allowed
products(id, name, price) that only allows updating the price through the view. Which approach using an INSTEAD OF trigger is correct?Solution
Step 1: Understand the goal
The view should allow updating only the price column, so the trigger must update only that column in the base table.Step 2: Choose the correct trigger approach
INSTEAD OF UPDATE triggers on views let you control exactly what updates happen. Updating only price and returning NEW is correct.Final Answer:
Create an INSTEAD OF UPDATE trigger that updates only the price column in products and returns NEW -> Option CQuick Check:
INSTEAD OF triggers control updates precisely [OK]
- Updating all columns ignoring restrictions
- Using BEFORE triggers on views (not supported)
- Using rules instead of triggers for complex logic
