Chapter 36. The Rule System

Table of Contents
36.1. The Query Tree
36.2. Views and the Rule System
36.2.1. How SELECT Rules Work
36.2.2. View Rules in Non-SELECT Statements
36.2.3. The Power of Views in PostgreSQL
36.2.4. Updating a View
36.3. Rules on INSERT, UPDATE, and DELETE
36.3.1. How Update Rules Work
36.3.2. Cooperation with Views
36.4. Rules and Privileges
36.5. Rules and Command Status
36.6. Rules Versus Triggers

Note: The following description applies both to Postgres-XC and PostgreSQL if not described explicitly.

This chapter discusses the rule system in PostgreSQL. Production rule systems are conceptually simple, but there are many subtle points involved in actually using them.

Some other database systems define active database rules, which are usually stored procedures and triggers. In PostgreSQL, these can be implemented using functions and triggers as well.

The rule system (more precisely speaking, the query rewrite rule system) is totally different from stored procedures and triggers. It modifies queries to take rules into consideration, and then passes the modified query to the query planner for planning and execution. It is very powerful, and can be used for many things such as query language procedures, views, and versions. The theoretical foundations and the power of this rule system are also discussed in On Rules, Procedures, Caching and Views in Database Systems and A Unified Framework for Version Modeling Using Production Rules in a Database System.