Kaiting Chen <ktche...@gmail.com> writes: > I'd like to propose a change to PostgreSQL to allow the creation of a foreign > key constraint referencing a superset of uniquely constrained columns.
TBH, I think this is a fundamentally bad idea and should be rejected outright. It fuzzes the semantics of the FK relationship, and I'm not convinced that there are legitimate use-cases. Your example schema could easily be dismissed as bad design that should be done some other way. For one example of where the semantics get fuzzy, it's not very clear how the extra-baggage columns ought to participate in CASCADE updates. Currently, if we have CREATE TABLE foo (a integer PRIMARY KEY, b integer); then an update that changes only foo.b doesn't need to update referencing tables, and I think we even have optimizations that assume that if no unique-key columns are touched then RI checks need not be made. But if you did CREATE TABLE bar (x integer, y integer, FOREIGN KEY (x, y) REFERENCES foo(a, b) ON UPDATE CASCADE); then perhaps you expect bar.y to be updated ... or maybe you don't? Another example is that I think the idea is only well-defined when the subset column(s) are a primary key, or at least all marked NOT NULL. Otherwise they're not as unique as you're claiming. But then the FK constraint really has to be dependent on a PK constraint not just an index definition, since indexes in themselves don't enforce not-nullness. That gets back to Peter's complaint that referring to an index isn't good enough. Anyway, seeing that the patch touches neither ri_triggers.c nor any regression tests, I find it hard to believe that such semantic questions have been thought through. It's also unclear to me how this ought to interact with the information_schema views concerning foreign keys. We generally feel that we don't want to present any non-SQL-compatible data in information_schema, for fear that it will confuse applications that expect to see SQL-spec behavior there. So do we leave such FKs out of the views altogether, or show only the columns involving the associated unique constraint? Neither answer seems pleasant. FWIW, the patch is currently failing to apply per the cfbot. I think you don't need to manually update backend/nodes/ anymore, but the gram.y changes look to have been sideswiped by some other recent commit. regards, tom lane