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 1kQZk7-0002Dm-KW for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Oct 2020 17:30:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kQZk6-00019E-HZ for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Oct 2020 17:30:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQZk6-000197-C3 for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2020 17:30:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQZk4-0006vh-CK for pgsql-bugs@lists.postgresql.org; Thu, 08 Oct 2020 17:30:13 +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 098HUAoK2576539; Thu, 8 Oct 2020 13:30:10 -0400 From: Tom Lane To: drakonard@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16661: Changing columns after the rule is created leads to an error when the RETURNING is used In-reply-to: <16661-01a98a6c58c8054a@postgresql.org> References: <16661-01a98a6c58c8054a@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 08 Oct 2020 12:26:59 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2576537.1602178210.1@sss.pgh.pa.us> Date: Thu, 08 Oct 2020 13:30:10 -0400 Message-ID: <2576538.1602178210@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > The SQL: > CREATE TABLE rule_bug_table ( > id serial NOT NULL PRIMARY KEY, > archived boolean NOT NULL > ); > CREATE OR REPLACE RULE rule_bug_table__archive_instead_of_delete__rule > AS ON DELETE TO rule_bug_table > DO INSTEAD > UPDATE rule_bug_table > SET archived = true > WHERE rule_bug_table."id" = OLD."id" > RETURNING OLD.*; > ALTER TABLE rule_bug_table ADD COLUMN select_allowed boolean NOT NULL > DEFAULT (false); > DELETE FROM rule_bug_table WHERE id = 1 RETURNING *; > gives an error "could not find replacement targetlist entry for attno 3". Hmm. The error message definitely needs to be more user-friendly, but I don't think this is actually a bug. The rule's RETURNING clause expands to this during parsing: RETURNING old.id, old.archived as you can see with "\d+ rule_bug_table". Then when you try to do "RETURNING *" in the DELETE, that's asking for a column not available from the rewritten query. So you *should* get an error, just not one that looks like it's an internal failure. The root issue here is that "*" is expanded at parse time for stored rules and views. Unfortunately, that behavior is specifically demanded by the SQL spec (at least for the view case), so I doubt we can change it. regards, tom lane