Re: Proposal / proof of concept: Triggers on VIEWs

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Proposal / proof of concept: Triggers on VIEWs
Date: 2010-08-04 13:31:40
Message-ID: AANLkTinuP9Q2d=H+hRnXWc1e70Jjy7VL1x+2X7p+C6oO@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 4 August 2010 13:22, Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi> wrote:
> On 8/4/10 2:39 PM +0300, Dean Rasheed wrote:
>>
>> Does this sound like a useful feature? Is this a sane approach to
>> implementing it? If not, has anyone else given any thought as to how
>> it might be implemented?
>
> I didn't look at the patch, but so far, I've identified three problems with
> the existing view system:
>
>    1) You can't re-evaluate the UPDATE expression like an UPDATE on a
>       table does.  Consider for example  UPDATE foo SET a=a+1;  If the
>       tuples change before we get to them, we lose data because we
>       simply can't re-evaluate "a+1" in the trigger.
>

Is this the same problem the writeable CTE patch ran into?
The way I've done this, the OLD values passed to the trigger all come
from a snapshot established at the start of the query, so you're
right, the trigger won't see values changed after the query started,
unless it re-queries for them. I don't see an easy way round that.

>    2) You can't set the number of affected rows.
>

Yeah, the assumption is that the number of affected rows is the number
of rows in the view that matched the user's WHERE clause. You could
return fewer affected rows by having the trigger return NULL for some
of them, but you can't say that you've affected more than that. So
even if the trigger updates 10 rows in the base tables for a given row
in the view, that still only counts as 1 row affected in the view by
the original query.

>    3) You can't set the RETURNING results.  You suggested that
>       RETURNING for DELETE would return the OLD value, but that seems
>       broken because that's not necessarily what was deleted.

Well that's what happens for a table. Alternatively the trigger could
modify OLD, and then have RETURNING return that, but that's not what
happens in a BEFORE DELETE trigger on a table.

>  I didn't
>       understand what you suggestion for UPDATE was; how does PG know
>       that if the view doesn't have a primary key?

For INSERT and UPDATE the trigger would compute and make the necessary
changes to the base tables, and then return the new contents of the
view's row in a modified copy of NEW, if necessary for RETURNING. This
might include re-computed derived values for example.

If the view doesn't have a PK, or any other way of uniquely
identifying rows then its probably hopeless. That's not a case that
this patch is targeted for.

Regards,
Dean

>
> I think these are the main three problems that prevent people from actually
> using views, and I think these should be focused on when adding triggers on
> VIEWS.  I would love to see the feature though.
>
> Any thoughts?
>
>
> Regards,
> Marko Tiikkaja
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2010-08-04 13:34:01 Re: documentation for committing with git
Previous Message Heikki Linnakangas 2010-08-04 13:29:19 Re: documentation for committing with git