mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Make rewriter prevent auto-updates on views with conditional INSTEAD rules.
A view with conditional INSTEAD rules and no unconditional INSTEAD rules or INSTEAD OF triggers is not auto-updatable. Previously we relied on a check in the executor to catch this, but that's problematic since the planner may fail to properly handle such a query and thus return a particularly unhelpful error to the user, before reaching the executor check. Instead, trap this in the rewriter and report the correct error there. Doing so also allows us to include more useful error detail than the executor check can provide. This doesn't change the existing behaviour of updatable views; it merely ensures that useful error messages are reported when a view isn't updatable. Per report from Pengzhou Tang, though not adopting that suggested fix. Back-patch to all supported branches. Discussion: https://postgr.es/m/CAG4reAQn+4xB6xHJqWdtE0ve_WqJkdyCV4P=trYr4Kn8_3_PEA@mail.gmail.com
This commit is contained in:
@ -330,6 +330,27 @@ UPDATE ro_view20 SET b=upper(b);
|
||||
ERROR: cannot update view "ro_view20"
|
||||
DETAIL: Views that return set-returning functions are not automatically updatable.
|
||||
HINT: To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
|
||||
-- A view with a conditional INSTEAD rule but no unconditional INSTEAD rules
|
||||
-- or INSTEAD OF triggers should be non-updatable and generate useful error
|
||||
-- messages with appropriate detail
|
||||
CREATE RULE rw_view16_ins_rule AS ON INSERT TO rw_view16
|
||||
WHERE NEW.a > 0 DO INSTEAD INSERT INTO base_tbl VALUES (NEW.a, NEW.b);
|
||||
CREATE RULE rw_view16_upd_rule AS ON UPDATE TO rw_view16
|
||||
WHERE OLD.a > 0 DO INSTEAD UPDATE base_tbl SET b=NEW.b WHERE a=OLD.a;
|
||||
CREATE RULE rw_view16_del_rule AS ON DELETE TO rw_view16
|
||||
WHERE OLD.a > 0 DO INSTEAD DELETE FROM base_tbl WHERE a=OLD.a;
|
||||
INSERT INTO rw_view16 (a, b) VALUES (3, 'Row 3'); -- should fail
|
||||
ERROR: cannot insert into view "rw_view16"
|
||||
DETAIL: Views with conditional DO INSTEAD rules are not automatically updatable.
|
||||
HINT: To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
|
||||
UPDATE rw_view16 SET b='ROW 2' WHERE a=2; -- should fail
|
||||
ERROR: cannot update view "rw_view16"
|
||||
DETAIL: Views with conditional DO INSTEAD rules are not automatically updatable.
|
||||
HINT: To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
|
||||
DELETE FROM rw_view16 WHERE a=2; -- should fail
|
||||
ERROR: cannot delete from view "rw_view16"
|
||||
DETAIL: Views with conditional DO INSTEAD rules are not automatically updatable.
|
||||
HINT: To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
|
||||
DROP TABLE base_tbl CASCADE;
|
||||
NOTICE: drop cascades to 16 other objects
|
||||
DETAIL: drop cascades to view ro_view1
|
||||
|
@ -101,6 +101,20 @@ DELETE FROM ro_view18;
|
||||
UPDATE ro_view19 SET last_value=1000;
|
||||
UPDATE ro_view20 SET b=upper(b);
|
||||
|
||||
-- A view with a conditional INSTEAD rule but no unconditional INSTEAD rules
|
||||
-- or INSTEAD OF triggers should be non-updatable and generate useful error
|
||||
-- messages with appropriate detail
|
||||
CREATE RULE rw_view16_ins_rule AS ON INSERT TO rw_view16
|
||||
WHERE NEW.a > 0 DO INSTEAD INSERT INTO base_tbl VALUES (NEW.a, NEW.b);
|
||||
CREATE RULE rw_view16_upd_rule AS ON UPDATE TO rw_view16
|
||||
WHERE OLD.a > 0 DO INSTEAD UPDATE base_tbl SET b=NEW.b WHERE a=OLD.a;
|
||||
CREATE RULE rw_view16_del_rule AS ON DELETE TO rw_view16
|
||||
WHERE OLD.a > 0 DO INSTEAD DELETE FROM base_tbl WHERE a=OLD.a;
|
||||
|
||||
INSERT INTO rw_view16 (a, b) VALUES (3, 'Row 3'); -- should fail
|
||||
UPDATE rw_view16 SET b='ROW 2' WHERE a=2; -- should fail
|
||||
DELETE FROM rw_view16 WHERE a=2; -- should fail
|
||||
|
||||
DROP TABLE base_tbl CASCADE;
|
||||
DROP VIEW ro_view10, ro_view12, ro_view18;
|
||||
DROP SEQUENCE uv_seq CASCADE;
|
||||
|
Reference in New Issue
Block a user