Sub topic 13
Transition Tables for Set-Based Statement Triggers
Prerequisites and the problem they solve
This chapter assumes that you know basic DELETE and INSERT statements, understand transactions, and can read simple PL/pgSQL. A trigger is database logic that runs automatically when a table event occurs.
A row-level trigger runs once for every affected row. That is useful when each row needs individual validation or modification. A statement-level trigger runs once for the entire SQL statement. A transition table is a temporary, queryable relation containing all rows changed by that statement.
Transition tables let a statement-level trigger process a set of rows with normal SQL instead of calling a function once per row. They are available to AFTER triggers for INSERT, UPDATE, and DELETE.
Defining a transition table
The following example records one summary row for every bulk delete. The trigger function reads deleted_rows, which is the transition table name assigned in CREATE TRIGGER.
CREATE TABLE demo_accounts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL,
balance numeric(12, 2) NOT NULL
);
CREATE TABLE delete_batches (
batch_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
deleted_count bigint NOT NULL,
deleted_total numeric(12, 2) NOT NULL,
recorded_at timestamptz NOT NULL DEFAULT now()
);
CREATE FUNCTION summarize_deleted_accounts() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO delete_batches (deleted_count, deleted_total)
SELECT count(*), coalesce(sum(balance), 0)
FROM deleted_rows;
RETURN NULL;
END;
$$;
CREATE TRIGGER demo_accounts_delete_summary
AFTER DELETE ON demo_accounts
REFERENCING OLD TABLE AS deleted_rows
FOR EACH STATEMENT
EXECUTE FUNCTION summarize_deleted_accounts();
INSERT INTO demo_accounts (email, balance)
VALUES
('[email protected]', 10.00),
('[email protected]', 25.00),
('[email protected]', 40.00);
DELETE FROM demo_accounts
WHERE balance < 40.00;
SELECT deleted_count, deleted_total
FROM delete_batches;The DELETE affects two rows, but summarize_deleted_accounts runs once. Its query sees both rows through deleted_rows, producing one batch summary.
OLD TABLE and NEW TABLE
For a delete, use OLD TABLE. It contains the rows before deletion. For an insert, use NEW TABLE. It contains the inserted rows. An update can use both:
CREATE TRIGGER account_update_batch
AFTER UPDATE ON demo_accounts
REFERENCING OLD TABLE AS old_rows NEW TABLE AS new_rows
FOR EACH STATEMENT
EXECUTE FUNCTION compare_account_batch();The function can join old_rows and new_rows by primary key to calculate set-level changes.
> callout note > Transition tables are only available to suitable AFTER statement triggers. They are not a replacement for row-level NEW and OLD records when each row must be changed or rejected individually.
Choosing this design
Use a transition table when the work is naturally aggregate or batch-oriented, such as one audit summary, one notification record, or one recalculated total per statement. Use a row-level trigger when the function needs to modify or validate each affected row.
Exercise
A statement deletes 500 rows. Which trigger design lets one function call count all 500 rows: a row-level trigger with OLD, or an AFTER statement-level trigger with OLD TABLE?
Show answer
An AFTER statement-level trigger with OLD TABLE. The transition table contains all 500 deleted rows, and the function runs once.