Database Reference
In-Depth Information
How it works...
We've just scratched the surface of what you can achieve with rules, though personally I find
them too complex for widespread use.
You can do a lot of things with rules though, you need to make sure that everything you do
makes sense and has a practical purpose. There are some other important things that I
should mention about rules before you dive in and start using them everywhere.
Rules are applied by PostgreSQL after the SQL has been received by the server and parsed for
syntax errors, but before the planner tries to optimize the SQL statement.
In the rules in the preceding recipe, we reference the values of the old or the new row, just as
we do within trigger functions. Similarly, there are only new values in an INSERT and only old
values in a DELETE .
One of the big downsides of using rules is that you cannot bulk load data into the table using
the COPY command. We cannot transform a stream of inserts into a single COPY command,
nor can we do a COPY against the view. Bulk loading requires direct access to the table.
If we have a view like the following:
CREATE VIEW cust_minor AS
SELECT customerid
,firstname
,lastname
,age
FROM cust
WHERE age < 18;
then we have some more difficulties. If we wish to update this view, then you might read the
manual, and see we can use a conditional rule by adding a WHERE clause to match the WHERE
clause in the view as follows:
CREATE RULE cust_minor_update AS
ON update TO cust_minor
WHERE new.age < 18
DO INSTEAD
UPDATE cust SET
firstname = new.firstname
,lastname = new.lastname
,age = new.age
WHERE customerid = old.customerid;
 
Search WWH ::




Custom Search