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 1padwe-0002pF-3M for pgsql-bugs@arkaria.postgresql.org; Fri, 10 Mar 2023 14:42:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1padwc-0003sl-M8 for pgsql-bugs@arkaria.postgresql.org; Fri, 10 Mar 2023 14:42:06 +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 1padwc-0003sc-E9 for pgsql-bugs@lists.postgresql.org; Fri, 10 Mar 2023 14:42:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1padwZ-0000O5-Uc for pgsql-bugs@lists.postgresql.org; Fri, 10 Mar 2023 14:42:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 32AEg1dU804433; Fri, 10 Mar 2023 09:42:01 -0500 From: Tom Lane To: Hanna Yanchurevich cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17827: Rule on insert into table doesn't work when excepting select from the table itself In-reply-to: References: <17827-1dfd5ad9af280484@postgresql.org> <377748.1678374108@sss.pgh.pa.us> Comments: In-reply-to Hanna Yanchurevich message dated "Fri, 10 Mar 2023 10:18:54 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <804431.1678459321.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 10 Mar 2023 09:42:01 -0500 Message-ID: <804432.1678459321@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hanna Yanchurevich writes: > According to this information I can cause another kind of error: > create table tbl (id serial primary key, msg text); > create table rule_stat (msg text, id int references tbl(id)); > create rule rule_tbl as on insert to tbl do insert into rule_stat values= ('Last > inserted id was ',new.id); > insert into tbl (msg) > select 'I`m an insert'; > SQL Error [23503]: ERROR: insert or update on table "rule_stat" violates > foreign key constraint "rule_stat_id_fkey" > Detail: Key (id)=3D(2) is not present in table "tbl". Yup. It's pretty obvious what's going on if you look at EXPLAIN: explain verbose insert into tbl (msg) select 'I`m an insert'; QUERY PLAN = ------------------------------------------------------------------------ Insert on public.tbl (cost=3D0.00..0.01 rows=3D0 width=3D0) -> Result (cost=3D0.00..0.01 rows=3D1 width=3D36) Output: nextval('tbl_id_seq'::regclass), 'I`m an insert'::text = Insert on public.rule_stat (cost=3D0.00..0.01 rows=3D0 width=3D0) -> Result (cost=3D0.00..0.01 rows=3D1 width=3D36) Output: 'Last inserted id was '::text, nextval('tbl_id_seq'::regclass) (8 rows) > Such behaviour is a bit confusing. Because by using new.* I expect to ge= t a > recently inserted row, but not the result of some query running the seco= nd > time (which causes implicit incrementing of id serial). If that's the mental model you want to work with, use a trigger. When you work with rules, you are working with macros, and they have the same sort of multiple-evaluation hazards as macros in, say, C. regards, tom lane