Re: ERROR: cache lookup failed for collation 0 on DELETE query after upgrading from 9.X to 12.3

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: cen <cen(dot)is(dot)imba(at)gmail(dot)com>
Cc: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for collation 0 on DELETE query after upgrading from 9.X to 12.3
Date: 2020-07-14 16:29:03
Message-ID: 2874034.1594744143@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

cen <cen(dot)is(dot)imba(at)gmail(dot)com> writes:
> The bug appears when you specify a foreign key of type varchar to point
> to primary key of type uuid.

Ah-hah. For the record, attached is an actually-self-contained test
case.

The problem comes from this bit in RI_FKey_cascade_del, which evidently
was added as part of the nondeterministic-collations patch (5e1963fb7):

if (pk_coll != fk_coll && !get_collation_isdeterministic(pk_coll))
ri_GenerateQualCollation(&querybuf, pk_coll);

which is failing to guard against the possibility that pk_coll is
InvalidOid, i.e. the referenced column is non-collatable.
ri_GenerateQualCollation would correctly do nothing in that case,
but we don't get there because get_collation_isdeterministic has
already fallen over.

While the fix seems relatively straightforward --- probably we just
need to add an OidIsValid(pk_coll) clause here and in the similar tests
elsewhere in ri_triggers.c --- I'm still going to hold Peter's feet
to the fire about this code, because I consider the state of its
documentation to be absolutely unforgivable. There is NO comment
explaining why it'd be appropriate to do this for a nondeterministic
PK collation (and not otherwise). Nor has anything been done to fix the
multiple ways in which this addition falsified ri_GenerateQualCollation's
header comment.

regards, tom lane

Attachment Content-Type Size
ri-collation-bug-example.sql text/plain 1.6 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2020-07-14 17:17:57 BUG #16541: Timestamp allowing greater than max documented value?
Previous Message Tom Lane 2020-07-14 15:48:08 Re: BUG #16536: Segfault with partition-wise joins