WITH x(v) AS (VALUES (1),(2),(NULL))
SELECT l.v, r.v, l.v = r.v AS equality,
l.v IS NOT DISTINCT FROM r.v AS isnotdistinctfrom
FROM x l, x r;
Thanks to Sam Mason via the PostgreSQL - general mailing list.
Work-products and stray thoughts from the Land of Entrapment
WITH x(v) AS (VALUES (1),(2),(NULL))
SELECT l.v, r.v, l.v = r.v AS equality,
l.v IS NOT DISTINCT FROM r.v AS isnotdistinctfrom
FROM x l, x r;
create schema transtest;
set searchpath to transtest,public;
-- it's nice to have a sandbox
-- remember to reset searchpath when done
create table myents ( id serial primary key, sum integer);
-- the entries table
create table myrecs ( id serial primary key, entid integer references myents (id), cases integer);
-- the reports table, many reports per entry
create table myids ( id integer primary key references myents (id));
-- table used to store trigger state
-- function for when-clause (condition) in trigger
-- add id to the "to-do" list if it's not there already
-- return True only if it's been added to the least
create or replace function f_condition(myid integer) returns boolean as $_$
DECLARE
myret boolean := True;
newid integer;
BEGIN
RAISE NOTICE 'Return Value here is %', myret;
select count(*) into newid from myids where id = myid;
if newid > 0 then -- id is there, return False
myret := False;
else -- add id to queue, return True
insert into myids (id) values (myid);
end if;
return myret;
END;
$_$ language plpgsql;
-- trigger function to rebuild myent.sum for this myrecs.id
create or replace function f_refresh() returns trigger as $_$
DECLARE
mysum integer;
myid integer;
BEGIN
IF (TG_OP = 'DELETE') THEN
myid := OLD.entid;
ELSE
myid := NEW.entid;
END IF;
RAISE NOTICE 'Return Value here is %', myid;
select sum(cases) into mysum from myrecs where entid = myid;
-- compute entry sum from records and update it.
-- PL/pgSQL rules on queries are quirky. Read the docs.
update myents set sum = mysum where id = myid;
delete from myids where id = myid;
-- clean up
RETURN NULL;
END;
$_$ language plpgsql;
-- only fire trigger once per myent.id using when clause (condition)
-- eval is deferred until transaction concludes
create constraint trigger t_cache after insert or update
on myrecs initially deferred for each row
when ( f_condition(NEW.entid) ) -- comment out for pre pg 9.0
execute procedure f_refresh();
-- need 2, one for each for NEW (insert and update) and OLD (delete)
create constraint trigger t_cache_del after delete
on myrecs initially deferred for each row
when ( f_condition(OLD.entid) )
execute procedure f_refresh();
insert into myents (sum) VALUES ( 0);
begin;
insert into myrecs (entid, cases) VALUES ( 1, 0), (1, 1), (1,2);
select * from myents; -- 0
select * from myids; -- entid from above
commit;
select * from myents; -- new sum
select * from myids; -- empty