Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

A post-mortem found that a hidden trigger written ten years ago silently changed prices during a data migration, costing the company money. How would you prevent hidden database logic from causing such failures across the company?

Make database logic impossible to hide: automatically inventory all triggers, functions, procedures and rules from the database catalogs and put them under version control, give every object an owner and documentation, and include database code in code review and tests. Make migrations and bulk operations check for active triggers and decide explicitly whether they should fire, set a policy that business behaviour lives in explicit application logic or CDC while triggers are limited to technical uses, and spread the lesson with a blameless write-up and new checks in the migration runbook.

Why this question is asked

An incident like this is rarely about one bad trigger; it is about an organisation that lost track of where its business logic lives. The trigger was probably reasonable when it was written, maybe applying a regional price rule, and everyone who knew about it has since moved on. Interviewers ask this leadership question to see whether we fix the class of problem rather than the instance, and how we bring many teams along without a heavy process. In this answer, we will walk through why hidden logic is dangerous, then the inventory, ownership and review, migration safeguards, the policy on where business logic belongs, and how to share the lesson.

1Why Hidden Database Logic Fails This Way

A trigger runs automatically whenever its table changes, so it acts on every write path: the application, a script, a migration and a manual fix. That is what made it attractive ten years ago, and it is what made the migration dangerous. The engineers writing the data migration read the application code, tested their transformation, and saw correct prices in their test copy, which perhaps had no triggers or an older schema. In production, every row they wrote passed through a function they had never seen, which recalculated the price.

Three conditions lined up. The logic was invisible from the application code. Nobody owned it, so nobody reviewed its effect on the migration. And the migration process had no step that asked what else fires when these rows change. Each of these is something we can change across the company.

2Inventory Everything From the Catalogs

The first step is to know what exists. We do not ask teams to list their triggers; we query the databases, because the catalogs are the truth. In PostgreSQL, pg_trigger lists triggers, pg_proc lists functions and procedures, and pg_rewrite lists rules; in MySQL, information_schema.TRIGGERS and information_schema.ROUTINES do the same. A scheduled job extracts the definitions from every database and commits them to a repository, so every object is visible, searchable and diffable.

SQL
-- PostgreSQL: every user-defined trigger with its table and function
SELECT c.relname AS table_name, t.tgname AS trigger_name,
p.proname AS function_name, t.tgenabled
FROM pg_trigger t
JOIN pg_class c ON c.oid = t.tgrelid
JOIN pg_proc p ON p.oid = t.tgfoid
WHERE NOT t.tgisinternal;
-- table_name | trigger_name | function_name | tgenabled
-- product | trg_price_adjust | fn_price_adjust | O
-- orders | trg_set_updated | set_updated_at | O
SELECT pg_get_functiondef('fn_price_adjust'::regproc); -- full source, committed to the repository
Step throughMaking database code visible
  1. Extract from catalogs nightlypg_trigger, pg_proc, pg_rewrite, MySQL info schema
  2. Commit to a repositorydiffable, searchable
  3. Matches the migration history?
    YesNo actionNoDrift ticket to the owner
  4. Has an owner and a description?
    YesIncluded in reviews and testsNoAssign owner or retire

4 steps in order. Play them, or step through them one at a time.

Nothing in the database is unknown, unowned or changed outside version control.

The job also flags drift: if the definition in a database differs from the one in version control, someone changed it by hand, and that becomes a ticket. Over time, the goal is that database code only changes through migrations in version control, so the extracted copy and the migration history agree.

3Owners, Documentation, Review and Tests

Every object in the inventory gets an owning team and a short description of what it does and why. Objects whose owner cannot be found are reviewed: some are obsolete and can be removed after checking that nothing depends on them, others are adopted by the team that owns the table. Ownership is what turns an inventory into something that is maintained.

Database code then follows the same rules as application code. Changes go through migrations and code review, ideally reviewed by someone who knows both the table and the business rule. And database code has tests: with Testcontainers, a test can start a real PostgreSQL, apply the migrations, update a product row and assert the price that comes out. If such a test had existed for the price trigger, its behaviour would have been documented in a way nobody could miss.

4Migrations Must Decide Explicitly Whether Triggers Fire

