I've found a problem with the VALUES-as-RTE approach: regression=# create table src(f1 int, f2 int); CREATE TABLE regression=# create table log(f1 int, f2 int, tag text); CREATE TABLE regression=# insert into src values(1,2); INSERT 0 1 regression=# create rule r2 as on update to src do regression-# insert into log values(old.*, 'old'), (new.*, 'new'); CREATE RULE regression=# update src set f2 = f2 + 1; server closed the connection unexpectedly
The problem with this is that the rewriter is substituting Vars referencing "src" into the values lists of the VALUES RTE, within a query that looks like a Cartesian product of src and *VALUES*: regression=# explain update src set f2 = f2 + 1; QUERY PLAN -------------------------------------------------------------------- Nested Loop (cost=0.00..97.62 rows=3880 width=40) -> Values Scan on "*VALUES*" (cost=0.00..0.02 rows=2 width=40) -> Seq Scan on src (cost=0.00..29.40 rows=1940 width=0) Seq Scan on src (cost=0.00..34.25 rows=1940 width=14) (5 rows) The ValuesScan node doesn't have access to the values of the current row of src ... indeed, the planner doesn't know that it shouldn't put the VALUES on the outside of the join, as it's done here, so there *isn't* a current row of src. AFAICT, the only way to make this work would be to implement SQL99's LATERAL construct (or something pretty close to it --- I'm not entirely sure I understand what LATERAL is supposed to do) so that the rewritten query could be expressed like insert into log select ... from src, LATERAL VALUES(src.f1, ...) That's obviously not something we can get done for 8.2. We could maybe kluge something to work for 8.2 if we were willing to abandon the VALUES-as-RTE approach and go back to the notion of some kind of multiple targetlist in a Query. I'm disinclined to do that though, because as I've been working with your patch I've come to agree that the RTE solution is a pretty clean one. What I'm inclined to do for 8.2 is to disallow OLD/NEW references in multi-element VALUES clauses; the feature is still tremendously useful without that. regards, tom lane ---------------------------(end of broadcast)--------------------------- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq