Re: Rules: A Modest Proposal

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>, Greg Sabino Mullane <greg(at)turnstep(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Rules: A Modest Proposal
Date: 2009-10-05 14:51:20
Message-ID: 20091005145120.GD1518@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Oct 05, 2009 at 10:32:53AM -0400, Tom Lane wrote:
> Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> > WITH NEW AS (
> > insert into t values (floor(random()*1000)::integer);
> > RETURNING *
> > )
> > insert into t_log values (NEW.a);
>
> > Would this not have the required semantics?
>
> Interesting idea, but it's not clear how to make it work with multiple
> DO ALSO rules, nor with conditional DO INSTEAD rules.

Well, my (possibly naive) view is:

- Multiple DO ALSO rules seem easy. There is a patch in the works which
makes INSERT/UPDATE/DELETE into proper node types so they can
actually appear in the WITH clause above. With a minor extension you
could create a MultipleStatement node type which merely runs each
substatement, like Append, but for plans.

- Conditional DO INSTEAD rules are brain benders. Logically, I think
they split the plan in two, one with the condition, one with the
negative of the condition. So *maybe* they could also be handled by
such a MultipleStatement node but then...

I get visions of people writing a SELECT rule with a conditional DELETE
statement with RETURNING *. Then, SELECTing the table would return
everything but conditionally DELETE some rows. Something like:

WITH OLD AS (SELECT * FROM foo)
MULTISTATEMENT(
SELECT * FROM OLD WHERE condition;
DELETE FROM OLD WHERE NOT condition RETURNING *;
)

As for actual implementation it seems doable, but I may be being
impossibly naive.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jonah H. Harris 2009-10-05 15:14:38 Re: 8.5 TODO: any info on "Create dump tool for write-ahead logs..." in PITR section (1.4)?
Previous Message Tom Lane 2009-10-05 14:32:53 Re: Rules: A Modest Proposal