Re: Inserting Data - Mailing list pgsql-general

From Tom Lane
Subject Re: Inserting Data
Date
Msg-id 25094.1156371501@sss.pgh.pa.us
Whole thread Raw
In response to Re: Inserting Data  (Bob Pawley <rjpawley@shaw.ca>)
List pgsql-general
Bob Pawley <rjpawley@shaw.ca> writes:
> Perhaps we can look at the following as a simple example of what is
> happening-

> ---------
> create or replace function loop_association() returns trigger as $$
>  begin

>  Insert Into p_id.loops (monitor)
>  select new.devices_id
>  from p_id.devices ;

>  return null ;
>  end ;
>  $$ language plpgsql ;

>  create trigger loop after insert on p_id.devices
>  for each row execute procedure loop_association();
> ------

> This trigger and procedure gives a single row on the first insert on an
> otherwise blank table. However it produces two identical rows of the second
> device_id on the second insert and three identical rows of the third
> device_id on the third insert. (This is the only trigger on the table)

Well, of course, because that's an unqualified "select", so each call
will copy *all* of p_id.devices into p_id.loops.  Methinks what you
really want is to insert the NEW row, not the whole table.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: CASCADING could not open relation with OID
Next
From: Bob Pawley
Date:
Subject: Re: Inserting Data