postgresql_16:6.4.4 Rules on INSERT, UPDATE, and DELETE
PostgreSQL 16 > Server Programming > The Rule System > Rules on INSERT, UPDATE, and DELETE
Rules that are defined on INSERT, UPDATE, and DELETE are significantly different from the view rules described in the previous sections. First, their CREATE RULE command allows more: They are allowed to have no action. They can have multiple actions. They can be INSTEAD or ALSO (the default). The pseudorelations NEW and OLD become useful. They can have rule qualifications. Second, they don't modify the query tree in place. Instead they create zero or more new query trees and can throw away the original one.
Caution
In many cases, tasks that could be performed by rules on INSERT/UPDATE/DELETE are better done with triggers. Triggers are notationally a bit more complicated, but their semantics are much simpler to understand. Rules tend to have surprising results when the original query contains volatile functions: volatile functions may get executed more times than expected in the process of carrying out the rules.
>
Also, there are some cases that are not supported by these types of rules at all, notably including WITH clauses in the original query and multiple-assignment sub-SELECTs in the SET list of UPDATE queries. This is because copying these constructs into a rule query would result in multiple evaluations of the sub-query, contrary to the express intent of the query's author.
Subsections
- postgresql_16:6.4.4.1 How Update Rules Work
- postgresql_16:6.4.4.2 Cooperation with Views