PostgreSQL Tutorial 120 lessons · 1:49:08+ of video
PostgreSQL Home What Is PostgreSQL
What PostgreSQL Is Running Postgres in Docker psql: Your First Query Databases, Tables and psql
Databases, Schemas and Tables Meet Riverside Books psql Meta-commands Querying Rows
SELECT and WHERE ORDER BY and LIMIT NULL and COALESCE Numbers, Text and Booleans
Numbers: integer, numeric, double Text Types Booleans and Casting Dates and Times
Dates and Times timestamptz and Time Zones Intervals and date_trunc Creating Tables
CREATE TABLE Identity Columns Constraints Foreign Keys and RETURNING
Foreign Keys INSERT ... RETURNING UPDATE and DELETE ... RETURNING Upsert, Generated Columns, ALTER
Upsert with ON CONFLICT Generated Columns ALTER TABLE Joins, LATERAL and FILTER
Joins Refresher LATERAL Joins Aggregates and FILTER ROLLUP and CTEs
GROUPING SETS and ROLLUP CTEs Recursive CTEs Window Functions
Window Functions Top N per Group Running Totals and LAG DISTINCT ON, generate_series, Report
DISTINCT ON generate_series Practice: The Monthly Report Arrays
Arrays Array Operators unnest and array_agg JSONB
JSONB Basics Querying JSONB JSONB Containment Updating and Building JSON
Updating JSONB Building JSON JSONB or Columns? Enums, UUIDs and Ranges
Enums and Domains UUIDs Range Types Indexes and EXPLAIN
How Indexes Work B-tree Indexes EXPLAIN EXPLAIN ANALYZE and Smarter Indexes
EXPLAIN ANALYZE Multicolumn Indexes Partial and Expression Indexes GIN, BRIN and Index Mistakes
GIN Indexes BRIN Indexes Index Mistakes Full-Text Search
Full-Text Search tsvector and tsquery Ranking and Headlines Fast Search, Fuzzy Search, Practice
A Search Column and Index Fuzzy Search with pg_trgm Practice: Book Search Transactions and MVCC
Transactions Savepoints MVCC Isolation, Locks and Job Queues
Isolation Levels Locks and Deadlocks FOR UPDATE SKIP LOCKED Views and Schemas
Views Materialized Views Schemas and search_path Functions and Procedures
SQL Functions PL/pgSQL Procedures Triggers
Triggers An Audit Log Trigger Practice: Stock Trigger Roles, Privileges and Row-Level Security
Roles and Privileges Row-Level Security Practice: Shop Staff Access Postgres from Python
psycopg Basics Parameters and SQL Injection Transactions in Python COPY, Pools and a Python Report
COPY and Bulk Loading Connection Pools Practice: A Python Report Partitioning, VACUUM and Statistics
Partitioning VACUUM and Autovacuum Statistics and ANALYZE Finding and Fixing Slow Queries
pg_stat_statements A Slow Query Workflow Configuration Basics Backups and Replication
Backups with pg_dump Restore and Point-in-Time Recovery Replication NOTIFY, Extensions and pgvector
LISTEN and NOTIFY Extensions pgvector Vector Search in Postgres
Vector Search Vector Indexes Hybrid Search Job Queues, Mistakes and Alternatives
Postgres as a Job Queue Common Mistakes Postgres vs Other Databases Project: Harbor Events, Part 1
Project: Harbor Events, the Schema Project: Load and Check Project: Core Queries Project: Harbor Events, Part 2
Project: Reports Project: Search Project: Speed It Up Project: Harbor Events, Part 3
Project: Safe Bookings Project: Access Rules Project: Python Service Interview Questions and Quiz
Interview Questions, Part 1 Interview Questions, Part 2 Quick Quiz Cheat Sheet, Roadmap and Wrap-up
Cheat Sheet Your Roadmap Wrap-up PostgreSQL · Lesson 77 of 120
An Audit Log Trigger Who changed what.
⏱ 2 min read ▶ Video at 1:09:14 🎓 Free
▶ starts at 1:09:14 Price changes need an audit trail. Who changed it, when, and from what to what. An after update trigger can write every change into a log table.
The log table stores the book, old price, new price, user and time. The trigger function inserts a row using old and new. When restricts it to real price changes. Run it. Two updates, two log rows.
query.sql sql Copy
CREATE TABLE price_log (book_id int , old numeric , new numeric ,
changed_by text DEFAULT current_user, changed_at timestamptz DEFAULT now ());
CREATE FUNCTION log_price () RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO price_log (book_id, old, new) VALUES ( OLD . book_id , OLD . price , NEW . price );
RETURN NULL ;
END $$;
CREATE TRIGGER books_price_log AFTER UPDATE OF price ON books
FOR EACH ROW WHEN ( OLD . price IS DISTINCT FROM NEW . price ) EXECUTE FUNCTION log_price();
UPDATE books SET price = price + 1 WHERE book_id IN ( 1 , 2 );
SELECT book_id, old, new, changed_by FROM price_log ORDER BY book_id; Output
CREATE TABLE
CREATE FUNCTION
CREATE TRIGGER
UPDATE 2
book_id | old | new | changed_by
---------+-------+-------+------------
1 | 14.99 | 15.99 | postgres
2 | 12.99 | 13.99 | postgres
(2 rows) After triggers see the final values. Store the user and time. Keep the log table append only. And for full history of every column, a generic trigger can store old and new as JSONB.
key value AFTER final values current_user who Append only never edit logs to_jsonb(OLD) whole rows
Prefer to watch? The full PostgreSQL course is free All lessons in one video with animations, real code and chapters.
Watch free An after update trigger can record every change into an audit table. Next, practise with a stock trigger.
Key takeaways AFTER UPDATE OLD / NEW Audit log