Re: BUG #16661: Changing columns after the rule is created leads to an error when the RETURNING is used

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: drakonard(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #16661: Changing columns after the rule is created leads to an error when the RETURNING is used
Date: 2020-10-08 17:30:10
Message-ID: 2576538.1602178210@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> 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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2020-10-09 00:32:22 Re: BUG #16662: pgbench: error: client 418 script 0 aborted in command 5 query 0: ERROR: invalid page in block 4830
Previous Message PG Bug reporting form 2020-10-08 15:49:58 BUG #16662: pgbench: error: client 418 script 0 aborted in command 5 query 0: ERROR: invalid page in block 4830