For data migrations and bulk operations, we add a mandatory step: list the triggers on every table the operation writes, and decide for each one whether it should fire. Sometimes it should, for example an updated_at trigger. Sometimes it must not, for example a trigger that recalculates prices or sends notifications, in which case the migration disables it for its window, or uses a method that skips it, and the decision is written in the migration's description. A check in the migration tooling can enforce this by refusing to run a data migration on a table with active triggers unless the migration declares which triggers it expects.

The migration is also rehearsed on a recent production-like copy, including its triggers, with before and after checks on key business values such as prices, totals and counts. Comparing a sample of prices before and after the rehearsal would have caught this incident before it reached production.

5A Policy on Where Business Logic Lives

The deeper fix is a policy. Business behaviour, such as pricing, discounts or eligibility, belongs in explicit application logic, where it is visible, tested and owned, or in consumers of change events through CDC when it must react to every change. Triggers are restricted to technical uses that are easy to understand and safe for every write path: maintaining updated_at, enforcing an invariant the database cannot express as a constraint, or writing a generic audit row.

We do not rip out every existing trigger at once, which would be its own risky migration. Existing business triggers are listed, prioritised by risk, and moved into application code or CDC consumers over time, each with tests proving the behaviour is unchanged. New business triggers need an exception approved by architecture review.

6Sharing the Lesson

Finally, we make sure the whole company learns from it, not just the team that ran the migration. A blameless post-mortem explains what happened, why it was hard to see, and what is changing, without pointing at the engineers involved, because the system made the mistake easy. It is presented at the engineering forum, and its outcome is concrete: the migration runbook gains the trigger check and the rehearsal with value comparisons, the inventory dashboard is published, and the policy is linked from the database guidelines. Six months later we report progress: how many objects have owners, how many business triggers remain, and whether any drift was found.

7Follow-up questions

Should we ban triggers entirely?

No. Some uses are valuable precisely because they apply to every write path, such as updated_at, generic audit rows or invariants that constraints cannot express. The problem is business logic hidden in triggers, so the policy restricts triggers to documented technical uses with owners and tests.

How do you handle triggers in databases owned by vendors or other companies?

Inventory them all the same, mark them as vendor-owned, and treat them as part of the vendor's interface: data migrations on those tables must be agreed with the vendor or done through their supported tools, and their behaviour is captured in rehearsal tests.

How do you know the inventory stays complete?

It is generated from the catalogs on a schedule, not maintained by hand, so new objects appear automatically. Drift detection shows objects changed outside migrations, and an object without an owner after a set period is escalated to the team that owns the table.

8Conclusion

In this answer, we responded to a hidden trigger that changed prices during a migration by fixing the conditions that made it possible. We inventoried all database code automatically from the catalogs into version control with drift detection, gave every object an owner, documentation, review and tests, and required migrations and bulk operations to list the triggers they touch and decide explicitly whether each fires, with rehearsal and value checks. We set a policy that business behaviour lives in application logic or CDC while triggers are kept for technical uses, and shared the lesson through a blameless post-mortem and a stronger migration runbook.

What to say in the interviewAbout 90 seconds aloud

Open with

I would treat this as a visibility and ownership problem rather than one bad trigger. First, inventory: a scheduled job extracts every trigger, function, procedure and rule from the catalogs, pg_trigger, pg_proc and pg_rewrite in PostgreSQL, information_schema in MySQL, from every database, and commits the definitions to a repository, with drift detection when something was changed by hand.

Then, if they want more

Second, ownership: every object gets an owning team and a description; unowned ones are either adopted or retired, and database code goes through code review and gets tests, for example with Testcontainers applying migrations and asserting what a trigger does.

Third, migration safety: every data migration or bulk operation must list the triggers on the tables it writes and decide explicitly whether each should fire, disabling the ones that should not for its window, and it is rehearsed on a production-like copy with before and after checks on business values like prices. That check alone would have caught this incident.

Fourth, a policy: business behaviour lives in explicit application code or CDC consumers, and triggers are limited to technical uses such as updated_at or generic audit, with existing business triggers moved out over time by risk.

Finally, I would share it through a blameless post-mortem, add the checks to the migration runbook, and report progress on owners and remaining business triggers after six months.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A post-mortem found that a hidden trigger written ten years ago silently changed prices...": https://scrivio-interview-library.netlify.app/databases/stored-code/9ec81be10d/