modifying a timestamp in a C trigger - Mailing list pgsql-hackers

From Louis-David Mitterrand
Subject modifying a timestamp in a C trigger
Date
Msg-id 20000814164733.A1765@styx
Whole thread Raw
Responses Re: modifying a timestamp in a C trigger
List pgsql-hackers
Hello,

I just finished a new C trigger that updated a "modified" column with
the current time upon an UPDATE event. It seems to work OK but I just
wanted to bounce this off you guys to check for some non-kosher stuff or
better way of doing it. Thanks in advance.

HeapTuple update_modified() {TupleDesc    tupdesc;HeapTuple    rettuple;bool isnull;TriggerData *trigdata =
CurrentTriggerData;
/* Get the current datetime. */Timestamp *tstamp = timestamp_in("now");Datum newdt = Float32GetDatum(tstamp);
CurrentTriggerData = NULL;
if (!trigdata)    elog(NOTICE, "bid_control.c: triggers are not initialized");
if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))    elog(ERROR, "bid_control.c: trigger should only be called on
INSERT");
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))    elog(ERROR, "bid_control.c: trigger should only be called BEFORE");
rettuple = trigdata->tg_trigtuple;tupdesc = trigdata->tg_relation->rd_att;
if ((i = SPI_connect()) < 0)    elog(NOTICE, "bid_control.c: SPI_connect returned %d", i);
i = SPI_fnumber(tupdesc, "modified");rettuple = SPI_modifytuple(        trigdata->tg_relation,        rettuple,
1,       &i,        &newdt,        NULL);
 
SPI_finish();return rettuple;
}

-- 
Louis-David Mitterrand - ldm@apartia.org - http://www.apartia.org

Lord, protect me from your followers.


pgsql-hackers by date:

Previous
From: Zeugswetter Andreas SB
Date:
Subject: AW: Optimizer confusion?
Next
From: Don Baccus
Date:
Subject: Re: modifying a timestamp in a C trigger