Re: BUG #1098: Multiple ON INSERT rules not applied properly in the case of INSERT...SELECT

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: "PostgreSQL Bugs List" <pgsql-bugs(at)postgresql(dot)org>, tim(at)queens(dot)unimelb(dot)edu(dot)au
Subject: Re: BUG #1098: Multiple ON INSERT rules not applied properly in the case of INSERT...SELECT
Date: 2004-03-11 15:24:03
Message-ID: 19911.1079018643@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Richard Huxton <dev(at)archonet(dot)com> writes:
> The rule should only be applied once. The rule system basically rewrites the
> insert you supply into two other insert queries (from/to). It will not
> generate one query for each row in quips_transactions (which is what you
> want). You probably need a trigger on the transactions table that issues
> separate queries for each row inserted.

The real issue is that since the multi-row insert command generates
multi-row update commands, user rows that are mentioned multiple times
in the "from" or "to" columns of the insert would have to be updated
multiple times in the same UPDATE command. We do not support that ---
an UPDATE can only update a given row once, because it does not see its
own output row versions as input candidates. This is annoying in this
scenario but it is correct and necessary in most other scenarios.
As an example, without this rule something like "UPDATE foo SET x = x + 1"
would likely go into an infinite loop, repeatedly seeing the new row
version it just created as fodder for another UPDATE cycle.

I concur with the suggestion that triggers would probably be the most
intuitive solution to the problem. I tried to think of a way to make
it work as a rule by aggregating all the updates affecting a single user
row into one row operation. However, since the UPDATE syntax has no
provision for GROUP BY there doesn't seem to be any good way to do that.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2004-03-11 15:57:08 Re: BUG #1099: bad syntax error localisation
Previous Message Peter Eisentraut 2004-03-11 15:01:22 Re: BUG #1099: bad syntax error localisation