Re: insert trigger

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Roberto Benitez <RBENITEZ(at)houston(dot)rr(dot)com>
Cc: <pgsql-admin(at)postgresql(dot)org>
Subject: Re: insert trigger
Date: 2002-11-07 16:32:25
Message-ID: 20021107082731.R97075-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Mon, 4 Nov 2002, Roberto Benitez wrote:

> when a new record is inserted in to a psql table, i want to be
> able to update certain fields. the way i'm doing it right now..to
> find the last record inserted...is by sorting the primary key (serial)
> in descending order and taking the first item in the list...the one at
> the top should be the one i just entered...and of course, i then
> procede to update the fieldsd i need to update..

As was said, if you're modifying the same row you can use a before trigger
and directly change the values by assigning them. This happens before
the row is inserted so any changes made there are in the row that's
actually written. You can also make the system ignore some row by
returning NULL. Rules have a problem that you need to be careful to
set things up so that you don't end up with a recursive rule.

> So, basically would like to know if this will always work. will the
> one at the top (the one w/ the highest primary key [serial type])
> always be the one i just entered. AND does psql ALWAYS execute the
> trigger(s) after EVERY single record that is inerted. what would
> happen if [ n ] different users try to insert records simultaniously?
> will psql insert one-execute the trigger, insert the next-execute the
> trigger..and so on? or will it insert ALL [n] records AND THEN execute
> the trigger?
It may interleave them. However, I think in an after trigger, the value
of the key given to this row might be set, so you probably want to use
that in any case (esp since you can have a statement that inserts multiple
rows and you'll want to do each row individually).

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Bruno Wolff III 2002-11-07 16:33:34 Re: insert trigger
Previous Message Gareth Kirwan 2002-11-07 16:27:27 Re: how could I get the source table name and field name from a view field