PostgreSQL 11 Administration Cookbook
by Simon Riggs, Gianni Ciolli, Sudheer Kumar Meesala, Sheldon Strauch
How it works…
In the rule-based example, we use the COALESCE function, which returns the first argument, if not null, or the second one otherwise. When the original INSERT statement does not specify a value in meters, then it uses the value in feet divided by 0.3048.
The second rule sets the value in meters to different expressions—if the value in meters was updated, we use the new one, if the value in feet was updated, we use the new value in feet divided by 0.3048, and otherwise we use the old value in meters (that is, we don't change it).
The logic implemented in the trigger function is similar to the previous one; note that we use the TG_OP automatic variable to handle INSERT and UPDATE separately.
We've just scratched the surface of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access