postgresql

Is it possible to update a view created from two views in PostgreSQL?


After searching about this issue, not sure if it is actually possible to do so, thanks for any pointers.

My view is created like this:

create or replace view OutputView as
select * from view1
natural full outer join view2;

Using:

update OutputView set colA = 'undefined' where colA is null;

gives:

ERROR:  cannot update view "OutputView"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.

No luck so far with that trigger and rule. The problem is that some columns in the output view have null values because those columns do not exist in the original views, for instance:

view1
-----
colA colB
1    a
2    b

view2
-----
colB colC
100  x
200  y

OutputView
----------
colA  colB  colC
1     a     null
2     b     null
null  100   x
null  200   y

Solution

  • The error explicitly states that the view is not updatable because it references multiple tables/views. Even if view1 and view2 are updatable themselves, combining them with a FULL OUTER JOIN makes the top-level view non-updatable. check:

    https://dba.stackexchange.com/questions/105957/how-do-i-make-this-postgres-view-that-performs-a-join-updatable