Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmNv2-0005U6-G6 for pgsql-general@arkaria.postgresql.org; Mon, 07 Dec 2020 21:19:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmNv1-0005tf-1M for pgsql-general@arkaria.postgresql.org; Mon, 07 Dec 2020 21:19:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmNv0-0005tY-P3 for pgsql-general@lists.postgresql.org; Mon, 07 Dec 2020 21:19:38 +0000 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kmNuy-0001aG-V8 for pgsql-general@lists.postgresql.org; Mon, 07 Dec 2020 21:19:38 +0000 Received: by mail-oi1-x22b.google.com with SMTP id 15so5874132oix.8 for ; Mon, 07 Dec 2020 13:19:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=CDu3Oonc0UQo1CE/bN6kTuQ0ugLLbEm0/eMLbTpHVdQ=; b=F6VxYbIDbOGbl2OPK3xxctbbo5l08AmM3cuc50NsOJKiBwoBxhCp6s0lsldFTooATs vZhhv37613lkUA5HGwAUcHW7ArLFZjGzBLOxvbMeupVfSg+isthRUO+OOYFFxYf7+Zve hzNAhenIfh23W5/Tg09sLNUcD7Wwp/N6RG4PDKD69lrKMUPhVyrM7p8wbrb+8UUMb9+t OZJQYUeH/MAtE5ZUXfv4JiVLFQYrzcqpIVE7BVD4EEswE6lXCNHsWufUK6lRJm5VLacw oxDUy9xMBPH8Oztf2NSXL+mQLp3XHpewb8H19uTIqyykvU4p63R7rQWiary6gcs0CU0c 6H0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=CDu3Oonc0UQo1CE/bN6kTuQ0ugLLbEm0/eMLbTpHVdQ=; b=UWQ2Okbtt9RQBDBTjXjL5b8ihuSmV69QNZw6cfKpv9r+nGiafL7HhX863S8SWWp5+c ORM6CuWCW74IUYHNDUBsUmdwK5RWAhH1kVPoMxCE8TrnpsGXv6UcrfEtLPnen6UgWnZe dzi5vj0FV7CB0Q0m6Mhx7xZXJxSYTt8V6QfGzDXTwdAzz4Pj9oRuoAM4dJIv9Ex0vtx2 X5xAZ8fYbfifKTgm1hd7QsEZoRbQZY0ntlhy9C4jzgXlLFzA2o66wRmqXtUEU7Iph75+ whZSxt5hD3CkSnF0Z10jwgLPQEetM3lNHToljYEFKcRNYTrqzoAFmAIwhOmUkkP7tHKo EKaw== X-Gm-Message-State: AOAM532un+A83y41PdkkY5hl6JCWoweMQSm4OGrAbzIRACsDRUjr7Wjm lI+eCeNWAV0Rfr2vSiOFORwtyEusYfz2kQ== X-Google-Smtp-Source: ABdhPJz2UuGFnbPsqWb5WlQpuEb+/+tgkv/ds7O4tRZl/rX4RjI3mNWw3vjhkaa2lhHxI1cckJuzqA== X-Received: by 2002:a05:6808:a1a:: with SMTP id n26mr570214oij.94.1607375974890; Mon, 07 Dec 2020 13:19:34 -0800 (PST) Received: from [192.168.1.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id w131sm3213817oif.8.2020.12.07.13.19.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Dec 2020 13:19:34 -0800 (PST) Subject: Re: Deferrable FK not behaving as expected. To: pgsql-general@lists.postgresql.org References: <75fe0761-a291-86a9-c8d8-4906da077469@gmail.com> <3c32cc9c-1589-7f22-b9e3-f4bcccbaf87e@gmail.com> <3144564.1607369236@sss.pgh.pa.us> From: Ron Message-ID: <9c844ed1-e9e3-57c9-3f34-3b09479078e6@gmail.com> Date: Mon, 7 Dec 2020 15:19:33 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <3144564.1607369236@sss.pgh.pa.us> Content-Type: multipart/alternative; boundary="------------43182D9DCD50AF955A733D4D" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------43182D9DCD50AF955A733D4D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 12/7/20 1:27 PM, Tom Lane wrote: > Ron writes: >> Neither technique worked. >> test=# ALTER TABLE sales_detail ALTER CONSTRAINT >> fk_sales_detail_sales_header DEFERRABLE INITIALLY DEFERRED; >> ALTER TABLE > Hmm, reproduced here. There seems to be some kind of bug > in ALTER CONSTRAINT --- it looks like it's updated all the > child FKs successfully, but it actually hasn't. If you > drop the FK constraint altogether, and recreate it having > DEFERRABLE INITIALLY DEFERRED from the start, it should > work --- at least it does for me. That works *sometimes*, but not when we *really* want it to work. test=# alter table sales_detail drop CONSTRAINT fk_sales_detail_sales_header; ALTER TABLE test=# ALTER TABLE sales_detail test-#     ADD CONSTRAINT fk_sales_detail_sales_header test-#     FOREIGN KEY (cust_id, order_ts) test-#     REFERENCES sales_header (cust_id, order_ts) DEFERRABLE INITIALLY DEFERRED; ALTER TABLE test=# test=# -- Works test=# begin transaction; BEGIN test=# delete from sales_header where cust_id = 1; DELETE 3 test=# test=# rollback; ROLLBACK -- Does not work test=# begin transaction; BEGIN test=# ALTER TABLE sales_header DETACH PARTITION sales_header_202001; ERROR:  removing partition "sales_header_202001" violates foreign key constraint "sales_detail_cust_id_order_ts_fkey" DETAIL:  Key (cust_id, order_ts)=(1, 2020-01-05 13:05:42.567) is still referenced from table "sales_detail". test=# rollback; ROLLBACK -- Angular momentum makes the world go 'round. --------------43182D9DCD50AF955A733D4D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 12/7/20 1:27 PM, Tom Lane wrote:
Ron <ronljohnsonjr@gmail.com> writes:
Neither technique worked.

      
test=# ALTER TABLE sales_detail ALTER CONSTRAINT 
fk_sales_detail_sales_header DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE
Hmm, reproduced here.  There seems to be some kind of bug
in ALTER CONSTRAINT --- it looks like it's updated all the
child FKs successfully, but it actually hasn't.  If you
drop the FK constraint altogether, and recreate it having
DEFERRABLE INITIALLY DEFERRED from the start, it should
work --- at least it does for me.

That works sometimes, but not when we really want it to work.

test=# alter table sales_detail drop CONSTRAINT fk_sales_detail_sales_header;
ALTER TABLE

test=# ALTER TABLE sales_detail
test-#     ADD CONSTRAINT fk_sales_detail_sales_header
test-#     FOREIGN KEY (cust_id, order_ts)
test-#     REFERENCES sales_header (cust_id, order_ts) DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE
test=#
test=#

-- Works

test=# begin transaction;
BEGIN
test=# delete from sales_header where cust_id = 1;
DELETE 3
test=#
test=# rollback;
ROLLBACK

-- Does not work

test=# begin transaction;
BEGIN
test=# ALTER TABLE sales_header DETACH PARTITION sales_header_202001;
ERROR:  removing partition "sales_header_202001" violates foreign key constraint "sales_detail_cust_id_order_ts_fkey"
DETAIL:  Key (cust_id, order_ts)=(1, 2020-01-05 13:05:42.567) is still referenced from table "sales_detail".
test=# rollback;
ROLLBACK


--
Angular momentum makes the world go 'round.
--------------43182D9DCD50AF955A733D4D--