Bạn đã rất gần;) bạn chỉ cần cập nhật NEW.id
trong trình kích hoạt của bạn.
CREATE FUNCTION t0ii_person_details() RETURNS trigger AS $$
DECLARE
addr integer;
BEGIN
-- Want at least a town to insert anything into table address.
IF (NEW.town IS NOT NULL) THEN
INSERT INTO address(street, town)
VALUES (NEW.street, NEW.town)
RETURNING id INTO addr;
ELSE
addr := NULL;
END IF;
INSERT INTO person(first_name, family_name, address)
VALUES (NEW.first_name, NEW.family_name, addr)
-- here is the only change I made
RETURNING id INTO NEW.id;
RETURN NEW;
END; $$ LANGUAGE plpgsql SECURITY DEFINER;