Re: PostgreSQL: Question about rules - Mailing list pgsql-general

From Jeff Davis
Subject Re: PostgreSQL: Question about rules
Date
Msg-id 1163808334.4514.39.camel@dogma.v10.wvs
Whole thread Raw
In response to Re: PostgreSQL: Question about rules  ("Jeremy Smith" <postgres@duckwizard.com>)
Responses Re: PostgreSQL: Question about rules  ("Jeremy Smith" <postgres@duckwizard.com>)
List pgsql-general
On Fri, 2006-11-17 at 11:49 -0800, Jeremy Smith wrote:

> Now, the default value for new.id gets evaluated *each time* I
> reference new.id - meaning the rule's first insert sees N for new.id
> while the rule's second insert sees N+1.  That is kind of odd - I
> would think that the default value would get evaluated and then
> assigned to new.id (since it is a concrete row), but that appears not
> to be the case.
>
> My stopgap solution to this is to call a PSQL/PL function for the body
> of the rule, which can store new.id in a variable so it is evaluated
> only once.  If there is a way to do this inside the rule without
> resorting to creating a view, 3 rules and 3 functions for every child
> table, I would love to hear about it!
>

I think you're operating on the edge of what rules are supposed to do. A
rule won't copy values or create variables, which is why you needed
COALESCE.

Why do you need so many functions though? Won't one SQL function do the
trick?

Regards,
    Jeff Davis


pgsql-general by date:

Previous
From: Ron Johnson
Date:
Subject: Re: Allowing SYSDATE to Work
Next
From: Adrian Klaver
Date:
Subject: Re: After Update Triggers