Re: Values list-of-targetlists patch for comments (was Re: [PATCHES] 8.2 features?)

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Values list-of-targetlists patch for comments (was Re: [PATCHES] 8.2 features?)
Дата
Msg-id 16429.1154463351@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: Values list-of-targetlists patch for comments (was Re: [PATCHES]  (Joe Conway <mail@joeconway.com>)
Ответы Re: Values list-of-targetlists patch for comments (was Re: [PATCHES] 8.2 features?)
Re: Values list-of-targetlists patch for comments (was Re: [PATCHES]
Список pgsql-hackers
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


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Bruce Momjian
Дата:
Сообщение: Re: pg_dump: add option to ignore TABLE DATA for failed
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: Values list-of-targetlists patch for comments (was Re: [PATCHES] 8.2 features?)