Coalesce in PostgreSQL trigger does not fire on upddate

From: ALT SHN <i(dot)geografica(at)alt-shn(dot)org>
To: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Coalesce in PostgreSQL trigger does not fire on upddate
Date: 2020-02-07 20:15:39
Message-ID: CAGFOAzy+0w0=9DbfyVvP11-FiySqRtsepBe85fsjtr8X1W+Wsw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

(This question is also exposed here:
https://stackoverflow.com/questions/60117123/coalesce-in-postgresql-trigger-does-not-fire-on-upddate
)

Hi Everyone!

In the context of a database for a paleontological collection, I have this
table definition:

CREATE TABLE taxon (
id integer DEFAULT NEXTVAL('taxon_oid_seq') PRIMARY KEY,
taxon varchar(100) UNIQUE NOT NULL,
reino varchar(50) NOT NULL,
phylum varchar(100) ,
subphylum varchar(100) ,
classe varchar(100) ,
subclasse varchar(100) ,
superordem varchar(100) ,
ordem varchar(100) ,
subordem varchar(100) ,
infraordem varchar(100) ,
familia varchar(100) ,
subfamilia varchar(100) ,
genero varchar(100) ,
especie varchar(100) ,
subespecie varchar(100) );

The taxon field is to be automatically filled with the lowest level to
which it was possible to determine a given species taxonomy. In order to
achieve that I have this trigger:

CREATE OR REPLACE FUNCTION get_taxon() RETURNS TRIGGER LANGUAGE
plpgsql AS $BODY$ BEGIN
NEW.taxon := coalesce(NEW.subespecie, NEW.especie, NEW.genero, NEW.subfamilia,
NEW.familia, NEW.infraordem, NEW.subordem,
NEW.ordem, NEW.superordem,
NEW.subclasse, NEW.classe, NEW.subphylum,
NEW.phylum, NEW.reino); RETURN NEW; END; $BODY$
VOLATILE;
CREATE TRIGGER update_taxon
BEFORE INSERT OR UPDATE ON taxonFOR EACH ROW EXECUTE PROCEDURE get_taxon();

However this trigger only fires on INSERT, nothing happens if an UPDATE is
made. How can have this trigger also firing in the case of an UPDATE?

Thanks,
Andre

--
---------------------------------------------------------------

*Sociedade de História Natural*
Departamento de Informação Geográfica
Polígono Industrial do Alto do Amial
Pav.H02 e H06

i(dot)geografica(at)alt-shn(dot)org <laboratorio(at)alt-shn(dot)org>
www.shn.pt
www.alt-shn.blogspot.com
Facebook <https://www.facebook.com/SociedadeDeHistoriaNatural?ref=hl>

TLM: 964138188

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Christian Barthel 2020-02-08 07:53:39 Re: Coalesce in PostgreSQL trigger does not fire on upddate
Previous Message Adam Middleton 2020-02-07 17:37:52 Southern California 2020 Linux Expo Emails