cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: allow a FK relationship to work even without index on the referencing column

Open knz opened this issue 8 years ago • 2 comments

See the discussion here: https://forum.cockroachlabs.com/t/v2-0-alpha-20180129-foreign-key-column-requires-index/1326

Currently a FK relationship requires a unique index on the referenced column (common) and an index (potentially non-unique) on the referencing column (not common: oracle and pg do not have this requirement).

The explanation: we need this to accelerate lookups on the referencing table during updates or deletes on the referenced table.

However as the user points out:

  1. it's not directly compatible with pg
  2. in the case the referenced table is infrequently modified (or not at all), this is pure overhead
  3. it removes control from the hands of the programmer about the performance of their application

So instead we could make the requirement optional.

cc @awoods187 for tracking and prioritization

knz avatar Jan 31 '18 16:01 knz

Ran into this pretty quickly when making a multi-table geo-partitioning dataset that uses composite FKs cc @awoods187

nstewart avatar Jun 15 '18 11:06 nstewart

A lot of the ongoing work to enable this is on the optimizer side (to support cascades). I'm going to use this issue to track what needs to be done on the schema change side of things, which is just

  1. remove the restriction when creating FKs, and
  2. stop auto-creating indexes on new tables and empty tables with FKs.

thoszhang avatar Apr 28 '20 14:04 thoszhang