pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: targen@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #14124: ON UPDATE CASCADE failure on repeated foreign key column
Date: Mon, 02 May 2016 19:39:52 -0400
Message-ID: <18488.1462232392@sss.pgh.pa.us> (raw)
In-Reply-To: <20160502223720.19110.71285@wrigleys.postgresql.org>
References: <20160502223720.19110.71285@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

targen@gmail.com writes:
> postgres=# create table bar (z int, foreign key (z, z) references foo (x, y)
> on update cascade);

Hmm.  Such a constraint is specifically forbidden by the SQL standard,
see SQL:2011 11.8 <referential constraint definition> syntax rule 8:

	Each referencing column shall identify a column of the referencing
	table, and the same column shall not be identified more than once.

Can you produce a convincing use-case for this structure?

> postgres=# update foo set x = 27, y = 27;
> ERROR:  multiple assignments to same column "z"
> CONTEXT:  SQL statement "UPDATE ONLY "bar" SET "z" = $1, "z" = $2 WHERE $3
> OPERATOR(pg_catalog.=) "z" AND $4 OPERATOR(pg_catalog.=) "z""

> I don't know whether this is expected behavior.  Indeed it would make sense
> for the process to fail if the two referenced columns are set to different
> values, as it would be unclear, in this example, whether z should take its
> value from x or y.  However, the cascading update should work when they are
> indeed equal, and not produce a malformed statement.

It would be difficult to support that, AFAICS, because the FK enforcement
queries do not depend on things like whether the specific values involved
are equal.  Nor would one generally expect that an FK constraint should be
allowed to restrict what can be stored in the referenced table.
Moreover, it's not even real clear to me that there is a well defined
notion of "equal" in such a situation.  If x and y are of different types,
it's possible they would have different equality rules.  (This is somewhat
constrained by the fact that z would have to have equality operators in
common with both; but I do not think that's quite sufficient to remove the
concern.)

It could be argued that we should hew strictly to the letter of the SQL
standard and forbid foreign-key constraints with duplicate referencing
columns.  Or maybe allow it only if the FK action is not one where we'd
run into this problem.  I'm not terribly excited about trying to support
the case, though.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (3+ messages)  latest in thread

Message-ID: <18488.1462232392@sss.pgh.pa.us>
Permalink:  ../18488.1462232392@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/18488.1462232392@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, targen@gmail.com
  Subject: Re: BUG #14124: ON UPDATE CASCADE failure on repeated foreign key column
  In-Reply-To: <18488.1462232392@sss.pgh.pa.us>
  References: <20160502223720.19110.71285@wrigleys.postgresql.org> <18488.1462232392@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox