Rules in PostgreSQL


We sometimes need to perform a different/alternative actions during database queries. We need it for data protection or data abstraction. The PostgreSQL rule system allows to define an alternative action on insert, update or delete. A rule generates an extra query. As a result, rule execution impacts the performance of the system.

Creating Rules

A rule can be created using the following syntax:

CREATE [ OR REPLACE ] RULE name AS ON event TO table_name [ WHERE condition ] DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command … ) }

CREATE OR REPLACE