ALTER CONSTRAINT on a partitioned FK isn't working

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: pgsql-bugs@lists.postgresql.org
Date: 2020-12-07T19:33:53Z
Lists: pgsql-bugs
I reproduced the problem shown in [1]:

--- snip ---
drop table if exists pt, ref;

create table pt(f1 int, f2 int, f3 int, primary key(f1,f2))
  partition by list(f1);
create table pt1 partition of pt for values in (1);
create table pt2 partition of pt for values in (2);

create table ref(f1 int, f2 int, f3 int)
  partition by list(f1);
create table ref1 partition of ref for values in (1);
create table ref2 partition of ref for values in (2);

alter table ref add foreign key(f1,f2) references pt;

alter table ref alter constraint ref_f1_f2_fkey
  deferrable initially deferred;

insert into pt values(1,2,3);
insert into ref values(1,2,3);

delete from pt;  -- expected to fail

begin;
delete from pt;  -- this should work, but does not
delete from ref;
abort;
--- snip ---

But if you create the FK in one step with

alter table ref add foreign key(f1,f2) references pt
  deferrable initially deferred;

then everything behaves as expected.  So something is broken
about propagating deferred-ness to partition triggers in an
ALTER CONSTRAINT.  Oddly, it *looks* like it worked if you
examine the child tables with "\d+".  I surmise that ALTER CONSTRAINT
fixes whatever catalog fields psql looks at, but there's some other
fields that also need to be updated and aren't being.

			regards, tom lane

[1] https://www.postgresql.org/message-id/flat/75fe0761-a291-86a9-c8d8-4906da077469%40gmail.com



Commits

  1. Have ALTER CONSTRAINT recurse on partitioned tables

  2. Fix OID passed to object-alter hook during ALTER CONSTRAINT