-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql: BEFORE triggers can cause spurious constraint violation errors #133784
Comments
Hi @DrewKimball, please add branch-* labels to identify which branch(es) this C-bug affects. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
This commit fixes a bug that could cause spurious constraint violation errors when a BEFORE trigger was planned on a cascaded mutation. Since we don't allow triggers to modify the rows of a cascaded mutation, it is safe to avoid the extra check entirely. This commit also adds a diamond-pattern cascade test, with triggers on each table. Fixes cockroachdb#133784 Release note: None
This commit fixes a bug that could cause spurious constraint violation errors when a BEFORE trigger was planned on a cascaded mutation. Since we don't allow triggers to modify the rows of a cascaded mutation, it is safe to avoid the extra check entirely. This commit also adds a diamond-pattern cascade test, with triggers on each table. Fixes cockroachdb#133784 Release note: None
133220: opt: plan row-level BEFORE triggers for cascading mutations r=DrewKimball a=DrewKimball #### opt: mechanical changes for BEFORE triggers on cascades This commit consists of the following changes to prepare for adding support for row-level BEFORE triggers to cascading mutations: * Added `unsafe_allow_triggers_modifying_cascades` which will determine whether a trigger can modify the rows mutated by a cascade, default off. * Added a "cascade" argument to `buildRowLevelBeforeTriggers` to provide the context when building a row-level BEFORE trigger. * Refactoring the logic for invoking `crdb_internal.plpgsql_raise` to be re-used for triggers. Informs #132971 Release note: None #### opt: plan row-level BEFORE triggers for cascading mutations This commit adds logic to invoke the functions for row-level BEFORE triggers that are fired by a cascade's mutation. This is mostly straightforward, except for the fact that a row-level BEFORE trigger can actually modify or filter rows-to-be-mutated. In Postgres, it's possible to cause constraint violations via this behavior. This commit adds a runtime check to ensure that trigger functions do not modify cascading updates/deletes. The check is gated behind `unsafe_allow_triggers_modifying_cascades`, default off. Fixes #132971 Release note (sql change): Cascades can now fire row-level BEFORE triggers. By default, attempting to modify or eliminate the cascading update/delete will result in a `Triggered Data Change Violation` error. Users that wish to do this anyway can set `unsafe_allow_triggers_modifying_cascades`. Note that doing so could result in constraint violations, similar to Postgres. #### sql: fix queuing behavior for checks and triggers This commit fixes a bug in `PlanAndRunPostQueries` that could cause check queries to be run more than once. This would cause problems with unclosed resources after query completion. This commit also fixes a minor bug that could cause triggers queued by triggers to be run before newly-queued checks and cascades, instead of after. The following commit includes a regression test. Fixes #133792 Release note: None #### opt: don't add spurious check queries after BEFORE trigger This commit fixes a bug that could cause spurious constraint violation errors when a BEFORE trigger was planned on a cascaded mutation. Since we don't allow triggers to modify the rows of a cascaded mutation, it is safe to avoid the extra check entirely. This commit also adds a diamond-pattern cascade test, with triggers on each table. Fixes #133784 Release note: None 134005: ui: delete old db pages r=xinhaoz a=xinhaoz Delete legacy db pages and related functionality. These pages are now replaced by the v2 db pages. Deleted - Database overview page and related apis - Database details page and related apis - Database table page and related apis Epic: none Release note (ui change): As of 25.1 the legacy db page which was previously available via Advanced Debug is no longer available. 134313: opt: make `max-stack` opt tester option more reliable r=mgartner a=mgartner The `max-stack` opt tester option now runs the test command in a separate goroutine. A fresh stack makes tests using this setting more reliable. It also decreases the `max-stack` of the original test that motivated the `max-stack` option (see #132701) to 100KB, between 65KB in which the test fails after the fix in #132701 and 135KB in which the test fails before the fix. Finally, the test has been disabled under `race` builds which increase the size of stack frames and would cause this test to fail. Epic: None Release note: None 134353: mixedversion: update skip upgrades logic r=RaduBerinde a=RaduBerinde This commit updates the logic that determines which versions support skipping the previous major release during upgrade. Epic: REL-1292 Release note: None Co-authored-by: Drew Kimball <drewk@cockroachlabs.com> Co-authored-by: Xin Hao Zhang <xzhang@cockroachlabs.com> Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com> Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
Based on the specified backports for linked PR #133220, I applied the following new label(s) to this issue: branch-release-24.3. Please adjust the labels as needed to match the branches actually affected by this issue, including adding any known older branches. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
This commit fixes a bug that could cause spurious constraint violation errors when a BEFORE trigger was planned on a cascaded mutation. Since we don't allow triggers to modify the rows of a cascaded mutation, it is safe to avoid the extra check entirely. This commit also adds a diamond-pattern cascade test, with triggers on each table. Fixes cockroachdb#133784 Release note: None
Consider a diamond cascade pattern, where an update to a parent table cascades to two child tables, which in turn cascade to the same grandchild table. In the time between when the first child cascades to the grandchild, and the second child does the same, attempting to check the FK relation from the grandchild to the second child would find a constraint violation. Here's an example logic test:
The problem is that the trigger could in principle change how the rows are modified during the cascade to the grandchild. This results in a check being queued for both child tables after the cascade for the first one completes. This results in a FK violation error, when simply waiting for the second cascade to complete would avoid the error.
We actually disallow BEFORE triggers that modify rows in cascades by default (determined by
unsafe_allow_triggers_modifying_cascades
). As long as this is the case, we could avoid building the extra checks, since we verify at runtime that the cascading mutation isn't modified.Jira issue: CRDB-43749
The text was updated successfully, but these errors were encountered: