Re: altering a column's collation leaves an invalid foreign key

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: altering a column's collation leaves an invalid foreign key
Date: 2024-11-19 16:27:26
Message-ID: ea5b2777-266a-46fa-852f-6fca6ec480ad@eisentraut.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 14.11.24 09:04, Peter Eisentraut wrote:
> You can also reproduce this with things that are not strings with
> collations.  You just need to find a type that has values that are
> "equal" but "distinct", which is not common, but it exists, for example
> 0.0 and -0.0 in floats.  Example:
>
>     create table parent (val float8 primary key);
>     insert into parent values ('0.0');
>
>     create table child (id int, val float8 references parent (val));
>
>     insert into child values (1, '0.0');
>     insert into child values (2, '-0.0');
>
>     update parent set val = '-0.0';  -- ok with NO ACTION
>
> but
>
>     create table child (id int, val float8 references parent (val) on
> update restrict);
>
>     insert into child values (1, '0.0');
>     insert into child values (2, '-0.0');
>
>     update parent set val = '-0.0';  -- error with RESTRICT
>
> So this is a meaningful difference.
>
> There is also a bug here in that the update in the case of NO ACTION
> doesn't actually run, because it thinks the values are the same and the
> update can be skipped.
>
> I think there is room for improvement here, in the documentation, the
> tests, and maybe in the code.  And while these are thematically related
> to this thread, they are actually separate issues.

Back to this. First, there is no bug above. This is all working
correctly, I was just confused.

I made a few patches to clarify this:

1. We were using the wrong error code for RESTRICT. A RESTRICT
violation is not the same as a foreign-key violation. (The foreign key
might in theory still be satisfied, but RESTRICT prevents the action
anyway.) I fixed that.

2. Added some tests to illustrate all of this (similar to above). I
used case-insensitive collations, which I think is easiest to
understand, but there is nothing special about that.

3. Some documentation updates to explain some of the differences between
NO ACTION and RESTRICT better.

Attachment Content-Type Size
0001-Fix-error-code-for-referential-action-RESTRICT.patch text/plain 16.9 KB
0002-Add-tests-for-foreign-keys-with-case-insensitive-col.patch text/plain 5.6 KB
0003-doc-Improve-description-of-referential-actions.patch text/plain 5.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2024-11-19 16:28:55 Re: per backend I/O statistics
Previous Message Tom Lane 2024-11-19 16:21:33 Re: [PATCH] Fixed assertion issues in "pg_get_viewdef"