Skip site navigation (1) Skip section navigation (2)

Re: Rule won't let me NOTIFY, no matter how hard I try

From: "Luis C(dot) Ferreira" <lcf(at)tuxedo(dot)com(dot)ar>
To: Jeff Boes <jboes(at)nexcerpt(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Rule won't let me NOTIFY, no matter how hard I try
Date: 2004-01-15 18:34:39
Message-ID: 200401151534.39772@tu.numero.de.suerte.17 (view raw or flat)
Thread:
Lists: pgsql-sql
El Mar 13 Ene 2004 18:07, Jeff Boes escribi├│:
>JB: Here's the setup: I wanted to write a rule that would fire on an update
>JB: to one table, and do an update to another table, followed by a notify.
>JB: My first attempt wasn't acceptable to PG (7.3.4):
>JB:
>JB: create rule "my_rule" as
>JB: on update to table_A
>JB: where new.col_A != old.col_A
>JB: do
>JB:    (update table_B ...;
>JB:     notify "my_signal";
>JB:    );
>JB:
>JB: ... because you can't have a "notify" statement in a rule that fires on
>JB: update (only select, update, and delete, I guess).
>JB:
hi,

you have to write to rules

first one (update):

create rule "my_rule" as
on update to table_A
 where new.col_A != old.col_A
 do
    update table_B ...;

second one (notify):

create rule "my_rule2" as
on update to table_A
notify my_rule;

	

>JB: Second attempt was to "hide" the notify in a function:
>JB:
>JB: create function fn_notify(TEXT) returns VOID as '
>JB:   execute ''notify " || $1 || "'';
>JB: ' language 'plpgsql';

The correct syntax...

create function fn_notify ( text ) RETURNS void as '
declare
        v_signal alias for $1;
begin
    execute '' notify "'' || v_signal || ''"'';
    return;
end;
  ' language 'plpgsql';

...and re-write the first rule

create rule "my_rule" as
on update to table_A
 where new.col_A != old.col_A
 do
    ( update table_B ...;
	SELECT fn_notify('my_signal'); );

CAVEAT: This rule always returns a tuple:
 fn_notify
-----------

(1 row)


-- 

---------------------------------------------------------------
Luis Carlos Ferreira                          lcf(at)tuxedo(dot)com(dot)ar 
Centro de C├│mputos            Junin 2957 - Santa Fe - Argentina
Sindicato de Luz y Fuerza              Tel.: (54)(342) 4520-075 
---------------------------------------------------------------

Estas loco?   come vaca!!


In response to

pgsql-sql by date

Next:From: Chris BowlbyDate: 2004-01-15 18:39:27
Subject: Problem with plpgsql function
Previous:From: Greg Sabino MullaneDate: 2004-01-15 12:47:39
Subject: Re: Atomic query and update of sequence generators

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group