Exploring rules in PostgreSQL
As mentioned earlier, rules are simple event handlers. At the user level, it is possible to manage all the events that perform write operations, which are as follows:
INSERT
DELETE
UPDATE
The fundamental concept behind rules is to modify the flow of an event. If we are given an event, what we can do when certain conditions occur is as follows:
- Do nothing and then undo the action of that event.
- Trigger another event instead of the default one.
- Trigger another event in conjunction with the default.
So, given a write operation, for example, an INSERT
operation, we can perform one of these three actions:
- Cancel the operation.
- Perform another operation instead of the
INSERT
. - Execute the
INSERT
and, in the same transaction, perform another operation.
Understanding the OLD and NEW variables
Before we start working with rules and then with triggers, we need to...