-
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: prepared statements can fail to be rejected by foreign key checks #68307
Comments
Hello, I am Blathers. I am here to help you get the issue triaged. It looks like you have not filled out the issue in the format of any of our templates. To best assist you, we advise you to use one of these templates. I have CC'd a few people who may be able to assist you:
If we have not gotten back to your issue within a few business days, you can try the following:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan. |
Hey @timchunght -- Can you provide the following:
|
@otan Here is the result of the SHOW CREATE TABLE. I will need some time to extract the code from our repo into a small isolated repo for testing. But essentially we are just using the default Background: SHOW CREATE TABLE transactions;
table_name | create_statement
---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
transactions | CREATE TABLE public.transactions (
| id VARCHAR(36) NOT NULL,
| organization_id VARCHAR(36) NULL,
| external_id VARCHAR(255) NULL,
| payment_order_id VARCHAR(255) NULL,
| parent_account_id VARCHAR(36) NULL,
| payment_channel_id VARCHAR(36) NULL,
| type VARCHAR(255) NULL,
| amount DECIMAL(50,10) NULL,
| currency_code VARCHAR(32) NULL,
| funds_amount DECIMAL(50,10) NULL,
| interest_amount DECIMAL(50,10) NULL,
| fees_amount DECIMAL(50,10) NULL,
| overdraft_amount DECIMAL(50,10) NULL,
| overdraft_fees_amount DECIMAL(50,10) NULL,
| overdraft_interest_amount DECIMAL(50,10) NULL,
| technical_overdraft_amount DECIMAL(50,10) NULL,
| technical_overdraft_interest_amount DECIMAL(50,10) NULL,
| fraction_amount DECIMAL(50,10) NULL,
| taxes JSONB NULL,
| account_balances JSONB NULL,
| user_id VARCHAR(36) NULL,
| terms JSONB NULL,
| transaction_details JSONB NULL,
| transfer_details JSONB NULL,
| linked_transaction_id VARCHAR(36) NULL,
| linked_transaction_type VARCHAR(255) NULL,
| is_originator BOOL NULL DEFAULT false,
| fees JSONB NULL,
| notes STRING NULL,
| custom_data JSONB NULL,
| value_date DATE NULL,
| booking_date DATE NULL,
| created_at INT8 NULL,
| updated_at INT8 NULL,
| card_transaction_id VARCHAR(36) NULL,
| total_balance DECIMAL(50,10) NULL,
| CONSTRAINT "primary" PRIMARY KEY (id ASC),
| CONSTRAINT fk_payment_channel_id_ref_payment_channels FOREIGN KEY (payment_channel_id) REFERENCES public.payment_channels(id),
| CONSTRAINT fk_organization_id_ref_organizations FOREIGN KEY (organization_id) REFERENCES public.organizations(id),
| CONSTRAINT fk_parent_account_id_ref_accounts FOREIGN KEY (parent_account_id) REFERENCES public.accounts(id),
| CONSTRAINT fk_user_id_ref_users FOREIGN KEY (user_id) REFERENCES public.users(id),
| CONSTRAINT fk_card_transaction_id_ref_card_transactions FOREIGN KEY (card_transaction_id) REFERENCES public.card_transactions(id),
| UNIQUE INDEX transactions_organization_id_external_id_key (organization_id ASC, external_id ASC),
| FAMILY "primary" (id, organization_id, external_id, payment_order_id, parent_account_id, payment_channel_id, type, amount, currency_code, funds_amount, interest_amount, fees_amount, overdraft_amount, overdraft_fees_amount, overdraft_interest_amount, technical_overdraft_amount, technical_overdraft_interest_amount, fraction_amount, taxes, account_balances, user_id, terms, transaction_details, transfer_details, linked_transaction_id, linked_transaction_type, is_originator, fees, notes, custom_data, value_date, booking_date, created_at, updated_at, card_transaction_id, total_balance)
| )
(1 row)
Time: 49ms total (execution 49ms / network 0ms) |
@otan Just put together a debug repo that should demonstrates the exact setup here https://github.com/timchunght/gorm-cockroachdb-fk-debug I noticed that with another foreign key explicitly NULL, the invalid foreign key does not return error. However, if the OTHER foreign key is simply missing, the invalid foreign key does return error. This is very strange behaviour and took us a while to simply find. |
@timchunght Thanks for this repro; it is extremely helpful! It looks like there definitely is a bug here. It seems specific to prepared statements. I can reproduce this using the CLI like so:
it seems like might relate to the order that the constraints are checked. |
This bug affects both v20.2 and v21.1 (as well as the current |
This |
So the problem is in the insert fast path - as soon as we find one FK that doesn't need to be checked because of NULL, we don't run any subsequent checks on that node. In this one row case, it only happens with prepare because otherwise we elide that FK check entirely at build time. Here's an example without prepare:
|
68429: [CRDB-8149] Add CES survey link r=Santamaura a=Santamaura The goal of this PR is to create a basic component that can open a link to the CES feedback survey. The current query parameters that are passed through are `clusterId` and `clusterVersion`. If there are any more query parameters that need to be included feel free to comment what they might be. The component will render empty (not able to open the survey) if for some reason we are unable to get the clusterId or clusterVersion values. Below are some screenshots showcasing the feedback survey link and what the url looks like when opened in a local dev environment: ![image](https://user-images.githubusercontent.com/17861665/128223481-2e6b5e6d-77de-4e3e-8a8e-125171f42500.png) ![image](https://user-images.githubusercontent.com/17861665/128223593-c0350d3e-0104-4e1a-ab73-4f4baa939554.png) This PR is in response to [this github issue](#66615) 68486: sql: fix accidental FK check skips r=RaduBerinde a=RaduBerinde This change fixes a bug in the insert fast path where we accidentally skip subsequent FK checks when a FK check can be skipped due to NULL value. Note that this bug does not manifest when optbuilder can determine that the check can be elided entirely; notably, this is always the case for non-prepared statements which insert a single row. Fixes #68307. Release note (bug fix): fixed missing foreign key checks in some cases when there are multiple checks and the inserted data contains a NULL for one of the checks. Co-authored-by: Santamaura <[email protected]> Co-authored-by: Radu Berinde <[email protected]>
This change fixes a bug in the insert fast path where we accidentally skip subsequent FK checks when a FK check can be skipped due to NULL value. Note that this bug does not manifest when optbuilder can determine that the check can be elided entirely; notably, this is always the case for non-prepared statements which insert a single row. Fixes cockroachdb#68307. Release note (bug fix): fixed missing foreign key checks in some cases when there are multiple checks and the inserted data contains a NULL for one of the checks.
This change fixes a bug in the insert fast path where we accidentally skip subsequent FK checks when a FK check can be skipped due to NULL value. Note that this bug does not manifest when optbuilder can determine that the check can be elided entirely; notably, this is always the case for non-prepared statements which insert a single row. Fixes cockroachdb#68307. Release note (bug fix): fixed missing foreign key checks in some cases when there are multiple checks and the inserted data contains a NULL for one of the checks.
This change fixes a bug in the insert fast path where we accidentally skip subsequent FK checks when a FK check can be skipped due to NULL value. Note that this bug does not manifest when optbuilder can determine that the check can be elided entirely; notably, this is always the case for non-prepared statements which insert a single row. Fixes cockroachdb#68307. Release note (bug fix): fixed missing foreign key checks in some cases when there are multiple checks and the inserted data contains a NULL for one of the checks.
A foreign key constraint enforced in Cockroach sql but does NOT get enforced when insert is done via Gorm.
To Reproduce
The following code in Go using Gorm succeeds despite the foreign key is violated within the parent_account_id column of the transactions table (parent_account_id REFERENCES accounts.id)
The above Gorm method results in an INSERT of a row within the
transactions
table despite the FK violation.The gorm code generates the following SQL
This SQL when pasted into cockroach sql results in the expected FK constraint error
Suspicion:
Gorm seems to use the parameters API in the postgres binary protocol that generates the following log when used against postgres that Cockroach might not enforce constraints when used. Please advise.
Expected behavior
A foreign key violation error is expected to happen.
Environment:
cockroach sql
, GormThe text was updated successfully, but these errors were encountered: