CREATE FUNCTION test_update() RETURNS trigger AS $$
begin
update testtable set testtable.updated = now() where id = old.id;
return new;
end;
$$ LANGUAGE plpgsql;
Main Topics
Browse All TopicsCan somebody give me a simple example of a trigger function that uses a parameter in Postgresql. In a table I have a column 'updated'. When a record is updated, the updated-value for that row should automatically get a new timestamp. For some reason the code below doesn't work. I guess because of the parameter.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The problem I have has to do with PG8.3, which doesn't allow implicit casting. I'm not sure if I'm passing the id correctly.
ERROR: invalid input syntax for integer: "id"
SQL status:22P02
Context:SQL statement "update testtable set updated = now() where id = CAST ( $1 as integer)"
PL/pgSQL function "test_update" line 2 at SQL statement
~]$ psql treacle;
Welcome to psql 8.2.4, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
treacle=> CREATE TABLE testtable
treacle-> (
treacle(> id serial NOT NULL,
treacle(> label text,
treacle(> created timestamp with time zone DEFAULT now(),
treacle(> updated timestamp with time zone DEFAULT now(),
treacle(> deleted boolean,
treacle(> description text
treacle(> )
treacle-> WITH (OIDS=FALSE);
NOTICE: CREATE TABLE will create implicit sequence "testtable_id_seq" for serial column "testtable.id"
CREATE TABLE
treacle=> CREATE FUNCTION test_update() RETURNS trigger AS $$
treacle$> begin
treacle$> new.updated := now();
treacle$> return new;
treacle$> end
treacle$> $$ LANGUAGE plpgsql;
CREATE FUNCTION
treacle=> CREATE TRIGGER test_update_trigger
treacle-> BEFORE UPDATE
treacle-> ON testtable
treacle-> FOR EACH ROW
treacle-> EXECUTE PROCEDURE test_update();
CREATE TRIGGER
treacle=> insert into testtable values ( default, 'First', default, default, false, ' Record' );
INSERT 0 1
treacle=> select * from testtable;
id | label | created | updated | deleted | description
----+-------+-------------
1 | First | 2009-04-24 07:04:06.0572+01 | 2009-04-24 07:04:06.0572+01 | f | Record
(1 row)
treacle=> update testtable set description = ' Hello World second time!';
UPDATE 1
treacle=> select * from testtable;
id | label | created | updated | deleted | description
----+-------+-------------
1 | First | 2009-04-24 07:04:06.0572+01 | 2009-04-24 07:07:08.139084+01 | f | Hello World second time!
(1 row)
Business Accounts
Answer for Membership
by: tcs224694Posted on 2009-04-22 at 08:38:48ID: 24206109
update testtable set updated = now()+timezone where id=TG_ARGV[0];