Here is an example.
First, create a test table:
CREATE TABLE test (a integer, b varchar, primary key (a));
Then we create a view:
CREATE OR REPLACE VIEW test_view AS SELECT * FROM test;
Next, an update rule is created:
CREATE OR REPLACE RULE rl_test_view_update AS ON UPDATE TO test_view DO INSTEAD UPDATE test SET a = NEW.a, b = NEW.b WHERE test.a = old.a AND test.b = old.b;
And finally, here is the insert rule:
CREATE OR REPLACE RULE rl_test_view_insert AS ON INSERT TO test_view DO INSTEAD INSERT INTO test VALUES (NEW.a, NEW.b) RETURNING test.*;
Now you can insert some test data:
INSERT INTO test_view (a, b) VALUES (1,'John Doe') RETURNING a;
and check the inserted tuples:
SELECT * FROM test_view;
source share