Repository navigation
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
Conversation
…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.
This was referenced Nov 21, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 frompull --datathe rows fired an audit trigger, filling a table that was not pulled, and adeferrable initially deferredconstraint trigger left events pending that made thealter table ... enable row level securityafter them fail withcannot 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 laterALTER TABLEstatements 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 IMMEDIATEafter 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.gowithtestdata/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 intestdata/pullhave no way to pass--data, so this is a separate test, modelled onTestIntegrationDataRoundTrip.Docs: the Reference Data section of
docs/schema.mdanddocs/feature_support.md.