To understand how the rule system works it is necessary to know
when it is invoked and what its input and results are. The rule
system is located between the parser and the planner. It takes
the output of the parser, one query tree, and the user-defined
rewrite rules, which are also query trees with some extra
information, and creates zero or more query trees as result. So
its input and output are always things the parser itself could
have produced and thus, anything it sees is basically
representable as an SQL statement. Now what is a query tree? It
is an internal representation of an SQL statement where the
single parts that it is built from are stored separately. These
query trees can be shown in the server log if you set the
configuration parameters debug_print_parse,
debug_print_rewritten, or debug_print_plan. The rule actions are
also stored as query trees, in the system catalog pg_rewrite.
They are not formatted like the log output, but they contain
exactly the same information. Reading a raw query tre