🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Data Governance & Quality › Lesson 7
TEC-DATA86-07 · Lesson 7 · 12 clock hours · lab

Data Quality Monitoring, Audit Logging, and Compliance Documentation

Objective. Students will implement an ongoing data-quality monitoring process using PostgreSQL triggers and scheduled queries, set up an audit log table, and produce a compliance summary document referencing applicable data-privacy principles.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Why Data Quality Monitoring and Audit Logging Matter

A database that stores accurate, trustworthy data is only as useful as the controls that keep it that way over time. Data quality degrades silently: NULL values accumulate, out-of-range entries slip through application bugs, and unauthorized changes go unnoticed. Two complementary mechanisms address this: audit logging records who changed what and when, while data quality monitoring checks whether the data still meets defined standards. Together they form the foundation of any compliance posture, whether the concern is an internal data governance policy or an external privacy framework.

Audit Logging with PostgreSQL Triggers

A PostgreSQL trigger is a function that the database engine calls automatically when a specified event occurs on a table. For audit purposes you attach an `AFTER` trigger to `INSERT`, `UPDATE`, and `DELETE` operations. Inside the trigger function you capture the operation type, the acting database user (`SESSION_USER` or `CURRENT_USER`), the wall-clock timestamp (`NOW()`), and the old and new row values exposed through the `OLD` and `NEW` pseudo-records.

The audit records go into a dedicated `audit_log` table. That table should be append-only: grant `INSERT` to the trigger-owning role but revoke `UPDATE` and `DELETE` so that no one can tamper with historical records. Storing changed values as `JSONB` columns (`old_data`, `new_data`) keeps the schema generic and allows you to reuse the same trigger function across multiple target tables.

A minimal `audit_log` schema looks like this:

```sql

CREATE TABLE audit_log (

log_id BIGSERIAL PRIMARY KEY,

table_name TEXT NOT NULL,

operation TEXT NOT NULL, -- INSERT, UPDATE, DELETE

db_user TEXT NOT NULL,

changed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

old_data JSONB,

new_data JSONB

);

```

The trigger function returns `NULL` for `AFTER` triggers (the return value is ignored) and uses `TG_TABLE_NAME`, `TG_OP`, `ROW_TO_JSON(OLD)`, and `ROW_TO_JSON(NEW)` to populate each column.

Scheduled Quality Checks with pg_cron

pg_cron is a background worker extension for PostgreSQL that lets you schedule SQL statements using standard cron syntax directly inside the database. Enabling it requires adding `pg_cron` to `shared_preload_libraries` in `postgresql.conf` and restarting the server. Once loaded, the `cron.schedule()` function registers a job.

A daily quality-check script should test at minimum:

Results write to a `data_quality_log` table that includes a `pass_fail` flag (a simple `BOOLEAN` or `TEXT` column), the check name, the table checked, the count of violations found, and the run timestamp. Setting `pass_fail = FALSE` when the violation count exceeds zero gives operators a quick filter.

If the `pg_cron` extension is unavailable in your environment, an OS-level cron job on the Ubuntu VM can call `psql` with a `-f` flag pointing to your SQL script. Both approaches are acceptable; the key is that the job runs unattended on a schedule.

Privacy Principles and Compliance Documentation

Data privacy frameworks worldwide share a set of core principles even when their legal texts differ. For this lesson you document how your technical controls support three of the most widely recognized:

Your one-page compliance summary must explain each principle in plain language, describe the specific technical control in your implementation that supports it, and note any gaps or limitations. This document is not a legal opinion; it is a technical governance artifact.

Safety and Professional Practice

In a database environment, "safety" means protecting data integrity and system availability rather than physical hazards. Apply these professional practices:

Key terms8

Audit logA database table (or external file) that records every data-changing event, including the user, timestamp, operation type, and affected row values, to support accountability and compliance review.
PostgreSQL triggerA database object that automatically executes a specified function before or after an INSERT, UPDATE, or DELETE event on a table.
pg_cronA PostgreSQL extension that runs SQL statements on a schedule defined using standard cron syntax, without requiring an external scheduler.
Data minimizationA data-privacy principle requiring that only the personal data necessary for a specific, stated purpose is collected and retained.
Purpose limitationA data-privacy principle stating that data collected for one purpose must not be used for a different, incompatible purpose without proper authorization.
JSONBA PostgreSQL binary JSON data type that stores semi-structured data efficiently and supports indexing; commonly used in audit log tables to capture row snapshots without a fixed schema.
data_quality_logA table used to store the results of automated data quality checks, including the check name, violation count, run timestamp, and a pass/fail flag.
Access controlA data-privacy and security principle ensuring that only authorized users or processes can read or modify sensitive data, enforced in PostgreSQL through roles, privileges, and row-level security.
In class: Write a PostgreSQL trigger function that inserts a record into an audit_log table on every INSERT, UPDATE, and DELETE on a nominated target table, capturing user, timestamp, operation, and changed values. · Create a scheduled SQL script (using pg_cron or an OS cron job) that runs daily quality checks (NULL counts, range checks) against the target table and writes results to a data_quality_log table with a pass/fail flag. · Draft a one-page compliance summary document explaining how the audit log and quality controls support general data-privacy principles (data minimization, purpose limitation, access control) without referencing specific law numbers, and present findings to the class in a five-minute verbal summary.
Materials: PostgreSQL 15 with pg_cron extension, DBeaver CE, Linux terminal (Ubuntu 22.04 VM), cron scheduling reference, data-privacy principles handout (instructor-prepared), compliance document template
Check: Trigger fires correctly on all three DML operations and audit_log records are accurate; scheduled job runs and populates quality_log; compliance document references at least three privacy principles with correct explanations — all three deliverables must be complete and accurate to pass.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PostgreSQL CE — trigger management and PL/pgSQL
1. Which PostgreSQL trigger timing is most appropriate for writing a record to an audit_log table after a row is deleted from the target table?
PostgreSQL CE — PL/pgSQL and triggers
2. In an audit_log trigger function, which pseudo-record holds the row's values as they existed BEFORE an UPDATE?
DAMA CDMP — data governance and security
3. A DBA wants the audit_log table to be tamper-evident. Which combination of privilege assignments best achieves this?
PostgreSQL CE — database administration and automation
4. Which pg_cron function call correctly schedules a SQL statement to run every day at 2:00 AM?
DAMA CDMP — data quality management
5. A data quality check finds 47 rows where the customer_age column is less than 0. What should the data_quality_log entry record for the pass_fail flag on this check?
DAMA CDMP — data governance and privacy principles
6. Which data-privacy principle is BEST supported by stripping password hash columns from the new_data JSONB field before inserting a record into audit_log?
DAMA CDMP — data governance and privacy principles
7. A compliance summary document states that the audit_log is accessible only to the security team role and is used exclusively for incident investigation and regulatory review. Which privacy principle does this MOST directly support?
PostgreSQL CE — backup, recovery, and database administration
8. Before deploying a new audit trigger to a production PostgreSQL database, which action BEST protects against data loss if the trigger function contains an error?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.