I'm using PostgreSQL. I have a table that I have partitioned into three sub-tables – and as far as I can tell, this all works correctly. Rows are correctly inserted into the appropriate partition and the master table remains empty.
My problem is that one of the triggers on the master table is no longer run on insert or update. Other triggers are run correctly and this trigger worked correctly prior to partitioning.
I suspected this might be similar to constraints and indexes and the trigger needed to be applied to child tables, however this doesn't work either.
Other triggers work, and if they are run alphabetically as the documentation suggests, all should be run before the partition-finding trigger.
The trigger that is causing me grief:
CREATE FUNCTION set_expiry_timestamp() RETURNS trigger AS $BODY$
BEGIN
-- Don't re-generate if the timeleft value has not changed
IF (TG_OP = 'UPDATE') THEN
IF OLD.expires IS NOT NULL AND OLD.timeleft = NEW.timeleft THEN
RETURN NEW;
END IF;
END IF;
IF (NEW.timeleft = 'SHORT') THEN
NEW.expires := NEW.updated + interval '30 minutes';
ELSIF (NEW.timeleft = 'MEDIUM') THEN
NEW.expires := NEW.updated + interval '2 hours';
ELSIF (NEW.timeleft = 'LONG') THEN
NEW.expires := NEW.updated + interval '12 hours';
ELSIF (NEW.timeleft = 'VERY_LONG') THEN
NEW.expires := NEW.updated + interval '48 hours';
ELSE
RAISE EXCEPTION 'timeleft was unrecognised. Expected SHORT, MEDIUM, LONG or VERY_LONG.';
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER set_expiry AFTER INSERT OR UPDATE ON auction FOR EACH ROW EXECUTE PROCEDURE set_expiry_timestamp()
Query expected to run the trigger:
INSERT INTO auction (created, timeleft) VALUES (NOW(), 'SHORT');
updated
is set in a BEFORE INSERT/UPDATE
trigger, however it still doesn't fire when updated is supplied.
Any suggestions?
Best Answer
The trigger is AFTER INSERT OR UPDATE. As you can see in your function, it ends with:
That means that the row is discarded and any changes to the row are void. Actually, the row returned from an
AFTER
trigger is discarded in any case. TheRETURN NULL
is just a reminder.You need to do this in a
BEFORE
trigger or issue an explicitUPDATE
statement. The second option is very tricky, however, because you could end up with an endless loop. Better make it aBEFORE
trigger.As an aside: the conditional expression could be simplified with a
CASE
statement (since version 8.4):