-
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
CockroachDB automatically creates duplicates of indices created within a transaction #46127
Comments
Nice find! Thank you for filing! |
As of now, postgres does not need an index at the source of a foreign key constraint, while cockroachDB does. I believe we are working on removing this constraint, but I don't know if there are trackers for it (cc @awoods187, @RaduBerinde). Because we require an index at the source of the foreign key relation, we must create it while executing the In 20.1 you'll be able to drop the auto generated index as long as there is another suitable index on the table for the foreign key constraint, if that helps at all. |
Right, but if the index is created before data is inserted that should obviate the need to do so. Eg I don’t know if cockroachdb supports deferred constraints but you could possibly treat all FK as deferred for the duration of the transaction they were created in until either an FK index is added, an attempt to insert data within that transaction is detected, or the transaction is committed? If this is strictly a code flow that is triggered by the creation of a table within a transaction, it should have no impact on performance in future transactions. |
We don't support deferrable constraints right now. Thanks for opening the issue! We'll see where it fits in on the roadmap. |
Excellent, thank you! |
No i think this is still a problem -- here the index is created after we add the foreign key. |
Sorry, you're right. Well, the real solution here is to stop auto-creating indexes and just let FK checks be planned with the index that was created later in the transaction, so this will be fixed once #46674 is done (and we make the necessary changes on the schema change side). |
Okay, this really is fixed now. |
CockroachDB always creates an auto-index on foreign keys if an index is not created at the time the table is created. Very helpful.
However, it does not recognize that a
CREATE TABLE
followed by a separateCREATE INDEX
within a single transaction are equivalent to aCREATE TABLE
with an inline definition of an index. As a result, duplicate indices are created (the user-created index plus the auto-created one).Illustrated:
Followed up with
SHOW INDEX FROM bar
:The discrepancy is clearer when looking at the output of
SHOW CREATE
because it makes immediately obvious that it's a duplicate and that creating the index separately is no different than creating it inline:Environment:
cockroach sql
Additional context
This behavior differs from that of PostgreSQL and has a negative impact on performance and size. I believe the auto index should be created only when the creation is committed and not before then.
ORM drivers for PostgreSQL exist that (for the obvious simplicity reasons) prefer to map logical tables to SQL via separate creation of table and indices (example: npgsql). Removing the auto-created index by hand is of course an option, but it is tedious and unnecessary as it should not be too difficult to delay the creation of the index until the the entirety of the transaction has been committed.
Jira issue: CRDB-5104
The text was updated successfully, but these errors were encountered: