Full answer
Tables and rows used here:
sql
CREATE TABLE sales (region text, amount int);
CREATE TABLE region_totals (region text PRIMARY KEY, total int NOT NULL);
CREATE FUNCTION add_to_totals() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO region_totals (region, total)
SELECT region, sum(amount) FROM new_rows GROUP BY region
ON CONFLICT (region) DO UPDATE SET total = region_totals.total + EXCLUDED.total;
RAISE NOTICE 'summary updated from % rows', (SELECT count(*) FROM new_rows);
RETURN NULL;
END;
$$;
CREATE TRIGGER sales_totals AFTER INSERT ON sales
REFERENCING NEW TABLE AS new_rows
FOR EACH STATEMENT EXECUTE FUNCTION add_to_totals();sql
INSERT INTO sales SELECT CASE WHEN g % 2 = 0 THEN 'north' ELSE 'south' END, 10 FROM generate_series(1, 1000) g;Result:
text
NOTICE: summary updated from 1000 rows
INSERT 0 1000sql
INSERT INTO sales VALUES ('east', 5), ('north', 7);Output:
text
NOTICE: summary updated from 2 rows
INSERT 0 2sql
SELECT region, total FROM region_totals ORDER BY region;PostgreSQL returns:
text
region | total
--------+-------
east | 5
north | 5007
south | 5000
(3 rows)The 1,000-row insert fires the trigger once, and its notice confirms it saw all 1,000 rows through the transition table; a row-level trigger would have run 1,000 separate upserts. The second insert adds a new region and increases north. The totals then match the base table exactly, and if the insert rolled back, the summary change would roll back with it. Concurrent inserts into the same region serialise on the summary row, so very hot summaries may need batching. UPDATE and DELETE need their own triggers with OLD TABLE as well.
sql
CREATE TABLE sales (region text, amount int);
CREATE TABLE region_totals (region text PRIMARY KEY, total int NOT NULL);
CREATE FUNCTION add_to_totals() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO region_totals (region, total)
SELECT region, sum(amount) FROM new_rows GROUP BY region
ON CONFLICT (region) DO UPDATE SET total = region_totals.total + EXCLUDED.total;
RAISE NOTICE 'summary updated from % rows', (SELECT count(*) FROM new_rows);
RETURN NULL;
END;
$$;
CREATE TRIGGER sales_totals AFTER INSERT ON sales
REFERENCING NEW TABLE AS new_rows
FOR EACH STATEMENT EXECUTE FUNCTION add_to_totals();
INSERT INTO sales SELECT CASE WHEN g % 2 = 0 THEN 'north' ELSE 'south' END, 10 FROM generate_series(1, 1000) g;
INSERT INTO sales VALUES ('east', 5), ('north', 7);
SELECT region, total FROM region_totals ORDER BY region;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- Statement-level triggers fire once per statement
- Transition tables expose affected rows
- Set-based summary updates scale
- Changes stay transactional
- Hot summary rows can become contention points
Common mistakes
- Row-level triggers for bulk-loaded summaries
- Forgetting UPDATE and DELETE paths
Follow-ups
- How would you handle UPDATEs that move rows between regions?
- When is a materialized view better?
Tips
- Show the notice