Constraint Trigger Defers Validation To Commit

Enforce a whole-set rule like two-leads-per-team with a DEFERRABLE INITIALLY DEFERRED constraint trigger, validated at COMMIT instead of per row.

SqlData Integrity

Code

snippet
-- some rules only hold for the whole set: promote a member first, remove a
-- lead afterwards -- a per-row CHECK cannot express that. A constraint
-- trigger defers validation to COMMIT and lets the transaction sit
-- temporarily over cap in between.
CREATE TEMP TABLE team_members (
    emp_id  integer PRIMARY KEY,
    team    text    NOT NULL,
    is_lead boolean NOT NULL DEFAULT false
);

-- seed rows: team core already has two leads (emp 7 and emp 12); emp 15 is a regular member
INSERT INTO team_members VALUES
    (7,  'core', true),
    (12, 'core', true),
    (15, 'core', false);

CREATE OR REPLACE FUNCTION enforce_lead_cap() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
    -- at commit time scan the team's current state, not just the row that fired
    IF (SELECT count(*) FROM team_members WHERE team = NEW.team AND is_lead) > 2 THEN
        RAISE EXCEPTION 'team % already has two leads', NEW.team;
    END IF;
    RETURN NULL;
END;
$$;

-- a constraint trigger must be an AFTER row-level trigger; DEFERRABLE INITIALLY
-- DEFERRED moves the check to commit time
CREATE CONSTRAINT TRIGGER lead_cap_on_promote
    AFTER INSERT OR UPDATE OF is_lead ON team_members
    DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE FUNCTION enforce_lead_cap();

BEGIN;
-- three temporary leads in between (7, 12, 15) raise nothing: validation happens at COMMIT
UPDATE team_members SET is_lead = true  WHERE emp_id = 15;
UPDATE team_members SET is_lead = false WHERE emp_id = 7;
COMMIT; -- passes: exactly two leads (12 and 15) remain at commit time

-- a transaction that still exceeds the cap at commit is rejected and rolled
-- back as a whole: no intermediate state ever leaks
BEGIN;
UPDATE team_members SET is_lead = true WHERE emp_id = 7;
COMMIT; -- ERROR: team core already has two leads

How it works

What runs here is INSERT / UPDATE / CREATE — team_members and of are the objects involved.

team_members gets built in this file itself as a session-local temp table — nothing has to be prepared ahead of it, and the session finishing removes it again.

Breaking the statements into their working parts: a WHERE clause constraining which rows the statement operates on; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; DEFAULT values filling omitted columns.

Applying it

It carries its own setup: the session-local tables in play (team_members) come from the file’s statements — run the entire file against a disposable database.

Statements that matter

CREATE CONSTRAINT TRIGGER lead_cap_on_promote
    AFTER INSERT OR UPDATE OF is_lead ON team_members
    DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE FUNCTION enforce_lead_cap();
UPDATE team_members SET is_lead = true  WHERE emp_id = 15;
UPDATE team_members SET is_lead = false WHERE emp_id = 7;

Interactive Tools for This Topic

FAQ

What are the relations in play here?
`team_members`, `of` — referenced directly, no join needed.