Обсуждение: question on UPDATE rules

Поиск
Список
Период
Сортировка

question on UPDATE rules

От
Matthew Denny
Дата:
I have a rule defined as the following:

CREATE RULE fooBar AS ON
UPDATE TO VectorMessageTable WHERE new.acks = (SELECT
numReplicas FROM VectorTable vt WHERE vt.ID =
new.ID) DO (DELETE FROM VectorMessageTable WHERE ID = new.ID AND
versionNum = new.versionNum);

if I call: 

UPDATE VectorMessageTable SET acks =
<corresponding-numReplicas-from-VectorTable>

Then I the update goes through, but none of the records are
deleted. However, if I run any other subsequent update on these tuples in
VectorMessageTable (even ones that change the acks value) then the
rule fires and the tuples are deleted.  The crux of the problem seems
to be that the "new" pseudo-relation seems to be returning old values, 
as this is the only way I can explain this behavior.  Has anyone else
experienced this problem?  If so, a workaround would be greatly
appreciated.

thanks,
Matt Denny



Re: question on UPDATE rules

От
wieck@debis.com (Jan Wieck)
Дата:
>
> I have a rule defined as the following:
>
> CREATE RULE fooBar AS ON
> UPDATE TO VectorMessageTable WHERE new.acks = (SELECT
> numReplicas FROM VectorTable vt WHERE vt.ID =
> new.ID) DO (DELETE FROM VectorMessageTable WHERE ID = new.ID AND
> versionNum = new.versionNum);
>
> if I call:
>
> UPDATE VectorMessageTable SET acks =
> <corresponding-numReplicas-from-VectorTable>
>
> Then I the update goes through, but none of the records are
> deleted. However, if I run any other subsequent update on these tuples in
> VectorMessageTable (even ones that change the acks value) then the
> rule fires and the tuples are deleted.  The crux of the problem seems
> to be that the "new" pseudo-relation seems to be returning old values,
> as this is the only way I can explain this behavior.  Has anyone else
> experienced this problem?  If so, a workaround would be greatly
> appreciated.
   Sorry, but I haven't understood what you really want the rule   to do.  Maybe you can give me enought  of  your
schema plus   some sample queries with comments what you want the rule then   to do.
 


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#========================================= wieck@debis.com (Jan Wieck) #




Re: question on UPDATE rules

От
Tom Lane
Дата:
Matthew Denny <mdenny@CS.Berkeley.EDU> writes:
> I have a rule defined as the following:

> CREATE RULE fooBar AS ON
> UPDATE TO VectorMessageTable WHERE new.acks = (SELECT
> numReplicas FROM VectorTable vt WHERE vt.ID =
> new.ID) DO (DELETE FROM VectorMessageTable WHERE ID = new.ID AND
> versionNum = new.versionNum);

> if I call: 

> UPDATE VectorMessageTable SET acks =
> <corresponding-numReplicas-from-VectorTable>

> Then I the update goes through, but none of the records are
> deleted. However, if I run any other subsequent update on these tuples in
> VectorMessageTable (even ones that change the acks value) then the
> rule fires and the tuples are deleted.

Not sure, but maybe you want DO INSTEAD DELETE ... rather than just
DO DELETE ... ?  As it stands, you're commanding the machine to both
delete and update the tuples for which the rule fires.  I think that
will result in the old tuple being marked deleted (twice!), but the
update will still produce a new tuple that's not marked deleted.
That seems to match your symptoms...
        regards, tom lane