Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YopLm-0006cZ-JN for pgsql-bugs@arkaria.postgresql.org; Sun, 03 May 2015 08:34:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YopLl-0002Od-F2 for pgsql-bugs@arkaria.postgresql.org; Sun, 03 May 2015 08:34:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YopLj-0002OU-Ie for pgsql-bugs@postgresql.org; Sun, 03 May 2015 08:34:07 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1YopLg-0007eM-9h for pgsql-bugs@postgresql.org; Sun, 03 May 2015 08:34:06 +0000 MIME-Version: 1.0 Subject: BUG #13224: Foreign key constraints cannot be changed to deferrable To: pgsql-bugs@postgresql.org From: chris@chrullrich.net Date: Sun, 03 May 2015 08:33:43 +0000 Message-ID: <20150503083343.2662.37021@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 13224 Logged by: Christian Ullrich Email address: chris@chrullrich.net PostgreSQL version: 9.4.1 Operating system: Windows Description: According to the manual, ALTER TABLE ... ALTER CONSTRAINT ... DEFERRABLE should work. It does not, according to the script below. -- Demonstrates bug related to FK constraints ALTERed to DEFERRABLE. CREATE TABLE master ( id INTEGER PRIMARY KEY ); CREATE TABLE detail ( id_master INTEGER REFERENCES master (id), val TEXT, PRIMARY KEY (id_master) ); -- Show the constraint. SELECT * FROM pg_constraint WHERE conname = 'detail_id_master_fkey'; INSERT INTO master VALUES (1); INSERT INTO detail VALUES (1, 'one'); -- Fail to change the primary key on the master table (expected). BEGIN; UPDATE master SET id = 2 WHERE id = 1; ROLLBACK; -- Make the constraint deferrable. ALTER TABLE detail ALTER CONSTRAINT detail_id_master_fkey DEFERRABLE INITIALLY IMMEDIATE; -- Show the constraint. Note condeferrable = true. SELECT * FROM pg_constraint WHERE conname = 'detail_id_master_fkey'; -- Fail again (unexpected). BEGIN; SET CONSTRAINTS ALL DEFERRED; UPDATE master SET id = 2 WHERE id = 1; ROLLBACK; -- Recreate the constraint as deferrable. ALTER TABLE detail DROP CONSTRAINT detail_id_master_fkey; ALTER TABLE detail ADD CONSTRAINT detail_id_master_fkey FOREIGN KEY (id_master) REFERENCES master (id) DEFERRABLE INITIALLY IMMEDIATE; -- Show the constraint. Note the record is identical to the one from -- the previous query. SELECT * FROM pg_constraint WHERE conname = 'detail_id_master_fkey'; -- Succeed this time (expected). BEGIN; SET CONSTRAINTS ALL DEFERRED; UPDATE master SET id = 2 WHERE id = 1; UPDATE detail SET id_master = 2 WHERE id_master = 1; COMMIT; -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs