Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two things here. Firstly depending on activity levels in your database you may hit a race condition between checking for a record and inserting it where another process may create that record in the interim. The manual contains an example of how to do this <a href="http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE" rel="noreferrer">link example</a></p> <p>To avoid doing an update there is the suppress_redundant_updates_trigger() procedure. To use this as you wish you wold have to have two before update triggers the first will call the suppress_redundant_updates_trigger() to abort the update if no change made and the second to set the timestamp and username if the update is made. Triggers are fired in alphabetical order. Doing this would also mean changing the code in the example above to try the insert first before the update.</p> <p>Example of how suppress update works:</p> <pre><code> DROP TABLE sru_test; CREATE TABLE sru_test(id integer not null primary key, data text, updated timestamp(3)); CREATE TRIGGER z_min_update BEFORE UPDATE ON sru_test FOR EACH ROW EXECUTE PROCEDURE suppress_redundant_updates_trigger(); DROP FUNCTION set_updated(); CREATE FUNCTION set_updated() RETURNS TRIGGER AS $$ DECLARE BEGIN NEW.updated := now(); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER zz_set_updated BEFORE INSERT OR UPDATE ON sru_test FOR EACH ROW EXECUTE PROCEDURE set_updated(); insert into sru_test(id,data) VALUES (1,'Data 1'); insert into sru_test(id,data) VALUES (2,'Data 2'); select * from sru_test; update sru_test set data = 'NEW'; select * from sru_test; update sru_test set data = 'NEW'; select * from sru_test; update sru_test set data = 'ALTERED' where id = 1; select * from sru_test; update sru_test set data = 'NEW' where id = 2; select * from sru_test; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload