Skip to content
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/backfill: prevent index backfill from evaluating new volatile key columns #81521

Open
ajwerner opened this issue May 19, 2022 · 4 comments
Open
Assignees
Labels
A-schema-changer-impl Related to the implementation of the new schema changer C-cleanup Tech debt, refactors, loose ends, etc. Solution not expected to significantly change behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@ajwerner
Copy link
Contributor

ajwerner commented May 19, 2022

Is your feature request related to a problem? Please describe.

Follow-up from #81486. The index backfiller can happily evaluate default expressions for new columns which will be part of a new primary index. This is critical to enable #47989. However, if the expressions being evaluated are volatile and are part of the key, then restarts will no longer be idempotent and will cause a corrupt index.

Describe the solution you'd like

We should return an assertion failure error if we ever try to execute such an index backfill. We should know the properties of the expressions at backfill time, so it's not hard to add.

Additional context

Relates both to #81449 and #81448.

When we want to support the following, we'll need to be smarter in how we plan. Namely, we need to first add the column to a primary index but not as a key column, take that all the way to at least validated, then go and build the index with the now materialized k being read from the new source.

CREATE TABLE t (i INT);
ALTER TABLE t ADD COLUMN k UUID DEFAULT (gen_random_uuid()) PRIMARY KEY

Jira issue: CRDB-15105

@ajwerner ajwerner added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label May 19, 2022
@jlinder jlinder added sync-me and removed sync-me labels May 20, 2022
@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label May 25, 2022
@ajwerner ajwerner assigned ajwerner and unassigned ajwerner May 31, 2022
@ajwerner
Copy link
Contributor Author

I will fill in more details on how to solve this in code and then pass it along.

@postamar postamar assigned postamar and unassigned ajwerner Jul 5, 2022
@ajwerner
Copy link
Contributor Author

ajwerner commented Jul 5, 2022

This issue is just about adding a check that we don't perform dangerous volatile expression evaluation in the index backfiller for key columns. Volatile expression evaluation should only be for stored columns. If we need to backfill a volatile new column into an index key, it must first be rendered into a stored column in some index and then that should be the source for the index backfiller.

@fqazi fqazi self-assigned this Oct 6, 2022
@postamar
Copy link
Contributor

The declarative schema changer should reliably evaluate this expression for a stored column so am I correct in understanding that this only concerns the legacy schema changer? In the legacy schema changer ADD COLUMN ... PRIMARY KEY won't work. Is ADD COLUMN ... UNIQUE affected here? I'm trying to think how we're going to test this, or whether to even bother.

Throwing the assertion error seems pretty straightforward, though.

@postamar postamar added C-cleanup Tech debt, refactors, loose ends, etc. Solution not expected to significantly change behavior. A-schema-changer-impl Related to the implementation of the new schema changer and removed C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) labels Nov 10, 2022
@ajwerner
Copy link
Contributor Author

This is about adding a safe-guard. This doesn't currently affect anything because we don't support adding a column. I think a unit test in the backfill logic would be straightforward enough.

@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-sql-schema-deprecated Use T-sql-foundations instead labels May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-schema-changer-impl Related to the implementation of the new schema changer C-cleanup Tech debt, refactors, loose ends, etc. Solution not expected to significantly change behavior. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

No branches or pull requests

4 participants