Skip to content

Insert the data rows of a new table before its triggers and rules, and fire deferred trigger events after rows written into existing tables. - #360

Merged
archlemon merged 2 commits into
mainfrom
data-before-triggers
Nov 21, 2026

Conversation

@archlemon

Copy link
Copy Markdown
Member

Fixes #343

New tables. A table the plan creates got its triggers and rules (in the deferred trigger stream of the creates) before its data: rows, which run with the alters. So in a database rebuilt from pull --data the rows fired an audit trigger, filling a table that was not pulled, and a deferrable initially deferred constraint trigger left events pending that made the alter table ... enable row level security after them fail with cannot ALTER TABLE "account" because it has pending trigger events (SQLSTATE 55006). The triggers, trigger enable statements and rules of such a table, and of the partitions the plan creates under it, now go right after the data statements, as pg_dump restores a table: the order is table, primary key/constraints/indexes, rows, triggers and rules, new foreign keys, row level security. Parents' rows still come first (dataTableOrder), and foreign keys were already added after the rows. A new table without rows keeps its triggers where they were.

Existing tables. Rows written into a table that exists still fire its triggers and rules, the same as any other write. When the plan writes rows into an existing table and some table has an initially deferred constraint trigger or constraint (from the live definitions, or an initially deferred trigger declared on an existing table, which the plan creates before the rows), set constraints all immediate; follows the data writes, and likewise the data deletes under --unsafe --prune. That runs the deferred checks at that point, so later ALTER TABLE statements in the same migration work. The deferred constraints of the rest of that migration's transaction are then checked immediately too. This is documented.

Tests:

  • internal/diff/data_trigger_test.go: rows before triggers, rules, trigger enable and RLS for a new table and before a new partition's trigger; triggers stay with the creates when a table has no rows; SET CONSTRAINTS ALL IMMEDIATE after inserts and after deletes for an existing table with a deferred trigger; left out when nothing is deferred; added for an initially deferred foreign key on another table.
  • internal/integration/data_triggers_test.go with testdata/data/triggers.sql: pulls tables with an audit trigger, a rule, a deferred constraint trigger, a deferred foreign key and forced RLS with their rows, rebuilds them (no audit rows, diff clean, pg_dump identical), then writes the rows into the existing tables of a third database (triggers fire, migration succeeds). The pull fixtures in testdata/pull have no way to pass --data, so this is a separate test, modelled on TestIntegrationDataRoundTrip.

Docs: the Reference Data section of docs/schema.md and docs/feature_support.md.

…d fire deferred trigger events after rows written into existing tables.

A table the plan creates got its triggers and rules before its rows, so
the rows fired an audit trigger in the rebuilt database, and a deferred
constraint trigger left events pending that made the ALTER TABLE ... ENABLE
ROW LEVEL SECURITY after them fail (SQLSTATE 55006). Its triggers and rules,
and those of the partitions the plan creates under it, now come right after
the rows, as pg_dump restores them. Rows written into existing tables still
fire their triggers; when a table has an initially deferred constraint
trigger or constraint, SET CONSTRAINTS ALL IMMEDIATE follows the rows and
the deletes, so later ALTER TABLE statements in the migration succeed.

Fixes #343.
@archlemon
archlemon merged commit bb257e8 into main Nov 21, 2026
8 checks passed
@archlemon
archlemon deleted the data-before-triggers branch November 21, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reference data rows are inserted after triggers: deferred constraint triggers break the migration, user triggers fire

1 participant