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 1oavzm-0004FR-U2 for pgsql-general@arkaria.postgresql.org; Wed, 21 Sep 2022 09:26:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oavzl-0000vy-Ry for pgsql-general@arkaria.postgresql.org; Wed, 21 Sep 2022 09:26:17 +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 1oavzl-0000vn-EK for pgsql-general@lists.postgresql.org; Wed, 21 Sep 2022 09:26:17 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oavzj-0002o5-AZ for pgsql-general@lists.postgresql.org; Wed, 21 Sep 2022 09:26:17 +0000 Received: by mail-ed1-x533.google.com with SMTP id z2so7759223edi.1 for ; Wed, 21 Sep 2022 02:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date; bh=TvoQP0a9Mmy2wwrpjShlJDDZxDD7oEyUFsT7nDpw5Lo=; b=xN08oJJF4u+tP5I9nw6Xv7Ok/uN56QPoBEW4SM4Or3eZun+rkVGwyK+GYmx/0wHvNA se7Z1BflaSQD1Ilhaad388pd2Q/HYmrLff0fQ/zPgOS/v2tLHsbj8zaieu++OETZlB3g kVsbYCdnV9FZ/OHhOXZz38W+IMxXQrNG/I08y/8neL9IDW3VIAOakNwsPaUq5PVDpW3g 5Qixatt9XvcXkI0QdeR1RPDaBYmilIg7jIarklRK0cq/uAO5rIwfUgVcSLN7dv+gQCED RjwjmYHUfuNgSxrkkuf5/90YY4h7eOgjPZz86CQKpkQX4buOvD5+jO5dnG/cfR3+IzE4 Hz1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date; bh=TvoQP0a9Mmy2wwrpjShlJDDZxDD7oEyUFsT7nDpw5Lo=; b=YyM/c3BOVuNdR4mNI1bdffTog6PPgOcHa6gL2CKD1WSmutemEr/GO4GgekGOE6Jj/c wwVvwaYTKbe1Y+PGy86+/tVUvmsch3OiUbXWIB+2FtQ2K7x0vVFIcT/K2bi3KpyxdsnL UnJdc3r7fDhgtZCI9cf0s83v+iTBj9Q0A1faP+CJhCJzsCNTjVfY3V+MR2/2py8bH9Zn yv1QqBOUR7aCpQKsWtUXxTJ0BVRVyyCNw4dYfnhJuYZiMytO83m1m5UsbFfYeBfj6mkE QvSdfFYZ29lGfD3AqLuGlFClWSoBbcyI05ksUK0LBJO71ElBDbKqAltsFUOY86OAHB0d dKgA== X-Gm-Message-State: ACrzQf35BlybcCbucMQyNJ9YWnMdyGVJz/4zNfkwQaJ+Re7Pqj4gDRsc XXzzq21lOw3wyZSiZ+utXXP1dgoYz3098Q== X-Google-Smtp-Source: AMsMyM5NpkTVGEWFzP7jvv+c1k9I+uW1hcTz3lRyqXu5ANFxXJafzVmsjXzAWIswmbkcydgmRUjZlw== X-Received: by 2002:a05:6402:42c7:b0:451:e6a6:48b5 with SMTP id i7-20020a05640242c700b00451e6a648b5mr24346995edc.113.1663752374200; Wed, 21 Sep 2022 02:26:14 -0700 (PDT) Received: from localhost.localdomain ([88.116.133.170]) by smtp.gmail.com with ESMTPSA id d11-20020a170906544b00b0078009d3e0b8sm1071506ejp.52.2022.09.21.02.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 02:26:13 -0700 (PDT) Message-ID: <0d52121503f0babc9ce3eeb0753067edb56d6e8e.camel@cybertec.at> Subject: Re: PostgreSQL Rule does not work with deferred constraint. From: Laurenz Albe To: Louis Tian , "pgsql-general@lists.postgresql.org" Date: Wed, 21 Sep 2022 11:26:12 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2022-09-21 at 01:19 +0000, Louis Tian wrote: > Encountered a possible bug today. In short, it appears that the rule system is not deferring the constraint checking as specified. Here is an example. > > create table "parent" ( > >   "id" uuid primary key > > ); > > > > create table "children" ( > >   "id" uuid primary key > >       default gen_random_uuid() > >        references parent("id") > >             deferrable > >             initially deferred > > ); > > > > -- this works as expected. > > begin; > >       insert into "children" values ('74c247be-2fc3-43e5-94f8-d5e1af147a9b'); > >       insert into "parent" values ('74c247be-2fc3-43e5-94f8-d5e1af147a9b'); > > commit; > > > > -- doing the same with a rule > > create rule on_insert as on insert to "children" do also ( > >       insert into "parent" (id) values (new."id"); > > ); > > > > -- this fails with: > > --    ERROR:  insert or update on table "children" violates foreign key constraint "children_id_fkey" > > --    DETAIL:  Key (id)=(40513132-f9fa-4e5a-aa46-20c8185b3e58) is not present in table "parent". > > --    SQL state: 23503 > > insert into "children" values (default); > The rules system supposedly transforms the insert statement and executed the rule action in the same transaction. So I think it should work.  > But I got the same error on both pg13 and pg14. > > Is there something I missed here? or is my understanding of the rule system just simply wrong?  I just tried your commands, and it works as you expect on my PostgreSQL v15 database. Yours, Laurenz Albe