A standard delete from foo_history where id = 123;
returns the text:
DELETE 1
I have created a trigger procedure to replace the delete trigger, and instead of the delete, set an archive flag:
CREATE FUNCTION archive()
RETURNS trigger AS $$
DECLARE
command text := '" SET timeEnd = current_timestamp WHERE id = $1';
BEGIN
EXECUTE 'UPDATE "' || TG_TABLE_NAME || command USING OLD.id;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Then created a view for the foo_history
table called foo
:
CREATE VIEW foo AS
SELECT id, timeStart from foo_history WHERE timeEnd IS NULL;
And added a trigger to the view:
CREATE TRIGGER foo_archive INSTEAD OF DELETE ON foo FOR EACH ROW EXECUTE PROCEDURE archive();
In other words, when deleting a foo
record , set the timeEnd
column instead - this way, the foo
view only shows records without timeEnd
set.
This works well:
delete from foo where id = 123
Does indeed set the timeEnd
column. However, the output from this is:
DELETE 0
Whereas I really wanted it to say:
DELETE 1
Being an SQL novice and Postgres newbie I'm not sure how to change the archive function to do this.
I did try changing it to:
RETURN 1
But I get an error about I need to return a composite (whatever that means).