Rules provide a powerful interface for interacting with the PostgreSQL query rewriting system. Thanks to rules, it is possible to transform a statement syntactically into another one or even into multiple ones. While this can also often be achieved by means of triggers, rules work at a syntactic level and are fired before any trigger. Of course, rules are not the universal solution to any problem and there are cases in which triggers are preferable and vice-versa. Knowing however how to handle statement rewriting is a valuable skill for any PostgreSQL server side developer.
In the next chapter, we will explain the PostgreSQL extension, which is a way to package your own code, including functions, procedures, triggers, tables, and so on, as well as manage its deployment and upgrade.