Allow specifying column list for foreign key ON DELETE SET actions
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 8 Dec 2021 10:09:44 +0000 (11:09 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 8 Dec 2021 10:13:57 +0000 (11:13 +0100)
commitd6f96ed94e73052f99a2e545ed17a8b2fdc1fb8a
tree621d033b72ab7da8a21acb729b41c015b6322747
parente464cb7af317e216fef9bfe19a7c4df542817012
Allow specifying column list for foreign key ON DELETE SET actions

Extend the foreign key ON DELETE actions SET NULL and SET DEFAULT by
allowing the specification of a column list, like

    CREATE TABLE posts (
        ...
        FOREIGN KEY (tenant_id, author_id) REFERENCES users ON DELETE SET NULL (author_id)
    );

If a column list is specified, only those columns are set to
null/default, instead of all the columns in the foreign-key
constraint.

This is useful for multitenant or sharded schemas, where the tenant or
shard ID is included in the primary key of all tables but shouldn't be
set to null.

Author: Paul Martinez <paulmtz@google.com>
Discussion: https://www.postgresql.org/message-id/flat/CACqFVBZQyMYJV=njbSMxf+rbDHpx=W=B7AEaMKn8dWn9OZJY7w@mail.gmail.com
22 files changed:
doc/src/sgml/catalogs.sgml
doc/src/sgml/ddl.sgml
doc/src/sgml/ref/alter_table.sgml
doc/src/sgml/ref/create_table.sgml
src/backend/catalog/heap.c
src/backend/catalog/index.c
src/backend/catalog/pg_constraint.c
src/backend/commands/tablecmds.c
src/backend/commands/trigger.c
src/backend/commands/typecmds.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/outfuncs.c
src/backend/parser/gram.y
src/backend/utils/adt/ri_triggers.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/relcache.c
src/include/catalog/catversion.h
src/include/catalog/pg_constraint.h
src/include/nodes/parsenodes.h
src/test/regress/expected/foreign_key.out
src/test/regress/sql/foreign_key.sql