Allow specifying column list for foreign key ON DELETE SET actions

Enterprise / PostgreSQL - Peter Eisentraut [eisentraut.org] - 8 December 2021 10:13 UTC

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 Discussion: https://www.postgresql.org/message-id/flat/CACqFVBZQyMYJV=njbSMxf+rbDHpx=W=B7AEaMKn8dWn9OZJY7w@mail.gmail.com

d6f96ed94e Allow specifying column list for foreign key ON DELETE SET actions
doc/src/sgml/catalogs.sgml | 12 +++
doc/src/sgml/ddl.sgml | 33 +++++++-
doc/src/sgml/ref/alter_table.sgml | 2 +-
doc/src/sgml/ref/create_table.sgml | 24 ++++--
src/backend/catalog/heap.c | 2 +
src/backend/catalog/index.c | 2 +
src/backend/catalog/pg_constraint.c | 53 +++++++++++-
src/backend/commands/tablecmds.c | 97 +++++++++++++++++++--
src/backend/commands/trigger.c | 2 +
src/backend/commands/typecmds.c | 2 +
src/backend/nodes/copyfuncs.c | 1 +
src/backend/nodes/equalfuncs.c | 1 +
src/backend/nodes/outfuncs.c | 1 +
src/backend/parser/gram.y | 134 +++++++++++++++++++++++++-----
src/backend/utils/adt/ri_triggers.c | 115 ++++++++++++++++++-------
src/backend/utils/adt/ruleutils.c | 10 +++
src/backend/utils/cache/relcache.c | 2 +-
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_constraint.h | 11 ++-
src/include/nodes/parsenodes.h | 1 +
src/test/regress/expected/foreign_key.out | 71 ++++++++++++++++
src/test/regress/sql/foreign_key.sql | 51 ++++++++++++
22 files changed, 557 insertions(+), 72 deletions(-)

Upstream: git.postgresql.org


  • Share